Skip to content

Commit

Permalink
wallet-core: Add function to sum the balance for phoenix notes
Browse files Browse the repository at this point in the history
Resolves #2117
  • Loading branch information
moCello committed Aug 13, 2024
1 parent 5bfca53 commit 7b50103
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
2 changes: 2 additions & 0 deletions wallet-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ rand_chacha = { version = "0.3", default-features = false }
sha2 = { version = "0.10", default-features = false }

[dev-dependencies]
rand = "0.8"
ff = { version = "0.13", default-features = false }

[features]
2 changes: 1 addition & 1 deletion wallet-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(clippy::pedantic)]

pub mod keys;
pub mod profile;

/// Length of the seed of the generated rng.
pub const RNG_SEED: usize = 64;
15 changes: 14 additions & 1 deletion wallet-core/src/keys.rs → wallet-core/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,27 @@ use sha2::{Digest, Sha256};
use execution_core::{
signatures::bls::SecretKey as BlsSecretKey,
transfer::phoenix::{
PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey,
Note, PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey,
ViewKey as PhoenixViewKey,
},
};
use zeroize::Zeroize;

use crate::RNG_SEED;

/// Calculate the sum for all the given [`Note`]s that belong to the given
/// [`PhoenixViewKey`].
///
/// This function assumes that all notes are owned by the given view-key.
pub fn phoenix_balance(
pvk: &PhoenixViewKey,
notes: impl IntoIterator<Item = Note>,
) -> u64 {
notes.into_iter().fold(0, |acc, note| {
acc + note.value(Some(pvk)).unwrap_or_default()
})
}

/// Generates a [`BlsSecretKey`] from a seed and index.
///
/// The randomness is generated using [`rng_with_index`].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,73 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use dusk_bytes::Serializable;
use wallet_core::keys::{
use ff::Field;
use rand::rngs::StdRng;
use rand::SeedableRng;

use execution_core::{
transfer::phoenix::{
Note, PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey,
},
JubJubScalar,
};

use wallet_core::profile::{
derive_bls_sk, derive_phoenix_pk, derive_phoenix_sk, derive_phoenix_vk,
phoenix_balance,
};

const SEED: [u8; 64] = [0; 64];
const INDEX: u8 = 42;

#[test]
fn test_balance() {
let mut rng = StdRng::seed_from_u64(0xdab);

let owner_sk = PhoenixSecretKey::random(&mut rng);
let owner_pk = PhoenixPublicKey::from(&owner_sk);
let sender_pk = PhoenixPublicKey::from(&PhoenixSecretKey::random(&mut rng));

let mut notes = Vec::new();

// the sum of these notes should be 5 * 11 = 55
let expected_balance = 55;
for value in 0..=10 {
let value_blinder = JubJubScalar::random(&mut rng);
let sender_blinder = [
JubJubScalar::random(&mut rng),
JubJubScalar::random(&mut rng),
];

// we want to test with a mix of transparent and obfuscated notes so we
// make every 10th note transparent
let note = if value % 10 == 0 {
Note::transparent(
&mut rng,
&sender_pk,
&owner_pk,
value,
sender_blinder,
)
} else {
Note::obfuscated(
&mut rng,
&sender_pk,
&owner_pk,
value,
value_blinder,
sender_blinder,
)
};
notes.push(note);
}

assert_eq!(
phoenix_balance(&(&owner_sk).into(), notes),
expected_balance
);
}

#[test]
fn test_derive_phoenix_sk() {
// it is important that we always derive the same key from a fixed seed
Expand Down

0 comments on commit 7b50103

Please sign in to comment.