From ac7fcc8f66cc675472a93d9b70793d0322bf3f3e Mon Sep 17 00:00:00 2001 From: Nick Farrow Date: Sat, 4 Feb 2023 03:45:45 +1100 Subject: [PATCH] Actually expire sessions when we have given out too many nonces --- schnorr_fun/src/blind.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/schnorr_fun/src/blind.rs b/schnorr_fun/src/blind.rs index e4c753fb..16a71de7 100644 --- a/schnorr_fun/src/blind.rs +++ b/schnorr_fun/src/blind.rs @@ -305,7 +305,7 @@ pub struct BlindSigner { pub schnorr: Schnorr, max_sessions: usize, signature_requests: Vec, - nonces: BTreeMap, Scalar>, + nonces: Vec<(Point, Scalar)>, already_signed: BTreeMap, Option>>, secret: Scalar, } @@ -324,7 +324,7 @@ where Self { max_sessions, signature_requests: vec![], - nonces: BTreeMap::new(), + nonces: vec![], already_signed: BTreeMap::new(), secret, schnorr, @@ -361,22 +361,24 @@ where ); let (pub_nonce, nonce_negated) = g!(nonce * G).normalize().into_point_with_even_y(); nonce.conditional_negate(nonce_negated); - self.nonces.insert(pub_nonce, nonce); + // If there are too many nonces we need to kick one of them out + if self.nonces.len() >= self.max_sessions { + self.nonces.remove(0); + } + self.nonces.push((pub_nonce, nonce)); + assert!(self.nonces.len() <= self.max_sessions); pub_nonce } /// Fetch the secret nonce for some public nonce and forget it fn use_secret_nonce(&mut self, public_nonce: Point) -> Option { - let secret_nonce = match self.nonces.get(&public_nonce) { - Some(secret_nonce) => Some(secret_nonce.clone()), - // skip because we do not know about this public nonce! - None => None, - }; - if secret_nonce.is_some() { - self.nonces.remove_entry(&public_nonce); - assert!(self.nonces.get(&public_nonce).is_none()); + for (i, (public, _)) in self.nonces.iter().enumerate() { + if *public == public_nonce { + let (_, secret) = self.nonces.remove(i); + return Some(secret); + } } - secret_nonce + return None; } /// Sign a blinded challenge and delete the associated secret_nonce