From 1895e5dd80359adf6a49a2a3c197ead1a512615a Mon Sep 17 00:00:00 2001 From: xevisalle Date: Thu, 16 May 2024 16:36:22 +0200 Subject: [PATCH] minor --- circuits/src/transfer.rs | 165 +++++++++++++++++++------------------ circuits/tests/transfer.rs | 66 ++++++++------- 2 files changed, 120 insertions(+), 111 deletions(-) diff --git a/circuits/src/transfer.rs b/circuits/src/transfer.rs index 901b5d6..4fabf49 100644 --- a/circuits/src/transfer.rs +++ b/circuits/src/transfer.rs @@ -20,12 +20,12 @@ use alloc::vec::Vec; use phoenix_core::{Error as PhoenixError, Note, Ownable, SecretKey, ViewKey}; -const OUTPUT: usize = 2; +const TX_OUTPUT_NOTES: usize = 2; /// Struct representing a note willing to be spent, in a way /// suitable for being introduced in the transfer circuit #[derive(Debug, Clone)] -pub struct InputNote { +pub struct TxInputNote { pub(crate) merkle_opening: Opening<(), H, A>, pub(crate) note: Note, pub(crate) note_pk_p: JubJubAffine, @@ -36,7 +36,7 @@ pub struct InputNote { } #[derive(Debug, Clone)] -struct WitnessInputNote { +struct WitnessTxInputNote { note_pk: WitnessPoint, note_pk_p: WitnessPoint, note_type: Witness, @@ -49,15 +49,15 @@ struct WitnessInputNote { signature_r_p: WitnessPoint, } -impl InputNote { - /// Create a circuit input note +impl TxInputNote { + /// Create a tx input note pub fn new( note: &Note, merkle_opening: poseidon_merkle::Opening<(), H, A>, sk: &SecretKey, skeleteon_hash: BlsScalar, rng: &mut (impl RngCore + CryptoRng), - ) -> Result, PhoenixError> { + ) -> Result, PhoenixError> { let note_sk = sk.gen_note_sk(note); let note_pk_p = JubJubAffine::from(GENERATOR_NUMS_EXTENDED * note_sk.as_ref()); @@ -74,7 +74,7 @@ impl InputNote { let signature = note_sk.sign_double(rng, skeleteon_hash); - Ok(crate::transfer::InputNote { + Ok(crate::transfer::TxInputNote { merkle_opening, note: note.clone(), note_pk_p, @@ -85,7 +85,7 @@ impl InputNote { }) } - fn append_to_circuit(&self, composer: &mut Composer) -> WitnessInputNote { + fn append_to_circuit(&self, composer: &mut Composer) -> WitnessTxInputNote { let nullifier = composer.append_public(self.nullifier); let note_pk = composer @@ -103,7 +103,7 @@ impl InputNote { let signature_r = composer.append_point(self.signature.R()); let signature_r_p = composer.append_point(self.signature.R_prime()); - WitnessInputNote { + WitnessTxInputNote { note_pk, note_pk_p, @@ -124,39 +124,42 @@ impl InputNote { /// Struct representing a note willing to be created, in a way /// suitable for being introduced in the transfer circuit #[derive(Debug, Clone)] -pub struct OutputNote { +pub struct TxOutputNote { pub(crate) value: u64, pub(crate) value_commitment: JubJubAffine, pub(crate) blinding_factor: JubJubScalar, } #[derive(Debug, Clone)] -struct WitnessOutputNote { +struct WitnessTxOutputNote { value: Witness, value_commitment: WitnessPoint, blinding_factor: Witness, } -impl OutputNote { - /// Create a circuit output note +impl TxOutputNote { + /// Create a tx output note pub fn new( note: &Note, vk: &ViewKey, - ) -> Result { - Ok(crate::transfer::OutputNote { + ) -> Result { + Ok(crate::transfer::TxOutputNote { value: note.value(Some(vk))?, value_commitment: note.value_commitment().into(), blinding_factor: note.blinding_factor(Some(vk))?, }) } - fn append_to_circuit(&self, composer: &mut Composer) -> WitnessOutputNote { + fn append_to_circuit( + &self, + composer: &mut Composer, + ) -> WitnessTxOutputNote { let value = composer.append_witness(self.value); let value_commitment = composer.append_public_point(self.value_commitment); let blinding_factor = composer.append_witness(self.blinding_factor); - WitnessOutputNote { + WitnessTxOutputNote { value, value_commitment, blinding_factor, @@ -164,12 +167,12 @@ impl OutputNote { } } -/// Transfer gadget expecting I input notes to be spent and O output -/// notes to be created. +/// Transfer gadget expecting I tx input notes to be spent, creating +/// TX_OUTPUT_NOTES = 2 tx output notes pub fn gadget( composer: &mut Composer, - input_notes: &[InputNote; I], - output_notes: &[OutputNote; OUTPUT], + tx_input_notes: &[TxInputNote; I], + tx_output_notes: &[TxOutputNote; TX_OUTPUT_NOTES], skeleton_hash: &BlsScalar, root: &BlsScalar, crossover: u64, @@ -178,21 +181,21 @@ pub fn gadget( let skeleton_hash_pi = composer.append_public(*skeleton_hash); let root_pi = composer.append_public(*root); - let mut input_notes_sum = Composer::ZERO; + let mut tx_input_notes_sum = Composer::ZERO; - // NULLIFY ALL INPUT NOTES - for input_note in input_notes { + // NULLIFY ALL TX INPUT NOTES + for tx_input_note in tx_input_notes { // APPEND THE WITNESSES TO THE CIRCUIT - let w_input_note = input_note.append_to_circuit(composer); + let w_tx_input_note = tx_input_note.append_to_circuit(composer); // VERIFY THE DOUBLE KEY SCHNORR SIGNATURE gadgets::verify_signature_double( composer, - w_input_note.signature_u, - w_input_note.signature_r, - w_input_note.signature_r_p, - w_input_note.note_pk, - w_input_note.note_pk_p, + w_tx_input_note.signature_u, + w_tx_input_note.signature_r, + w_tx_input_note.signature_r_p, + w_tx_input_note.note_pk, + w_tx_input_note.note_pk_p, skeleton_hash_pi, )?; @@ -200,29 +203,29 @@ pub fn gadget( let nullifier = sponge::gadget( composer, &[ - *w_input_note.note_pk_p.x(), - *w_input_note.note_pk_p.y(), - w_input_note.pos, + *w_tx_input_note.note_pk_p.x(), + *w_tx_input_note.note_pk_p.y(), + w_tx_input_note.pos, ], ); - composer.assert_equal(nullifier, w_input_note.nullifier); + composer.assert_equal(nullifier, w_tx_input_note.nullifier); // PERFORM A RANGE CHECK ([0, 2^64 - 1]) ON THE VALUE OF THE NOTE - composer.component_range::<32>(w_input_note.value); + composer.component_range::<32>(w_tx_input_note.value); - // SUM UP ALL THE SPENT VALUES + // SUM UP ALL THE TX INPUT NOTE VALUES let constraint = Constraint::new() .left(1) - .a(input_notes_sum) + .a(tx_input_notes_sum) .right(1) - .b(w_input_note.value); - input_notes_sum = composer.gate_add(constraint); + .b(w_tx_input_note.value); + tx_input_notes_sum = composer.gate_add(constraint); // COMMIT TO THE VALUE OF THE NOTE - let pc_1 = - composer.component_mul_generator(w_input_note.value, GENERATOR)?; + let pc_1 = composer + .component_mul_generator(w_tx_input_note.value, GENERATOR)?; let pc_2 = composer.component_mul_generator( - w_input_note.blinding_factor, + w_tx_input_note.blinding_factor, GENERATOR_NUMS, )?; let value_commitment = composer.component_add_point(pc_1, pc_2); @@ -231,50 +234,50 @@ pub fn gadget( let note_hash = sponge::gadget( composer, &[ - w_input_note.note_type, + w_tx_input_note.note_type, *value_commitment.x(), *value_commitment.y(), - *w_input_note.note_pk.x(), - *w_input_note.note_pk.y(), - w_input_note.pos, + *w_tx_input_note.note_pk.x(), + *w_tx_input_note.note_pk.y(), + w_tx_input_note.pos, ], ); // VERIFY THE MERKLE OPENING let root = - opening_gadget(composer, &input_note.merkle_opening, note_hash); + opening_gadget(composer, &tx_input_note.merkle_opening, note_hash); composer.assert_equal(root, root_pi); } - let mut output_sum = Composer::ZERO; + let mut tx_output_sum = Composer::ZERO; - // COMMIT TO ALL OUTPUT NOTES - for output_note in output_notes { + // COMMIT TO ALL TX OUTPUT NOTES + for tx_output_note in tx_output_notes { // APPEND THE WITNESSES TO THE CIRCUIT - let w_output_note = output_note.append_to_circuit(composer); + let w_tx_output_note = tx_output_note.append_to_circuit(composer); // PERFORM A RANGE CHECK ([0, 2^64 - 1]) ON THE VALUE OF THE NOTE - composer.component_range::<32>(w_output_note.value); + composer.component_range::<32>(w_tx_output_note.value); - // SUM UP ALL THE CREATED NOTE VALUES + // SUM UP ALL THE TX OUTPUT NOTE VALUES let constraint = Constraint::new() .left(1) - .a(output_sum) + .a(tx_output_sum) .right(1) - .b(w_output_note.value); - output_sum = composer.gate_add(constraint); + .b(w_tx_output_note.value); + tx_output_sum = composer.gate_add(constraint); // COMMIT TO THE VALUE OF THE NOTE - let pc_1 = - composer.component_mul_generator(w_output_note.value, GENERATOR)?; + let pc_1 = composer + .component_mul_generator(w_tx_output_note.value, GENERATOR)?; let pc_2 = composer.component_mul_generator( - w_output_note.blinding_factor, + w_tx_output_note.blinding_factor, GENERATOR_NUMS, )?; let value_commitment = composer.component_add_point(pc_1, pc_2); composer.assert_equal_point( - w_output_note.value_commitment, + w_tx_output_note.value_commitment, value_commitment, ); } @@ -285,15 +288,15 @@ pub fn gadget( // SUM UP THE CROSSOVER AND THE MAX FEE let constraint = Constraint::new() .left(1) - .a(output_sum) + .a(tx_output_sum) .right(1) .b(max_fee) .fourth(1) .d(crossover); - output_sum = composer.gate_add(constraint); + tx_output_sum = composer.gate_add(constraint); // VERIFY BALANCE - composer.assert_equal(input_notes_sum, output_sum); + composer.assert_equal(tx_input_notes_sum, tx_output_sum); Ok(()) } @@ -301,8 +304,8 @@ pub fn gadget( /// Declaration of the transfer circuit #[derive(Debug)] pub struct TransferCircuit { - input_notes: [InputNote; I], - output_notes: [OutputNote; OUTPUT], + tx_input_notes: [TxInputNote; I], + tx_output_notes: [TxOutputNote; TX_OUTPUT_NOTES], skeleton_hash: BlsScalar, root: BlsScalar, crossover: u64, @@ -321,7 +324,7 @@ impl Default let mut tree = Tree::<(), H, A>::new(); let skeleton_hash = BlsScalar::default(); - let mut input_notes = Vec::new(); + let mut tx_input_notes = Vec::new(); let note = Note::empty(); let item = Item { hash: note.hash(), @@ -331,7 +334,7 @@ impl Default for _ in 0..I { let merkle_opening = tree.opening(*note.pos()).expect("Tree read."); - let input_note = InputNote::new( + let tx_input_note = TxInputNote::new( ¬e, merkle_opening, &sk, @@ -340,23 +343,23 @@ impl Default ) .expect("Note created properly."); - input_notes.push(input_note); + tx_input_notes.push(tx_input_note); } - let output_note_1 = - OutputNote::new(¬e, &vk).expect("Note created properly."); - let output_note_2 = - OutputNote::new(¬e, &vk).expect("Note created properly."); + 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 output_notes = [output_note_1, output_note_2]; + let tx_output_notes = [tx_output_note_1, tx_output_note_2]; let root = BlsScalar::default(); let crossover = u64::default(); let max_fee = u64::default(); Self { - input_notes: input_notes.try_into().unwrap(), - output_notes, + tx_input_notes: tx_input_notes.try_into().unwrap(), + tx_output_notes, skeleton_hash, root, crossover, @@ -368,16 +371,16 @@ impl Default impl TransferCircuit { /// Create a new transfer circuit pub fn new( - input_notes: [InputNote; I], - output_notes: [OutputNote; OUTPUT], + tx_input_notes: [TxInputNote; I], + tx_output_notes: [TxOutputNote; TX_OUTPUT_NOTES], skeleton_hash: BlsScalar, root: BlsScalar, crossover: u64, max_fee: u64, ) -> Self { Self { - input_notes, - output_notes, + tx_input_notes, + tx_output_notes, skeleton_hash, root, crossover, @@ -392,8 +395,8 @@ impl Circuit fn circuit(&self, composer: &mut Composer) -> Result<(), Error> { gadget::( composer, - &self.input_notes, - &self.output_notes, + &self.tx_input_notes, + &self.tx_output_notes, &self.skeleton_hash, &self.root, self.crossover, diff --git a/circuits/tests/transfer.rs b/circuits/tests/transfer.rs index b018fee..03e32a6 100644 --- a/circuits/tests/transfer.rs +++ b/circuits/tests/transfer.rs @@ -6,7 +6,7 @@ use rand_core::{CryptoRng, OsRng, RngCore}; -use phoenix_circuits::transfer::{InputNote, OutputNote, TransferCircuit}; +use phoenix_circuits::transfer::{TransferCircuit, TxInputNote, TxOutputNote}; use phoenix_core::{Note, PublicKey, SecretKey, ViewKey}; use dusk_plonk::prelude::*; @@ -23,8 +23,8 @@ const ARITY: usize = 4; struct TestingParameters { pp: PublicParameters, - input_notes: [InputNote; 4], - output_notes: [OutputNote; 2], + tx_input_notes: [TxInputNote; 4], + tx_output_notes: [TxOutputNote; 2], skeleton_hash: BlsScalar, root: BlsScalar, crossover: u64, @@ -39,26 +39,25 @@ lazy_static! { let sk = SecretKey::random(&mut rng); let mut tree = Tree::<(), HEIGHT, ARITY>::new(); - let skeleton_hash = BlsScalar::from(1234u64); - // create and insert into the tree 4 testing notes - let input_notes = - create_test_input_notes::<4>(&mut tree, &sk, skeleton_hash, &mut rng); + // create and insert into the tree 4 testing tx input notes + let tx_input_notes = + create_test_tx_input_notes::<4>(&mut tree, &sk, skeleton_hash, &mut rng); // retrieve the root from the tree after inserting the notes let root = tree.root().hash; - // create 2 testing circuit output notes - let output_notes = [ - create_test_output_note(&sk, &mut rng), - create_test_output_note(&sk, &mut rng), + // create 2 testing tx output notes + let tx_output_notes = [ + create_test_tx_output_note(&sk, &mut rng), + create_test_tx_output_note(&sk, &mut rng), ]; let crossover = 0; let max_fee = 0; - TestingParameters { pp, input_notes, output_notes, skeleton_hash, root, crossover, max_fee } + TestingParameters { pp, tx_input_notes, tx_output_notes, skeleton_hash, root, crossover, max_fee } }; } @@ -81,12 +80,12 @@ fn create_and_insert_test_note( note } -fn create_test_input_notes( +fn create_test_tx_input_notes( tree: &mut Tree<(), HEIGHT, ARITY>, sk: &SecretKey, skeleton_hash: BlsScalar, rng: &mut (impl RngCore + CryptoRng), -) -> [InputNote; I] { +) -> [TxInputNote; I] { let pk = PublicKey::from(sk); let mut notes = Vec::new(); @@ -103,9 +102,14 @@ fn create_test_input_notes( let mut input_notes = Vec::new(); for i in 0..I { let merkle_opening = tree.opening(*notes[i].pos()).expect("Tree read."); - let input_note = - InputNote::new(¬es[i], merkle_opening, &sk, skeleton_hash, rng) - .expect("Note created properly."); + let input_note = TxInputNote::new( + ¬es[i], + merkle_opening, + &sk, + skeleton_hash, + rng, + ) + .expect("Note created properly."); input_notes.push(input_note); } @@ -113,12 +117,13 @@ fn create_test_input_notes( input_notes.try_into().unwrap() } -fn create_test_output_note( +fn create_test_tx_output_note( sk: &SecretKey, rng: &mut (impl RngCore + CryptoRng), -) -> OutputNote { +) -> TxOutputNote { let note = Note::transparent(rng, &PublicKey::from(sk), 0); - OutputNote::new(¬e, &ViewKey::from(sk)).expect("Note created properly.") + TxOutputNote::new(¬e, &ViewKey::from(sk)) + .expect("Note created properly.") } #[test] @@ -127,14 +132,14 @@ fn test_transfer_circuit_1_2() { Compiler::compile::>(&TP.pp, LABEL) .expect("failed to compile circuit"); - let input_notes = [TP.input_notes[0].clone()]; + let input_notes = [TP.tx_input_notes[0].clone()]; let (proof, public_inputs) = prover .prove( &mut OsRng, &TransferCircuit::new( input_notes, - TP.output_notes.clone(), + TP.tx_output_notes.clone(), TP.skeleton_hash, TP.root, TP.crossover, @@ -154,14 +159,15 @@ fn test_transfer_circuit_2_2() { Compiler::compile::>(&TP.pp, LABEL) .expect("failed to compile circuit"); - let input_notes = [TP.input_notes[0].clone(), TP.input_notes[1].clone()]; + let input_notes = + [TP.tx_input_notes[0].clone(), TP.tx_input_notes[1].clone()]; let (proof, public_inputs) = prover .prove( &mut OsRng, &TransferCircuit::new( input_notes, - TP.output_notes.clone(), + TP.tx_output_notes.clone(), TP.skeleton_hash, TP.root, TP.crossover, @@ -182,9 +188,9 @@ fn test_transfer_circuit_3_2() { .expect("failed to compile circuit"); let input_notes = [ - TP.input_notes[0].clone(), - TP.input_notes[1].clone(), - TP.input_notes[2].clone(), + TP.tx_input_notes[0].clone(), + TP.tx_input_notes[1].clone(), + TP.tx_input_notes[2].clone(), ]; let (proof, public_inputs) = prover @@ -192,7 +198,7 @@ fn test_transfer_circuit_3_2() { &mut OsRng, &TransferCircuit::new( input_notes, - TP.output_notes.clone(), + TP.tx_output_notes.clone(), TP.skeleton_hash, TP.root, TP.crossover, @@ -216,8 +222,8 @@ fn test_transfer_circuit_4_2() { .prove( &mut OsRng, &TransferCircuit::new( - TP.input_notes.clone(), - TP.output_notes.clone(), + TP.tx_input_notes.clone(), + TP.tx_output_notes.clone(), TP.skeleton_hash, TP.root, TP.crossover,