-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wallet-core: Add filter_map function
Resolves #2129
- Loading branch information
Showing
4 changed files
with
177 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// 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 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::{ | ||
filter_map, keys::derive_multiple_phoenix_sk, phoenix_balance, BalanceInfo, | ||
}; | ||
|
||
#[test] | ||
fn test_filter_map() { | ||
// Assuming this set of notes where the number used as suffix is the | ||
// "owner": | ||
// notes := [A1, B1, C2, D2, E1, F3] | ||
|
||
let mut rng = StdRng::seed_from_u64(0xdab); | ||
const SEED_1: [u8; 64] = [1; 64]; | ||
const SEED_2: [u8; 64] = [2; 64]; | ||
|
||
let owner_1_sks = derive_multiple_phoenix_sk(&SEED_1, 0..3); | ||
let owner_1_pks = [ | ||
PhoenixPublicKey::from(&owner_1_sks[0]), | ||
PhoenixPublicKey::from(&owner_1_sks[1]), | ||
PhoenixPublicKey::from(&owner_1_sks[2]), | ||
]; | ||
let owner_2_sks = derive_multiple_phoenix_sk(&SEED_2, 0..2); | ||
let owner_2_pks = [ | ||
PhoenixPublicKey::from(&owner_2_sks[0]), | ||
PhoenixPublicKey::from(&owner_2_sks[1]), | ||
]; | ||
let owner_3_pk = | ||
PhoenixPublicKey::from(&PhoenixSecretKey::random(&mut rng)); | ||
|
||
let value = 42; | ||
let notes = vec![ | ||
gen_note(&mut rng, true, &owner_1_pks[0], value), // owner 1 | ||
gen_note(&mut rng, true, &owner_1_pks[1], value), // owner 1 | ||
gen_note(&mut rng, true, &owner_2_pks[0], value), // owner 2 | ||
gen_note(&mut rng, true, &owner_2_pks[1], value), // owner 2 | ||
gen_note(&mut rng, true, &owner_1_pks[2], value), // owner 1 | ||
gen_note(&mut rng, true, &owner_3_pk, value), // owner 3 | ||
]; | ||
|
||
// notes with idx 0, 1 and 4 are owned by owner_1 | ||
let notes_by_1 = filter_map(&owner_1_sks, ¬es); | ||
assert_eq!(notes_by_1.len(), 3); | ||
let note = ¬es[0]; | ||
let nullifier = note.gen_nullifier(&owner_1_sks[0]); | ||
assert_eq!(¬es_by_1[&nullifier], note); | ||
let note = ¬es[1]; | ||
let nullifier = note.gen_nullifier(&owner_1_sks[1]); | ||
assert_eq!(¬es_by_1[&nullifier], note); | ||
let note = ¬es[4]; | ||
let nullifier = note.gen_nullifier(&owner_1_sks[2]); | ||
assert_eq!(¬es_by_1[&nullifier], note); | ||
|
||
// notes with idx 2 and 3 are owned by owner_2 | ||
let notes_by_2 = filter_map(&owner_2_sks, ¬es); | ||
assert_eq!(notes_by_2.len(), 2); | ||
let note = ¬es[2]; | ||
let nullifier = note.gen_nullifier(&owner_2_sks[0]); | ||
assert_eq!(¬es_by_2[&nullifier], note); | ||
let note = ¬es[3]; | ||
let nullifier = note.gen_nullifier(&owner_2_sks[1]); | ||
assert_eq!(¬es_by_2[&nullifier], note); | ||
} | ||
|
||
#[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 mut notes = Vec::new(); | ||
|
||
// create the notes | ||
for value in 0..=10 { | ||
// we want to test with a mix of transparent and obfuscated notes so we | ||
// make every 10th note transparent | ||
let obfuscated_note = if value % 10 == 0 { false } else { true }; | ||
|
||
notes.push(gen_note(&mut rng, obfuscated_note, &owner_pk, value)); | ||
|
||
// also push some notes that are not owned | ||
if value % 4 == 0 { | ||
let not_owner_pk = | ||
PhoenixPublicKey::from(&PhoenixSecretKey::random(&mut rng)); | ||
notes.push(gen_note( | ||
&mut rng, | ||
obfuscated_note, | ||
¬_owner_pk, | ||
value, | ||
)); | ||
} | ||
} | ||
|
||
// the sum of these notes should be 5 * 11 = 55 | ||
// and the spendable notes are 7 + 8 + 9 + 10 = 34 | ||
let expected_balance = BalanceInfo { | ||
value: 55, | ||
spendable: 34, | ||
}; | ||
|
||
assert_eq!( | ||
phoenix_balance(&(&owner_sk).into(), notes), | ||
expected_balance | ||
); | ||
} | ||
|
||
fn gen_note( | ||
rng: &mut StdRng, | ||
obfuscated_note: bool, | ||
owner_pk: &PhoenixPublicKey, | ||
value: u64, | ||
) -> Note { | ||
let sender_pk = PhoenixPublicKey::from(&PhoenixSecretKey::random(rng)); | ||
|
||
let value_blinder = JubJubScalar::random(&mut *rng); | ||
let sender_blinder = [ | ||
JubJubScalar::random(&mut *rng), | ||
JubJubScalar::random(&mut *rng), | ||
]; | ||
if obfuscated_note { | ||
Note::obfuscated( | ||
rng, | ||
&sender_pk, | ||
&owner_pk, | ||
value, | ||
value_blinder, | ||
sender_blinder, | ||
) | ||
} else { | ||
Note::transparent(rng, &sender_pk, &owner_pk, value, sender_blinder) | ||
} | ||
} |