Fix for nVssCommitments declarations

This commit is contained in:
kngako 2024-08-22 10:47:18 +02:00
parent 64e15574db
commit 8cacf80491
3 changed files with 37 additions and 82 deletions

View File

@ -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<CPointerVar<secp256k1_pubkey>>(vssCommitments.size)
vssCommitments.forEachIndexed { index, vssCommitment ->
nVssCommitments[index] = allocArrayOf(
vssCommitment.map { bytes ->
allocPublicKey(bytes).ptr
vssCommitments.forEachIndexed { index, commitments ->
val pubkeyArray = allocArray<secp256k1_pubkey>(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<CPointerVar<secp256k1_pubkey>>(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<secp256k1_pubkey>()
val nVssCommitments = allocArray<CPointerVar<secp256k1_pubkey>>(vssCommitments.size)
vssCommitments.forEachIndexed { index, vssCommitment ->
nVssCommitments[index] = allocArrayOf(
vssCommitment.map { bytes ->
allocPublicKey(bytes).ptr
vssCommitments.forEachIndexed { index, commitments ->
val pubkeyArray = allocArray<secp256k1_pubkey>(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)
}
}

View File

@ -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"
)
}
}

View File

@ -19,7 +19,6 @@
"bf0eaac669eac6ac43d094bb2e07e4fa7fd4b1d317188c690aad7ea211b49bdb",
"00c266074c34720f6d9a8511e4ec82bed44e104f93f20d9bbfbff8e2edf44400"
],
"vss_commitment": [
"04bc2f60d5a7494d506e6517c49db2104b05e087536ccb1cb2730282f469782bb93e2c0029d733beeea75120e831ed71255adde4ddbd0be049419572502d7b73b9",
"04ced2029d64827253175b5382cb327123fd2cdcdb5b2092e66020e9b6ece639f675029e36604347735eef9bf64137474b14d92d2996e67f5721705ee574c916a1",