Skip to content

Commit

Permalink
Separate sign_all_but_one for immediate signing
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfarrow committed Feb 2, 2023
1 parent c2240f3 commit e2e47e5
Showing 1 changed file with 51 additions and 30 deletions.
81 changes: 51 additions & 30 deletions schnorr_fun/src/blind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,17 +390,63 @@ where
/// Returns a scalar of the unblinded signature
pub fn sign_single(&mut self, sig_request: SignatureRequest) -> Option<Scalar<Public, Zero>> {
let secret_nonce = self.use_secret_nonce(sig_request.public_nonce);
match secret_nonce {
let signature_response = match secret_nonce {
Some(secret_nonce) => {
let sig = s!(secret_nonce + sig_request.blind_challenge * self.secret).public();
Some(sig) //.secret().non_zero()
Some(sig)
}
// Did not expect this nonce
None => None,
}
};
// Store this signature
self.already_signed
.insert(sig_request.public_nonce, signature_response);
signature_response
}

/// Sign multiple blind schnorr signatures concurrently once enough have been requested
/// Sign all the signature requests immediately, except for one
///
/// # Returns
///
/// A vector of scalar signature options
pub fn sign_all_but_one<R: RngCore>(
&mut self,
rng: &mut R,
) -> Vec<Option<Scalar<Public, Zero>>> {
// Choose an index to skip signing request
let skip_i = rng.gen_range(0..self.signature_requests.len() as u32);

// Sign all the signature requests but don't store one (given there is more than one)
let signatures = self
.signature_requests
.clone()
.into_iter()
.enumerate()
.map(|(i, sig_request)| {
let sig_response = if self.max_sessions > 1 && i as u32 == skip_i {
// For one out of the N sessions, drop the signature.
// ⚠ IMPORTANT: Overwrite the stored signature for this nonce
self.already_signed.insert(sig_request.public_nonce, None);
assert!(self
.already_signed
.get(&sig_request.public_nonce)
.expect("history has to have None written for this nonce")
.is_none());
None
} else {
// Otherwise, sign and store the signature
self.sign_single(sig_request.clone())
};
sig_response
})
.collect();

// Clear our signature requests
self.signature_requests = vec![];
signatures
}

/// Pass signature request in for signing, batches until max_sessions requests have been made.
///
/// Does not do any signing until max_session number of [`SignatureRequest`]s have been requested.
/// Then sign them all but randomly disconnect (return None) one of the N sessions.
Expand All @@ -421,32 +467,7 @@ where
if self.max_sessions > 1 && self.signature_requests.len() < self.max_sessions {
vec![]
} else {
// Choose an index to skip signing request
let skip_i = rng.gen_range(0..self.signature_requests.len() as u32);

// Sign all the signature requests but don't store one (given there is more than one)
let signatures = self
.signature_requests
.clone()
.into_iter()
.enumerate()
.map(|(i, sig_req)| {
let sig = self.sign_single(sig_req.clone());
let response = if self.max_sessions > 1 && i as u32 == skip_i {
// Maybe don't return the signature
None
} else {
sig
};
// Store signature (or None) for this public nonce
self.already_signed.insert(sig_req.public_nonce, response);
response
})
.collect();

// Clear our signature requests
self.signature_requests = vec![];
signatures
self.sign_all_but_one(rng)
}
}
}
Expand Down

0 comments on commit e2e47e5

Please sign in to comment.