From 05fbbdfe81c37fc7b8f5e619754b7ef6ccf06b23 Mon Sep 17 00:00:00 2001 From: David Rusu Date: Sun, 21 Jul 2024 19:59:10 +0400 Subject: [PATCH] cl: make transfer scenario readable --- goas/cl/ledger/tests/simple_transfer.rs | 50 +++++++++++++++++-------- goas/cl/proof_statements/src/lib.rs | 1 - 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/goas/cl/ledger/tests/simple_transfer.rs b/goas/cl/ledger/tests/simple_transfer.rs index 6bb8def..66fad90 100644 --- a/goas/cl/ledger/tests/simple_transfer.rs +++ b/goas/cl/ledger/tests/simple_transfer.rs @@ -1,6 +1,22 @@ use ledger::{bundle::ProvedBundle, partial_tx::ProvedPartialTx}; use rand_core::CryptoRngCore; +struct User(cl::NullifierSecret); + +impl User { + fn random(mut rng: impl CryptoRngCore) -> Self { + Self(cl::NullifierSecret::random(&mut rng)) + } + + fn pk(&self) -> cl::NullifierCommitment { + self.0.commit() + } + + fn sk(&self) -> cl::NullifierSecret { + self.0 + } +} + fn receive_utxo( note: cl::NoteWitness, nf_pk: cl::NullifierCommitment, @@ -13,37 +29,39 @@ fn receive_utxo( fn test_simple_transfer() { let mut rng = rand::thread_rng(); - let sender_nf_sk = cl::NullifierSecret::random(&mut rng); - let sender_nf_pk = sender_nf_sk.commit(); + // alice is sending 8 NMO to bob. - let recipient_nf_pk = cl::NullifierSecret::random(&mut rng).commit(); + let alice = User::random(&mut rng); + let bob = User::random(&mut rng); - // Assume the sender has received an unspent output from somewhere - let utxo = receive_utxo(cl::NoteWitness::basic(10, "NMO"), sender_nf_pk, &mut rng); + // Alice has an unspent note worth 10 NMO + let utxo = receive_utxo(cl::NoteWitness::basic(10, "NMO"), alice.pk(), &mut rng); + let alices_input = cl::InputWitness::random(utxo, alice.sk(), &mut rng); - let note_commitments = vec![utxo.commit_note()]; + // Alice wants to send 8 NMO to bob + let bobs_output = + cl::OutputWitness::random(cl::NoteWitness::basic(8, "NMO"), bob.pk(), &mut rng); - let input = cl::InputWitness::random(utxo, sender_nf_sk, &mut rng); - - // and wants to send 8 NMO to some recipient and return 2 NMO to itself. - let recipient_output = - cl::OutputWitness::random(cl::NoteWitness::basic(8, "NMO"), recipient_nf_pk, &mut rng); + // .. and return the 2 NMO in change to herself. let change_output = - cl::OutputWitness::random(cl::NoteWitness::basic(2, "NMO"), sender_nf_pk, &mut rng); + cl::OutputWitness::random(cl::NoteWitness::basic(2, "NMO"), alice.pk(), &mut rng); + // Construct the ptx consuming Alices inputs and producing the two outputs. let ptx_witness = cl::PartialTxWitness { - inputs: vec![input], - outputs: vec![recipient_output, change_output], + inputs: vec![alices_input], + outputs: vec![bobs_output, change_output], }; + // assume we only have one note commitment on chain for now ... + let note_commitments = vec![utxo.commit_note()]; let proved_ptx = ProvedPartialTx::prove(&ptx_witness, ¬e_commitments); - assert!(proved_ptx.verify()); + assert!(proved_ptx.verify()); // It's a valid ptx. let bundle = cl::Bundle { partials: vec![ptx_witness.commit()], }; let proved_bundle = ProvedBundle::prove(&bundle, &ptx_witness.balance_blinding()); - assert!(proved_bundle.verify()); + assert!(proved_bundle.verify()); // The bundle is balanced. } diff --git a/goas/cl/proof_statements/src/lib.rs b/goas/cl/proof_statements/src/lib.rs index 36287af..8e58650 100644 --- a/goas/cl/proof_statements/src/lib.rs +++ b/goas/cl/proof_statements/src/lib.rs @@ -1,4 +1,3 @@ pub mod death_constraint; pub mod input; -pub mod partial_tx; pub mod ptx;