Skip to content

Commit

Permalink
feat: separate sign() and sign_with_rng()
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbinth committed Mar 22, 2024
1 parent 1354e0a commit b751c0f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/dsa/rpo_falcon512/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod tests {

// sign a random message
let message: Word = [ONE; 4];
let signature = sk.sign(message, &mut rng);
let signature = sk.sign_with_rng(message, &mut rng);

// make sure the signature verifies correctly
assert!(pk.verify(message, &signature));
Expand Down
18 changes: 11 additions & 7 deletions src/dsa/rpo_falcon512/keys/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,17 @@ impl SecretKey {
// SIGNATURE GENERATION
// --------------------------------------------------------------------------------------------

/// Signs a message with the secret key.
///
/// Takes a randomness generator implementing `Rng` and outputs a signature `Signature`.
///
/// # Errors
/// Returns an error of signature generation fails.
pub fn sign<R: Rng>(&self, message: Word, rng: &mut R) -> Signature {
/// Signs a message with this secret key.
#[cfg(feature = "std")]
pub fn sign(&self, message: Word) -> Signature {
use rand::{rngs::StdRng, SeedableRng};

let mut rng = StdRng::from_entropy();
self.sign_with_rng(message, &mut rng)
}

/// Signs a message with the secret key relying on the provided randomness generator.
pub fn sign_with_rng<R: Rng>(&self, message: Word, rng: &mut R) -> Signature {
let mut nonce_bytes = [0u8; SIG_NONCE_LEN];
rng.fill_bytes(&mut nonce_bytes);
let nonce = Nonce::new(nonce_bytes);
Expand Down
2 changes: 1 addition & 1 deletion src/dsa/rpo_falcon512/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ mod tests {
let mut rng = RpoRandomCoin::new(seed);

let sk = SecretKey::with_rng(&mut rng);
let signature = sk.sign(Word::default(), &mut rng);
let signature = sk.sign_with_rng(Word::default(), &mut rng);
let serialized = signature.to_bytes();
let deserialized = Signature::read_from_bytes(&serialized).unwrap();
assert_eq!(signature.sig_poly(), deserialized.sig_poly());
Expand Down

0 comments on commit b751c0f

Please sign in to comment.