@@ -20,8 +20,8 @@ use rand::{CryptoRng, Rng};
2020use sha2:: { Digest , Sha256 } ;
2121
2222use crate :: { error:: Error , fixed_base:: gen_mul} ;
23- /* ------------------------------ Utilities --------------------------------- */
2423
24+ /// Helpers to serialize and deserialize field as per BIP340
2525fn serialize_field < F : PrimeField > ( x : & F ) -> [ u8 ; 32 ] {
2626 // `Fq` modulus is 256 bits, so its big-endian encoding always fits in 32 bytes.
2727 x. into_bigint ( )
@@ -46,19 +46,6 @@ fn deserialize_field<F: PrimeField>(bytes: [u8; 32]) -> Result<F, Error> {
4646 ) )
4747}
4848
49- /* --------------------------------- Types ---------------------------------- */
50-
51- /// Adaptor for the VSSS
52- #[ derive( Debug , Clone , PartialEq , Eq ) ]
53- pub struct Adaptor {
54- /// s' = ±r' + e * x (the evaluator’s partial Schnorr s)
55- pub tweaked_s : ark_secp256k1:: Fr ,
56- /// R' = r'*G
57- pub R_dash_commit : ark_secp256k1:: Projective ,
58- /// S = share*G
59- pub share_commitment : ark_secp256k1:: Projective ,
60- }
61-
6249/// Signature for the VSSS
6350#[ derive( Debug , Clone , PartialEq , Eq ) ]
6451pub struct Signature {
@@ -93,28 +80,17 @@ impl Signature {
9380 }
9481}
9582
96- /* ----------------------------- Challenge helper --------------------------- */
97-
98- /// e = H(BIP0340/challenge, R.x, P.x, wire_index, sighash)
99- fn challenge_e (
100- R : ark_secp256k1:: Affine ,
101- P : ark_secp256k1:: Affine ,
102- sighash : & [ u8 ] ,
103- ) -> ark_secp256k1:: Fr {
104- // BIP340 tag
105- let tag_hash = Sha256 :: digest ( b"BIP0340/challenge" ) ;
106- let mut h = Sha256 :: new ( ) ;
107- h. update ( tag_hash) ;
108- h. update ( tag_hash) ;
109- h. update ( serialize_field ( & R . x ) ) ;
110- h. update ( serialize_field ( & P . x ) ) ;
111- h. update ( sighash) ;
112- let digest = h. finalize ( ) ;
113- ark_secp256k1:: Fr :: from_be_bytes_mod_order ( & digest)
83+ /// Adaptor for the VSSS
84+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
85+ pub struct Adaptor {
86+ /// s' = ±r' + e * x (the evaluator’s partial Schnorr s)
87+ pub tweaked_s : ark_secp256k1:: Fr ,
88+ /// R' = r'*G
89+ pub R_dash_commit : ark_secp256k1:: Projective ,
90+ /// S = share*G
91+ pub share_commitment : ark_secp256k1:: Projective ,
11492}
11593
116- /* --------------------------------- Methods -------------------------------- */
117-
11894impl Adaptor {
11995 /// Generates an adaptor from the evaluator’s master secret key `x`, a commitment
12096 /// to the garbler’s share (`S = share·G`), and the `(wire_index, sighash)` transcript data.
@@ -161,7 +137,7 @@ impl Adaptor {
161137 ) ) ;
162138 }
163139
164- let e = challenge_e ( expected_R, evaluator_master_pk, sighash) ;
140+ let e = Self :: challenge_e ( expected_R, evaluator_master_pk, sighash) ;
165141
166142 if expected_R. y . into_bigint ( ) . is_odd ( ) {
167143 // negate to make commitment of completed nonce (i.e. r_dash + share) even
@@ -207,7 +183,7 @@ impl Adaptor {
207183 ) ) ;
208184 }
209185
210- let e = challenge_e ( expected_R, evaluator_master_pk_affine, sighash) ;
186+ let e = Self :: challenge_e ( expected_R, evaluator_master_pk_affine, sighash) ;
211187
212188 // LHS: s'·G - e.P
213189 let lhs = gen_mul ( & self . tweaked_s ) - evaluator_master_pk * e;
@@ -262,6 +238,24 @@ impl Adaptor {
262238 let diff = signature. s - self . tweaked_s ;
263239 if is_odd { -diff } else { diff }
264240 }
241+
242+ /// e = H(BIP0340/challenge, R.x, P.x, wire_index, sighash)
243+ fn challenge_e (
244+ R : ark_secp256k1:: Affine ,
245+ P : ark_secp256k1:: Affine ,
246+ sighash : & [ u8 ] ,
247+ ) -> ark_secp256k1:: Fr {
248+ // BIP340 tag
249+ let tag_hash = Sha256 :: digest ( b"BIP0340/challenge" ) ;
250+ let mut h = Sha256 :: new ( ) ;
251+ h. update ( tag_hash) ;
252+ h. update ( tag_hash) ;
253+ h. update ( serialize_field ( & R . x ) ) ;
254+ h. update ( serialize_field ( & P . x ) ) ;
255+ h. update ( sighash) ;
256+ let digest = h. finalize ( ) ;
257+ ark_secp256k1:: Fr :: from_be_bytes_mod_order ( & digest)
258+ }
265259}
266260
267261/* ---------------------------------- Tests ---------------------------------- */
@@ -463,7 +457,7 @@ mod tests {
463457 // Recompute e like verify() does
464458 let R = fx. adaptor . expected_R ( ) . into_affine ( ) ;
465459 let P = fx. P . into_affine ( ) ;
466- let e = super :: challenge_e ( R , P , & fx. sighash ) ;
460+ let e = Adaptor :: challenge_e ( R , P , & fx. sighash ) ;
467461
468462 let lhs = gen_mul ( & fx. adaptor . tweaked_s ) ; // s'·G
469463 let tweaked_R = if R . y . into_bigint ( ) . is_odd ( ) {
0 commit comments