Skip to content

Commit

Permalink
refactor: minor code organization improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbinth committed Mar 20, 2024
1 parent 94ed313 commit 6f6906d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 36 deletions.
29 changes: 13 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,29 @@ harness = false

[features]
default = ["std"]
executable = ["dep:clap", "dep:rand_utils", "std"]
serde = ["dep:serde", "serde?/alloc", "winter_math/serde"]
executable = ["dep:clap", "dep:rand-utils", "std"]
serde = ["dep:serde", "serde?/alloc", "winter-math/serde"]
std = [
"blake3/std",
"dep:cc",
"winter_crypto/std",
"winter_math/std",
"winter_utils/std",
"winter-crypto/std",
"winter-math/std",
"winter-utils/std",
"rand/std",
]

[dependencies]
blake3 = { version = "1.5", default-features = false }
clap = { version = "4.5", features = ["derive"], optional = true }
getrandom = { version = "0.2", features = ["js"] }
clap = { version = "4.5", optional = true, features = ["derive"] }
num = { version = "0.4", default-features = false, features = ["alloc"] }
num-complex = { version = "0.4.4", default-features = false }
rand = { version = "0.8", default-features = false, features = ["getrandom"] }
rand_utils = { version = "0.8", package = "winter-rand-utils", optional = true }
serde = { version = "1.0", features = [
"derive",
], default-features = false, optional = true }
rand = { version = "0.8", default-features = false }
rand-utils = { version = "0.8", package = "winter-rand-utils", optional = true }
serde = { version = "1.0", default-features = false, optional = true, features = ["derive"] }
sha3 = { version = "0.10", default-features = false }
winter_crypto = { version = "0.8", package = "winter-crypto", default-features = false }
winter_math = { version = "0.8", package = "winter-math", default-features = false }
winter_utils = { version = "0.8", package = "winter-utils", default-features = false }
winter-crypto = { version = "0.8", default-features = false }
winter-math = { version = "0.8", default-features = false }
winter-utils = { version = "0.8", default-features = false }

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
Expand All @@ -68,5 +65,5 @@ rand_utils = { version = "0.8", package = "winter-rand-utils" }
seq-macro = { version = "0.3" }

[build-dependencies]
cc = { version = "1.0", features = ["parallel"], optional = true }
cc = { version = "1.0", optional = true, features = ["parallel"] }
glob = "0.3"
2 changes: 1 addition & 1 deletion src/dsa/rpo_falcon512/keys/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{
math::{FalconFelt, Polynomial},
ByteReader, ByteWriter, Deserializable, DeserializationError, Felt, Serializable, Signature,
Word, MODULUS,
Word,
};

mod public_key;
Expand Down
15 changes: 8 additions & 7 deletions src/dsa/rpo_falcon512/keys/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::dsa::rpo_falcon512::FALCON_ENCODING_BITS;
use super::{
super::{Rpo256, LOG_N, N, PK_LEN},
ByteReader, ByteWriter, Deserializable, DeserializationError, FalconFelt, Felt, Polynomial,
Serializable, Signature, Word, MODULUS,
Serializable, Signature, Word,
};
use alloc::string::ToString;
use core::ops::Deref;
Expand Down Expand Up @@ -117,12 +117,13 @@ impl Deserializable for PubKeyPoly {
if acc_len >= FALCON_ENCODING_BITS {
acc_len -= FALCON_ENCODING_BITS;
let w = (acc >> acc_len) & 0x3FFF;
if let Ok(value) = w.try_into() {
output[output_idx] = FalconFelt::new(value);
output_idx += 1;
} else {
return Err(DeserializationError::InvalidValue(format!("Failed to decode public key: coefficient {w} is greater than or equal to the field modulus {MODULUS}")));
}
let element = w.try_into().map_err(|err| {
DeserializationError::InvalidValue(format!(
"Failed to decode public key: {err}"
))
})?;
output[output_idx] = element;
output_idx += 1;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/dsa/rpo_falcon512/keys/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ impl SecretKey {

break [-s0, s1];
};

let s1 = bold_s[0].ifft();
let s2 = bold_s[1].ifft();
let s1_coef: [i16; N] = s1
Expand Down
10 changes: 5 additions & 5 deletions src/dsa/rpo_falcon512/math/field.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{fft::CyclotomicFourier, Inverse, MODULUS};
use alloc::string::{String, ToString};
use alloc::string::String;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use num::{One, Zero};

Expand Down Expand Up @@ -162,11 +162,11 @@ impl CyclotomicFourier for FalconFelt {
impl TryFrom<u32> for FalconFelt {
type Error = String;

fn try_from(x: u32) -> Result<Self, Self::Error> {
if let Ok(value) = x.try_into() {
Ok(FalconFelt::new(value))
fn try_from(value: u32) -> Result<Self, Self::Error> {
if value >= MODULUS as u32 {
Err(format!("value {value} is greater than or equal to the field modulus {MODULUS}"))
} else {
Err("Value outside valid range for a field element".to_string())
Ok(FalconFelt::new(value as i16))
}
}
}
13 changes: 6 additions & 7 deletions src/dsa/rpo_falcon512/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ impl Signature {
/// against the secret key matching the specified public key commitment.
pub fn verify(&self, message: Word, pubkey_com: Word) -> bool {
let c = hash_to_point_rpo256(message, &self.nonce);
let s1 = self.s1.clone();
let s2 = self.s2.clone();
let s1_fft = s1.fft();
let s2_fft = s2.fft();

let s1_fft = self.s1.fft();
let s2_fft = self.s2.fft();
let c_fft = c.fft();

// recover the public key polynomial using h = s2^(-1) * (c - s1)
Expand All @@ -100,7 +99,7 @@ impl Signature {
let h_felt: Polynomial<Felt> = h.clone().into();
let h_digest: Word = Rpo256::hash_elements(&h_felt.coefficients).into();

h_digest == pubkey_com && verify_helper(c, s2, h.into())
h_digest == pubkey_com && verify_helper(&c, &self.s2, &h.into())
}
}

Expand Down Expand Up @@ -301,7 +300,7 @@ impl Deserializable for SignaturePoly {
"Failed to decode signature: Non-zero unused bits in the last byte".to_string(),
));
}
Ok(SignaturePoly::from(Polynomial::new(coefficients.to_vec())))
Ok(Polynomial::new(coefficients.to_vec()).into())
}
}

Expand All @@ -311,7 +310,7 @@ impl Deserializable for SignaturePoly {
/// Takes the hash-to-point polynomial `c` of a message, the signature polynomial over
/// the message `s2` and a public key polynomial and returns `true` is the signature is a valid
/// signature for the given parameters, otherwise it returns `false`.
fn verify_helper(c: Polynomial<FalconFelt>, s2: SignaturePoly, h: PubKeyPoly) -> bool {
fn verify_helper(c: &Polynomial<FalconFelt>, s2: &SignaturePoly, h: &PubKeyPoly) -> bool {
let h_fft = h.fft();
let s2_fft = s2.fft();
let c_fft = c.fft();
Expand Down

0 comments on commit 6f6906d

Please sign in to comment.