mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-03 22:33:06 +00:00
feature-gate rand
This commit is contained in:
parent
94def938aa
commit
e7216f2683
@ -4,12 +4,16 @@ description = "Finite field arithmetic"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
default = ["rand"]
|
||||
rand = ["dep:rand"]
|
||||
|
||||
[dependencies]
|
||||
plonky2_util = { path = "../util" }
|
||||
anyhow = "1.0.40"
|
||||
itertools = "0.10.0"
|
||||
num = { version = "0.4", features = [ "rand" ] }
|
||||
rand = "0.8.4"
|
||||
rand = { optional = true, version = "0.8.4" }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
unroll = "0.1.5"
|
||||
static_assertions = "1.1.0"
|
||||
|
||||
@ -4,7 +4,6 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssi
|
||||
|
||||
use num::bigint::BigUint;
|
||||
use num::Integer;
|
||||
use rand::Rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::extension::{Extendable, FieldExtension, Frobenius, OEF};
|
||||
@ -103,7 +102,8 @@ impl<F: Extendable<2>> Field for QuadraticExtension<F> {
|
||||
F::from_noncanonical_u128(n).into()
|
||||
}
|
||||
|
||||
fn rand_from_rng<R: Rng>(rng: &mut R) -> Self {
|
||||
#[cfg(feature = "rand")]
|
||||
fn rand_from_rng<R: rand::Rng>(rng: &mut R) -> Self {
|
||||
Self([F::rand_from_rng(rng), F::rand_from_rng(rng)])
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,7 +5,6 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssi
|
||||
use num::bigint::BigUint;
|
||||
use num::traits::Pow;
|
||||
use num::Integer;
|
||||
use rand::Rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::extension::{Extendable, FieldExtension, Frobenius, OEF};
|
||||
@ -115,7 +114,8 @@ impl<F: Extendable<4>> Field for QuarticExtension<F> {
|
||||
F::from_noncanonical_u128(n).into()
|
||||
}
|
||||
|
||||
fn rand_from_rng<R: Rng>(rng: &mut R) -> Self {
|
||||
#[cfg(feature = "rand")]
|
||||
fn rand_from_rng<R: rand::Rng>(rng: &mut R) -> Self {
|
||||
Self::from_basefield_array([
|
||||
F::rand_from_rng(rng),
|
||||
F::rand_from_rng(rng),
|
||||
|
||||
@ -4,7 +4,6 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssi
|
||||
|
||||
use num::bigint::BigUint;
|
||||
use num::traits::Pow;
|
||||
use rand::Rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::extension::{Extendable, FieldExtension, Frobenius, OEF};
|
||||
@ -112,7 +111,8 @@ impl<F: Extendable<5>> Field for QuinticExtension<F> {
|
||||
F::from_noncanonical_u128(n).into()
|
||||
}
|
||||
|
||||
fn rand_from_rng<R: Rng>(rng: &mut R) -> Self {
|
||||
#[cfg(feature = "rand")]
|
||||
fn rand_from_rng<R: rand::Rng>(rng: &mut R) -> Self {
|
||||
Self::from_basefield_array([
|
||||
F::rand_from_rng(rng),
|
||||
F::rand_from_rng(rng),
|
||||
|
||||
@ -6,7 +6,6 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssi
|
||||
|
||||
use num::{BigUint, Integer};
|
||||
use plonky2_util::{assume, branch_hint};
|
||||
use rand::Rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::inversion::try_inverse_u64;
|
||||
@ -105,7 +104,8 @@ impl Field for GoldilocksField {
|
||||
reduce128(n)
|
||||
}
|
||||
|
||||
fn rand_from_rng<R: Rng>(rng: &mut R) -> Self {
|
||||
#[cfg(feature = "rand")]
|
||||
fn rand_from_rng<R: rand::Rng>(rng: &mut R) -> Self {
|
||||
Self::from_canonical_u64(rng.gen_range(0..Self::ORDER))
|
||||
}
|
||||
|
||||
|
||||
@ -5,9 +5,8 @@ use std::iter::{Product, Sum};
|
||||
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||
|
||||
use itertools::Itertools;
|
||||
use num::bigint::{BigUint, RandBigInt};
|
||||
use num::bigint::BigUint;
|
||||
use num::{Integer, One};
|
||||
use rand::Rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::types::{Field, PrimeField};
|
||||
@ -133,7 +132,9 @@ impl Field for Secp256K1Base {
|
||||
Self([n.0, n.1 as u64, 0, 0])
|
||||
}
|
||||
|
||||
fn rand_from_rng<R: Rng>(rng: &mut R) -> Self {
|
||||
#[cfg(feature = "rand")]
|
||||
fn rand_from_rng<R: rand::Rng>(rng: &mut R) -> Self {
|
||||
use num::bigint::RandBigInt;
|
||||
Self::from_biguint(rng.gen_biguint_below(&Self::order()))
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,9 +6,8 @@ use std::iter::{Product, Sum};
|
||||
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||
|
||||
use itertools::Itertools;
|
||||
use num::bigint::{BigUint, RandBigInt};
|
||||
use num::bigint::BigUint;
|
||||
use num::{Integer, One};
|
||||
use rand::Rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::types::{Field, PrimeField};
|
||||
@ -142,7 +141,9 @@ impl Field for Secp256K1Scalar {
|
||||
Self([n.0, n.1 as u64, 0, 0])
|
||||
}
|
||||
|
||||
fn rand_from_rng<R: Rng>(rng: &mut R) -> Self {
|
||||
#[cfg(feature = "rand")]
|
||||
fn rand_from_rng<R: rand::Rng>(rng: &mut R) -> Self {
|
||||
use num::bigint::RandBigInt;
|
||||
Self::from_biguint(rng.gen_biguint_below(&Self::order()))
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,7 +6,6 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssi
|
||||
use num::bigint::BigUint;
|
||||
use num::{Integer, One, ToPrimitive, Zero};
|
||||
use plonky2_util::bits_u64;
|
||||
use rand::Rng;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
||||
@ -312,7 +311,8 @@ pub trait Field:
|
||||
Self::from_noncanonical_u128(n)
|
||||
}
|
||||
|
||||
fn rand_from_rng<R: Rng>(rng: &mut R) -> Self;
|
||||
#[cfg(feature = "rand")]
|
||||
fn rand_from_rng<R: rand::Rng>(rng: &mut R) -> Self;
|
||||
|
||||
fn exp_power_of_2(&self, power_log: usize) -> Self {
|
||||
let mut res = *self;
|
||||
@ -391,14 +391,17 @@ pub trait Field:
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rand")]
|
||||
fn rand() -> Self {
|
||||
Self::rand_from_rng(&mut rand::thread_rng())
|
||||
}
|
||||
|
||||
#[cfg(feature = "rand")]
|
||||
fn rand_arr<const N: usize>() -> [Self; N] {
|
||||
Self::rand_vec(N).try_into().unwrap()
|
||||
}
|
||||
|
||||
#[cfg(feature = "rand")]
|
||||
fn rand_vec(n: usize) -> Vec<Self> {
|
||||
(0..n).map(|_| Self::rand()).collect()
|
||||
}
|
||||
|
||||
@ -11,8 +11,11 @@ edition = "2021"
|
||||
default-run = "generate_constants"
|
||||
|
||||
[features]
|
||||
default = ["parallel"]
|
||||
default = ["parallel", "rand", "rand_chacha", "gate_testing"]
|
||||
parallel = ["maybe_rayon/parallel"]
|
||||
rand = ["dep:rand", "plonky2_field/rand"]
|
||||
gate_testing = ["rand"]
|
||||
rand_chacha = ["dep:rand_chacha"]
|
||||
|
||||
[dependencies]
|
||||
plonky2_field = { path = "../field" }
|
||||
@ -21,8 +24,8 @@ env_logger = "0.9.0"
|
||||
log = "0.4.14"
|
||||
itertools = "0.10.0"
|
||||
num = { version = "0.4", features = [ "rand" ] }
|
||||
rand = "0.8.4"
|
||||
rand_chacha = "0.3.1"
|
||||
rand = { version = "0.8.4", optional = true }
|
||||
rand_chacha = { version = "0.3.1", optional = true }
|
||||
maybe_rayon = { path = "../maybe_rayon" }
|
||||
unroll = "0.1.5"
|
||||
anyhow = "1.0.40"
|
||||
@ -32,6 +35,8 @@ keccak-hash = "0.8.0"
|
||||
static_assertions = "1.1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
rand = "0.8.4"
|
||||
rand_chacha = "0.3.1"
|
||||
criterion = "0.3.5"
|
||||
tynm = "0.1.6"
|
||||
structopt = "0.3.26"
|
||||
@ -41,6 +46,10 @@ rayon = "1.5.1"
|
||||
[target.'cfg(not(target_env = "msvc"))'.dev-dependencies]
|
||||
jemallocator = "0.3.2"
|
||||
|
||||
[[bin]]
|
||||
name = "generate_constants"
|
||||
required-features = ["rand", "rand_chacha"]
|
||||
|
||||
[[bench]]
|
||||
name = "field_arithmetic"
|
||||
harness = false
|
||||
|
||||
@ -24,4 +24,5 @@ pub mod util;
|
||||
|
||||
// Can't use #[cfg(test)] here because it needs to be visible to other crates.
|
||||
// See https://github.com/rust-lang/cargo/issues/8379
|
||||
#[cfg(any(feature = "gate_testing", test))]
|
||||
pub mod gate_testing;
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
use plonky2_field::goldilocks_field::GoldilocksField;
|
||||
use plonky2_field::types::{Field, PrimeField64};
|
||||
use rand::Rng;
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
use crate::hash::poseidon::Poseidon;
|
||||
@ -37,7 +36,8 @@ impl<F: Field> HashOut<F> {
|
||||
Self { elements }
|
||||
}
|
||||
|
||||
pub fn rand_from_rng<R: Rng>(rng: &mut R) -> Self {
|
||||
#[cfg(feature = "parallel")]
|
||||
pub fn rand_from_rng<R: rand::Rng>(rng: &mut R) -> Self {
|
||||
Self {
|
||||
elements: [
|
||||
F::rand_from_rng(rng),
|
||||
@ -115,12 +115,14 @@ pub struct MerkleCapTarget(pub Vec<HashOutTarget>);
|
||||
pub struct BytesHash<const N: usize>(pub [u8; N]);
|
||||
|
||||
impl<const N: usize> BytesHash<N> {
|
||||
pub fn rand_from_rng<R: Rng>(rng: &mut R) -> Self {
|
||||
#[cfg(feature = "parallel")]
|
||||
pub fn rand_from_rng<R: rand::Rng>(rng: &mut R) -> Self {
|
||||
let mut buf = [0; N];
|
||||
rng.fill_bytes(&mut buf);
|
||||
Self(buf)
|
||||
}
|
||||
|
||||
#[cfg(feature = "rand")]
|
||||
pub fn rand() -> Self {
|
||||
Self::rand_from_rng(&mut rand::thread_rng())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user