Handle inputs/outputs and nulls correctly.

This commit is contained in:
kngako
2024-08-07 02:48:25 +02:00
parent 23739e13a8
commit 86d7d9835f
6 changed files with 123 additions and 62 deletions

View File

@@ -271,7 +271,7 @@ public interface Secp256k1 {
*/
public fun musigPartialSigAgg(session: ByteArray, psigs: Array<ByteArray>): ByteArray
public fun frostSharesGen(pok64: ByteArray, seed32: ByteArray, threshold: Int, totalSigners: Int, ids33: Array<ByteArray>): Pair<Array<ByteArray>,Array<ByteArray>>
public fun frostSharesGen(seed32: ByteArray, threshold: Int, totalSigners: Int, ids33: Array<ByteArray>): Triple<Array<ByteArray>,Array<ByteArray>, ByteArray>
/**
*
@@ -291,21 +291,21 @@ public interface Secp256k1 {
public fun frostPublicKeyTweak(xOnlyPublicKey: ByteArray): ByteArray
public fun frostPublicKeyEcTweakAdd(tweakCache: ByteArray, tweak32: ByteArray): ByteArray
public fun frostPublicKeyEcTweakAdd(tweakCache: ByteArray, tweak32: ByteArray): ByteArray?
public fun frostPublicKeyXonlyTweakAdd(tweakCache: ByteArray, tweak32: ByteArray): Pair<ByteArray, ByteArray>
public fun frostPublicKeyXonlyTweakAdd(tweakCache: ByteArray, tweak32: ByteArray): Pair<ByteArray?, ByteArray>
public fun frostNonceGen(sessionId32: ByteArray, share: ByteArray, msg32: ByteArray, publicKey: ByteArray, extraInput32: ByteArray?): Pair<ByteArray, ByteArray>
public fun frostNonceGen(sessionId32: ByteArray, share: ByteArray?, msg32: ByteArray?, publicKey: ByteArray?, extraInput32: ByteArray?): Pair<ByteArray, ByteArray>
/**
*
* threshold can be deduced from the size of the pubnonces array.
*/
public fun frostNonceProcess(publicNonces: Array<ByteArray>, msg32: ByteArray, publicKey: ByteArray, id33: ByteArray, ids33: Array<ByteArray>, tweakCache: ByteArray, adaptor: ByteArray?): ByteArray
public fun frostNonceProcess(publicNonces: Array<ByteArray>, msg32: ByteArray, publicKey: ByteArray, id33: ByteArray, ids33: Array<ByteArray>, tweakCache: ByteArray?, adaptor: ByteArray?): ByteArray
public fun frostPartialSign(secnonce: ByteArray, share: ByteArray, session: ByteArray, tweakCache: ByteArray): ByteArray
public fun frostPartialSign(secnonce: ByteArray, share: ByteArray, session: ByteArray, tweakCache: ByteArray?): ByteArray
public fun frostPartialSignatureVerify(partialSig: ByteArray, publicNonce: ByteArray, publicShare: ByteArray, session: ByteArray, tweakCache: ByteArray): Int
public fun frostPartialSignatureVerify(partialSig: ByteArray, publicNonce: ByteArray, publicShare: ByteArray, session: ByteArray, tweakCache: ByteArray?): Int
public fun frostPartialSignatureAggregate(session: ByteArray, partialSignatures: Array<ByteArray>): ByteArray

View File

@@ -484,13 +484,11 @@ public object Secp256k1Native : Secp256k1 {
}
override fun frostSharesGen(
pok64: ByteArray,
seed32: ByteArray,
threshold: Int,
totalSigners: Int,
ids33: Array<ByteArray>
): Pair<Array<ByteArray>, Array<ByteArray>> {
require(pok64.size == 64)
): Triple<Array<ByteArray>, Array<ByteArray>, ByteArray> {
require(seed32.size == 32)
require(threshold > 0)
require(threshold <= totalSigners)
@@ -500,6 +498,7 @@ public object Secp256k1Native : Secp256k1 {
memScoped {
val nShares = allocArray<secp256k1_frost_share>(ids33.size)
val nVssCommitment = allocArray<secp256k1_pubkey>(threshold)
val pok64 = ByteArray(64)
val nIds33s = ids33.map { toNat(it) }
@@ -514,9 +513,10 @@ public object Secp256k1Native : Secp256k1 {
ids33 = nIds33s.toCValues()
)
return Pair(
return Triple(
ids33.indices.map { serializeFrostShare(nShares[it]) }.toTypedArray(),
(0 until threshold).map { serializePubkey(nVssCommitment[it]) }.toTypedArray()
(0 until threshold).map { serializePubkey(nVssCommitment[it]) }.toTypedArray(),
pok64
)
}
}
@@ -696,7 +696,7 @@ public object Secp256k1Native : Secp256k1 {
return natOutput
}
override fun frostPublicKeyXonlyTweakAdd(tweakCache: ByteArray, tweak32: ByteArray): Pair<ByteArray, ByteArray> {
override fun frostPublicKeyXonlyTweakAdd(tweakCache: ByteArray, tweak32: ByteArray): Pair<ByteArray?, ByteArray> {
require(tweakCache.size == Secp256k1.FROST_TWEAK_CACHE_SIZE)
require(tweak32.size == 32)
@@ -735,15 +735,21 @@ public object Secp256k1Native : Secp256k1 {
override fun frostNonceGen(
sessionId32: ByteArray,
share: ByteArray,
msg32: ByteArray,
publicKey: ByteArray,
share: ByteArray?,
msg32: ByteArray?,
publicKey: ByteArray?,
extraInput32: ByteArray?
): Pair<ByteArray, ByteArray> {
require(sessionId32.size == 32)
require(share.size == Secp256k1.FROST_SHARE_SIZE)
require(msg32.size == 33)
require(publicKey.size == 33 || publicKey.size == 65)
share?.let {
require(share.size == Secp256k1.FROST_SHARE_SIZE)
}
msg32?.let {
require(msg32.size == 33)
}
publicKey?.let {
require(publicKey.size == 33 || publicKey.size == 65)
}
extraInput32?.let {
require(it.size == 33)
}
@@ -752,8 +758,8 @@ public object Secp256k1Native : Secp256k1 {
val nFrostSecnonce = alloc<secp256k1_frost_secnonce>()
val nPublicNonce = alloc<secp256k1_frost_pubnonce>()
val nShare = allocFrostShare(share)
val nPublicKey = allocXonlyPublicKey(publicKey)
val nShare = share?.let { allocFrostShare(it) }
val nPublicKey = publicKey?.let { allocXonlyPublicKey(it) }
val nExtraInput32 = extraInput32?.let {
toNat(it)
}
@@ -763,9 +769,9 @@ public object Secp256k1Native : Secp256k1 {
secnonce = nFrostSecnonce.ptr,
pubnonce = nPublicNonce.ptr,
session_id32 = toNat(sessionId32),
agg_share = nShare.ptr,
msg32 = toNat(msg32),
agg_pk = nPublicKey.ptr,
agg_share = nShare?.ptr,
msg32 = msg32?.let { toNat(it) },
agg_pk = nPublicKey?.ptr,
extra_input32 = nExtraInput32
)
@@ -788,7 +794,7 @@ public object Secp256k1Native : Secp256k1 {
publicKey: ByteArray,
id33: ByteArray,
ids33: Array<ByteArray>,
tweakCache: ByteArray,
tweakCache: ByteArray?,
adaptor: ByteArray?
): ByteArray {
publicNonces.forEach { publicNonce ->
@@ -799,7 +805,9 @@ public object Secp256k1Native : Secp256k1 {
ids33.forEach {
require(it.size == 33)
}
require(tweakCache.size == Secp256k1.FROST_TWEAK_CACHE_SIZE)
tweakCache?.let {
require(tweakCache.size == Secp256k1.FROST_TWEAK_CACHE_SIZE)
}
adaptor?.let {
require(it.size == 33 || it.size == 65)
}
@@ -813,8 +821,11 @@ public object Secp256k1Native : Secp256k1 {
val nIds33 = ids33.map { toNat(it) }
val nTweakCache = alloc<secp256k1_frost_tweak_cache>()
memcpy(nTweakCache.ptr, toNat(tweakCache), Secp256k1.FROST_TWEAK_CACHE_SIZE.toULong())
val nTweakCache = tweakCache?.let {
alloc<secp256k1_frost_tweak_cache>()
}?.also { nTweakCache ->
memcpy(nTweakCache.ptr, toNat(tweakCache) , Secp256k1.FROST_TWEAK_CACHE_SIZE.toULong())
}
val nAdaptor = adaptor?.let {
allocPublicKey(it).ptr
@@ -828,7 +839,7 @@ public object Secp256k1Native : Secp256k1 {
agg_pk = nPublicKey.ptr,
my_id33 = toNat(id33),
ids33 = nIds33.toCValues(),
tweak_cache = nTweakCache.ptr,
tweak_cache = nTweakCache?.ptr,
adaptor = nAdaptor
)
@@ -850,12 +861,15 @@ public object Secp256k1Native : Secp256k1 {
secnonce: ByteArray,
share: ByteArray,
session: ByteArray,
tweakCache: ByteArray
tweakCache: ByteArray?
): ByteArray {
require(secnonce.size == Secp256k1.FROST_SECNONCE_SIZE)
require(share.size == Secp256k1.FROST_SHARE_SIZE)
require(session.size == Secp256k1.FROST_SESSION_SIZE)
require(tweakCache.size == Secp256k1.FROST_TWEAK_CACHE_SIZE)
tweakCache?.let {
require(tweakCache.size == Secp256k1.FROST_TWEAK_CACHE_SIZE)
}
memScoped {
val nPartialSignature = alloc<secp256k1_frost_partial_sig>();
@@ -868,8 +882,12 @@ public object Secp256k1Native : Secp256k1 {
val nSession = alloc<secp256k1_frost_session>()
memcpy(nSession.ptr, toNat(session), Secp256k1.FROST_SESSION_SIZE.toULong())
val nTweakCache = alloc<secp256k1_frost_tweak_cache>()
memcpy(nTweakCache.ptr, toNat(tweakCache), Secp256k1.FROST_TWEAK_CACHE_SIZE.toULong())
val nTweakCache = tweakCache?.let {
alloc<secp256k1_frost_tweak_cache>()
}?.also { nTweakCache ->
memcpy(nTweakCache.ptr, toNat(tweakCache) , Secp256k1.FROST_TWEAK_CACHE_SIZE.toULong())
}
secp256k1_frost_partial_sign(
ctx,
@@ -877,7 +895,7 @@ public object Secp256k1Native : Secp256k1 {
nSecnonce.ptr,
nShare.ptr,
nSession.ptr,
nTweakCache.ptr
nTweakCache?.ptr
)
return serializeFrostPartialSignature(nPartialSignature)
@@ -889,13 +907,16 @@ public object Secp256k1Native : Secp256k1 {
publicNonce: ByteArray,
publicShare: ByteArray,
session: ByteArray,
tweakCache: ByteArray
tweakCache: ByteArray?
): Int {
require(partialSig.size == 32)
require(publicNonce.size == Secp256k1.MUSIG2_PUBLIC_NONCE_SIZE)
require(publicShare.size == 33 || publicShare.size == 65)
require(session.size == Secp256k1.FROST_SESSION_SIZE)
require(tweakCache.size == Secp256k1.FROST_TWEAK_CACHE_SIZE)
tweakCache?.let {
require(tweakCache.size == Secp256k1.FROST_TWEAK_CACHE_SIZE)
}
memScoped {
val nPartialSignature = allocFrostPartialSignature(partialSig)
@@ -903,8 +924,11 @@ public object Secp256k1Native : Secp256k1 {
val nPublicShare = allocPublicKey(publicShare)
val nSession = alloc<secp256k1_frost_session>()
memcpy(nSession.ptr, toNat(session), Secp256k1.FROST_SESSION_SIZE.toULong())
val nTweakCache = alloc<secp256k1_frost_tweak_cache>()
memcpy(nTweakCache.ptr, toNat(tweakCache), Secp256k1.FROST_TWEAK_CACHE_SIZE.toULong())
val nTweakCache = tweakCache?.let {
alloc<secp256k1_frost_tweak_cache>()
}?.also { nTweakCache ->
memcpy(nTweakCache.ptr, toNat(tweakCache) , Secp256k1.FROST_TWEAK_CACHE_SIZE.toULong())
}
return secp256k1_frost_partial_sig_verify(
ctx,
@@ -912,7 +936,7 @@ public object Secp256k1Native : Secp256k1 {
nPublicNonce.ptr,
nPublicShare.ptr,
nSession.ptr,
nTweakCache.ptr
nTweakCache?.ptr
)
}
}