@@ -305,7 +305,7 @@ pub struct BlindSigner<CH, NG> {
305
305
pub schnorr : Schnorr < CH , NG > ,
306
306
max_sessions : usize ,
307
307
signature_requests : Vec < SignatureRequest > ,
308
- nonces : BTreeMap < Point < EvenY > , Scalar > ,
308
+ nonces : Vec < ( Point < EvenY > , Scalar ) > ,
309
309
already_signed : BTreeMap < Point < EvenY > , Option < Scalar < Public , Zero > > > ,
310
310
secret : Scalar ,
311
311
}
@@ -324,7 +324,7 @@ where
324
324
Self {
325
325
max_sessions,
326
326
signature_requests : vec ! [ ] ,
327
- nonces : BTreeMap :: new ( ) ,
327
+ nonces : vec ! [ ] ,
328
328
already_signed : BTreeMap :: new ( ) ,
329
329
secret,
330
330
schnorr,
@@ -361,22 +361,24 @@ where
361
361
) ;
362
362
let ( pub_nonce, nonce_negated) = g ! ( nonce * G ) . normalize ( ) . into_point_with_even_y ( ) ;
363
363
nonce. conditional_negate ( nonce_negated) ;
364
- self . nonces . insert ( pub_nonce, nonce) ;
364
+ // If there are too many nonces we need to kick one of them out
365
+ if self . nonces . len ( ) >= self . max_sessions {
366
+ self . nonces . remove ( 0 ) ;
367
+ }
368
+ self . nonces . push ( ( pub_nonce, nonce) ) ;
369
+ assert ! ( self . nonces. len( ) <= self . max_sessions) ;
365
370
pub_nonce
366
371
}
367
372
368
373
/// Fetch the secret nonce for some public nonce and forget it
369
374
fn use_secret_nonce ( & mut self , public_nonce : Point < EvenY > ) -> Option < Scalar > {
370
- let secret_nonce = match self . nonces . get ( & public_nonce) {
371
- Some ( secret_nonce) => Some ( secret_nonce. clone ( ) ) ,
372
- // skip because we do not know about this public nonce!
373
- None => None ,
374
- } ;
375
- if secret_nonce. is_some ( ) {
376
- self . nonces . remove_entry ( & public_nonce) ;
377
- assert ! ( self . nonces. get( & public_nonce) . is_none( ) ) ;
375
+ for ( i, ( public, _) ) in self . nonces . iter ( ) . enumerate ( ) {
376
+ if * public == public_nonce {
377
+ let ( _, secret) = self . nonces . remove ( i) ;
378
+ return Some ( secret) ;
379
+ }
378
380
}
379
- secret_nonce
381
+ return None ;
380
382
}
381
383
382
384
/// Sign a blinded challenge and delete the associated secret_nonce
0 commit comments