Skip to content

Commit

Permalink
refactor: minor updates signature tests, comments, and private key co…
Browse files Browse the repository at this point in the history
…nstructor
  • Loading branch information
bobbinth committed Mar 22, 2024
1 parent bc78890 commit 8f9ccfa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ std = [
[dependencies]
blake3 = { version = "1.5", default-features = false }
clap = { version = "4.5", optional = true, features = ["derive"] }
getrandom = { version = "0.2", features = ["js"] }
num = { version = "0.4", default-features = false, features = ["alloc", "libm"] }
num-complex = { version = "0.4.4", default-features = false }
rand = { version = "0.8", default-features = false }
Expand All @@ -62,6 +61,7 @@ winter-utils = { version = "0.8", default-features = false }

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
getrandom = { version = "0.2", features = ["js"] }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
proptest = "1.4"
rand_utils = { version = "0.8", package = "winter-rand-utils" }
Expand Down
4 changes: 1 addition & 3 deletions src/dsa/rpo_falcon512/keys/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ impl SecretKey {
/// Generates a secret key from OS-provided randomness.
#[cfg(feature = "std")]
pub fn new() -> Self {
use rand::{rngs::StdRng, RngCore, SeedableRng};
use rand::{rngs::StdRng, SeedableRng};

let mut seed: [u8; 32] = [0; 32];
let mut rng = StdRng::from_entropy();
rng.fill_bytes(&mut seed);
Self::with_rng(&mut rng)
}

Expand Down
32 changes: 14 additions & 18 deletions src/dsa/rpo_falcon512/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,13 @@ use num::Zero;
/// function. c is a polynomial that is the hash-to-point of the message being signed.
///
/// The polynomial h is serialized as:
///
/// 1. 1 byte representing the log2(512) i.e., 9.
/// 2. 896 bytes for the public key itself.
///
/// The signature is serialized as:
/// 1. A header byte specifying the algorithm used to encode the coefficients of the `s2` polynomial
/// together with the degree of the irreducible polynomial phi.
/// The general format of this byte is 0b0cc1nnnn where:
/// a. cc is either 01 when the compressed encoding algorithm is used and 10 when the
/// uncompressed algorithm is used.
/// b. nnnn is log2(N) where N is the degree of the irreducible polynomial phi.
/// The current implementation works always with cc equal to 0b01 and nnnn equal to 0b1001 and
/// thus the header byte is always equal to 0b00111001.
/// together with the degree of the irreducible polynomial phi. For RPO Falcon512, the header
/// byte is set to `10111001` (see more in [SignatureHeader]).

Check failure on line 41 in src/dsa/rpo_falcon512/signature.rs

View workflow job for this annotation

GitHub Actions / Verify the docs on stable

public documentation for `Signature` links to private item `SignatureHeader`
/// 2. 40 bytes for the nonce.
/// 4. 625 bytes encoding the `s2` polynomial above.
///
Expand Down Expand Up @@ -133,15 +127,15 @@ pub struct SignatureHeader(u8);

impl Default for SignatureHeader {
/// According to section 3.11.3 in the specification [1], the signature header has the format
/// `0 c c 1 n n n n` where:
/// `0cc1nnnn` where:
///
/// 1. `c c` signifies the encoding method. `0 1` denotes using the compression encoding method
/// and `1 0` denotes encoding using the uncompressed method.
/// 2. `n n n n` encodes `LOG_N`.
/// 1. `cc` signifies the encoding method. `01` denotes using the compression encoding method
/// and `10` denotes encoding using the uncompressed method.
/// 2. `nnnn` encodes `LOG_N`.
///
/// For RPO Falcon 512 we use compression encoding and N = 512. Moreover, to differentiate the
/// RPO Falcon variant from the reference variant using SHAKE256, we flip the first bit in the
/// header. Thus, for RPO Falcon 512 the header is `1 0 1 1 1 0 0 1`
/// header. Thus, for RPO Falcon 512 the header is `10111001`
///
/// [1]: https://falcon-sign.info/falcon.pdf
fn default() -> Self {
Expand Down Expand Up @@ -358,16 +352,18 @@ fn are_coefficients_valid(x: &[i16]) -> bool {
// TESTS
// ================================================================================================

#[cfg(all(test, feature = "std"))]
#[cfg(test)]
mod tests {
use super::{super::SecretKey, *};
use rand::rngs::OsRng;
use crate::{rand::RpoRandomCoin, ZERO};

#[test]
fn test_serialization_round_trip() {
let key = SecretKey::new();
let mut rng = OsRng;
let signature = key.sign(Word::default(), &mut rng);
let seed = [ZERO; 4];
let mut rng = RpoRandomCoin::new(seed);

let sk = SecretKey::with_rng(&mut rng);
let signature = sk.sign(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 8f9ccfa

Please sign in to comment.