Skip to content

Commit

Permalink
update the token exmaple
Browse files Browse the repository at this point in the history
  • Loading branch information
XuyangSong committed Aug 26, 2024
1 parent 1a76750 commit 5452c0a
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 555 deletions.
2 changes: 1 addition & 1 deletion taiga_halo2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ name = "tx_examples"
required-features = ["examples"]

[features]
default = ["serde", "examples"]
default = ["serde"]
nif = ["dep:rustler", "borsh", "pasta_curves/repr-erlang"]
serde = ["dep:serde", "pasta_curves/serde"]
borsh = ["dep:borsh"]
Expand Down
12 changes: 6 additions & 6 deletions taiga_halo2/examples/tx_examples/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod partial_fulfillment_token_swap;
// mod partial_fulfillment_token_swap;
mod token;
mod token_swap_with_intent;
// mod token_swap_with_intent;
mod token_swap_without_intent;
fn main() {
use rand::rngs::OsRng;
Expand All @@ -9,9 +9,9 @@ fn main() {
let tx = token_swap_without_intent::create_token_swap_transaction(rng);
tx.execute().unwrap();

let tx = token_swap_with_intent::create_token_swap_intent_transaction(rng);
tx.execute().unwrap();
// let tx = token_swap_with_intent::create_token_swap_intent_transaction(rng);
// tx.execute().unwrap();

let tx = partial_fulfillment_token_swap::create_token_swap_transaction(rng);
tx.execute().unwrap();
// let tx = partial_fulfillment_token_swap::create_token_swap_transaction(rng);
// tx.execute().unwrap();
}
84 changes: 27 additions & 57 deletions taiga_halo2/examples/tx_examples/token.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use halo2_proofs::arithmetic::Field;

use pasta_curves::pallas;
use rand::RngCore;

Expand All @@ -10,8 +8,8 @@ use taiga_halo2::{
},
compliance::ComplianceInfo,
constant::TAIGA_COMMITMENT_TREE_DEPTH,
merkle_tree::{Anchor, MerklePath},
resource::{Resource, ResourceLogics},
merkle_tree::MerklePath,
resource_tree::ResourceMerkleTreeLeaves,
shielded_ptx::ShieldedPartialTransaction,
};

Expand All @@ -36,10 +34,6 @@ pub fn create_token_swap_ptx<R: RngCore>(
let mut output_resource =
output_token.create_random_output_token_resource(&mut rng, output_npk, &output_auth);

// padding the zero resources
let padding_input_resource = Resource::random_padding_resource(&mut rng);
let mut padding_output_resource = Resource::random_padding_resource(&mut rng);

// Generate proving info
let merkle_path = MerklePath::random(&mut rng, TAIGA_COMMITMENT_TREE_DEPTH);

Expand All @@ -53,66 +47,42 @@ pub fn create_token_swap_ptx<R: RngCore>(
&mut rng,
);

// Fetch a valid anchor for padding input resources
let anchor = Anchor::from(pallas::Base::random(&mut rng));
let compliance_2 = ComplianceInfo::new(
padding_input_resource,
merkle_path,
Some(anchor),
&mut padding_output_resource,
&mut rng,
);
vec![compliance_1, compliance_2]
vec![compliance_1]
};

