Skip to content

Commit

Permalink
refactor shielded ptx building (#235)
Browse files Browse the repository at this point in the history
* refactor shielded ptx building

* generate the rseed of action inside of the new function and improve the anchor calculation

* automatically reset the rho of output note when constructing the Action

* simplify the random and padding note creation

* fix typo

* rename reset_rho to set_rho
  • Loading branch information
XuyangSong authored Nov 8, 2023
1 parent 88ca28f commit 4770ea1
Show file tree
Hide file tree
Showing 20 changed files with 796 additions and 802 deletions.
12 changes: 8 additions & 4 deletions taiga_halo2/benches/action_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn bench_action_proof(name: &str, c: &mut Criterion) {
rho,
}
};
let output_note = {
let mut output_note = {
let rho = input_note.get_nf().unwrap();
let nk_com = NullifierKeyContainer::from_commitment(pallas::Base::random(&mut rng));
let note_type = {
Expand All @@ -66,9 +66,13 @@ fn bench_action_proof(name: &str, c: &mut Criterion) {
}
};
let input_merkle_path = MerklePath::random(&mut rng, TAIGA_COMMITMENT_TREE_DEPTH);
let anchor = input_note.calculate_root(&input_merkle_path);
let rseed = RandomSeed::random(&mut rng);
ActionInfo::new(input_note, input_merkle_path, anchor, output_note, rseed)
ActionInfo::new(
input_note,
input_merkle_path,
None,
&mut output_note,
&mut rng,
)
};
let (action, action_circuit) = action_info.build();
let params = SETUP_PARAMS_MAP.get(&ACTION_CIRCUIT_PARAMS_SIZE).unwrap();
Expand Down
254 changes: 142 additions & 112 deletions taiga_halo2/examples/tx_examples/cascaded_partial_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,45 @@ use halo2_proofs::arithmetic::Field;
use pasta_curves::pallas;
use rand::{CryptoRng, RngCore};
use taiga_halo2::{
action::ActionInfo,
circuit::vp_examples::{
cascade_intent::{create_intent_note, CascadeIntentValidityPredicateCircuit},
signature_verification::COMPRESSED_TOKEN_AUTH_VK,
token::{Token, TokenAuthorization},
},
constant::TAIGA_COMMITMENT_TREE_DEPTH,
merkle_tree::{Anchor, MerklePath},
note::{InputNoteProvingInfo, OutputNoteProvingInfo},
nullifier::{Nullifier, NullifierKeyContainer},
note::NoteValidityPredicates,
shielded_ptx::ShieldedPartialTransaction,
transaction::{ShieldedPartialTxBundle, Transaction, TransparentPartialTxBundle},
};

pub fn create_transaction<R: RngCore + CryptoRng>(mut rng: R) -> Transaction {
let alice_auth_sk = pallas::Scalar::random(&mut rng);
let alice_auth = TokenAuthorization::from_sk_vk(&alice_auth_sk, &COMPRESSED_TOKEN_AUTH_VK);
let alice_nk = NullifierKeyContainer::random_key(&mut rng);
let alice_nk = pallas::Base::random(&mut rng);

let bob_auth = TokenAuthorization::random(&mut rng);
let bob_nk_com = NullifierKeyContainer::random_commitment(&mut rng);
let bob_nk_com = pallas::Base::random(&mut rng);

let rho = Nullifier::from(pallas::Base::random(&mut rng));
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 input_note_1 =
input_token_1.create_random_input_token_note(&mut rng, 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,
input_note_1.get_nf().unwrap(),
bob_nk_com,
&bob_auth,
);
let mut output_note_1 = output_token_1.create_random_output_token_note(bob_nk_com, &bob_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_2 =
input_token_2.create_random_input_token_note(&mut rng, 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 input_note_3 =
input_token_3.create_random_input_token_note(&mut rng, alice_nk, &alice_auth);
let mut cascade_intent_note =
create_intent_note(&mut rng, input_note_3.commitment().inner(), alice_nk);
let output_token_2 = Token::new("eth".to_string(), 2u64);
let output_note_2 = output_token_2.create_random_token_note(
&mut rng,
cascade_intent_note.get_nf().unwrap(),
bob_nk_com,
&bob_auth,
);
let mut output_note_2 = output_token_2.create_random_output_token_note(bob_nk_com, &bob_auth);
let output_token_3 = Token::new("xan".to_string(), 3u64);
let output_note_3 = output_token_3.create_random_token_note(
&mut rng,
input_note_3.get_nf().unwrap(),
bob_nk_com,
&bob_auth,
);
let mut output_note_3 = output_token_3.create_random_output_token_note(bob_nk_com, &bob_auth);

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

Expand All @@ -71,106 +54,153 @@ 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.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 = input_note_1.generate_input_token_note_proving_info(
&mut rng,
alice_auth,
alice_auth_sk,
merkle_path.clone(),
input_notes,
output_notes,
);
let input_note_2_proving_info = input_note_2.generate_input_token_note_proving_info(
&mut rng,
alice_auth,
alice_auth_sk,
merkle_path.clone(),
input_notes,
output_notes,
);

// Create the output note proving info
let output_note_1_proving_info = output_note_1.generate_output_token_note_proving_info(
&mut rng,
bob_auth,
input_notes,
output_notes,
);

let intent_note_proving_info = {
let intent_vp = CascadeIntentValidityPredicateCircuit {
owned_note_pub_id: cascade_intent_note.commitment().inner(),
// Create action pairs
let actions = {
let action_1 = ActionInfo::new(
*input_note_1.note(),
merkle_path.clone(),
None,
&mut output_note_1.note,
&mut rng,
);

let action_2 = ActionInfo::new(
*input_note_2.note(),
merkle_path.clone(),
None,
&mut cascade_intent_note,
&mut rng,
);
vec![action_1, action_2]
};

// Create VPs
let (input_vps, output_vps) = {
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_1 vps
let input_note_1_vps = input_note_1.generate_input_token_vps(
&mut rng,
alice_auth,
alice_auth_sk,
input_notes,
output_notes,
cascade_note_cm: cascade_intent_note.get_app_data_static(),
);

// Create the input note_2 vps
let input_note_2_vps = input_note_2.generate_input_token_vps(
&mut rng,
alice_auth,
alice_auth_sk,
input_notes,
output_notes,
);

// Create the output note_1 vps
let output_note_1_vps = output_note_1.generate_output_token_vps(
&mut rng,
bob_auth,
input_notes,
output_notes,
);

// Create intent vps
let intent_vps = {
let intent_vp = CascadeIntentValidityPredicateCircuit {
owned_note_pub_id: cascade_intent_note.commitment().inner(),
input_notes,
output_notes,
cascade_note_cm: cascade_intent_note.get_app_data_static(),
};

NoteValidityPredicates::new(Box::new(intent_vp), vec![])
};

OutputNoteProvingInfo::new(cascade_intent_note, Box::new(intent_vp), vec![])
(
vec![input_note_1_vps, input_note_2_vps],
vec![output_note_1_vps, intent_vps],
)
};

// Create shielded partial tx
ShieldedPartialTransaction::build(
[input_note_1_proving_info, input_note_2_proving_info],
[output_note_1_proving_info, intent_note_proving_info],
vec![],
&mut rng,
)
ShieldedPartialTransaction::build(actions, input_vps, output_vps, vec![], &mut rng).unwrap()
};

// The second partial 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.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 {
owned_note_pub_id: cascade_intent_note.get_nf().unwrap().inner(),
input_notes,
output_notes,
cascade_note_cm: cascade_intent_note.get_app_data_static(),
};

InputNoteProvingInfo::new(
// Create action pairs
let actions = {
let action_1 = ActionInfo::new(
cascade_intent_note,
merkle_path.clone(),
Some(anchor),
Box::new(intent_vp),
vec![],
&mut output_note_2.note,
&mut rng,
);

let action_2 = ActionInfo::new(
*input_note_3.note(),
merkle_path,
None,
&mut output_note_3.note,
&mut rng,
);
vec![action_1, action_2]
};

// Create VPs
let (input_vps, output_vps) = {
let input_notes = [cascade_intent_note, *input_note_3.note()];
let output_notes = [*output_note_2.note(), *output_note_3.note()];

// Create intent vps
let intent_vps = {
let intent_vp = CascadeIntentValidityPredicateCircuit {
owned_note_pub_id: cascade_intent_note.get_nf().unwrap().inner(),
input_notes,
output_notes,
cascade_note_cm: cascade_intent_note.get_app_data_static(),
};

NoteValidityPredicates::new(Box::new(intent_vp), vec![])
};

// Create input note_3 vps
let input_note_3_vps = input_note_3.generate_input_token_vps(
&mut rng,
alice_auth,
alice_auth_sk,
input_notes,
output_notes,
);

// Create output note_2 vps
let output_note_2_vps = output_note_2.generate_output_token_vps(
&mut rng,
bob_auth,
input_notes,
output_notes,
);

// Create output note_3 vps
let output_note_3_vps = output_note_3.generate_output_token_vps(
&mut rng,
bob_auth,
input_notes,
output_notes,
);

(
vec![intent_vps, input_note_3_vps],
vec![output_note_2_vps, output_note_3_vps],
)
};
let input_note_3_proving_info = input_note_3.generate_input_token_note_proving_info(
&mut rng,
alice_auth,
alice_auth_sk,
merkle_path,
input_notes,
output_notes,
);
// Create the output note proving info
let output_note_2_proving_info = output_note_2.generate_output_token_note_proving_info(
&mut rng,
bob_auth,
input_notes,
output_notes,
);
let output_note_3_proving_info = output_note_3.generate_output_token_note_proving_info(
&mut rng,
bob_auth,
input_notes,
output_notes,
);

// Create shielded partial tx
ShieldedPartialTransaction::build(
[intent_note_proving_info, input_note_3_proving_info],
[output_note_2_proving_info, output_note_3_proving_info],
vec![],
&mut rng,
)
ShieldedPartialTransaction::build(actions, input_vps, output_vps, vec![], &mut rng).unwrap()
};

// Create the final transaction
Expand Down
Loading

0 comments on commit 4770ea1

Please sign in to comment.