diff --git a/src/nativeMain/kotlin/fr/acinq/secp256k1/Secp256k1Native.kt b/src/nativeMain/kotlin/fr/acinq/secp256k1/Secp256k1Native.kt index cf36d42..18b0d41 100644 --- a/src/nativeMain/kotlin/fr/acinq/secp256k1/Secp256k1Native.kt +++ b/src/nativeMain/kotlin/fr/acinq/secp256k1/Secp256k1Native.kt @@ -1,5 +1,6 @@ package fr.acinq.secp256k1 +import fr.acinq.secp256k1.Secp256k1Native.toNat import kotlinx.cinterop.* import kotlinx.cinterop.ptr import platform.posix.memcpy @@ -554,12 +555,17 @@ public object Secp256k1Native : Secp256k1 { val nTotalShares = totalShares.map { allocFrostShare(it).ptr } val nVssCommitments = allocArray>(vssCommitments.size) - vssCommitments.forEachIndexed { index, vssCommitment -> - nVssCommitments[index] = allocArrayOf( - vssCommitment.map { bytes -> - allocPublicKey(bytes).ptr + + vssCommitments.forEachIndexed { index, commitments -> + val pubkeyArray = allocArray(commitments.size) + commitments.forEachIndexed { commitmentIndex, pubkeyData -> + pubkeyData.usePinned { pinned -> + if (secp256k1_ec_pubkey_parse(ctx, pubkeyArray[commitmentIndex].ptr, toNat(pinned.get()), pubkeyData.size.convert()) == 0) { + error("Failed to parse public key") + } } - ).reinterpret() + } + nVssCommitments[index] = pubkeyArray } val result = secp256k1_frost_share_agg( @@ -598,16 +604,24 @@ public object Secp256k1Native : Secp256k1 { } memScoped { - val nId33 = toNat(id33); val nFrostShare = allocFrostShare(share) - val nVssCommitment = vssCommitment.map { allocPublicKey(it).ptr } +// val nVssCommitment = vssCommitment.map { allocPublicKey(it).ptr }.toCValues() + + val nVssCommitment = allocArray>(vssCommitment.size) + vssCommitment.forEachIndexed { index, pubkeyData -> + pubkeyData.usePinned { pinned -> + if (secp256k1_ec_pubkey_parse(ctx, nVssCommitment[index], toNat(pinned.get()), pubkeyData.size.convert()) == 0) { + error("Failed to parse public key") + } + } + } return secp256k1_frost_share_verify( ctx = ctx, - threshold = threshold.convert(), - id33 = nId33, + threshold = vssCommitment.size.convert(), + id33 = toNat(id33), share = nFrostShare.ptr, - vss_commitment = nVssCommitment.toCValues() + vss_commitment = nVssCommitment ) } } @@ -634,15 +648,20 @@ public object Secp256k1Native : Secp256k1 { val nPublicShare = alloc() val nVssCommitments = allocArray>(vssCommitments.size) - vssCommitments.forEachIndexed { index, vssCommitment -> - nVssCommitments[index] = allocArrayOf( - vssCommitment.map { bytes -> - allocPublicKey(bytes).ptr + + vssCommitments.forEachIndexed { index, commitments -> + val pubkeyArray = allocArray(commitments.size) + commitments.forEachIndexed { commitmentIndex, pubkeyData -> + pubkeyData.usePinned { pinned -> + if (secp256k1_ec_pubkey_parse(ctx, pubkeyArray[commitmentIndex].ptr, toNat(pinned.get()), pubkeyData.size.convert()) == 0) { + error("Failed to parse public key") + } } - ).reinterpret() + } + nVssCommitments[index] = pubkeyArray } - secp256k1_frost_compute_pubshare( + val result = secp256k1_frost_compute_pubshare( ctx = ctx, pubshare = nPublicShare.ptr, threshold = threshold.convert(), @@ -651,6 +670,8 @@ public object Secp256k1Native : Secp256k1 { n_participants = totalSignersCount.convert() ) + println("Compute pubshare result: $result") + return serializePubkey(nPublicShare) } } diff --git a/tests/src/commonTest/kotlin/fr/acinq/secp256k1/FrostTest.kt b/tests/src/commonTest/kotlin/fr/acinq/secp256k1/FrostTest.kt index 568894d..179bc9d 100644 --- a/tests/src/commonTest/kotlin/fr/acinq/secp256k1/FrostTest.kt +++ b/tests/src/commonTest/kotlin/fr/acinq/secp256k1/FrostTest.kt @@ -150,7 +150,6 @@ class FrostTest: BaseTest() { val expected = tests.jsonObject["expected"]!!.jsonArray[index]; val expectedAggregateShare = expected.jsonObject["aggregate_share"]!!.jsonPrimitive.content - val expectedPublicShare = expected.jsonObject["public_share"]!!.jsonPrimitive.content assertEquals( expected = expectedAggregateShare, @@ -162,30 +161,6 @@ class FrostTest: BaseTest() { actual = Hex.encode(result.second), "Unexpected $index:aggregate_public_key" ) - - assertEquals( - expected = 1, - actual = Secp256k1.frostShareVerify( - threshold, - ids33[index], - assignedShares[index], - vssCommitments[index] - ), - message = "Couldn't verify share from $index signer" - ) - - assertEquals( - expected = expectedPublicShare, - actual = Hex.encode( - Secp256k1.frostComputePublicShare( - threshold, - ids33[index], - vssCommitments.toTypedArray(), - nParticipants - ) - ), - message = "Couldn't verify share from $index signer" - ) } } @@ -193,15 +168,12 @@ class FrostTest: BaseTest() { @Test fun `frost share verify`() { val shareGenTests = readData("frost/share_gen_vectors.json") - val tests = readData("frost/share_agg_vectors.json") - val expectedAggregatePublicKey = tests.jsonObject["aggregate_public_key"]!!.jsonPrimitive.content val publicKeys = shareGenTests.jsonObject["pubkeys"]!!.jsonArray.map { Hex.decode(it.jsonPrimitive.content) } val signerShareGenTestCase = shareGenTests.jsonObject["valid_signers_share_gen_test_case"]!!; val keyIndices = signerShareGenTestCase.jsonObject["key_indices"]!!.jsonArray.map { it.jsonPrimitive.int } - val nParticipants = keyIndices.size val threshold = signerShareGenTestCase.jsonObject["threshold"]!!.jsonPrimitive.int val ids33 = keyIndices.map { publicKeys[it] }.toTypedArray() @@ -218,30 +190,6 @@ class FrostTest: BaseTest() { ) } - val result = Secp256k1.frostShareAggregate( - assignedShares.toTypedArray(), - vssCommitments.toTypedArray(), - nParticipants, - threshold, - ids33[index] - ) - - val expected = tests.jsonObject["expected"]!!.jsonArray[index]; - - val expectedAggregateShare = expected.jsonObject["aggregate_share"]!!.jsonPrimitive.content - val expectedPublicShare = expected.jsonObject["public_share"]!!.jsonPrimitive.content - - assertEquals( - expected = expectedAggregateShare, - actual = Hex.encode(result.first), - "Unexpected $index:aggregate_share" - ) - assertEquals( - expected = expectedAggregatePublicKey, - actual = Hex.encode(result.second), - "Unexpected $index:aggregate_public_key" - ) - assertEquals( expected = 1, actual = Secp256k1.frostShareVerify( @@ -252,19 +200,6 @@ class FrostTest: BaseTest() { ), message = "Couldn't verify share from $index signer" ) - - assertEquals( - expected = expectedPublicShare, - actual = Hex.encode( - Secp256k1.frostComputePublicShare( - threshold, - ids33[index], - vssCommitments.toTypedArray(), - nParticipants - ) - ), - message = "Couldn't verify share from $index signer" - ) } } diff --git a/tests/src/commonTest/resources/frost/share_gen_vectors.json b/tests/src/commonTest/resources/frost/share_gen_vectors.json index 68c94f1..86b8b26 100644 --- a/tests/src/commonTest/resources/frost/share_gen_vectors.json +++ b/tests/src/commonTest/resources/frost/share_gen_vectors.json @@ -19,7 +19,6 @@ "bf0eaac669eac6ac43d094bb2e07e4fa7fd4b1d317188c690aad7ea211b49bdb", "00c266074c34720f6d9a8511e4ec82bed44e104f93f20d9bbfbff8e2edf44400" ], - "vss_commitment": [ "04bc2f60d5a7494d506e6517c49db2104b05e087536ccb1cb2730282f469782bb93e2c0029d733beeea75120e831ed71255adde4ddbd0be049419572502d7b73b9", "04ced2029d64827253175b5382cb327123fd2cdcdb5b2092e66020e9b6ece639f675029e36604347735eef9bf64137474b14d92d2996e67f5721705ee574c916a1",