Skip to content

Commit

Permalink
tx_examples: use TokenNote type
Browse files Browse the repository at this point in the history
  • Loading branch information
therealyingtong committed Oct 16, 2023
1 parent 6f091e8 commit d11ff6a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 139 deletions.
77 changes: 23 additions & 54 deletions taiga_halo2/examples/tx_examples/cascaded_partial_transactions.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
/// The example shows how to cascade the partial transactions by intents.
/// Alice wants to spend 1 "BTC", 2 "ETH" and 3 "XAN" simultaneously
///
use crate::token::create_random_token_note;
use halo2_proofs::arithmetic::Field;
use pasta_curves::pallas;
use rand::{CryptoRng, RngCore};
use taiga_halo2::{
circuit::vp_examples::{
cascade_intent::{create_intent_note, CascadeIntentValidityPredicateCircuit},
signature_verification::COMPRESSED_TOKEN_AUTH_VK,
token::{
generate_input_token_note_proving_info, generate_output_token_note_proving_info, Token,
TokenAuthorization, TokenName,
},
token::{Token, TokenAuthorization},
},
constant::TAIGA_COMMITMENT_TREE_DEPTH,
merkle_tree::{Anchor, MerklePath},
Expand All @@ -31,51 +27,36 @@ pub fn create_transaction<R: RngCore + CryptoRng>(mut rng: R) -> Transaction {
let bob_nk_com = NullifierKeyContainer::random_commitment(&mut rng);

let rho = Nullifier::from(pallas::Base::random(&mut rng));
let input_note_1 = create_random_token_note(
let input_token_1 = Token::new("btc".to_string(), 1u64);
let input_note_1 = input_token_1.create_random_token_note(&mut rng, rho, alice_nk, &alice_auth);
let output_token_1 = Token::new("btc".to_string(), 1u64);
let output_note_1 = output_token_1.create_random_token_note(
&mut rng,
&Token::new("btc".to_string(), 1u64),
rho,
alice_nk,
&alice_auth,
);
let output_note_1 = create_random_token_note(
&mut rng,
&Token::new("btc".to_string(), 1u64),
input_note_1.get_nf().unwrap(),
bob_nk_com,
&bob_auth,
);
let input_note_2 = create_random_token_note(
&mut rng,
&Token::new("eth".to_string(), 2u64),
rho,
alice_nk,
&alice_auth,
);
let input_token_2 = Token::new("eth".to_string(), 2u64);
let input_note_2 = input_token_2.create_random_token_note(&mut rng, rho, alice_nk, &alice_auth);

let input_note_3 = create_random_token_note(
&mut rng,
&Token::new("xan".to_string(), 3u64),
rho,
alice_nk,
&alice_auth,
);
let input_token_3 = Token::new("xan".to_string(), 3u64);
let input_note_3 = input_token_3.create_random_token_note(&mut rng, rho, alice_nk, &alice_auth);
let cascade_intent_note = create_intent_note(
&mut rng,
input_note_3.commitment().inner(),
input_note_2.get_nf().unwrap(),
alice_nk,
);
let output_note_2 = create_random_token_note(
let output_token_2 = Token::new("eth".to_string(), 2u64);
let output_note_2 = output_token_2.create_random_token_note(
&mut rng,
&Token::new("eth".to_string(), 2u64),
cascade_intent_note.get_nf().unwrap(),
bob_nk_com,
&bob_auth,
);
let output_note_3 = create_random_token_note(
let output_token_3 = Token::new("xan".to_string(), 3u64);
let output_note_3 = output_token_3.create_random_token_note(
&mut rng,
&Token::new("xan".to_string(), 3u64),
input_note_3.get_nf().unwrap(),
bob_nk_com,
&bob_auth,
Expand All @@ -90,23 +71,19 @@ pub fn create_transaction<R: RngCore + CryptoRng>(mut rng: R) -> Transaction {
// Alice consumes 1 "BTC" and 2 "ETH".
// Alice creates a cascade intent note and 1 "BTC" to Bob.
let ptx_1 = {
let input_notes = [input_note_1, input_note_2];
let output_notes = [output_note_1, cascade_intent_note];
let input_notes = [*input_note_1.note(), *input_note_2.note()];
let output_notes = [*output_note_1.note(), cascade_intent_note];
// Create the input note proving info
let input_note_1_proving_info = generate_input_token_note_proving_info(
let input_note_1_proving_info = input_note_1.generate_input_token_note_proving_info(
&mut rng,
input_note_1,
&TokenName("btc".to_string()),
alice_auth,
alice_auth_sk,
merkle_path.clone(),
input_notes,
output_notes,
);
let input_note_2_proving_info = generate_input_token_note_proving_info(
let input_note_2_proving_info = input_note_2.generate_input_token_note_proving_info(
&mut rng,
input_note_2,
&TokenName("eth".to_string()),
alice_auth,
alice_auth_sk,
merkle_path.clone(),
Expand All @@ -115,10 +92,8 @@ pub fn create_transaction<R: RngCore + CryptoRng>(mut rng: R) -> Transaction {
);

// Create the output note proving info
let output_note_1_proving_info = generate_output_token_note_proving_info(
let output_note_1_proving_info = output_note_1.generate_output_token_note_proving_info(
&mut rng,
output_note_1,
&TokenName("btc".to_string()),
bob_auth,
input_notes,
output_notes,
Expand Down Expand Up @@ -148,8 +123,8 @@ pub fn create_transaction<R: RngCore + CryptoRng>(mut rng: R) -> Transaction {
// Alice consumes the intent note and 3 "XAN";
// Alice creates 2 "ETH" and 3 "XAN" to Bob
let ptx_2 = {
let input_notes = [cascade_intent_note, input_note_3];
let output_notes = [output_note_2, output_note_3];
let input_notes = [cascade_intent_note, *input_note_3.note()];
let output_notes = [*output_note_2.note(), *output_note_3.note()];
// Create the input note proving info
let intent_note_proving_info = {
let intent_vp = CascadeIntentValidityPredicateCircuit {
Expand All @@ -167,29 +142,23 @@ pub fn create_transaction<R: RngCore + CryptoRng>(mut rng: R) -> Transaction {
vec![],
)
};
let input_note_3_proving_info = generate_input_token_note_proving_info(
let input_note_3_proving_info = input_note_3.generate_input_token_note_proving_info(
&mut rng,
input_note_3,
&TokenName("xan".to_string()),
alice_auth,
alice_auth_sk,
merkle_path,
input_notes,
output_notes,
);
// Create the output note proving info
let output_note_2_proving_info = generate_output_token_note_proving_info(
let output_note_2_proving_info = output_note_2.generate_output_token_note_proving_info(
&mut rng,
output_note_2,
&TokenName("eth".to_string()),
bob_auth,
input_notes,
output_notes,
);
let output_note_3_proving_info = generate_output_token_note_proving_info(
let output_note_3_proving_info = output_note_3.generate_output_token_note_proving_info(
&mut rng,
output_note_3,
&TokenName("xan".to_string()),
bob_auth,
input_notes,
output_notes,
Expand Down
37 changes: 11 additions & 26 deletions taiga_halo2/examples/tx_examples/partial_fulfillment_token_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// Bob has 5 "ETH" and wants 1 "BTC".
/// The Solver/Bob can partially fulfill Alice's intent and return 1 "BTC" back to Alice.
///
use crate::token::{create_random_token_note, create_token_swap_ptx};
use crate::token::create_token_swap_ptx;
use group::Group;
use halo2_proofs::arithmetic::Field;
use pasta_curves::{group::Curve, pallas};
Expand All @@ -14,10 +14,7 @@ use taiga_halo2::{
create_intent_note, PartialFulfillmentIntentValidityPredicateCircuit,
},
signature_verification::COMPRESSED_TOKEN_AUTH_VK,
token::{
generate_input_token_note_proving_info, generate_output_token_note_proving_info, Token,
TokenAuthorization,
},
token::{Token, TokenAuthorization},
},
constant::TAIGA_COMMITMENT_TREE_DEPTH,
merkle_tree::{Anchor, MerklePath},
Expand All @@ -44,7 +41,7 @@ pub fn create_token_intent_ptx<R: RngCore>(

// input note
let rho = Nullifier::from(pallas::Base::random(&mut rng));
let input_note = create_random_token_note(&mut rng, &sell, rho, input_nk, &input_auth);
let input_note = sell.create_random_token_note(&mut rng, rho, input_nk, &input_auth);

// output intent note
let input_note_nk_com = input_note.get_nk_commitment();
Expand All @@ -64,7 +61,7 @@ pub fn create_token_intent_ptx<R: RngCore>(
let padding_input_note_nf = padding_input_note.get_nf().unwrap();
let padding_output_note = Note::random_padding_output_note(&mut rng, padding_input_note_nf);

let input_notes = [input_note, padding_input_note];
let input_notes = [*input_note.note(), padding_input_note];
let output_notes = [intent_note, padding_output_note];

let merkle_path = MerklePath::random(&mut rng, TAIGA_COMMITMENT_TREE_DEPTH);
Expand All @@ -73,10 +70,8 @@ pub fn create_token_intent_ptx<R: RngCore>(
let anchor = Anchor::from(pallas::Base::random(&mut rng));

// Create the input note proving info
let input_note_proving_info = generate_input_token_note_proving_info(
let input_note_proving_info = input_note.generate_input_token_note_proving_info(
&mut rng,
input_note,
sell.name(),
input_auth,
input_auth_sk,
merkle_path.clone(),
Expand Down Expand Up @@ -160,29 +155,23 @@ pub fn consume_token_intent_ptx<R: RngCore>(
let input_note_nf = intent_note.get_nf().unwrap();
let output_auth = TokenAuthorization::new(output_auth_pk, *COMPRESSED_TOKEN_AUTH_VK);
let bought_token = Token::new(buy.name().inner(), bought_note_value);
let bought_note = create_random_token_note(
&mut rng,
&bought_token,
input_note_nf,
input_nk,
&output_auth,
);
let bought_note =
bought_token.create_random_token_note(&mut rng, input_note_nf, input_nk, &output_auth);

// padding the zero note
let padding_input_note = Note::random_padding_input_note(&mut rng);
let padding_input_note_nf = padding_input_note.get_nf().unwrap();
let returned_token = Token::new(sell.name().inner(), returned_note_value);
let returned_note = create_random_token_note(
let returned_note = returned_token.create_random_token_note(
&mut rng,
&returned_token,
padding_input_note_nf,
input_nk,
&output_auth,
);
// let padding_output_note = Note::random_padding_input_note(&mut rng, padding_input_note_nf);

let input_notes = [intent_note, padding_input_note];
let output_notes = [bought_note, returned_note];
let output_notes = [*bought_note.note(), *returned_note.note()];

let merkle_path = MerklePath::random(&mut rng, TAIGA_COMMITMENT_TREE_DEPTH);

Expand Down Expand Up @@ -211,10 +200,8 @@ pub fn consume_token_intent_ptx<R: RngCore>(
};

// Create the output note proving info
let bought_note_proving_info = generate_output_token_note_proving_info(
let bought_note_proving_info = bought_note.generate_output_token_note_proving_info(
&mut rng,
bought_note,
buy.name(),
output_auth,
input_notes,
output_notes,
Expand All @@ -230,10 +217,8 @@ pub fn consume_token_intent_ptx<R: RngCore>(
);

// Create the returned note proving info
let returned_note_proving_info = generate_output_token_note_proving_info(
let returned_note_proving_info = returned_note.generate_output_token_note_proving_info(
&mut rng,
returned_note,
sell.name(),
output_auth,
input_notes,
output_notes,
Expand Down
52 changes: 9 additions & 43 deletions taiga_halo2/examples/tx_examples/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,15 @@ use rand::RngCore;
use taiga_halo2::{
circuit::vp_examples::{
signature_verification::COMPRESSED_TOKEN_AUTH_VK,
token::{
generate_input_token_note_proving_info, generate_output_token_note_proving_info, Token,
TokenAuthorization, COMPRESSED_TOKEN_VK,
},
token::{Token, TokenAuthorization},
},
constant::TAIGA_COMMITMENT_TREE_DEPTH,
merkle_tree::{Anchor, MerklePath},
note::{InputNoteProvingInfo, Note, OutputNoteProvingInfo, RandomSeed},
note::{InputNoteProvingInfo, Note, OutputNoteProvingInfo},
nullifier::{Nullifier, NullifierKeyContainer},
shielded_ptx::ShieldedPartialTransaction,
};

pub fn create_random_token_note<R: RngCore>(
mut rng: R,
token: &Token,
rho: Nullifier,
nk_container: NullifierKeyContainer,
auth: &TokenAuthorization,
) -> Note {
let app_data_static = token.encode_name();
let app_data_dynamic = auth.to_app_data_dynamic();
let rseed = RandomSeed::random(&mut rng);
Note::new(
*COMPRESSED_TOKEN_VK,
app_data_static,
app_data_dynamic,
token.value(),
nk_container,
rho,
true,
rseed,
)
}

#[allow(clippy::too_many_arguments)]
pub fn create_token_swap_ptx<R: RngCore>(
mut rng: R,
Expand All @@ -54,26 +29,21 @@ pub fn create_token_swap_ptx<R: RngCore>(

// input note
let rho = Nullifier::from(pallas::Base::random(&mut rng));
let input_note = create_random_token_note(&mut rng, &input_token, rho, input_nk, &input_auth);
let input_note = input_token.create_random_token_note(&mut rng, rho, input_nk, &input_auth);

// output note
let input_note_nf = input_note.get_nf().unwrap();
let output_auth = TokenAuthorization::new(output_auth_pk, *COMPRESSED_TOKEN_AUTH_VK);
let output_note = create_random_token_note(
&mut rng,
&output_token,
input_note_nf,
output_nk_com,
&output_auth,
);
let output_note =
output_token.create_random_token_note(&mut rng, input_note_nf, output_nk_com, &output_auth);

// padding the zero notes
let padding_input_note = Note::random_padding_input_note(&mut rng);
let padding_input_note_nf = padding_input_note.get_nf().unwrap();
let padding_output_note = Note::random_padding_output_note(&mut rng, padding_input_note_nf);

let input_notes = [input_note, padding_input_note];
let output_notes = [output_note, padding_output_note];
let input_notes = [*input_note.note(), padding_input_note];
let output_notes = [*output_note.note(), padding_output_note];

// Generate proving info
let merkle_path = MerklePath::random(&mut rng, TAIGA_COMMITMENT_TREE_DEPTH);
Expand All @@ -82,10 +52,8 @@ pub fn create_token_swap_ptx<R: RngCore>(
let anchor = Anchor::from(pallas::Base::random(&mut rng));

// Create the input note proving info
let input_note_proving_info = generate_input_token_note_proving_info(
let input_note_proving_info = input_note.generate_input_token_note_proving_info(
&mut rng,
input_note,
input_token.name(),
input_auth,
input_auth_sk,
merkle_path.clone(),
Expand All @@ -94,10 +62,8 @@ pub fn create_token_swap_ptx<R: RngCore>(
);

// Create the output note proving info
let output_note_proving_info = generate_output_token_note_proving_info(
let output_note_proving_info = output_note.generate_output_token_note_proving_info(
&mut rng,
output_note,
output_token.name(),
output_auth,
input_notes,
output_notes,
Expand Down
Loading

0 comments on commit d11ff6a

Please sign in to comment.