diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationFixture.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationFixture.kt index 6c28d486..89139677 100644 --- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationFixture.kt +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationFixture.kt @@ -64,4 +64,13 @@ object ConfederationFixture { /** Groups 0 and 2, two admins of each: one of the smallest coalitions the roster accepts. */ val selection = ConfederationSignerSelection(mapOf(0u to setOf(0u, 2u), 2u to setOf(1u, 2u))) + /** + * Nonce randomness for [selection]'s signers, one secret each, distinct per [attempt]. Fixed + * so a failure reproduces; a real signer draws it fresh, and never from public data. + */ + fun nonces(attempt: Int, selection: ConfederationSignerSelection = this.selection): Map = + selection.signers.associateWith { role -> + ByteVector32(ByteArray(32).also { it[0] = 0x4d; it[1] = attempt.toByte(); it[30] = role.groupId.toByte(); it[31] = role.adminId.toByte() }) + } + } diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationReachableJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationReachableJvmTest.kt index 51ac3a2b..136c1712 100644 --- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationReachableJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationReachableJvmTest.kt @@ -1,9 +1,16 @@ package mobi.giza.compose +import fr.acinq.bitcoin.ByteVector import fr.acinq.bitcoin.ByteVector32 import fr.acinq.bitcoin.Crypto import fr.acinq.bitcoin.crypto.confederation.Confederation +import fr.acinq.bitcoin.crypto.confederation.ConfederationBroadcast +import fr.acinq.bitcoin.crypto.confederation.ConfederationDkg +import fr.acinq.bitcoin.crypto.confederation.ConfederationSignerSelection +import fr.acinq.bitcoin.crypto.confederation.ConfederationSigning +import fr.acinq.bitcoin.crypto.confederation.ConfederationSigningSession import fr.acinq.bitcoin.crypto.confederation.ConfederationTweak +import mobi.giza.compose.managers.SharedKeyDerivation import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse @@ -17,11 +24,12 @@ import kotlin.test.assertTrue * secp256k1-zkp compiles just the same and fails at the first call, with an * UnsatisfiedLinkError -- here, rather than in whatever first needs a confederation. * - * This is not a ceremony and nothing in the app calls it. [Confederation.dkgLocal] and - * [Confederation.signLocal] run every role in one process, holding every host key and every - * share: upstream's test harness, because the distributed rounds are not public at the pinned - * native revision. The module is experimental and unaudited. Seeds are fixed so a failure - * reproduces; a real caller would use fresh randomness. + * Nothing in the app calls it yet. [Confederation.dkgLocal] and [Confederation.signLocal] run + * every role in one process, holding every host key and every share: upstream's test harness. + * The last test runs the same protocols the way a ceremony will, through [ConfederationDkg] and + * [ConfederationSigning], one role at a time with only bytes between them -- the distributed + * rounds docs/confederation-upstream.md made public. The module is experimental and unaudited. + * Seeds are fixed so a failure reproduces; a real caller would use fresh randomness. */ class ConfederationReachableJvmTest { @@ -41,7 +49,7 @@ class ConfederationReachableJvmTest { @Test fun `a confederation key signs and the signature verifies`() { val message = seed(0x99, 0) - val sig = Confederation.signLocal(dkg.keyState, dkg.shares, hostSecrets, selection, emptyList(), seed(0x7a, 0), message).right!! + val sig = Confederation.signLocal(dkg.keyState, dkg.shares, hostSecrets, selection, emptyList(), seed(0x7a, 0), message, ConfederationFixture.nonces(0x7a)).right!! assertTrue(Crypto.verifySignatureSchnorr(message, sig, dkg.keyState.publicKey)) } @@ -52,9 +60,62 @@ class ConfederationReachableJvmTest { val message = seed(0x99, 1) val sig = Confederation.signLocal( dkg.keyState, dkg.shares, hostSecrets, selection, - listOf(ConfederationTweak.taproot(internalKey, tapTweak)), seed(0x7a, 1), message, + listOf(ConfederationTweak.taproot(internalKey, tapTweak)), seed(0x7a, 1), message, ConfederationFixture.nonces(0x7b), ).right!! assertTrue(Crypto.verifySignatureSchnorr(message, sig, internalKey.outputKey(tapTweak).first)) assertFalse(Crypto.verifySignatureSchnorr(message, sig, internalKey)) } + + @Test + fun `three groups make a key one role at a time and sign as their room`() { + // The DKG: each role on its own device, the coordinator relaying bytes. + val sessionSeed = seed(0x5f, 0) + val roles = roster.roleIds + val dealings = roles.withIndex().associate { (i, role) -> + role to ConfederationDkg.participantStep1(roster, role, hostSecrets.getValue(role), seed(0xd1, i), sessionSeed).right!! + } + val broadcast = ConfederationDkg.coordinatorStep1(roster, sessionSeed, dealings).right!!.broadcast!! + val step2 = roles.associateWith { role -> + ConfederationDkg.participantStep2(roster, role, hostSecrets.getValue(role), sessionSeed, dealings.getValue(role).record, ConfederationBroadcast(ByteVector(broadcast.toByteArray()))).right!! + } + val keyState = step2.getValue(roles[0]).keyState!! + val authorizations = ConfederationDkg.evidenceList(roster, step2.mapValues { it.value.authorization!! }).right!! + val acknowledgments = ConfederationDkg.evidenceList( + roster, + roles.associateWith { role -> + ConfederationDkg.participantAcknowledge(roster, role, hostSecrets.getValue(role), step2.getValue(role).transitionHash!!, authorizations).right!!.acknowledgment!! + }, + ).right!! + val genesis = ConfederationDkg.coordinatorFinalize(ConfederationDkg.deriveKeyState(roster, broadcast).right!!, authorizations, acknowledgments).right!!.genesis!! + val shares = roles.withIndex().associate { (i, role) -> + role to ConfederationDkg.participantFinalize(genesis, keyState, role, hostSecrets.getValue(role), dealings.getValue(role).record, broadcast, seed(0x43, i)).right!!.share + } + + // Signing as the room: all three groups, two admins each, under the room path's tweaks. + val thresholdPublicKey = "02" + keyState.publicKey.value.toHex() + val room = SharedKeyDerivation.derive(thresholdPublicKey) + val selection = ConfederationSignerSelection(mapOf(0u to setOf(0u, 1u), 1u to setOf(1u, 2u), 2u to setOf(0u, 2u))) + val session = ConfederationSigningSession.create(keyState, seed(0x7c, 0), selection, room.tweaks.map { ConfederationTweak(it, isXonly = true) }).right!! + val message = seed(0x99, 3) + val signers = session.signers.withIndex().map { (i, role) -> + val own = ConfederationSigningSession.create(keyState, session.sessionId, selection, session.tweaks).right!! + assertTrue(own.check(session.dSign)) + ConfederationSigning.generateNonce(own, role, shares.getValue(role), seed(0x4e, i)).right!! + } + val transcript = ConfederationSigning.transcript(session, signers.associate { it.role to it.publicNonce }).right!! + val certificate = ConfederationSigning.certificate( + session, transcript, message, + signers.associate { it.role to ConfederationSigning.confirm(it, transcript, message, hostSecrets.getValue(it.role)).right!! }, + ).right!! + val partials = signers.associate { it.role to ConfederationSigning.sign(it, shares.getValue(it.role), transcript, message, certificate).right!! } + val groupPartials = session.groups.associateWith { g -> + ConfederationSigning.aggregateGroup(session, transcript, message, certificate, g, partials.filterKeys { it.groupId == g }).right!! + } + val sig = ConfederationSigning.aggregate(session, transcript, message, certificate, groupPartials).right!! + + assertEquals(SharedKeyDerivation.marmotGroupId(thresholdPublicKey), room.hex) + assertTrue(Crypto.verifySignatureSchnorr(message, sig, room.publicKey)) + assertFalse(Crypto.verifySignatureSchnorr(message, sig, keyState.publicKey)) + signers.forEach { assertTrue(it.isSpent) } + } } diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationRoomKeyJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationRoomKeyJvmTest.kt index 88c55b5d..cd2ac486 100644 --- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationRoomKeyJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ConfederationRoomKeyJvmTest.kt @@ -34,7 +34,7 @@ class ConfederationRoomKeyJvmTest { private fun sign(message: ByteVector32, tweaks: List, attempt: Int) = Confederation.signLocal( dkg.keyState, dkg.shares, ConfederationFixture.hostSecrets, ConfederationFixture.selection, - tweaks, ConfederationFixture.seed(0x7b, attempt), message, + tweaks, ConfederationFixture.seed(0x7b, attempt), message, ConfederationFixture.nonces(0x7b + attempt), ).right!! private val roomTweaks = derived.tweaks.map { ConfederationTweak(it, isXonly = true) } diff --git a/docs/confederation-upstream.md b/docs/confederation-upstream.md index 8f38d810..04c070be 100644 --- a/docs/confederation-upstream.md +++ b/docs/confederation-upstream.md @@ -18,6 +18,49 @@ Written against zkp `c68e2384`, secp256k1-kmp `fce4d81`, bitcoin-kmp `0c34097` a [confederation.md](./confederation.md) first: its Part B (phases 10 to 13) and Part C (phases 14 to 16) are what this work is for, and its decisions D5, D7 and D10 decide some of the shapes here. +## Status: done, merged and pushed + +Every phase below is committed and pushed bottom up, each to its repository's default branch: zkp +`frost` (the branch secp256k1-kmp's `.gitmodules` tracks; the remote's `main` is upstream's), +secp256k1-kmp and bitcoin-kmp `threshold`, lightning-kmp and lightning-kmp-app `master`, Giza +`giza`. + +lightning-kmp's `master` had not had the `threshold` line before. It held an earlier Iceberg port +of its own: JVM-only, over a secp256k1-kmp fork published to mavenLocal. The `threshold` line had +re-done that port multiplatform and gone on from it. The merge (`47a0f585`) takes the `threshold` +side for all 28 files both touched. It keeps `master`'s measurement harness, ported to the +multiplatform signer, and its two plans, each marked as superseded. + +| phase | repository | commit | checked by | +|---|---|---|---| +| Z0 | secp256k1-zkp | `965fe8ac` | protocol and plan text only | +| Z1 | secp256k1-zkp | `5078a22e` | the reference model regenerates the vectors | +| Z2 | secp256k1-zkp | `a56e4a57` | `tests`, ASan/UBSan, Valgrind, the fuzz harness, an Autotools build | +| Z3 | secp256k1-zkp | `78cf4ebc` | the same | +| Z4 | secp256k1-zkp | `b5819301` | review intake, `doc/confederation-review.md` section 21 | +| K0 | secp256k1-kmp | `8394c82` | the Android tests dex | +| K1 | secp256k1-kmp | `a5aa35e` | `jvmTest`, `linuxX64Test`, `connectedAndroidTest` | +| K2 | secp256k1-kmp | `8022069` | the same, 138 tests each; 57 exported confederation symbols | +| B1 | bitcoin-kmp | `18cb99b` | `jvmTest` (298) | +| B2 | bitcoin-kmp | `bca235a` | `jvmTest` (306), including two threads racing one signer | +| C1 | lightning-kmp | `54c8c4e8`, merged into `master` as `47a0f585` | `:lightning-kmp-core:jvmTest` crypto, channel, iceberg and prefractal (615) | +| C1 | lightning-kmp-app | `572b931` | `:library:compileKotlinJvm`, `:library:compileAndroidMain` | +| C1 | Giza | the commit that adds this table | `compileDebugKotlinAndroid`, `check` (jvmTest 1,602, the audits) | + +Two things came out differently from the text below: + +- **K2's signer state** is a bare `CONFEDERATION_SIGNER_SIZE` array that the caller allocates + and the binding writes back after every call, as planned. The arrays the library reads with no + length (nonces, confirmations, partials, group partials) are copied into buffers padded to the + module maximum. A short array therefore fails on a NULL or zeroed entry instead of being read + past its end. +- **`Confederation.signLocal` takes nonce randomness**, one secret per selected signer. Z3 gave + `secp256k1_confederation_sign` a `nonce_secrands32` argument, and bitcoin-kmp has no secure + random source of its own to fill it. Giza's three callers, all tests, pass fixed values. + +Not done: the iOS build. Kotlin/Native cinterop regenerates from the header there and needs a Mac. +`linuxX64Test` exercises the same Kotlin/Native code. + --- ## Where it stands diff --git a/docs/confederation.md b/docs/confederation.md index 5dbaccbb..fcede8f3 100644 --- a/docs/confederation.md +++ b/docs/confederation.md @@ -671,6 +671,12 @@ withdraw, and the coordinator's device ends holding three verified admin lists. Planned in detail, level by level, in [confederation-upstream.md](./confederation-upstream.md). +**Done, and pushed.** zkp exports the DKG and signing steps; secp256k1-kmp binds them; +bitcoin-kmp wraps them as `ConfederationDkg` and `ConfederationSigning`; the chain is bumped up to +Giza. `ConfederationReachableJvmTest` runs three groups through both, one role at a time, and signs +as the room's id. The commits, and what checked each, are in that plan's "Status". Parts B and C +can start. + Not Giza work, but Part B cannot start without it. Giza needs these to be public in zkp, bound in secp256k1-kmp, and wrapped in bitcoin-kmp. The names are from zkp's static functions: diff --git a/lightning-kmp-app b/lightning-kmp-app index e6be5e59..572b931c 160000 --- a/lightning-kmp-app +++ b/lightning-kmp-app @@ -1 +1 @@ -Subproject commit e6be5e594992659154ffd1da4a687ba862732953 +Subproject commit 572b931c3a1d85333333cc9887795ddc39052c4a