diff --git a/taiga_halo2/examples/tx_examples/cascaded_partial_transactions.rs b/taiga_halo2/examples/tx_examples/cascaded_partial_transactions.rs index 4b867e7a..f8ef030e 100644 --- a/taiga_halo2/examples/tx_examples/cascaded_partial_transactions.rs +++ b/taiga_halo2/examples/tx_examples/cascaded_partial_transactions.rs @@ -1,7 +1,6 @@ /// 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}; @@ -9,10 +8,7 @@ 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}, @@ -31,51 +27,36 @@ pub fn create_transaction(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, @@ -90,23 +71,19 @@ pub fn create_transaction(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(), @@ -115,10 +92,8 @@ pub fn create_transaction(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, @@ -148,8 +123,8 @@ pub fn create_transaction(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 { @@ -167,10 +142,8 @@ pub fn create_transaction(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, @@ -178,18 +151,14 @@ pub fn create_transaction(mut rng: R) -> Transaction { 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, diff --git a/taiga_halo2/examples/tx_examples/partial_fulfillment_token_swap.rs b/taiga_halo2/examples/tx_examples/partial_fulfillment_token_swap.rs index faa4c0b4..1fd1c42a 100644 --- a/taiga_halo2/examples/tx_examples/partial_fulfillment_token_swap.rs +++ b/taiga_halo2/examples/tx_examples/partial_fulfillment_token_swap.rs @@ -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}; @@ -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}, @@ -44,7 +41,7 @@ pub fn create_token_intent_ptx( // 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(); @@ -64,7 +61,7 @@ pub fn create_token_intent_ptx( 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); @@ -73,10 +70,8 @@ pub fn create_token_intent_ptx( 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(), @@ -160,21 +155,15 @@ pub fn consume_token_intent_ptx( 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, @@ -182,7 +171,7 @@ pub fn consume_token_intent_ptx( // 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); @@ -211,10 +200,8 @@ pub fn consume_token_intent_ptx( }; // 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, @@ -230,10 +217,8 @@ pub fn consume_token_intent_ptx( ); // 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, diff --git a/taiga_halo2/examples/tx_examples/token.rs b/taiga_halo2/examples/tx_examples/token.rs index 8ec5deab..79b8fb97 100644 --- a/taiga_halo2/examples/tx_examples/token.rs +++ b/taiga_halo2/examples/tx_examples/token.rs @@ -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( - 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( mut rng: R, @@ -54,26 +29,21 @@ pub fn create_token_swap_ptx( // 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); @@ -82,10 +52,8 @@ pub fn create_token_swap_ptx( 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(), @@ -94,10 +62,8 @@ pub fn create_token_swap_ptx( ); // 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, diff --git a/taiga_halo2/examples/tx_examples/token_swap_with_intent.rs b/taiga_halo2/examples/tx_examples/token_swap_with_intent.rs index 163bfdb4..507cb92f 100644 --- a/taiga_halo2/examples/tx_examples/token_swap_with_intent.rs +++ b/taiga_halo2/examples/tx_examples/token_swap_with_intent.rs @@ -3,7 +3,7 @@ /// Bob has 1 "DOLPHIN" and wants 5 "BTC". /// The Solver/Bob matches Alice's intent and creates the final tx. /// -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}; @@ -12,10 +12,7 @@ use taiga_halo2::{ circuit::vp_examples::{ or_relation_intent::{create_intent_note, OrRelationIntentValidityPredicateCircuit}, 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}, @@ -43,7 +40,7 @@ pub fn create_token_intent_ptx( // 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 intent note let input_note_nf = input_note.get_nf().unwrap(); @@ -65,16 +62,14 @@ pub fn create_token_intent_ptx( // Fetch a valid anchor for padding input notes let anchor = Anchor::from(pallas::Base::random(&mut rng)); - 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); // 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(), @@ -156,9 +151,8 @@ pub fn consume_token_intent_ptx( // output note let input_note_nf = intent_note.get_nf().unwrap(); let output_auth = TokenAuthorization::new(output_auth_pk, *COMPRESSED_TOKEN_AUTH_VK); - let output_note = create_random_token_note( + let output_note = output_token.create_random_token_note( &mut rng, - &output_token, input_note_nf, input_nk.to_commitment(), &output_auth, @@ -170,7 +164,7 @@ pub fn consume_token_intent_ptx( let padding_output_note = Note::random_padding_output_note(&mut rng, padding_input_note_nf); let input_notes = [intent_note, padding_input_note]; - let output_notes = [output_note, padding_output_note]; + let output_notes = [*output_note.note(), padding_output_note]; let merkle_path = MerklePath::random(&mut rng, TAIGA_COMMITMENT_TREE_DEPTH); @@ -199,10 +193,8 @@ pub fn consume_token_intent_ptx( }; // 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,