@@ -55,7 +55,7 @@ public interface Secp256k1 {
5555 */
5656 public fun signSchnorr (data : ByteArray , sec : ByteArray , auxrand32 : ByteArray? ): ByteArray
5757
58- /* *
58+ /* *
5959 * Convert an ECDSA signature to a normalized lower-S form (bitcoin standardness rule).
6060 * Returns the normalized signature and a boolean set to true if the input signature was not normalized.
6161 *
@@ -149,29 +149,108 @@ public interface Secp256k1 {
149149 compressed[0 ] = if (pubkey.last() % 2 == 0 ) 2 .toByte() else 3 .toByte()
150150 compressed
151151 }
152+
152153 else -> throw Secp256k1Exception (" invalid public key" )
153154 }
154155 }
155156
156- public fun musigNonceGen (session_id32 : ByteArray , seckey : ByteArray? , pubkey : ByteArray , msg32 : ByteArray? , keyagg_cache : ByteArray? , extra_input32 : ByteArray? ): ByteArray
157+ /* *
158+ * Generate a secret nonce to be used in a musig2 signing session.
159+ * This nonce must never be persisted or reused across signing sessions.
160+ * All optional arguments exist to enrich the quality of the randomness used, which is critical for security.
161+ *
162+ * @param sessionId32 unique 32-byte session ID.
163+ * @param privkey (optional) signer's private key.
164+ * @param aggpubkey aggregated public key of all participants in the signing session.
165+ * @param msg32 (optional) 32-byte message that will be signed, if already known.
166+ * @param keyaggCache (optional) key aggregation cache data from the signing session.
167+ * @param extraInput32 (optional) additional 32-byte random data.
168+ * @return serialized version of the secret nonce and the corresponding public nonce.
169+ */
170+ public fun musigNonceGen (sessionId32 : ByteArray , privkey : ByteArray? , aggpubkey : ByteArray , msg32 : ByteArray? , keyaggCache : ByteArray? , extraInput32 : ByteArray? ): ByteArray
157171
172+ /* *
173+ * Aggregate public nonces from all participants of a signing session.
174+ *
175+ * @param pubnonces public nonces (one per participant).
176+ * @return 66-byte aggregate public nonce (two public keys) or throws an exception is a nonce is invalid.
177+ */
158178 public fun musigNonceAgg (pubnonces : Array <ByteArray >): ByteArray
159179
160- public fun musigPubkeyAgg (pubkeys : Array <ByteArray >, keyagg_cache : ByteArray? ): ByteArray
180+ /* *
181+ * Aggregate public keys from all participants of a signing session.
182+ *
183+ * @param pubkeys public keys of all participants in the signing session.
184+ * @param keyaggCache (optional) key aggregation cache data from the signing session. If an empty byte array is
185+ * provided, it will be filled with key aggregation data that can be used for the next steps of the signing process.
186+ * @return 32-byte x-only public key.
187+ */
188+ public fun musigPubkeyAgg (pubkeys : Array <ByteArray >, keyaggCache : ByteArray? ): ByteArray
161189
162- public fun musigPubkeyTweakAdd (keyagg_cache : ByteArray , tweak32 : ByteArray ): ByteArray
190+ /* *
191+ * Tweak the aggregated public key of a signing session.
192+ *
193+ * @param keyaggCache key aggregation cache filled by [musigPubkeyAgg].
194+ * @param tweak32 private key tweak to apply.
195+ * @return P + tweak32 * G (where P is the aggregated public key from [keyaggCache]). The key aggregation cache will
196+ * be updated with the tweaked public key.
197+ */
198+ public fun musigPubkeyTweakAdd (keyaggCache : ByteArray , tweak32 : ByteArray ): ByteArray
163199
164- public fun musigPubkeyXonlyTweakAdd (keyagg_cache : ByteArray , tweak32 : ByteArray ): ByteArray
200+ /* *
201+ * Tweak the aggregated public key of a signing session, treating it as an x-only public key (e.g. when using taproot).
202+ *
203+ * @param keyaggCache key aggregation cache filled by [musigPubkeyAgg].
204+ * @param tweak32 private key tweak to apply.
205+ * @return with_even_y(P) + tweak32 * G (where P is the aggregated public key from [keyaggCache]). The key aggregation
206+ * cache will be updated with the tweaked public key.
207+ */
208+ public fun musigPubkeyXonlyTweakAdd (keyaggCache : ByteArray , tweak32 : ByteArray ): ByteArray
165209
166- public fun musigNonceProcess (aggnonce : ByteArray , msg32 : ByteArray , keyagg_cache : ByteArray ): ByteArray
210+ /* *
211+ * Create a signing session context based on the public information from all participants.
212+ *
213+ * @param aggnonce aggregated public nonce (see [musigNonceAgg]).
214+ * @param msg32 32-byte message that will be signed.
215+ * @param keyaggCache aggregated public key cache filled by calling [musigPubkeyAgg] with the public keys of all participants.
216+ * @return signing session context that can be used to create partial signatures and aggregate them.
217+ */
218+ public fun musigNonceProcess (aggnonce : ByteArray , msg32 : ByteArray , keyaggCache : ByteArray ): ByteArray
167219
168- public fun musigPartialSign (secnonce : ByteArray , privkey : ByteArray , keyagg_cache : ByteArray , session : ByteArray ): ByteArray
220+ /* *
221+ * Create a partial signature.
222+ *
223+ * @param secnonce signer's secret nonce (see [musigNonceGen]).
224+ * @param privkey signer's private key.
225+ * @param keyaggCache aggregated public key cache filled by calling [musigPubkeyAgg] with the public keys of all participants.
226+ * @param session signing session context (see [musigNonceProcess]).
227+ * @return 32-byte partial signature.
228+ */
229+ public fun musigPartialSign (secnonce : ByteArray , privkey : ByteArray , keyaggCache : ByteArray , session : ByteArray ): ByteArray
169230
170- public fun musigPartialSigVerify (psig : ByteArray , pubnonce : ByteArray , pubkey : ByteArray , keyagg_cache : ByteArray , session : ByteArray ): Int
231+ /* *
232+ * Verify the partial signature from one of the signing session's participants.
233+ *
234+ * @param psig 32-byte partial signature.
235+ * @param pubnonce individual public nonce of the signing participant.
236+ * @param pubkey individual public key of the signing participant.
237+ * @param keyaggCache aggregated public key cache filled by calling [musigPubkeyAgg] with the public keys of all participants.
238+ * @param session signing session context (see [musigNonceProcess]).
239+ * @return result code (1 if the partial signature is valid, 0 otherwise).
240+ */
241+ public fun musigPartialSigVerify (psig : ByteArray , pubnonce : ByteArray , pubkey : ByteArray , keyaggCache : ByteArray , session : ByteArray ): Int
171242
243+ /* *
244+ * Aggregate partial signatures from all participants into a single schnorr signature. If some of the partial
245+ * signatures are invalid, this function will return an invalid aggregated signature without raising an error.
246+ * It is recommended to use [musigPartialSigVerify] to verify partial signatures first.
247+ *
248+ * @param session signing session context (see [musigNonceProcess]).
249+ * @param psigs list of 32-byte partial signatures.
250+ * @return 64-byte aggregated schnorr signature.
251+ */
172252 public fun musigPartialSigAgg (session : ByteArray , psigs : Array <ByteArray >): ByteArray
173253
174-
175254 /* *
176255 * Delete the secp256k1 context from dynamic memory.
177256 */
0 commit comments