From 144e614561cd4bfcc88b0244bb30ffcd8e2ae8f7 Mon Sep 17 00:00:00 2001 From: moana Date: Mon, 3 Jun 2024 14:59:11 +0200 Subject: [PATCH 1/2] circuits: Remove `rand_core` dependency --- circuits/Cargo.toml | 3 +-- circuits/src/transaction.rs | 2 +- circuits/tests/elgamal.rs | 19 ++++++++++++------- circuits/tests/transaction.rs | 28 ++++++++++++++++++++-------- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/circuits/Cargo.toml b/circuits/Cargo.toml index a3f2c89..71587f0 100644 --- a/circuits/Cargo.toml +++ b/circuits/Cargo.toml @@ -16,8 +16,7 @@ dusk-jubjub = { version = "0.14", default-features = false } poseidon-merkle = { version = "0.6", features = ["rkyv-impl", "zk", "size_32"] } dusk-poseidon = { version = "0.39", features = ["zk"] } jubjub-schnorr = { version = "0.4", features = ["zk"] } -rand_core = { version = "0.6", default-features = false } -rand = "0.8" +rand = { version = "0.8", default-features = false, features = ["std_rng"] } [dev-dependencies] ff = { version = "0.13", default-features = false } diff --git a/circuits/src/transaction.rs b/circuits/src/transaction.rs index 9c866ba..dfb1cd2 100644 --- a/circuits/src/transaction.rs +++ b/circuits/src/transaction.rs @@ -14,7 +14,7 @@ use jubjub_schnorr::{gadgets, SignatureDouble}; use poseidon_merkle::{zk::opening_gadget, Item, Opening, Tree}; use rand::rngs::StdRng; -use rand_core::{CryptoRng, RngCore, SeedableRng}; +use rand::{CryptoRng, RngCore, SeedableRng}; extern crate alloc; use alloc::vec::Vec; diff --git a/circuits/tests/elgamal.rs b/circuits/tests/elgamal.rs index 83e6ed8..b3817e8 100644 --- a/circuits/tests/elgamal.rs +++ b/circuits/tests/elgamal.rs @@ -9,17 +9,20 @@ use dusk_plonk::prelude::*; use ff::Field; use phoenix_circuits::elgamal; use phoenix_core::{PublicKey, SecretKey}; -use rand_core::OsRng; +use rand::rngs::StdRng; +use rand::SeedableRng; #[test] fn test_elgamal_encrypt_and_decrypt() { - let sk = SecretKey::random(&mut OsRng); + let mut rng = StdRng::seed_from_u64(0xc0b); + + let sk = SecretKey::random(&mut rng); let pk = PublicKey::from(&sk); let message = GENERATOR_EXTENDED * JubJubScalar::from(1234u64); // Encrypt using a fresh random value 'r' - let r = JubJubScalar::random(&mut OsRng); + let r = JubJubScalar::random(&mut rng); let (c1, c2) = elgamal::encrypt(pk.A(), &message, &r); // Assert decryption @@ -97,21 +100,23 @@ impl Circuit for ElGamalCircuit { #[test] fn test_elgamal_gadgets() { - let sk = SecretKey::random(&mut OsRng); + let mut rng = StdRng::seed_from_u64(0xc0b); + + let sk = SecretKey::random(&mut rng); let pk = PublicKey::from(&sk); let message = GENERATOR_EXTENDED * JubJubScalar::from(1234u64); - let r = JubJubScalar::random(&mut OsRng); + let r = JubJubScalar::random(&mut rng); let (c1, c2) = elgamal::encrypt(pk.A(), &message, &r); - let pp = PublicParameters::setup(1 << CAPACITY, &mut OsRng).unwrap(); + let pp = PublicParameters::setup(1 << CAPACITY, &mut rng).unwrap(); let (prover, verifier) = Compiler::compile::(&pp, LABEL) .expect("failed to compile circuit"); let (proof, public_inputs) = prover .prove( - &mut OsRng, + &mut rng, &ElGamalCircuit::new(&pk.A(), &sk.a(), &message, &r, &c1, &c2), ) .expect("failed to prove"); diff --git a/circuits/tests/transaction.rs b/circuits/tests/transaction.rs index 0857d59..a186eee 100644 --- a/circuits/tests/transaction.rs +++ b/circuits/tests/transaction.rs @@ -4,7 +4,9 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use rand_core::{CryptoRng, OsRng, RngCore}; +use rand::rngs::StdRng; +use rand::SeedableRng; +use rand::{CryptoRng, RngCore}; use dusk_jubjub::JubJubScalar; use phoenix_circuits::transaction::{TxCircuit, TxInputNote, TxOutputNote}; @@ -32,15 +34,17 @@ struct TestingParameters { lazy_static! { static ref TP: TestingParameters = { - let pp = PublicParameters::setup(1 << CAPACITY, &mut OsRng).unwrap(); - let sk = SecretKey::random(&mut OsRng); + let mut rng = StdRng::seed_from_u64(0xc0b); + + let pp = PublicParameters::setup(1 << CAPACITY, &mut rng).unwrap(); + let sk = SecretKey::random(&mut rng); let mut tree = Tree::<(), HEIGHT>::new(); let skeleton_hash = BlsScalar::from(1234u64); // create and insert into the tree 4 testing tx input notes let tx_input_notes = - create_test_tx_input_notes::<4>(&mut OsRng, &mut tree, &sk, skeleton_hash); + create_test_tx_input_notes::<4>(&mut rng, &mut tree, &sk, skeleton_hash); // retrieve the root from the tree after inserting the notes let root = tree.root().hash; @@ -117,6 +121,8 @@ fn create_test_tx_output_note(value: u64) -> TxOutputNote { #[test] fn test_transfer_circuit_1_2() { + let mut rng = StdRng::seed_from_u64(0xc0b); + let (prover, verifier) = Compiler::compile::>(&TP.pp, LABEL) .expect("failed to compile circuit"); @@ -131,7 +137,7 @@ fn test_transfer_circuit_1_2() { let (proof, public_inputs) = prover .prove( - &mut OsRng, + &mut rng, &TxCircuit::new( input_notes, tx_output_notes, @@ -150,6 +156,8 @@ fn test_transfer_circuit_1_2() { #[test] fn test_transfer_circuit_2_2() { + let mut rng = StdRng::seed_from_u64(0xc0b); + let (prover, verifier) = Compiler::compile::>(&TP.pp, LABEL) .expect("failed to compile circuit"); @@ -165,7 +173,7 @@ fn test_transfer_circuit_2_2() { let (proof, public_inputs) = prover .prove( - &mut OsRng, + &mut rng, &TxCircuit::new( input_notes, tx_output_notes, @@ -184,6 +192,8 @@ fn test_transfer_circuit_2_2() { #[test] fn test_transfer_circuit_3_2() { + let mut rng = StdRng::seed_from_u64(0xc0b); + let (prover, verifier) = Compiler::compile::>(&TP.pp, LABEL) .expect("failed to compile circuit"); @@ -202,7 +212,7 @@ fn test_transfer_circuit_3_2() { let (proof, public_inputs) = prover .prove( - &mut OsRng, + &mut rng, &TxCircuit::new( input_notes, tx_output_notes, @@ -221,6 +231,8 @@ fn test_transfer_circuit_3_2() { #[test] fn test_transfer_circuit_4_2() { + let mut rng = StdRng::seed_from_u64(0xc0b); + let (prover, verifier) = Compiler::compile::>(&TP.pp, LABEL) .expect("failed to compile circuit"); @@ -233,7 +245,7 @@ fn test_transfer_circuit_4_2() { let (proof, public_inputs) = prover .prove( - &mut OsRng, + &mut rng, &TxCircuit::new( TP.tx_input_notes.clone(), tx_output_notes, From d548a8b054cc20e6040cbb3f3ddaef3ef062eb98 Mon Sep 17 00:00:00 2001 From: moana Date: Mon, 3 Jun 2024 14:59:46 +0200 Subject: [PATCH 2/2] core: Make lib wasm compilable This commit also removes the `rand_core` dependency as it can be replaced by `rand`. Resolves #195 --- core/CHANGELOG.md | 5 +++++ core/Cargo.toml | 4 ++-- core/src/encryption/aes.rs | 2 +- core/src/keys/secret.rs | 2 +- core/src/note.rs | 2 +- core/tests/encryption.rs | 7 +++++-- core/tests/keys.rs | 23 ++++++++++++++++------- core/tests/note_test.rs | 17 +++++++++-------- core/tests/transaction.rs | 2 +- 9 files changed, 41 insertions(+), 23 deletions(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index d9f61af..a9f2e37 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Rename `crossover` to `deposit` [#190] +### Removed + +- Remove `"getrandom"` feature from `aes-gcm` dependency [#195] + ## [0.28.1] - 2024-05-23 ### Changed @@ -321,6 +325,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Canonical implementation shielded by feature. +[#195]: https://github.com/dusk-network/phoenix/issues/195 [#190]: https://github.com/dusk-network/phoenix/issues/190 [#183]: https://github.com/dusk-network/phoenix/issues/183 [#179]: https://github.com/dusk-network/phoenix/issues/179 diff --git a/core/Cargo.toml b/core/Cargo.toml index 4e95719..c922a04 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -8,7 +8,7 @@ license = "MPL-2.0" exclude = [".github/workflows/dusk-ci.yml", ".gitignore"] [dependencies] -rand_core = { version = "0.6", default-features = false } +rand = { version = "0.8", default-features = false } dusk-bytes = "0.1" dusk-bls12_381 = { version = "0.13", default-features = false } bls12_381-bls = { version = "0.3", default-features = false } @@ -17,7 +17,7 @@ dusk-poseidon = "0.39" jubjub-schnorr = "0.4" subtle = { version = "^2.2.1", default-features = false } ff = { version = "0.13", default-features = false } -aes-gcm = "0.10" +aes-gcm = { version = "0.10", default-features = false, features = ["aes", "alloc", "rand_core"] } zeroize = { version = "1", default-features = false, features = ["derive"] } rkyv = { version = "0.7", optional = true, default-features = false } bytecheck = { version = "0.6", optional = true, default-features = false } diff --git a/core/src/encryption/aes.rs b/core/src/encryption/aes.rs index 370c953..9a3a8bb 100644 --- a/core/src/encryption/aes.rs +++ b/core/src/encryption/aes.rs @@ -5,7 +5,7 @@ // Copyright (c) DUSK NETWORK. All rights reserved. use dusk_jubjub::JubJubAffine; -use rand_core::{CryptoRng, RngCore}; +use rand::{CryptoRng, RngCore}; use aes_gcm::{ aead::{Aead, AeadCore, KeyInit}, diff --git a/core/src/keys/secret.rs b/core/src/keys/secret.rs index 34bf87e..d81a971 100644 --- a/core/src/keys/secret.rs +++ b/core/src/keys/secret.rs @@ -14,7 +14,7 @@ use zeroize::Zeroize; use rkyv::{Archive, Deserialize, Serialize}; use dusk_bytes::{DeserializableSlice, Error, Serializable}; -use rand_core::{CryptoRng, RngCore}; +use rand::{CryptoRng, RngCore}; use subtle::{Choice, ConstantTimeEq}; /// Secret pair of `a` and `b` defining a [`SecretKey`] diff --git a/core/src/note.rs b/core/src/note.rs index bd402e0..d0b16fd 100644 --- a/core/src/note.rs +++ b/core/src/note.rs @@ -18,7 +18,7 @@ use crate::aes; use dusk_poseidon::{Domain, Hash}; use ff::Field; -use rand_core::{CryptoRng, RngCore}; +use rand::{CryptoRng, RngCore}; #[cfg(feature = "rkyv-impl")] use rkyv::{Archive, Deserialize, Serialize}; diff --git a/core/tests/encryption.rs b/core/tests/encryption.rs index 7a6252e..9d33574 100644 --- a/core/tests/encryption.rs +++ b/core/tests/encryption.rs @@ -6,10 +6,13 @@ use dusk_jubjub::{JubJubAffine, JubJubScalar, GENERATOR}; use phoenix_core::aes; -use rand_core::OsRng; +use rand::rngs::StdRng; +use rand::SeedableRng; #[test] fn test_aes_encrypt_and_decrypt() { + let mut rng = StdRng::seed_from_u64(0xc0b); + const PLAINTEXT_SIZE: usize = 20; const ENCRYPTION_SIZE: usize = PLAINTEXT_SIZE + aes::ENCRYPTION_EXTRA_SIZE; @@ -18,7 +21,7 @@ fn test_aes_encrypt_and_decrypt() { let plaintext = b"00112233445566778899"; let encryption: [u8; ENCRYPTION_SIZE] = - aes::encrypt(&shared_secret_key, plaintext, &mut OsRng) + aes::encrypt(&shared_secret_key, plaintext, &mut rng) .expect("Encrypted correctly."); let dec_plaintext = aes::decrypt(&shared_secret_key, &encryption) .expect("Decrypted correctly."); diff --git a/core/tests/keys.rs b/core/tests/keys.rs index 540b7a2..451aa27 100644 --- a/core/tests/keys.rs +++ b/core/tests/keys.rs @@ -8,12 +8,15 @@ use dusk_bytes::{DeserializableSlice, Serializable}; use dusk_jubjub::JubJubScalar; use ff::Field; use phoenix_core::{PublicKey, SecretKey, ViewKey}; -use rand_core::OsRng; +use rand::rngs::StdRng; +use rand::SeedableRng; use zeroize::Zeroize; #[test] fn sk_from_bytes() { - let sk = SecretKey::random(&mut OsRng); + let mut rng = StdRng::seed_from_u64(0xc0b); + + let sk = SecretKey::random(&mut rng); let sk_bytes = sk.to_bytes(); assert_eq!( @@ -24,7 +27,9 @@ fn sk_from_bytes() { #[test] fn sk_zeroize() { - let mut sk = SecretKey::random(&mut OsRng); + let mut rng = StdRng::seed_from_u64(0xc0b); + + let mut sk = SecretKey::random(&mut rng); let sk_zeroized = SecretKey::new(JubJubScalar::zero(), JubJubScalar::zero()); @@ -37,7 +42,9 @@ fn sk_zeroize() { #[test] fn keys_encoding() { - let sk = SecretKey::random(&mut OsRng); + let mut rng = StdRng::seed_from_u64(0xc0b); + + let sk = SecretKey::random(&mut rng); let vk = ViewKey::from(&sk); let pk = PublicKey::from(&sk); @@ -49,15 +56,17 @@ fn keys_encoding() { fn keys_consistency() { use dusk_jubjub::{JubJubScalar, GENERATOR_EXTENDED}; - let r = JubJubScalar::random(&mut OsRng); - let sk = SecretKey::random(&mut OsRng); + let mut rng = StdRng::seed_from_u64(0xc0b); + + let r = JubJubScalar::random(&mut rng); + let sk = SecretKey::random(&mut rng); let pk = PublicKey::from(&sk); let vk = ViewKey::from(&sk); let sa = pk.gen_stealth_address(&r); assert!(vk.owns(&sa)); - let wrong_sk = SecretKey::random(&mut OsRng); + let wrong_sk = SecretKey::random(&mut rng); let wrong_vk = ViewKey::from(&wrong_sk); assert_ne!(sk, wrong_sk); diff --git a/core/tests/note_test.rs b/core/tests/note_test.rs index dc7cbcf..8a5c8f7 100644 --- a/core/tests/note_test.rs +++ b/core/tests/note_test.rs @@ -9,11 +9,12 @@ use ff::Field; use phoenix_core::{ Error, Note, NoteType, Ownable, PublicKey, SecretKey, ViewKey, }; -use rand_core::OsRng; +use rand::rngs::StdRng; +use rand::SeedableRng; #[test] fn transparent_note() -> Result<(), Error> { - let mut rng = OsRng; + let mut rng = StdRng::seed_from_u64(0xc0b); let sk = SecretKey::random(&mut rng); let pk = PublicKey::from(&sk); @@ -29,7 +30,7 @@ fn transparent_note() -> Result<(), Error> { #[test] fn transparent_stealth_note() -> Result<(), Error> { - let mut rng = OsRng; + let mut rng = StdRng::seed_from_u64(0xc0b); let sk = SecretKey::random(&mut rng); let pk = PublicKey::from(&sk); @@ -50,7 +51,7 @@ fn transparent_stealth_note() -> Result<(), Error> { #[test] fn obfuscated_note() -> Result<(), Error> { - let mut rng = OsRng; + let mut rng = StdRng::seed_from_u64(0xc0b); let sk = SecretKey::random(&mut rng); let pk = PublicKey::from(&sk); @@ -68,7 +69,7 @@ fn obfuscated_note() -> Result<(), Error> { #[test] fn obfuscated_deterministic_note() -> Result<(), Error> { - let mut rng = OsRng; + let mut rng = StdRng::seed_from_u64(0xc0b); let sk = SecretKey::random(&mut rng); let pk = PublicKey::from(&sk); @@ -88,7 +89,7 @@ fn obfuscated_deterministic_note() -> Result<(), Error> { #[test] fn value_commitment_transparent() { - let mut rng = OsRng; + let mut rng = StdRng::seed_from_u64(0xc0b); let sk = SecretKey::random(&mut rng); let vk = ViewKey::from(&sk); @@ -115,7 +116,7 @@ fn value_commitment_transparent() { #[test] fn value_commitment_obfuscated() { - let mut rng = OsRng; + let mut rng = StdRng::seed_from_u64(0xc0b); let sk = SecretKey::random(&mut rng); let vk = ViewKey::from(&sk); @@ -143,7 +144,7 @@ fn value_commitment_obfuscated() { #[test] fn note_keys_consistency() { - let mut rng = OsRng; + let mut rng = StdRng::seed_from_u64(0xc0b); let sk = SecretKey::random(&mut rng); let pk = PublicKey::from(&sk); diff --git a/core/tests/transaction.rs b/core/tests/transaction.rs index bf19e93..6f1695a 100644 --- a/core/tests/transaction.rs +++ b/core/tests/transaction.rs @@ -10,7 +10,7 @@ use dusk_bls12_381::BlsScalar; use dusk_jubjub::JubJubScalar; use ff::Field; use phoenix_core::{Error, Note, PublicKey, SecretKey, TxSkeleton}; -use rand_core::OsRng; +use rand::rngs::OsRng; #[test] fn transaction_parse() -> Result<(), Error> {