11import { FleetError , _0n , concatBytes } from "@fleet-sdk/common" ;
2- import { bigintBE , blake2b256 , hex , randomBytes , validateEcPoint } from "@fleet-sdk/crypto" ;
2+ import {
3+ bigintBE ,
4+ blake2b256 ,
5+ hex ,
6+ randomBytes ,
7+ validateEcPoint ,
8+ } from "@fleet-sdk/crypto" ;
39import { secp256k1 } from "@noble/curves/secp256k1" ;
410
511const { ProjectivePoint : ECPoint , CURVE } = secp256k1 ;
@@ -20,14 +26,14 @@ const MAX_ITERATIONS = 100;
2026export function sign ( message : Uint8Array , secretKey : Uint8Array ) {
2127 for ( let i = 0 ; i < MAX_ITERATIONS ; i ++ ) {
2228 const signature = genSignature ( message , secretKey ) ;
29+ /* v8 ignore else -- @preserve */
2330 if ( signature ) return signature ;
24- /* v8 ignore start */
2531 }
2632
2733 // This branch is ignored in the coverage report because it depends on randomness.
34+ /* v8 ignore next -- @preserve */
2835 throw new FleetError ( "Failed to generate signature" ) ;
2936}
30- /* v8 ignore stop */
3137
3238/**
3339 * Generates a Schnorr signature for the given message using the provided secret key.
@@ -37,22 +43,25 @@ export function sign(message: Uint8Array, secretKey: Uint8Array) {
3743 * @returns The generated signature as a Uint8Array, or undefined if the verification fails.
3844 * @throws Error if failed to generate commitment.
3945 */
40- export function genSignature ( message : Uint8Array , secretKey : Uint8Array ) : undefined | Uint8Array {
46+ export function genSignature (
47+ message : Uint8Array ,
48+ secretKey : Uint8Array ,
49+ ) : undefined | Uint8Array {
4150 const sk = bigintBE . encode ( secretKey ) ;
4251 const pk = G . multiply ( sk ) . toRawBytes ( ) ;
4352 const k = genRandomSecret ( ) ;
4453 const w = G . multiply ( k ) . toRawBytes ( ) ;
4554 const c = fiatShamirHash ( genCommitment ( pk , w , message ) ) ;
4655
4756 // The next line is ignored in the coverage report because it depends on randomness.
48- /* v8 ignore next */
57+ /* v8 ignore next -- @preserve */
4958 if ( c === 0n ) throw new FleetError ( "Failed to generate challenge" ) ;
5059
5160 const z = umod ( sk * c + k , CURVE . n ) ;
5261 const signature = concatBytes ( bigintBE . decode ( c ) , bigintBE . decode ( z ) ) ;
5362
5463 // The next line is ignored in the coverage report because it depends on randomness.
55- /* v8 ignore next */
64+ /* v8 ignore next -- @preserve */
5665 if ( ! verify ( message , signature , pk ) ) return ;
5766
5867 return signature ;
@@ -74,7 +83,7 @@ function genRandomSecret() {
7483 }
7584
7685 // The next line is ignored in the coverage report because it depends on randomness.
77- /* v8 ignore next */
86+ /* v8 ignore next -- @preserve */
7887 if ( r === 0n ) throw new FleetError ( "Failed to generate randomness" ) ;
7988
8089 return r ;
@@ -98,12 +107,18 @@ export function umod(a: bigint, b: bigint): bigint {
98107 * @returns A boolean indicating whether the signature is valid or not.
99108 * @throws FleetError if the public key is invalid.
100109 */
101- export function verify ( message : Uint8Array , proof : Uint8Array , publicKey : Uint8Array ) {
110+ export function verify (
111+ message : Uint8Array ,
112+ proof : Uint8Array ,
113+ publicKey : Uint8Array ,
114+ ) {
102115 if ( ! proof || proof . length !== ERGO_SCHNORR_SIG_LEN ) return false ;
103116 if ( ! validateEcPoint ( publicKey ) ) throw new FleetError ( "Invalid Public Key." ) ;
104117
105118 const c = bigintBE . encode ( proof . slice ( 0 , ERGO_SOUNDNESS_BYTES ) ) ;
106- const z = bigintBE . encode ( proof . slice ( ERGO_SOUNDNESS_BYTES , ERGO_SCHNORR_SIG_LEN ) ) ;
119+ const z = bigintBE . encode (
120+ proof . slice ( ERGO_SOUNDNESS_BYTES , ERGO_SCHNORR_SIG_LEN ) ,
121+ ) ;
107122
108123 const t = ECPoint . fromHex ( publicKey ) . multiply ( CURVE . n - c ) ;
109124 const w = G . multiply ( z ) . add ( t ) . toRawBytes ( ) ;
0 commit comments