From bf018bbf8e2609f8c7e80747b6a269253eb95a91 Mon Sep 17 00:00:00 2001 From: moana Date: Wed, 12 Jun 2024 16:42:13 +0200 Subject: [PATCH] circuits: Roll back value-commitment recomputation --- circuits/src/transaction.rs | 14 ++++++----- circuits/tests/transaction.rs | 46 ++++++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/circuits/src/transaction.rs b/circuits/src/transaction.rs index 59c8a78..a4766bd 100644 --- a/circuits/src/transaction.rs +++ b/circuits/src/transaction.rs @@ -19,9 +19,7 @@ use rand::{CryptoRng, RngCore, SeedableRng}; extern crate alloc; use alloc::vec::Vec; -use phoenix_core::{ - value_commitment, Error as PhoenixError, Note, Ownable, SecretKey, ViewKey, -}; +use phoenix_core::{Error as PhoenixError, Note, Ownable, SecretKey, ViewKey}; use crate::{recipient, recipient::RecipientParameters}; @@ -142,11 +140,15 @@ struct WitnessTxOutputNote { } impl TxOutputNote { - /// Crate a new `TxOutputNote`. - pub fn new(value: u64, blinding_factor: JubJubScalar) -> Self { + /// Create a new `TxOutputNote`. + pub fn new( + value: u64, + value_commitment: JubJubAffine, + blinding_factor: JubJubScalar, + ) -> Self { Self { value, - value_commitment: value_commitment(value, blinding_factor), + value_commitment, blinding_factor, } } diff --git a/circuits/tests/transaction.rs b/circuits/tests/transaction.rs index 7c82708..ccc6fdd 100644 --- a/circuits/tests/transaction.rs +++ b/circuits/tests/transaction.rs @@ -17,7 +17,7 @@ use phoenix_circuits::{ transaction::{TxCircuit, TxInputNote, TxOutputNote}, RecipientParameters, }; -use phoenix_core::{Note, PublicKey, SecretKey}; +use phoenix_core::{value_commitment, Note, PublicKey, SecretKey}; #[macro_use] extern crate lazy_static; @@ -141,10 +141,20 @@ fn test_transfer_circuit_1_2() { let input_notes = [TP.tx_input_notes[0].clone()]; // create 2 testing tx output notes + let value1 = 10; + let blinder1 = JubJubScalar::random(&mut rng); + let commitment1 = value_commitment(value1, blinder1); + let value2 = 5; + let blinder2 = JubJubScalar::random(&mut rng); + let commitment2 = value_commitment(value2, blinder2); let tx_output_notes = [ - TxOutputNote::new(10, JubJubScalar::random(&mut rng)), - TxOutputNote::new(5, JubJubScalar::random(&mut rng)), + TxOutputNote::new(value1, commitment1, blinder1), + TxOutputNote::new(value2, commitment2, blinder2), ]; + // let tx_output_notes = [ + // TxOutputNote::new(10, JubJubScalar::random(&mut rng)), + // TxOutputNote::new(5, JubJubScalar::random(&mut rng)), + // ]; let (proof, public_inputs) = prover .prove( @@ -178,9 +188,15 @@ fn test_transfer_circuit_2_2() { [TP.tx_input_notes[0].clone(), TP.tx_input_notes[1].clone()]; // create 2 testing tx output notes + let value1 = 35; + let blinder1 = JubJubScalar::random(&mut rng); + let commitment1 = value_commitment(value1, blinder1); + let value2 = 5; + let blinder2 = JubJubScalar::random(&mut rng); + let commitment2 = value_commitment(value2, blinder2); let tx_output_notes = [ - TxOutputNote::new(35, JubJubScalar::random(&mut rng)), - TxOutputNote::new(5, JubJubScalar::random(&mut rng)), + TxOutputNote::new(value1, commitment1, blinder1), + TxOutputNote::new(value2, commitment2, blinder2), ]; let (proof, public_inputs) = prover @@ -218,9 +234,15 @@ fn test_transfer_circuit_3_2() { ]; // create 2 testing tx output notes + let value1 = 35; + let blinder1 = JubJubScalar::random(&mut rng); + let commitment1 = value_commitment(value1, blinder1); + let value2 = 30; + let blinder2 = JubJubScalar::random(&mut rng); + let commitment2 = value_commitment(value2, blinder2); let tx_output_notes = [ - TxOutputNote::new(35, JubJubScalar::random(&mut rng)), - TxOutputNote::new(30, JubJubScalar::random(&mut rng)), + TxOutputNote::new(value1, commitment1, blinder1), + TxOutputNote::new(value2, commitment2, blinder2), ]; let (proof, public_inputs) = prover @@ -252,9 +274,15 @@ fn test_transfer_circuit_4_2() { .expect("failed to compile circuit"); // create 2 testing tx output notes + let value1 = 60; + let blinder1 = JubJubScalar::random(&mut rng); + let commitment1 = value_commitment(value1, blinder1); + let value2 = 30; + let blinder2 = JubJubScalar::random(&mut rng); + let commitment2 = value_commitment(value2, blinder2); let tx_output_notes = [ - TxOutputNote::new(60, JubJubScalar::random(&mut rng)), - TxOutputNote::new(30, JubJubScalar::random(&mut rng)), + TxOutputNote::new(value1, commitment1, blinder1), + TxOutputNote::new(value2, commitment2, blinder2), ]; let (proof, public_inputs) = prover