Skip to content

Commit

Permalink
replace the sinsemillar note commitment with the poseidon commitment (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
XuyangSong authored Sep 19, 2023
1 parent e0844fb commit 8ecd229
Show file tree
Hide file tree
Showing 22 changed files with 491 additions and 1,474 deletions.
2 changes: 1 addition & 1 deletion taiga_halo2/deprecated/taiga_sudoku/app_vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ fn test_halo2_sudoku_app_vp_circuit_init() {
output_notes[0].note_type.app_data_static =
poseidon_hash(encoded_init_state, current_state.encode());
output_notes[0].value = 1u64;
let owned_note_pub_id = output_notes[0].commitment().get_x();
let owned_note_pub_id = output_notes[0].commitment().inner();
SudokuAppValidityPredicateCircuit {
owned_note_pub_id,
input_notes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ fn test_halo2_dealer_intent_vp_circuit() {
sudoku_app_vk,
);
let encoded_solution = pallas::Base::random(&mut rng);
let owned_note_pub_id = output_notes[0].commitment().get_x();
let owned_note_pub_id = output_notes[0].commitment().inner();
DealerIntentValidityPredicateCircuit {
owned_note_pub_id,
input_notes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn create_transaction<R: RngCore + CryptoRng>(mut rng: R) -> Transaction {
let input_note_3 = create_random_token_note(&mut rng, "xan", 3u64, rho, alice_nk, &alice_auth);
let cascade_intent_note = create_intent_note(
&mut rng,
input_note_3.commitment().get_x(),
input_note_3.commitment().inner(),
input_note_2.get_nf().unwrap(),
alice_nk,
);
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn create_transaction<R: RngCore + CryptoRng>(mut rng: R) -> Transaction {

let intent_note_proving_info = {
let intent_vp = CascadeIntentValidityPredicateCircuit {
owned_note_pub_id: cascade_intent_note.commitment().get_x(),
owned_note_pub_id: cascade_intent_note.commitment().inner(),
input_notes,
output_notes,
cascade_note_cm: cascade_intent_note.get_app_data_static(),
Expand Down
44 changes: 31 additions & 13 deletions taiga_halo2/examples/tx_examples/partial_fulfillment_token_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub fn create_token_intent_ptx<R: RngCore>(
pallas::Scalar,
NullifierKeyContainer,
pallas::Base,
pallas::Base,
Nullifier,
) {
let input_auth = TokenAuthorization::from_sk_vk(&input_auth_sk, &COMPRESSED_TOKEN_AUTH_VK);
Expand All @@ -48,14 +49,14 @@ pub fn create_token_intent_ptx<R: RngCore>(
create_random_token_note(&mut rng, &sell.name, sell.value, rho, input_nk, &input_auth);

// output intent note
// Use the same address as that in the input note. They can be different.
let receiver_address = input_note.get_address();
let input_note_nk_com = input_note.get_nk_commitment();
let input_note_nf = input_note.get_nf().unwrap();
let intent_note = create_intent_note(
&mut rng,
&sell,
&buy,
receiver_address,
input_note_nk_com,
input_note.app_data_dynamic,
input_note_nf,
input_nk,
);
Expand Down Expand Up @@ -85,12 +86,13 @@ pub fn create_token_intent_ptx<R: RngCore>(
// Create the intent note proving info
let intent_note_proving_info = {
let intent_vp = PartialFulfillmentIntentValidityPredicateCircuit {
owned_note_pub_id: intent_note.commitment().get_x(),
owned_note_pub_id: intent_note.commitment().inner(),
input_notes,
output_notes,
sell: sell.clone(),
buy,
receiver_address,
receiver_nk_com: input_note_nk_com,
receiver_app_data_dynamic: input_note.app_data_dynamic,
};

OutputNoteProvingInfo::new(intent_note, Box::new(intent_vp), vec![])
Expand Down Expand Up @@ -118,7 +120,14 @@ pub fn create_token_intent_ptx<R: RngCore>(
&mut rng,
);

(ptx, r, input_nk, receiver_address, rho)
(
ptx,
r,
input_nk,
input_note_nk_com,
input_note.app_data_dynamic,
rho,
)
}

#[allow(clippy::too_many_arguments)]
Expand All @@ -130,11 +139,20 @@ pub fn consume_token_intent_ptx<R: RngCore>(
returned_note_value: u64,
input_rho: Nullifier,
input_nk: NullifierKeyContainer, // NullifierKeyContainer::Key
input_address: pallas::Base,
receiver_nk_com: pallas::Base,
receiver_app_data_dynamic: pallas::Base,
output_auth_pk: pallas::Point,
) -> (ShieldedPartialTransaction, pallas::Scalar) {
// input intent note
let intent_note = create_intent_note(&mut rng, &sell, &buy, input_address, input_rho, input_nk);
let intent_note = create_intent_note(
&mut rng,
&sell,
&buy,
receiver_nk_com,
receiver_app_data_dynamic,
input_rho,
input_nk,
);

// output notes
let input_note_nf = intent_note.get_nf().unwrap();
Expand All @@ -147,8 +165,6 @@ pub fn consume_token_intent_ptx<R: RngCore>(
input_nk,
&output_auth,
);
let address = bought_note.get_address();
assert_eq!(address, input_address);

// padding the zero note
let padding_input_note = Note::random_padding_input_note(&mut rng);
Expand Down Expand Up @@ -176,7 +192,8 @@ pub fn consume_token_intent_ptx<R: RngCore>(
output_notes,
sell: sell.clone(),
buy: buy.clone(),
receiver_address: input_address,
receiver_nk_com,
receiver_app_data_dynamic,
};

InputNoteProvingInfo::new(
Expand Down Expand Up @@ -238,7 +255,7 @@ pub fn create_token_swap_transaction<R: RngCore + CryptoRng>(mut rng: R) -> Tran
name: "eth".to_string(),
value: 10u64,
};
let (alice_ptx, alice_r, intent_nk, receiver_address, intent_rho) =
let (alice_ptx, alice_r, 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
Expand Down Expand Up @@ -268,7 +285,8 @@ pub fn create_token_swap_transaction<R: RngCore + CryptoRng>(mut rng: R) -> Tran
1u64,
intent_rho,
intent_nk,
receiver_address,
receiver_nk_com,
receiver_app_data_dynamic,
alice_auth_pk,
);

Expand Down
54 changes: 33 additions & 21 deletions taiga_halo2/examples/tx_examples/token_swap_with_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn create_token_intent_ptx<R: RngCore>(
pallas::Scalar,
NullifierKeyContainer,
pallas::Base,
pallas::Base,
Nullifier,
) {
let input_auth = TokenAuthorization::from_sk_vk(&input_auth_sk, &COMPRESSED_TOKEN_AUTH_VK);
Expand All @@ -56,14 +57,14 @@ pub fn create_token_intent_ptx<R: RngCore>(
);

// output intent note
// Use the same address as that in the input note. They can be different.
let receiver_address = input_note.get_address();
let input_note_nf = input_note.get_nf().unwrap();
let input_note_nk_com = input_note.get_nk_commitment();
let intent_note = create_intent_note(
&mut rng,
&condition1,
&condition2,
receiver_address,
input_note_nk_com,
input_note.app_data_dynamic,
input_note_nf,
input_nk,
);
Expand Down Expand Up @@ -93,12 +94,13 @@ pub fn create_token_intent_ptx<R: RngCore>(
// Create the intent note proving info
let intent_note_proving_info = {
let intent_vp = OrRelationIntentValidityPredicateCircuit {
owned_note_pub_id: intent_note.commitment().get_x(),
owned_note_pub_id: intent_note.commitment().inner(),
input_notes,
output_notes,
condition1,
condition2,
receiver_address,
receiver_nk_com: input_note_nk_com,
receiver_app_data_dynamic: input_note.app_data_dynamic,
};

OutputNoteProvingInfo::new(intent_note, Box::new(intent_vp), vec![])
Expand Down Expand Up @@ -126,7 +128,14 @@ pub fn create_token_intent_ptx<R: RngCore>(
&mut rng,
);

(ptx, r, input_nk, receiver_address, rho)
(
ptx,
r,
input_nk,
input_note_nk_com,
input_note.app_data_dynamic,
rho,
)
}

#[allow(clippy::too_many_arguments)]
Expand All @@ -136,7 +145,8 @@ pub fn consume_token_intent_ptx<R: RngCore>(
condition2: Condition,
input_rho: Nullifier,
input_nk: NullifierKeyContainer, // NullifierKeyContainer::Key
input_address: pallas::Base,
receiver_nk_com: pallas::Base,
receiver_app_data_dynamic: pallas::Base,
output_token: &str,
output_value: u64,
output_auth_pk: pallas::Point,
Expand All @@ -146,7 +156,8 @@ pub fn consume_token_intent_ptx<R: RngCore>(
&mut rng,
&condition1,
&condition2,
input_address,
receiver_nk_com,
receiver_app_data_dynamic,
input_rho,
input_nk,
);
Expand All @@ -162,8 +173,6 @@ pub fn consume_token_intent_ptx<R: RngCore>(
input_nk.to_commitment(),
&output_auth,
);
let address = output_note.get_address();
assert_eq!(address, input_address);

// padding the zero notes
let padding_input_note = Note::random_padding_input_note(&mut rng);
Expand All @@ -183,7 +192,8 @@ pub fn consume_token_intent_ptx<R: RngCore>(
output_notes,
condition1,
condition2,
receiver_address: input_address,
receiver_nk_com,
receiver_app_data_dynamic,
};

InputNoteProvingInfo::new(
Expand Down Expand Up @@ -242,15 +252,16 @@ pub fn create_token_swap_intent_transaction<R: RngCore + CryptoRng>(mut rng: R)
token_name: "monkey".to_string(),
token_value: 2u64,
};
let (alice_ptx, alice_r, intent_nk, receiver_address, intent_rho) = create_token_intent_ptx(
&mut rng,
condition1.clone(),
condition2.clone(),
"btc",
5u64,
alice_auth_sk,
alice_nk,
);
let (alice_ptx, alice_r, intent_nk, receiver_nk_com, receiver_app_data_dynamic, intent_rho) =
create_token_intent_ptx(
&mut rng,
condition1.clone(),
condition2.clone(),
"btc",
5u64,
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);
Expand All @@ -277,7 +288,8 @@ pub fn create_token_swap_intent_transaction<R: RngCore + CryptoRng>(mut rng: R)
condition2,
intent_rho,
intent_nk,
receiver_address,
receiver_nk_com,
receiver_app_data_dynamic,
"dolphin",
1u64,
alice_auth_pk,
Expand Down
22 changes: 11 additions & 11 deletions taiga_halo2/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use serde;
use borsh::{BorshDeserialize, BorshSerialize};

/// The action result used in transaction.
#[derive(Copy, Debug, Clone)]
#[derive(Debug, Clone)]
#[cfg_attr(feature = "nif", derive(NifStruct))]
#[cfg_attr(feature = "nif", module = "Taiga.Action.Instance")]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -30,8 +30,8 @@ pub struct ActionInstance {
/// The nullifier of input note.
pub nf: Nullifier,
/// The commitment to the output note.
pub cm_x: pallas::Base,
/// The commitment to net value
pub cm: pallas::Base,
/// net value commitment
pub cv_net: ValueCommitment,
/// The commitment to input note application(static) vp
pub input_vp_commitment: ValidityPredicateCommitment,
Expand All @@ -56,7 +56,7 @@ impl ActionInstance {
vec![
self.nf.inner(),
self.anchor,
self.cm_x,
self.cm,
self.cv_net.get_x(),
self.cv_net.get_y(),
input_vp_commitment[0],
Expand All @@ -73,7 +73,7 @@ impl BorshSerialize for ActionInstance {
use ff::PrimeField;
writer.write_all(&self.anchor.to_repr())?;
writer.write_all(&self.nf.to_bytes())?;
writer.write_all(&self.cm_x.to_repr())?;
writer.write_all(&self.cm.to_repr())?;
writer.write_all(&self.cv_net.to_bytes())?;
writer.write_all(&self.input_vp_commitment.to_bytes())?;
writer.write_all(&self.output_vp_commitment.to_bytes())?;
Expand All @@ -92,9 +92,9 @@ impl BorshDeserialize for ActionInstance {
let nf_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let nf = Option::from(Nullifier::from_bytes(nf_bytes))
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "nf not in field"))?;
let cm_x_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let cm_x = Option::from(pallas::Base::from_repr(cm_x_bytes))
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "cm_x not in field"))?;
let cm_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let cm = Option::from(pallas::Base::from_repr(cm_bytes))
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "cm not in field"))?;
let cv_net_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let cv_net = Option::from(ValueCommitment::from_bytes(cv_net_bytes))
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "cv_net not in field"))?;
Expand All @@ -108,7 +108,7 @@ impl BorshDeserialize for ActionInstance {
Ok(ActionInstance {
anchor,
nf,
cm_x,
cm,
cv_net,
input_vp_commitment,
output_vp_commitment,
Expand Down Expand Up @@ -167,7 +167,7 @@ impl ActionInfo {
"The nf of input note should be equal to the rho of output note"
);

let cm_x = self.output_note.commitment().get_x();
let cm = self.output_note.commitment().inner();
let anchor = {
let cm_node = Node::from_note(&self.input_note);
self.input_merkle_path.root(cm_node).inner()
Expand All @@ -186,7 +186,7 @@ impl ActionInfo {

let action = ActionInstance {
nf,
cm_x,
cm,
anchor,
cv_net,
input_vp_commitment,
Expand Down
Loading

0 comments on commit 8ecd229

Please sign in to comment.