Skip to content

Commit

Permalink
circuits: Remove rand_core dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
moCello committed Jun 3, 2024
1 parent 6f1b7eb commit 144e614
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
3 changes: 1 addition & 2 deletions circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion circuits/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 12 additions & 7 deletions circuits/tests/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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::<ElGamalCircuit>(&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");
Expand Down
28 changes: 20 additions & 8 deletions circuits/tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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::<TxCircuit<HEIGHT, 1>>(&TP.pp, LABEL)
.expect("failed to compile circuit");
Expand All @@ -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,
Expand All @@ -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::<TxCircuit<HEIGHT, 2>>(&TP.pp, LABEL)
.expect("failed to compile circuit");
Expand All @@ -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,
Expand All @@ -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::<TxCircuit<HEIGHT, 3>>(&TP.pp, LABEL)
.expect("failed to compile circuit");
Expand All @@ -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,
Expand All @@ -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::<TxCircuit<HEIGHT, 4>>(&TP.pp, LABEL)
.expect("failed to compile circuit");
Expand All @@ -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,
Expand Down

0 comments on commit 144e614

Please sign in to comment.