Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wasm-compilation for phoenix-core #196

Merged
merged 2 commits into from
Jun 3, 2024
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
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
5 changes: 5 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -321,6 +325,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Canonical implementation shielded by feature.

<!-- ISSUES -->
[#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
Expand Down
4 changes: 2 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion core/src/encryption/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion core/src/keys/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand Down
2 changes: 1 addition & 1 deletion core/src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
7 changes: 5 additions & 2 deletions core/tests/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.");
Expand Down
23 changes: 16 additions & 7 deletions core/tests/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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());

Expand All @@ -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);

Expand All @@ -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);
Expand Down
17 changes: 9 additions & 8 deletions core/tests/note_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading