Skip to content

Commit

Permalink
chore: nits and code shuffling
Browse files Browse the repository at this point in the history
  • Loading branch information
Al-Kindi-0 committed Mar 20, 2024
1 parent 3eaeb4b commit 8d9cad5
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 285 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,30 @@ 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"] }
num = { version = "0.4.1", default-features = false, features = ["alloc", "rand"] }
num = { version = "0.4", default-features = false, features = ["alloc", "rand"] }
num-complex = { version = "0.4.4", default-features = false }
rand = { version = "0.8.5", default-features = false, features = ["getrandom"] }
rand = { version = "0.8", default-features = false, features = ["alloc"] }
rand_utils = { version = "0.8", package = "winter-rand-utils", optional = true }
serde = { version = "1.0", features = [
"derive",
], default-features = false, optional = true }
sha3 = { version = "0.10.8", default-features = false }
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 }

[dev-dependencies]
seq-macro = { version = "0.3" }
criterion = { version = "0.5", features = ["html_reports"] }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
proptest = "1.4"
rand_utils = { version = "0.8", package = "winter-rand-utils" }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
seq-macro = { version = "0.3" }

[build-dependencies]
cc = { version = "1.0", features = ["parallel"], optional = true }
Expand Down
68 changes: 0 additions & 68 deletions src/dsa/rpo_falcon512/error.rs

This file was deleted.

7 changes: 4 additions & 3 deletions src/dsa/rpo_falcon512/keys/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,12 @@ impl Deserializable for PubKeyPoly {
if acc_len >= FALCON_ENCODING_BITS {
acc_len -= FALCON_ENCODING_BITS;
let w = (acc >> acc_len) & 0x3FFF;
if w >= MODULUS as u32 {
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}")));
}
output[output_idx] = FalconFelt::new(w as i16);
output_idx += 1;
}
}

Expand Down
80 changes: 76 additions & 4 deletions src/dsa/rpo_falcon512/keys/secret_key.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use super::{
super::{
math::{
decode_i8, encode_i8, ffldl, ffsampling, gram, normalize_tree, FalconFelt, FastFft,
LdlTree, Polynomial,
},
math::{ffldl, ffsampling, gram, normalize_tree, FalconFelt, FastFft, LdlTree, Polynomial},
signature::SignaturePoly,
ByteReader, ByteWriter, Deserializable, DeserializationError, Nonce, Serializable,
ShortLatticeBasis, Signature, Word, MODULUS, N, SIGMA, SIG_L2_BOUND,
Expand Down Expand Up @@ -318,3 +315,78 @@ fn to_complex_fft(basis: &[Polynomial<i16>; 4]) -> [Polynomial<Complex<f64>>; 4]
let minus_big_f_fft = big_f.map(|cc| -Complex64::new(*cc as f64, 0.0)).fft();
[g_fft, minus_f_fft, big_g_fft, minus_big_f_fft]
}

/// Encodes a sequence of signed integers such that each integer x satisfies |x| < 2^(bits-1)
/// for a given parameter bits. bits can take either the value 6 or 8.
pub fn encode_i8(x: &[i8], bits: usize) -> Option<Vec<u8>> {
let maxv = (1 << (bits - 1)) - 1_usize;
let maxv = maxv as i8;
let minv = -maxv;

for &c in x {
if c > maxv || c < minv {
return None;
}
}

let out_len = ((N * bits) + 7) >> 3;
let mut buf = vec![0_u8; out_len];

let mut acc = 0_u32;
let mut acc_len = 0;
let mask = ((1_u16 << bits) - 1) as u8;

let mut input_pos = 0;
for &c in x {
acc = (acc << bits) | (c as u8 & mask) as u32;
acc_len += bits;
while acc_len >= 8 {
acc_len -= 8;
buf[input_pos] = (acc >> acc_len) as u8;
input_pos += 1;
}
}
if acc_len > 0 {
buf[input_pos] = (acc >> (8 - acc_len)) as u8;
}

Some(buf)
}

/// Decodes a sequence of bytes into a sequence of signed integers such that each integer x
/// satisfies |x| < 2^(bits-1) for a given parameter bits. bits can take either the value 6 or 8.
pub fn decode_i8(buf: &[u8], bits: usize) -> Option<Vec<i8>> {
let mut x = [0_i8; N];

let mut i = 0;
let mut j = 0;
let mut acc = 0_u32;
let mut acc_len = 0;
let mask = (1_u32 << bits) - 1;
let a = (1 << bits) as u8;
let b = ((1 << (bits - 1)) - 1) as u8;

while i < N {
acc = (acc << 8) | (buf[j] as u32);
j += 1;
acc_len += 8;

while acc_len >= bits && i < N {
acc_len -= bits;
let w = (acc >> acc_len) & mask;

let w = w as u8;

let z = if w > b { w as i8 - a as i8 } else { w as i8 };

x[i] = z;
i += 1;
}
}

if (acc & ((1u32 << acc_len) - 1)) == 0 {
Some(x.to_vec())
} else {
None
}
}
77 changes: 0 additions & 77 deletions src/dsa/rpo_falcon512/math/codec.rs

This file was deleted.

13 changes: 13 additions & 0 deletions src/dsa/rpo_falcon512/math/field.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{fft::CyclotomicFourier, Inverse, MODULUS};
use alloc::string::{String, ToString};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use num::{One, Zero};

Expand Down Expand Up @@ -157,3 +158,15 @@ impl CyclotomicFourier for FalconFelt {
a
}
}

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))
} else {
Err("Value outside valid range for a field element".to_string())
}
}
}
3 changes: 0 additions & 3 deletions src/dsa/rpo_falcon512/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ use self::samplerz::sampler_z;
mod polynomial;
pub use polynomial::Polynomial;

mod codec;
pub use codec::{decode_i8, encode_i8};

pub trait Inverse: Copy + Zero + MulAssign + One {
/// Gets the inverse of a, or zero if it is zero.
fn inverse_or_zero(self) -> Self;
Expand Down
2 changes: 0 additions & 2 deletions src/dsa/rpo_falcon512/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ use crate::{
Felt, Word, ZERO,
};

mod error;
mod hash_to_point;
mod keys;
mod math;
mod signature;

use self::math::Polynomial;
pub use error::FalconError;
pub use keys::{PublicKey, SecretKey};
pub use signature::Signature;

Expand Down
Loading

0 comments on commit 8d9cad5

Please sign in to comment.