Skip to content

Commit

Permalink
Refactor examples to use Token, TokenNote (#229)
Browse files Browse the repository at this point in the history
* vp_examples::token: Introduce convenience methods for Token

* tx_examples::token: Use Token type in methods

* circuit::vp_examples: Update examples to use new APIs

* examples::tx_examples: Update examples to use new APIs

* vp_examples::token: Introduce TokenNote type

* tx_examples: use TokenNote type
  • Loading branch information
therealyingtong authored Oct 18, 2023
1 parent 7076461 commit 5880f50
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 374 deletions.
62 changes: 23 additions & 39 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,
TokenAuthorization,
},
token::{Token, TokenAuthorization},
},
constant::TAIGA_COMMITMENT_TREE_DEPTH,
merkle_tree::{Anchor, MerklePath},
Expand All @@ -31,36 +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(&mut rng, "btc", 1u64, rho, alice_nk, &alice_auth);
let output_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,
"btc",
1u64,
input_note_1.get_nf().unwrap(),
bob_nk_com,
&bob_auth,
);
let input_note_2 = create_random_token_note(&mut rng, "eth", 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, "xan", 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,
"eth",
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,
"xan",
3u64,
input_note_3.get_nf().unwrap(),
bob_nk_com,
&bob_auth,
Expand All @@ -75,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,
"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,
"eth".to_string(),
alice_auth,
alice_auth_sk,
merkle_path.clone(),
Expand All @@ -100,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,
"btc".to_string(),
bob_auth,
input_notes,
output_notes,
Expand Down Expand Up @@ -133,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 @@ -152,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,
"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,
"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,
"xan".to_string(),
bob_auth,
input_notes,
output_notes,
Expand Down
60 changes: 19 additions & 41 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,8 +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.name, sell.value, 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 @@ -65,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 @@ -74,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.clone(),
input_auth,
input_auth_sk,
merkle_path.clone(),
Expand Down Expand Up @@ -160,30 +154,24 @@ pub fn consume_token_intent_ptx<R: RngCore>(
// output notes
let input_note_nf = intent_note.get_nf().unwrap();
let output_auth = TokenAuthorization::new(output_auth_pk, *COMPRESSED_TOKEN_AUTH_VK);
let bought_note = create_random_token_note(
&mut rng,
&buy.name,
bought_note_value,
input_note_nf,
input_nk,
&output_auth,
);
let bought_token = Token::new(buy.name().inner(), bought_note_value);
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_note = create_random_token_note(
let returned_token = Token::new(sell.name().inner(), returned_note_value);
let returned_note = returned_token.create_random_token_note(
&mut rng,
&sell.name,
returned_note_value,
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 @@ -212,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 @@ -231,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 All @@ -256,30 +240,24 @@ pub fn create_token_swap_transaction<R: RngCore + CryptoRng>(mut rng: R) -> Tran
let alice_auth_sk = pallas::Scalar::random(&mut rng);
let alice_auth_pk = generator * alice_auth_sk;
let alice_nk = NullifierKeyContainer::random_key(&mut rng);
let sell = Token {
name: "btc".to_string(),
value: 2u64,
};
let buy = Token {
name: "eth".to_string(),
value: 10u64,
};
let sell = Token::new("btc".to_string(), 2u64);
let buy = Token::new("eth".to_string(), 10u64);
let (alice_ptx, intent_nk, receiver_nk_com, receiver_app_data_dynamic, intent_rho) =
create_token_intent_ptx(&mut rng, sell.clone(), buy.clone(), alice_auth_sk, alice_nk);

// Bob creates the partial transaction with 1 DOLPHIN input and 5 BTC output
let bob_auth_sk = pallas::Scalar::random(&mut rng);
let bob_auth_pk = generator * bob_auth_sk;
let bob_nk = NullifierKeyContainer::random_key(&mut rng);
let eth_token = Token::new("eth".to_string(), 5);
let btc_token = Token::new("btc".to_string(), 1);

let bob_ptx = create_token_swap_ptx(
&mut rng,
"eth",
5,
eth_token,
bob_auth_sk,
bob_nk,
"btc",
1,
btc_token,
bob_auth_pk,
bob_nk.to_commitment(),
);
Expand Down
Loading

0 comments on commit 5880f50

Please sign in to comment.