diff --git a/Cargo.toml b/Cargo.toml index fd1d56b..b9edf12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = ["integration", "mp-spdz-rs", "offline-phase", "online-phase"] +resolver = "3" [profile.bench] opt-level = 3 diff --git a/README.md b/README.md index 1329521..d25627e 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ use ark_mpc::{ PARTY0, PARTY1, }; use ark_curve25519::EdwardsProjective as Curve25519Projective; -use rand::thread_rng; +use rand::rng; type Curve = Curve25519Projective; @@ -36,7 +36,7 @@ async fn main() { let network = QuicTwoPartyNet::new(PARTY0, local_addr, peer_addr); // MPC circuit - let mut rng = thread_rng(); + let mut rng = rng(); let my_val = Scalar::::random(&mut rng); let fabric = MpcFabric::new(network, beaver); diff --git a/integration/Cargo.toml b/integration/Cargo.toml index 7bfa173..118df31 100644 --- a/integration/Cargo.toml +++ b/integration/Cargo.toml @@ -5,21 +5,21 @@ edition = "2021" [dependencies] # === Cryptography === # -ark-bn254 = "0.4" -ark-ec = "0.4" +ark-bn254 = "0.5" +ark-ec = "0.5" ark-mpc = { path = "../online-phase", features = ["test_helpers"] } ark-mpc-offline = { path = "../offline-phase", features = ["parallel"] } # === Runtime + Harness === # -clap = { version = "3.2.8", features = ["derive"] } -colored = "2" -env_logger = "0.10" +clap = { version = "4.6.0", features = ["derive"] } +colored = "3" +env_logger = "0.11" futures = "0.3" inventory = "0.3" -tokio = "1.26" +tokio = "1.50" # === Misc === # -dns-lookup = "1.0" -itertools = "0.12" -rand = "0.8" +dns-lookup = "3.0" +itertools = "0.14" +rand = "0.10" tracing = "0.1" diff --git a/integration/src/authenticated_curve.rs b/integration/src/authenticated_curve.rs index ff97feb..673e363 100644 --- a/integration/src/authenticated_curve.rs +++ b/integration/src/authenticated_curve.rs @@ -8,7 +8,7 @@ use ark_mpc::{ random_point, PARTY0, PARTY1, }; use itertools::Itertools; -use rand::thread_rng; +use rand::rng; use crate::{ helpers::{ @@ -297,7 +297,7 @@ fn test_batch_negation(test_args: &mut IntegrationTestArgs) -> Result<(), String /// Test multiplication with a public scalar fn test_multiplication_public_scalar(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Sample a test point, party 1 will make theirs public - let mut rng = thread_rng(); + let mut rng = rng(); let point = random_point(); let scalar = Scalar::random(&mut rng); @@ -319,7 +319,7 @@ fn test_multiplication_public_scalar(test_args: &mut IntegrationTestArgs) -> Res /// Test multiplication with a secret shared scalar fn test_multiplication(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Sample a test point, party 1 will make theirs public - let mut rng = thread_rng(); + let mut rng = rng(); let point = random_point(); let scalar = Scalar::random(&mut rng); diff --git a/integration/src/authenticated_scalar.rs b/integration/src/authenticated_scalar.rs index e232f2a..9d65357 100644 --- a/integration/src/authenticated_scalar.rs +++ b/integration/src/authenticated_scalar.rs @@ -6,7 +6,7 @@ use ark_mpc::{ ResultValue, PARTY0, PARTY1, }; use itertools::Itertools; -use rand::thread_rng; +use rand::rng; use std::ops::Neg; use crate::{ @@ -26,7 +26,7 @@ use crate::{ /// it fn test_open_authenticated(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Each party samples a value - let mut rng = thread_rng(); + let mut rng = rng(); let my_val = Scalar::random(&mut rng); // Share the values with the counterparty and compute the expected result @@ -47,7 +47,7 @@ fn test_open_authenticated(test_args: &mut IntegrationTestArgs) -> Result<(), St /// Tests opening with a corrupted MAC #[allow(non_snake_case)] fn test_open_authenticated__bad_mac(test_args: &mut IntegrationTestArgs) -> Result<(), String> { - let mut rng = thread_rng(); + let mut rng = rng(); let my_val = Scalar::random(&mut rng); let mut party0_value = share_authenticated_scalar(my_val, PARTY0, test_args); @@ -62,7 +62,7 @@ fn test_open_authenticated__bad_mac(test_args: &mut IntegrationTestArgs) -> Resu /// Tests opening with a corrupted secret share #[allow(non_snake_case)] fn test_open_authenticated__bad_share(test_args: &mut IntegrationTestArgs) -> Result<(), String> { - let mut rng = thread_rng(); + let mut rng = rng(); let my_val = Scalar::random(&mut rng); let mut party0_value = share_authenticated_scalar(my_val, PARTY0, test_args); @@ -81,7 +81,7 @@ fn test_open_authenticated__bad_share(test_args: &mut IntegrationTestArgs) -> Re /// Test addition with a public value fn test_add_public_value(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Each party samples a value, party 1's value is made public - let mut rng = thread_rng(); + let mut rng = rng(); let val = Scalar::random(&mut rng); let my_value = test_args.fabric.allocate_scalar(ResultValue::Scalar(val)); @@ -105,7 +105,7 @@ fn test_add_public_value(test_args: &mut IntegrationTestArgs) -> Result<(), Stri /// Test addition between two secret shared values fn test_add(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Each party samples a value - let mut rng = thread_rng(); + let mut rng = rng(); let my_val = Scalar::random(&mut rng); // Share the values with the counterparty and compute the expected result @@ -130,7 +130,7 @@ fn test_batch_add(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Each party samples a batch of values let n = 10; let fabric = &test_args.fabric; - let mut rng = thread_rng(); + let mut rng = rng(); let my_vals = (0..n).map(|_| Scalar::random(&mut rng)).collect_vec(); let my_vals_allocated = fabric.allocate_scalars(my_vals.clone()); @@ -160,7 +160,7 @@ fn test_batch_add_public(test_args: &mut IntegrationTestArgs) -> Result<(), Stri // Each party samples a batch of values, party 1's values are made public let n = 10; let fabric = &test_args.fabric; - let mut rng = thread_rng(); + let mut rng = rng(); let my_vals = (0..n).map(|_| Scalar::random(&mut rng)).collect_vec(); let my_vals_allocated = fabric.allocate_scalars(my_vals.clone()); @@ -187,7 +187,7 @@ fn test_batch_add_public(test_args: &mut IntegrationTestArgs) -> Result<(), Stri /// Test subtraction between a shared point and a public scalar fn test_sub_public_scalar(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Each party samples a value, party 1's value is made public - let mut rng = thread_rng(); + let mut rng = rng(); let val = Scalar::random(&mut rng); let my_value = test_args.fabric.allocate_scalar(ResultValue::Scalar(val)); @@ -211,7 +211,7 @@ fn test_sub_public_scalar(test_args: &mut IntegrationTestArgs) -> Result<(), Str /// Test subtraction between two secret shared values fn test_sub(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Each party samples a value - let mut rng = thread_rng(); + let mut rng = rng(); let my_val = Scalar::random(&mut rng); // Share the values with the counterparty and compute the expected result @@ -236,7 +236,7 @@ fn test_batch_sub(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Each party samples a batch of values let n = 10; let fabric = &test_args.fabric; - let mut rng = thread_rng(); + let mut rng = rng(); let my_vals = (0..n).map(|_| Scalar::random(&mut rng)).collect_vec(); let my_vals_allocated = fabric.allocate_scalars(my_vals.clone()); @@ -266,7 +266,7 @@ fn test_batch_sub_public(test_args: &mut IntegrationTestArgs) -> Result<(), Stri // Each party samples a batch of values, party 1's values are made public let n = 10; let fabric = &test_args.fabric; - let mut rng = thread_rng(); + let mut rng = rng(); let my_vals = (0..n).map(|_| Scalar::random(&mut rng)).collect_vec(); let my_vals_allocated = fabric.allocate_scalars(my_vals.clone()); @@ -293,7 +293,7 @@ fn test_batch_sub_public(test_args: &mut IntegrationTestArgs) -> Result<(), Stri /// Test negation of a value fn test_neg(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Each party samples a value - let mut rng = thread_rng(); + let mut rng = rng(); let my_val = Scalar::random(&mut rng); // Share the values with the counterparty and compute the expected result @@ -316,7 +316,7 @@ fn test_batch_neg(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Party 0 chooses the values alone for this test let n = 10; let fabric = &test_args.fabric; - let mut rng = thread_rng(); + let mut rng = rng(); let my_vals = (0..n).map(|_| Scalar::random(&mut rng)).collect_vec(); let my_vals_allocated = fabric.allocate_scalars(my_vals.clone()); @@ -337,7 +337,7 @@ fn test_batch_neg(test_args: &mut IntegrationTestArgs) -> Result<(), String> { /// Test multiplication between a shared point and a public scalar fn test_mul_public_scalar(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Each party samples a value, party 1's value is made public - let mut rng = thread_rng(); + let mut rng = rng(); let val = Scalar::random(&mut rng); let my_value = test_args.fabric.allocate_scalar(ResultValue::Scalar(val)); @@ -361,7 +361,7 @@ fn test_mul_public_scalar(test_args: &mut IntegrationTestArgs) -> Result<(), Str /// Test multiplication between two secret shared values fn test_mul(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Each party samples a value - let mut rng = thread_rng(); + let mut rng = rng(); let my_val = Scalar::random(&mut rng); // Share the values with the counterparty and compute the expected result @@ -386,7 +386,7 @@ fn test_batch_mul(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Each party samples a batch of values let n = 10; let fabric = &test_args.fabric; - let mut rng = thread_rng(); + let mut rng = rng(); let my_vals = (0..n).map(|_| Scalar::random(&mut rng)).collect_vec(); let my_vals_allocated = fabric.allocate_scalars(my_vals.clone()); @@ -416,7 +416,7 @@ fn test_batch_mul_public(test_args: &mut IntegrationTestArgs) -> Result<(), Stri // Each party samples a batch of values, party 1's values are made public let n = 10; let fabric = &test_args.fabric; - let mut rng = thread_rng(); + let mut rng = rng(); let my_vals = (0..n).map(|_| Scalar::random(&mut rng)).collect_vec(); let my_vals_allocated = fabric.allocate_scalars(my_vals.clone()); @@ -443,7 +443,7 @@ fn test_batch_mul_public(test_args: &mut IntegrationTestArgs) -> Result<(), Stri /// Test the case in which we add and then multiply by a public value fn test_public_add_then_mul(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Each party samples a value, party 1's value is made public - let mut rng = thread_rng(); + let mut rng = rng(); let val = Scalar::random(&mut rng); let my_value = test_args.fabric.allocate_scalar(ResultValue::Scalar(val)); diff --git a/integration/src/circuits.rs b/integration/src/circuits.rs index 2b23d90..7063ca7 100644 --- a/integration/src/circuits.rs +++ b/integration/src/circuits.rs @@ -5,7 +5,7 @@ use ark_mpc::{ random_point, PARTY0, PARTY1, }; use itertools::Itertools; -use rand::thread_rng; +use rand::rng; use crate::{ helpers::{ @@ -23,7 +23,7 @@ fn test_inner_product(test_args: &mut IntegrationTestArgs) -> Result<(), String> // Sample local values let n = 100; let fabric = &test_args.fabric; - let mut rng = thread_rng(); + let mut rng = rng(); let my_vals = (0..n).map(|_| Scalar::random(&mut rng)).collect_vec(); @@ -56,7 +56,7 @@ fn test_msm(test_args: &mut IntegrationTestArgs) -> Result<(), String> { // Sample local values let n = 100; let fabric = &test_args.fabric; - let mut rng = thread_rng(); + let mut rng = rng(); let my_scalars = (0..n).map(|_| Scalar::random(&mut rng)).collect_vec(); let my_points = (0..n).map(|_| random_point()).collect_vec(); @@ -89,14 +89,14 @@ fn test_msm(test_args: &mut IntegrationTestArgs) -> Result<(), String> { /// Tests evaluation of a shared polynomial on a public input fn test_polynomial_eval(test_args: &mut IntegrationTestArgs) -> Result<(), String> { let fabric = &test_args.fabric; - let mut rng = thread_rng(); + let mut rng = rng(); let public_modifier = Scalar::random(&mut rng); let public_modifier = share_plaintext_value(fabric.allocate_scalar(public_modifier), PARTY0, fabric); // Party 0 and party 1 choose a public input let fabric = &test_args.fabric; - let my_x = fabric.allocate_scalar(Scalar::random(&mut thread_rng())); + let my_x = fabric.allocate_scalar(Scalar::random(&mut rng)); let x = fabric.exchange_value(my_x.clone()) + my_x; let x_res = await_result(x.clone()); diff --git a/integration/src/lowgear.rs b/integration/src/lowgear.rs index 6db9311..5a163fa 100644 --- a/integration/src/lowgear.rs +++ b/integration/src/lowgear.rs @@ -2,7 +2,7 @@ use ark_mpc::{algebra::Scalar, MpcFabric, PARTY0, PARTY1}; use ark_mpc_offline::{ - lowgear::{self, LowGear}, + lowgear::LowGear, structs::OfflineSizingParams, }; diff --git a/integration/src/main.rs b/integration/src/main.rs index 744aeea..afe1a26 100644 --- a/integration/src/main.rs +++ b/integration/src/main.rs @@ -93,7 +93,7 @@ struct Args { #[clap(short, long, value_parser)] test: Option, /// Whether running in docker or not, used for peer lookup - #[clap(long, takes_value = false, value_parser)] + #[clap(long)] docker: bool, } @@ -129,11 +129,14 @@ fn main() { let peer_addr: SocketAddr = { if args.docker { let other_host_alias = format!("party{}", if args.party == 1 { 0 } else { 1 }); - let hosts = lookup_host(other_host_alias.as_str()).unwrap(); + let hosts = lookup_host(other_host_alias.as_str()) + .unwrap() + .collect::>(); println!("Lookup successful for {}... found hosts: {:?}", other_host_alias, hosts); - format!("{}:{}", hosts[0], args.port2).parse().unwrap() + let peer_host = hosts.first().expect("DNS lookup returned no hosts"); + format!("{}:{}", peer_host, args.port2).parse().unwrap() } else { format!("{}:{}", "127.0.0.1", args.port2).parse().unwrap() } diff --git a/mp-spdz-rs/Cargo.toml b/mp-spdz-rs/Cargo.toml index c29aad8..4934afa 100644 --- a/mp-spdz-rs/Cargo.toml +++ b/mp-spdz-rs/Cargo.toml @@ -24,26 +24,26 @@ required-features = ["test-helpers"] [dependencies] # === Arithmetic + Crypto === # -ark-bn254 = "0.4" -ark-ec = { version = "0.4" } -ark-ff = { version = "0.4" } +ark-bn254 = "0.5" +ark-ec = { version = "0.5" } +ark-ff = { version = "0.5" } ark-mpc = { path = "../online-phase" } # === Bindings === # cxx = "1.0" # === Misc === # -rand = { version = "0.8.4", optional = true } -rayon = { version = "1.10", optional = true } +rand = { version = "0.10.0", optional = true } +rayon = { version = "1.11", optional = true } serde = { version = "1.0", features = ["derive"] } [build-dependencies] cxx-build = "1.0" -itertools = "0.12.0" +itertools = "0.14.0" pkg-config = "0.3" [dev-dependencies] -ark-bn254 = "0.4" -criterion = { version = "0.5", features = ["async", "async_tokio"] } -rand = "0.8.4" +ark-bn254 = "0.5" +criterion = { version = "0.8", features = ["async", "async_tokio"] } +rand = "0.10.0" serde_json = "1.0" diff --git a/mp-spdz-rs/build.rs b/mp-spdz-rs/build.rs index 517d146..8bcaa3a 100644 --- a/mp-spdz-rs/build.rs +++ b/mp-spdz-rs/build.rs @@ -1,4 +1,5 @@ use itertools::Itertools; +use std::path::Path; // Build entrypoint fn main() { @@ -48,13 +49,11 @@ fn main() { link_lib("gmpxx"); add_link_path("boost"); - link_lib("boost_system"); - link_lib("boost_filesystem"); - link_lib("boost_iostreams"); - if get_host_vendor() == "apple" { - println!("cargo:rustc-link-arg=-lboost_thread-mt"); - } else { - println!("cargo:rustc-link-arg=-lboost_thread"); + link_boost_lib("boost_system", false); + link_boost_lib("boost_filesystem", true); + link_boost_lib("boost_iostreams", true); + if !link_boost_lib("boost_thread-mt", false) { + link_boost_lib("boost_thread", true); } // Link in realtime extensions if running on linux @@ -90,6 +89,34 @@ fn link_lib(lib: &str) { println!("cargo:rustc-link-lib=static={lib}"); } +/// Link a boost library, preferring static when available and otherwise +/// falling back to dynamic. +/// +/// Returns true when the library was found and linked. +fn link_boost_lib(lib: &str, required: bool) -> bool { + let lib_path = find_lib_path("boost"); + + let static_path = format!("{lib_path}/lib{lib}.a"); + if Path::new(&static_path).exists() { + println!("cargo:rustc-link-lib=static={lib}"); + return true; + } + + let dylib_ext = if get_host_vendor() == "apple" { "dylib" } else { "so" }; + let dylib_path = format!("{lib_path}/lib{lib}.{dylib_ext}"); + if Path::new(&dylib_path).exists() { + println!("cargo:rustc-link-lib=dylib={lib}"); + return true; + } + + if required { + panic!("required boost library `{lib}` not found in `{lib_path}`"); + } + + println!("cargo:warning=optional boost library `{lib}` not found in `{lib_path}`, skipping"); + false +} + /// Find the include location for a package fn find_include_path(name: &str) -> String { let base_path = find_package(name); diff --git a/mp-spdz-rs/src/ffi.rs b/mp-spdz-rs/src/ffi.rs index 7c6b044..ab9e7db 100644 --- a/mp-spdz-rs/src/ffi.rs +++ b/mp-spdz-rs/src/ffi.rs @@ -186,7 +186,7 @@ unsafe impl Sync for PlaintextVector {} #[cfg(test)] mod test { use cxx::UniquePtr; - use rand::{thread_rng, Rng, RngCore}; + use rand::{rng, Rng, RngCore}; use super::*; @@ -231,7 +231,7 @@ mod test { #[test] fn test_bigint_to_from_bytes() { const N_BYTES: usize = 32; - let mut rng = thread_rng(); + let mut rng = rng(); let data = rng.gen::<[u8; N_BYTES]>(); // Convert the data to a bigint @@ -244,7 +244,7 @@ mod test { /// Tests addition of a plaintext to a ciphertext #[test] fn test_plaintext_addition() { - let mut rng = thread_rng(); + let mut rng = rng(); let (params, keypair) = setup_fhe(0, 254); // Add a plaintext to a ciphertext @@ -266,7 +266,7 @@ mod test { /// Tests multiplication of a plaintext to a ciphertext #[test] fn test_plaintext_multiplication() { - let mut rng = thread_rng(); + let mut rng = rng(); let (params, keypair) = setup_fhe(1, 254); // Multiply a plaintext to a ciphertext @@ -289,7 +289,7 @@ mod test { /// Tests addition of two encrypted values #[test] fn test_encrypted_addition() { - let mut rng = thread_rng(); + let mut rng = rng(); let (params, keypair) = setup_fhe(0, 254); // Add two ciphertexts, divide by two to avoid overflow @@ -311,7 +311,7 @@ mod test { /// Tests multiplication of two encrypted values #[test] fn test_encrypted_multiplication() { - let mut rng = thread_rng(); + let mut rng = rng(); let (params, keypair) = setup_fhe(1, 254); // Multiply two ciphertexts; capped bit length to avoid overflow diff --git a/mp-spdz-rs/src/fhe/ciphertext.rs b/mp-spdz-rs/src/fhe/ciphertext.rs index fa8b1c2..fe0407e 100644 --- a/mp-spdz-rs/src/fhe/ciphertext.rs +++ b/mp-spdz-rs/src/fhe/ciphertext.rs @@ -243,7 +243,7 @@ impl CiphertextPoK { #[cfg(test)] mod test { use ark_mpc::algebra::Scalar; - use rand::thread_rng; + use rand::rng; use crate::fhe::{keys::BGVKeypair, params::BGVParams, plaintext::Plaintext}; use crate::{compare_bytes, FromBytesWithParams, TestCurve, ToBytes}; @@ -282,7 +282,7 @@ mod test { /// Tests serialization and deserialization of a ciphertext #[test] fn test_serde() { - let mut rng = thread_rng(); + let mut rng = rng(); let (params, keypair) = setup_fhe(); let plaintext = plaintext_int(Scalar::random(&mut rng), ¶ms); let ciphertext = keypair.encrypt(&plaintext); @@ -296,7 +296,7 @@ mod test { /// Tests addition of a ciphertext with a plaintext #[test] fn test_ciphertext_plaintext_addition() { - let mut rng = thread_rng(); + let mut rng = rng(); let (params, mut keypair) = setup_fhe(); // Add a ciphertext with a plaintext @@ -319,7 +319,7 @@ mod test { /// Tests multiplication of a ciphertext with a plaintext #[test] fn test_ciphertext_plaintext_multiplication() { - let mut rng = thread_rng(); + let mut rng = rng(); let (params, mut keypair) = setup_fhe(); // Multiply a ciphertext with a plaintext @@ -342,7 +342,7 @@ mod test { /// Tests addition of two ciphertexts #[test] fn test_ciphertext_ciphertext_addition() { - let mut rng = thread_rng(); + let mut rng = rng(); let (params, mut keypair) = setup_fhe(); // Add two ciphertexts @@ -365,7 +365,7 @@ mod test { /// Tests multiplication of two ciphertexts #[test] fn test_ciphertext_ciphertext_multiplication() { - let mut rng = thread_rng(); + let mut rng = rng(); let (params, mut keypair) = setup_fhe(); // Multiply two ciphertexts diff --git a/mp-spdz-rs/src/fhe/keys.rs b/mp-spdz-rs/src/fhe/keys.rs index 3ab7d54..fcdcd32 100644 --- a/mp-spdz-rs/src/fhe/keys.rs +++ b/mp-spdz-rs/src/fhe/keys.rs @@ -250,7 +250,7 @@ impl FromBytesWithParams for BGVKeypair { #[cfg(test)] mod test { use ark_mpc::algebra::Scalar; - use rand::thread_rng; + use rand::rng; use crate::fhe::keys::{BGVKeypair, BGVPublicKey, BGVSecretKey}; use crate::fhe::params::BGVParams; @@ -299,7 +299,7 @@ mod test { /// Tests encrypting and proving a single plaintext #[test] fn test_encrypt_and_prove_single() { - let mut rng = thread_rng(); + let mut rng = rng(); let params = BGVParams::::new_no_mults(); let keypair = BGVKeypair::gen(¶ms); diff --git a/mp-spdz-rs/src/fhe/plaintext.rs b/mp-spdz-rs/src/fhe/plaintext.rs index d1b99fa..567b748 100644 --- a/mp-spdz-rs/src/fhe/plaintext.rs +++ b/mp-spdz-rs/src/fhe/plaintext.rs @@ -365,7 +365,7 @@ impl Mul for &PlaintextVector { #[cfg(test)] mod tests { - use rand::thread_rng; + use rand::rng; use super::*; use crate::{compare_bytes, TestCurve}; @@ -389,7 +389,7 @@ mod tests { #[test] fn test_add() { - let mut rng = thread_rng(); + let mut rng = rng(); let params = get_params(); let val1: Scalar = Scalar::random(&mut rng); let val2: Scalar = Scalar::random(&mut rng); @@ -406,7 +406,7 @@ mod tests { #[test] fn test_sub() { - let mut rng = thread_rng(); + let mut rng = rng(); let params = get_params(); let val1: Scalar = Scalar::random(&mut rng); let val2: Scalar = Scalar::random(&mut rng); @@ -423,7 +423,7 @@ mod tests { #[test] fn test_mul() { - let mut rng = thread_rng(); + let mut rng = rng(); let params = get_params(); let val1: Scalar = Scalar::random(&mut rng); let val2: Scalar = Scalar::random(&mut rng); diff --git a/mp-spdz-rs/src/lib.rs b/mp-spdz-rs/src/lib.rs index 0598f6c..f466342 100644 --- a/mp-spdz-rs/src/lib.rs +++ b/mp-spdz-rs/src/lib.rs @@ -49,7 +49,7 @@ pub mod benchmark_helpers { //! Helper methods for benchmarks use ark_ec::CurveGroup; use ark_mpc::algebra::Scalar; - use rand::thread_rng; + use rand::rng; use crate::fhe::{ params::BGVParams, @@ -58,7 +58,7 @@ pub mod benchmark_helpers { /// Get a random plaintext filled with random values pub fn random_plaintext(params: &BGVParams) -> Plaintext { - let mut rng = thread_rng(); + let mut rng = rng(); let mut pt = Plaintext::new(params); for i in 0..pt.num_slots() { diff --git a/offline-phase/Cargo.toml b/offline-phase/Cargo.toml index e739491..b54acc6 100644 --- a/offline-phase/Cargo.toml +++ b/offline-phase/Cargo.toml @@ -14,9 +14,9 @@ parallel = [ [dependencies] # === Crypto + Arithmetic === # -ark-ec = "0.4" -ark-ff = "0.4" -ark-std = "0.4" +ark-ec = "0.5" +ark-ff = "0.5" +ark-std = "0.5" ark-mpc = { path = "../online-phase" } mp-spdz-rs = { path = "../mp-spdz-rs" } @@ -25,12 +25,12 @@ mp-spdz-rs = { path = "../mp-spdz-rs" } futures = "0.3" # === Misc Dependencies === # -itertools = "0.10" -rand = "0.8" -rayon = { version = "1.10", optional = true } +itertools = "0.14" +rand = "0.10" +rayon = { version = "1.11", optional = true } sha3 = { version = "0.10" } [dev-dependencies] -ark-bn254 = "0.4" +ark-bn254 = "0.5" ark-mpc = { path = "../online-phase", features = ["test_helpers"] } tokio = { version = "1", features = ["full"] } diff --git a/offline-phase/src/lib.rs b/offline-phase/src/lib.rs index 049f548..891e959 100644 --- a/offline-phase/src/lib.rs +++ b/offline-phase/src/lib.rs @@ -34,7 +34,7 @@ pub(crate) mod test_helpers { params::BGVParams, plaintext::Plaintext, }; - use rand::thread_rng; + use rand::rng; use crate::{lowgear::LowGear, structs::ValueMacBatch}; @@ -84,7 +84,7 @@ pub(crate) mod test_helpers { pub fn generate_triples( n: usize, ) -> (Vec>, Vec>, Vec>) { - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..n).map(|_| Scalar::::random(&mut rng)).collect_vec(); let b = (0..n).map(|_| Scalar::::random(&mut rng)).collect_vec(); let c = a.iter().zip(b.iter()).map(|(a, b)| a * b).collect_vec(); @@ -108,7 +108,7 @@ pub(crate) mod test_helpers { pub fn generate_secret_shares( values: &[Scalar], ) -> (Vec>, Vec>) { - let mut rng = thread_rng(); + let mut rng = rng(); let mut shares1 = Vec::with_capacity(values.len()); let mut shares2 = Vec::with_capacity(values.len()); for value in values { @@ -184,7 +184,7 @@ pub(crate) mod test_helpers { net2: MockNetwork, ) -> (LowGear>, LowGear>) { - let mut rng = thread_rng(); + let mut rng = rng(); let mut lowgear1 = LowGear::new(net1); let mut lowgear2 = LowGear::new(net2); diff --git a/offline-phase/src/lowgear/input_masks.rs b/offline-phase/src/lowgear/input_masks.rs index 7cb240c..9f3eb81 100644 --- a/offline-phase/src/lowgear/input_masks.rs +++ b/offline-phase/src/lowgear/input_masks.rs @@ -4,7 +4,7 @@ use ark_ec::CurveGroup; use ark_mpc::{algebra::Scalar, network::MpcNetwork}; use itertools::Itertools; use mp_spdz_rs::fhe::plaintext::Plaintext; -use rand::rngs::OsRng; +use rand::rng; use crate::{error::LowGearError, structs::ValueMacBatch}; @@ -23,7 +23,7 @@ impl + Unpin + Send> LowGear { } // Each party generates their values, shares, and mac shares - let mut rng = OsRng; + let mut rng = rng(); let my_values = (0..n).map(|_| Scalar::::random(&mut rng)).collect_vec(); let my_share = (0..n).map(|_| Scalar::::random(&mut rng)).collect_vec(); diff --git a/offline-phase/src/lowgear/mod.rs b/offline-phase/src/lowgear/mod.rs index d28b17e..2c07a97 100644 --- a/offline-phase/src/lowgear/mod.rs +++ b/offline-phase/src/lowgear/mod.rs @@ -27,7 +27,7 @@ use mp_spdz_rs::{ }, FromBytesWithParams, ToBytes, }; -use rand::thread_rng; +use rand::rng; use crate::{ error::LowGearError, @@ -68,7 +68,7 @@ impl + Unpin> LowGear { /// Create a new LowGear instance #[allow(clippy::new_without_default)] pub fn new(network: N) -> Self { - let mut rng = thread_rng(); + let mut rng = rng(); let params = BGVParams::new_no_mults(); let local_keypair = BGVKeypair::gen(¶ms); let mac_share = Scalar::random(&mut rng); diff --git a/offline-phase/src/lowgear/setup.rs b/offline-phase/src/lowgear/setup.rs index 7b97e0b..ece7d2b 100644 --- a/offline-phase/src/lowgear/setup.rs +++ b/offline-phase/src/lowgear/setup.rs @@ -36,14 +36,14 @@ impl + Unpin> LowGear { mod test { use ark_mpc::{algebra::Scalar, network::MpcNetwork, PARTY0}; use mp_spdz_rs::fhe::ciphertext::Ciphertext; - use rand::thread_rng; + use rand::rng; use crate::test_helpers::{encrypt_val, mock_lowgear, plaintext_val, TestCurve}; /// Tests the setup phase, i.e. that encrypted values are correctly shared #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_key_exchange() { - let mut rng = thread_rng(); + let mut rng = rng(); let val1 = Scalar::::random(&mut rng); let val2 = Scalar::::random(&mut rng); diff --git a/offline-phase/src/lowgear/shared_random.rs b/offline-phase/src/lowgear/shared_random.rs index 62d38fa..135e89f 100644 --- a/offline-phase/src/lowgear/shared_random.rs +++ b/offline-phase/src/lowgear/shared_random.rs @@ -4,7 +4,7 @@ use ark_ec::CurveGroup; use ark_mpc::{algebra::Scalar, network::MpcNetwork}; use itertools::Itertools; use mp_spdz_rs::fhe::plaintext::PlaintextVector; -use rand::rngs::OsRng; +use rand::rng; use crate::{error::LowGearError, structs::ValueMacBatch}; @@ -40,7 +40,7 @@ impl + Unpin + Send> LowGear { n: usize, ) -> Result>, LowGearError> { // Generate local random values - let mut rng = OsRng; + let mut rng = rng(); let my_shares = (0..n).map(|_| Scalar::random(&mut rng)).collect_vec(); let their_shares = self.commit_reveal(&my_shares).await?; @@ -59,7 +59,7 @@ impl + Unpin + Send> LowGear { ) -> Result, LowGearError> { // Each party generates shares locally with the represented value implicitly // defined as the sum of the shares - let mut rng = OsRng; + let mut rng = rng(); let my_shares = (0..n).map(|_| Scalar::::random(&mut rng)).collect_vec(); let pt_vec = PlaintextVector::from_scalars(&my_shares, &self.params); diff --git a/offline-phase/src/lowgear/triplets.rs b/offline-phase/src/lowgear/triplets.rs index 6ee87a0..2f6f903 100644 --- a/offline-phase/src/lowgear/triplets.rs +++ b/offline-phase/src/lowgear/triplets.rs @@ -242,7 +242,7 @@ mod test { params::BGVParams, plaintext::{Plaintext, PlaintextVector}, }; - use rand::{rngs::OsRng, thread_rng}; + use rand::{rngs::OsRng, rng}; use crate::{ structs::ValueMacBatch, @@ -262,7 +262,7 @@ mod test { fn create_shares( values: &[Scalar], ) -> (Vec>, Vec>) { - let mut rng = thread_rng(); + let mut rng = rng(); let mut shares1 = Vec::new(); let mut shares2 = Vec::new(); diff --git a/offline-phase/src/structs.rs b/offline-phase/src/structs.rs index 45ba83b..9748e22 100644 --- a/offline-phase/src/structs.rs +++ b/offline-phase/src/structs.rs @@ -451,7 +451,7 @@ mod test { use ark_mpc::{ algebra::Scalar, test_helpers::execute_mock_mpc_with_beaver_source, PARTY0, PARTY1, }; - use rand::thread_rng; + use rand::rng; use crate::test_helpers::mock_lowgear_with_triples; @@ -471,7 +471,7 @@ mod test { .await; // Run a mock mpc using the lowgear offline phase - let mut rng = thread_rng(); + let mut rng = rng(); let a = Scalar::random(&mut rng); let b = Scalar::random(&mut rng); let expected = a * b; diff --git a/online-phase/Cargo.toml b/online-phase/Cargo.toml index 946393a..5346b42 100644 --- a/online-phase/Cargo.toml +++ b/online-phase/Cargo.toml @@ -82,48 +82,48 @@ required-features = ["benchmarks", "stats", "test_helpers"] async-trait = "0.1" crossbeam = "0.8" futures = "0.3" -kanal = "0.1.0-pre8" -tokio = { version = "1.12", features = ["macros", "rt-multi-thread"] } +kanal = "0.1.1" +tokio = { version = "1.50", features = ["macros", "rt-multi-thread"] } # == Arithemtic + Crypto == # -ark-bn254 = { version = "0.4", optional = true } -ark-ec = { version = "0.4", features = ["parallel"] } -ark-ff = { version = "0.4", features = ["parallel"] } -ark-poly = { version = "0.4", features = ["std", "parallel"] } -ark-serialize = "0.4" -ark-std = { version = "0.4", features = ["parallel"] } -digest = "0.10" +ark-bn254 = { version = "0.5", optional = true } +ark-ec = { version = "0.5", features = ["parallel"] } +ark-ff = { version = "0.5", features = ["parallel"] } +ark-poly = { version = "0.5", features = ["std", "parallel"] } +ark-serialize = "0.5" +ark-std = { version = "0.5", features = ["parallel"] } +digest = "0.11" num-bigint = "0.4" num-traits = "0.2" -rand = "0.8" +rand = "0.10" sha3 = { version = "0.10" } -# == Networking + Messaging == # -rcgen = "0.9" -rustls = { version = "0.20", features = ["dangerous_configuration"] } +# == Networking + Messaging == # +rcgen = "0.14" +rustls = { version = "0.23", features = [] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -quinn = { version = "0.9", features = ["tls-rustls", "native-certs"] } +quinn = { version = "0.11", features = [] } # == Multithreaded Executor == # bitvec = { version = "1.0", optional = true } -dashmap = { version = "5.5", optional = true } +dashmap = { version = "6.1", optional = true } identity-hash = { version = "0.1", optional = true } -rayon = { version = "1.8", optional = true } +rayon = { version = "1.11", optional = true } # == Misc == # -bytes = "1.2" -itertools = "0.10" -rustc-hash = "1.1" +bytes = "1.11" +itertools = "0.14" +rustc-hash = "2.1" tracing = { version = "0.1", features = ["log"] } -zeroize = "1.3" +zeroize = "1.8" [dev-dependencies] -clap = { version = "3.2.8", features = ["derive"] } -colored = "2" -criterion = { version = "0.5", features = ["async", "async_tokio"] } +clap = { version = "4.6.0", features = ["derive"] } +colored = "3" +criterion = { version = "0.8", features = ["async", "async_tokio"] } cpuprofiler = "0.0.4" -dns-lookup = "1.0" -env_logger = "0.10" +dns-lookup = "3.0" +env_logger = "0.11" gperftools = { version = "0.2", features = ["heap"] } inventory = "0.3" diff --git a/online-phase/benches/batch_ops.rs b/online-phase/benches/batch_ops.rs index c848f01..5c68336 100644 --- a/online-phase/benches/batch_ops.rs +++ b/online-phase/benches/batch_ops.rs @@ -10,7 +10,7 @@ use ark_mpc::{ use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; use futures::future; use itertools::Itertools; -use rand::thread_rng; +use rand::rng; use tokio::runtime::Builder as RuntimeBuilder; /// A scalar with curve generics defined @@ -54,7 +54,7 @@ fn bench_batch_mul(c: &mut Criterion) { async_bencher.iter_custom(|n_iters| async move { let mut total_time = Duration::default(); for _ in 0..n_iters { - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..batch_size).map(|_| Scalar::random(&mut rng)).collect_vec(); let b = (0..batch_size).map(|_| Scalar::random(&mut rng)).collect_vec(); diff --git a/online-phase/benches/gate_throughput.rs b/online-phase/benches/gate_throughput.rs index 115faad..b5364e6 100644 --- a/online-phase/benches/gate_throughput.rs +++ b/online-phase/benches/gate_throughput.rs @@ -9,7 +9,7 @@ use criterion::{ criterion_group, criterion_main, profiler::Profiler as CriterionProfiler, BenchmarkId, Criterion, Throughput, }; -use rand::{rngs::OsRng, thread_rng}; +use rand::{rngs::OsRng, rng}; use tokio::runtime::Builder as RuntimeBuilder; // ----------- @@ -54,7 +54,7 @@ pub fn mock_fabric(size_hint: usize) -> MpcFabric { /// Measures the raw throughput of scalar addition pub fn scalar_addition(c: &mut Criterion) { - let mut rng = thread_rng(); + let mut rng = rng(); let mut res = Scalar::::random(&mut rng); let mut group = c.benchmark_group("raw_scalar_addition"); diff --git a/online-phase/benches/gate_throughput_traced.rs b/online-phase/benches/gate_throughput_traced.rs index a8fe91b..cca3d95 100644 --- a/online-phase/benches/gate_throughput_traced.rs +++ b/online-phase/benches/gate_throughput_traced.rs @@ -10,7 +10,7 @@ use ark_mpc::{ use clap::Parser; use cpuprofiler::PROFILER; use gperftools::HEAP_PROFILER; -use rand::thread_rng; +use rand::rng; // ----------- // | Helpers | @@ -82,7 +82,7 @@ async fn main() { let allocation_time = start_time.elapsed(); start_cpu_profiler(args.cpu_profiled); - let mut rng = thread_rng(); + let mut rng = rng(); let base = Scalar::random(&mut rng); let base_res = fabric.allocate_scalar(base); diff --git a/online-phase/benches/growable_buffer.rs b/online-phase/benches/growable_buffer.rs index b85f8e5..f7d23c1 100644 --- a/online-phase/benches/growable_buffer.rs +++ b/online-phase/benches/growable_buffer.rs @@ -2,7 +2,7 @@ use ark_mpc::{algebra::Scalar, test_helpers::TestCurve, GrowableBuffer}; use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; -use rand::{seq::SliceRandom, thread_rng}; +use rand::{seq::SliceRandom, rng}; use std::hint::black_box; // -------------- @@ -56,7 +56,7 @@ pub fn buffer_write__random(c: &mut Criterion) { let mut buffer: GrowableBuffer> = GrowableBuffer::new(buffer_size); group.throughput(Throughput::Elements(buffer_size as u64)); - let mut rng = thread_rng(); + let mut rng = rng(); let mut indices: Vec = (0..buffer_size).collect(); indices.shuffle(&mut rng); group.bench_function(BenchmarkId::from_parameter(buffer_size), |b| { diff --git a/online-phase/benches/native_msm.rs b/online-phase/benches/native_msm.rs index 1edc5a0..aed425f 100644 --- a/online-phase/benches/native_msm.rs +++ b/online-phase/benches/native_msm.rs @@ -8,14 +8,14 @@ use ark_mpc::{ }; use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; use itertools::Itertools; -use rand::thread_rng; +use rand::rng; /// The maximum power of two to scale the MSM benchmark to const MAX_POWER_OF_TWO: usize = 16; // 2^16 = 65536 /// Measures the raw throughput of a native MSM pub fn bench_native_msm(c: &mut Criterion) { - let mut rng = thread_rng(); + let mut rng = rng(); let mut group = c.benchmark_group("native_msm"); for n_elems in (0..MAX_POWER_OF_TWO).map(|i| 1 << i) { diff --git a/online-phase/benches/scalar_serialization.rs b/online-phase/benches/scalar_serialization.rs index 2fb9304..543f0bb 100644 --- a/online-phase/benches/scalar_serialization.rs +++ b/online-phase/benches/scalar_serialization.rs @@ -2,11 +2,11 @@ use std::time::{Duration, Instant}; use ark_mpc::{algebra::Scalar, test_helpers::TestCurve}; use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput}; -use rand::thread_rng; +use rand::rng; /// Benchmark the serialization of scalars fn bench_scalar_serialization(c: &mut Criterion) { - let mut rng = thread_rng(); + let mut rng = rng(); let mut group = c.benchmark_group("scalar_serialization"); group.throughput(Throughput::Elements(1)); @@ -29,7 +29,7 @@ fn bench_scalar_serialization(c: &mut Criterion) { /// Benchmark the deserialization of scalars fn bench_scalar_deserialization(c: &mut Criterion) { - let mut rng = thread_rng(); + let mut rng = rng(); let mut group = c.benchmark_group("scalar_serialization"); group.throughput(Throughput::Elements(1)); diff --git a/online-phase/benches/test_stats.rs b/online-phase/benches/test_stats.rs index fc9ac15..4b6f38c 100644 --- a/online-phase/benches/test_stats.rs +++ b/online-phase/benches/test_stats.rs @@ -1,13 +1,13 @@ //! A simple benchmark for testing that stats collection is properly working use ark_mpc::{algebra::Scalar, test_helpers::execute_mock_mpc, PARTY0, PARTY1}; -use rand::{distributions::uniform::SampleRange, thread_rng}; +use rand::{distributions::uniform::SampleRange, rng}; #[tokio::main] async fn main() { // Run the following circuit with the `stats` feature enabled and // the stats will be dumped at the end of execution - let mut rng = thread_rng(); + let mut rng = rng(); let depth = (0usize..=1000).sample_single(&mut rng); let value1 = Scalar::random(&mut rng); diff --git a/online-phase/src/algebra/curve/authenticated_curve.rs b/online-phase/src/algebra/curve/authenticated_curve.rs index fc483e7..ab2e01e 100644 --- a/online-phase/src/algebra/curve/authenticated_curve.rs +++ b/online-phase/src/algebra/curve/authenticated_curve.rs @@ -865,7 +865,7 @@ pub mod test_helpers { #[cfg(test)] mod test { use itertools::Itertools; - use rand::thread_rng; + use rand::rng; use crate::{ algebra::{AuthenticatedPointResult, CurvePoint, Scalar}, @@ -1136,7 +1136,7 @@ mod test { /// Tests multiplication with a constant scalar #[tokio::test] async fn test_mul_constant() { - let mut rng = thread_rng(); + let mut rng = rng(); let p = random_point(); let s = Scalar::random(&mut rng); let expected = p * s; @@ -1155,7 +1155,7 @@ mod test { /// Tests multiplication with a public (allocated but not shared) scalar #[tokio::test] async fn test_mul_public_scalar() { - let mut rng = thread_rng(); + let mut rng = rng(); let p = random_point(); let s = Scalar::random(&mut rng); let expected = p * s; @@ -1175,7 +1175,7 @@ mod test { /// Tests multiplication with a shared scalar #[tokio::test] async fn test_mul_shared_scalar() { - let mut rng = thread_rng(); + let mut rng = rng(); let p = random_point(); let s = Scalar::random(&mut rng); let expected = p * s; @@ -1197,7 +1197,7 @@ mod test { #[tokio::test] async fn test_batch_mul_public() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| random_point()).collect_vec(); let b = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let expected_res = a.iter().zip(b.iter()).map(|(a, b)| a * b).collect_vec(); @@ -1222,7 +1222,7 @@ mod test { #[tokio::test] async fn test_batch_mul() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| random_point()).collect_vec(); let b = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let expected_res = a.iter().zip(b.iter()).map(|(a, b)| a * b).collect_vec(); @@ -1247,7 +1247,7 @@ mod test { #[tokio::test] async fn test_batch_mul_generator() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let expected_res = a.iter().map(|x| x * CurvePoint::::generator()).collect_vec(); @@ -1273,7 +1273,7 @@ mod test { #[tokio::test] async fn test_msm() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let b = (0..N).map(|_| random_point()).collect_vec(); let expected_res = a.iter().zip(b.iter()).map(|(a, b)| a * b).sum(); diff --git a/online-phase/src/algebra/curve/curve.rs b/online-phase/src/algebra/curve/curve.rs index 677f04f..ad4ef18 100644 --- a/online-phase/src/algebra/curve/curve.rs +++ b/online-phase/src/algebra/curve/curve.rs @@ -155,9 +155,9 @@ where let f2 = Self::hash_to_field(&buf[n_bytes / 2..]); // Map to curve - let mapper = SWUMap::::new()?; - let p1 = mapper.map_to_curve(f1)?; - let p2 = mapper.map_to_curve(f2)?; + SWUMap::::check_parameters()?; + let p1 = SWUMap::::map_to_curve(f1)?; + let p2 = SWUMap::::map_to_curve(f2)?; // Clear the cofactor let p1_clear = p1.clear_cofactor(); @@ -751,7 +751,7 @@ impl CurvePointResult { #[cfg(test)] mod test { - use rand::thread_rng; + use rand::rng; use crate::{test_helpers::mock_fabric, test_helpers::TestCurve}; @@ -763,7 +763,7 @@ mod test { /// Generate a random point, by multiplying the basepoint with a random /// scalar pub fn random_point() -> TestCurvePoint { - let mut rng = thread_rng(); + let mut rng = rng(); let scalar = Scalar::random(&mut rng); let point = TestCurvePoint::generator() * scalar; point * scalar @@ -792,7 +792,7 @@ mod test { async fn test_scalar_mul() { let fabric = mock_fabric(); - let mut rng = thread_rng(); + let mut rng = rng(); let s1 = Scalar::::random(&mut rng); let p1 = random_point(); diff --git a/online-phase/src/algebra/poly/authenticated_poly.rs b/online-phase/src/algebra/poly/authenticated_poly.rs index cdb43cf..09fffaa 100644 --- a/online-phase/src/algebra/poly/authenticated_poly.rs +++ b/online-phase/src/algebra/poly/authenticated_poly.rs @@ -577,7 +577,7 @@ impl_borrow_variants!(AuthenticatedDensePoly, Div, div, /, AuthenticatedDense #[cfg(test)] mod test { use ark_poly::Polynomial; - use rand::thread_rng; + use rand::rng; use crate::{ algebra::{ @@ -594,7 +594,7 @@ mod test { /// Test evaluating a polynomial at a given point #[tokio::test] async fn test_eval() { - let mut rng = thread_rng(); + let mut rng = rng(); let poly = random_poly(DEGREE_BOUND); let point = Scalar::random(&mut rng); @@ -823,7 +823,7 @@ mod test { /// Tests multiplying by a public constant scalar #[tokio::test] async fn test_scalar_mul_constant() { - let mut rng = thread_rng(); + let mut rng = rng(); let poly = random_poly(DEGREE_BOUND); let scaling_factor = Scalar::random(&mut rng); @@ -845,7 +845,7 @@ mod test { /// Tests multiplying by a public result #[tokio::test] async fn test_scalar_mul_public() { - let mut rng = thread_rng(); + let mut rng = rng(); let poly = random_poly(DEGREE_BOUND); let scaling_factor = Scalar::random(&mut rng); @@ -869,7 +869,7 @@ mod test { /// Tests multiplying by a shared scalar #[tokio::test] async fn test_scalar_mul() { - let mut rng = thread_rng(); + let mut rng = rng(); let poly = random_poly(DEGREE_BOUND); let scaling_factor = Scalar::random(&mut rng); diff --git a/online-phase/src/algebra/poly/mod.rs b/online-phase/src/algebra/poly/mod.rs index 607567c..abef4f3 100644 --- a/online-phase/src/algebra/poly/mod.rs +++ b/online-phase/src/algebra/poly/mod.rs @@ -80,7 +80,7 @@ pub mod poly_test_helpers { use ark_poly::{univariate::DensePolynomial, DenseUVPolynomial, Polynomial}; use ark_std::UniformRand; use itertools::Itertools; - use rand::{thread_rng, Rng}; + use rand::{rng, Rng}; use crate::{algebra::Scalar, network::PartyId, test_helpers::TestCurve, MpcFabric}; @@ -91,7 +91,7 @@ pub mod poly_test_helpers { /// Generate a random polynomial given a degree bound pub fn random_poly(degree_bound: usize) -> DensePolynomial { - let mut rng = thread_rng(); + let mut rng = rng(); // Sample a random degree below the bound let degree = rng.gen_range(1..degree_bound); diff --git a/online-phase/src/algebra/poly/poly.rs b/online-phase/src/algebra/poly/poly.rs index adb498b..a2d008d 100644 --- a/online-phase/src/algebra/poly/poly.rs +++ b/online-phase/src/algebra/poly/poly.rs @@ -380,7 +380,7 @@ pub(crate) mod test { use ark_ff::{One, Zero}; use ark_poly::Polynomial; use itertools::Itertools; - use rand::{thread_rng, Rng}; + use rand::{rng, Rng}; use crate::{ algebra::{ @@ -573,7 +573,7 @@ pub(crate) mod test { async fn test_scalar_mul_constant() { let poly = random_poly(DEGREE_BOUND); - let mut rng = thread_rng(); + let mut rng = rng(); let scaling_factor = Scalar::random(&mut rng); let expected_res = &poly * scaling_factor.inner(); @@ -596,7 +596,7 @@ pub(crate) mod test { async fn test_scalar_mul() { let poly = random_poly(DEGREE_BOUND); - let mut rng = thread_rng(); + let mut rng = rng(); let scaling_factor = Scalar::random(&mut rng); let expected_res = &poly * scaling_factor.inner(); @@ -620,7 +620,7 @@ pub(crate) mod test { async fn test_scalar_mul_shared() { let poly = random_poly(DEGREE_BOUND); - let mut rng = thread_rng(); + let mut rng = rng(); let scaling_factor = Scalar::random(&mut rng); let expected_res = &poly * scaling_factor.inner(); @@ -645,7 +645,7 @@ pub(crate) mod test { async fn test_eval() { let poly = random_poly(DEGREE_BOUND); - let mut rng = thread_rng(); + let mut rng = rng(); let eval_point = Scalar::random(&mut rng); let expected_eval = poly.evaluate(&eval_point.inner()); @@ -669,7 +669,7 @@ pub(crate) mod test { async fn test_mod_inv() { let poly = random_poly(DEGREE_BOUND); - let mut rng = thread_rng(); + let mut rng = rng(); let t = rng.gen_range(1..(DEGREE_BOUND * 2)); let (res, _) = execute_mock_mpc(|fabric| { diff --git a/online-phase/src/algebra/scalar/authenticated_scalar.rs b/online-phase/src/algebra/scalar/authenticated_scalar.rs index f290a6f..9ec660a 100644 --- a/online-phase/src/algebra/scalar/authenticated_scalar.rs +++ b/online-phase/src/algebra/scalar/authenticated_scalar.rs @@ -1115,7 +1115,7 @@ mod tests { use ark_poly::{EvaluationDomain, Radix2EvaluationDomain}; use futures::future; use itertools::Itertools; - use rand::{thread_rng, Rng, RngCore}; + use rand::{rng, Rng, RngCore}; use crate::{ algebra::{poly_test_helpers::TestPolyField, scalar::Scalar, AuthenticatedScalarResult}, @@ -1130,7 +1130,7 @@ mod tests { /// Tests addition with a constant value #[tokio::test] async fn test_add_constant() { - let mut rng = thread_rng(); + let mut rng = rng(); let a = Scalar::random(&mut rng); let b = Scalar::random(&mut rng); @@ -1149,7 +1149,7 @@ mod tests { #[tokio::test] async fn test_batch_add_constant() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let b = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); @@ -1174,7 +1174,7 @@ mod tests { /// Tests addition with a public value #[tokio::test] async fn test_add_public() { - let mut rng = thread_rng(); + let mut rng = rng(); let a = Scalar::random(&mut rng); let b = Scalar::random(&mut rng); @@ -1194,7 +1194,7 @@ mod tests { #[tokio::test] async fn test_batch_add_public() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let b = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); @@ -1220,7 +1220,7 @@ mod tests { /// Tests adding two shared values #[tokio::test] async fn test_add() { - let mut rng = thread_rng(); + let mut rng = rng(); let a = Scalar::random(&mut rng); let b = Scalar::random(&mut rng); @@ -1239,7 +1239,7 @@ mod tests { #[tokio::test] async fn test_batch_add() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let b = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); @@ -1266,7 +1266,7 @@ mod tests { #[tokio::test] async fn test_sum() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let values = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let expected_res = values.iter().cloned().sum(); @@ -1292,7 +1292,7 @@ mod tests { /// Test subtraction with a constant value #[tokio::test] async fn test_sub_constant() { - let mut rng = thread_rng(); + let mut rng = rng(); let a = Scalar::random(&mut rng); let b = Scalar::random(&mut rng); @@ -1310,7 +1310,7 @@ mod tests { /// Tests subtraction with a public value #[tokio::test] async fn test_sub_public() { - let mut rng = thread_rng(); + let mut rng = rng(); let a = Scalar::random(&mut rng); let b = Scalar::random(&mut rng); @@ -1330,7 +1330,7 @@ mod tests { #[tokio::test] async fn test_batch_sub_public() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let b = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); @@ -1355,7 +1355,7 @@ mod tests { /// Test subtraction across non-commutative types #[tokio::test] async fn test_sub() { - let mut rng = thread_rng(); + let mut rng = rng(); let value1 = Scalar::random(&mut rng); let value2 = Scalar::random(&mut rng); @@ -1386,7 +1386,7 @@ mod tests { #[tokio::test] async fn test_batch_sub() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let b = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); @@ -1415,7 +1415,7 @@ mod tests { /// Tests negation of a shared value #[tokio::test] async fn test_negation() { - let mut rng = thread_rng(); + let mut rng = rng(); let value = Scalar::random(&mut rng); let (res, _) = execute_mock_mpc(|fabric| async move { @@ -1433,7 +1433,7 @@ mod tests { #[tokio::test] async fn test_batch_negation() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let values = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let expected_res = values.iter().map(|v| -v).collect_vec(); @@ -1459,7 +1459,7 @@ mod tests { /// Tests multiplication between a constant and a shared value #[tokio::test] async fn test_mul_constant() { - let mut rng = thread_rng(); + let mut rng = rng(); let a = Scalar::random(&mut rng); let b = Scalar::random(&mut rng); @@ -1477,7 +1477,7 @@ mod tests { /// Tests multiplication between a public and a shared value #[tokio::test] async fn test_mul_public() { - let mut rng = thread_rng(); + let mut rng = rng(); let a = Scalar::random(&mut rng); let b = Scalar::random(&mut rng); @@ -1496,7 +1496,7 @@ mod tests { /// Tests multiplication between two shared values #[tokio::test] async fn test_mul() { - let mut rng = thread_rng(); + let mut rng = rng(); let a = Scalar::random(&mut rng); let b = Scalar::random(&mut rng); @@ -1515,7 +1515,7 @@ mod tests { #[tokio::test] async fn test_batch_mul_constant() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::::random(&mut rng)).collect_vec(); let b = (0..N).map(|_| Scalar::::random(&mut rng)).collect_vec(); @@ -1544,7 +1544,7 @@ mod tests { #[tokio::test] async fn test_batch_mul_public() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::::random(&mut rng)).collect_vec(); let b = (0..N).map(|_| Scalar::::random(&mut rng)).collect_vec(); @@ -1571,7 +1571,7 @@ mod tests { #[tokio::test] async fn test_batch_mul() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::::random(&mut rng)).collect_vec(); let b = (0..N).map(|_| Scalar::::random(&mut rng)).collect_vec(); @@ -1600,7 +1600,7 @@ mod tests { /// Tests division between a shared and public scalar #[tokio::test] async fn test_public_division() { - let mut rng = thread_rng(); + let mut rng = rng(); let value1 = Scalar::random(&mut rng); let value2 = Scalar::random(&mut rng); @@ -1620,7 +1620,7 @@ mod tests { /// Tests division between two authenticated values #[tokio::test] async fn test_division() { - let mut rng = thread_rng(); + let mut rng = rng(); let value1 = Scalar::random(&mut rng); let value2 = Scalar::random(&mut rng); @@ -1641,7 +1641,7 @@ mod tests { #[tokio::test] async fn test_batch_div() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let a_values = (0..N).map(|_| Scalar::::random(&mut rng)).collect_vec(); let b_values = (0..N).map(|_| Scalar::::random(&mut rng)).collect_vec(); @@ -1692,7 +1692,7 @@ mod tests { async fn test_batch_inverse() { const N: usize = 10; - let mut rng = thread_rng(); + let mut rng = rng(); let values = (0..N).map(|_| Scalar::::random(&mut rng)).collect_vec(); let expected_res = values.iter().map(|v| v.inverse()).collect_vec(); @@ -1721,7 +1721,7 @@ mod tests { /// Tests exponentiation #[tokio::test] async fn test_pow() { - let mut rng = thread_rng(); + let mut rng = rng(); let exp = rng.next_u64(); let value = Scalar::::random(&mut rng); @@ -1740,7 +1740,7 @@ mod tests { #[tokio::test] async fn test_fft() { - let mut rng = thread_rng(); + let mut rng = rng(); let n: usize = rng.gen_range(0..100); let domain_size = rng.gen_range(n..10 * n); @@ -1782,7 +1782,7 @@ mod tests { #[tokio::test] async fn test_ifft() { - let mut rng = thread_rng(); + let mut rng = rng(); let n: usize = rng.gen_range(0..100); let domain_size = rng.gen_range(n..10 * n); diff --git a/online-phase/src/algebra/scalar/scalar.rs b/online-phase/src/algebra/scalar/scalar.rs index bca47ad..6f2e896 100644 --- a/online-phase/src/algebra/scalar/scalar.rs +++ b/online-phase/src/algebra/scalar/scalar.rs @@ -18,7 +18,7 @@ use ark_std::UniformRand; use itertools::Itertools; use num_bigint::BigUint; use num_traits::Num; -use rand::{CryptoRng, RngCore}; +use rand::{CryptoRng as RandCryptoRng, Rng as RandRng}; use serde::{Deserialize, Serialize}; use crate::algebra::{ToBytes, macros::*}; @@ -36,6 +36,31 @@ pub const fn n_bytes_field() -> usize { (n_bits + 7) / 8 } +/// Adapter from `rand 0.10` RNG traits to the `rand 0.8` traits expected by +/// arkworks. +struct ArkRandAdapter<'a, R>(&'a mut R); + +impl ark_std::rand::RngCore for ArkRandAdapter<'_, R> { + fn next_u32(&mut self) -> u32 { + self.0.next_u32() + } + + fn next_u64(&mut self) -> u64 { + self.0.next_u64() + } + + fn fill_bytes(&mut self, dest: &mut [u8]) { + self.0.fill_bytes(dest); + } + + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), ark_std::rand::Error> { + self.0.fill_bytes(dest); + Ok(()) + } +} + +impl ark_std::rand::CryptoRng for ArkRandAdapter<'_, R> {} + // --------------------- // | Scalar Definition | // --------------------- @@ -75,8 +100,9 @@ impl Scalar { } /// Sample a random field element - pub fn random(rng: &mut R) -> Self { - Self(C::ScalarField::rand(rng)) + pub fn random(rng: &mut R) -> Self { + let mut adapter = ArkRandAdapter(rng); + Self(C::ScalarField::rand(&mut adapter)) } /// Compute the multiplicative inverse of the scalar in its field @@ -381,12 +407,12 @@ mod test { use ark_poly::{EvaluationDomain, Radix2EvaluationDomain}; use futures::future; use itertools::Itertools; - use rand::{Rng, RngCore, thread_rng}; + use rand::{Rng, RngCore, rng}; /// Tests serialization and deserialization of scalars #[test] fn test_scalar_serialization() { - let mut rng = thread_rng(); + let mut rng = rng(); let scalar = Scalar::::random(&mut rng); let bytes = serde_json::to_vec(&scalar).unwrap(); @@ -397,7 +423,7 @@ mod test { /// Tests addition of raw scalars in a circuit #[tokio::test] async fn test_scalar_add() { - let mut rng = thread_rng(); + let mut rng = rng(); let a = Scalar::random(&mut rng); let b = Scalar::random(&mut rng); @@ -418,7 +444,7 @@ mod test { #[tokio::test] async fn test_batch_add_constant() { const N: usize = 1000; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let b = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); @@ -442,7 +468,7 @@ mod test { /// Tests subtraction of raw scalars in the circuit #[tokio::test] async fn test_scalar_sub() { - let mut rng = thread_rng(); + let mut rng = rng(); let a = Scalar::random(&mut rng); let b = Scalar::random(&mut rng); @@ -464,7 +490,7 @@ mod test { #[tokio::test] async fn test_batch_sub_constant() { const N: usize = 1000; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let b = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); @@ -488,7 +514,7 @@ mod test { /// Tests negation of raw scalars in a circuit #[tokio::test] async fn test_scalar_neg() { - let mut rng = thread_rng(); + let mut rng = rng(); let a = Scalar::random(&mut rng); let expected_res = -a; @@ -507,7 +533,7 @@ mod test { /// Tests multiplication of raw scalars in a circuit #[tokio::test] async fn test_scalar_mul() { - let mut rng = thread_rng(); + let mut rng = rng(); let a = Scalar::random(&mut rng); let b = Scalar::random(&mut rng); @@ -528,7 +554,7 @@ mod test { #[tokio::test] async fn test_batch_mul_constant() { const N: usize = 1000; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let b = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); @@ -552,7 +578,7 @@ mod test { /// Tests exponentiation or raw scalars in a circuit #[tokio::test] async fn test_exp() { - let mut rng = thread_rng(); + let mut rng = rng(); let base = Scalar::::random(&mut rng); let exp = rng.next_u64(); @@ -571,7 +597,7 @@ mod test { /// Tests fft of scalars allocated in a circuit #[tokio::test] async fn test_circuit_fft() { - let mut rng = thread_rng(); + let mut rng = rng(); let n: usize = rng.gen_range(1..=100); let domain_size = rng.gen_range(n..10 * n); @@ -603,7 +629,7 @@ mod test { /// Tests the ifft of scalars allocated in a circuit #[tokio::test] async fn test_circuit_ifft() { - let mut rng = thread_rng(); + let mut rng = rng(); let n: usize = rng.gen_range(1..=100); let domain_size = rng.gen_range(n..10 * n); diff --git a/online-phase/src/commitment.rs b/online-phase/src/commitment.rs index b20bec5..0117410 100644 --- a/online-phase/src/commitment.rs +++ b/online-phase/src/commitment.rs @@ -3,7 +3,7 @@ use ark_ec::CurveGroup; use itertools::Itertools; -use rand::thread_rng; +use rand::rng; use sha3::{Digest, Sha3_256}; use crate::{ @@ -64,7 +64,7 @@ impl> + ToBytes> HashCommitmentResult> + ToBytes> HashCommitmentResult::random(&mut rng); let (res, _) = execute_mock_mpc(|fabric| async move { @@ -136,7 +136,7 @@ mod test { #[tokio::test] async fn test_scalar_batch_commit() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let values = (0..N).map(|_| Scalar::::random(&mut rng)).collect_vec(); let (res, _) = execute_mock_mpc(|fabric| { @@ -156,7 +156,7 @@ mod test { /// Tests committing and verifying a curve point #[tokio::test] async fn test_point_commit() { - let mut rng = thread_rng(); + let mut rng = rng(); let value = CurvePoint::::generator() * Scalar::::random(&mut rng); let (res, _) = execute_mock_mpc(|fabric| async move { @@ -174,7 +174,7 @@ mod test { #[tokio::test] async fn test_point_batch_commit() { const N: usize = 100; - let mut rng = thread_rng(); + let mut rng = rng(); let values = (0..N) .map(|_| CurvePoint::::generator() * Scalar::::random(&mut rng)) .collect_vec(); @@ -196,7 +196,7 @@ mod test { /// Tests an invalid commitment #[tokio::test] async fn test_invalid_commit() { - let mut rng = thread_rng(); + let mut rng = rng(); let value = Scalar::::random(&mut rng); let (res, _) = execute_mock_mpc(|fabric| async move { diff --git a/online-phase/src/fabric/executor/multi_threaded/result_buffer.rs b/online-phase/src/fabric/executor/multi_threaded/result_buffer.rs index a80e519..d1ad591 100644 --- a/online-phase/src/fabric/executor/multi_threaded/result_buffer.rs +++ b/online-phase/src/fabric/executor/multi_threaded/result_buffer.rs @@ -187,7 +187,7 @@ mod test { use std::{collections::HashSet, thread}; use itertools::Itertools; - use rand::{distributions::uniform::SampleRange, thread_rng, Rng}; + use rand::{distributions::uniform::SampleRange, rng, Rng}; use crate::{algebra::Scalar, test_helpers::TestCurve, ResultValue}; @@ -204,7 +204,7 @@ mod test { /// Tests a simple get and set pattern #[test] fn test_get_and_set() { - let mut rng = thread_rng(); + let mut rng = rng(); let buf = test_buffer(); let idx = rng.gen_range(0..DEFAULT_SIZE); @@ -224,7 +224,7 @@ mod test { /// Tests various ways of getting values that are not present #[test] fn test_missing_value() { - let mut rng = thread_rng(); + let mut rng = rng(); let buf = test_buffer(); // A random index not yet set @@ -242,7 +242,7 @@ mod test { /// Tests growing the buffer to multiple shards and then getting all values #[test] fn test_grow_and_get() { - let mut rng = thread_rng(); + let mut rng = rng(); let buf = test_buffer(); const N: usize = DEFAULT_SIZE * 4; @@ -261,7 +261,7 @@ mod test { /// Tests setting a value that requires growing multiple shards #[test] fn test_grow_multi() { - let mut rng = thread_rng(); + let mut rng = rng(); let buf = test_buffer(); // Set a value well outside of the current capacity @@ -280,7 +280,7 @@ mod test { fn test_set_and_get_random() { const N: usize = 1000; const MAX_IDX: usize = DEFAULT_SIZE * 100; - let mut rng = thread_rng(); + let mut rng = rng(); let buf = test_buffer(); // Use a hash set to ensure the indices are unique @@ -301,7 +301,7 @@ mod test { /// Tests data coherence between threads #[test] fn test_multithreaded_coherence() { - let mut rng = thread_rng(); + let mut rng = rng(); let buf = test_buffer(); let idx = (2 * DEFAULT_SIZE..3 * DEFAULT_SIZE).sample_single(&mut rng); diff --git a/online-phase/src/fabric/executor/multi_threaded/result_mask.rs b/online-phase/src/fabric/executor/multi_threaded/result_mask.rs index a390861..48eeda1 100644 --- a/online-phase/src/fabric/executor/multi_threaded/result_mask.rs +++ b/online-phase/src/fabric/executor/multi_threaded/result_mask.rs @@ -47,7 +47,7 @@ impl ResultMask { #[cfg(test)] mod result_mask_test { - use rand::{distributions::uniform::SampleRange, thread_rng}; + use rand::{distributions::uniform::SampleRange, rng}; use super::ResultMask; @@ -57,7 +57,7 @@ mod result_mask_test { /// Tests getting a value that has not been set yet #[test] fn test_unset_value() { - let mut rng = thread_rng(); + let mut rng = rng(); let mask = ResultMask::new(DEFAULT_SIZE); // Not set @@ -75,7 +75,7 @@ mod result_mask_test { /// Tests a simple set and get pattern #[test] fn test_set_value() { - let mut rng = thread_rng(); + let mut rng = rng(); let mut mask = ResultMask::new(DEFAULT_SIZE); // Get the value before it is set @@ -90,7 +90,7 @@ mod result_mask_test { /// Tests growing the buffer by setting a value #[test] fn test_grow_and_get() { - let mut rng = thread_rng(); + let mut rng = rng(); let mut mask = ResultMask::new(DEFAULT_SIZE); // Get the value well out of range before it is set diff --git a/online-phase/src/gadgets.rs b/online-phase/src/gadgets.rs index 5cbb9b9..b90893c 100644 --- a/online-phase/src/gadgets.rs +++ b/online-phase/src/gadgets.rs @@ -151,7 +151,7 @@ pub fn prefix_product( mod test { use futures::future; use itertools::Itertools; - use rand::{thread_rng, Rng}; + use rand::{rng, Rng}; use crate::{ algebra::{AuthenticatedScalarResult, Scalar}, @@ -164,7 +164,7 @@ mod test { #[tokio::test] async fn test_prefix_prod() { const N: usize = 10; - let mut rng = thread_rng(); + let mut rng = rng(); let values = (0..N).map(|_| Scalar::random(&mut rng)).collect_vec(); let mut expected_res = vec![values[0]]; @@ -224,7 +224,7 @@ mod test { #[tokio::test] async fn test_xor_batch() { const N: usize = 10; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::from(rng.gen_bool(0.5))).collect_vec(); let b = (0..N).map(|_| Scalar::from(rng.gen_bool(0.5))).collect_vec(); @@ -284,7 +284,7 @@ mod test { #[tokio::test] async fn test_xor_public_batch() { const N: usize = 10; - let mut rng = thread_rng(); + let mut rng = rng(); let a = (0..N).map(|_| Scalar::from(rng.gen_bool(0.5))).collect_vec(); let b = (0..N).map(|_| Scalar::from(rng.gen_bool(0.5))).collect_vec(); diff --git a/online-phase/src/lib.rs b/online-phase/src/lib.rs index 19b859a..e0ea951 100644 --- a/online-phase/src/lib.rs +++ b/online-phase/src/lib.rs @@ -47,9 +47,9 @@ pub const PARTY1: u64 = 1; #[cfg(feature = "types")] pub fn random_point() -> algebra::CurvePoint { use algebra::{CurvePoint, Scalar}; - use rand::thread_rng; + use rand::rng; - let mut rng = thread_rng(); + let mut rng = rng(); CurvePoint::generator() * Scalar::random(&mut rng) } diff --git a/online-phase/src/network/cert_verifier.rs b/online-phase/src/network/cert_verifier.rs index 35253e2..e3c72c8 100644 --- a/online-phase/src/network/cert_verifier.rs +++ b/online-phase/src/network/cert_verifier.rs @@ -2,9 +2,11 @@ //! request We do not care about the authenticity of certificates during the //! course of a p2p MPC -use rustls::client::{ServerCertVerified, ServerCertVerifier}; +use rustls::pki_types::{CertificateDer, ServerName, UnixTime}; +use rustls::{DigitallySignedStruct, SignatureScheme, client::danger::*}; /// Responds Ok() to any certificate verification request +#[derive(Debug)] pub(crate) struct PassThroughCertVerifier; impl PassThroughCertVerifier { @@ -17,13 +19,45 @@ impl PassThroughCertVerifier { impl ServerCertVerifier for PassThroughCertVerifier { fn verify_server_cert( &self, - _end_entity: &rustls::Certificate, - _intermediates: &[rustls::Certificate], - _server_name: &rustls::ServerName, - _scts: &mut dyn Iterator, + _end_entity: &CertificateDer<'_>, + _intermediates: &[CertificateDer<'_>], + _server_name: &ServerName<'_>, _ocsp_response: &[u8], - _now: std::time::SystemTime, - ) -> Result { + _now: UnixTime, + ) -> Result { Ok(ServerCertVerified::assertion()) } + + fn verify_tls12_signature( + &self, + _message: &[u8], + _cert: &CertificateDer<'_>, + _dss: &DigitallySignedStruct, + ) -> Result { + Ok(HandshakeSignatureValid::assertion()) + } + + fn verify_tls13_signature( + &self, + _message: &[u8], + _cert: &CertificateDer<'_>, + _dss: &DigitallySignedStruct, + ) -> Result { + Ok(HandshakeSignatureValid::assertion()) + } + + fn supported_verify_schemes(&self) -> Vec { + vec![ + SignatureScheme::ECDSA_NISTP521_SHA512, + SignatureScheme::ECDSA_NISTP384_SHA384, + SignatureScheme::ECDSA_NISTP256_SHA256, + SignatureScheme::ED25519, + SignatureScheme::RSA_PSS_SHA512, + SignatureScheme::RSA_PSS_SHA384, + SignatureScheme::RSA_PSS_SHA256, + SignatureScheme::RSA_PKCS1_SHA512, + SignatureScheme::RSA_PKCS1_SHA384, + SignatureScheme::RSA_PKCS1_SHA256, + ] + } } diff --git a/online-phase/src/network/config.rs b/online-phase/src/network/config.rs index 487e041..56d2674 100644 --- a/online-phase/src/network/config.rs +++ b/online-phase/src/network/config.rs @@ -2,9 +2,11 @@ use std::{sync::Arc, time::Duration}; +use quinn::crypto::rustls::QuicClientConfig; use quinn::{ClientConfig, IdleTimeout, ServerConfig, TransportConfig, VarInt}; -use rcgen::RcgenError; -use rustls::{Certificate, ClientConfig as CryptoClientConfig}; +use rcgen::Error as RcgenError; +use rustls::ClientConfig as CryptoClientConfig; +use rustls::pki_types::{CertificateDer, PrivateKeyDer}; use crate::error::SetupError; use crate::network::cert_verifier::PassThroughCertVerifier; @@ -34,22 +36,18 @@ pub fn build_configs() -> Result<(ClientConfig, ServerConfig), SetupError> { // Generate a self-signed server certificate for the QUIC connection let (cert, key) = generate_cert().map_err(|_| SetupError::KeygenError)?; - // Setup the certificate root - let mut roots = rustls::RootCertStore::empty(); - roots.add(&cert).map_err(|_| SetupError::ServerSetupError)?; - - // Pass the self-signed cert to the client, and disable auth; p2p auth should - // happen at a higher layer - let mut client_crypto_config = CryptoClientConfig::builder() - .with_safe_defaults() - .with_root_certificates(roots) - .with_no_client_auth(); - client_crypto_config + // Disable certificate authenticity checks; p2p auth should happen at a + // higher layer + let client_crypto_config = CryptoClientConfig::builder() .dangerous() - .set_certificate_verifier(Arc::new(PassThroughCertVerifier::new())); + .with_custom_certificate_verifier(Arc::new(PassThroughCertVerifier::new())) + .with_no_client_auth(); // 3. Client and server setup - let mut client_config = ClientConfig::new(Arc::new(client_crypto_config)); + let mut client_config = ClientConfig::new(Arc::new( + QuicClientConfig::try_from(client_crypto_config) + .map_err(|_| SetupError::ServerSetupError)?, + )); client_config.transport_config(transport.clone()); let mut server_config = ServerConfig::with_single_cert(vec![cert], key) @@ -61,13 +59,9 @@ pub fn build_configs() -> Result<(ClientConfig, ServerConfig), SetupError> { /// Generates a self-signed certificate to construct TLS 1.3 connections with /// borrowed from https://github.com/maidsafe/qp2p/blob/main/src/config.rs#L317 -fn generate_cert() -> Result<(Certificate, rustls::PrivateKey), RcgenError> { - let cert = rcgen::generate_simple_self_signed(vec![SERVER_NAME.to_string()])?; - - let key = cert.serialize_private_key_der(); - let cert = cert.serialize_der().unwrap(); +fn generate_cert() -> Result<(CertificateDer<'static>, PrivateKeyDer<'static>), RcgenError> { + let rcgen::CertifiedKey { cert, signing_key } = + rcgen::generate_simple_self_signed(vec![SERVER_NAME.to_string()])?; - let key = rustls::PrivateKey(key); - let cert = Certificate(cert); - Ok((cert, key)) + Ok((cert.der().clone(), PrivateKeyDer::from(signing_key))) } diff --git a/online-phase/src/network/quic.rs b/online-phase/src/network/quic.rs index 3902664..24f2e16 100644 --- a/online-phase/src/network/quic.rs +++ b/online-phase/src/network/quic.rs @@ -267,7 +267,6 @@ where .as_mut() .unwrap() .finish() - .await .map_err(|_| MpcNetworkError::ConnectionTeardownError) } }