Skip to content

Commit ac7fcc8

Browse files
committed
Actually expire sessions when we have given out too many nonces
1 parent e23f800 commit ac7fcc8

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

Diff for: schnorr_fun/src/blind.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ pub struct BlindSigner<CH, NG> {
305305
pub schnorr: Schnorr<CH, NG>,
306306
max_sessions: usize,
307307
signature_requests: Vec<SignatureRequest>,
308-
nonces: BTreeMap<Point<EvenY>, Scalar>,
308+
nonces: Vec<(Point<EvenY>, Scalar)>,
309309
already_signed: BTreeMap<Point<EvenY>, Option<Scalar<Public, Zero>>>,
310310
secret: Scalar,
311311
}
@@ -324,7 +324,7 @@ where
324324
Self {
325325
max_sessions,
326326
signature_requests: vec![],
327-
nonces: BTreeMap::new(),
327+
nonces: vec![],
328328
already_signed: BTreeMap::new(),
329329
secret,
330330
schnorr,
@@ -361,22 +361,24 @@ where
361361
);
362362
let (pub_nonce, nonce_negated) = g!(nonce * G).normalize().into_point_with_even_y();
363363
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);
365370
pub_nonce
366371
}
367372

368373
/// Fetch the secret nonce for some public nonce and forget it
369374
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+
}
378380
}
379-
secret_nonce
381+
return None;
380382
}
381383

382384
/// Sign a blinded challenge and delete the associated secret_nonce

0 commit comments

Comments
 (0)