@@ -54,7 +54,7 @@ use crate::{
54
54
g,
55
55
marker:: * ,
56
56
nonce:: NonceGen ,
57
- s, Point , Scalar ,
57
+ s, Point , Scalar , G ,
58
58
} ,
59
59
KeyPair , Message , Schnorr , Signature ,
60
60
} ;
@@ -78,7 +78,7 @@ pub trait EncryptedSign {
78
78
) -> EncryptedSignature ;
79
79
}
80
80
81
- impl < NG , CH , GT > EncryptedSign for Schnorr < CH , NG , GT >
81
+ impl < NG , CH > EncryptedSign for Schnorr < CH , NG >
82
82
where
83
83
CH : Digest < OutputSize = U32 > + Clone ,
84
84
NG : NonceGen ,
98
98
public => [ X , Y , message]
99
99
) ;
100
100
101
- let R = g ! ( r * { self . G ( ) } + Y )
101
+ let R = g ! ( r * G + Y )
102
102
// R_hat = r * G is sampled pseudorandomly for every Y which means R_hat + Y is also
103
103
// be pseudoranodm and therefore will not be zero.
104
104
// NOTE: Crucially we add Y to the nonce derivation to ensure this is true.
@@ -110,7 +110,7 @@ where
110
110
// key before decrypting it
111
111
r. conditional_negate ( needs_negation) ;
112
112
113
- let c = self . challenge ( & R . to_xonly ( ) , X , message) ;
113
+ let c = self . challenge ( R . to_xonly ( ) , X , message) ;
114
114
let s_hat = s ! ( r + c * x) . mark :: < Public > ( ) ;
115
115
116
116
EncryptedSignature {
@@ -188,12 +188,12 @@ pub trait Adaptor {
188
188
) -> Option < Scalar > ;
189
189
}
190
190
191
- impl < CH , NG , GT > Adaptor for Schnorr < CH , NG , GT >
191
+ impl < CH , NG > Adaptor for Schnorr < CH , NG >
192
192
where
193
193
CH : Digest < OutputSize = U32 > + Clone ,
194
194
{
195
195
fn encryption_key_for ( & self , decryption_key : & Scalar ) -> Point {
196
- g ! ( decryption_key * { self . G ( ) } ) . normalize ( )
196
+ g ! ( decryption_key * G ) . normalize ( )
197
197
}
198
198
199
199
#[ must_use]
@@ -216,9 +216,9 @@ where
216
216
// !needs_negation => R_hat = R - Y
217
217
let R_hat = g ! ( R + { Y . conditional_negate( !needs_negation) } ) ;
218
218
219
- let c = self . challenge ( & R . to_xonly ( ) , & X . to_xonly ( ) , message) ;
219
+ let c = self . challenge ( R . to_xonly ( ) , X . to_xonly ( ) , message) ;
220
220
221
- R_hat == g ! ( s_hat * { self . G ( ) } - c * X )
221
+ R_hat == g ! ( s_hat * G - c * X )
222
222
}
223
223
224
224
fn decrypt_signature (
@@ -257,7 +257,7 @@ where
257
257
258
258
let mut y = s ! ( s - s_hat) ;
259
259
y. conditional_negate ( * needs_negation) ;
260
- let implied_encryption_key = g ! ( y * { self . G ( ) } ) ;
260
+ let implied_encryption_key = g ! ( y * G ) ;
261
261
262
262
if implied_encryption_key == * encryption_key {
263
263
Some ( y. expect_nonzero ( "unreachable - encryption_key is NonZero and y*G equals it" ) )
@@ -271,48 +271,55 @@ where
271
271
mod test {
272
272
273
273
use super :: * ;
274
- use crate :: nonce:: { self , Deterministic } ;
275
- use secp256kfun:: TEST_SOUNDNESS ;
274
+ use crate :: nonce:: { Deterministic , GlobalRng , Synthetic } ;
275
+ use rand:: rngs:: ThreadRng ;
276
+ use secp256kfun:: proptest:: prelude:: * ;
277
+ use sha2:: Sha256 ;
276
278
#[ cfg( target_arch = "wasm32" ) ]
277
279
use wasm_bindgen_test:: wasm_bindgen_test as test;
278
280
279
- fn test_schnorr < NG : NonceGen > ( schnorr : Schnorr < sha2:: Sha256 , NG > ) {
280
- for _ in 0 ..TEST_SOUNDNESS {
281
- let signing_keypair = schnorr. new_keypair ( Scalar :: random ( & mut rand:: thread_rng ( ) ) ) ;
282
- let verification_key = signing_keypair. verification_key ( ) ;
283
- let decryption_key = Scalar :: random ( & mut rand:: thread_rng ( ) ) ;
284
- let encryption_key = schnorr. encryption_key_for ( & decryption_key) ;
285
- let message = Message :: < Public > :: plain ( "test" , b"give 100 coins to Bob" . as_ref ( ) ) ;
286
-
287
- let encrypted_signature =
288
- schnorr. encrypted_sign ( & signing_keypair, & encryption_key, message) ;
289
-
290
- assert ! ( schnorr. verify_encrypted_signature(
291
- & signing_keypair. verification_key( ) ,
292
- & encryption_key,
293
- message,
294
- & encrypted_signature,
295
- ) ) ;
281
+ proptest ! {
282
+ #[ test]
283
+ fn signing_tests_deterministic( secret_key in any:: <Scalar >( ) , decryption_key in any:: <Scalar >( ) ) {
284
+ let schnorr = Schnorr :: <Sha256 , Deterministic <Sha256 >>:: default ( ) ;
285
+ test_it( schnorr, secret_key, decryption_key) ;
286
+ }
296
287
297
- let decryption_key = decryption_key. mark :: < Public > ( ) ;
298
- let signature =
299
- schnorr. decrypt_signature ( decryption_key. clone ( ) , encrypted_signature. clone ( ) ) ;
300
- assert ! ( schnorr. verify( & verification_key, message, & signature) ) ;
301
- let rec_decryption_key = schnorr
302
- . recover_decryption_key ( & encryption_key, & encrypted_signature, & signature)
303
- . expect ( "recovery works" ) ;
304
- assert_eq ! ( rec_decryption_key, decryption_key) ;
288
+ #[ test]
289
+ fn signing_tests_synthetic( secret_key in any:: <Scalar >( ) , decryption_key in any:: <Scalar >( ) ) {
290
+ let schnorr = Schnorr :: <Sha256 , Synthetic <Sha256 , GlobalRng <ThreadRng >>>:: default ( ) ;
291
+ test_it( schnorr, secret_key, decryption_key) ;
305
292
}
293
+
306
294
}
307
295
308
- #[ test]
309
- fn sign_plain_message ( ) {
310
- use rand:: rngs:: ThreadRng ;
311
- use sha2:: Sha256 ;
312
- test_schnorr ( Schnorr :: new ( Deterministic :: < Sha256 > :: default ( ) ) ) ;
313
- test_schnorr ( Schnorr :: new ( nonce:: Synthetic :: <
314
- Sha256 ,
315
- nonce:: GlobalRng < ThreadRng > ,
316
- > :: default ( ) ) ) ;
296
+ fn test_it < NG : NonceGen > (
297
+ schnorr : Schnorr < Sha256 , NG > ,
298
+ secret_key : Scalar ,
299
+ decryption_key : Scalar ,
300
+ ) {
301
+ let signing_keypair = schnorr. new_keypair ( secret_key) ;
302
+ let verification_key = signing_keypair. verification_key ( ) ;
303
+ let encryption_key = schnorr. encryption_key_for ( & decryption_key) ;
304
+ let message = Message :: < Public > :: plain ( "test" , b"give 100 coins to Bob" . as_ref ( ) ) ;
305
+
306
+ let encrypted_signature =
307
+ schnorr. encrypted_sign ( & signing_keypair, & encryption_key, message) ;
308
+
309
+ assert ! ( schnorr. verify_encrypted_signature(
310
+ & signing_keypair. verification_key( ) ,
311
+ & encryption_key,
312
+ message,
313
+ & encrypted_signature,
314
+ ) ) ;
315
+
316
+ let decryption_key = decryption_key. mark :: < Public > ( ) ;
317
+ let signature =
318
+ schnorr. decrypt_signature ( decryption_key. clone ( ) , encrypted_signature. clone ( ) ) ;
319
+ assert ! ( schnorr. verify( & verification_key, message, & signature) ) ;
320
+ let rec_decryption_key = schnorr
321
+ . recover_decryption_key ( & encryption_key, & encrypted_signature, & signature)
322
+ . expect ( "recovery works" ) ;
323
+ assert_eq ! ( rec_decryption_key, decryption_key) ;
317
324
}
318
325
}
0 commit comments