Skip to content

Commit

Permalink
core: Make lib wasm compilable
Browse files Browse the repository at this point in the history
This commit also removes the `rand_core` dependency as it can be
replaced by `rand`.

Resolves #195
  • Loading branch information
moCello committed Jun 3, 2024
1 parent 144e614 commit d548a8b
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 23 deletions.
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
2 changes: 1 addition & 1 deletion core/tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down

0 comments on commit d548a8b

Please sign in to comment.