From 63fba18b6503e0aae95da9ef8d651a1b7a20ad51 Mon Sep 17 00:00:00 2001 From: moana Date: Fri, 31 May 2024 16:49:40 +0200 Subject: [PATCH] circuits: Remove `ViewKey` from `TxOutputNote::new` Resolves #191 --- circuits/CHANGELOG.md | 5 +++++ circuits/src/transaction.rs | 42 +++++++++++++++++++---------------- circuits/tests/transaction.rs | 35 +++++++++++++---------------- 3 files changed, 44 insertions(+), 38 deletions(-) diff --git a/circuits/CHANGELOG.md b/circuits/CHANGELOG.md index 67a8b59..87a0464 100644 --- a/circuits/CHANGELOG.md +++ b/circuits/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- Remove `ViewKey` from `TxOutputNote::new()` parameters [#191] + ## [0.1.0] - 2024-05-22 ### Added @@ -23,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Update `poseidon-merkle` to v0.6 [#179] +[#191]: https://github.com/dusk-network/phoenix/issues/191 [#179]: https://github.com/dusk-network/phoenix/issues/179 [#177]: https://github.com/dusk-network/phoenix/issues/177 [#171]: https://github.com/dusk-network/phoenix/issues/171 diff --git a/circuits/src/transaction.rs b/circuits/src/transaction.rs index 379c6fb..ae69b4c 100644 --- a/circuits/src/transaction.rs +++ b/circuits/src/transaction.rs @@ -5,7 +5,8 @@ // Copyright (c) DUSK NETWORK. All rights reserved. use dusk_jubjub::{ - JubJubScalar, GENERATOR, GENERATOR_NUMS, GENERATOR_NUMS_EXTENDED, + JubJubAffine, JubJubScalar, GENERATOR, GENERATOR_EXTENDED, GENERATOR_NUMS, + GENERATOR_NUMS_EXTENDED, }; use dusk_plonk::prelude::*; use dusk_poseidon::{Domain, Hash, HashGadget}; @@ -124,9 +125,9 @@ impl TxInputNote { /// suitable for being introduced in the transfer circuit #[derive(Debug, Clone)] pub struct TxOutputNote { - pub(crate) value: u64, - pub(crate) value_commitment: JubJubAffine, - pub(crate) blinding_factor: JubJubScalar, + value: u64, + value_commitment: JubJubAffine, + blinding_factor: JubJubScalar, } #[derive(Debug, Clone)] @@ -137,16 +138,18 @@ struct WitnessTxOutputNote { } impl TxOutputNote { - /// Create a tx output note - pub fn new( - note: &Note, - vk: &ViewKey, - ) -> Result { - Ok(crate::transaction::TxOutputNote { - value: note.value(Some(vk))?, - value_commitment: note.value_commitment().into(), - blinding_factor: note.blinding_factor(Some(vk))?, - }) + /// Crate a new `TxOutputNote`. + pub fn new(value: u64, blinding_factor: JubJubScalar) -> Self { + let value_commitment = JubJubAffine::from( + (GENERATOR_EXTENDED * JubJubScalar::from(value)) + + (GENERATOR_NUMS_EXTENDED * blinding_factor), + ); + + Self { + value, + value_commitment, + blinding_factor, + } } fn append_to_circuit( @@ -338,7 +341,6 @@ impl Default for TxCircuit { let mut rng = StdRng::seed_from_u64(0xbeef); let sk = SecretKey::random(&mut rng); - let vk = ViewKey::from(&sk); let mut tree = Tree::<(), H>::new(); let skeleton_hash = BlsScalar::default(); @@ -365,10 +367,12 @@ impl Default for TxCircuit { tx_input_notes.push(tx_input_note); } - let tx_output_note_1 = - TxOutputNote::new(¬e, &vk).expect("Note created properly."); - let tx_output_note_2 = - TxOutputNote::new(¬e, &vk).expect("Note created properly."); + let tx_output_note_1 = TxOutputNote { + value: 0, + value_commitment: JubJubAffine::default(), + blinding_factor: JubJubScalar::default(), + }; + let tx_output_note_2 = tx_output_note_1.clone(); let tx_output_notes = [tx_output_note_1, tx_output_note_2]; diff --git a/circuits/tests/transaction.rs b/circuits/tests/transaction.rs index fc1856f..829a97a 100644 --- a/circuits/tests/transaction.rs +++ b/circuits/tests/transaction.rs @@ -6,8 +6,9 @@ use rand_core::{CryptoRng, OsRng, RngCore}; +use dusk_jubjub::JubJubScalar; use phoenix_circuits::transaction::{TxCircuit, TxInputNote, TxOutputNote}; -use phoenix_core::{Note, PublicKey, SecretKey, ViewKey}; +use phoenix_core::{Note, PublicKey, SecretKey}; use dusk_plonk::prelude::*; use poseidon_merkle::{Item, Tree}; @@ -21,7 +22,6 @@ const CAPACITY: usize = 17; // capacity required for the setup const HEIGHT: usize = 17; struct TestingParameters { - sk: SecretKey, pp: PublicParameters, tx_input_notes: [TxInputNote; 4], skeleton_hash: BlsScalar, @@ -48,7 +48,7 @@ lazy_static! { let crossover = 5; let max_fee = 5; - TestingParameters { sk, pp, tx_input_notes, skeleton_hash, root, crossover, max_fee } + TestingParameters { pp, tx_input_notes, skeleton_hash, root, crossover, max_fee } }; } @@ -108,14 +108,11 @@ fn create_test_tx_input_notes( input_notes.try_into().unwrap() } -fn create_test_tx_output_note( - sk: &SecretKey, - value: u64, - rng: &mut (impl RngCore + CryptoRng), -) -> TxOutputNote { - let note = Note::transparent(rng, &PublicKey::from(sk), value); - TxOutputNote::new(¬e, &ViewKey::from(&TP.sk)) - .expect("Note created properly.") +// we don't care if the test output notes are spendable +fn create_test_tx_output_note(value: u64) -> TxOutputNote { + let blinding_factor = JubJubScalar::from(42u64); + + TxOutputNote::new(value, blinding_factor) } #[test] @@ -128,8 +125,8 @@ fn test_transfer_circuit_1_2() { // create 2 testing tx output notes let tx_output_notes = [ - create_test_tx_output_note(&TP.sk, 10, &mut OsRng), - create_test_tx_output_note(&TP.sk, 5, &mut OsRng), + create_test_tx_output_note(10), + create_test_tx_output_note(5), ]; let (proof, public_inputs) = prover @@ -162,8 +159,8 @@ fn test_transfer_circuit_2_2() { // create 2 testing tx output notes let tx_output_notes = [ - create_test_tx_output_note(&TP.sk, 35, &mut OsRng), - create_test_tx_output_note(&TP.sk, 5, &mut OsRng), + create_test_tx_output_note(35), + create_test_tx_output_note(5), ]; let (proof, public_inputs) = prover @@ -199,8 +196,8 @@ fn test_transfer_circuit_3_2() { // create 2 testing tx output notes let tx_output_notes = [ - create_test_tx_output_note(&TP.sk, 35, &mut OsRng), - create_test_tx_output_note(&TP.sk, 30, &mut OsRng), + create_test_tx_output_note(35), + create_test_tx_output_note(30), ]; let (proof, public_inputs) = prover @@ -230,8 +227,8 @@ fn test_transfer_circuit_4_2() { // create 2 testing tx output notes let tx_output_notes = [ - create_test_tx_output_note(&TP.sk, 60, &mut OsRng), - create_test_tx_output_note(&TP.sk, 30, &mut OsRng), + create_test_tx_output_note(60), + create_test_tx_output_note(30), ]; let (proof, public_inputs) = prover