Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ proptest = "^1.4.0"
prost = "^0.12.4"
prost-build = "^0.12.3"
pulp = "^0.21.5"
rand = "^0.8.5"
rand_chacha = "^0.3.1"
rand = "^0.9.2"
rand_chacha = "^0.9.0"
sha2 = "^0.10.8"
thiserror = "^1.0.58"
zeroize = "^1.8.0"
Expand Down
4 changes: 2 additions & 2 deletions crates/fhe-math/benches/ntt.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use fhe_math::{ntt::NttOperator, zq::Modulus};
use rand::thread_rng;
use rand::rng;
use std::sync::Arc;

pub fn ntt_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("ntt");
group.sample_size(50);
let mut rng = thread_rng();
let mut rng = rng();

for vector_size in [1024usize, 4096].iter() {
for p in [4611686018326724609u64, 40961u64] {
Expand Down
4 changes: 2 additions & 2 deletions crates/fhe-math/benches/rns.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use fhe_math::rns::{RnsContext, RnsScaler, ScalingFactor};
use num_bigint::BigUint;
use rand::{thread_rng, RngCore};
use rand::{rng, RngCore};
use std::sync::Arc;

pub fn rns_benchmark(c: &mut Criterion) {
Expand All @@ -20,7 +20,7 @@ pub fn rns_benchmark(c: &mut Criterion) {
4611686018106523649,
];

let mut rng = thread_rng();
let mut rng = rng();
let mut x = vec![];
for qi in &q {
x.push(rng.next_u64() % *qi);
Expand Down
12 changes: 6 additions & 6 deletions crates/fhe-math/benches/rq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use criterion::measurement::WallTime;
use criterion::{criterion_group, criterion_main, BenchmarkGroup, BenchmarkId, Criterion};
use fhe_math::rq::*;
use itertools::{izip, Itertools};
use rand::thread_rng;
use rand::rng;
use std::{
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
sync::Arc,
Expand Down Expand Up @@ -33,7 +33,7 @@ macro_rules! bench_op {
$name.to_string()
};
let mut group = create_group($c, name);
let mut rng = thread_rng();
let mut rng = rng();

for degree in DEGREE {
let ctx = Arc::new(Context::new(&MODULI[..1], *degree).unwrap());
Expand Down Expand Up @@ -61,7 +61,7 @@ macro_rules! bench_op_unary {
$name.to_string()
};
let mut group = create_group($c, name);
let mut rng = thread_rng();
let mut rng = rng();

for degree in DEGREE {
let ctx = Arc::new(Context::new(&MODULI[..1], *degree).unwrap());
Expand Down Expand Up @@ -89,7 +89,7 @@ macro_rules! bench_op_assign {
$name.to_string()
};
let mut group = create_group($c, name);
let mut rng = thread_rng();
let mut rng = rng();

for degree in DEGREE {
let ctx = Arc::new(Context::new(&MODULI[..1], *degree).unwrap());
Expand Down Expand Up @@ -123,7 +123,7 @@ pub fn rq_op_benchmark(c: &mut Criterion) {

pub fn rq_dot_product(c: &mut Criterion) {
let mut group = create_group(c, "rq_dot_product".to_string());
let mut rng = thread_rng();
let mut rng = rng();
for degree in DEGREE {
for i in [1, 4] {
let ctx = Arc::new(Context::new(&MODULI[..i], *degree).unwrap());
Expand Down Expand Up @@ -178,7 +178,7 @@ pub fn rq_benchmark(c: &mut Criterion) {
group.warm_up_time(Duration::from_millis(100));
group.measurement_time(Duration::from_secs(1));

let mut rng = thread_rng();
let mut rng = rng();
for degree in DEGREE {
for nmoduli in 1..=MODULI.len() {
if !nmoduli.is_power_of_two() {
Expand Down
4 changes: 2 additions & 2 deletions crates/fhe-math/benches/zq.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use fhe_math::zq::Modulus;
use rand::thread_rng;
use rand::rng;

pub fn zq_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("zq");
group.sample_size(50);

let p = 4611686018326724609;
let mut rng = thread_rng();
let mut rng = rng();

for vector_size in [1024usize, 4096].iter() {
let q = Modulus::new(p).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions crates/fhe-math/src/ntt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) fn supports_ntt(p: u64, n: usize) -> bool {

#[cfg(test)]
mod tests {
use rand::thread_rng;
use rand::rng;

use super::{supports_ntt, NttOperator};
use crate::zq::Modulus;
Expand All @@ -50,7 +50,7 @@ mod tests {
#[test]
fn bijection() {
let ntests = 100;
let mut rng = thread_rng();
let mut rng = rng();

for size in [32, 1024] {
for p in [1153, 4611686018326724609] {
Expand Down Expand Up @@ -84,7 +84,7 @@ mod tests {
#[test]
fn forward_lazy() {
let ntests = 100;
let mut rng = thread_rng();
let mut rng = rng();

for size in [32, 1024] {
for p in [1153, 4611686018326724609] {
Expand Down
2 changes: 1 addition & 1 deletion crates/fhe-math/src/ntt/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl NttOperator {

let mut rng: ChaCha8Rng = SeedableRng::seed_from_u64(0);
for _ in 0..100 {
let mut root = rng.gen_range(0..p.p);
let mut root = rng.random_range(0..p.p);
root = p.pow(root, lambda);
if Self::is_primitive_root(root, 2 * n, p) {
return root;
Expand Down
2 changes: 1 addition & 1 deletion crates/fhe-math/src/rns/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ mod tests {
BigUint::from(product - 1)
);

let mut rng = rand::thread_rng();
let mut rng = rand::rng();

for _ in 0..ntests {
let b = BigUint::from(rng.next_u64() % product);
Expand Down
6 changes: 3 additions & 3 deletions crates/fhe-math/src/rns/scaler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ mod tests {
use ndarray::ArrayView1;
use num_bigint::BigUint;
use num_traits::{ToPrimitive, Zero};
use rand::{thread_rng, RngCore};
use rand::{rng, RngCore};

#[test]
fn constructor() -> Result<(), Box<dyn Error>> {
Expand All @@ -368,7 +368,7 @@ mod tests {
fn scale_same_context() -> Result<(), Box<dyn Error>> {
let ntests = 1000;
let q = Arc::new(RnsContext::new(&[4u64, 4611686018326724609, 1153])?);
let mut rng = thread_rng();
let mut rng = rng();

for numerator in &[1u64, 2, 3, 100, 1000, 4611686018326724610] {
for denominator in &[1u64, 2, 3, 4, 100, 101, 1000, 1001, 4611686018326724610] {
Expand Down Expand Up @@ -422,7 +422,7 @@ mod tests {
4611686018106523649,
4611686018058289153,
])?);
let mut rng = thread_rng();
let mut rng = rng();

for numerator in &[1u64, 2, 3, 100, 1000, 4611686018326724610] {
for denominator in &[1u64, 2, 3, 4, 100, 101, 1000, 1001, 4611686018326724610] {
Expand Down
6 changes: 3 additions & 3 deletions crates/fhe-math/src/rq/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,14 @@ mod tests {
Error as CrateError,
};
use num_bigint::BigUint;
use rand::thread_rng;
use rand::rng;
use std::{error::Error, sync::Arc};

static MODULI: &[u64; 3] = &[1153, 4611686018326724609, 4611686018309947393];

#[test]
fn proto() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let mut rng = rng();
for modulus in MODULI {
let ctx = Arc::new(Context::new(&[*modulus], 16)?);
let p = Poly::random(&ctx, Representation::PowerBasis, &mut rng);
Expand Down Expand Up @@ -649,7 +649,7 @@ mod tests {

#[test]
fn biguint() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let mut rng = rng();
for _ in 0..100 {
for modulus in MODULI {
let ctx = Arc::new(Context::new(&[*modulus], 16)?);
Expand Down
31 changes: 16 additions & 15 deletions crates/fhe-math/src/rq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ mod tests {
use itertools::Itertools;
use num_bigint::BigUint;
use num_traits::{One, Zero};
use rand::{thread_rng, Rng, SeedableRng};
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha8Rng;
use std::{error::Error, sync::Arc};

Expand Down Expand Up @@ -623,10 +623,10 @@ mod tests {

#[test]
fn random() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let mut rng = rand::rng();
for _ in 0..100 {
let mut seed = <ChaCha8Rng as SeedableRng>::Seed::default();
thread_rng().fill(&mut seed);
rand::rng().fill(&mut seed);

for modulus in MODULI {
let ctx = Arc::new(Context::new(&[*modulus], 16)?);
Expand All @@ -640,7 +640,7 @@ mod tests {
let q = Poly::random_from_seed(&ctx, Representation::Ntt, seed);
assert_eq!(p, q);

thread_rng().fill(&mut seed);
rand::rng().fill(&mut seed);
let p = Poly::random_from_seed(&ctx, Representation::Ntt, seed);
assert_ne!(p, q);

Expand All @@ -653,7 +653,7 @@ mod tests {

#[test]
fn coefficients() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let mut rng = rand::rng();
for _ in 0..50 {
for modulus in MODULI {
let ctx = Arc::new(Context::new(&[*modulus], 16)?);
Expand Down Expand Up @@ -688,7 +688,7 @@ mod tests {

#[test]
fn allow_variable_time_computations() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let mut rng = rand::rng();
for modulus in MODULI {
let ctx = Arc::new(Context::new(&[*modulus], 16)?);
let mut p = Poly::random(&ctx, Representation::default(), &mut rng);
Expand Down Expand Up @@ -739,7 +739,7 @@ mod tests {

#[test]
fn change_representation() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let mut rng = rand::rng();
let ctx = Arc::new(Context::new(MODULI, 16)?);

let mut p = Poly::random(&ctx, Representation::default(), &mut rng);
Expand Down Expand Up @@ -783,7 +783,7 @@ mod tests {

#[test]
fn override_representation() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let mut rng = rand::rng();
let ctx = Arc::new(Context::new(MODULI, 16)?);

let mut p = Poly::random(&ctx, Representation::PowerBasis, &mut rng);
Expand Down Expand Up @@ -816,7 +816,7 @@ mod tests {

#[test]
fn small() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let mut rng = rand::rng();
for modulus in MODULI {
let ctx = Arc::new(Context::new(&[*modulus], 16)?);
let q = Modulus::new(*modulus).unwrap();
Expand Down Expand Up @@ -846,7 +846,8 @@ mod tests {
// Generate a very large polynomial to check the variance (here equal to 8).
let ctx = Arc::new(Context::new(&[4611686018326724609], 1 << 18)?);
let q = Modulus::new(4611686018326724609).unwrap();
let p = Poly::small(&ctx, Representation::PowerBasis, 16, &mut thread_rng())?;
let mut rng = rand::rng();
let p = Poly::small(&ctx, Representation::PowerBasis, 16, &mut rng)?;
let coefficients = p.coefficients().to_slice().unwrap();
let v = unsafe { q.center_vec_vt(coefficients) };
assert!(v.iter().map(|vi| vi.abs()).max().unwrap() <= 32);
Expand All @@ -857,7 +858,7 @@ mod tests {

#[test]
fn substitute() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let mut rng = rand::rng();
for modulus in MODULI {
let ctx = Arc::new(Context::new(&[*modulus], 16)?);
let p = Poly::random(&ctx, Representation::PowerBasis, &mut rng);
Expand Down Expand Up @@ -953,7 +954,7 @@ mod tests {

#[test]
fn switch_down() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let mut rng = rand::rng();
let ntests = 100;
let ctx = Arc::new(Context::new(MODULI, 16)?);

Expand Down Expand Up @@ -999,7 +1000,7 @@ mod tests {

#[test]
fn switch_down_to() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let mut rng = rand::rng();
let ntests = 100;
let ctx1 = Arc::new(Context::new(MODULI, 16)?);
let ctx2 = Arc::new(Context::new(&MODULI[..2], 16)?);
Expand All @@ -1025,7 +1026,7 @@ mod tests {

#[test]
fn switch() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let mut rng = rand::rng();
let ntests = 100;
let ctx1 = Arc::new(Context::new(&MODULI[..2], 16)?);
let ctx2 = Arc::new(Context::new(&MODULI[3..], 16)?);
Expand All @@ -1050,7 +1051,7 @@ mod tests {

#[test]
fn mul_x_power() -> Result<(), Box<dyn Error>> {
let mut rng = thread_rng();
let mut rng = rand::rng();
let ctx = Arc::new(Context::new(MODULI, 16)?);
let e = Poly::random(&ctx, Representation::Ntt, &mut rng).multiply_inverse_power_of_x(1);
assert!(e.is_err());
Expand Down
Loading
Loading