// Create resource logics
let (input_resource_logics, output_resource_logics) = {
let input_resources = [*input_resource.resource(), padding_input_resource];
let output_resources = [*output_resource.resource(), padding_output_resource];
// Create resource_logics for the input token
let input_token_resource_logics = input_resource.generate_input_token_resource_logics(
// Collect resource merkle leaves
let input_resource_nf_1 = input_resource.get_nf().unwrap().inner();
let output_resource_cm_1 = output_resource.commitment().inner();
let resource_merkle_tree =
ResourceMerkleTreeLeaves::new(vec![input_resource_nf_1, output_resource_cm_1]);

// Create resource logics for the input resource

let input_token_resource_logics = {
let merkle_path = resource_merkle_tree
.generate_path(input_resource_nf_1)
.unwrap();
input_resource.generate_input_token_resource_logics(
&mut rng,
input_auth,
input_auth_sk,
input_resources,
output_resources,
);

// Create resource logics for the output token
let output_token_resource_logics = output_resource.generate_output_token_resource_logics(
&mut rng,
output_auth,
input_resources,
output_resources,
);

// Create resource logics for the padding input
let padding_input_resource_logics =
ResourceLogics::create_input_padding_resource_resource_logics(
&padding_input_resource,
input_resources,
output_resources,
);

// Create resource logics for the padding output
let padding_output_resource_logics =
ResourceLogics::create_output_padding_resource_resource_logics(
&padding_output_resource,
input_resources,
output_resources,
);

(
vec![input_token_resource_logics, padding_input_resource_logics],
vec![output_token_resource_logics, padding_output_resource_logics],
merkle_path,
)
};

// Create resource logics for the output resource
let output_token_resource_logics = {
let merkle_path = resource_merkle_tree
.generate_path(output_resource_cm_1)
.unwrap();
output_resource.generate_output_token_resource_logics(&mut rng, output_auth, merkle_path)
};

// Create shielded partial tx
ShieldedPartialTransaction::build(
compliances,
input_resource_logics,
output_resource_logics,
vec![input_token_resource_logics],
vec![output_token_resource_logics],
vec![],
&mut rng,
)
Expand Down
48 changes: 24 additions & 24 deletions taiga_halo2/src/circuit/resource_logic_bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use crate::circuit::resource_logic_examples::TrivialResourceLogicCircuit;
#[cfg(feature = "examples")]
use crate::circuit::resource_logic_examples::{
or_relation_intent::OrRelationIntentResourceLogicCircuit,
partial_fulfillment_intent::PartialFulfillmentIntentResourceLogicCircuit,
// or_relation_intent::OrRelationIntentResourceLogicCircuit,
// partial_fulfillment_intent::PartialFulfillmentIntentResourceLogicCircuit,
receiver_resource_logic::ReceiverResourceLogicCircuit,
signature_verification::SignatureVerificationResourceLogicCircuit,
token::TokenResourceLogicCircuit,
Expand Down Expand Up @@ -101,17 +101,17 @@ impl ResourceLogicByteCode {
let resource_logic = ReceiverResourceLogicCircuit::from_bytes(&self.inputs);
Ok(resource_logic.get_verifying_info())
}
#[cfg(feature = "examples")]
ResourceLogicRepresentation::PartialFulfillmentIntent => {
let resource_logic =
PartialFulfillmentIntentResourceLogicCircuit::from_bytes(&self.inputs);
Ok(resource_logic.get_verifying_info())
}
#[cfg(feature = "examples")]
ResourceLogicRepresentation::OrRelationIntent => {
let resource_logic = OrRelationIntentResourceLogicCircuit::from_bytes(&self.inputs);
Ok(resource_logic.get_verifying_info())
}
// #[cfg(feature = "examples")]
// ResourceLogicRepresentation::PartialFulfillmentIntent => {
// let resource_logic =
// PartialFulfillmentIntentResourceLogicCircuit::from_bytes(&self.inputs);
// Ok(resource_logic.get_verifying_info())
// }
// #[cfg(feature = "examples")]
// ResourceLogicRepresentation::OrRelationIntent => {
// let resource_logic = OrRelationIntentResourceLogicCircuit::from_bytes(&self.inputs);
// Ok(resource_logic.get_verifying_info())
// }
#[allow(unreachable_patterns)]
_ => Err(TransactionError::InvalidResourceLogicRepresentation),
}
Expand Down Expand Up @@ -157,17 +157,17 @@ impl ResourceLogicByteCode {
let resource_logic = ReceiverResourceLogicCircuit::from_bytes(&self.inputs);
resource_logic.verify_transparently()?
}
#[cfg(feature = "examples")]
ResourceLogicRepresentation::PartialFulfillmentIntent => {
let resource_logic =
PartialFulfillmentIntentResourceLogicCircuit::from_bytes(&self.inputs);
resource_logic.verify_transparently()?
}
#[cfg(feature = "examples")]
ResourceLogicRepresentation::OrRelationIntent => {
let resource_logic = OrRelationIntentResourceLogicCircuit::from_bytes(&self.inputs);
resource_logic.verify_transparently()?
}
// #[cfg(feature = "examples")]
// ResourceLogicRepresentation::PartialFulfillmentIntent => {
// let resource_logic =
// PartialFulfillmentIntentResourceLogicCircuit::from_bytes(&self.inputs);
// resource_logic.verify_transparently()?
// }
// #[cfg(feature = "examples")]
// ResourceLogicRepresentation::OrRelationIntent => {
// let resource_logic = OrRelationIntentResourceLogicCircuit::from_bytes(&self.inputs);
// resource_logic.verify_transparently()?
// }
#[allow(unreachable_patterns)]
_ => return Err(TransactionError::InvalidResourceLogicRepresentation),
};
Expand Down
10 changes: 5 additions & 5 deletions taiga_halo2/src/circuit/resource_logic_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ mod field_addition;
// pub mod or_relation_intent;
// #[cfg(feature = "examples")]
// pub mod partial_fulfillment_intent;
// #[cfg(feature = "examples")]
// pub mod receiver_resource_logic;
// #[cfg(feature = "examples")]
// pub mod signature_verification;
// #[cfg(feature = "examples")]
#[cfg(feature = "examples")]
pub mod receiver_resource_logic;
#[cfg(feature = "examples")]
pub mod signature_verification;
#[cfg(feature = "examples")]
pub mod token;

lazy_static! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
constant::{RESOURCE_LOGIC_CIRCUIT_CUSTOM_PUBLIC_INPUT_BEGIN_IDX, SETUP_PARAMS_MAP},
error::TransactionError,
proof::Proof,
resource::{RandomSeed, Resource},
resource::RandomSeed,
resource_logic_commitment::ResourceLogicCommitment,
resource_logic_vk::ResourceLogicVerifyingKey,
resource_tree::ResourceExistenceWitness,
Expand Down
Loading

0 comments on commit 5452c0a

Please sign in to comment.