cl: make transfer scenario readable

This commit is contained in:
David Rusu 2024-07-21 19:59:10 +04:00
parent 9ca5e8af57
commit 05fbbdfe81
2 changed files with 34 additions and 17 deletions

View File

@ -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, &note_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.
}

View File

@ -1,4 +1,3 @@
pub mod death_constraint;
pub mod input;
pub mod partial_tx;
pub mod ptx;