diff --git a/Cargo.toml b/Cargo.toml index a4f1d55..d310388 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.21.6" edition = "2021" description = "The core functionality of the Dusk wallet" license = "MPL-2.0" -respository = "https://github.com/dusk-network/wallet-core" +repository = "https://github.com/dusk-network/wallet-core" [lib] crate-type = ["cdylib", "rlib"] @@ -15,21 +15,19 @@ bs58 = { version = "0.5", default-features = false, features = [ "alloc", "cb58", ] } -dusk-bls12_381-sign = { version = "0.5", default-features = false } +bls12_381-bls = { version = "0.2", 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 = [ +dusk-jubjub = { version = "0.14", default-features = false } +jubjub-schnorr = { version = "0.2", default-features = false, features = [ "rkyv-impl", "alloc", + "double", ] } -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"] } @@ -40,22 +38,22 @@ 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" [features] @@ -64,10 +62,10 @@ 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" diff --git a/assets/dusk_wallet_core.wasm b/assets/dusk_wallet_core.wasm index 23f63b8..c0c1655 100755 Binary files a/assets/dusk_wallet_core.wasm and b/assets/dusk_wallet_core.wasm differ diff --git a/assets/schema.json b/assets/schema.json index c72a138..3cedb8d 100644 --- a/assets/schema.json +++ b/assets/schema.json @@ -331,8 +331,8 @@ } } }, - "PublicSpendKeysArgs": { - "description": "The arguments of the public_spend_keys function", + "PublicKeysArgs": { + "description": "The arguments of the public_keys function", "type": "object", "required": [ "seed" @@ -351,15 +351,15 @@ } } }, - "PublicSpendKeysResponse": { - "description": "The response of the public_spend_keys function", + "PublicKeysResponse": { + "description": "The response of the public_keys function", "type": "object", "required": [ "keys" ], "properties": { "keys": { - "description": "The Base58 public spend keys of the wallet.", + "description": "The Base58 public keys of the wallet.", "type": "array", "items": { "type": "string" @@ -449,7 +449,7 @@ } }, "RkyvTreeLeafResponse": { - "description": "The response of the public_spend_keys function", + "description": "The response of the public_keys function", "type": "object", "required": [ "block_height", @@ -557,13 +557,13 @@ } } }, - "PublicSpendKeysAndNotesType": { + "PublicKeysAndNotesType": { "description": "Type of the response of the check_note_validity function", "type": "object", - "required": ["public_spend_key", "notes"], + "required": ["public_key", "notes"], "properties": { - "public_spend_key": { - "description": "The public spend key as a bs58 formated string", + "public_key": { + "description": "The public key as a bs58 formated string", "type": "string" }, "notes": { @@ -616,8 +616,8 @@ "format": "uint8" } }, - "public_spend_key": { - "description": "A base 58 encoded public spend key string", + "public_key": { + "description": "A base 58 encoded public key string", "type": "string" } } @@ -745,7 +745,7 @@ "UnspentSpentNotesArgs": { "description": "Arguents of the unspent_spent_notes function", "type": "object", - "required": ["notes", "nullifiers_of_notes", "block_heights", "existing_nullifiers", "psks"], + "required": ["notes", "nullifiers_of_notes", "block_heights", "existing_nullifiers", "pks"], "properties": { "notes": { "description": "The Array of rkyv serialized notes", @@ -785,7 +785,7 @@ "format": "uint8" } }, - "psks": { + "pks": { "description": "Array of bs58 encoded string to be sent with the response of the function", "type": "array", "items": { @@ -818,14 +818,14 @@ "NoteInfoType": { "description": "Information about the note", "type": "object", - "required": ["pos", "psk", "note", "nullifier", "block_height"], + "required": ["pos", "pk", "note", "nullifier", "block_height"], "properties": { "pos": { "description": "position of the note", "type": "integer" }, - "psk": { - "description": "public spend key belonging to that note", + "pk": { + "description": "public key belonging to that note", "type": "string" }, "note": { @@ -944,7 +944,7 @@ "GetStakeCallDataArgs": { "description": "Get the call data for stakeing", "type": "object", - "required": ["staker_index", "seed", "spend_proof", "value", "counter"], + "required": ["staker_index", "seed", "proof", "value", "counter"], "properties": { "staker_index": { "description": "Index of the address of the staker in the seed", @@ -959,7 +959,7 @@ "format": "uint8" } }, - "spend_proof": { + "proof": { "description": "The stct proof as recieved from the node", "type": "array", "items": { @@ -1076,8 +1076,8 @@ } } }, - "GetPublicKeyRkyvSerializedArgs": { - "description": "Args of the get_public_key_rkyv_serialized function", + "GetStakePKrkyvSerializedArgs": { + "description": "Args of the get_stake_pk_rkyv_serialized function", "type": "object", "required": ["seed", "index"], "properties": { @@ -1211,7 +1211,7 @@ "format": "uint64" }, "refund": { - "description": "psk in string of who to refund this tx to", + "description": "pk in string of who to refund this tx to", "type": "string" }, "owner_index": { diff --git a/src/compat/allow.rs b/src/compat/allow.rs index e3dbf17..b3cc3bf 100644 --- a/src/compat/allow.rs +++ b/src/compat/allow.rs @@ -8,11 +8,13 @@ use crate::{key::*, types, utils, MAX_LEN}; use alloc::string::String; -use dusk_bls12_381_sign::PublicKey; +use bls12_381_bls::PublicKey as StakePublicKey; use dusk_jubjub::JubJubScalar; -use phoenix_core::{Note, *}; - -use super::stake_contract_types::*; +use ff::Field; +use phoenix_core::{ + transaction::{allow_signature_message, Allow}, + Crossover, Fee, Note, PublicKey, +}; /// Get unstake call data #[no_mangle] @@ -41,23 +43,23 @@ pub fn get_allow_call_data(args: i32, len: i32) -> i64 { None => return utils::fail(), }; - let refund: dusk_pki::PublicSpendKey = match utils::bs58_to_psk(&refund) { + let refund: PublicKey = match utils::bs58_to_pk(&refund) { Some(a) => a, None => return utils::fail(), }; - let sk = derive_sk(&seed, owner_index); - let staker = PublicKey::from(&sk); + let stake_sk = derive_stake_sk(&seed, owner_index); + let staker_pk = StakePublicKey::from(&stake_sk); - let owner_sk = derive_sk(&seed, sender_index); - let owner_pk = PublicKey::from(&owner_sk); + let owner_sk = derive_stake_sk(&seed, sender_index); + let owner_pk = StakePublicKey::from(&owner_sk); let rng = &mut utils::rng(rng_seed); - let msg = allow_signature_message(counter, &staker); + let msg = allow_signature_message(counter, staker_pk); let signature = owner_sk.sign(&owner_pk, &msg); - let blinder = JubJubScalar::random(rng); + let blinder = JubJubScalar::random(&mut *rng); let note = Note::obfuscated(rng, &refund, 0, blinder); let (mut fee, crossover) = note .try_into() @@ -67,7 +69,7 @@ pub fn get_allow_call_data(args: i32, len: i32) -> i64 { fee.gas_price = gas_price; let allow = Allow { - public_key: staker, + public_key: staker_pk, owner: owner_pk, signature, }; diff --git a/src/compat/crypto.rs b/src/compat/crypto.rs index 87c70e3..7c2595e 100644 --- a/src/compat/crypto.rs +++ b/src/compat/crypto.rs @@ -6,7 +6,7 @@ use dusk_bls12_381::BlsScalar; use dusk_bytes::Serializable; -use phoenix_core::Note; +use phoenix_core::{Note, PublicKey, ViewKey}; use alloc::vec::Vec; @@ -41,26 +41,25 @@ pub fn check_note_ownership(args: i32, len: i32) -> i64 { let mut is_owned: bool = false; let mut nullifier_found = BlsScalar::default(); - let mut psk_found: Option = None; + let mut pk_found: Option = None; for idx in 0..=MAX_KEY { let idx = idx as u64; - let view_key = key::derive_vk(&seed, idx); + let sk = key::derive_sk(&seed, idx); + let vk = ViewKey::from(&sk); - if view_key.owns(¬e) { - let ssk = key::derive_ssk(&seed, idx); - let nullifier = note.gen_nullifier(&ssk); + if vk.owns(¬e) { + let nullifier = note.gen_nullifier(&sk); nullifier_found = nullifier; is_owned = true; - psk_found = Some(ssk.public_spend_key()); + pk_found = Some(PublicKey::from(&sk)); break; } } - let psk_found = - psk_found.map(|psk| bs58::encode(psk.to_bytes()).into_string()); + let pk_found = pk_found.map(|pk| bs58::encode(pk.to_bytes()).into_string()); let nullifier_found = match rkyv::to_bytes::(&nullifier_found).ok() { @@ -71,7 +70,7 @@ pub fn check_note_ownership(args: i32, len: i32) -> i64 { utils::into_ptr(types::CheckNoteOwnershipResponse { is_owned, nullifier: nullifier_found, - public_spend_key: psk_found, + public_key: pk_found, }) } @@ -84,7 +83,7 @@ pub fn unspent_spent_notes(args: i32, len: i32) -> i64 { nullifiers_of_notes, block_heights, existing_nullifiers, - psks, + pks, } = match utils::take_args(args, len) { Some(a) => a, None => return utils::fail(), @@ -99,10 +98,10 @@ pub fn unspent_spent_notes(args: i32, len: i32) -> i64 { let mut spent_notes = Vec::new(); let mut unspent_notes = Vec::new(); - for (index, ((note, nullifier), psk)) in notes + for (index, ((note, nullifier), pk)) in notes .into_iter() .zip(nullifiers_of_notes) - .zip(psks) + .zip(pks) .enumerate() { let parsed_note: Note = match rkyv::from_bytes::(¬e).ok() { @@ -124,7 +123,7 @@ pub fn unspent_spent_notes(args: i32, len: i32) -> i64 { if existing_nullifiers.contains(&parsed_nullifier) { spent_notes.push(types::NoteInfoType { pos: *parsed_note.pos(), - psk, + pk, block_height, note, nullifier, @@ -134,7 +133,7 @@ pub fn unspent_spent_notes(args: i32, len: i32) -> i64 { pos: *parsed_note.pos(), note, block_height, - psk, + pk, nullifier, }); } diff --git a/src/compat/mod.rs b/src/compat/mod.rs index f45b5d9..fea6eb1 100644 --- a/src/compat/mod.rs +++ b/src/compat/mod.rs @@ -21,11 +21,3 @@ pub mod tx; pub mod unstake; /// Includes functions to interact with the stake contract withdraw tx pub mod withdraw; - -mod stake_contract_types { - pub use stake_contract_types::{ - allow_signature_message, stake_signature_message, - unstake_signature_message, withdraw_signature_message, - }; - pub use stake_contract_types::{Allow, Stake, Unstake, Withdraw}; -} diff --git a/src/compat/rkyv.rs b/src/compat/rkyv.rs index e869fb4..4abdb72 100644 --- a/src/compat/rkyv.rs +++ b/src/compat/rkyv.rs @@ -11,8 +11,8 @@ use crate::{ utils, MAX_LEN, }; +use bls12_381_bls::PublicKey as StakePublicKey; use dusk_bls12_381::BlsScalar; -use dusk_bls12_381_sign::PublicKey; use phoenix_core::{transaction::TreeLeaf, Note}; use alloc::vec::Vec; @@ -163,11 +163,11 @@ pub fn rkyv_openings_array(args: i32, len: i32) -> i64 { utils::rkyv_into_ptr::>(openings_vec) } -/// Rkyv serialize public key to send to the node to obtain +/// Rkyv serialize stake public key to send to the node to obtain /// stake-info #[no_mangle] -fn get_public_key_rkyv_serialized(args: i32, len: i32) -> i64 { - let types::GetPublicKeyRkyvSerializedArgs { seed, index } = +fn get_stake_pk_rkyv_serialized(args: i32, len: i32) -> i64 { + let types::GetStakePKrkyvSerializedArgs { seed, index } = match utils::take_args(args, len) { Some(a) => a, None => return utils::fail(), @@ -178,9 +178,8 @@ fn get_public_key_rkyv_serialized(args: i32, len: i32) -> i64 { None => return utils::fail(), }; - let sk = key::derive_sk(&seed, index); + let stake_sk = key::derive_stake_sk(&seed, index); + let stake_pk = StakePublicKey::from(&stake_sk); - let pk = PublicKey::from(&sk); - - utils::rkyv_into_ptr::(pk) + utils::rkyv_into_ptr::(stake_pk) } diff --git a/src/compat/stake.rs b/src/compat/stake.rs index cdcfdc0..82e837f 100644 --- a/src/compat/stake.rs +++ b/src/compat/stake.rs @@ -15,20 +15,18 @@ 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_bytes::{Serializable, Write}; use dusk_jubjub::JubJubScalar; -use dusk_pki::{Ownable, SecretKey as SchnorrKey}; -use dusk_plonk::proof_system::Proof; -use dusk_schnorr::Signature; +use dusk_plonk::prelude::Proof; +use ff::Field; +use jubjub_schnorr::Signature; use phoenix_core::{ transaction::{stct_signature_message, StakeData}, - *, + Crossover, Fee, Note, Ownable, }; - -use super::stake_contract_types::*; +use stake_contract_types::{stake_signature_message, Stake}; const STCT_INPUT_SIZE: usize = Fee::SIZE + Crossover::SIZE @@ -64,15 +62,15 @@ pub fn get_stct_proof(args: i32, len: i32) -> i64 { None => return utils::fail(), }; - let sender = derive_ssk(&seed, sender_index); - let refund = match bs58_to_psk(&refund) { + let sender = derive_sk(&seed, sender_index); + let refund = match bs58_to_pk(&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() @@ -89,10 +87,8 @@ 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 stct_signature = Signature::new(&secret, rng, stct_message); + let note_sk = sender.sk_r(fee.stealth_address()); + let stct_signature = note_sk.sign(rng, stct_message); let vec_allocation = allocate(STCT_INPUT_SIZE as i32) as *mut _; let mut buf: Vec = unsafe { @@ -118,8 +114,7 @@ pub fn get_stct_proof(args: i32, len: i32) -> i64 { } .to_vec(); - let signature = match rkyv::to_bytes::(&stct_signature) - { + let stct_sig = match rkyv::to_bytes::(&stct_signature) { Ok(a) => a.to_vec(), Err(_) => return utils::fail(), }; @@ -141,7 +136,7 @@ pub fn get_stct_proof(args: i32, len: i32) -> i64 { utils::into_ptr(types::GetStctProofResponse { bytes, - signature, + signature: stct_sig, crossover, blinder, fee, @@ -154,7 +149,7 @@ pub fn get_stake_call_data(args: i32, len: i32) -> i64 { let types::GetStakeCallDataArgs { staker_index, seed, - spend_proof, + proof, value, counter, } = match utils::take_args(args, len) { @@ -162,12 +157,12 @@ pub fn get_stake_call_data(args: i32, len: i32) -> i64 { None => return utils::fail(), }; - let spend_proof: [u8; Proof::SIZE] = match spend_proof.try_into().ok() { + let proof: [u8; Proof::SIZE] = match proof.try_into().ok() { Some(a) => a, None => return utils::fail(), }; - let proof = match Proof::from_bytes(&spend_proof).ok() { + let proof = match Proof::from_bytes(&proof).ok() { Some(a) => a.to_bytes().to_vec(), None => return utils::fail(), }; @@ -177,15 +172,15 @@ 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 stake_sk = derive_stake_sk(&seed, staker_index); + let stake_pk = StakePublicKey::from(&stake_sk); let msg = stake_signature_message(counter, value); - let signature = sk.sign(&pk, &msg); + let stake_sig = stake_sk.sign(&stake_pk, &msg); let stake = Stake { - public_key: pk, - signature, + public_key: stake_pk, + signature: stake_sig, value, proof, }; diff --git a/src/compat/tx.rs b/src/compat/tx.rs index 78a587a..9f682d2 100644 --- a/src/compat/tx.rs +++ b/src/compat/tx.rs @@ -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}; @@ -429,7 +429,7 @@ fn write_optional_call( } fn input_to_var_bytes(input: &tx::Input) -> Vec { - let affine_pkr = JubJubAffine::from(&input.pk_r_prime); + let affine_pkr = JubJubAffine::from(&input.note_pk_prime); let opening_bytes = rkyv::to_bytes::<_, 256>(&input.opening) .expect("Rkyv serialization should always succeed for an opening") @@ -438,7 +438,7 @@ fn input_to_var_bytes(input: &tx::Input) -> Vec { let size = BlsScalar::SIZE + Note::SIZE + JubJubAffine::SIZE - + SchnorrSig::SIZE + + SignatureDouble::SIZE + u64::SIZE + JubJubScalar::SIZE + opening_bytes.len(); diff --git a/src/compat/unstake.rs b/src/compat/unstake.rs index eeb777d..a3cbc57 100644 --- a/src/compat/unstake.rs +++ b/src/compat/unstake.rs @@ -15,14 +15,13 @@ use crate::{ use alloc::string::String; use alloc::vec::Vec; -use dusk_bls12_381_sign::PublicKey; -use dusk_bytes::Serializable; -use dusk_bytes::Write; +use bls12_381_bls::PublicKey as StakePublicKey; +use dusk_bytes::{Serializable, Write}; use dusk_jubjub::{JubJubAffine, JubJubScalar}; -use dusk_plonk::proof_system::Proof; -use phoenix_core::*; - -use super::stake_contract_types::*; +use dusk_plonk::prelude::Proof; +use ff::Field; +use phoenix_core::{Crossover, Fee, Note, PublicKey}; +use stake_contract_types::{unstake_signature_message, Unstake}; const WFCT_INPUT_SIZE: usize = JubJubAffine::SIZE + u64::SIZE + JubJubScalar::SIZE; @@ -54,15 +53,15 @@ pub fn get_wfct_proof(args: i32, len: i32) -> i64 { None => return utils::fail(), }; - let sender = derive_ssk(&seed, sender_index); - let refund = match bs58_to_psk(&refund) { + let sender = derive_sk(&seed, sender_index); + let refund = match bs58_to_pk(&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, 0, blinder); let (mut fee, crossover) = note .try_into() @@ -71,9 +70,8 @@ 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_blinder: dusk_jubjub::Fr = unstake_note + let unstake_note = Note::transparent(rng, &PublicKey::from(&sender), value); + let unstake_blinder: JubJubScalar = unstake_note .blinding_factor(None) .expect("Note is transparent so blinding factor is unencrypted"); @@ -164,16 +162,16 @@ 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 stake_sk = derive_stake_sk(&seed, sender_index); + let stake_pk = StakePublicKey::from(&stake_sk); let unstake_note = unstake_note.to_bytes(); let signature_message = unstake_signature_message(counter, unstake_note); - let signature = sk.sign(&public_key, &signature_message); + let signature = stake_sk.sign(&stake_pk, &signature_message); let unstake = Unstake { - public_key, + public_key: stake_pk, signature, note: unstake_note.to_vec(), proof, diff --git a/src/compat/withdraw.rs b/src/compat/withdraw.rs index cd15d96..9026b27 100644 --- a/src/compat/withdraw.rs +++ b/src/compat/withdraw.rs @@ -7,14 +7,12 @@ 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::*; +use ff::Field; +use phoenix_core::{Crossover, Fee, Note, PublicKey, StealthAddress}; +use stake_contract_types::{withdraw_signature_message, Withdraw}; /// Get unstake call data #[no_mangle] @@ -44,28 +42,30 @@ pub fn get_withdraw_call_data(args: i32, len: i32) -> i64 { None => return utils::fail(), }; - let refund = match utils::bs58_to_psk(&refund) { + let refund = match utils::bs58_to_pk(&refund) { Some(a) => a, 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_sk = derive_sk(&seed, sender_index); + let sender_pk = PublicKey::from(&sender_sk); + + let stake_sk = derive_stake_sk(&seed, owner_index); + let stake_pk = StakePublicKey::from(&stake_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 withdraw_r = JubJubScalar::random(&mut *rng); + let address: StealthAddress = sender_pk.gen_stealth_address(&withdraw_r); let nonce = BlsScalar::random(&mut *rng); let msg = withdraw_signature_message(counter, address, nonce); - let signature = sk.sign(&pk, &msg); + let signature = stake_sk.sign(&stake_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 blinder = JubJubScalar::random(&mut *rng); let note = Note::obfuscated(rng, &refund, 0, blinder); let (mut fee, crossover) = note .try_into() @@ -75,7 +75,7 @@ pub fn get_withdraw_call_data(args: i32, len: i32) -> i64 { fee.gas_price = gas_price; let withdraw = Withdraw { - public_key: pk, + public_key: stake_pk, signature, address, nonce, diff --git a/src/ffi.rs b/src/ffi.rs index 3aa61c2..f22002e 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -13,7 +13,7 @@ use alloc::{ 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}; @@ -188,11 +188,11 @@ 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 sk = key::derive_sk(&seed, sender_index); + let vk = ViewKey::from(&sk); 'outer: for input in inputs { - if let Ok(value) = input.value(Some(&view_key)) { + if let Ok(value) = input.value(Some(&vk)) { let opening = match openings.iter().find(|(_, pos)| input.pos() == pos) { Some(a) => a.0, @@ -201,7 +201,7 @@ pub fn execute(args: i32, len: i32) -> i64 { } }; - let blinder = match input.blinding_factor(Some(&view_key)).ok() { + let blinder = match input.blinding_factor(Some(&vk)).ok() { Some(a) => a, None => return utils::fail(), }; @@ -225,7 +225,7 @@ pub fn execute(args: i32, len: i32) -> i64 { note, opening, value, - ssk: &ssk, + sk: &sk, }) .collect(); @@ -247,7 +247,7 @@ pub fn execute(args: i32, len: i32) -> i64 { let rng: &mut rand_chacha::ChaCha12Rng = &mut utils::rng(rng_seed); let actual_fee; - let refund = match utils::bs58_to_psk(&refund) { + let refund = match utils::bs58_to_pk(&refund) { Some(r) => r, None => return utils::fail(), }; @@ -336,17 +336,16 @@ pub fn filter_notes(args: i32, len: i32) -> i64 { utils::rkyv_into_ptr(notes) } -/// Returns a list of [PublicSpendKey] that belongs to this wallet. +/// Returns a list of [`PublicKey`]s that belongs to this wallet. /// /// Expects as argument a fat pointer to a JSON string representing -/// [types::PublicSpendKeysArgs]. +/// [types::PublicKeysArgs]. /// /// Will return a triplet (status, ptr, len) pointing to JSON string -/// representing [types::PublicSpendKeysResponse]. +/// representing [types::PublicKeysResponse]. #[no_mangle] -pub fn public_spend_keys(args: i32, len: i32) -> i64 { - let types::PublicSpendKeysArgs { seed } = match utils::take_args(args, len) - { +pub fn public_keys(args: i32, len: i32) -> i64 { + let types::PublicKeysArgs { seed } = match utils::take_args(args, len) { Some(a) => a, None => return utils::fail(), }; @@ -357,11 +356,11 @@ 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(|psk| bs58::encode(psk.to_bytes()).into_string()) + .map(|idx| key::derive_pk(&seed, idx as u64)) + .map(|pk| bs58::encode(pk.to_bytes()).into_string()) .collect(); - utils::into_ptr(types::PublicSpendKeysResponse { keys }) + utils::into_ptr(types::PublicKeysResponse { keys }) } /// Returns a list of [ViewKey] that belongs to this wallet. @@ -370,7 +369,7 @@ pub fn public_spend_keys(args: i32, len: i32) -> i64 { /// [types::ViewKeysArgs]. /// /// Will return a triplet (status, ptr, len) pointing to the rkyv serialized -/// [Vec]. +/// [`Vec`]. #[no_mangle] pub fn view_keys(args: i32, len: i32) -> i64 { let types::ViewKeysArgs { seed } = match utils::take_args(args, len) { @@ -383,11 +382,11 @@ pub fn view_keys(args: i32, len: i32) -> i64 { None => return utils::fail(), }; - let vks: Vec<_> = (0..=MAX_KEY) + let keys: Vec<_> = (0..=MAX_KEY) .map(|idx| key::derive_vk(&seed, idx as u64)) .collect(); - utils::rkyv_into_ptr(vks) + utils::rkyv_into_ptr(keys) } /// Returns a list of [BlsScalar] nullifiers for the given [Vec] combined @@ -417,22 +416,22 @@ pub fn nullifiers(args: i32, len: i32) -> i64 { }; let mut nullifiers = Vec::with_capacity(notes.len()); - let mut keys = unsafe { [mem::zeroed(); MAX_KEY + 1] }; - let mut keys_ssk = unsafe { [mem::zeroed(); MAX_KEY + 1] }; + let mut sks = unsafe { [mem::zeroed(); MAX_KEY + 1] }; + let mut vks = unsafe { [mem::zeroed(); MAX_KEY + 1] }; let mut keys_len = 0; 'outer: for note in notes { - // we iterate all the available keys until one can successfully + // we iterate all the available view key until one can successfully // 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(); + sks[idx] = key::derive_sk(&seed, idx as u64); + vks[idx] = ViewKey::from(&sks[idx]); keys_len += 1; } - if keys[idx].owns(¬e) { - nullifiers.push(note.gen_nullifier(&keys_ssk[idx])); + if vks[idx].owns(¬e) { + nullifiers.push(note.gen_nullifier(&sks[idx])); continue 'outer; } } diff --git a/src/key.rs b/src/key.rs index 01d4061..4295eb6 100644 --- a/src/key.rs +++ b/src/key.rs @@ -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. +/// Generates a stake secret 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_stake_sk(seed: &[u8; RNG_SEED], index: u64) -> StakeSecretKey { + StakeSecretKey::random(&mut utils::rng_with_index(seed, index, b"SK")) } /// Generates a secret key from its seed and index. @@ -28,21 +28,23 @@ pub fn derive_ssk(seed: &[u8; RNG_SEED], index: u64) -> SecretSpendKey { /// 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")) + SecretKey::random(&mut utils::rng_with_index(seed, index, b"SSK")) } -/// Generates a public spend key from its seed and index. +/// Generates a public 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() +/// First the secret key is derived with [`derive_sk`], then the public key +/// is generated from it and the secret key is erased from memory. +pub fn derive_pk(seed: &[u8; RNG_SEED], index: u64) -> PublicKey { + let sk = derive_sk(seed, index); + PublicKey::from(&sk) } /// 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]. +/// First the secret key is derived with [`derive_sk`], then the view key is +/// generated from it and the secret key is erased from memory. pub fn derive_vk(seed: &[u8; RNG_SEED], index: u64) -> ViewKey { - derive_ssk(seed, index).view_key() + let sk = derive_sk(seed, index); + ViewKey::from(&sk) } diff --git a/src/tx.rs b/src/tx.rs index 8bbe065..f296ad7 100644 --- a/src/tx.rs +++ b/src/tx.rs @@ -13,11 +13,11 @@ use core::mem; use bytecheck::CheckBytes; use dusk_bls12_381::BlsScalar; use dusk_jubjub::{JubJubExtended, JubJubScalar, GENERATOR_NUMS_EXTENDED}; -use dusk_pki::{Ownable, PublicSpendKey, SecretSpendKey}; -use dusk_schnorr::Proof as SchnorrSig; use ff::Field; +use jubjub_schnorr::SignatureDouble; use phoenix_core::{ - Crossover as PhoenixCrossover, Fee, Note, NoteType, Transaction, + Crossover as PhoenixCrossover, Fee, Note, NoteType, Ownable, PublicKey, + SecretKey, Transaction, ViewKey, }; use rand_core::{CryptoRng, RngCore}; use rkyv::{Archive, Deserialize, Serialize}; @@ -42,7 +42,7 @@ pub struct PreInput<'a> { /// Decrypted value of the input note. pub value: u64, /// Secret key to generate the nullifier of the input note. - pub ssk: &'a SecretSpendKey, + pub sk: &'a SecretKey, } /// An input to a transaction that is yet to be proven. @@ -60,9 +60,9 @@ pub struct Input { /// Blinding factor used to construct the note. pub blinder: JubJubScalar, /// Stealth address derived from the key of the owner of the note. - pub pk_r_prime: JubJubExtended, + pub note_pk_prime: JubJubExtended, /// Schnorr signature to prove the ownership of the note. - pub sig: SchnorrSig, + pub sig: SignatureDouble, } /// A preliminary output to a transaction that is yet to be proven. @@ -74,7 +74,7 @@ pub struct OutputValue { /// Value of the output. pub value: u64, /// Public key that will receive the note as spendable input. - pub receiver: PublicSpendKey, + pub receiver: PublicKey, /// Nonce/reference to be attached to the note. pub ref_id: u64, } @@ -155,7 +155,7 @@ impl UnprovenTransaction { let (nullifiers, inputs): (Vec<_>, Vec<_>) = inputs .into_iter() .map(|i| { - let nullifier = i.note.gen_nullifier(i.ssk); + let nullifier = i.note.gen_nullifier(i.sk); (nullifier, i) }) .unzip(); @@ -177,10 +177,10 @@ impl UnprovenTransaction { types::OutputType::Obfuscated => NoteType::Obfuscated, }; - let r = JubJubScalar::random(rng); - let blinder = JubJubScalar::random(rng); + let r = JubJubScalar::random(&mut *rng); + let blinder = JubJubScalar::random(&mut *rng); let nonce = BlsScalar::random(&mut *rng); - let receiver = utils::bs58_to_psk(&receiver)?; + let receiver = utils::bs58_to_pk(&receiver)?; let note = Note::deterministic( r#type, &r, nonce, &receiver, value, blinder, ); @@ -258,17 +258,18 @@ impl UnprovenTransaction { note, opening, value, - ssk, + sk, }, nullifier, )| { - let vk = ssk.view_key(); - let sk_r = ssk.sk_r(note.stealth_address()); + let vk = ViewKey::from(sk); + let note_sk = sk.sk_r(note.stealth_address()); let blinder = note.blinding_factor(Some(&vk)).map_err(|_| ())?; - let pk_r_prime = GENERATOR_NUMS_EXTENDED * sk_r.as_ref(); - let sig = SchnorrSig::new(&sk_r, rng, tx_hash); + let note_pk_prime = + GENERATOR_NUMS_EXTENDED * note_sk.as_ref(); + let sig = note_sk.sign_double(rng, tx_hash); Ok(Input { nullifier, @@ -276,7 +277,7 @@ impl UnprovenTransaction { note, value, blinder, - pk_r_prime, + note_pk_prime, sig, }) }, diff --git a/src/types.rs b/src/types.rs index 07148e9..efff049 100644 --- a/src/types.rs +++ b/src/types.rs @@ -46,9 +46,9 @@ pub struct CheckNoteOwnershipResponse { pub is_owned: bool, #[doc = " Nullifier of the note that we were checking the ownership of"] pub nullifier: Vec, - #[doc = " A base 58 encoded public spend key string"] + #[doc = " A base 58 encoded public key string"] #[serde(skip_serializing_if = "Option::is_none")] - pub public_spend_key: Option, + pub public_key: Option, } #[doc = " The value of the Crossover and the blinder"] #[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] @@ -164,7 +164,7 @@ pub struct GetAllowCallDataArgs { pub gas_price: u64, #[doc = " index of the owner of the stake"] pub owner_index: u64, - #[doc = " psk in string of who to refund this tx to"] + #[doc = " pk in string of who to refund this tx to"] pub refund: String, #[doc = " random rng seed"] pub rng_seed: Vec, @@ -221,23 +221,15 @@ pub struct GetMnemonicSeedResponse { #[doc = " Seed bytes from the given passphrase and Mnemonic"] pub mnemonic_seed: Vec, } -#[doc = " Args of the get_public_key_rkyv_serialized function"] -#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] -pub struct GetPublicKeyRkyvSerializedArgs { - #[doc = " The index of the public key to get"] - pub index: u64, - #[doc = " The seed to generate the sender keys from"] - pub seed: Vec, -} #[doc = " Get the call data for stakeing"] #[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] pub struct GetStakeCallDataArgs { #[doc = " The stake counter value"] pub counter: u64, + #[doc = " The stct proof as recieved from the node"] + pub proof: Vec, #[doc = " The seed to generate the sender keys from"] pub seed: Vec, - #[doc = " The stct proof as recieved from the node"] - pub spend_proof: Vec, #[doc = " Index of the address of the staker in the seed"] pub staker_index: u64, #[doc = " The amount of value to stake"] @@ -279,6 +271,14 @@ pub struct GetStakeInfoRespose { #[serde(skip_serializing_if = "Option::is_none")] pub reward: Option, } +#[doc = " Args of the get_stake_pk_rkyv_serialized function"] +#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] +pub struct GetStakePKrkyvSerializedArgs { + #[doc = " The index of the public key to get"] + pub index: u64, + #[doc = " The seed to generate the sender keys from"] + pub seed: Vec, +} #[doc = " Get the bytes for the stct proof to send to the node"] #[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] pub struct GetStctProofArgs { @@ -366,10 +366,10 @@ pub struct NoteInfoType { pub note: Vec, #[doc = " Nullifier of a Singular Note rkyv serialized"] pub nullifier: Vec, + #[doc = " public key belonging to that note"] + pub pk: String, #[doc = " position of the note"] pub pos: u64, - #[doc = " public spend key belonging to that note"] - pub psk: String, } #[doc = " The arguments of the nullifiers function"] #[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] @@ -411,22 +411,22 @@ pub struct ProveTxResponse { } #[doc = " Type of the response of the check_note_validity function"] #[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] -pub struct PublicSpendKeysAndNotesType { +pub struct PublicKeysAndNotesType { #[doc = " Array of notes which are rkyv serialized"] pub notes: Vec, - #[doc = " The public spend key as a bs58 formated string"] - pub public_spend_key: String, + #[doc = " The public key as a bs58 formated string"] + pub public_key: String, } -#[doc = " The arguments of the public_spend_keys function"] +#[doc = " The arguments of the public_keys function"] #[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] -pub struct PublicSpendKeysArgs { +pub struct PublicKeysArgs { #[doc = " Seed used to derive the keys of the wallet"] pub seed: Vec, } -#[doc = " The response of the public_spend_keys function"] +#[doc = " The response of the public_keys function"] #[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] -pub struct PublicSpendKeysResponse { - #[doc = " The Base58 public spend keys of the wallet."] +pub struct PublicKeysResponse { + #[doc = " The Base58 public keys of the wallet."] pub keys: Vec, } #[doc = " Arguments of the rkyv_bls_scalar_array function"] @@ -453,7 +453,7 @@ pub struct RkyvTreeLeaf { #[doc = " Bytes that are rkyv serialized into a phoenix_core::transaction::TreeLeaf"] pub bytes: Vec, } -#[doc = " The response of the public_spend_keys function"] +#[doc = " The response of the public_keys function"] #[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] pub struct RkyvTreeLeafResponse { #[doc = " The block height of the note."] @@ -540,7 +540,7 @@ pub struct UnspentSpentNotesArgs { #[doc = " notes"] pub nullifiers_of_notes: Vec>, #[doc = " Array of bs58 encoded string to be sent with the response of the function"] - pub psks: Vec, + pub pks: Vec, } #[doc = " The arguments of the view_keys function"] #[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] diff --git a/src/utils.rs b/src/utils.rs index c2c313e..bbccded 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -13,8 +13,7 @@ use core::ptr; use dusk_bytes::DeserializableSlice; use dusk_jubjub::JubJubScalar; -use dusk_pki::PublicSpendKey; -use phoenix_core::Note; +use phoenix_core::{Note, PublicKey}; use rand_chacha::ChaCha12Rng; use rand_core::SeedableRng; use serde::{Deserialize, Serialize}; @@ -155,11 +154,11 @@ pub fn sanitize_notes(mut notes: Vec) -> Vec { notes } -/// Converts a Base58 string into a [PublicSpendKey]. -pub fn bs58_to_psk(psk: &str) -> Option { - // TODO this should be defined in dusk-pki - let bytes = bs58::decode(psk).into_vec().ok()?; - PublicSpendKey::from_reader(&mut &bytes[..]).ok() +/// Converts a Base58 string into a [`PublicKey`]. +pub fn bs58_to_pk(pk: &str) -> Option { + // TODO this should be defined in phoenix-core + let bytes = bs58::decode(pk).into_vec().ok()?; + PublicKey::from_reader(&mut &bytes[..]).ok() } /// Calculate the inputs for a transaction. @@ -271,7 +270,8 @@ fn compose_works() { fn knapsack_works() { use core::mem; use dusk_jubjub::JubJubScalar; - use dusk_pki::SecretSpendKey; + use ff::Field; + use phoenix_core::{PublicKey, SecretKey}; use rand::{rngs::StdRng, SeedableRng}; // openings are not checked here; no point in setting them up properly @@ -282,31 +282,39 @@ fn knapsack_works() { assert_eq!(inputs(vec![], 70), None); // basic check - let key = SecretSpendKey::random(rng); - let blinder = JubJubScalar::random(rng); - let note = Note::obfuscated(rng, &key.public_spend_key(), 100, blinder); + let sk = SecretKey::random(rng); + let pk = PublicKey::from(&sk); + let blinder = JubJubScalar::random(&mut *rng); + let note = Note::obfuscated(rng, &pk, 100, blinder); let available = vec![(note, o, 100, blinder)]; let inputs_notes = available.clone(); assert_eq!(inputs(available, 70), Some(inputs_notes)); // out of balance basic check - let key = SecretSpendKey::random(rng); - let blinder = JubJubScalar::random(rng); - let note = Note::obfuscated(rng, &key.public_spend_key(), 100, blinder); + let sk = SecretKey::random(rng); + let pk = PublicKey::from(&sk); + let blinder = JubJubScalar::random(&mut *rng); + let note = Note::obfuscated(rng, &pk, 100, blinder); let available = vec![(note, o, 100, blinder)]; assert_eq!(inputs(available, 101), None); // multiple inputs check // note: this test is checking a naive, simple order-based output - let key = SecretSpendKey::random(rng); - let blinder1 = JubJubScalar::random(rng); - let note1 = Note::obfuscated(rng, &key.public_spend_key(), 100, blinder); - let key = SecretSpendKey::random(rng); - let blinder2 = JubJubScalar::random(rng); - let note2 = Note::obfuscated(rng, &key.public_spend_key(), 500, blinder); - let key = SecretSpendKey::random(rng); - let blinder3 = JubJubScalar::random(rng); - let note3 = Note::obfuscated(rng, &key.public_spend_key(), 300, blinder); + let sk = SecretKey::random(rng); + let pk = PublicKey::from(&sk); + let blinder1 = JubJubScalar::random(&mut *rng); + // shouldn't this note be created with blinder1? + let note1 = Note::obfuscated(rng, &pk, 100, blinder); + let sk = SecretKey::random(rng); + let pk = PublicKey::from(&sk); + let blinder2 = JubJubScalar::random(&mut *rng); + // shouldn't this note be created with blinder2? + let note2 = Note::obfuscated(rng, &pk, 500, blinder); + let sk = SecretKey::random(rng); + let pk = PublicKey::from(&sk); + let blinder3 = JubJubScalar::random(&mut *rng); + // shouldn't this note be created with blinder3? + let note3 = Note::obfuscated(rng, &pk, 300, blinder); let available = vec![ (note1, o, 100, blinder1), (note2, o, 500, blinder2), @@ -316,15 +324,21 @@ fn knapsack_works() { assert_eq!(inputs(available.clone(), 600), Some(available)); // multiple inputs, out of balance check - let key = SecretSpendKey::random(rng); - let blinder1 = JubJubScalar::random(rng); - let note1 = Note::obfuscated(rng, &key.public_spend_key(), 100, blinder); - let key = SecretSpendKey::random(rng); - let blinder2 = JubJubScalar::random(rng); - let note2 = Note::obfuscated(rng, &key.public_spend_key(), 500, blinder); - let key = SecretSpendKey::random(rng); - let blinder3 = JubJubScalar::random(rng); - let note3 = Note::obfuscated(rng, &key.public_spend_key(), 300, blinder); + let sk = SecretKey::random(rng); + let pk = PublicKey::from(&sk); + let blinder1 = JubJubScalar::random(&mut *rng); + // shouldn't this note be created with blinder1? + let note1 = Note::obfuscated(rng, &pk, 100, blinder); + let sk = SecretKey::random(rng); + let pk = PublicKey::from(&sk); + let blinder2 = JubJubScalar::random(&mut *rng); + // shouldn't this note be created with blinder2? + let note2 = Note::obfuscated(rng, &pk, 500, blinder); + let sk = SecretKey::random(rng); + let pk = PublicKey::from(&sk); + let blinder3 = JubJubScalar::random(&mut *rng); + // shouldn't this note be created with blinder3? + let note3 = Note::obfuscated(rng, &pk, 300, blinder); let available = vec![ (note1, o, 100, blinder1), (note2, o, 500, blinder2), diff --git a/tests/keys_derive.rs b/tests/keys_derive.rs new file mode 100644 index 0000000..5b64b9b --- /dev/null +++ b/tests/keys_derive.rs @@ -0,0 +1,36 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +// +// Copyright (c) DUSK NETWORK. All rights reserved. + +use dusk_bytes::Serializable; +use dusk_wallet_core::key::{derive_sk, derive_stake_sk}; + +const SEED: [u8; 64] = [0; 64]; +const INDEX: u64 = 42; + +#[test] +fn test_derive_sk() { + // it is important that derive_sk always derives the same key from a seed + let sk_bytes = [ + 12, 16, 72, 188, 33, 76, 44, 178, 86, 123, 107, 153, 230, 149, 238, + 131, 87, 30, 94, 88, 52, 129, 247, 167, 30, 167, 163, 246, 68, 254, 14, + 9, 218, 135, 245, 104, 11, 190, 143, 129, 83, 202, 64, 179, 157, 248, + 175, 120, 157, 220, 98, 211, 141, 50, 224, 8, 1, 125, 29, 180, 206, + 195, 34, 0, + ]; + assert_eq!(derive_sk(&SEED, INDEX).to_bytes(), sk_bytes); +} + +#[test] +fn test_derive_stake_sk() { + // it is important that derive_stake_sk always derives the same key from a + // seed + let sk_bytes = [ + 95, 35, 167, 191, 106, 171, 71, 158, 159, 39, 84, 1, 132, 238, 152, + 235, 154, 5, 250, 158, 255, 195, 79, 95, 193, 58, 36, 189, 0, 99, 230, + 86, + ]; + assert_eq!(derive_stake_sk(&SEED, INDEX).to_bytes(), sk_bytes); +} diff --git a/tests/wallet.rs b/tests/wallet.rs index f785782..829f5b4 100644 --- a/tests/wallet.rs +++ b/tests/wallet.rs @@ -8,14 +8,12 @@ use dusk_bytes::Serializable; use dusk_jubjub::JubJubScalar; -use dusk_pki::PublicSpendKey; use dusk_wallet_core::{ tx, types::{self, CrossoverType as WasmCrossover}, utils, MAX_KEY, MAX_LEN, RNG_SEED, }; -use phoenix_core::Crossover; - +use phoenix_core::{Crossover, PublicKey, ViewKey}; use rusk_abi::ContractId; use serde::{Deserialize, Serialize}; use serde_json::json; @@ -60,15 +58,15 @@ fn execute_works() { let mut wallet = Wallet::default(); - let types::PublicSpendKeysResponse { keys } = wallet + let types::PublicKeysResponse { keys } = wallet .call( - "public_spend_keys", + "public_keys", json!({ "seed": seed.to_vec(), }), ) .take_contents(); - let psk = &keys[0]; + let pk = &keys[0]; let mut contract: ContractId = ContractId::uninitialized(); contract.as_bytes_mut().iter_mut().for_each(|b| *b = 0xfa); @@ -106,7 +104,7 @@ fn execute_works() { "ref_id": 15, "value": 10, }, - "refund": psk, + "refund": pk, "rng_seed": rng_seed.to_vec(), "seed": seed.to_vec() }); @@ -181,14 +179,14 @@ fn filter_notes_works() { } #[test] -fn public_spend_keys_works() { +fn public_keys_works() { let seed = [0xfa; RNG_SEED]; let mut wallet = Wallet::default(); - let types::PublicSpendKeysResponse { keys } = wallet + let types::PublicKeysResponse { keys } = wallet .call( - "public_spend_keys", + "public_keys", json!({ "seed": seed.to_vec(), }), @@ -197,9 +195,9 @@ fn public_spend_keys_works() { for key in &keys { let key = bs58::decode(key).into_vec().unwrap(); - let mut key_array = [0u8; PublicSpendKey::SIZE]; + let mut key_array = [0u8; PublicKey::SIZE]; key_array.copy_from_slice(&key); - PublicSpendKey::from_bytes(&key_array).unwrap(); + PublicKey::from_bytes(&key_array).unwrap(); } assert_eq!(keys.len(), MAX_KEY + 1); @@ -220,7 +218,7 @@ fn view_keys_works() { ) .take_memory(); - rkyv::from_bytes::>(&vk).unwrap(); + rkyv::from_bytes::>(&vk).unwrap(); } #[test] @@ -258,7 +256,8 @@ mod node { use dusk_jubjub::{BlsScalar, JubJubScalar}; use dusk_wallet_core::{key, tx, MAX_KEY, MAX_LEN, RNG_SEED}; - use phoenix_core::Note; + use ff::Field; + use phoenix_core::{Note, PublicKey}; use rand::{rngs::StdRng, RngCore}; use rand_core::SeedableRng; @@ -271,13 +270,13 @@ mod node { .into_iter() .map(|value| { let obfuscated = (rng.next_u32() & 1) == 1; - let psk = key::derive_ssk(seed, 0).public_spend_key(); + let pk = key::derive_pk(seed, 0); if obfuscated { - let blinder = JubJubScalar::random(rng); - Note::obfuscated(rng, &psk, value, blinder) + let blinder = JubJubScalar::random(&mut *rng); + Note::obfuscated(rng, &pk, value, blinder) } else { - Note::transparent(rng, &psk, value) + Note::transparent(rng, &pk, value) } }) .collect() @@ -338,17 +337,17 @@ mod node { .map(|value| { let obfuscated = (rng.next_u32() & 1) == 1; let idx = rng.next_u64() % MAX_KEY as u64; - let ssk = key::derive_ssk(seed, idx); - let psk = ssk.public_spend_key(); + let sk = key::derive_sk(seed, idx); + let pk = PublicKey::from(&sk); let note = if obfuscated { - let blinder = JubJubScalar::random(rng); - Note::obfuscated(rng, &psk, value, blinder) + let blinder = JubJubScalar::random(&mut *rng); + Note::obfuscated(rng, &pk, value, blinder) } else { - Note::transparent(rng, &psk, value) + Note::transparent(rng, &pk, value) }; - let nullifier = note.gen_nullifier(&ssk); + let nullifier = note.gen_nullifier(&sk); (note, nullifier) }) .collect()