Skip to content

Commit

Permalink
Add no-std rand implementation for fields
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby committed Feb 16, 2024
1 parent 47948fa commit 04184f5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ license = "MIT OR Apache-2.0"
# no-alloc, no-std
cfg-if = "1.0"
hex = { version ="=0.4.3", default-features = false }
rand_core = "0.6"
subtle = { version="2.5", default-features = false }
tracing-subscriber = { version = "0.3", default-features = false }
zeroize = { version = "1.7", default-features = false }
Expand Down
16 changes: 12 additions & 4 deletions src/fields/fp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Fiat-crypto generates some unused type aliases, but we don't want to edit the generated code at all.
#![allow(dead_code)]

use cfg_if::cfg_if;
use rand_core::CryptoRngCore;

use crate::EncodingError;

Expand Down Expand Up @@ -115,7 +113,6 @@ impl Fp {
}) // let acc =
}

///
/// Convert bytes into an Fp element, returning None if these bytes are not already reduced.
///
/// This means that values that cannot be produced by encoding a field element will return
Expand All @@ -132,6 +129,17 @@ impl Fp {
pub fn to_bytes(&self) -> [u8; N_8] {
self.to_bytes_le()
}

/// Sample a random field element uniformly.
pub fn rand<R: CryptoRngCore>(rng: &mut R) -> Self {
// Sample wide, reduce
let bytes = {
let mut out = [0u8; N_8 + 16];
rng.fill_bytes(&mut out);
out
};
Self::from_le_bytes_mod_order(&bytes)
}
}

#[cfg(test)]
Expand Down
15 changes: 12 additions & 3 deletions src/fields/fq.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Fiat-crypto generates some unused type aliases, but we don't want to edit the generated code at all.
#![allow(dead_code)]

use cfg_if::cfg_if;
use rand_core::CryptoRngCore;

use crate::EncodingError;

Expand Down Expand Up @@ -116,6 +114,17 @@ impl Fq {
pub fn to_bytes(&self) -> [u8; N_8] {
self.to_bytes_le()
}

/// Sample a random field element uniformly.
pub fn rand<R: CryptoRngCore>(rng: &mut R) -> Self {
// Sample wide, reduce
let bytes = {
let mut out = [0u8; N_8 + 16];
rng.fill_bytes(&mut out);
out
};
Self::from_le_bytes_mod_order(&bytes)
}
}

#[cfg(test)]
Expand Down
15 changes: 12 additions & 3 deletions src/fields/fr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Fiat-crypto generates some unused type aliases, but we don't want to edit the generated code at all.
#![allow(dead_code)]

use cfg_if::cfg_if;
use rand_core::CryptoRngCore;

use crate::EncodingError;

Expand Down Expand Up @@ -108,6 +106,17 @@ impl Fr {
pub fn to_bytes(&self) -> [u8; N_8] {
self.to_bytes_le()
}

/// Sample a random field element uniformly.
pub fn rand<R: CryptoRngCore>(rng: &mut R) -> Self {
// Sample wide, reduce
let bytes = {
let mut out = [0u8; N_8 + 16];
rng.fill_bytes(&mut out);
out
};
Self::from_le_bytes_mod_order(&bytes)
}
}

#[cfg(test)]
Expand Down

0 comments on commit 04184f5

Please sign in to comment.