Skip to content

Commit

Permalink
Update crypto primitives to latest versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Daksh14 committed May 5, 2024
1 parent b6af8a7 commit a9573d4
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 169 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
[target.wasm32-unknown-unknown]
rustflags = ["-C", "target-feature=+simd128"]
32 changes: 13 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,12 @@ bs58 = { version = "0.5", default-features = false, features = [
"alloc",
"cb58",
] }
dusk-bls12_381-sign = { version = "0.5", default-features = false }
dusk-bytes = "0.1.7"
dusk-jubjub = { version = "0.13.1", default-features = false }
dusk-pki = { version = "0.13", default-features = false, features = [
"rkyv-impl",
] }
dusk-schnorr = { version = "0.14", default-features = false, features = [
"rkyv-impl",
"alloc",
] }
phoenix-core = { version = "0.21", default-features = false, features = [
phoenix-core = { version = "0.26", default-features = false, features = [
"alloc",
"rkyv-impl",
] }
poseidon-merkle = { version = "0.3", features = ["rkyv-impl"] }
poseidon-merkle = { version = "0.5", features = ["rkyv-impl"] }
rand_chacha = { version = "^0.3", default-features = false }
rand_core = "^0.6"
rkyv = { version = "^0.7", default-features = false, features = ["size_32"] }
Expand All @@ -40,37 +31,40 @@ serde = { version = "1.0", default-features = false, features = [
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
sha2 = { version = "^0.10", default-features = false }
bip39 = { version = "2.0.0", optional = true, default-features = false }
dusk-plonk = { version = "0.16", default-features = false, features = [
dusk-plonk = { version = "0.19", default-features = false, features = [
"alloc",
"rkyv-impl",
] }
dusk-poseidon = { version = "0.31", default-features = false, features = [
dusk-poseidon = { version = "0.33", default-features = false, features = [
"alloc",
"rkyv-impl",
] }
ff = { version = "0.13", default-features = false }
dusk-bls12_381 = { version = "0.12.3", default-features = false, features = [
dusk-bls12_381 = { version = "0.13", default-features = false, features = [
"alloc",
"rkyv-impl",
] }
hex = { version = "0.4", default_features = false, features = ["alloc"] }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
hashbrown = "0.14.3"
stake-contract-types = "0.0.1-rc.2"
stake-contract-types = "0.1.0-rc"
bls12_381-bls = { version = "0.2", default-features = false, features = ["rkyv-impl"] }
dusk-jubjub = { version = "0.14", default-features = false, features = ["rkyv-impl", "alloc"] }
jubjub-schnorr = { version = "0.2", default-features = false, features = ["double", "rkyv-impl", "alloc"] }

[features]
# rust platforms can use this module without the compact feature
default = ["compat"]
compat = ["dep:bip39"]

[target.'cfg(target_family = "wasm")'.dependencies]
rusk-abi = "0.12.0-rc.0"
rusk-abi = "0.13.0-rc"

[target.'cfg(not(target_family = "wasm"))'.dependencies]
rusk-abi = { version = "0.12.0-rc.0", default-features = false }
rusk-abi = { version = "0.13.0-rc", default-features = false }

[dev-dependencies]
rand = "^0.8"
wasmer = "=3.1"
wasmer = "3.1.1"

[build-dependencies]
schemafy_lib = "0.6"
Expand Down
Binary file modified assets/dusk_wallet_core.wasm
Binary file not shown.
10 changes: 5 additions & 5 deletions src/compat/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use dusk_bls12_381::BlsScalar;
use dusk_bytes::Serializable;
use phoenix_core::{
transaction::{ArchivedTreeLeaf, TreeLeaf},
Note,
Note, PublicKey,
};

use alloc::{string::ToString, vec::Vec};
Expand Down Expand Up @@ -63,8 +63,8 @@ pub fn check_note_ownership(args: i32, len: i32) -> i64 {
let view_key = key::derive_vk(&seed, idx);

if view_key.owns(&note) {
let ssk: dusk_pki::SecretSpendKey = key::derive_ssk(&seed, idx);
let nullifier = note.gen_nullifier(&ssk);
let sk = key::derive_sk(&seed, idx);
let nullifier = note.gen_nullifier(&sk);

let nullifier_found =
match rkyv::to_bytes::<BlsScalar, MAX_LEN>(&nullifier).ok()
Expand All @@ -73,8 +73,8 @@ pub fn check_note_ownership(args: i32, len: i32) -> i64 {
None => return utils::fail(),
};

let psk_found = bs58::encode(ssk.public_spend_key().to_bytes())
.into_string();
let psk_found =
bs58::encode(PublicKey::from(sk).to_bytes()).into_string();

let raw_note: Vec<u8> =
match rkyv::to_bytes::<Note, MAX_LEN>(&note).ok() {
Expand Down
4 changes: 2 additions & 2 deletions src/compat/rkyv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::{
utils, MAX_LEN,
};

use bls12_381_bls::PublicKey;
use dusk_bls12_381::BlsScalar;
use dusk_bls12_381_sign::PublicKey;
use phoenix_core::Note;

use alloc::vec::Vec;
Expand Down Expand Up @@ -145,7 +145,7 @@ fn get_public_key_rkyv_serialized(args: i32, len: i32) -> i64 {
None => return utils::fail(),
};

let sk = key::derive_sk(&seed, index);
let sk = key::derive_stake_sk(&seed, index);

let pk = PublicKey::from(&sk);

Expand Down
23 changes: 12 additions & 11 deletions src/compat/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ use crate::{
use alloc::string::String;
use alloc::vec::Vec;

use bls12_381_bls::PublicKey as StakePublicKey;
use dusk_bls12_381::BlsScalar;
use dusk_bls12_381_sign::PublicKey;

use dusk_bytes::Serializable;
use dusk_bytes::Write;
use dusk_jubjub::JubJubScalar;
use dusk_pki::{Ownable, SecretKey as SchnorrKey};
use dusk_plonk::proof_system::Proof;
use dusk_schnorr::Signature;
use ff::Field;

use dusk_plonk::prelude::Proof;
use jubjub_schnorr::Signature;
use phoenix_core::{
transaction::{stct_signature_message, StakeData},
*,
Expand Down Expand Up @@ -64,15 +66,15 @@ pub fn get_stct_proof(args: i32, len: i32) -> i64 {
None => return utils::fail(),
};

let sender = derive_ssk(&seed, sender_index);
let sender = derive_sk(&seed, sender_index);
let refund = match bs58_to_psk(&refund) {
Some(a) => a,
None => return utils::fail(),
};

let rng = &mut utils::rng(rng_seed);

let blinder = JubJubScalar::random(rng);
let blinder = JubJubScalar::random(&mut *rng);
let note = Note::obfuscated(rng, &refund, value, blinder);
let (mut fee, crossover) = note
.try_into()
Expand All @@ -89,10 +91,9 @@ pub fn get_stct_proof(args: i32, len: i32) -> i64 {
let stct_message = stct_signature_message(&crossover, value, contract_id);
let stct_message = dusk_poseidon::sponge::hash(&stct_message);

let sk_r = *sender.sk_r(fee.stealth_address()).as_ref();
let secret = SchnorrKey::from(sk_r);
let nsk = sender.sk_r(fee.stealth_address());

let stct_signature = Signature::new(&secret, rng, stct_message);
let stct_signature = nsk.sign(rng, stct_message);

let vec_allocation = allocate(STCT_INPUT_SIZE as i32) as *mut _;
let mut buf: Vec<u8> = unsafe {
Expand Down Expand Up @@ -177,8 +178,8 @@ pub fn get_stake_call_data(args: i32, len: i32) -> i64 {
None => return utils::fail(),
};

let sk = derive_sk(&seed, staker_index);
let pk = PublicKey::from(&sk);
let sk = derive_stake_sk(&seed, staker_index);
let pk = StakePublicKey::from(&sk);

let msg = stake_signature_message(counter, value);
let signature = sk.sign(&pk, &msg);
Expand Down
6 changes: 3 additions & 3 deletions src/compat/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use dusk_bytes::{
DeserializableSlice, Error as BytesError, Serializable, Write,
};
use dusk_jubjub::{BlsScalar, JubJubAffine, JubJubScalar};
use dusk_plonk::proof_system::Proof;
use dusk_schnorr::Proof as SchnorrSig;
use dusk_plonk::prelude::Proof;
use hashbrown::{hash_map::Entry, HashMap};
use jubjub_schnorr::SignatureDouble;
use phoenix_core::{transaction, Crossover, Fee, Note, Transaction};
use rusk_abi::{hash::Hasher, ContractId, CONTRACT_ID_BYTES};

Expand Down Expand Up @@ -438,7 +438,7 @@ fn input_to_var_bytes(input: &tx::Input) -> Vec<u8> {
let size = BlsScalar::SIZE
+ Note::SIZE
+ JubJubAffine::SIZE
+ SchnorrSig::SIZE
+ SignatureDouble::SIZE
+ u64::SIZE
+ JubJubScalar::SIZE
+ opening_bytes.len();
Expand Down
16 changes: 8 additions & 8 deletions src/compat/unstake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ use crate::{
use alloc::string::String;
use alloc::vec::Vec;

use dusk_bls12_381_sign::PublicKey;
use bls12_381_bls::PublicKey as StakePublicKey;
use dusk_bytes::Serializable;
use dusk_bytes::Write;
use dusk_jubjub::{JubJubAffine, JubJubScalar};
use dusk_plonk::proof_system::Proof;
use dusk_plonk::prelude::Proof;
use ff::Field;
use phoenix_core::*;

use super::stake_contract_types::*;
Expand Down Expand Up @@ -54,15 +55,15 @@ pub fn get_wfct_proof(args: i32, len: i32) -> i64 {
None => return utils::fail(),
};

let sender = derive_ssk(&seed, sender_index);
let sender = derive_sk(&seed, sender_index);
let refund = match bs58_to_psk(&refund) {
Some(a) => a,
None => return utils::fail(),
};

let rng = &mut utils::rng(rng_seed);

let blinder = JubJubScalar::random(rng);
let blinder = JubJubScalar::random(rng.clone());
let note = Note::obfuscated(rng, &refund, 0, blinder);
let (mut fee, crossover) = note
.try_into()
Expand All @@ -71,8 +72,7 @@ pub fn get_wfct_proof(args: i32, len: i32) -> i64 {
fee.gas_limit = gas_limit;
fee.gas_price = gas_price;

let unstake_note =
Note::transparent(rng, &sender.public_spend_key(), value);
let unstake_note = Note::transparent(rng, &PublicKey::from(sender), value);
let unstake_blinder: dusk_jubjub::Fr = unstake_note
.blinding_factor(None)
.expect("Note is transparent so blinding factor is unencrypted");
Expand Down Expand Up @@ -164,8 +164,8 @@ pub fn get_unstake_call_data(args: i32, len: i32) -> i64 {
None => return utils::fail(),
};

let sk = derive_sk(&seed, sender_index);
let public_key = PublicKey::from(&sk);
let sk = derive_stake_sk(&seed, sender_index);
let public_key = StakePublicKey::from(&sk);

let unstake_note = unstake_note.to_bytes();
let signature_message = unstake_signature_message(counter, unstake_note);
Expand Down
19 changes: 9 additions & 10 deletions src/compat/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use crate::{key::*, types, utils, MAX_LEN};
use alloc::string::String;
use ff::Field;

use dusk_bls12_381_sign::PublicKey;
use bls12_381_bls::PublicKey as StakePublicKey;
use dusk_jubjub::{BlsScalar, JubJubScalar};
use dusk_pki::StealthAddress;
use phoenix_core::*;

use super::stake_contract_types::*;
Expand Down Expand Up @@ -49,24 +48,24 @@ pub fn get_withdraw_call_data(args: i32, len: i32) -> i64 {
None => return utils::fail(),
};

let sender_psk = derive_ssk(&seed, sender_index).public_spend_key();
let sk = derive_sk(&seed, owner_index);
let pk = PublicKey::from(&sk);
let sender_psk = derive_pk(&seed, sender_index);
let sk = derive_stake_sk(&seed, owner_index);
let pk = StakePublicKey::from(&sk);

let rng = &mut utils::rng(rng_seed);

let withdraw_r = JubJubScalar::random(rng);
let address: StealthAddress = sender_psk.gen_stealth_address(&withdraw_r);
let nonce = BlsScalar::random(&mut *rng);
let withdraw_r = JubJubScalar::random(rng.clone());
let address = sender_psk.gen_stealth_address(&withdraw_r);
let nonce = BlsScalar::random(&mut rng.clone());

let msg = withdraw_signature_message(counter, address, nonce);
let signature = sk.sign(&pk, &msg);

// Since we're not transferring value *to* the contract the crossover
// shouldn't contain a value. As such the note used to created it should
// be valueless as well.
let blinder = JubJubScalar::random(rng);
let note = Note::obfuscated(rng, &refund, 0, blinder);
let blinder = JubJubScalar::random(rng.clone());
let note = Note::obfuscated(&mut rng.clone(), &refund, 0, blinder);
let (mut fee, crossover) = note
.try_into()
.expect("Obfuscated notes should always yield crossovers");
Expand Down
10 changes: 5 additions & 5 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use alloc::{vec, vec::Vec};
use core::mem;

use dusk_bytes::Serializable;
use phoenix_core::{Fee, Note};
use phoenix_core::{Fee, Note, ViewKey};
use sha2::{Digest, Sha512};

use crate::{key, tx, types, utils, MAX_KEY, MAX_LEN};
Expand Down Expand Up @@ -182,7 +182,7 @@ pub fn execute(args: i32, len: i32) -> i64 {
let mut full_inputs = Vec::with_capacity(inputs.len());

let view_key = key::derive_vk(&seed, sender_index);
let ssk = key::derive_ssk(&seed, sender_index);
let ssk = key::derive_sk(&seed, sender_index);

'outer: for input in inputs {
if let Ok(value) = input.value(Some(&view_key)) {
Expand Down Expand Up @@ -350,7 +350,7 @@ pub fn public_spend_keys(args: i32, len: i32) -> i64 {
};

let keys = (0..=MAX_KEY)
.map(|idx| key::derive_psk(&seed, idx as u64))
.map(|idx| key::derive_pk(&seed, idx as u64))
.map(|psk| bs58::encode(psk.to_bytes()).into_string())
.collect();

Expand Down Expand Up @@ -419,8 +419,8 @@ pub fn nullifiers(args: i32, len: i32) -> i64 {
// decrypt the note. if any fails, returns false
for idx in 0..=MAX_KEY {
if keys_len == idx {
keys_ssk[idx] = key::derive_ssk(&seed, idx as u64);
keys[idx] = keys_ssk[idx].view_key();
keys_ssk[idx] = key::derive_sk(&seed, idx as u64);
keys[idx] = ViewKey::from(keys_ssk[idx]);
keys_len += 1;
}

Expand Down
18 changes: 9 additions & 9 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@

use crate::{utils, RNG_SEED};

use dusk_bls12_381_sign::SecretKey;
use dusk_pki::{PublicSpendKey, SecretSpendKey, ViewKey};
use bls12_381_bls::SecretKey as StakeSecretKey;
use phoenix_core::{PublicKey, SecretKey, ViewKey};

/// Generates a secret spend key from its seed and index.
///
/// First the `seed` and then the little-endian representation of the key's
/// `index` are passed through SHA-256. A constant is then mixed in and the
/// resulting hash is then used to seed a `ChaCha12` CSPRNG, which is
/// subsequently used to generate the key.
pub fn derive_ssk(seed: &[u8; RNG_SEED], index: u64) -> SecretSpendKey {
SecretSpendKey::random(&mut utils::rng_with_index(seed, index, b"SSK"))
pub fn derive_sk(seed: &[u8; RNG_SEED], index: u64) -> SecretKey {
SecretKey::random(&mut utils::rng_with_index(seed, index, b"SSK"))
}

/// Generates a secret key from its seed and index.
Expand All @@ -27,22 +27,22 @@ pub fn derive_ssk(seed: &[u8; RNG_SEED], index: u64) -> SecretSpendKey {
/// `index` are passed through SHA-256. A constant is then mixed in and the
/// resulting hash is then used to seed a `ChaCha12` CSPRNG, which is
/// subsequently used to generate the key.
pub fn derive_sk(seed: &[u8; RNG_SEED], index: u64) -> SecretKey {
SecretKey::random(&mut utils::rng_with_index(seed, index, b"SK"))
pub fn derive_stake_sk(seed: &[u8; RNG_SEED], index: u64) -> StakeSecretKey {
StakeSecretKey::random(&mut utils::rng_with_index(seed, index, b"SK"))
}

/// Generates a public spend key from its seed and index.
///
/// The secret spend key is derived from [derive_ssk], and then the key is
/// generated via [SecretSpendKey::public_spend_key].
pub fn derive_psk(seed: &[u8; RNG_SEED], index: u64) -> PublicSpendKey {
derive_ssk(seed, index).public_spend_key()
pub fn derive_pk(seed: &[u8; RNG_SEED], index: u64) -> PublicKey {
PublicKey::from(derive_sk(seed, index))
}

/// Generates a view key from its seed and index.
///
/// The secret spend key is derived from [derive_ssk], and then the key is
/// generated via [SecretSpendKey::view_key].
pub fn derive_vk(seed: &[u8; RNG_SEED], index: u64) -> ViewKey {
derive_ssk(seed, index).view_key()
ViewKey::from(derive_sk(seed, index))
}
Loading

0 comments on commit a9573d4

Please sign in to comment.