Skip to content

Commit

Permalink
Merge pull request #216 from anoma/xuyang/improve_basic_structs
Browse files Browse the repository at this point in the history
improve some basic structs
  • Loading branch information
XuyangSong authored Sep 22, 2023
2 parents 250ac39 + dbb8e84 commit 1ebcf08
Show file tree
Hide file tree
Showing 21 changed files with 148 additions and 101 deletions.
2 changes: 1 addition & 1 deletion taiga_halo2/benches/action_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn bench_action_proof(name: &str, c: &mut Criterion) {
let mut rng = OsRng;
let action_info = {
let input_note = {
let rho = Nullifier::new(pallas::Base::random(&mut rng));
let rho = Nullifier::from(pallas::Base::random(&mut rng));
let nk = NullifierKeyContainer::from_key(pallas::Base::random(&mut rng));
let note_type = {
let app_vk = pallas::Base::random(&mut rng);
Expand Down
2 changes: 1 addition & 1 deletion taiga_halo2/benches/vp_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn bench_vp_proof(name: &str, c: &mut Criterion) {

let vp_circuit = {
let input_notes = [(); NUM_NOTE].map(|_| {
let rho = Nullifier::new(pallas::Base::random(&mut rng));
let rho = Nullifier::from(pallas::Base::random(&mut rng));
let nk = NullifierKeyContainer::from_key(pallas::Base::random(&mut rng));
let note_type = {
let app_vk = pallas::Base::random(&mut rng);
Expand Down
2 changes: 1 addition & 1 deletion taiga_halo2/deprecated/simple_sudoku/vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ mod tests {
let value: u64 = 0;
let nk = NullifierKeyContainer::random_key(&mut rng);
let rseed = RandomSeed::random(&mut rng);
let rho = Nullifier::new(pallas::Base::random(&mut rng));
let rho = Nullifier::from(pallas::Base::random(&mut rng));
Note::new(
vp_vk,
app_data_static,
Expand Down
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 @@ -602,7 +602,7 @@ pub mod tests {
};

pub fn random_input_note<R: RngCore>(mut rng: R) -> Note {
let rho = Nullifier::new(pallas::Base::random(&mut rng));
let rho = Nullifier::from(pallas::Base::random(&mut rng));
let nk = NullifierKeyContainer::from_key(pallas::Base::random(&mut rng));
let note_type = {
let app_vk = pallas::Base::random(&mut rng);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn create_transaction<R: RngCore + CryptoRng>(mut rng: R) -> Transaction {
let bob_auth = TokenAuthorization::random(&mut rng);
let bob_nk_com = NullifierKeyContainer::random_commitment(&mut rng);

let rho = Nullifier::new(pallas::Base::random(&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(
&mut rng,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn create_token_intent_ptx<R: RngCore>(
let input_auth = TokenAuthorization::from_sk_vk(&input_auth_sk, &COMPRESSED_TOKEN_AUTH_VK);

// input note
let rho = Nullifier::new(pallas::Base::random(&mut rng));
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);

Expand Down
2 changes: 1 addition & 1 deletion taiga_halo2/examples/tx_examples/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn create_token_swap_ptx<R: RngCore>(
let input_auth = TokenAuthorization::from_sk_vk(&input_auth_sk, &COMPRESSED_TOKEN_AUTH_VK);

// input note
let rho = Nullifier::new(pallas::Base::random(&mut rng));
let rho = Nullifier::from(pallas::Base::random(&mut rng));
let input_note = create_random_token_note(
&mut rng,
input_token,
Expand Down
2 changes: 1 addition & 1 deletion taiga_halo2/examples/tx_examples/token_swap_with_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn create_token_intent_ptx<R: RngCore>(
let input_auth = TokenAuthorization::from_sk_vk(&input_auth_sk, &COMPRESSED_TOKEN_AUTH_VK);

// input note
let rho = Nullifier::new(pallas::Base::random(&mut rng));
let rho = Nullifier::from(pallas::Base::random(&mut rng));
let input_note = create_random_token_note(
&mut rng,
input_token,
Expand Down
48 changes: 23 additions & 25 deletions taiga_halo2/src/action.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
circuit::action_circuit::ActionCircuit,
constant::{PRF_EXPAND_INPUT_VP_CM_R, PRF_EXPAND_OUTPUT_VP_CM_R},
merkle_tree::{MerklePath, Node},
note::{InputNoteProvingInfo, Note, OutputNoteProvingInfo, RandomSeed},
merkle_tree::{Anchor, MerklePath, Node},
note::{InputNoteProvingInfo, Note, NoteCommitment, OutputNoteProvingInfo, RandomSeed},
nullifier::Nullifier,
value_commitment::ValueCommitment,
vp_commitment::ValidityPredicateCommitment,
Expand All @@ -19,18 +19,18 @@ use serde;
#[cfg(feature = "borsh")]
use borsh::{BorshDeserialize, BorshSerialize};

/// The action result used in transaction.
/// The public inputs of action proof.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "nif", derive(NifStruct))]
#[cfg_attr(feature = "nif", module = "Taiga.Action.Instance")]
#[cfg_attr(feature = "nif", module = "Taiga.Action.PublicInputs")]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ActionInstance {
pub struct ActionPublicInputs {
/// The root of the note commitment Merkle tree.
pub anchor: pallas::Base,
pub anchor: Anchor,
/// The nullifier of input note.
pub nf: Nullifier,
/// The commitment to the output note.
pub cm: pallas::Base,
pub cm: NoteCommitment,
/// net value commitment
pub cv_net: ValueCommitment,
/// The commitment to input note application(static) vp
Expand All @@ -39,7 +39,7 @@ pub struct ActionInstance {
pub output_vp_commitment: ValidityPredicateCommitment,
}

/// The information to build ActionInstance and ActionCircuit.
/// The information to build ActionPublicInputs and ActionCircuit.
#[derive(Clone)]
pub struct ActionInfo {
input_note: Note,
Expand All @@ -49,14 +49,14 @@ pub struct ActionInfo {
rseed: RandomSeed,
}

impl ActionInstance {
impl ActionPublicInputs {
pub fn to_instance(&self) -> Vec<pallas::Base> {
let input_vp_commitment = self.input_vp_commitment.to_public_inputs();
let output_vp_commitment = self.output_vp_commitment.to_public_inputs();
vec![
self.nf.inner(),
self.anchor,
self.cm,
self.anchor.inner(),
self.cm.inner(),
self.cv_net.get_x(),
self.cv_net.get_y(),
input_vp_commitment[0],
Expand All @@ -68,12 +68,11 @@ impl ActionInstance {
}

#[cfg(feature = "borsh")]
impl BorshSerialize for ActionInstance {
impl BorshSerialize for ActionPublicInputs {
fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
use ff::PrimeField;
writer.write_all(&self.anchor.to_repr())?;
writer.write_all(&self.anchor.to_bytes())?;
writer.write_all(&self.nf.to_bytes())?;
writer.write_all(&self.cm.to_repr())?;
writer.write_all(&self.cm.to_bytes())?;
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 @@ -82,18 +81,17 @@ impl BorshSerialize for ActionInstance {
}

#[cfg(feature = "borsh")]
impl BorshDeserialize for ActionInstance {
impl BorshDeserialize for ActionPublicInputs {
fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
use ff::PrimeField;
use std::io;
let anchor_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let anchor = Option::from(pallas::Base::from_repr(anchor_bytes))
let anchor = Option::from(Anchor::from_bytes(anchor_bytes))
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "anchor not in field"))?;
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_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let cm = Option::from(pallas::Base::from_repr(cm_bytes))
let cm = Option::from(NoteCommitment::from_bytes(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))
Expand All @@ -105,7 +103,7 @@ impl BorshDeserialize for ActionInstance {
let output_vp_commitment =
ValidityPredicateCommitment::from_bytes(output_vp_commitment_bytes);

Ok(ActionInstance {
Ok(ActionPublicInputs {
anchor,
nf,
cm,
Expand Down Expand Up @@ -160,17 +158,17 @@ impl ActionInfo {
self.rseed.get_vp_cm_r(PRF_EXPAND_OUTPUT_VP_CM_R)
}

pub fn build(&self) -> (ActionInstance, ActionCircuit) {
pub fn build(&self) -> (ActionPublicInputs, ActionCircuit) {
let nf = self.input_note.get_nf().unwrap();
assert_eq!(
nf, self.output_note.rho,
"The nf of input note should be equal to the rho of output note"
);

let cm = self.output_note.commitment().inner();
let cm = self.output_note.commitment();
let anchor = {
let cm_node = Node::from_note(&self.input_note);
self.input_merkle_path.root(cm_node).inner()
let cm_node = Node::from(&self.input_note);
self.input_merkle_path.root(cm_node)
};

let rcv = self.get_rcv();
Expand All @@ -184,7 +182,7 @@ impl ActionInfo {
let output_vp_commitment =
ValidityPredicateCommitment::commit(&self.output_note.get_app_vk(), &output_vp_cm_r);

let action = ActionInstance {
let action = ActionPublicInputs {
nf,
cm,
anchor,
Expand Down
4 changes: 2 additions & 2 deletions taiga_halo2/src/circuit/merkle_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ fn test_halo2_merkle_circuit() {
)?;

let expected_root = {
let root = self.merkle_path.root(Node::new(self.leaf)).inner();
let root = self.merkle_path.root(Node::from(self.leaf));
assign_free_advice(
layouter.namespace(|| "witness leaf"),
config.advices[0],
Value::known(root),
Value::known(root.inner()),
)?
};
layouter.assign_region(
Expand Down
13 changes: 7 additions & 6 deletions taiga_halo2/src/circuit/vp_circuit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::circuit::vamp_ir_utils::{get_circuit_assignments, parse, VariableAssignmentError};
use crate::{
circuit::{
blake2s::publicize_default_dynamic_vp_commitments,
Expand All @@ -14,6 +13,7 @@ use crate::{
target_note_variable::{GetIsInputNoteFlagConfig, GetOwnedNoteVariableConfig},
},
integrity::{check_input_note, check_output_note},
vamp_ir_utils::{get_circuit_assignments, parse, VariableAssignmentError},
},
constant::{
TaigaFixedBases, NOTE_ENCRYPTION_CIPHERTEXT_NUM, NUM_NOTE, SETUP_PARAMS_MAP,
Expand All @@ -24,14 +24,13 @@ use crate::{
VP_CIRCUIT_OWNED_NOTE_PUB_ID_PUBLIC_INPUT_IDX, VP_CIRCUIT_PARAMS_SIZE,
VP_CIRCUIT_PUBLIC_INPUT_NUM,
},
note::{Note, RandomSeed},
note::{Note, NoteCommitment, RandomSeed},
note_encryption::{NoteCiphertext, SecretKey},
proof::Proof,
utils::mod_r_p,
vp_vk::ValidityPredicateVerifyingKey,
};
use dyn_clone::{clone_trait_object, DynClone};
//use ff::PrimeField;
use group::cofactor::CofactorCurveAffine;
use halo2_gadgets::{
ecc::chip::EccChip,
Expand Down Expand Up @@ -165,12 +164,14 @@ impl VPVerifyingInfo {
]
}

pub fn get_note_commitments(&self) -> [pallas::Base; NUM_NOTE] {
pub fn get_note_commitments(&self) -> [NoteCommitment; NUM_NOTE] {
[
self.public_inputs
.get_from_index(VP_CIRCUIT_OUTPUT_CM_ONE_PUBLIC_INPUT_IDX),
.get_from_index(VP_CIRCUIT_OUTPUT_CM_ONE_PUBLIC_INPUT_IDX)
.into(),
self.public_inputs
.get_from_index(VP_CIRCUIT_OUTPUT_CM_TWO_PUBLIC_INPUT_IDX),
.get_from_index(VP_CIRCUIT_OUTPUT_CM_TWO_PUBLIC_INPUT_IDX)
.into(),
]
}

Expand Down
2 changes: 1 addition & 1 deletion taiga_halo2/src/circuit/vp_examples/cascade_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn test_halo2_cascade_intent_vp_circuit() {
let circuit = {
let cascade_input_note = random_input_note(&mut rng);
let cascade_note_cm = cascade_input_note.commitment().inner();
let rho = Nullifier::new(pallas::Base::random(&mut rng));
let rho = Nullifier::from(pallas::Base::random(&mut rng));
let nk = NullifierKeyContainer::random_key(&mut rng);
let intent_note = create_intent_note(&mut rng, cascade_note_cm, rho, nk);
let input_notes = [intent_note, cascade_input_note];
Expand Down
2 changes: 1 addition & 1 deletion taiga_halo2/src/circuit/vp_examples/or_relation_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ fn test_halo2_or_relation_intent_vp_circuit() {
transfrom_token_name_to_token_property(&condition1.token_name);
output_notes[0].value = condition1.token_value;

let rho = Nullifier::new(pallas::Base::random(&mut rng));
let rho = Nullifier::from(pallas::Base::random(&mut rng));
let nk = NullifierKeyContainer::random_key(&mut rng);
let nk_com = output_notes[0].get_nk_commitment();
let intent_note = create_intent_note(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ fn test_halo2_partial_fulfillment_intent_vp_circuit() {
sold_note.note_type.app_data_static = transfrom_token_name_to_token_property(&sell.name);
sold_note.value = sell.value;
let receiver_nk_com = sold_note.get_nk_commitment();
let rho = Nullifier::new(pallas::Base::random(&mut rng));
let rho = Nullifier::from(pallas::Base::random(&mut rng));
let nk = NullifierKeyContainer::random_key(&mut rng);
let intent_note = create_intent_note(
&mut rng,
Expand Down
10 changes: 6 additions & 4 deletions taiga_halo2/src/executable.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::{error::TransactionError, nullifier::Nullifier, value_commitment::ValueCommitment};
use pasta_curves::pallas;
use crate::{
error::TransactionError, merkle_tree::Anchor, note::NoteCommitment, nullifier::Nullifier,
value_commitment::ValueCommitment,
};

// Executable is an unified interface for partial transaction, which is the atomic executable uinit.
pub trait Executable {
fn execute(&self) -> Result<(), TransactionError>;
fn get_nullifiers(&self) -> Vec<Nullifier>;
fn get_output_cms(&self) -> Vec<pallas::Base>;
fn get_output_cms(&self) -> Vec<NoteCommitment>;
fn get_value_commitments(&self) -> Vec<ValueCommitment>;
fn get_anchors(&self) -> Vec<pallas::Base>;
fn get_anchors(&self) -> Vec<Anchor>;
}
Loading

0 comments on commit 1ebcf08

Please sign in to comment.