diff --git a/taiga_halo2/Cargo.toml b/taiga_halo2/Cargo.toml index 9de5e1f5..4ba7da56 100644 --- a/taiga_halo2/Cargo.toml +++ b/taiga_halo2/Cargo.toml @@ -36,7 +36,7 @@ name = "compliance_proof" harness = false [[bench]] -name = "vp_proof" +name = "resource_logic_proof" harness = false # [[example]] diff --git a/taiga_halo2/benches/Perfromance.md b/taiga_halo2/benches/Perfromance.md index e1cc150c..c233663a 100644 --- a/taiga_halo2/benches/Perfromance.md +++ b/taiga_halo2/benches/Perfromance.md @@ -4,10 +4,10 @@ compliance-proof-prover time: [3.6500 s 3.1445 s 3.7210 s] compliance-proof-verifier time: [35.858 ms 36.359 ms 36.873 ms] ``` -# VP proof performance +# ResourceLogic proof performance ``` -vp-proof-prover time: [2.2098 s 2.2328 s 2.2579 s] -vp-proof-verifier time: [34.580 ms 35.075 ms 35.585 ms] +resource-logic-proof-prover time: [2.2098 s 2.2328 s 2.2579 s] +resource-logic-proof-verifier time: [34.580 ms 35.075 ms 35.585 ms] ``` # Verifier proof performance \ No newline at end of file diff --git a/taiga_halo2/benches/vp_proof.rs b/taiga_halo2/benches/resource_logic_proof.rs similarity index 81% rename from taiga_halo2/benches/vp_proof.rs rename to taiga_halo2/benches/resource_logic_proof.rs index cf952dd7..0de26f3e 100644 --- a/taiga_halo2/benches/vp_proof.rs +++ b/taiga_halo2/benches/resource_logic_proof.rs @@ -6,17 +6,20 @@ use pasta_curves::pallas; use rand::rngs::OsRng; use rand::Rng; use taiga_halo2::{ - circuit::{vp_circuit::ValidityPredicateCircuit, vp_examples::TrivialValidityPredicateCircuit}, - constant::{NUM_RESOURCE, SETUP_PARAMS_MAP, VP_CIRCUIT_PARAMS_SIZE}, + circuit::{ + resource_logic_circuit::ResourceLogicCircuit, + resource_logic_examples::TrivialResourceLogicCircuit, + }, + constant::{NUM_RESOURCE, RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE, SETUP_PARAMS_MAP}, nullifier::{Nullifier, NullifierKeyContainer}, proof::Proof, resource::{Resource, ResourceKind}, }; -fn bench_vp_proof(name: &str, c: &mut Criterion) { +fn bench_resource_logic_proof(name: &str, c: &mut Criterion) { let mut rng = OsRng; - let vp_circuit = { + let resource_logic_circuit = { let input_resources = [(); NUM_RESOURCE].map(|_| { let nonce = Nullifier::from(pallas::Base::random(&mut rng)); let nk = NullifierKeyContainer::from_key(pallas::Base::random(&mut rng)); @@ -63,17 +66,19 @@ fn bench_vp_proof(name: &str, c: &mut Criterion) { }) .collect::>(); let owned_resource_id = input_resources[0].get_nf().unwrap().inner(); - TrivialValidityPredicateCircuit::new( + TrivialResourceLogicCircuit::new( owned_resource_id, input_resources, output_resources.try_into().unwrap(), ) }; - let params = SETUP_PARAMS_MAP.get(&VP_CIRCUIT_PARAMS_SIZE).unwrap(); - let empty_circuit: TrivialValidityPredicateCircuit = Default::default(); + let params = SETUP_PARAMS_MAP + .get(&RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE) + .unwrap(); + let empty_circuit: TrivialResourceLogicCircuit = Default::default(); let vk = keygen_vk(params, &empty_circuit).expect("keygen_vk should not fail"); let pk = keygen_pk(params, vk, &empty_circuit).expect("keygen_pk should not fail"); - let public_inputs = vp_circuit.get_public_inputs(&mut rng); + let public_inputs = resource_logic_circuit.get_public_inputs(&mut rng); // Prover bench let prover_name = name.to_string() + "-prover"; @@ -82,7 +87,7 @@ fn bench_vp_proof(name: &str, c: &mut Criterion) { Proof::create( &pk, ¶ms, - vp_circuit.clone(), + resource_logic_circuit.clone(), &[public_inputs.inner()], &mut rng, ) @@ -95,7 +100,7 @@ fn bench_vp_proof(name: &str, c: &mut Criterion) { let proof = Proof::create( &pk, ¶ms, - vp_circuit.clone(), + resource_logic_circuit.clone(), &[public_inputs.inner()], &mut rng, ) @@ -111,7 +116,7 @@ fn bench_vp_proof(name: &str, c: &mut Criterion) { }); } fn criterion_benchmark(c: &mut Criterion) { - bench_vp_proof("halo2-vp-proof", c); + bench_resource_logic_proof("halo2-resource-logic-proof", c); } criterion_group!(benches, criterion_benchmark); diff --git a/taiga_halo2/deprecated/simple_sudoku/main.rs b/taiga_halo2/deprecated/simple_sudoku/main.rs index e64fbc7d..418b29ef 100644 --- a/taiga_halo2/deprecated/simple_sudoku/main.rs +++ b/taiga_halo2/deprecated/simple_sudoku/main.rs @@ -1,4 +1,4 @@ pub mod circuit; -pub mod vp; +pub mod resource_logic; pub fn main() {} diff --git a/taiga_halo2/deprecated/simple_sudoku/vp.rs b/taiga_halo2/deprecated/simple_sudoku/resource_logic.rs similarity index 73% rename from taiga_halo2/deprecated/simple_sudoku/vp.rs rename to taiga_halo2/deprecated/simple_sudoku/resource_logic.rs index 76afc3d2..0da249b4 100644 --- a/taiga_halo2/deprecated/simple_sudoku/vp.rs +++ b/taiga_halo2/deprecated/simple_sudoku/resource_logic.rs @@ -8,47 +8,47 @@ extern crate taiga_halo2; use taiga_halo2::{ circuit::{ resource_circuit::ResourceConfig, - vp_circuit::{ - BasicValidityPredicateVariables, VPVerifyingInfo, ValidityPredicateCircuit, - ValidityPredicateConfig, ValidityPredicateInfo, ValidityPredicatePublicInputs, - ValidityPredicateVerifyingInfo, + resource_logic_circuit::{ + BasicResourceLogicVariables, ResourceLogicVerifyingInfoTrait, ResourceLogicCircuit, + ResourceLogicConfig, ResourceLogicInfo, ResourceLogicPublicInputs, + ResourceLogicVerifyingInfo, }, }, constant::{NUM_RESOURCE, SETUP_PARAMS_MAP}, resource::{Resource, RandomSeed}, proof::Proof, - vp_circuit_impl, - vp_vk::ValidityPredicateVerifyingKey, + resource_logic_circuit_impl, + resource_logic_vk::ResourceLogicVerifyingKey, }; use crate::circuit::{SudokuCircuit, SudokuConfig}; use rand::{rngs::OsRng, RngCore}; #[derive(Clone, Debug)] -pub struct SudokuVPConfig { +pub struct SudokuResourceLogicConfig { resource_config: ResourceConfig, sudoku_config: SudokuConfig, } #[derive(Clone, Debug, Default)] -pub struct SudokuVP { +pub struct SudokuResourceLogic { pub sudoku: SudokuCircuit, input_resources: [Resource; NUM_RESOURCE], output_resources: [Resource; NUM_RESOURCE], } -impl ValidityPredicateCircuit for SudokuVP { +impl ResourceLogicCircuit for SudokuResourceLogic { fn custom_constraints( &self, - config: ValidityPredicateConfig, + config: ResourceLogicConfig, layouter: impl Layouter, - _basic_variables: BasicValidityPredicateVariables, + _basic_variables: BasicResourceLogicVariables, ) -> Result<(), plonk::Error> { self.sudoku.synthesize(config.sudoku_config, layouter) } } -impl ValidityPredicateInfo for SudokuVP { +impl ResourceLogicInfo for SudokuResourceLogic { fn get_input_resources(&self) -> &[Resource; NUM_RESOURCE] { &self.input_resources } @@ -57,9 +57,9 @@ impl ValidityPredicateInfo for SudokuVP { &self.output_resources } - fn get_public_inputs(&self, mut rng: impl RngCore) -> ValidityPredicatePublicInputs { + fn get_public_inputs(&self, mut rng: impl RngCore) -> ResourceLogicPublicInputs { let mut public_inputs = self.get_mandatory_public_inputs(); - let padding = ValidityPredicatePublicInputs::get_public_input_padding( + let padding = ResourceLogicPublicInputs::get_public_input_padding( public_inputs.len(), &RandomSeed::random(&mut rng), ); @@ -72,7 +72,7 @@ impl ValidityPredicateInfo for SudokuVP { } } -impl SudokuVP { +impl SudokuResourceLogic { pub fn new( sudoku: SudokuCircuit, input_resources: [Resource; NUM_RESOURCE], @@ -86,7 +86,7 @@ impl SudokuVP { } } -vp_circuit_impl!(SudokuVP); +resource_logic_circuit_impl!(SudokuResourceLogic); #[cfg(test)] mod tests { @@ -94,7 +94,7 @@ mod tests { constant::NUM_RESOURCE, resource::{Resource, RandomSeed}, nullifier::{Nullifier, NullifierKeyContainer}, - vp_vk::ValidityPredicateVerifyingKey, + resource_logic_vk::ResourceLogicVerifyingKey, }; use ff::Field; @@ -103,10 +103,10 @@ mod tests { use halo2_proofs::{plonk, poly::commitment::Params}; - use crate::{circuit::SudokuCircuit, vp::SudokuVP}; + use crate::{circuit::SudokuCircuit, resource_logic::SudokuResourceLogic}; #[test] - fn test_vp() { + fn test_resource_logic() { let mut rng = OsRng; let input_resources = [(); NUM_RESOURCE].map(|_| Resource::dummy(&mut rng)); let output_resources = [(); NUM_RESOURCE].map(|_| Resource::dummy(&mut rng)); @@ -129,9 +129,9 @@ mod tests { let vk = plonk::keygen_vk(¶ms, &sudoku).unwrap(); - let mut _vp = SudokuVP::new(sudoku, input_resources, output_resources); + let mut _resource_logic = SudokuResourceLogic::new(sudoku, input_resources, output_resources); - let vp_vk = ValidityPredicateVerifyingKey::from_vk(vk); + let resource_logic_vk = ResourceLogicVerifyingKey::from_vk(vk); let app_data_static = pallas::Base::zero(); let app_data_dynamic = pallas::Base::zero(); @@ -141,7 +141,7 @@ mod tests { let rseed = RandomSeed::random(&mut rng); let rho = Nullifier::from(pallas::Base::random(&mut rng)); Resource::new( - vp_vk, + resource_logic_vk, app_data_static, app_data_dynamic, quantity, diff --git a/taiga_halo2/deprecated/taiga_sudoku/app_vp.rs b/taiga_halo2/deprecated/taiga_sudoku/app_resource_logic.rs similarity index 94% rename from taiga_halo2/deprecated/taiga_sudoku/app_vp.rs rename to taiga_halo2/deprecated/taiga_sudoku/app_resource_logic.rs index bc1aaee7..cbaa1a4f 100644 --- a/taiga_halo2/deprecated/taiga_sudoku/app_vp.rs +++ b/taiga_halo2/deprecated/taiga_sudoku/app_resource_logic.rs @@ -17,18 +17,18 @@ use taiga_halo2::{ triple_mul::TripleMulConfig, }, resource_circuit::ResourceConfig, - vp_circuit::{ - BasicValidityPredicateVariables, InputResourceVariables, OutputResourceVariables, - VPVerifyingInfo, ValidityPredicateCircuit, ValidityPredicateConfig, - ValidityPredicateInfo, ValidityPredicatePublicInputs, ValidityPredicateVerifyingInfo, + resource_logic_circuit::{ + BasicResourceLogicVariables, InputResourceVariables, OutputResourceVariables, + ResourceLogicVerifyingInfoTrait, ResourceLogicCircuit, ResourceLogicConfig, + ResourceLogicInfo, ResourceLogicPublicInputs, ResourceLogicVerifyingInfo, }, }, constant::{NUM_RESOURCE, SETUP_PARAMS_MAP}, resource::{Resource, RandomSeed}, proof::Proof, utils::poseidon_hash, - vp_circuit_impl, - vp_vk::ValidityPredicateVerifyingKey, + resource_logic_circuit_impl, + resource_logic_vk::ResourceLogicVerifyingKey, }; use crate::gadgets::{ @@ -106,7 +106,7 @@ impl Default for SudokuState { } #[derive(Clone, Debug, Default)] -struct SudokuAppValidityPredicateCircuit { +struct SudokuAppResourceLogicCircuit { owned_resource_id: pallas::Base, input_resources: [Resource; NUM_RESOURCE], output_resources: [Resource; NUM_RESOURCE], @@ -118,7 +118,7 @@ struct SudokuAppValidityPredicateCircuit { } #[derive(Clone, Debug)] -struct SudokuAppValidityPredicateConfig { +struct SudokuAppResourceLogicConfig { resource_config: ResourceConfig, advices: [Column; 10], get_is_input_resource_flag_config: GetIsInputResourceFlagConfig, @@ -130,7 +130,7 @@ struct SudokuAppValidityPredicateConfig { mul_config: MulConfig, } -impl SudokuAppValidityPredicateConfig { +impl SudokuAppResourceLogicConfig { pub fn sub_chip(&self) -> SubChip { SubChip::construct(self.sub_config.clone(), ()) } @@ -140,7 +140,7 @@ impl SudokuAppValidityPredicateConfig { } } -impl ValidityPredicateConfig for SudokuAppValidityPredicateConfig { +impl ResourceLogicConfig for SudokuAppResourceLogicConfig { fn get_resource_config(&self) -> ResourceConfig { self.resource_config.clone() } @@ -176,12 +176,12 @@ impl ValidityPredicateConfig for SudokuAppValidityPredicateConfig { } } -impl SudokuAppValidityPredicateCircuit { +impl SudokuAppResourceLogicCircuit { // Copy from valid_puzzle/circuit.rs #[allow(clippy::too_many_arguments)] fn check_puzzle( mut layouter: impl Layouter, - config: &SudokuAppValidityPredicateConfig, + config: &SudokuAppResourceLogicConfig, // advice: Column, state: &[AssignedCell], ) -> Result<(), Error> { @@ -434,7 +434,7 @@ impl SudokuAppValidityPredicateCircuit { } } -impl ValidityPredicateInfo for SudokuAppValidityPredicateCircuit { +impl ResourceLogicInfo for SudokuAppResourceLogicCircuit { fn get_input_resources(&self) -> &[Resource; NUM_RESOURCE] { &self.input_resources } @@ -443,9 +443,9 @@ impl ValidityPredicateInfo for SudokuAppValidityPredicateCircuit { &self.output_resources } - fn get_public_inputs(&self, mut rng: impl RngCore) -> ValidityPredicatePublicInputs { + fn get_public_inputs(&self, mut rng: impl RngCore) -> ResourceLogicPublicInputs { let mut public_inputs = self.get_mandatory_public_inputs(); - let padding = ValidityPredicatePublicInputs::get_public_input_padding( + let padding = ResourceLogicPublicInputs::get_public_input_padding( public_inputs.len(), &RandomSeed::random(&mut rng), ); @@ -458,14 +458,14 @@ impl ValidityPredicateInfo for SudokuAppValidityPredicateCircuit { } } -impl ValidityPredicateCircuit for SudokuAppValidityPredicateCircuit { - type VPConfig = SudokuAppValidityPredicateConfig; +impl ResourceLogicCircuit for SudokuAppResourceLogicCircuit { + type ResourceLogicConfig = SudokuAppResourceLogicConfig; // Add custom constraints fn custom_constraints( &self, - config: Self::VPConfig, + config: Self::ResourceLogicConfig, mut layouter: impl Layouter, - basic_variables: BasicValidityPredicateVariables, + basic_variables: BasicResourceLogicVariables, ) -> Result<(), Error> { let owned_resource_id = basic_variables.get_owned_resource_id(); let is_input_resource = get_is_input_resource_flag( @@ -589,7 +589,7 @@ impl ValidityPredicateCircuit for SudokuAppValidityPredicateCircuit { } } -vp_circuit_impl!(SudokuAppValidityPredicateCircuit); +resource_logic_circuit_impl!(SudokuAppResourceLogicCircuit); #[cfg(test)] pub mod tests { @@ -648,8 +648,8 @@ pub mod tests { } #[test] -fn test_halo2_sudoku_app_vp_circuit_init() { - use crate::app_vp::tests::{random_input_resource, random_output_resource}; +fn test_halo2_sudoku_app_resource_logic_circuit_init() { + use crate::app_resource_logic::tests::{random_input_resource, random_output_resource}; use halo2_proofs::dev::MockProver; use rand::rngs::OsRng; @@ -667,7 +667,7 @@ fn test_halo2_sudoku_app_vp_circuit_init() { poseidon_hash(encoded_init_state, current_state.encode()); output_resources[0].quantity = 1u64; let owned_resource_id = output_resources[0].commitment().inner(); - SudokuAppValidityPredicateCircuit { + SudokuAppResourceLogicCircuit { owned_resource_id, input_resources, output_resources: output_resources.try_into().unwrap(), @@ -684,8 +684,8 @@ fn test_halo2_sudoku_app_vp_circuit_init() { } #[test] -fn test_halo2_sudoku_app_vp_circuit_update() { - use crate::app_vp::tests::{random_input_resource, random_output_resource}; +fn test_halo2_sudoku_app_resource_logic_circuit_update() { + use crate::app_resource_logic::tests::{random_input_resource, random_output_resource}; use halo2_proofs::dev::MockProver; use rand::rngs::OsRng; @@ -744,7 +744,7 @@ fn test_halo2_sudoku_app_vp_circuit_update() { poseidon_hash(encoded_init_state, current_state.encode()); output_resources[0].quantity = 1u64; output_resources[0].kind.app_vk = input_resources[0].kind.app_vk; - SudokuAppValidityPredicateCircuit { + SudokuAppResourceLogicCircuit { owned_resource_id: input_resources[0].get_nf().unwrap().inner(), input_resources, output_resources: output_resources.try_into().unwrap(), @@ -761,8 +761,8 @@ fn test_halo2_sudoku_app_vp_circuit_update() { } #[test] -fn halo2_sudoku_app_vp_circuit_final() { - use crate::app_vp::tests::{random_input_resource, random_output_resource}; +fn halo2_sudoku_app_resource_logic_circuit_final() { + use crate::app_resource_logic::tests::{random_input_resource, random_output_resource}; use halo2_proofs::dev::MockProver; let mut rng = OsRng; @@ -820,7 +820,7 @@ fn halo2_sudoku_app_vp_circuit_final() { poseidon_hash(encoded_init_state, current_state.encode()); output_resources[0].quantity = 0u64; output_resources[0].kind.app_vk = input_resources[0].kind.app_vk; - SudokuAppValidityPredicateCircuit { + SudokuAppResourceLogicCircuit { owned_resource_id: input_resources[0].get_nf().unwrap().inner(), input_resources, output_resources: output_resources.try_into().unwrap(), diff --git a/taiga_halo2/deprecated/taiga_sudoku/dealer_intent_app_vp.rs b/taiga_halo2/deprecated/taiga_sudoku/dealer_intent_app_resource_logc.rs similarity index 91% rename from taiga_halo2/deprecated/taiga_sudoku/dealer_intent_app_vp.rs rename to taiga_halo2/deprecated/taiga_sudoku/dealer_intent_app_resource_logc.rs index cd9df917..048b750a 100644 --- a/taiga_halo2/deprecated/taiga_sudoku/dealer_intent_app_vp.rs +++ b/taiga_halo2/deprecated/taiga_sudoku/dealer_intent_app_resource_logc.rs @@ -21,22 +21,22 @@ use taiga_halo2::{ }, }, resource_circuit::ResourceConfig, - vp_circuit::{ - BasicValidityPredicateVariables, OutputResourceVariables, VPVerifyingInfo, - ValidityPredicateCircuit, ValidityPredicateConfig, ValidityPredicateInfo, - ValidityPredicatePublicInputs, ValidityPredicateVerifyingInfo, + resource_logic_circuit::{ + BasicResourceLogicVariables, OutputResourceVariables, ResourceLogicVerifyingInfoTrait, + ResourceLogicCircuit, ResourceLogicConfig, ResourceLogicInfo, + ResourceLogicPublicInputs, ResourceLogicVerifyingInfo, }, }, constant::{NUM_RESOURCE, SETUP_PARAMS_MAP}, resource::{Resource, RandomSeed}, proof::Proof, utils::poseidon_hash, - vp_circuit_impl, - vp_vk::ValidityPredicateVerifyingKey, + resource_logic_circuit_impl, + resource_logic_vk::ResourceLogicVerifyingKey, }; #[derive(Clone, Debug, Default)] -struct DealerIntentValidityPredicateCircuit { +struct DealerIntentResourceLogicCircuit { owned_resource_id: pallas::Base, input_resources: [Resource; NUM_RESOURCE], output_resources: [Resource; NUM_RESOURCE], @@ -47,7 +47,7 @@ struct DealerIntentValidityPredicateCircuit { } #[derive(Clone, Debug)] -struct IntentAppValidityPredicateConfig { +struct IntentAppResourceLogicConfig { resource_config: ResourceConfig, advices: [Column; 10], get_owned_resource_variable_config: GetOwnedResourceVariableConfig, @@ -55,7 +55,7 @@ struct IntentAppValidityPredicateConfig { dealer_intent_check_config: DealerIntentCheckConfig, } -impl ValidityPredicateConfig for IntentAppValidityPredicateConfig { +impl ResourceLogicConfig for IntentAppResourceLogicConfig { fn get_resource_config(&self) -> ResourceConfig { self.resource_config.clone() } @@ -85,7 +85,7 @@ impl ValidityPredicateConfig for IntentAppValidityPredicateConfig { } } -impl DealerIntentValidityPredicateCircuit { +impl DealerIntentResourceLogicCircuit { #![allow(dead_code)] pub fn compute_app_data_static( encoded_puzzle: pallas::Base, @@ -96,7 +96,7 @@ impl DealerIntentValidityPredicateCircuit { fn dealer_intent_check( &self, - config: &IntentAppValidityPredicateConfig, + config: &IntentAppResourceLogicConfig, mut layouter: impl Layouter, is_input_resource: &AssignedCell, encoded_puzzle: &AssignedCell, @@ -134,7 +134,7 @@ impl DealerIntentValidityPredicateCircuit { } } -impl ValidityPredicateInfo for DealerIntentValidityPredicateCircuit { +impl ResourceLogicInfo for DealerIntentResourceLogicCircuit { fn get_input_resources(&self) -> &[Resource; NUM_RESOURCE] { &self.input_resources } @@ -143,9 +143,9 @@ impl ValidityPredicateInfo for DealerIntentValidityPredicateCircuit { &self.output_resources } - fn get_public_inputs(&self, mut rng: impl RngCore) -> ValidityPredicatePublicInputs { + fn get_public_inputs(&self, mut rng: impl RngCore) -> ResourceLogicPublicInputs { let mut public_inputs = self.get_mandatory_public_inputs(); - let padding = ValidityPredicatePublicInputs::get_public_input_padding( + let padding = ResourceLogicPublicInputs::get_public_input_padding( public_inputs.len(), &RandomSeed::random(&mut rng), ); @@ -158,14 +158,14 @@ impl ValidityPredicateInfo for DealerIntentValidityPredicateCircuit { } } -impl ValidityPredicateCircuit for DealerIntentValidityPredicateCircuit { - type VPConfig = IntentAppValidityPredicateConfig; +impl ResourceLogicCircuit for DealerIntentResourceLogicCircuit { + type ResourceLogicConfig = IntentAppResourceLogicConfig; // Add custom constraints fn custom_constraints( &self, - config: Self::VPConfig, + config: Self::ResourceLogicConfig, mut layouter: impl Layouter, - basic_variables: BasicValidityPredicateVariables, + basic_variables: BasicResourceLogicVariables, ) -> Result<(), Error> { let owned_resource_id = basic_variables.get_owned_resource_id(); let is_input_resource = get_is_input_resource_flag( @@ -221,7 +221,7 @@ impl ValidityPredicateCircuit for DealerIntentValidityPredicateCircuit { } } -vp_circuit_impl!(DealerIntentValidityPredicateCircuit); +resource_logic_circuit_impl!(DealerIntentResourceLogicCircuit); #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct DealerIntentCheckConfig { @@ -352,8 +352,8 @@ impl DealerIntentCheckConfig { } #[test] -fn test_halo2_dealer_intent_vp_circuit() { - use crate::app_vp::tests::{random_input_resource, random_output_resource}; +fn test_halo2_dealer_intent_resource_logic_circuit() { + use crate::app_resource_logic::tests::{random_input_resource, random_output_resource}; use halo2_proofs::dev::MockProver; use rand::rngs::OsRng; @@ -367,13 +367,13 @@ fn test_halo2_dealer_intent_vp_circuit() { let encoded_puzzle = pallas::Base::random(&mut rng); let sudoku_app_vk = pallas::Base::random(&mut rng); output_resources[0].kind.app_data_static = - DealerIntentValidityPredicateCircuit::compute_app_data_static( + DealerIntentResourceLogicCircuit::compute_app_data_static( encoded_puzzle, sudoku_app_vk, ); let encoded_solution = pallas::Base::random(&mut rng); let owned_resource_id = output_resources[0].commitment().inner(); - DealerIntentValidityPredicateCircuit { + DealerIntentResourceLogicCircuit { owned_resource_id, input_resources, output_resources: output_resources.try_into().unwrap(), diff --git a/taiga_halo2/deprecated/taiga_sudoku/main.rs b/taiga_halo2/deprecated/taiga_sudoku/main.rs index db685a04..3f10c7f8 100644 --- a/taiga_halo2/deprecated/taiga_sudoku/main.rs +++ b/taiga_halo2/deprecated/taiga_sudoku/main.rs @@ -1,4 +1,4 @@ -pub mod app_vp; -pub mod dealer_intent_app_vp; +pub mod app_resource_logic; +pub mod dealer_intent_app_resource_logic; pub mod gadgets; fn main() {} diff --git a/taiga_halo2/examples/tx_examples/cascaded_partial_transactions.rs b/taiga_halo2/examples/tx_examples/cascaded_partial_transactions.rs index b93879d3..c1204b96 100644 --- a/taiga_halo2/examples/tx_examples/cascaded_partial_transactions.rs +++ b/taiga_halo2/examples/tx_examples/cascaded_partial_transactions.rs @@ -5,15 +5,15 @@ use halo2_proofs::arithmetic::Field; use pasta_curves::pallas; use rand::{CryptoRng, RngCore}; use taiga_halo2::{ - circuit::vp_examples::{ - cascade_intent::{create_intent_resource, CascadeIntentValidityPredicateCircuit}, + circuit::resource_logic_examples::{ + cascade_intent::{create_intent_resource, CascadeIntentResourceLogicCircuit}, signature_verification::COMPRESSED_TOKEN_AUTH_VK, token::{Token, TokenAuthorization}, }, compliance::ComplianceInfo, constant::TAIGA_COMMITMENT_TREE_DEPTH, merkle_tree::{Anchor, MerklePath}, - resource::ResourceValidityPredicates, + resource::ResourceLogics, shielded_ptx::ShieldedPartialTransaction, transaction::{ShieldedPartialTxBundle, Transaction, TransparentPartialTxBundle}, }; @@ -77,58 +77,70 @@ pub fn create_transaction(mut rng: R) -> Transaction { vec![compliance_1, compliance_2] }; - // Create VPs - let (input_vps, output_vps) = { + // Create resource logics + let (input_resource_logics, output_resource_logics) = { let input_resources = [*input_resource_1.resource(), *input_resource_2.resource()]; let output_resources = [*output_resource_1.resource(), cascade_intent_resource]; - // Create the input resource_1 vps - let input_resource_1_vps = input_resource_1.generate_input_token_vps( - &mut rng, - alice_auth, - alice_auth_sk, - input_resources, - output_resources, - ); - - // Create the input resource_2 vps - let input_resource_2_vps = input_resource_2.generate_input_token_vps( - &mut rng, - alice_auth, - alice_auth_sk, - input_resources, - output_resources, - ); + // Create resource logics for the input resource_1 + let input_resource_1_resource_logics = input_resource_1 + .generate_input_token_resource_logics( + &mut rng, + alice_auth, + alice_auth_sk, + input_resources, + output_resources, + ); + + // Create resource logics for the input resource_2 + let input_resource_2_resource_logics = input_resource_2 + .generate_input_token_resource_logics( + &mut rng, + alice_auth, + alice_auth_sk, + input_resources, + output_resources, + ); - // Create the output resource_1 vps - let output_resource_1_vps = output_resource_1.generate_output_token_vps( - &mut rng, - bob_auth, - input_resources, - output_resources, - ); + // Create resource logics for the output resource_1 + let output_resource_1_resource_logics = output_resource_1 + .generate_output_token_resource_logics( + &mut rng, + bob_auth, + input_resources, + output_resources, + ); - // Create intent vps - let intent_vps = { - let intent_vp = CascadeIntentValidityPredicateCircuit { + // Create resource logics for the intent + let intent_resource_logics = { + let intent_resource_logic = CascadeIntentResourceLogicCircuit { owned_resource_id: cascade_intent_resource.commitment().inner(), input_resources, output_resources, cascade_resource_cm: cascade_intent_resource.get_label(), }; - ResourceValidityPredicates::new(Box::new(intent_vp), vec![]) + ResourceLogics::new(Box::new(intent_resource_logic), vec![]) }; ( - vec![input_resource_1_vps, input_resource_2_vps], - vec![output_resource_1_vps, intent_vps], + vec![ + input_resource_1_resource_logics, + input_resource_2_resource_logics, + ], + vec![output_resource_1_resource_logics, intent_resource_logics], ) }; // Create shielded partial tx - ShieldedPartialTransaction::build(compliances, input_vps, output_vps, vec![], &mut rng) - .unwrap() + ShieldedPartialTransaction::build( + compliances, + input_resource_logics, + output_resource_logics, + vec![], + &mut rng, + ) + .unwrap() }; // The second partial transaction: @@ -155,57 +167,69 @@ pub fn create_transaction(mut rng: R) -> Transaction { vec![compliance_1, compliance_2] }; - // Create VPs - let (input_vps, output_vps) = { + // Create resource logics + let (input_resource_logics, output_resource_logics) = { let input_resources = [cascade_intent_resource, *input_resource_3.resource()]; let output_resources = [*output_resource_2.resource(), *output_resource_3.resource()]; - // Create intent vps - let intent_vps = { - let intent_vp = CascadeIntentValidityPredicateCircuit { + // Create resource_logics for the intent + let intent_resource_logics = { + let intent_resource_logic = CascadeIntentResourceLogicCircuit { owned_resource_id: cascade_intent_resource.get_nf().unwrap().inner(), input_resources, output_resources, cascade_resource_cm: cascade_intent_resource.get_label(), }; - ResourceValidityPredicates::new(Box::new(intent_vp), vec![]) + ResourceLogics::new(Box::new(intent_resource_logic), vec![]) }; - // Create input resource_3 vps - let input_resource_3_vps = input_resource_3.generate_input_token_vps( - &mut rng, - alice_auth, - alice_auth_sk, - input_resources, - output_resources, - ); + // Create resource logics for the input resource_3 + let input_resource_3_resource_logics = input_resource_3 + .generate_input_token_resource_logics( + &mut rng, + alice_auth, + alice_auth_sk, + input_resources, + output_resources, + ); - // Create output resource_2 vps - let output_resource_2_vps = output_resource_2.generate_output_token_vps( - &mut rng, - bob_auth, - input_resources, - output_resources, - ); + // Create resource_logics for the output resource_2 + let output_resource_2_resource_logics = output_resource_2 + .generate_output_token_resource_logics( + &mut rng, + bob_auth, + input_resources, + output_resources, + ); - // Create output resource_3 vps - let output_resource_3_vps = output_resource_3.generate_output_token_vps( - &mut rng, - bob_auth, - input_resources, - output_resources, - ); + // Create resource_logics for the output resource_3 + let output_resource_3_resource_logics = output_resource_3 + .generate_output_token_resource_logics( + &mut rng, + bob_auth, + input_resources, + output_resources, + ); ( - vec![intent_vps, input_resource_3_vps], - vec![output_resource_2_vps, output_resource_3_vps], + vec![intent_resource_logics, input_resource_3_resource_logics], + vec![ + output_resource_2_resource_logics, + output_resource_3_resource_logics, + ], ) }; // Create shielded partial tx - ShieldedPartialTransaction::build(compliances, input_vps, output_vps, vec![], &mut rng) - .unwrap() + ShieldedPartialTransaction::build( + compliances, + input_resource_logics, + output_resource_logics, + vec![], + &mut rng, + ) + .unwrap() }; // Create the final transaction 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 e7f91e23..1e6adcbc 100644 --- a/taiga_halo2/examples/tx_examples/partial_fulfillment_token_swap.rs +++ b/taiga_halo2/examples/tx_examples/partial_fulfillment_token_swap.rs @@ -9,8 +9,8 @@ use halo2_proofs::arithmetic::Field; use pasta_curves::{group::Curve, pallas}; use rand::{CryptoRng, RngCore}; use taiga_halo2::{ - circuit::vp_examples::{ - partial_fulfillment_intent::{PartialFulfillmentIntentValidityPredicateCircuit, Swap}, + circuit::resource_logic_examples::{ + partial_fulfillment_intent::{PartialFulfillmentIntentResourceLogicCircuit, Swap}, signature_verification::COMPRESSED_TOKEN_AUTH_VK, token::{Token, TokenAuthorization, TokenResource}, }, @@ -18,7 +18,7 @@ use taiga_halo2::{ constant::TAIGA_COMMITMENT_TREE_DEPTH, merkle_tree::{Anchor, MerklePath}, nullifier::NullifierKeyContainer, - resource::{Resource, ResourceValidityPredicates}, + resource::{Resource, ResourceLogics}, shielded_ptx::ShieldedPartialTransaction, transaction::{ShieldedPartialTxBundle, Transaction, TransparentPartialTxBundle}, }; @@ -60,12 +60,12 @@ pub fn create_token_intent_ptx( vec![compliance_1, compliance_2] }; - // Create VPs - let (input_vps, output_vps) = { + // Create resource logics + let (input_resource_logics, output_resource_logics) = { let input_resources = [*swap.sell.resource(), padding_input_resource]; let output_resources = [intent_resource, padding_output_resource]; - // Create the input token vps - let input_token_vps = swap.sell.generate_input_token_vps( + // Create resource_logics for the input token + let input_token_resource_logics = swap.sell.generate_input_token_resource_logics( &mut rng, input_auth, input_auth_sk, @@ -73,42 +73,49 @@ pub fn create_token_intent_ptx( output_resources, ); - // Create the intent vps - let intent_vps = { - let intent_vp = PartialFulfillmentIntentValidityPredicateCircuit { + // Create resource_logics for the intent + let intent_resource_logics = { + let intent_resource_logic = PartialFulfillmentIntentResourceLogicCircuit { owned_resource_id: intent_resource.commitment().inner(), input_resources, output_resources, swap: swap.clone(), }; - ResourceValidityPredicates::new(Box::new(intent_vp), vec![]) + ResourceLogics::new(Box::new(intent_resource_logic), vec![]) }; - // Create the padding input vps - let padding_input_vps = ResourceValidityPredicates::create_input_padding_resource_vps( - &padding_input_resource, - 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 the padding output vps - let padding_output_vps = ResourceValidityPredicates::create_output_padding_resource_vps( - &padding_output_resource, - input_resources, - output_resources, - ); + // Create resource_logics 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_vps, padding_input_vps], - vec![intent_vps, padding_output_vps], + vec![input_token_resource_logics, padding_input_resource_logics], + vec![intent_resource_logics, padding_output_resource_logics], ) }; // Create shielded partial tx - let ptx = - ShieldedPartialTransaction::build(compliances, input_vps, output_vps, vec![], &mut rng) - .unwrap(); + let ptx = ShieldedPartialTransaction::build( + compliances, + input_resource_logics, + output_resource_logics, + vec![], + &mut rng, + ) + .unwrap(); (ptx, swap, intent_resource) } @@ -152,46 +159,47 @@ pub fn consume_token_intent_ptx( vec![compliance_1, compliance_2] }; - // Create VPs - let (input_vps, output_vps) = { + // Create resource logics + let (input_resource_logics, output_resource_logics) = { let output_resources = [bought_resource, returned_resource]; - // Create intent vps - let intent_vps = { - let intent_vp = PartialFulfillmentIntentValidityPredicateCircuit { + // Create resource_logics for the intent + let intent_resource_logics = { + let intent_resource_logic = PartialFulfillmentIntentResourceLogicCircuit { owned_resource_id: intent_resource.get_nf().unwrap().inner(), input_resources, output_resources, swap: swap.clone(), }; - ResourceValidityPredicates::new(Box::new(intent_vp), vec![]) + ResourceLogics::new(Box::new(intent_resource_logic), vec![]) }; - // Create bought_resource_vps - let bought_resource_vps = TokenResource { + // Create resource_logics for the bought_resource + let bought_resource_resource_logics = TokenResource { token_name: swap.buy.name().clone(), resource: bought_resource, } - .generate_output_token_vps( + .generate_output_token_resource_logics( &mut rng, output_auth, input_resources, output_resources, ); - // Create the padding input vps - let padding_input_vps = ResourceValidityPredicates::create_input_padding_resource_vps( - &padding_input_resource, - 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 returned_resource_vps - let returned_resource_vps = TokenResource { + // Create resource_logics for the returned_resource + let returned_resource_resource_logics = TokenResource { token_name: swap.sell.token_name().clone(), resource: returned_resource, } - .generate_output_token_vps( + .generate_output_token_resource_logics( &mut rng, output_auth, input_resources, @@ -199,13 +207,23 @@ pub fn consume_token_intent_ptx( ); ( - vec![intent_vps, padding_input_vps], - vec![bought_resource_vps, returned_resource_vps], + vec![intent_resource_logics, padding_input_resource_logics], + vec![ + bought_resource_resource_logics, + returned_resource_resource_logics, + ], ) }; // Create shielded partial tx - ShieldedPartialTransaction::build(compliances, input_vps, output_vps, vec![], &mut rng).unwrap() + ShieldedPartialTransaction::build( + compliances, + input_resource_logics, + output_resource_logics, + vec![], + &mut rng, + ) + .unwrap() } pub fn create_token_swap_transaction(mut rng: R) -> Transaction { diff --git a/taiga_halo2/examples/tx_examples/token.rs b/taiga_halo2/examples/tx_examples/token.rs index 68a829ac..5a6c74bd 100644 --- a/taiga_halo2/examples/tx_examples/token.rs +++ b/taiga_halo2/examples/tx_examples/token.rs @@ -4,14 +4,14 @@ use pasta_curves::pallas; use rand::RngCore; use taiga_halo2::{ - circuit::vp_examples::{ + circuit::resource_logic_examples::{ signature_verification::COMPRESSED_TOKEN_AUTH_VK, token::{Token, TokenAuthorization}, }, compliance::ComplianceInfo, constant::TAIGA_COMMITMENT_TREE_DEPTH, merkle_tree::{Anchor, MerklePath}, - resource::{Resource, ResourceValidityPredicates}, + resource::{Resource, ResourceLogics}, shielded_ptx::ShieldedPartialTransaction, }; @@ -65,12 +65,12 @@ pub fn create_token_swap_ptx( vec![compliance_1, compliance_2] }; - // Create VPs - let (input_vps, output_vps) = { + // 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 the input token vps - let input_token_vps = input_resource.generate_input_token_vps( + // Create resource_logics for the input token + let input_token_resource_logics = input_resource.generate_input_token_resource_logics( &mut rng, input_auth, input_auth_sk, @@ -78,34 +78,43 @@ pub fn create_token_swap_ptx( output_resources, ); - // Create the output token vps - let output_token_vps = output_resource.generate_output_token_vps( + // 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 the padding input vps - let padding_input_vps = ResourceValidityPredicates::create_input_padding_resource_vps( - &padding_input_resource, - 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 the padding output vps - let padding_output_vps = ResourceValidityPredicates::create_output_padding_resource_vps( - &padding_output_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_vps, padding_input_vps], - vec![output_token_vps, padding_output_vps], + vec![input_token_resource_logics, padding_input_resource_logics], + vec![output_token_resource_logics, padding_output_resource_logics], ) }; // Create shielded partial tx - ShieldedPartialTransaction::build(compliances, input_vps, output_vps, vec![], &mut rng).unwrap() + ShieldedPartialTransaction::build( + compliances, + input_resource_logics, + output_resource_logics, + vec![], + &mut rng, + ) + .unwrap() } 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 3cbd3b88..f7dbcaac 100644 --- a/taiga_halo2/examples/tx_examples/token_swap_with_intent.rs +++ b/taiga_halo2/examples/tx_examples/token_swap_with_intent.rs @@ -9,8 +9,8 @@ use halo2_proofs::arithmetic::Field; use pasta_curves::{group::Curve, pallas}; use rand::{CryptoRng, RngCore}; use taiga_halo2::{ - circuit::vp_examples::{ - or_relation_intent::{create_intent_resource, OrRelationIntentValidityPredicateCircuit}, + circuit::resource_logic_examples::{ + or_relation_intent::{create_intent_resource, OrRelationIntentResourceLogicCircuit}, signature_verification::COMPRESSED_TOKEN_AUTH_VK, token::{Token, TokenAuthorization}, }, @@ -18,7 +18,7 @@ use taiga_halo2::{ constant::TAIGA_COMMITMENT_TREE_DEPTH, merkle_tree::{Anchor, MerklePath}, nullifier::NullifierKeyContainer, - resource::{Resource, ResourceValidityPredicates}, + resource::{Resource, ResourceLogics}, shielded_ptx::ShieldedPartialTransaction, transaction::{ShieldedPartialTxBundle, Transaction, TransparentPartialTxBundle}, }; @@ -81,12 +81,12 @@ pub fn create_token_intent_ptx( vec![compliance_1, compliance_2] }; - // Create VPs - let (input_vps, output_vps) = { + // Create resource logics + let (input_resource_logics, output_resource_logics) = { let input_resources = [*input_resource.resource(), padding_input_resource]; let output_resources = [intent_resource, padding_output_resource]; - // Create the input resource vps - let input_resource_vps = input_resource.generate_input_token_vps( + // Create resource_logics for the input resource + let input_resource_resource_logics = input_resource.generate_input_token_resource_logics( &mut rng, input_auth, input_auth_sk, @@ -94,9 +94,9 @@ pub fn create_token_intent_ptx( output_resources, ); - // Create the intent resource proving info - let intent_resource_vps = { - let intent_vp = OrRelationIntentValidityPredicateCircuit { + // Create resource logics for the intent resource + let intent_resource_resource_logics = { + let intent_resource_logic = OrRelationIntentResourceLogicCircuit { owned_resource_id: intent_resource.commitment().inner(), input_resources, output_resources, @@ -106,33 +106,46 @@ pub fn create_token_intent_ptx( receiver_value: input_resource.value, }; - ResourceValidityPredicates::new(Box::new(intent_vp), vec![]) + ResourceLogics::new(Box::new(intent_resource_logic), vec![]) }; - // Create the padding input vps - let padding_input_vps = ResourceValidityPredicates::create_input_padding_resource_vps( - &padding_input_resource, - 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 the padding output vps - let padding_output_vps = ResourceValidityPredicates::create_output_padding_resource_vps( - &padding_output_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_resource_vps, padding_input_vps], - vec![intent_resource_vps, padding_output_vps], + vec![ + input_resource_resource_logics, + padding_input_resource_logics, + ], + vec![ + intent_resource_resource_logics, + padding_output_resource_logics, + ], ) }; // Create shielded partial tx - let ptx = - ShieldedPartialTransaction::build(compliances, input_vps, output_vps, vec![], &mut rng) - .unwrap(); + let ptx = ShieldedPartialTransaction::build( + compliances, + input_resource_logics, + output_resource_logics, + vec![], + &mut rng, + ) + .unwrap(); (ptx, input_nk, input_resource_npk, input_resource.value) } @@ -194,13 +207,13 @@ pub fn consume_token_intent_ptx( vec![compliance_1, compliance_2] }; - // Create VPs - let (input_vps, output_vps) = { + // Create resource logics + let (input_resource_logics, output_resource_logics) = { let input_resources = [intent_resource, padding_input_resource]; let output_resources = [*output_resource.resource(), padding_output_resource]; - // Create intent vps - let intent_vps = { - let intent_vp = OrRelationIntentValidityPredicateCircuit { + // Create resource_logics for the intent + let intent_resource_logics = { + let intent_resource_logic = OrRelationIntentResourceLogicCircuit { owned_resource_id: input_resource_nf.inner(), input_resources, output_resources, @@ -210,39 +223,48 @@ pub fn consume_token_intent_ptx( receiver_value, }; - ResourceValidityPredicates::new(Box::new(intent_vp), vec![]) + ResourceLogics::new(Box::new(intent_resource_logic), vec![]) }; - // Create the output token vps - let output_token_vps = output_resource.generate_output_token_vps( + // Create resource logics for the output token resource + let output_token_resource_logics = output_resource.generate_output_token_resource_logics( &mut rng, output_auth, input_resources, output_resources, ); - // Create the padding input vps - let padding_input_vps = ResourceValidityPredicates::create_input_padding_resource_vps( - &padding_input_resource, - 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 the padding output vps - let padding_output_vps = ResourceValidityPredicates::create_output_padding_resource_vps( - &padding_output_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![intent_vps, padding_input_vps], - vec![output_token_vps, padding_output_vps], + vec![intent_resource_logics, padding_input_resource_logics], + vec![output_token_resource_logics, padding_output_resource_logics], ) }; // Create shielded partial tx - ShieldedPartialTransaction::build(compliances, input_vps, output_vps, vec![], &mut rng).unwrap() + ShieldedPartialTransaction::build( + compliances, + input_resource_logics, + output_resource_logics, + vec![], + &mut rng, + ) + .unwrap() } pub fn create_token_swap_intent_transaction(mut rng: R) -> Transaction { diff --git a/taiga_halo2/examples/tx_examples/token_swap_without_intent.rs b/taiga_halo2/examples/tx_examples/token_swap_without_intent.rs index 39d0a8be..151ac457 100644 --- a/taiga_halo2/examples/tx_examples/token_swap_without_intent.rs +++ b/taiga_halo2/examples/tx_examples/token_swap_without_intent.rs @@ -9,7 +9,7 @@ use halo2_proofs::arithmetic::Field; use pasta_curves::{group::Curve, pallas}; use rand::{CryptoRng, RngCore}; use taiga_halo2::{ - circuit::vp_examples::token::Token, + circuit::resource_logic_examples::token::Token, nullifier::NullifierKeyContainer, transaction::{ShieldedPartialTxBundle, Transaction, TransparentPartialTxBundle}, }; diff --git a/taiga_halo2/src/circuit/blake2s.rs b/taiga_halo2/src/circuit/blake2s.rs index 429a2dd6..05085934 100644 --- a/taiga_halo2/src/circuit/blake2s.rs +++ b/taiga_halo2/src/circuit/blake2s.rs @@ -1,11 +1,13 @@ use super::gadgets::assign_free_advice; use crate::circuit::gadgets::assign_free_constant; use crate::constant::{ - VP_CIRCUIT_FIRST_DYNAMIC_VP_CM_1, VP_CIRCUIT_FIRST_DYNAMIC_VP_CM_2, - VP_CIRCUIT_SECOND_DYNAMIC_VP_CM_1, VP_CIRCUIT_SECOND_DYNAMIC_VP_CM_2, - VP_COMMITMENT_PERSONALIZATION, + RESOURCE_LOGIC_CIRCUIT_FIRST_DYNAMIC_RESOURCE_LOGIC_CM_1, + RESOURCE_LOGIC_CIRCUIT_FIRST_DYNAMIC_RESOURCE_LOGIC_CM_2, + RESOURCE_LOGIC_CIRCUIT_SECOND_DYNAMIC_RESOURCE_LOGIC_CM_1, + RESOURCE_LOGIC_CIRCUIT_SECOND_DYNAMIC_RESOURCE_LOGIC_CM_2, + RESOURCE_LOGIC_COMMITMENT_PERSONALIZATION, }; -use crate::vp_commitment::ValidityPredicateCommitment; +use crate::resource_logic_commitment::ResourceLogicCommitment; use byteorder::{ByteOrder, LittleEndian}; use group::ff::PrimeField; use halo2_gadgets::utilities::bool_check; @@ -18,37 +20,57 @@ use halo2_proofs::{ }; use std::{convert::TryInto, marker::PhantomData}; -pub fn vp_commitment_gadget( +pub fn resource_logic_commitment_gadget( layouter: &mut impl Layouter, blake2s_chip: &Blake2sChip, - vp: AssignedCell, + resource_logic: AssignedCell, rcm: AssignedCell, ) -> Result<[AssignedCell; 2], Error> { - let hash = blake2s_chip.process(layouter, &[vp, rcm], VP_COMMITMENT_PERSONALIZATION)?; + let hash = blake2s_chip.process( + layouter, + &[resource_logic, rcm], + RESOURCE_LOGIC_COMMITMENT_PERSONALIZATION, + )?; blake2s_chip.encode_result(layouter, &hash) } -pub fn publicize_default_dynamic_vp_commitments( +pub fn publicize_default_dynamic_resource_logic_commitments( layouter: &mut impl Layouter, advice: Column, instances: Column, ) -> Result<(), Error> { - let vp_cm_fields: [F; 2] = ValidityPredicateCommitment::default().to_public_inputs(); - let vp_cm_1 = assign_free_advice( - layouter.namespace(|| "vp_cm 1"), + let resource_logic_cm_fields: [F; 2] = ResourceLogicCommitment::default().to_public_inputs(); + let resource_logic_cm_1 = assign_free_advice( + layouter.namespace(|| "resource_logic_cm 1"), advice, - Value::known(vp_cm_fields[0]), + Value::known(resource_logic_cm_fields[0]), )?; - let vp_cm_2 = assign_free_advice( - layouter.namespace(|| "vp_cm 2"), + let resource_logic_cm_2 = assign_free_advice( + layouter.namespace(|| "resource_logic_cm 2"), advice, - Value::known(vp_cm_fields[1]), + Value::known(resource_logic_cm_fields[1]), )?; - layouter.constrain_instance(vp_cm_1.cell(), instances, VP_CIRCUIT_FIRST_DYNAMIC_VP_CM_1)?; - layouter.constrain_instance(vp_cm_2.cell(), instances, VP_CIRCUIT_FIRST_DYNAMIC_VP_CM_2)?; - layouter.constrain_instance(vp_cm_1.cell(), instances, VP_CIRCUIT_SECOND_DYNAMIC_VP_CM_1)?; - layouter.constrain_instance(vp_cm_2.cell(), instances, VP_CIRCUIT_SECOND_DYNAMIC_VP_CM_2)?; + layouter.constrain_instance( + resource_logic_cm_1.cell(), + instances, + RESOURCE_LOGIC_CIRCUIT_FIRST_DYNAMIC_RESOURCE_LOGIC_CM_1, + )?; + layouter.constrain_instance( + resource_logic_cm_2.cell(), + instances, + RESOURCE_LOGIC_CIRCUIT_FIRST_DYNAMIC_RESOURCE_LOGIC_CM_2, + )?; + layouter.constrain_instance( + resource_logic_cm_1.cell(), + instances, + RESOURCE_LOGIC_CIRCUIT_SECOND_DYNAMIC_RESOURCE_LOGIC_CM_1, + )?; + layouter.constrain_instance( + resource_logic_cm_2.cell(), + instances, + RESOURCE_LOGIC_CIRCUIT_SECOND_DYNAMIC_RESOURCE_LOGIC_CM_2, + )?; Ok(()) } @@ -541,7 +563,7 @@ impl Blake2sChip { // | v[14] := v[14] ^ 0xFF..FF // Invert all bits. // END IF. let v_14 = if f { - Blake2sWord::from_constant_u32(IV[6] ^ u32::max_value(), layouter, self)? + Blake2sWord::from_constant_u32(IV[6] ^ u32::MAX, layouter, self)? } else { Blake2sWord::from_constant_u32(IV[6], layouter, self)? }; @@ -1082,8 +1104,8 @@ impl Blake2sWord { #[test] fn test_blake2s_circuit() { use crate::{ - circuit::gadgets::assign_free_advice, constant::VP_COMMITMENT_PERSONALIZATION, - vp_commitment::ValidityPredicateCommitment, + circuit::gadgets::assign_free_advice, constant::RESOURCE_LOGIC_COMMITMENT_PERSONALIZATION, + resource_logic_commitment::ResourceLogicCommitment, }; use halo2_proofs::{ circuit::{floor_planner, Layouter, Value}, @@ -1131,12 +1153,12 @@ fn test_blake2s_circuit() { config: Self::Config, mut layouter: impl Layouter, ) -> Result<(), Error> { - let vp = pallas::Base::one(); + let resource_logic = pallas::Base::one(); let rcm = pallas::Base::one(); - let vp_var = assign_free_advice( + let resource_logic_var = assign_free_advice( layouter.namespace(|| "message one"), config.advices[0], - Value::known(vp), + Value::known(resource_logic), )?; let rcm_var = assign_free_advice( layouter.namespace(|| "message two"), @@ -1147,11 +1169,11 @@ fn test_blake2s_circuit() { let blake2s_chip = Blake2sChip::construct(config); let words_result = blake2s_chip.process( &mut layouter, - &[vp_var, rcm_var], - VP_COMMITMENT_PERSONALIZATION, + &[resource_logic_var, rcm_var], + RESOURCE_LOGIC_COMMITMENT_PERSONALIZATION, )?; - let expect_ret = ValidityPredicateCommitment::commit(&vp, &rcm); + let expect_ret = ResourceLogicCommitment::commit(&resource_logic, &rcm); let expect_words_result: Vec = expect_ret .to_bytes() .chunks(4) diff --git a/taiga_halo2/src/circuit/compliance_circuit.rs b/taiga_halo2/src/circuit/compliance_circuit.rs index 390482f7..3af45f1c 100644 --- a/taiga_halo2/src/circuit/compliance_circuit.rs +++ b/taiga_halo2/src/circuit/compliance_circuit.rs @@ -1,4 +1,4 @@ -use crate::circuit::blake2s::{vp_commitment_gadget, Blake2sChip, Blake2sConfig}; +use crate::circuit::blake2s::{resource_logic_commitment_gadget, Blake2sChip, Blake2sConfig}; use crate::circuit::gadgets::assign_free_advice; use crate::circuit::hash_to_curve::HashToCurveConfig; use crate::circuit::integrity::{ @@ -10,9 +10,9 @@ use crate::circuit::merkle_circuit::{ use crate::constant::{ TaigaFixedBases, COMPLIANCE_ANCHOR_PUBLIC_INPUT_ROW_IDX, COMPLIANCE_DELTA_CM_X_PUBLIC_INPUT_ROW_IDX, COMPLIANCE_DELTA_CM_Y_PUBLIC_INPUT_ROW_IDX, - COMPLIANCE_INPUT_VP_CM_1_ROW_IDX, COMPLIANCE_INPUT_VP_CM_2_ROW_IDX, + COMPLIANCE_INPUT_RESOURCE_LOGIC_CM_1_ROW_IDX, COMPLIANCE_INPUT_RESOURCE_LOGIC_CM_2_ROW_IDX, COMPLIANCE_NF_PUBLIC_INPUT_ROW_IDX, COMPLIANCE_OUTPUT_CM_PUBLIC_INPUT_ROW_IDX, - COMPLIANCE_OUTPUT_VP_CM_1_ROW_IDX, COMPLIANCE_OUTPUT_VP_CM_2_ROW_IDX, + COMPLIANCE_OUTPUT_RESOURCE_LOGIC_CM_1_ROW_IDX, COMPLIANCE_OUTPUT_RESOURCE_LOGIC_CM_2_ROW_IDX, TAIGA_COMMITMENT_TREE_DEPTH, }; use crate::merkle_tree::LR; @@ -60,10 +60,10 @@ pub struct ComplianceCircuit { pub output_resource: Resource, /// random scalar for delta commitment pub rcv: pallas::Scalar, - /// The randomness for input resource application vp commitment - pub input_vp_cm_r: pallas::Base, - /// The randomness for output resource application vp commitment - pub output_vp_cm_r: pallas::Base, + /// The randomness of input resource logic commitment + pub input_resource_logic_cm_r: pallas::Base, + /// The randomness of output resource logic commitment + pub output_resource_logic_cm_r: pallas::Base, } impl Circuit for ComplianceCircuit { @@ -277,50 +277,50 @@ impl Circuit for ComplianceCircuit { }, )?; - // Input resource application VP commitment - let input_vp_cm_r = assign_free_advice( - layouter.namespace(|| "witness input_vp_cm_r"), + // Input resource logic commitment + let input_resource_logic_cm_r = assign_free_advice( + layouter.namespace(|| "witness input_resource_logic_cm_r"), config.advices[0], - Value::known(self.input_vp_cm_r), + Value::known(self.input_resource_logic_cm_r), )?; - let input_vp_commitment = vp_commitment_gadget( + let input_resource_logic_commitment = resource_logic_commitment_gadget( &mut layouter, &blake2s_chip, input_resource_variables.resource_variables.logic.clone(), - input_vp_cm_r, + input_resource_logic_cm_r, )?; layouter.constrain_instance( - input_vp_commitment[0].cell(), + input_resource_logic_commitment[0].cell(), config.instances, - COMPLIANCE_INPUT_VP_CM_1_ROW_IDX, + COMPLIANCE_INPUT_RESOURCE_LOGIC_CM_1_ROW_IDX, )?; layouter.constrain_instance( - input_vp_commitment[1].cell(), + input_resource_logic_commitment[1].cell(), config.instances, - COMPLIANCE_INPUT_VP_CM_2_ROW_IDX, + COMPLIANCE_INPUT_RESOURCE_LOGIC_CM_2_ROW_IDX, )?; - // Output resource application VP commitment - let output_vp_cm_r = assign_free_advice( - layouter.namespace(|| "witness output_vp_cm_r"), + // Output resource logic commitment + let output_resource_logic_cm_r = assign_free_advice( + layouter.namespace(|| "witness output_resource_logic_cm_r"), config.advices[0], - Value::known(self.output_vp_cm_r), + Value::known(self.output_resource_logic_cm_r), )?; - let output_vp_commitment = vp_commitment_gadget( + let output_resource_logic_commitment = resource_logic_commitment_gadget( &mut layouter, &blake2s_chip, output_resource_vars.resource_variables.logic.clone(), - output_vp_cm_r, + output_resource_logic_cm_r, )?; layouter.constrain_instance( - output_vp_commitment[0].cell(), + output_resource_logic_commitment[0].cell(), config.instances, - COMPLIANCE_OUTPUT_VP_CM_1_ROW_IDX, + COMPLIANCE_OUTPUT_RESOURCE_LOGIC_CM_1_ROW_IDX, )?; layouter.constrain_instance( - output_vp_commitment[1].cell(), + output_resource_logic_commitment[1].cell(), config.instances, - COMPLIANCE_OUTPUT_VP_CM_2_ROW_IDX, + COMPLIANCE_OUTPUT_RESOURCE_LOGIC_CM_2_ROW_IDX, )?; Ok(()) diff --git a/taiga_halo2/src/circuit/gadgets/target_resource_variable.rs b/taiga_halo2/src/circuit/gadgets/target_resource_variable.rs index 9aa68636..1fef6b2f 100644 --- a/taiga_halo2/src/circuit/gadgets/target_resource_variable.rs +++ b/taiga_halo2/src/circuit/gadgets/target_resource_variable.rs @@ -1,4 +1,4 @@ -use crate::circuit::vp_circuit::ResourceSearchableVariablePair; +use crate::circuit::resource_logic_circuit::ResourceSearchableVariablePair; use crate::constant::NUM_RESOURCE; use halo2_gadgets::utilities::bool_check; use halo2_proofs::{ diff --git a/taiga_halo2/src/circuit/integrity.rs b/taiga_halo2/src/circuit/integrity.rs index 075d4fe7..a40da119 100644 --- a/taiga_halo2/src/circuit/integrity.rs +++ b/taiga_halo2/src/circuit/integrity.rs @@ -2,7 +2,7 @@ use crate::circuit::{ gadgets::{assign_free_advice, assign_free_constant, poseidon_hash::poseidon_hash_gadget}, hash_to_curve::{hash_to_curve_circuit, HashToCurveConfig}, resource_commitment::{resource_commit, ResourceCommitChip}, - vp_circuit::{InputResourceVariables, OutputResourceVariables, ResourceVariables}, + resource_logic_circuit::{InputResourceVariables, OutputResourceVariables, ResourceVariables}, }; use crate::constant::{ TaigaFixedBases, TaigaFixedBasesFull, POSEIDON_TO_CURVE_INPUT_LEN, diff --git a/taiga_halo2/src/circuit/mod.rs b/taiga_halo2/src/circuit/mod.rs index d6c4cc3a..6a0118e1 100644 --- a/taiga_halo2/src/circuit/mod.rs +++ b/taiga_halo2/src/circuit/mod.rs @@ -2,14 +2,13 @@ pub mod compliance_circuit; pub mod gadgets; pub mod integrity; pub mod merkle_circuit; -// pub mod resource_circuit; #[macro_use] -pub mod vp_circuit; +pub mod resource_logic_circuit; pub mod blake2s; pub mod curve; pub mod hash_to_curve; pub mod resource_commitment; pub mod resource_encryption_circuit; +pub mod resource_logic_bytecode; +pub mod resource_logic_examples; mod vamp_ir_utils; -pub mod vp_bytecode; -pub mod vp_examples; diff --git a/taiga_halo2/src/circuit/resource_commitment.rs b/taiga_halo2/src/circuit/resource_commitment.rs index e1ed169c..dcaf9c7f 100644 --- a/taiga_halo2/src/circuit/resource_commitment.rs +++ b/taiga_halo2/src/circuit/resource_commitment.rs @@ -137,7 +137,7 @@ impl ResourceCommitChip { pub fn resource_commit( mut layouter: impl Layouter, chip: ResourceCommitChip, - app_vp: AssignedCell, + app_resource_logic: AssignedCell, label: AssignedCell, value: AssignedCell, npk: AssignedCell, @@ -155,7 +155,7 @@ pub fn resource_commit( // resource commitment let poseidon_message = [ - app_vp, + app_resource_logic, label, value, npk, diff --git a/taiga_halo2/src/circuit/resource_encryption_circuit.rs b/taiga_halo2/src/circuit/resource_encryption_circuit.rs index c7e06a65..2948e263 100644 --- a/taiga_halo2/src/circuit/resource_encryption_circuit.rs +++ b/taiga_halo2/src/circuit/resource_encryption_circuit.rs @@ -4,7 +4,8 @@ use crate::circuit::gadgets::{ }; use crate::constant::{ BaseFieldGenerators, TaigaFixedBases, POSEIDON_RATE, POSEIDON_WIDTH, - RESOURCE_ENCRYPTION_PLAINTEXT_NUM, VP_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX, + RESOURCE_ENCRYPTION_PLAINTEXT_NUM, + RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX, }; use ff::PrimeField; use halo2_gadgets::{ @@ -130,7 +131,7 @@ pub fn resource_encryption_gadget( layouter.constrain_instance( ele.cell(), instances, - VP_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX + i, + RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX + i, )?; } diff --git a/taiga_halo2/src/circuit/resource_logic_bytecode.rs b/taiga_halo2/src/circuit/resource_logic_bytecode.rs new file mode 100644 index 00000000..d8201921 --- /dev/null +++ b/taiga_halo2/src/circuit/resource_logic_bytecode.rs @@ -0,0 +1,269 @@ +#[cfg(feature = "borsh")] +use crate::circuit::resource_logic_examples::TrivialResourceLogicCircuit; +#[cfg(feature = "examples")] +use crate::circuit::resource_logic_examples::{ + cascade_intent::CascadeIntentResourceLogicCircuit, + or_relation_intent::OrRelationIntentResourceLogicCircuit, + partial_fulfillment_intent::PartialFulfillmentIntentResourceLogicCircuit, + receiver_resource_logic::ReceiverResourceLogicCircuit, + signature_verification::SignatureVerificationResourceLogicCircuit, + token::TokenResourceLogicCircuit, +}; +use crate::error::TransactionError; +use crate::shielded_ptx::ResourceLogicVerifyingInfoSet; +use crate::{ + circuit::resource_logic_circuit::{ + ResourceLogicVerifyingInfo, ResourceLogicVerifyingInfoTrait, VampIRResourceLogicCircuit, + }, + constant::{ + RESOURCE_LOGIC_CIRCUIT_NULLIFIER_ONE_PUBLIC_INPUT_IDX, + RESOURCE_LOGIC_CIRCUIT_NULLIFIER_TWO_PUBLIC_INPUT_IDX, + RESOURCE_LOGIC_CIRCUIT_OUTPUT_CM_ONE_PUBLIC_INPUT_IDX, + RESOURCE_LOGIC_CIRCUIT_OUTPUT_CM_TWO_PUBLIC_INPUT_IDX, + RESOURCE_LOGIC_CIRCUIT_OWNED_RESOURCE_ID_PUBLIC_INPUT_IDX, + }, + nullifier::Nullifier, + resource::ResourceCommitment, +}; + +#[cfg(feature = "borsh")] +use borsh::{BorshDeserialize, BorshSerialize}; +use pasta_curves::pallas; +#[cfg(feature = "serde")] +use serde; +use std::path::PathBuf; + +#[derive(Clone, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub enum ResourceLogicRepresentation { + // vampir has a unified circuit representation. + VampIR(Vec), + // Native halo2 circuits don't have a unified representatioin, enumerate the resource_logic circuit examples for the moment. + // TODO: figure out if we can have a unified circuit presentation. In theory, it's possible to separate the circuit system and proving system. + Trivial, + Token, + SignatureVerification, + Receiver, + PartialFulfillmentIntent, + OrRelationIntent, + CascadeIntent, + // Add other native resource_logic types here if needed +} + +#[derive(Clone, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct ResourceLogicByteCode { + circuit: ResourceLogicRepresentation, + inputs: Vec, +} + +#[derive(Clone, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct ApplicationByteCode { + app_resource_logic_bytecode: ResourceLogicByteCode, + dynamic_resource_logic_bytecode: Vec, +} + +impl ResourceLogicByteCode { + pub fn new(circuit: ResourceLogicRepresentation, inputs: Vec) -> Self { + Self { circuit, inputs } + } + + pub fn generate_proof(self) -> Result { + match self.circuit { + ResourceLogicRepresentation::VampIR(circuit) => { + // TDDO: use the file_name api atm, + // request vamp_ir to provide a api to generate circuit from bytes. + let vamp_ir_circuit_file = + PathBuf::from(String::from_utf8_lossy(&circuit).to_string()); + let inputs_file = PathBuf::from(String::from_utf8_lossy(&self.inputs).to_string()); + let resource_logic_circuit = VampIRResourceLogicCircuit::from_vamp_ir_file( + &vamp_ir_circuit_file, + &inputs_file, + ); + Ok(resource_logic_circuit.get_verifying_info()) + } + #[cfg(feature = "borsh")] + ResourceLogicRepresentation::Trivial => { + let resource_logic = TrivialResourceLogicCircuit::from_bytes(&self.inputs); + Ok(resource_logic.get_verifying_info()) + } + #[cfg(feature = "examples")] + ResourceLogicRepresentation::Token => { + let resource_logic = TokenResourceLogicCircuit::from_bytes(&self.inputs); + Ok(resource_logic.get_verifying_info()) + } + #[cfg(feature = "examples")] + ResourceLogicRepresentation::SignatureVerification => { + let resource_logic = + SignatureVerificationResourceLogicCircuit::from_bytes(&self.inputs); + Ok(resource_logic.get_verifying_info()) + } + #[cfg(feature = "examples")] + ResourceLogicRepresentation::Receiver => { + 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::CascadeIntent => { + let resource_logic = CascadeIntentResourceLogicCircuit::from_bytes(&self.inputs); + Ok(resource_logic.get_verifying_info()) + } + #[allow(unreachable_patterns)] + _ => Err(TransactionError::InvalidResourceLogicRepresentation), + } + } + + // Verify resource_logic circuit transparently and return owned resource PubID for further checking + pub fn verify_transparently( + &self, + compliance_nfs: &[Nullifier], + compliance_cms: &[ResourceCommitment], + ) -> Result { + // check resource logic transparently + let public_inputs = match &self.circuit { + ResourceLogicRepresentation::VampIR(circuit) => { + // TDDO: use the file_name api atm, + // request vamp_ir to provide a api to generate circuit from bytes. + let vamp_ir_circuit_file = + PathBuf::from(String::from_utf8_lossy(circuit).to_string()); + let inputs_file = PathBuf::from(String::from_utf8_lossy(&self.inputs).to_string()); + let resource_logic_circuit = VampIRResourceLogicCircuit::from_vamp_ir_file( + &vamp_ir_circuit_file, + &inputs_file, + ); + resource_logic_circuit.verify_transparently()? + } + #[cfg(feature = "borsh")] + ResourceLogicRepresentation::Trivial => { + let resource_logic = TrivialResourceLogicCircuit::from_bytes(&self.inputs); + resource_logic.verify_transparently()? + } + #[cfg(feature = "examples")] + ResourceLogicRepresentation::Token => { + let resource_logic = TokenResourceLogicCircuit::from_bytes(&self.inputs); + resource_logic.verify_transparently()? + } + #[cfg(feature = "examples")] + ResourceLogicRepresentation::SignatureVerification => { + let resource_logic = + SignatureVerificationResourceLogicCircuit::from_bytes(&self.inputs); + resource_logic.verify_transparently()? + } + #[cfg(feature = "examples")] + ResourceLogicRepresentation::Receiver => { + 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::CascadeIntent => { + let resource_logic = CascadeIntentResourceLogicCircuit::from_bytes(&self.inputs); + resource_logic.verify_transparently()? + } + #[allow(unreachable_patterns)] + _ => return Err(TransactionError::InvalidResourceLogicRepresentation), + }; + + // check nullifiers + // Check the resource_logic actually uses the input resources from compliance circuits. + let resource_logic_nfs = [ + public_inputs.get_from_index(RESOURCE_LOGIC_CIRCUIT_NULLIFIER_ONE_PUBLIC_INPUT_IDX), + public_inputs.get_from_index(RESOURCE_LOGIC_CIRCUIT_NULLIFIER_TWO_PUBLIC_INPUT_IDX), + ]; + + if !((compliance_nfs[0].inner() == resource_logic_nfs[0] + && compliance_nfs[1].inner() == resource_logic_nfs[1]) + || (compliance_nfs[0].inner() == resource_logic_nfs[1] + && compliance_nfs[1].inner() == resource_logic_nfs[0])) + { + return Err(TransactionError::InconsistentNullifier); + } + + // check resource_commitments + // Check the resource_logic actually uses the output resources from compliance circuits. + let resource_logic_cms = [ + public_inputs.get_from_index(RESOURCE_LOGIC_CIRCUIT_OUTPUT_CM_ONE_PUBLIC_INPUT_IDX), + public_inputs.get_from_index(RESOURCE_LOGIC_CIRCUIT_OUTPUT_CM_TWO_PUBLIC_INPUT_IDX), + ]; + if !((compliance_cms[0].inner() == resource_logic_cms[0] + && compliance_cms[1].inner() == resource_logic_cms[1]) + || (compliance_cms[0].inner() == resource_logic_cms[1] + && compliance_cms[1].inner() == resource_logic_cms[0])) + { + return Err(TransactionError::InconsistentOutputResourceCommitment); + } + + Ok(public_inputs.get_from_index(RESOURCE_LOGIC_CIRCUIT_OWNED_RESOURCE_ID_PUBLIC_INPUT_IDX)) + } +} + +impl ApplicationByteCode { + pub fn new( + app_resource_logic_bytecode: ResourceLogicByteCode, + dynamic_resource_logic_bytecode: Vec, + ) -> Self { + Self { + app_resource_logic_bytecode, + dynamic_resource_logic_bytecode, + } + } + + pub fn generate_proofs(self) -> Result { + let app_resource_logic_verifying_info = + self.app_resource_logic_bytecode.generate_proof()?; + + let app_dynamic_resource_logic_verifying_info: Result, _> = self + .dynamic_resource_logic_bytecode + .into_iter() + .map(|bytecode| bytecode.generate_proof()) + .collect(); + Ok(ResourceLogicVerifyingInfoSet::new( + app_resource_logic_verifying_info, + app_dynamic_resource_logic_verifying_info?, + )) + } + + // Verify resource_logic circuits transparently and return owned resource PubID for further checking + pub fn verify_transparently( + &self, + compliance_nfs: &[Nullifier], + compliance_cms: &[ResourceCommitment], + ) -> Result { + let owned_resource_id = self + .app_resource_logic_bytecode + .verify_transparently(compliance_nfs, compliance_cms)?; + for dynamic_resource_logic in self.dynamic_resource_logic_bytecode.iter() { + let id = dynamic_resource_logic.verify_transparently(compliance_nfs, compliance_cms)?; + // check: the app_resource_logic and dynamic_resource_logics belong to the resource + if id != owned_resource_id { + return Err(TransactionError::InconsistentOwnedResourceID); + } + } + Ok(owned_resource_id) + } +} diff --git a/taiga_halo2/src/circuit/vp_circuit.rs b/taiga_halo2/src/circuit/resource_logic_circuit.rs similarity index 76% rename from taiga_halo2/src/circuit/vp_circuit.rs rename to taiga_halo2/src/circuit/resource_logic_circuit.rs index 7615ceb1..2782350c 100644 --- a/taiga_halo2/src/circuit/vp_circuit.rs +++ b/taiga_halo2/src/circuit/resource_logic_circuit.rs @@ -1,6 +1,6 @@ use crate::{ circuit::{ - blake2s::publicize_default_dynamic_vp_commitments, + blake2s::publicize_default_dynamic_resource_logic_commitments, blake2s::Blake2sConfig, gadgets::{ add::{AddChip, AddConfig}, @@ -19,20 +19,23 @@ use crate::{ vamp_ir_utils::{get_circuit_assignments, parse, VariableAssignmentError}, }, constant::{ - TaigaFixedBases, NUM_RESOURCE, RESOURCE_ENCRYPTION_CIPHERTEXT_NUM, SETUP_PARAMS_MAP, - VP_CIRCUIT_NULLIFIER_ONE_PUBLIC_INPUT_IDX, VP_CIRCUIT_NULLIFIER_TWO_PUBLIC_INPUT_IDX, - VP_CIRCUIT_OUTPUT_CM_ONE_PUBLIC_INPUT_IDX, VP_CIRCUIT_OUTPUT_CM_TWO_PUBLIC_INPUT_IDX, - VP_CIRCUIT_OWNED_RESOURCE_ID_PUBLIC_INPUT_IDX, VP_CIRCUIT_PARAMS_SIZE, - VP_CIRCUIT_PUBLIC_INPUT_NUM, VP_CIRCUIT_RESOURCE_ENCRYPTION_PK_X_IDX, - VP_CIRCUIT_RESOURCE_ENCRYPTION_PK_Y_IDX, - VP_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX, + TaigaFixedBases, NUM_RESOURCE, RESOURCE_ENCRYPTION_CIPHERTEXT_NUM, + RESOURCE_LOGIC_CIRCUIT_NULLIFIER_ONE_PUBLIC_INPUT_IDX, + RESOURCE_LOGIC_CIRCUIT_NULLIFIER_TWO_PUBLIC_INPUT_IDX, + RESOURCE_LOGIC_CIRCUIT_OUTPUT_CM_ONE_PUBLIC_INPUT_IDX, + RESOURCE_LOGIC_CIRCUIT_OUTPUT_CM_TWO_PUBLIC_INPUT_IDX, + RESOURCE_LOGIC_CIRCUIT_OWNED_RESOURCE_ID_PUBLIC_INPUT_IDX, + RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE, RESOURCE_LOGIC_CIRCUIT_PUBLIC_INPUT_NUM, + RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PK_X_IDX, + RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PK_Y_IDX, + RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX, SETUP_PARAMS_MAP, }, error::TransactionError, proof::Proof, resource::{RandomSeed, Resource, ResourceCommitment}, resource_encryption::{ResourceCiphertext, SecretKey}, + resource_logic_vk::ResourceLogicVerifyingKey, utils::mod_r_p, - vp_vk::ValidityPredicateVerifyingKey, }; use dyn_clone::{clone_trait_object, DynClone}; use group::cofactor::CofactorCurveAffine; @@ -74,11 +77,11 @@ use rustler::types::atom; #[cfg(feature = "nif")] use rustler::{Decoder, Encoder, Env, NifResult, Term}; -pub type ValidityPredicate = dyn ValidityPredicateVerifyingInfo; +pub type ResourceLogic = dyn ResourceLogicVerifyingInfoTrait; #[derive(Debug, Clone)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub struct VPVerifyingInfo { +pub struct ResourceLogicVerifyingInfo { #[cfg_attr( feature = "serde", serde( @@ -88,14 +91,14 @@ pub struct VPVerifyingInfo { )] pub vk: VerifyingKey, pub proof: Proof, - pub public_inputs: ValidityPredicatePublicInputs, + pub public_inputs: ResourceLogicPublicInputs, } #[cfg(feature = "nif")] rustler::atoms! {verifying_info} #[cfg(feature = "nif")] -impl Encoder for VPVerifyingInfo { +impl Encoder for ResourceLogicVerifyingInfo { fn encode<'a>(&self, env: Env<'a>) -> Term<'a> { ( verifying_info().encode(env), @@ -108,20 +111,22 @@ impl Encoder for VPVerifyingInfo { } #[cfg(feature = "nif")] -impl<'a> Decoder<'a> for VPVerifyingInfo { +impl<'a> Decoder<'a> for ResourceLogicVerifyingInfo { fn decode(term: Term<'a>) -> NifResult { let (term, vk, proof, public_inputs): ( atom::Atom, Vec, Proof, - ValidityPredicatePublicInputs, + ResourceLogicPublicInputs, ) = term.decode()?; if term == verifying_info() { - use crate::circuit::vp_examples::TrivialValidityPredicateCircuit; - let params = SETUP_PARAMS_MAP.get(&VP_CIRCUIT_PARAMS_SIZE).unwrap(); - let vk = VerifyingKey::from_bytes::(&vk, params) + use crate::circuit::resource_logic_examples::TrivialResourceLogicCircuit; + let params = SETUP_PARAMS_MAP + .get(&RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE) + .unwrap(); + let vk = VerifyingKey::from_bytes::(&vk, params) .map_err(|_e| rustler::Error::Atom("failure to decode"))?; - Ok(VPVerifyingInfo { + Ok(ResourceLogicVerifyingInfo { vk, proof, public_inputs, @@ -134,26 +139,28 @@ impl<'a> Decoder<'a> for VPVerifyingInfo { #[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub struct ValidityPredicatePublicInputs([pallas::Base; VP_CIRCUIT_PUBLIC_INPUT_NUM]); +pub struct ResourceLogicPublicInputs([pallas::Base; RESOURCE_LOGIC_CIRCUIT_PUBLIC_INPUT_NUM]); #[cfg(feature = "nif")] -impl Encoder for ValidityPredicatePublicInputs { +impl Encoder for ResourceLogicPublicInputs { fn encode<'a>(&self, env: Env<'a>) -> Term<'a> { self.0.to_vec().encode(env) } } #[cfg(feature = "nif")] -impl<'a> Decoder<'a> for ValidityPredicatePublicInputs { +impl<'a> Decoder<'a> for ResourceLogicPublicInputs { fn decode(term: Term<'a>) -> NifResult { let val: Vec = Decoder::decode(term)?; Ok(val.into()) } } -impl VPVerifyingInfo { +impl ResourceLogicVerifyingInfo { pub fn verify(&self) -> Result<(), Error> { - let params = SETUP_PARAMS_MAP.get(&VP_CIRCUIT_PARAMS_SIZE).unwrap(); + let params = SETUP_PARAMS_MAP + .get(&RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE) + .unwrap(); self.proof .verify(&self.vk, params, &[self.public_inputs.inner()]) } @@ -161,31 +168,31 @@ impl VPVerifyingInfo { pub fn get_nullifiers(&self) -> [pallas::Base; NUM_RESOURCE] { [ self.public_inputs - .get_from_index(VP_CIRCUIT_NULLIFIER_ONE_PUBLIC_INPUT_IDX), + .get_from_index(RESOURCE_LOGIC_CIRCUIT_NULLIFIER_ONE_PUBLIC_INPUT_IDX), self.public_inputs - .get_from_index(VP_CIRCUIT_NULLIFIER_TWO_PUBLIC_INPUT_IDX), + .get_from_index(RESOURCE_LOGIC_CIRCUIT_NULLIFIER_TWO_PUBLIC_INPUT_IDX), ] } pub fn get_resource_commitments(&self) -> [ResourceCommitment; NUM_RESOURCE] { [ self.public_inputs - .get_from_index(VP_CIRCUIT_OUTPUT_CM_ONE_PUBLIC_INPUT_IDX) + .get_from_index(RESOURCE_LOGIC_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(RESOURCE_LOGIC_CIRCUIT_OUTPUT_CM_TWO_PUBLIC_INPUT_IDX) .into(), ] } pub fn get_owned_resource_id(&self) -> pallas::Base { self.public_inputs - .get_from_index(VP_CIRCUIT_OWNED_RESOURCE_ID_PUBLIC_INPUT_IDX) + .get_from_index(RESOURCE_LOGIC_CIRCUIT_OWNED_RESOURCE_ID_PUBLIC_INPUT_IDX) } } #[cfg(feature = "borsh")] -impl BorshSerialize for VPVerifyingInfo { +impl BorshSerialize for ResourceLogicVerifyingInfo { fn serialize(&self, writer: &mut W) -> std::io::Result<()> { use ff::PrimeField; // Write vk @@ -201,20 +208,22 @@ impl BorshSerialize for VPVerifyingInfo { } #[cfg(feature = "borsh")] -impl BorshDeserialize for VPVerifyingInfo { +impl BorshDeserialize for ResourceLogicVerifyingInfo { fn deserialize_reader(reader: &mut R) -> std::io::Result { // Read vk - use crate::circuit::vp_examples::TrivialValidityPredicateCircuit; + use crate::circuit::resource_logic_examples::TrivialResourceLogicCircuit; use crate::utils::read_base_field; - let params = SETUP_PARAMS_MAP.get(&VP_CIRCUIT_PARAMS_SIZE).unwrap(); - let vk = VerifyingKey::read::<_, TrivialValidityPredicateCircuit>(reader, params)?; + let params = SETUP_PARAMS_MAP + .get(&RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE) + .unwrap(); + let vk = VerifyingKey::read::<_, TrivialResourceLogicCircuit>(reader, params)?; // Read proof let proof = Proof::deserialize_reader(reader)?; // Read public inputs - let public_inputs: Vec<_> = (0..VP_CIRCUIT_PUBLIC_INPUT_NUM) + let public_inputs: Vec<_> = (0..RESOURCE_LOGIC_CIRCUIT_PUBLIC_INPUT_NUM) .map(|_| read_base_field(reader)) .collect::>()?; - Ok(VPVerifyingInfo { + Ok(ResourceLogicVerifyingInfo { vk, proof, public_inputs: public_inputs.into(), @@ -243,26 +252,28 @@ where use serde::de::Error; let buf: Vec = serde::Deserialize::deserialize(d)?; - use crate::circuit::vp_examples::TrivialValidityPredicateCircuit; - let params = SETUP_PARAMS_MAP.get(&VP_CIRCUIT_PARAMS_SIZE).unwrap(); - let vk = VerifyingKey::read::<_, TrivialValidityPredicateCircuit>(&mut buf.as_slice(), params) + use crate::circuit::resource_logic_examples::TrivialResourceLogicCircuit; + let params = SETUP_PARAMS_MAP + .get(&RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE) + .unwrap(); + let vk = VerifyingKey::read::<_, TrivialResourceLogicCircuit>(&mut buf.as_slice(), params) .map_err(|e| Error::custom(format!("Error reading VerifyingKey: {}", e)))?; Ok(vk) } -impl ValidityPredicatePublicInputs { - pub fn inner(&self) -> &[pallas::Base; VP_CIRCUIT_PUBLIC_INPUT_NUM] { +impl ResourceLogicPublicInputs { + pub fn inner(&self) -> &[pallas::Base; RESOURCE_LOGIC_CIRCUIT_PUBLIC_INPUT_NUM] { &self.0 } pub fn get_from_index(&self, idx: usize) -> pallas::Base { - assert!(idx < VP_CIRCUIT_PUBLIC_INPUT_NUM); + assert!(idx < RESOURCE_LOGIC_CIRCUIT_PUBLIC_INPUT_NUM); self.0[idx] } pub fn get_public_input_padding(input_len: usize, rseed: &RandomSeed) -> Vec { - assert!(input_len < VP_CIRCUIT_PUBLIC_INPUT_NUM); - rseed.get_random_padding(VP_CIRCUIT_PUBLIC_INPUT_NUM - input_len) + assert!(input_len < RESOURCE_LOGIC_CIRCUIT_PUBLIC_INPUT_NUM); + rseed.get_random_padding(RESOURCE_LOGIC_CIRCUIT_PUBLIC_INPUT_NUM - input_len) } // Only pad the custom public inputs, then we can add the actual resource encryption public inputs. @@ -270,8 +281,10 @@ impl ValidityPredicatePublicInputs { input_len: usize, rseed: &RandomSeed, ) -> Vec { - assert!(input_len < VP_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX); - rseed.get_random_padding(VP_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX - input_len) + assert!(input_len < RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX); + rseed.get_random_padding( + RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX - input_len, + ) } pub fn to_vec(&self) -> Vec { @@ -280,14 +293,14 @@ impl ValidityPredicatePublicInputs { pub fn decrypt(&self, sk: pallas::Base) -> Option> { let cipher: ResourceCiphertext = self.0 - [VP_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX - ..VP_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX + [RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX + ..RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX + RESOURCE_ENCRYPTION_CIPHERTEXT_NUM] .to_vec() .into(); let sender_pk = pallas::Affine::from_xy( - self.get_from_index(VP_CIRCUIT_RESOURCE_ENCRYPTION_PK_X_IDX), - self.get_from_index(VP_CIRCUIT_RESOURCE_ENCRYPTION_PK_Y_IDX), + self.get_from_index(RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PK_X_IDX), + self.get_from_index(RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PK_Y_IDX), ) .unwrap() .to_curve(); @@ -296,9 +309,9 @@ impl ValidityPredicatePublicInputs { } } -impl From> for ValidityPredicatePublicInputs { +impl From> for ResourceLogicPublicInputs { fn from(public_input_vec: Vec) -> Self { - ValidityPredicatePublicInputs( + ResourceLogicPublicInputs( public_input_vec .try_into() .expect("public input with incorrect length"), @@ -307,7 +320,7 @@ impl From> for ValidityPredicatePublicInputs { } #[derive(Clone, Debug)] -pub struct ValidityPredicateConfig { +pub struct ResourceLogicConfig { pub advices: [Column; 10], pub instances: Column, pub table_idx: TableColumn, @@ -325,7 +338,7 @@ pub struct ValidityPredicateConfig { pub resource_commit_config: ResourceCommitConfig, } -impl ValidityPredicateConfig { +impl ResourceLogicConfig { pub fn configure(meta: &mut ConstraintSystem) -> Self { let instances = meta.instance_column(); meta.enable_equality(instances); @@ -421,22 +434,22 @@ impl ValidityPredicateConfig { } } -pub trait ValidityPredicateVerifyingInfo: DynClone { - fn get_verifying_info(&self) -> VPVerifyingInfo; - fn verify_transparently(&self) -> Result; - fn get_vp_vk(&self) -> ValidityPredicateVerifyingKey; +pub trait ResourceLogicVerifyingInfoTrait: DynClone { + fn get_verifying_info(&self) -> ResourceLogicVerifyingInfo; + fn verify_transparently(&self) -> Result; + fn get_resource_logic_vk(&self) -> ResourceLogicVerifyingKey; } -clone_trait_object!(ValidityPredicateVerifyingInfo); +clone_trait_object!(ResourceLogicVerifyingInfoTrait); -pub trait ValidityPredicateCircuit: Circuit + ValidityPredicateVerifyingInfo { +pub trait ResourceLogicCircuit: Circuit + ResourceLogicVerifyingInfoTrait { // Default implementation, constrains the resources integrity. - // TODO: how to enforce the constraints in vp circuit? + // TODO: how to enforce the constraints in resource_logic circuit? fn basic_constraints( &self, - config: ValidityPredicateConfig, + config: ResourceLogicConfig, mut layouter: impl Layouter, - ) -> Result { + ) -> Result { layouter.assign_table( || "table_idx", |mut table| { @@ -496,29 +509,29 @@ pub trait ValidityPredicateCircuit: Circuit + ValidityPredicateVer layouter.constrain_instance( owned_resource_id.cell(), config.instances, - VP_CIRCUIT_OWNED_RESOURCE_ID_PUBLIC_INPUT_IDX, + RESOURCE_LOGIC_CIRCUIT_OWNED_RESOURCE_ID_PUBLIC_INPUT_IDX, )?; - Ok(BasicValidityPredicateVariables { + Ok(BasicResourceLogicVariables { owned_resource_id, input_resource_variables: input_resource_variables.try_into().unwrap(), output_resource_variables: output_resource_variables.try_into().unwrap(), }) } - // VP designer need to implement the following functions. + // resource logic designer need to implement the following functions. // `get_input_resources` and `get_output_resources` will be used in `basic_constraints` to get the basic resource info. // Add custom constraints on basic resource variables and user-defined variables. - // It should at least contain the default vp commitment + // It should at least contain the default resource_logic commitment fn custom_constraints( &self, - config: ValidityPredicateConfig, + config: ResourceLogicConfig, mut layouter: impl Layouter, - _basic_variables: BasicValidityPredicateVariables, + _basic_variables: BasicResourceLogicVariables, ) -> Result<(), Error> { - // Publicize the dynamic vp commitments with default value - publicize_default_dynamic_vp_commitments( + // Publicize the dynamic resource_logic commitments with default value + publicize_default_dynamic_resource_logic_commitments( &mut layouter, config.advices[0], config.instances, @@ -543,17 +556,17 @@ pub trait ValidityPredicateCircuit: Circuit + ValidityPredicateVer } fn get_input_resources(&self) -> &[Resource; NUM_RESOURCE]; fn get_output_resources(&self) -> &[Resource; NUM_RESOURCE]; - fn get_public_inputs(&self, rng: impl RngCore) -> ValidityPredicatePublicInputs; + fn get_public_inputs(&self, rng: impl RngCore) -> ResourceLogicPublicInputs; // The owned_resource_id is the input_resource_nf or the output_resource_cm_x // The owned_resource_id is the key to look up the target variables and - // help determine whether the owned resource is the input resource or not in VP circuit. + // help determine whether the owned resource is the input resource or not in resource logic circuit. fn get_owned_resource_id(&self) -> pallas::Base; } -/// BasicValidityPredicateVariables are generally constrained in ValidityPredicateCircuit::basic_constraints -/// and will be used in ValidityPredicateCircuit::custom_constraints +/// BasicResourceLogicVariables are generally constrained in ResourceLogicCircuit::basic_constraints +/// and will be used in ResourceLogicCircuit::custom_constraints #[derive(Debug, Clone)] -pub struct BasicValidityPredicateVariables { +pub struct BasicResourceLogicVariables { pub owned_resource_id: AssignedCell, pub input_resource_variables: [InputResourceVariables; NUM_RESOURCE], pub output_resource_variables: [OutputResourceVariables; NUM_RESOURCE], @@ -594,7 +607,7 @@ pub struct ResourceSearchableVariablePair { pub target_variable: AssignedCell, } -impl BasicValidityPredicateVariables { +impl BasicResourceLogicVariables { pub fn get_owned_resource_id(&self) -> AssignedCell { self.owned_resource_id.clone() } @@ -710,10 +723,10 @@ impl BasicValidityPredicateVariables { // Default Circuit trait implementation #[macro_export] -macro_rules! vp_circuit_impl { +macro_rules! resource_logic_circuit_impl { ($name:ident) => { impl Circuit for $name { - type Config = ValidityPredicateConfig; + type Config = ResourceLogicConfig; type FloorPlanner = floor_planner::V1; fn without_witnesses(&self) -> Self { @@ -744,12 +757,12 @@ macro_rules! vp_circuit_impl { }; } -// Default ValidityPredicateVerifyingInfo trait implementation +// Default ResourceLogicVerifyingInfoTrait trait implementation #[macro_export] -macro_rules! vp_verifying_info_impl { +macro_rules! resource_logic_verifying_info_impl { ($name:ident) => { - impl ValidityPredicateVerifyingInfo for $name { - fn get_verifying_info(&self) -> VPVerifyingInfo { + impl ResourceLogicVerifyingInfoTrait for $name { + fn get_verifying_info(&self) -> ResourceLogicVerifyingInfo { let mut rng = OsRng; let params = SETUP_PARAMS_MAP.get(&15).unwrap(); let vk = keygen_vk(params, self).expect("keygen_vk should not fail"); @@ -763,16 +776,14 @@ macro_rules! vp_verifying_info_impl { &mut rng, ) .unwrap(); - VPVerifyingInfo { + ResourceLogicVerifyingInfo { vk, proof, public_inputs, } } - fn verify_transparently( - &self, - ) -> Result { + fn verify_transparently(&self) -> Result { use halo2_proofs::dev::MockProver; let mut rng = OsRng; let public_inputs = self.get_public_inputs(&mut rng); @@ -783,19 +794,19 @@ macro_rules! vp_verifying_info_impl { Ok(public_inputs) } - fn get_vp_vk(&self) -> ValidityPredicateVerifyingKey { + fn get_resource_logic_vk(&self) -> ResourceLogicVerifyingKey { let params = SETUP_PARAMS_MAP.get(&15).unwrap(); let vk = keygen_vk(params, self).expect("keygen_vk should not fail"); - ValidityPredicateVerifyingKey::from_vk(vk) + ResourceLogicVerifyingKey::from_vk(vk) } } }; } #[derive(Clone)] -pub struct VampIRValidityPredicateCircuit { +pub struct VampIRResourceLogicCircuit { // TODO: vamp_ir doesn't support to set the params size manually, add the params here temporarily. - // remove the params once we can set it as VP_CIRCUIT_PARAMS_SIZE in vamp_ir. + // remove the params once we can set it as RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE in vamp_ir. pub params: Params, pub circuit: Halo2Module, pub public_inputs: Vec, @@ -817,7 +828,7 @@ impl VampIRCircuitError { } } -impl VampIRValidityPredicateCircuit { +impl VampIRResourceLogicCircuit { pub fn from_vamp_ir_source( vamp_ir_source: &str, named_field_assignments: HashMap, @@ -890,8 +901,8 @@ impl VampIRValidityPredicateCircuit { } } -impl ValidityPredicateVerifyingInfo for VampIRValidityPredicateCircuit { - fn get_verifying_info(&self) -> VPVerifyingInfo { +impl ResourceLogicVerifyingInfoTrait for VampIRResourceLogicCircuit { + fn get_verifying_info(&self) -> ResourceLogicVerifyingInfo { let mut rng = OsRng; let vk = keygen_vk(&self.params, &self.circuit).expect("keygen_vk should not fail"); let pk = @@ -899,7 +910,7 @@ impl ValidityPredicateVerifyingInfo for VampIRValidityPredicateCircuit { let mut public_inputs = self.public_inputs.clone(); let rseed = RandomSeed::random(&mut rng); - public_inputs.extend(ValidityPredicatePublicInputs::get_public_input_padding( + public_inputs.extend(ResourceLogicPublicInputs::get_public_input_padding( self.public_inputs.len(), &rseed, )); @@ -912,19 +923,19 @@ impl ValidityPredicateVerifyingInfo for VampIRValidityPredicateCircuit { &mut rng, ) .unwrap(); - VPVerifyingInfo { + ResourceLogicVerifyingInfo { vk, proof, public_inputs: public_inputs.into(), } } - fn verify_transparently(&self) -> Result { + fn verify_transparently(&self) -> Result { use halo2_proofs::dev::MockProver; let mut rng = OsRng; let mut public_inputs = self.public_inputs.clone(); let rseed = RandomSeed::random(&mut rng); - public_inputs.extend(ValidityPredicatePublicInputs::get_public_input_padding( + public_inputs.extend(ResourceLogicPublicInputs::get_public_input_padding( self.public_inputs.len(), &rseed, )); @@ -932,19 +943,19 @@ impl ValidityPredicateVerifyingInfo for VampIRValidityPredicateCircuit { MockProver::::run(15, &self.circuit, vec![public_inputs.to_vec()]) .unwrap(); prover.verify().unwrap(); - Ok(ValidityPredicatePublicInputs::from(public_inputs)) + Ok(ResourceLogicPublicInputs::from(public_inputs)) } - fn get_vp_vk(&self) -> ValidityPredicateVerifyingKey { + fn get_resource_logic_vk(&self) -> ResourceLogicVerifyingKey { let vk = keygen_vk(&self.params, &self.circuit).expect("keygen_vk should not fail"); - ValidityPredicateVerifyingKey::from_vk(vk) + ResourceLogicVerifyingKey::from_vk(vk) } } #[cfg(test)] mod tests { - use crate::circuit::vp_circuit::{ - ValidityPredicateVerifyingInfo, VampIRValidityPredicateCircuit, + use crate::circuit::resource_logic_circuit::{ + ResourceLogicVerifyingInfoTrait, VampIRResourceLogicCircuit, }; use num_bigint::BigInt; use std::collections::HashMap; @@ -953,90 +964,89 @@ mod tests { #[ignore] #[test] - fn test_create_vp_from_vamp_ir_file() { + fn test_create_resource_logic_from_vamp_ir_file() { let vamp_ir_circuit_file = PathBuf::from("./src/circuit/vamp_ir_circuits/pyth.pir"); let inputs_file = PathBuf::from("./src/circuit/vamp_ir_circuits/pyth.inputs"); - let vp_circuit = - VampIRValidityPredicateCircuit::from_vamp_ir_file(&vamp_ir_circuit_file, &inputs_file); + let resource_logic_circuit = + VampIRResourceLogicCircuit::from_vamp_ir_file(&vamp_ir_circuit_file, &inputs_file); // generate proof and instance - let vp_info = vp_circuit.get_verifying_info(); + let resource_logic_info = resource_logic_circuit.get_verifying_info(); // verify the proof - // TODO: use the vp_info.verify() instead. vp_info.verify() doesn't work now because it uses the fixed VP_CIRCUIT_PARAMS_SIZE params. - vp_info + // TODO: use the resource_logic_info.verify() instead. resource_logic_info.verify() doesn't work now because it uses the fixed RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE params. + resource_logic_info .proof .verify( - &vp_info.vk, - &vp_circuit.params, - &[vp_info.public_inputs.inner()], + &resource_logic_info.vk, + &resource_logic_circuit.params, + &[resource_logic_info.public_inputs.inner()], ) .unwrap(); } #[test] - fn test_create_vp_from_invalid_vamp_ir_file() { + fn test_create_resource_logic_from_invalid_vamp_ir_file() { let invalid_vamp_ir_source = - VampIRValidityPredicateCircuit::from_vamp_ir_source("{aaxxx", HashMap::new()); + VampIRResourceLogicCircuit::from_vamp_ir_source("{aaxxx", HashMap::new()); assert!(invalid_vamp_ir_source.is_err()); } #[test] - fn test_create_vp_with_missing_assignment() { + fn test_create_resource_logic_with_missing_assignment() { let missing_x_assignment = - VampIRValidityPredicateCircuit::from_vamp_ir_source("x = 1;", HashMap::new()); + VampIRResourceLogicCircuit::from_vamp_ir_source("x = 1;", HashMap::new()); assert!(missing_x_assignment.is_err()); } #[test] - fn test_create_vp_with_no_assignment() { - let zero_constraint = - VampIRValidityPredicateCircuit::from_vamp_ir_source("0;", HashMap::new()); + fn test_create_resource_logic_with_no_assignment() { + let zero_constraint = VampIRResourceLogicCircuit::from_vamp_ir_source("0;", HashMap::new()); assert!(zero_constraint.is_ok()); } #[ignore] #[test] - fn test_create_vp_with_valid_assignment() { - let x_assignment_circuit = VampIRValidityPredicateCircuit::from_vamp_ir_source( + fn test_create_resource_logic_with_valid_assignment() { + let x_assignment_circuit = VampIRResourceLogicCircuit::from_vamp_ir_source( "x = 1;", HashMap::from([(String::from("x"), make_constant(BigInt::from(1)))]), ); assert!(x_assignment_circuit.is_ok()); - let vp_circuit = x_assignment_circuit.unwrap(); - let vp_info = vp_circuit.get_verifying_info(); + let resource_logic_circuit = x_assignment_circuit.unwrap(); + let resource_logic_info = resource_logic_circuit.get_verifying_info(); - assert!(vp_info + assert!(resource_logic_info .proof .verify( - &vp_info.vk, - &vp_circuit.params, - &[vp_info.public_inputs.inner()] + &resource_logic_info.vk, + &resource_logic_circuit.params, + &[resource_logic_info.public_inputs.inner()] ) .is_ok()); } #[ignore] #[test] - fn test_create_vp_with_invalid_assignment() { - let x_assignment_circuit = VampIRValidityPredicateCircuit::from_vamp_ir_source( + fn test_create_resource_logic_with_invalid_assignment() { + let x_assignment_circuit = VampIRResourceLogicCircuit::from_vamp_ir_source( "x = 1;", HashMap::from([(String::from("x"), make_constant(BigInt::from(0)))]), ); assert!(x_assignment_circuit.is_ok()); - let vp_circuit = x_assignment_circuit.unwrap(); - let vp_info = vp_circuit.get_verifying_info(); + let resource_logic_circuit = x_assignment_circuit.unwrap(); + let resource_logic_info = resource_logic_circuit.get_verifying_info(); - assert!(vp_info + assert!(resource_logic_info .proof .verify( - &vp_info.vk, - &vp_circuit.params, - &[vp_info.public_inputs.inner()] + &resource_logic_info.vk, + &resource_logic_circuit.params, + &[resource_logic_info.public_inputs.inner()] ) .is_err()); } @@ -1045,8 +1055,10 @@ mod tests { #[test] fn test_vk_serialize() { use crate::circuit::{ - vp_circuit::{serde_deserialize_verifying_key, serde_serialize_verifying_key}, - vp_examples::TrivialValidityPredicateCircuit, + resource_logic_circuit::{ + serde_deserialize_verifying_key, serde_serialize_verifying_key, + }, + resource_logic_examples::TrivialResourceLogicCircuit, }; use halo2_proofs::plonk::VerifyingKey; use pasta_curves::vesta; @@ -1061,7 +1073,7 @@ mod tests { vk: VerifyingKey, } - let t = TrivialValidityPredicateCircuit::default().get_vp_vk(); + let t = TrivialResourceLogicCircuit::default().get_resource_logic_vk(); let a = TestStruct { vk: t.get_vk().unwrap(), diff --git a/taiga_halo2/src/circuit/vp_examples.rs b/taiga_halo2/src/circuit/resource_logic_examples.rs similarity index 61% rename from taiga_halo2/src/circuit/vp_examples.rs rename to taiga_halo2/src/circuit/resource_logic_examples.rs index adaa99ef..983ee1f1 100644 --- a/taiga_halo2/src/circuit/vp_examples.rs +++ b/taiga_halo2/src/circuit/resource_logic_examples.rs @@ -1,16 +1,16 @@ #[cfg(feature = "borsh")] -use crate::circuit::vp_bytecode::{ValidityPredicateByteCode, ValidityPredicateRepresentation}; +use crate::circuit::resource_logic_bytecode::{ResourceLogicByteCode, ResourceLogicRepresentation}; use crate::{ - circuit::vp_circuit::{ - VPVerifyingInfo, ValidityPredicateCircuit, ValidityPredicateConfig, - ValidityPredicatePublicInputs, ValidityPredicateVerifyingInfo, + circuit::resource_logic_circuit::{ + ResourceLogicCircuit, ResourceLogicConfig, ResourceLogicPublicInputs, + ResourceLogicVerifyingInfo, ResourceLogicVerifyingInfoTrait, }, - constant::{NUM_RESOURCE, SETUP_PARAMS_MAP, VP_CIRCUIT_PARAMS_SIZE}, + constant::{NUM_RESOURCE, RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE, SETUP_PARAMS_MAP}, error::TransactionError, proof::Proof, resource::{RandomSeed, Resource}, - vp_commitment::ValidityPredicateCommitment, - vp_vk::ValidityPredicateVerifyingKey, + resource_logic_commitment::ResourceLogicCommitment, + resource_logic_vk::ResourceLogicVerifyingKey, }; #[cfg(feature = "borsh")] use borsh::{BorshDeserialize, BorshSerialize}; @@ -34,31 +34,40 @@ pub mod or_relation_intent; #[cfg(feature = "examples")] pub mod partial_fulfillment_intent; #[cfg(feature = "examples")] -pub mod receiver_vp; +pub mod receiver_resource_logic; #[cfg(feature = "examples")] pub mod signature_verification; #[cfg(feature = "examples")] pub mod token; lazy_static! { - pub static ref TRIVIAL_VP_VK: ValidityPredicateVerifyingKey = { - let params = SETUP_PARAMS_MAP.get(&VP_CIRCUIT_PARAMS_SIZE).unwrap(); - let empty_circuit = TrivialValidityPredicateCircuit::default(); + pub static ref TRIVIAL_RESOURCE_LOGIC_VK: ResourceLogicVerifyingKey = { + let params = SETUP_PARAMS_MAP + .get(&RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE) + .unwrap(); + let empty_circuit = TrivialResourceLogicCircuit::default(); let vk = keygen_vk(params, &empty_circuit).expect("keygen_vk should not fail"); - ValidityPredicateVerifyingKey::from_vk(vk) + ResourceLogicVerifyingKey::from_vk(vk) }; - pub static ref TRIVIAL_VP_PK: ProvingKey = { - let params = SETUP_PARAMS_MAP.get(&VP_CIRCUIT_PARAMS_SIZE).unwrap(); - let empty_circuit = TrivialValidityPredicateCircuit::default(); - keygen_pk(params, TRIVIAL_VP_VK.get_vk().unwrap(), &empty_circuit) - .expect("keygen_pk should not fail") + pub static ref TRIVIAL_RESOURCE_LOGIC_PK: ProvingKey = { + let params = SETUP_PARAMS_MAP + .get(&RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE) + .unwrap(); + let empty_circuit = TrivialResourceLogicCircuit::default(); + keygen_pk( + params, + TRIVIAL_RESOURCE_LOGIC_VK.get_vk().unwrap(), + &empty_circuit, + ) + .expect("keygen_pk should not fail") }; - pub static ref COMPRESSED_TRIVIAL_VP_VK: pallas::Base = TRIVIAL_VP_VK.get_compressed(); + pub static ref COMPRESSED_TRIVIAL_RESOURCE_LOGIC_VK: pallas::Base = + TRIVIAL_RESOURCE_LOGIC_VK.get_compressed(); } -// TrivialValidityPredicateCircuit with empty custom constraints. +// TrivialResourceLogicCircuit with empty custom constraints. #[derive(Clone, Debug, Default)] -pub struct TrivialValidityPredicateCircuit { +pub struct TrivialResourceLogicCircuit { pub owned_resource_id: pallas::Base, pub input_resources: [Resource; NUM_RESOURCE], pub output_resources: [Resource; NUM_RESOURCE], @@ -67,14 +76,14 @@ pub struct TrivialValidityPredicateCircuit { // I only exist to allow trivial derivation of the nifstruct #[derive(Clone, Debug, Default)] #[cfg_attr(feature = "nif", derive(NifStruct))] -#[cfg_attr(feature = "nif", module = "Taiga.VP.Trivial")] -struct TrivialValidtyPredicateCircuitProxy { +#[cfg_attr(feature = "nif", module = "Taiga.ResourceLogic.Trivial")] +struct TrivialResourceLogicCircuitProxy { owned_resource_id: pallas::Base, input_resources: Vec, output_resources: Vec, } -impl TrivialValidityPredicateCircuit { +impl TrivialResourceLogicCircuit { pub fn new( owned_resource_id: pallas::Base, input_resources: [Resource; NUM_RESOURCE], @@ -89,8 +98,8 @@ impl TrivialValidityPredicateCircuit { // Only for test #[cfg(feature = "borsh")] - pub fn to_bytecode(&self) -> ValidityPredicateByteCode { - ValidityPredicateByteCode::new(ValidityPredicateRepresentation::Trivial, self.to_bytes()) + pub fn to_bytecode(&self) -> ResourceLogicByteCode { + ResourceLogicByteCode::new(ResourceLogicRepresentation::Trivial, self.to_bytes()) } // Only for test @@ -105,8 +114,8 @@ impl TrivialValidityPredicateCircuit { BorshDeserialize::deserialize(&mut bytes.as_ref()).unwrap() } - fn to_proxy(&self) -> TrivialValidtyPredicateCircuitProxy { - TrivialValidtyPredicateCircuitProxy { + fn to_proxy(&self) -> TrivialResourceLogicCircuitProxy { + TrivialResourceLogicCircuitProxy { owned_resource_id: self.owned_resource_id, input_resources: self.input_resources.to_vec(), output_resources: self.output_resources.to_vec(), @@ -115,7 +124,7 @@ impl TrivialValidityPredicateCircuit { } #[cfg(feature = "borsh")] -impl BorshSerialize for TrivialValidityPredicateCircuit { +impl BorshSerialize for TrivialResourceLogicCircuit { fn serialize(&self, writer: &mut W) -> std::io::Result<()> { use ff::PrimeField; writer.write_all(&self.owned_resource_id.to_repr())?; @@ -131,7 +140,7 @@ impl BorshSerialize for TrivialValidityPredicateCircuit { } #[cfg(feature = "borsh")] -impl BorshDeserialize for TrivialValidityPredicateCircuit { +impl BorshDeserialize for TrivialResourceLogicCircuit { fn deserialize_reader(reader: &mut R) -> std::io::Result { let owned_resource_id = crate::utils::read_base_field(reader)?; let input_resources: Vec<_> = (0..NUM_RESOURCE) @@ -148,12 +157,12 @@ impl BorshDeserialize for TrivialValidityPredicateCircuit { } } -impl TrivialValidtyPredicateCircuitProxy { - fn to_concrete(&self) -> Option { +impl TrivialResourceLogicCircuitProxy { + fn to_concrete(&self) -> Option { let input_resources = self.input_resources.clone().try_into().ok()?; let output_resources = self.output_resources.clone().try_into().ok()?; let owned_resource_id = self.owned_resource_id; - Some(TrivialValidityPredicateCircuit { + Some(TrivialResourceLogicCircuit { owned_resource_id, input_resources, output_resources, @@ -161,21 +170,21 @@ impl TrivialValidtyPredicateCircuitProxy { } } #[cfg(feature = "nif")] -impl Encoder for TrivialValidityPredicateCircuit { +impl Encoder for TrivialResourceLogicCircuit { fn encode<'a>(&self, env: Env<'a>) -> Term<'a> { self.to_proxy().encode(env) } } #[cfg(feature = "nif")] -impl<'a> Decoder<'a> for TrivialValidityPredicateCircuit { +impl<'a> Decoder<'a> for TrivialResourceLogicCircuit { fn decode(term: Term<'a>) -> NifResult { - let val: TrivialValidtyPredicateCircuitProxy = Decoder::decode(term)?; + let val: TrivialResourceLogicCircuitProxy = Decoder::decode(term)?; val.to_concrete() .ok_or(rustler::Error::RaiseAtom("Could not decode proxy")) } } -impl ValidityPredicateCircuit for TrivialValidityPredicateCircuit { +impl ResourceLogicCircuit for TrivialResourceLogicCircuit { fn get_input_resources(&self) -> &[Resource; NUM_RESOURCE] { &self.input_resources } @@ -184,13 +193,13 @@ impl ValidityPredicateCircuit for TrivialValidityPredicateCircuit { &self.output_resources } - fn get_public_inputs(&self, mut rng: impl RngCore) -> ValidityPredicatePublicInputs { + fn get_public_inputs(&self, mut rng: impl RngCore) -> ResourceLogicPublicInputs { let mut public_inputs = self.get_mandatory_public_inputs(); - let default_vp_cm: [pallas::Base; 2] = - ValidityPredicateCommitment::default().to_public_inputs(); - public_inputs.extend(default_vp_cm); - public_inputs.extend(default_vp_cm); - let padding = ValidityPredicatePublicInputs::get_public_input_padding( + let default_resource_logic_cm: [pallas::Base; 2] = + ResourceLogicCommitment::default().to_public_inputs(); + public_inputs.extend(default_resource_logic_cm); + public_inputs.extend(default_resource_logic_cm); + let padding = ResourceLogicPublicInputs::get_public_input_padding( public_inputs.len(), &RandomSeed::random(&mut rng), ); @@ -203,29 +212,29 @@ impl ValidityPredicateCircuit for TrivialValidityPredicateCircuit { } } -vp_circuit_impl!(TrivialValidityPredicateCircuit); +resource_logic_circuit_impl!(TrivialResourceLogicCircuit); -impl ValidityPredicateVerifyingInfo for TrivialValidityPredicateCircuit { - fn get_verifying_info(&self) -> VPVerifyingInfo { +impl ResourceLogicVerifyingInfoTrait for TrivialResourceLogicCircuit { + fn get_verifying_info(&self) -> ResourceLogicVerifyingInfo { let mut rng = OsRng; let params = SETUP_PARAMS_MAP.get(&15).unwrap(); let public_inputs = self.get_public_inputs(&mut rng); let proof = Proof::create( - &TRIVIAL_VP_PK, + &TRIVIAL_RESOURCE_LOGIC_PK, params, self.clone(), &[public_inputs.inner()], &mut rng, ) .unwrap(); - VPVerifyingInfo { - vk: TRIVIAL_VP_PK.get_vk().clone(), + ResourceLogicVerifyingInfo { + vk: TRIVIAL_RESOURCE_LOGIC_PK.get_vk().clone(), proof, public_inputs, } } - fn verify_transparently(&self) -> Result { + fn verify_transparently(&self) -> Result { use halo2_proofs::dev::MockProver; let mut rng = OsRng; let public_inputs = self.get_public_inputs(&mut rng); @@ -235,38 +244,40 @@ impl ValidityPredicateVerifyingInfo for TrivialValidityPredicateCircuit { Ok(public_inputs) } - fn get_vp_vk(&self) -> ValidityPredicateVerifyingKey { - TRIVIAL_VP_VK.clone() + fn get_resource_logic_vk(&self) -> ResourceLogicVerifyingKey { + TRIVIAL_RESOURCE_LOGIC_VK.clone() } } #[cfg(test)] pub mod tests { - use super::TrivialValidityPredicateCircuit; + use super::TrivialResourceLogicCircuit; use crate::{constant::NUM_RESOURCE, resource::tests::random_resource}; use ff::Field; use pasta_curves::pallas; use rand::RngCore; - pub fn random_trivial_vp_circuit(mut rng: R) -> TrivialValidityPredicateCircuit { + pub fn random_trivial_resource_logic_circuit( + mut rng: R, + ) -> TrivialResourceLogicCircuit { let owned_resource_id = pallas::Base::random(&mut rng); let input_resources = [(); NUM_RESOURCE].map(|_| random_resource(&mut rng)); let output_resources = [(); NUM_RESOURCE].map(|_| random_resource(&mut rng)); - TrivialValidityPredicateCircuit::new(owned_resource_id, input_resources, output_resources) + TrivialResourceLogicCircuit::new(owned_resource_id, input_resources, output_resources) } #[test] - fn test_halo2_trivial_vp_circuit() { - use crate::circuit::vp_circuit::ValidityPredicateCircuit; - use crate::constant::VP_CIRCUIT_PARAMS_SIZE; + fn test_halo2_trivial_resource_logic_circuit() { + use crate::circuit::resource_logic_circuit::ResourceLogicCircuit; + use crate::constant::RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE; use halo2_proofs::dev::MockProver; use rand::rngs::OsRng; let mut rng = OsRng; - let circuit = random_trivial_vp_circuit(&mut rng); + let circuit = random_trivial_resource_logic_circuit(&mut rng); let public_inputs = circuit.get_public_inputs(&mut rng); let prover = MockProver::::run( - VP_CIRCUIT_PARAMS_SIZE, + RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE, &circuit, vec![public_inputs.to_vec()], ) diff --git a/taiga_halo2/src/circuit/vp_examples/cascade_intent.rs b/taiga_halo2/src/circuit/resource_logic_examples/cascade_intent.rs similarity index 71% rename from taiga_halo2/src/circuit/vp_examples/cascade_intent.rs rename to taiga_halo2/src/circuit/resource_logic_examples/cascade_intent.rs index a4eaa0d3..51af856a 100644 --- a/taiga_halo2/src/circuit/vp_examples/cascade_intent.rs +++ b/taiga_halo2/src/circuit/resource_logic_examples/cascade_intent.rs @@ -1,20 +1,22 @@ -/// The intent is to show how to cascade partial transactions so they can be executed atomically. -/// In this example, Alice wants to spend three(more than the fixed NUM_RESOURCE) different kinds of tokens/resources simultaneously. -/// She needs to distribute the resources to two partial transactions. She can use the intent to cascade the partial transactions. -/// In the first partial transaction, she spends two resources and creates a cascade intent resource to encode and check the third resource info. -/// In the second partial transaction, she spends the cascade resource and the third resource. -/// +/// This example is to demonstrate how to cascade partial transactions for +/// atomic execution by the cascade intent. In this example, Alice wants to +/// simultaneously spend three different kinds of tokens/resources (more than +/// the fixed NUM_RESOURCE). She needs to distribute the resources into two +/// partial transactions and can utilize a cascade intent resource for encoding +/// and verifying the third resource information in the first transaction. In +/// the second transaction, she spends both the cascade resource and the third +/// resource. use crate::{ circuit::{ - blake2s::publicize_default_dynamic_vp_commitments, + blake2s::publicize_default_dynamic_resource_logic_commitments, gadgets::{ assign_free_advice, target_resource_variable::{get_is_input_resource_flag, get_owned_resource_variable}, }, - vp_bytecode::{ValidityPredicateByteCode, ValidityPredicateRepresentation}, - vp_circuit::{ - BasicValidityPredicateVariables, VPVerifyingInfo, ValidityPredicateCircuit, - ValidityPredicateConfig, ValidityPredicatePublicInputs, ValidityPredicateVerifyingInfo, + resource_logic_bytecode::{ResourceLogicByteCode, ResourceLogicRepresentation}, + resource_logic_circuit::{ + BasicResourceLogicVariables, ResourceLogicCircuit, ResourceLogicConfig, + ResourceLogicPublicInputs, ResourceLogicVerifyingInfo, ResourceLogicVerifyingInfoTrait, }, }, constant::{NUM_RESOURCE, SETUP_PARAMS_MAP}, @@ -22,9 +24,9 @@ use crate::{ nullifier::Nullifier, proof::Proof, resource::{RandomSeed, Resource}, + resource_logic_commitment::ResourceLogicCommitment, + resource_logic_vk::ResourceLogicVerifyingKey, utils::read_base_field, - vp_commitment::ValidityPredicateCommitment, - vp_vk::ValidityPredicateVerifyingKey, }; use borsh::{BorshDeserialize, BorshSerialize}; use halo2_proofs::arithmetic::Field; @@ -38,14 +40,14 @@ use rand::rngs::OsRng; use rand::RngCore; lazy_static! { - pub static ref CASCADE_INTENT_VK: ValidityPredicateVerifyingKey = - CascadeIntentValidityPredicateCircuit::default().get_vp_vk(); + pub static ref CASCADE_INTENT_VK: ResourceLogicVerifyingKey = + CascadeIntentResourceLogicCircuit::default().get_resource_logic_vk(); pub static ref COMPRESSED_CASCADE_INTENT_VK: pallas::Base = CASCADE_INTENT_VK.get_compressed(); } -// CascadeIntentValidityPredicateCircuit +// CascadeIntentResourceLogicCircuit #[derive(Clone, Debug, Default)] -pub struct CascadeIntentValidityPredicateCircuit { +pub struct CascadeIntentResourceLogicCircuit { pub owned_resource_id: pallas::Base, pub input_resources: [Resource; NUM_RESOURCE], pub output_resources: [Resource; NUM_RESOURCE], @@ -53,17 +55,14 @@ pub struct CascadeIntentValidityPredicateCircuit { pub cascade_resource_cm: pallas::Base, } -impl CascadeIntentValidityPredicateCircuit { +impl CascadeIntentResourceLogicCircuit { // We can encode at most three resources to label if needed. pub fn encode_label(cascade_resource_cm: pallas::Base) -> pallas::Base { cascade_resource_cm } - pub fn to_bytecode(&self) -> ValidityPredicateByteCode { - ValidityPredicateByteCode::new( - ValidityPredicateRepresentation::CascadeIntent, - self.to_bytes(), - ) + pub fn to_bytecode(&self) -> ResourceLogicByteCode { + ResourceLogicByteCode::new(ResourceLogicRepresentation::CascadeIntent, self.to_bytes()) } pub fn to_bytes(&self) -> Vec { @@ -75,13 +74,13 @@ impl CascadeIntentValidityPredicateCircuit { } } -impl ValidityPredicateCircuit for CascadeIntentValidityPredicateCircuit { +impl ResourceLogicCircuit for CascadeIntentResourceLogicCircuit { // Add custom constraints fn custom_constraints( &self, config: Self::Config, mut layouter: impl Layouter, - basic_variables: BasicValidityPredicateVariables, + basic_variables: BasicResourceLogicVariables, ) -> Result<(), Error> { let owned_resource_id = basic_variables.get_owned_resource_id(); let is_input_resource = get_is_input_resource_flag( @@ -127,8 +126,8 @@ impl ValidityPredicateCircuit for CascadeIntentValidityPredicateCircuit { }, )?; - // Publicize the dynamic vp commitments with default value - publicize_default_dynamic_vp_commitments( + // Publicize the dynamic resource_logic commitments with default value + publicize_default_dynamic_resource_logic_commitments( &mut layouter, config.advices[0], config.instances, @@ -145,13 +144,13 @@ impl ValidityPredicateCircuit for CascadeIntentValidityPredicateCircuit { &self.output_resources } - fn get_public_inputs(&self, mut rng: impl RngCore) -> ValidityPredicatePublicInputs { + fn get_public_inputs(&self, mut rng: impl RngCore) -> ResourceLogicPublicInputs { let mut public_inputs = self.get_mandatory_public_inputs(); - let default_vp_cm: [pallas::Base; 2] = - ValidityPredicateCommitment::default().to_public_inputs(); - public_inputs.extend(default_vp_cm); - public_inputs.extend(default_vp_cm); - let padding = ValidityPredicatePublicInputs::get_public_input_padding( + let default_resource_logic_cm: [pallas::Base; 2] = + ResourceLogicCommitment::default().to_public_inputs(); + public_inputs.extend(default_resource_logic_cm); + public_inputs.extend(default_resource_logic_cm); + let padding = ResourceLogicPublicInputs::get_public_input_padding( public_inputs.len(), &RandomSeed::random(&mut rng), ); @@ -164,10 +163,10 @@ impl ValidityPredicateCircuit for CascadeIntentValidityPredicateCircuit { } } -vp_circuit_impl!(CascadeIntentValidityPredicateCircuit); -vp_verifying_info_impl!(CascadeIntentValidityPredicateCircuit); +resource_logic_circuit_impl!(CascadeIntentResourceLogicCircuit); +resource_logic_verifying_info_impl!(CascadeIntentResourceLogicCircuit); -impl BorshSerialize for CascadeIntentValidityPredicateCircuit { +impl BorshSerialize for CascadeIntentResourceLogicCircuit { fn serialize(&self, writer: &mut W) -> std::io::Result<()> { writer.write_all(&self.owned_resource_id.to_repr())?; for input in self.input_resources.iter() { @@ -183,7 +182,7 @@ impl BorshSerialize for CascadeIntentValidityPredicateCircuit { } } -impl BorshDeserialize for CascadeIntentValidityPredicateCircuit { +impl BorshDeserialize for CascadeIntentResourceLogicCircuit { fn deserialize_reader(reader: &mut R) -> std::io::Result { let owned_resource_id = read_base_field(reader)?; let input_resources: Vec<_> = (0..NUM_RESOURCE) @@ -207,7 +206,7 @@ pub fn create_intent_resource( cascade_resource_cm: pallas::Base, nk: pallas::Base, ) -> Resource { - let label = CascadeIntentValidityPredicateCircuit::encode_label(cascade_resource_cm); + let label = CascadeIntentResourceLogicCircuit::encode_label(cascade_resource_cm); let rseed = pallas::Base::random(&mut rng); let nonce = Nullifier::random(&mut rng); Resource::new_input_resource( @@ -223,8 +222,8 @@ pub fn create_intent_resource( } #[test] -fn test_halo2_cascade_intent_vp_circuit() { - use crate::constant::VP_CIRCUIT_PARAMS_SIZE; +fn test_halo2_cascade_intent_resource_logic_circuit() { + use crate::constant::RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE; use crate::resource::tests::random_resource; use halo2_proofs::arithmetic::Field; use halo2_proofs::dev::MockProver; @@ -239,7 +238,7 @@ fn test_halo2_cascade_intent_vp_circuit() { let input_resources = [intent_resource, cascade_input_resource]; let output_resources = [(); NUM_RESOURCE].map(|_| random_resource(&mut rng)); - CascadeIntentValidityPredicateCircuit { + CascadeIntentResourceLogicCircuit { owned_resource_id: input_resources[0].get_nf().unwrap().inner(), input_resources, output_resources, @@ -250,13 +249,13 @@ fn test_halo2_cascade_intent_vp_circuit() { // Test serialization let circuit = { let circuit_bytes = circuit.to_bytes(); - CascadeIntentValidityPredicateCircuit::from_bytes(&circuit_bytes) + CascadeIntentResourceLogicCircuit::from_bytes(&circuit_bytes) }; let public_inputs = circuit.get_public_inputs(&mut rng); let prover = MockProver::::run( - VP_CIRCUIT_PARAMS_SIZE, + RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE, &circuit, vec![public_inputs.to_vec()], ) diff --git a/taiga_halo2/src/circuit/vp_examples/field_addition.rs b/taiga_halo2/src/circuit/resource_logic_examples/field_addition.rs similarity index 62% rename from taiga_halo2/src/circuit/vp_examples/field_addition.rs rename to taiga_halo2/src/circuit/resource_logic_examples/field_addition.rs index 96b739aa..2dae6a81 100644 --- a/taiga_halo2/src/circuit/vp_examples/field_addition.rs +++ b/taiga_halo2/src/circuit/resource_logic_examples/field_addition.rs @@ -1,21 +1,23 @@ use crate::{ circuit::{ - blake2s::publicize_default_dynamic_vp_commitments, + blake2s::publicize_default_dynamic_resource_logic_commitments, gadgets::{ add::{AddChip, AddInstructions}, assign_free_advice, }, - vp_circuit::{ - BasicValidityPredicateVariables, VPVerifyingInfo, ValidityPredicateCircuit, - ValidityPredicateConfig, ValidityPredicatePublicInputs, ValidityPredicateVerifyingInfo, + resource_logic_circuit::{ + BasicResourceLogicVariables, ResourceLogicCircuit, ResourceLogicConfig, + ResourceLogicPublicInputs, ResourceLogicVerifyingInfo, ResourceLogicVerifyingInfoTrait, }, }, - constant::{NUM_RESOURCE, SETUP_PARAMS_MAP, VP_CIRCUIT_CUSTOM_PUBLIC_INPUT_BEGIN_IDX}, + constant::{ + NUM_RESOURCE, RESOURCE_LOGIC_CIRCUIT_CUSTOM_PUBLIC_INPUT_BEGIN_IDX, SETUP_PARAMS_MAP, + }, error::TransactionError, proof::Proof, resource::{RandomSeed, Resource}, - vp_commitment::ValidityPredicateCommitment, - vp_vk::ValidityPredicateVerifyingKey, + resource_logic_commitment::ResourceLogicCommitment, + resource_logic_vk::ResourceLogicVerifyingKey, }; use halo2_proofs::{ circuit::{floor_planner, Layouter, Value}, @@ -25,9 +27,9 @@ use pasta_curves::pallas; use rand::rngs::OsRng; use rand::RngCore; -// FieldAdditionValidityPredicateCircuit with a trivial constraint a + b = c. +// FieldAdditionResourceLogicCircuit with a trivial constraint a + b = c. #[derive(Clone, Debug, Default)] -struct FieldAdditionValidityPredicateCircuit { +struct FieldAdditionResourceLogicCircuit { owned_resource_id: pallas::Base, input_resources: [Resource; NUM_RESOURCE], output_resources: [Resource; NUM_RESOURCE], @@ -35,14 +37,14 @@ struct FieldAdditionValidityPredicateCircuit { b: pallas::Base, } -impl ValidityPredicateCircuit for FieldAdditionValidityPredicateCircuit { +impl ResourceLogicCircuit for FieldAdditionResourceLogicCircuit { // Add custom constraints - // Resource: the trivial vp doesn't constrain on input_resource_variables and output_resource_variables + // Resource: the trivial resource_logic doesn't constrain on input_resource_variables and output_resource_variables fn custom_constraints( &self, config: Self::Config, mut layouter: impl Layouter, - _basic_variables: BasicValidityPredicateVariables, + _basic_variables: BasicResourceLogicVariables, ) -> Result<(), Error> { let a = assign_free_advice( layouter.namespace(|| "witness a"), @@ -64,11 +66,11 @@ impl ValidityPredicateCircuit for FieldAdditionValidityPredicateCircuit { layouter.constrain_instance( c.cell(), config.instances, - VP_CIRCUIT_CUSTOM_PUBLIC_INPUT_BEGIN_IDX, + RESOURCE_LOGIC_CIRCUIT_CUSTOM_PUBLIC_INPUT_BEGIN_IDX, )?; - // Publicize the dynamic vp commitments with default value - publicize_default_dynamic_vp_commitments( + // Publicize the dynamic resource_logic commitments with default value + publicize_default_dynamic_resource_logic_commitments( &mut layouter, config.advices[0], config.instances, @@ -85,14 +87,14 @@ impl ValidityPredicateCircuit for FieldAdditionValidityPredicateCircuit { &self.output_resources } - fn get_public_inputs(&self, mut rng: impl RngCore) -> ValidityPredicatePublicInputs { + fn get_public_inputs(&self, mut rng: impl RngCore) -> ResourceLogicPublicInputs { let mut public_inputs = self.get_mandatory_public_inputs(); - let default_vp_cm: [pallas::Base; 2] = - ValidityPredicateCommitment::default().to_public_inputs(); - public_inputs.extend(default_vp_cm); - public_inputs.extend(default_vp_cm); + let default_resource_logic_cm: [pallas::Base; 2] = + ResourceLogicCommitment::default().to_public_inputs(); + public_inputs.extend(default_resource_logic_cm); + public_inputs.extend(default_resource_logic_cm); public_inputs.push(self.a + self.b); - let padding = ValidityPredicatePublicInputs::get_public_input_padding( + let padding = ResourceLogicPublicInputs::get_public_input_padding( public_inputs.len(), &RandomSeed::random(&mut rng), ); @@ -105,12 +107,12 @@ impl ValidityPredicateCircuit for FieldAdditionValidityPredicateCircuit { } } -vp_circuit_impl!(FieldAdditionValidityPredicateCircuit); -vp_verifying_info_impl!(FieldAdditionValidityPredicateCircuit); +resource_logic_circuit_impl!(FieldAdditionResourceLogicCircuit); +resource_logic_verifying_info_impl!(FieldAdditionResourceLogicCircuit); #[test] -fn test_halo2_addition_vp_circuit() { - use crate::constant::VP_CIRCUIT_PARAMS_SIZE; +fn test_halo2_addition_resource_logic_circuit() { + use crate::constant::RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE; use crate::resource::tests::random_resource; use halo2_proofs::arithmetic::Field; use halo2_proofs::dev::MockProver; @@ -123,7 +125,7 @@ fn test_halo2_addition_vp_circuit() { let a = pallas::Base::random(&mut rng); let b = pallas::Base::random(&mut rng); let owned_resource_id = pallas::Base::random(&mut rng); - FieldAdditionValidityPredicateCircuit { + FieldAdditionResourceLogicCircuit { owned_resource_id, input_resources, output_resources, @@ -134,7 +136,7 @@ fn test_halo2_addition_vp_circuit() { let public_inputs = circuit.get_public_inputs(&mut rng); let prover = MockProver::::run( - VP_CIRCUIT_PARAMS_SIZE, + RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE, &circuit, vec![public_inputs.to_vec()], ) diff --git a/taiga_halo2/src/circuit/vp_examples/or_relation_intent.rs b/taiga_halo2/src/circuit/resource_logic_examples/or_relation_intent.rs similarity index 80% rename from taiga_halo2/src/circuit/vp_examples/or_relation_intent.rs rename to taiga_halo2/src/circuit/resource_logic_examples/or_relation_intent.rs index 427b04ff..4967db55 100644 --- a/taiga_halo2/src/circuit/vp_examples/or_relation_intent.rs +++ b/taiga_halo2/src/circuit/resource_logic_examples/or_relation_intent.rs @@ -1,31 +1,31 @@ -/// The intent can be satisfied with two conditions. -/// For example, Alice has 5 BTC and wants 1 Dolphin or 2 Monkeys. -/// Then Alice creates an intent with the "or relaiton". +/// The intent can be satisfied with two conditions. For instance, Alice has 5 +/// BTC and desires either 1 Dolphin or 2 Monkeys. Then Alice creates an intent +/// using the "or relation". /// use crate::{ circuit::{ - blake2s::publicize_default_dynamic_vp_commitments, + blake2s::publicize_default_dynamic_resource_logic_commitments, gadgets::{ assign_free_advice, poseidon_hash::poseidon_hash_gadget, target_resource_variable::{get_is_input_resource_flag, get_owned_resource_variable}, }, - vp_bytecode::{ValidityPredicateByteCode, ValidityPredicateRepresentation}, - vp_circuit::{ - BasicValidityPredicateVariables, VPVerifyingInfo, ValidityPredicateCircuit, - ValidityPredicateConfig, ValidityPredicatePublicInputs, ValidityPredicateVerifyingInfo, + resource_logic_bytecode::{ResourceLogicByteCode, ResourceLogicRepresentation}, + resource_logic_circuit::{ + BasicResourceLogicVariables, ResourceLogicCircuit, ResourceLogicConfig, + ResourceLogicPublicInputs, ResourceLogicVerifyingInfo, ResourceLogicVerifyingInfoTrait, }, - vp_examples::token::{Token, TOKEN_VK}, + resource_logic_examples::token::{Token, TOKEN_VK}, }, constant::{NUM_RESOURCE, SETUP_PARAMS_MAP}, error::TransactionError, nullifier::Nullifier, proof::Proof, resource::{RandomSeed, Resource}, + resource_logic_commitment::ResourceLogicCommitment, + resource_logic_vk::ResourceLogicVerifyingKey, utils::poseidon_hash_n, utils::read_base_field, - vp_commitment::ValidityPredicateCommitment, - vp_vk::ValidityPredicateVerifyingKey, }; use borsh::{BorshDeserialize, BorshSerialize}; use halo2_proofs::arithmetic::Field; @@ -39,15 +39,15 @@ use rand::rngs::OsRng; use rand::RngCore; lazy_static! { - pub static ref OR_RELATION_INTENT_VK: ValidityPredicateVerifyingKey = - OrRelationIntentValidityPredicateCircuit::default().get_vp_vk(); + pub static ref OR_RELATION_INTENT_VK: ResourceLogicVerifyingKey = + OrRelationIntentResourceLogicCircuit::default().get_resource_logic_vk(); pub static ref COMPRESSED_OR_RELATION_INTENT_VK: pallas::Base = OR_RELATION_INTENT_VK.get_compressed(); } -// OrRelationIntentValidityPredicateCircuit +// OrRelationIntentResourceLogicCircuit #[derive(Clone, Debug, Default)] -pub struct OrRelationIntentValidityPredicateCircuit { +pub struct OrRelationIntentResourceLogicCircuit { pub owned_resource_id: pallas::Base, pub input_resources: [Resource; NUM_RESOURCE], pub output_resources: [Resource; NUM_RESOURCE], @@ -57,7 +57,7 @@ pub struct OrRelationIntentValidityPredicateCircuit { pub receiver_value: pallas::Base, } -impl OrRelationIntentValidityPredicateCircuit { +impl OrRelationIntentResourceLogicCircuit { pub fn encode_label( token_1: &Token, token_2: &Token, @@ -79,9 +79,9 @@ impl OrRelationIntentValidityPredicateCircuit { ]) } - pub fn to_bytecode(&self) -> ValidityPredicateByteCode { - ValidityPredicateByteCode::new( - ValidityPredicateRepresentation::OrRelationIntent, + pub fn to_bytecode(&self) -> ResourceLogicByteCode { + ResourceLogicByteCode::new( + ResourceLogicRepresentation::OrRelationIntent, self.to_bytes(), ) } @@ -95,13 +95,13 @@ impl OrRelationIntentValidityPredicateCircuit { } } -impl ValidityPredicateCircuit for OrRelationIntentValidityPredicateCircuit { +impl ResourceLogicCircuit for OrRelationIntentResourceLogicCircuit { // Add custom constraints fn custom_constraints( &self, config: Self::Config, mut layouter: impl Layouter, - basic_variables: BasicValidityPredicateVariables, + basic_variables: BasicResourceLogicVariables, ) -> Result<(), Error> { let owned_resource_id = basic_variables.get_owned_resource_id(); let is_input_resource = get_is_input_resource_flag( @@ -112,8 +112,8 @@ impl ValidityPredicateCircuit for OrRelationIntentValidityPredicateCircuit { &basic_variables.get_output_resource_cms(), )?; - let token_vp_vk = assign_free_advice( - layouter.namespace(|| "witness token vp vk"), + let token_resource_logic_vk = assign_free_advice( + layouter.namespace(|| "witness token resource_logic vk"), config.advices[0], Value::known(TOKEN_VK.get_compressed()), )?; @@ -163,7 +163,7 @@ impl ValidityPredicateCircuit for OrRelationIntentValidityPredicateCircuit { token_quantity_1.clone(), token_property_2.clone(), token_quantity_2.clone(), - token_vp_vk.clone(), + token_resource_logic_vk.clone(), receiver_npk.clone(), receiver_value.clone(), ], @@ -183,13 +183,13 @@ impl ValidityPredicateCircuit for OrRelationIntentValidityPredicateCircuit { |mut region| region.constrain_equal(encoded_label.cell(), label.cell()), )?; - // check the vp vk of output resource + // check the resource_logic vk of output resource layouter.assign_region( - || "conditional equal: check vp vk", + || "conditional equal: check resource_logic vk", |mut region| { config.conditional_equal_config.assign_region( &is_input_resource, - &token_vp_vk, + &token_resource_logic_vk, &basic_variables.output_resource_variables[0] .resource_variables .logic, @@ -255,8 +255,8 @@ impl ValidityPredicateCircuit for OrRelationIntentValidityPredicateCircuit { }, )?; - // Publicize the dynamic vp commitments with default value - publicize_default_dynamic_vp_commitments( + // Publicize the dynamic resource_logic commitments with default value + publicize_default_dynamic_resource_logic_commitments( &mut layouter, config.advices[0], config.instances, @@ -273,13 +273,13 @@ impl ValidityPredicateCircuit for OrRelationIntentValidityPredicateCircuit { &self.output_resources } - fn get_public_inputs(&self, mut rng: impl RngCore) -> ValidityPredicatePublicInputs { + fn get_public_inputs(&self, mut rng: impl RngCore) -> ResourceLogicPublicInputs { let mut public_inputs = self.get_mandatory_public_inputs(); - let default_vp_cm: [pallas::Base; 2] = - ValidityPredicateCommitment::default().to_public_inputs(); - public_inputs.extend(default_vp_cm); - public_inputs.extend(default_vp_cm); - let padding = ValidityPredicatePublicInputs::get_public_input_padding( + let default_resource_logic_cm: [pallas::Base; 2] = + ResourceLogicCommitment::default().to_public_inputs(); + public_inputs.extend(default_resource_logic_cm); + public_inputs.extend(default_resource_logic_cm); + let padding = ResourceLogicPublicInputs::get_public_input_padding( public_inputs.len(), &RandomSeed::random(&mut rng), ); @@ -292,10 +292,10 @@ impl ValidityPredicateCircuit for OrRelationIntentValidityPredicateCircuit { } } -vp_circuit_impl!(OrRelationIntentValidityPredicateCircuit); -vp_verifying_info_impl!(OrRelationIntentValidityPredicateCircuit); +resource_logic_circuit_impl!(OrRelationIntentResourceLogicCircuit); +resource_logic_verifying_info_impl!(OrRelationIntentResourceLogicCircuit); -impl BorshSerialize for OrRelationIntentValidityPredicateCircuit { +impl BorshSerialize for OrRelationIntentResourceLogicCircuit { fn serialize(&self, writer: &mut W) -> std::io::Result<()> { writer.write_all(&self.owned_resource_id.to_repr())?; for input in self.input_resources.iter() { @@ -316,7 +316,7 @@ impl BorshSerialize for OrRelationIntentValidityPredicateCircuit { } } -impl BorshDeserialize for OrRelationIntentValidityPredicateCircuit { +impl BorshDeserialize for OrRelationIntentResourceLogicCircuit { fn deserialize_reader(reader: &mut R) -> std::io::Result { let owned_resource_id = read_base_field(reader)?; let input_resources: Vec<_> = (0..NUM_RESOURCE) @@ -349,7 +349,7 @@ pub fn create_intent_resource( receiver_value: pallas::Base, nk: pallas::Base, ) -> Resource { - let label = OrRelationIntentValidityPredicateCircuit::encode_label( + let label = OrRelationIntentResourceLogicCircuit::encode_label( token_1, token_2, receiver_npk, @@ -370,10 +370,11 @@ pub fn create_intent_resource( } #[test] -fn test_halo2_or_relation_intent_vp_circuit() { - use crate::constant::VP_CIRCUIT_PARAMS_SIZE; +fn test_halo2_or_relation_intent_resource_logic_circuit() { + use crate::constant::RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE; use crate::{ - circuit::vp_examples::token::COMPRESSED_TOKEN_VK, resource::tests::random_resource, + circuit::resource_logic_examples::token::COMPRESSED_TOKEN_VK, + resource::tests::random_resource, }; use halo2_proofs::arithmetic::Field; use halo2_proofs::dev::MockProver; @@ -400,7 +401,7 @@ fn test_halo2_or_relation_intent_vp_circuit() { ); let padding_input_resource = Resource::random_padding_resource(&mut rng); let input_resources = [intent_resource, padding_input_resource]; - OrRelationIntentValidityPredicateCircuit { + OrRelationIntentResourceLogicCircuit { owned_resource_id: input_resources[0].get_nf().unwrap().inner(), input_resources, output_resources, @@ -414,13 +415,13 @@ fn test_halo2_or_relation_intent_vp_circuit() { // Test serialization let circuit = { let circuit_bytes = circuit.to_bytes(); - OrRelationIntentValidityPredicateCircuit::from_bytes(&circuit_bytes) + OrRelationIntentResourceLogicCircuit::from_bytes(&circuit_bytes) }; let public_inputs = circuit.get_public_inputs(&mut rng); let prover = MockProver::::run( - VP_CIRCUIT_PARAMS_SIZE, + RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE, &circuit, vec![public_inputs.to_vec()], ) diff --git a/taiga_halo2/src/circuit/vp_examples/partial_fulfillment_intent.rs b/taiga_halo2/src/circuit/resource_logic_examples/partial_fulfillment_intent.rs similarity index 78% rename from taiga_halo2/src/circuit/vp_examples/partial_fulfillment_intent.rs rename to taiga_halo2/src/circuit/resource_logic_examples/partial_fulfillment_intent.rs index c3d2fb28..dfa3acaa 100644 --- a/taiga_halo2/src/circuit/vp_examples/partial_fulfillment_intent.rs +++ b/taiga_halo2/src/circuit/resource_logic_examples/partial_fulfillment_intent.rs @@ -1,29 +1,28 @@ -/// The intent can be partially fulfilled. -/// For example, Alice has 5 BTC and wants 10 ETH. -/// Alice utilizes this intent to do a partial swap in proportion. She can exchange 2 BTC for 4 ETH and get 3 BTC back. -/// +/// The intent can be "partially fulfilled". For instance, Alice has 5 BTC and +/// wants 10 ETH. Alice utilizes this intent to swap a portion proportionally, +/// exchanging 2 BTC for 4 ETH and receiving back 3 BTC. use crate::{ circuit::{ - blake2s::publicize_default_dynamic_vp_commitments, + blake2s::publicize_default_dynamic_resource_logic_commitments, gadgets::{ assign_free_constant, mul::MulChip, sub::{SubChip, SubInstructions}, target_resource_variable::{get_is_input_resource_flag, get_owned_resource_variable}, }, - vp_bytecode::{ValidityPredicateByteCode, ValidityPredicateRepresentation}, - vp_circuit::{ - BasicValidityPredicateVariables, VPVerifyingInfo, ValidityPredicateCircuit, - ValidityPredicateConfig, ValidityPredicatePublicInputs, ValidityPredicateVerifyingInfo, + resource_logic_bytecode::{ResourceLogicByteCode, ResourceLogicRepresentation}, + resource_logic_circuit::{ + BasicResourceLogicVariables, ResourceLogicCircuit, ResourceLogicConfig, + ResourceLogicPublicInputs, ResourceLogicVerifyingInfo, ResourceLogicVerifyingInfoTrait, }, }, constant::{NUM_RESOURCE, SETUP_PARAMS_MAP}, error::TransactionError, proof::Proof, resource::{RandomSeed, Resource}, + resource_logic_commitment::ResourceLogicCommitment, + resource_logic_vk::ResourceLogicVerifyingKey, utils::read_base_field, - vp_commitment::ValidityPredicateCommitment, - vp_vk::ValidityPredicateVerifyingKey, }; use borsh::{BorshDeserialize, BorshSerialize}; use halo2_proofs::{ @@ -42,25 +41,25 @@ mod label; use label::PartialFulfillmentIntentLabel; lazy_static! { - pub static ref PARTIAL_FULFILLMENT_INTENT_VK: ValidityPredicateVerifyingKey = - PartialFulfillmentIntentValidityPredicateCircuit::default().get_vp_vk(); + pub static ref PARTIAL_FULFILLMENT_INTENT_VK: ResourceLogicVerifyingKey = + PartialFulfillmentIntentResourceLogicCircuit::default().get_resource_logic_vk(); pub static ref COMPRESSED_PARTIAL_FULFILLMENT_INTENT_VK: pallas::Base = PARTIAL_FULFILLMENT_INTENT_VK.get_compressed(); } -// PartialFulfillmentIntentValidityPredicateCircuit +// PartialFulfillmentIntentResourceLogicCircuit #[derive(Clone, Debug, Default)] -pub struct PartialFulfillmentIntentValidityPredicateCircuit { +pub struct PartialFulfillmentIntentResourceLogicCircuit { pub owned_resource_id: pallas::Base, pub input_resources: [Resource; NUM_RESOURCE], pub output_resources: [Resource; NUM_RESOURCE], pub swap: Swap, } -impl PartialFulfillmentIntentValidityPredicateCircuit { - pub fn to_bytecode(&self) -> ValidityPredicateByteCode { - ValidityPredicateByteCode::new( - ValidityPredicateRepresentation::PartialFulfillmentIntent, +impl PartialFulfillmentIntentResourceLogicCircuit { + pub fn to_bytecode(&self) -> ResourceLogicByteCode { + ResourceLogicByteCode::new( + ResourceLogicRepresentation::PartialFulfillmentIntent, self.to_bytes(), ) } @@ -74,13 +73,13 @@ impl PartialFulfillmentIntentValidityPredicateCircuit { } } -impl ValidityPredicateCircuit for PartialFulfillmentIntentValidityPredicateCircuit { +impl ResourceLogicCircuit for PartialFulfillmentIntentResourceLogicCircuit { // Add custom constraints fn custom_constraints( &self, config: Self::Config, mut layouter: impl Layouter, - basic_variables: BasicValidityPredicateVariables, + basic_variables: BasicResourceLogicVariables, ) -> Result<(), Error> { let sub_chip = SubChip::construct(config.sub_config.clone(), ()); let mul_chip = MulChip::construct(config.mul_config.clone()); @@ -158,8 +157,8 @@ impl ValidityPredicateCircuit for PartialFulfillmentIntentValidityPredicateCircu layouter.namespace(|| "is_partial_fulfillment checks"), )?; - // Publicize the dynamic vp commitments with default value - publicize_default_dynamic_vp_commitments( + // Publicize the dynamic resource_logic commitments with default value + publicize_default_dynamic_resource_logic_commitments( &mut layouter, config.advices[0], config.instances, @@ -176,13 +175,13 @@ impl ValidityPredicateCircuit for PartialFulfillmentIntentValidityPredicateCircu &self.output_resources } - fn get_public_inputs(&self, mut rng: impl RngCore) -> ValidityPredicatePublicInputs { + fn get_public_inputs(&self, mut rng: impl RngCore) -> ResourceLogicPublicInputs { let mut public_inputs = self.get_mandatory_public_inputs(); - let default_vp_cm: [pallas::Base; 2] = - ValidityPredicateCommitment::default().to_public_inputs(); - public_inputs.extend(default_vp_cm); - public_inputs.extend(default_vp_cm); - let padding = ValidityPredicatePublicInputs::get_public_input_padding( + let default_resource_logic_cm: [pallas::Base; 2] = + ResourceLogicCommitment::default().to_public_inputs(); + public_inputs.extend(default_resource_logic_cm); + public_inputs.extend(default_resource_logic_cm); + let padding = ResourceLogicPublicInputs::get_public_input_padding( public_inputs.len(), &RandomSeed::random(&mut rng), ); @@ -195,10 +194,10 @@ impl ValidityPredicateCircuit for PartialFulfillmentIntentValidityPredicateCircu } } -vp_circuit_impl!(PartialFulfillmentIntentValidityPredicateCircuit); -vp_verifying_info_impl!(PartialFulfillmentIntentValidityPredicateCircuit); +resource_logic_circuit_impl!(PartialFulfillmentIntentResourceLogicCircuit); +resource_logic_verifying_info_impl!(PartialFulfillmentIntentResourceLogicCircuit); -impl BorshSerialize for PartialFulfillmentIntentValidityPredicateCircuit { +impl BorshSerialize for PartialFulfillmentIntentResourceLogicCircuit { fn serialize(&self, writer: &mut W) -> std::io::Result<()> { writer.write_all(&self.owned_resource_id.to_repr())?; for input in self.input_resources.iter() { @@ -215,7 +214,7 @@ impl BorshSerialize for PartialFulfillmentIntentValidityPredicateCircuit { } } -impl BorshDeserialize for PartialFulfillmentIntentValidityPredicateCircuit { +impl BorshDeserialize for PartialFulfillmentIntentResourceLogicCircuit { fn deserialize_reader(reader: &mut R) -> std::io::Result { let owned_resource_id = read_base_field(reader)?; let input_resources: Vec<_> = (0..NUM_RESOURCE) @@ -237,11 +236,11 @@ impl BorshDeserialize for PartialFulfillmentIntentValidityPredicateCircuit { #[cfg(test)] mod tests { use super::*; - use crate::circuit::vp_examples::{ + use crate::circuit::resource_logic_examples::{ signature_verification::COMPRESSED_TOKEN_AUTH_VK, token::{Token, TokenAuthorization}, }; - use crate::constant::VP_CIRCUIT_PARAMS_SIZE; + use crate::constant::RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE; use halo2_proofs::arithmetic::Field; use halo2_proofs::dev::MockProver; use rand::rngs::OsRng; @@ -270,7 +269,7 @@ mod tests { let input_resources = [*swap.sell.resource(), input_padding_resource]; let output_resources = [intent_resource, output_padding_resource]; - let circuit = PartialFulfillmentIntentValidityPredicateCircuit { + let circuit = PartialFulfillmentIntentResourceLogicCircuit { owned_resource_id: intent_resource.commitment().inner(), input_resources, output_resources, @@ -279,7 +278,7 @@ mod tests { let public_inputs = circuit.get_public_inputs(&mut rng); let prover = MockProver::::run( - VP_CIRCUIT_PARAMS_SIZE, + RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE, &circuit, vec![public_inputs.to_vec()], ) @@ -299,7 +298,7 @@ mod tests { let bob_sell = swap.buy.clone(); let (input_resources, output_resources) = swap.fill(&mut rng, intent_resource, bob_sell); - let circuit = PartialFulfillmentIntentValidityPredicateCircuit { + let circuit = PartialFulfillmentIntentResourceLogicCircuit { owned_resource_id: intent_resource.get_nf().unwrap().inner(), input_resources, output_resources, @@ -308,7 +307,7 @@ mod tests { let public_inputs = circuit.get_public_inputs(&mut rng); let prover = MockProver::::run( - VP_CIRCUIT_PARAMS_SIZE, + RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE, &circuit, vec![public_inputs.to_vec()], ) @@ -328,7 +327,7 @@ mod tests { let bob_sell = Token::new(swap.buy.name().inner().to_string(), 2u64); let (input_resources, output_resources) = swap.fill(&mut rng, intent_resource, bob_sell); - let circuit = PartialFulfillmentIntentValidityPredicateCircuit { + let circuit = PartialFulfillmentIntentResourceLogicCircuit { owned_resource_id: intent_resource.get_nf().unwrap().inner(), input_resources, output_resources, @@ -338,13 +337,13 @@ mod tests { // Test serialization let circuit = { let circuit_bytes = circuit.to_bytes(); - PartialFulfillmentIntentValidityPredicateCircuit::from_bytes(&circuit_bytes) + PartialFulfillmentIntentResourceLogicCircuit::from_bytes(&circuit_bytes) }; let public_inputs = circuit.get_public_inputs(&mut rng); let prover = MockProver::::run( - VP_CIRCUIT_PARAMS_SIZE, + RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE, &circuit, vec![public_inputs.to_vec()], ) diff --git a/taiga_halo2/src/circuit/vp_examples/partial_fulfillment_intent/label.rs b/taiga_halo2/src/circuit/resource_logic_examples/partial_fulfillment_intent/label.rs similarity index 94% rename from taiga_halo2/src/circuit/vp_examples/partial_fulfillment_intent/label.rs rename to taiga_halo2/src/circuit/resource_logic_examples/partial_fulfillment_intent/label.rs index 785c4330..2d477451 100644 --- a/taiga_halo2/src/circuit/vp_examples/partial_fulfillment_intent/label.rs +++ b/taiga_halo2/src/circuit/resource_logic_examples/partial_fulfillment_intent/label.rs @@ -5,7 +5,7 @@ use crate::circuit::{ poseidon_hash::poseidon_hash_gadget, sub::{SubChip, SubInstructions}, }, - vp_circuit::BasicValidityPredicateVariables, + resource_logic_circuit::BasicResourceLogicVariables, }; use halo2_gadgets::poseidon::Pow5Config as PoseidonConfig; use halo2_proofs::{ @@ -16,7 +16,7 @@ use pasta_curves::pallas; #[derive(Clone, Debug)] pub struct PartialFulfillmentIntentLabel { - pub token_vp_vk: AssignedCell, + pub token_resource_logic_vk: AssignedCell, pub sold_token: AssignedCell, pub sold_token_quantity: AssignedCell, pub bought_token: AssignedCell, @@ -40,7 +40,7 @@ impl PartialFulfillmentIntentLabel { self.sold_token_quantity.clone(), self.bought_token.clone(), self.bought_token_quantity.clone(), - self.token_vp_vk.clone(), + self.token_resource_logic_vk.clone(), self.receiver_npk.clone(), self.receiver_value.clone(), ], @@ -51,7 +51,7 @@ impl PartialFulfillmentIntentLabel { pub fn is_input_resource_checks( &self, is_input_resource: &AssignedCell, - basic_variables: &BasicValidityPredicateVariables, + basic_variables: &BasicResourceLogicVariables, config: &ConditionalEqualConfig, mut layouter: impl Layouter, ) -> Result<(), Error> { @@ -60,7 +60,7 @@ impl PartialFulfillmentIntentLabel { |mut region| { config.assign_region( is_input_resource, - &self.token_vp_vk, + &self.token_resource_logic_vk, &basic_variables.output_resource_variables[0] .resource_variables .logic, @@ -124,16 +124,16 @@ impl PartialFulfillmentIntentLabel { pub fn is_output_resource_checks( &self, is_output_resource: &AssignedCell, - basic_variables: &BasicValidityPredicateVariables, + basic_variables: &BasicResourceLogicVariables, config: &ConditionalEqualConfig, mut layouter: impl Layouter, ) -> Result<(), Error> { layouter.assign_region( - || "conditional equal: check sold token vp_vk", + || "conditional equal: check sold token resource_logic_vk", |mut region| { config.assign_region( is_output_resource, - &self.token_vp_vk, + &self.token_resource_logic_vk, &basic_variables.input_resource_variables[0] .resource_variables .logic, @@ -180,7 +180,7 @@ impl PartialFulfillmentIntentLabel { pub fn is_partial_fulfillment_checks( &self, is_input_resource: &AssignedCell, - basic_variables: &BasicValidityPredicateVariables, + basic_variables: &BasicResourceLogicVariables, config: &ConditionalEqualConfig, sub_chip: &SubChip, mul_chip: &MulChip, @@ -210,7 +210,7 @@ impl PartialFulfillmentIntentLabel { |mut region| { config.assign_region( &is_partial_fulfillment, - &self.token_vp_vk, + &self.token_resource_logic_vk, &basic_variables.output_resource_variables[1] .resource_variables .logic, diff --git a/taiga_halo2/src/circuit/vp_examples/partial_fulfillment_intent/swap.rs b/taiga_halo2/src/circuit/resource_logic_examples/partial_fulfillment_intent/swap.rs similarity index 95% rename from taiga_halo2/src/circuit/vp_examples/partial_fulfillment_intent/swap.rs rename to taiga_halo2/src/circuit/resource_logic_examples/partial_fulfillment_intent/swap.rs index ae391226..0aed6188 100644 --- a/taiga_halo2/src/circuit/vp_examples/partial_fulfillment_intent/swap.rs +++ b/taiga_halo2/src/circuit/resource_logic_examples/partial_fulfillment_intent/swap.rs @@ -2,7 +2,7 @@ use super::{PartialFulfillmentIntentLabel, COMPRESSED_PARTIAL_FULFILLMENT_INTENT use crate::{ circuit::{ gadgets::assign_free_advice, - vp_examples::token::{Token, TokenAuthorization, TokenResource, TOKEN_VK}, + resource_logic_examples::token::{Token, TokenAuthorization, TokenResource, TOKEN_VK}, }, constant::NUM_RESOURCE, resource::Resource, @@ -122,8 +122,8 @@ impl Swap { column: Column, mut layouter: impl Layouter, ) -> Result { - let token_vp_vk = assign_free_advice( - layouter.namespace(|| "witness token vp vk"), + let token_resource_logic_vk = assign_free_advice( + layouter.namespace(|| "witness token resource_logic vk"), column, Value::known(TOKEN_VK.get_compressed()), )?; @@ -165,7 +165,7 @@ impl Swap { )?; Ok(PartialFulfillmentIntentLabel { - token_vp_vk, + token_resource_logic_vk, sold_token, sold_token_quantity, bought_token, diff --git a/taiga_halo2/src/circuit/vp_examples/receiver_vp.rs b/taiga_halo2/src/circuit/resource_logic_examples/receiver_resource_logic.rs similarity index 78% rename from taiga_halo2/src/circuit/vp_examples/receiver_vp.rs rename to taiga_halo2/src/circuit/resource_logic_examples/receiver_resource_logic.rs index 4cead5c9..544bddf9 100644 --- a/taiga_halo2/src/circuit/vp_examples/receiver_vp.rs +++ b/taiga_halo2/src/circuit/resource_logic_examples/receiver_resource_logic.rs @@ -1,26 +1,26 @@ use crate::{ circuit::{ - blake2s::publicize_default_dynamic_vp_commitments, + blake2s::publicize_default_dynamic_resource_logic_commitments, gadgets::{ add::AddChip, assign_free_advice, poseidon_hash::poseidon_hash_gadget, target_resource_variable::get_owned_resource_variable, }, resource_encryption_circuit::resource_encryption_gadget, - vp_bytecode::{ValidityPredicateByteCode, ValidityPredicateRepresentation}, - vp_circuit::{ - BasicValidityPredicateVariables, VPVerifyingInfo, ValidityPredicateCircuit, - ValidityPredicateConfig, ValidityPredicatePublicInputs, ValidityPredicateVerifyingInfo, + resource_logic_bytecode::{ResourceLogicByteCode, ResourceLogicRepresentation}, + resource_logic_circuit::{ + BasicResourceLogicVariables, ResourceLogicCircuit, ResourceLogicConfig, + ResourceLogicPublicInputs, ResourceLogicVerifyingInfo, ResourceLogicVerifyingInfoTrait, }, - vp_examples::signature_verification::COMPRESSED_TOKEN_AUTH_VK, + resource_logic_examples::signature_verification::COMPRESSED_TOKEN_AUTH_VK, }, constant::{GENERATOR, NUM_RESOURCE, SETUP_PARAMS_MAP}, error::TransactionError, proof::Proof, resource::{RandomSeed, Resource}, resource_encryption::{ResourceCiphertext, ResourcePlaintext, SecretKey}, + resource_logic_commitment::ResourceLogicCommitment, + resource_logic_vk::ResourceLogicVerifyingKey, utils::{mod_r_p, read_base_field, read_point}, - vp_commitment::ValidityPredicateCommitment, - vp_vk::ValidityPredicateVerifyingKey, }; use borsh::{BorshDeserialize, BorshSerialize}; use group::{cofactor::CofactorCurveAffine, ff::PrimeField, Curve, Group, GroupEncoding}; @@ -38,27 +38,27 @@ use rand::RngCore; const CIPHER_LEN: usize = 9; lazy_static! { - pub static ref RECEIVER_VK: ValidityPredicateVerifyingKey = - ReceiverValidityPredicateCircuit::default().get_vp_vk(); + pub static ref RECEIVER_VK: ResourceLogicVerifyingKey = + ReceiverResourceLogicCircuit::default().get_resource_logic_vk(); pub static ref COMPRESSED_RECEIVER_VK: pallas::Base = RECEIVER_VK.get_compressed(); } -// ReceiverValidityPredicateCircuit is used in the token vp as dynamic vp and contains the resource encryption constraints. +// ReceiverResourceLogicCircuit is used in the token resource_logic as dynamic resource_logic and contains the resource encryption constraints. #[derive(Clone, Debug)] -pub struct ReceiverValidityPredicateCircuit { +pub struct ReceiverResourceLogicCircuit { pub owned_resource_id: pallas::Base, pub input_resources: [Resource; NUM_RESOURCE], pub output_resources: [Resource; NUM_RESOURCE], - pub vp_vk: pallas::Base, + pub resource_logic_vk: pallas::Base, pub encrypt_nonce: pallas::Base, pub sk: pallas::Base, pub rcv_pk: pallas::Point, - pub auth_vp_vk: pallas::Base, + pub auth_resource_logic_vk: pallas::Base, } -impl ReceiverValidityPredicateCircuit { - pub fn to_bytecode(&self) -> ValidityPredicateByteCode { - ValidityPredicateByteCode::new(ValidityPredicateRepresentation::Receiver, self.to_bytes()) +impl ReceiverResourceLogicCircuit { + pub fn to_bytecode(&self) -> ResourceLogicByteCode { + ResourceLogicByteCode::new(ResourceLogicRepresentation::Receiver, self.to_bytes()) } pub fn to_bytes(&self) -> Vec { @@ -70,28 +70,28 @@ impl ReceiverValidityPredicateCircuit { } } -impl Default for ReceiverValidityPredicateCircuit { +impl Default for ReceiverResourceLogicCircuit { fn default() -> Self { Self { owned_resource_id: pallas::Base::zero(), input_resources: [(); NUM_RESOURCE].map(|_| Resource::default()), output_resources: [(); NUM_RESOURCE].map(|_| Resource::default()), - vp_vk: pallas::Base::zero(), + resource_logic_vk: pallas::Base::zero(), encrypt_nonce: pallas::Base::zero(), sk: pallas::Base::zero(), rcv_pk: pallas::Point::generator(), - auth_vp_vk: pallas::Base::zero(), + auth_resource_logic_vk: pallas::Base::zero(), } } } -impl ValidityPredicateCircuit for ReceiverValidityPredicateCircuit { +impl ResourceLogicCircuit for ReceiverResourceLogicCircuit { // Add custom constraints fn custom_constraints( &self, config: Self::Config, mut layouter: impl Layouter, - basic_variables: BasicValidityPredicateVariables, + basic_variables: BasicResourceLogicVariables, ) -> Result<(), Error> { let encrypt_nonce = assign_free_advice( layouter.namespace(|| "witness encrypt_nonce"), @@ -122,15 +122,15 @@ impl ValidityPredicateCircuit for ReceiverValidityPredicateCircuit { &basic_variables.get_value_searchable_pairs(), )?; - let auth_vp_vk = assign_free_advice( - layouter.namespace(|| "witness auth vp vk"), + let auth_resource_logic_vk = assign_free_advice( + layouter.namespace(|| "witness auth resource_logic vk"), config.advices[0], Value::known(*COMPRESSED_TOKEN_AUTH_VK), )?; - let receiver_vp_vk = assign_free_advice( - layouter.namespace(|| "witness receiver vp vk"), + let receiver_resource_logic_vk = assign_free_advice( + layouter.namespace(|| "witness receiver resource_logic vk"), config.advices[0], - Value::known(self.vp_vk), + Value::known(self.resource_logic_vk), )?; // Decode the value, and check the value encoding @@ -140,8 +140,8 @@ impl ValidityPredicateCircuit for ReceiverValidityPredicateCircuit { [ rcv_pk.inner().x(), rcv_pk.inner().y(), - auth_vp_vk, - receiver_vp_vk, + auth_resource_logic_vk, + receiver_resource_logic_vk, ], )?; @@ -229,8 +229,8 @@ impl ValidityPredicateCircuit for ReceiverValidityPredicateCircuit { &mut message, )?; - // Publicize the dynamic vp commitments with default value - publicize_default_dynamic_vp_commitments( + // Publicize the dynamic resource_logic commitments with default value + publicize_default_dynamic_resource_logic_commitments( &mut layouter, config.advices[0], config.instances, @@ -247,14 +247,14 @@ impl ValidityPredicateCircuit for ReceiverValidityPredicateCircuit { &self.output_resources } - fn get_public_inputs(&self, rng: impl RngCore) -> ValidityPredicatePublicInputs { + fn get_public_inputs(&self, rng: impl RngCore) -> ResourceLogicPublicInputs { let mut public_inputs = self.get_mandatory_public_inputs(); - let default_vp_cm: [pallas::Base; 2] = - ValidityPredicateCommitment::default().to_public_inputs(); - public_inputs.extend(default_vp_cm); - public_inputs.extend(default_vp_cm); + let default_resource_logic_cm: [pallas::Base; 2] = + ResourceLogicCommitment::default().to_public_inputs(); + public_inputs.extend(default_resource_logic_cm); + public_inputs.extend(default_resource_logic_cm); let custom_public_input_padding = - ValidityPredicatePublicInputs::get_custom_public_input_padding( + ResourceLogicPublicInputs::get_custom_public_input_padding( public_inputs.len(), &RandomSeed::random(rng), ); @@ -295,10 +295,10 @@ impl ValidityPredicateCircuit for ReceiverValidityPredicateCircuit { } } -vp_circuit_impl!(ReceiverValidityPredicateCircuit); -vp_verifying_info_impl!(ReceiverValidityPredicateCircuit); +resource_logic_circuit_impl!(ReceiverResourceLogicCircuit); +resource_logic_verifying_info_impl!(ReceiverResourceLogicCircuit); -impl BorshSerialize for ReceiverValidityPredicateCircuit { +impl BorshSerialize for ReceiverResourceLogicCircuit { fn serialize(&self, writer: &mut W) -> std::io::Result<()> { writer.write_all(&self.owned_resource_id.to_repr())?; for input in self.input_resources.iter() { @@ -309,17 +309,17 @@ impl BorshSerialize for ReceiverValidityPredicateCircuit { output.serialize(writer)?; } - writer.write_all(&self.vp_vk.to_repr())?; + writer.write_all(&self.resource_logic_vk.to_repr())?; writer.write_all(&self.encrypt_nonce.to_repr())?; writer.write_all(&self.sk.to_repr())?; writer.write_all(&self.rcv_pk.to_bytes())?; - writer.write_all(&self.auth_vp_vk.to_repr())?; + writer.write_all(&self.auth_resource_logic_vk.to_repr())?; Ok(()) } } -impl BorshDeserialize for ReceiverValidityPredicateCircuit { +impl BorshDeserialize for ReceiverResourceLogicCircuit { fn deserialize_reader(reader: &mut R) -> std::io::Result { let owned_resource_id = read_base_field(reader)?; let input_resources: Vec<_> = (0..NUM_RESOURCE) @@ -328,27 +328,27 @@ impl BorshDeserialize for ReceiverValidityPredicateCircuit { let output_resources: Vec<_> = (0..NUM_RESOURCE) .map(|_| Resource::deserialize_reader(reader)) .collect::>()?; - let vp_vk = read_base_field(reader)?; + let resource_logic_vk = read_base_field(reader)?; let encrypt_nonce = read_base_field(reader)?; let sk = read_base_field(reader)?; let rcv_pk = read_point(reader)?; - let auth_vp_vk = read_base_field(reader)?; + let auth_resource_logic_vk = read_base_field(reader)?; Ok(Self { owned_resource_id, input_resources: input_resources.try_into().unwrap(), output_resources: output_resources.try_into().unwrap(), - vp_vk, + resource_logic_vk, encrypt_nonce, sk, rcv_pk, - auth_vp_vk, + auth_resource_logic_vk, }) } } #[test] -fn test_halo2_receiver_vp_circuit() { - use crate::constant::VP_CIRCUIT_PARAMS_SIZE; +fn test_halo2_receiver_resource_logic_circuit() { + use crate::constant::RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE; use crate::{resource::tests::random_resource, utils::poseidon_hash_n}; use ff::{Field, PrimeField}; use halo2_proofs::dev::MockProver; @@ -372,15 +372,15 @@ fn test_halo2_receiver_vp_circuit() { ]); let owned_resource_id = output_resources[0].commitment().inner(); ( - ReceiverValidityPredicateCircuit { + ReceiverResourceLogicCircuit { owned_resource_id, input_resources, output_resources, - vp_vk: *COMPRESSED_RECEIVER_VK, + resource_logic_vk: *COMPRESSED_RECEIVER_VK, encrypt_nonce, sk, rcv_pk, - auth_vp_vk: *COMPRESSED_TOKEN_AUTH_VK, + auth_resource_logic_vk: *COMPRESSED_TOKEN_AUTH_VK, }, rcv_sk, ) @@ -389,13 +389,13 @@ fn test_halo2_receiver_vp_circuit() { // Test serialization let circuit = { let circuit_bytes = circuit.to_bytes(); - ReceiverValidityPredicateCircuit::from_bytes(&circuit_bytes) + ReceiverResourceLogicCircuit::from_bytes(&circuit_bytes) }; let public_inputs = circuit.get_public_inputs(&mut rng); let prover = MockProver::::run( - VP_CIRCUIT_PARAMS_SIZE, + RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE, &circuit, vec![public_inputs.to_vec()], ) diff --git a/taiga_halo2/src/circuit/vp_examples/signature_verification.rs b/taiga_halo2/src/circuit/resource_logic_examples/signature_verification.rs similarity index 75% rename from taiga_halo2/src/circuit/vp_examples/signature_verification.rs rename to taiga_halo2/src/circuit/resource_logic_examples/signature_verification.rs index 7ee323b4..5a891e31 100644 --- a/taiga_halo2/src/circuit/vp_examples/signature_verification.rs +++ b/taiga_halo2/src/circuit/resource_logic_examples/signature_verification.rs @@ -1,23 +1,23 @@ use crate::{ circuit::{ - blake2s::publicize_default_dynamic_vp_commitments, + blake2s::publicize_default_dynamic_resource_logic_commitments, gadgets::{ assign_free_advice, poseidon_hash::poseidon_hash_gadget, target_resource_variable::get_owned_resource_variable, }, - vp_bytecode::{ValidityPredicateByteCode, ValidityPredicateRepresentation}, - vp_circuit::{ - BasicValidityPredicateVariables, VPVerifyingInfo, ValidityPredicateCircuit, - ValidityPredicateConfig, ValidityPredicatePublicInputs, ValidityPredicateVerifyingInfo, + resource_logic_bytecode::{ResourceLogicByteCode, ResourceLogicRepresentation}, + resource_logic_circuit::{ + BasicResourceLogicVariables, ResourceLogicCircuit, ResourceLogicConfig, + ResourceLogicPublicInputs, ResourceLogicVerifyingInfo, ResourceLogicVerifyingInfoTrait, }, }, constant::{TaigaFixedBasesFull, NUM_RESOURCE, SETUP_PARAMS_MAP}, error::TransactionError, proof::Proof, resource::{RandomSeed, Resource}, + resource_logic_commitment::ResourceLogicCommitment, + resource_logic_vk::ResourceLogicVerifyingKey, utils::{mod_r_p, poseidon_hash_n, read_base_field, read_point, read_scalar_field}, - vp_commitment::ValidityPredicateCommitment, - vp_vk::ValidityPredicateVerifyingKey, }; use borsh::{BorshDeserialize, BorshSerialize}; use halo2_gadgets::ecc::{chip::EccChip, FixedPoint, NonIdentityPoint, ScalarFixed, ScalarVar}; @@ -39,8 +39,8 @@ use rand::RngCore; const MESSAGE_LEN: usize = NUM_RESOURCE * 2; const POSEIDON_HASH_LEN: usize = MESSAGE_LEN + 4; lazy_static! { - pub static ref TOKEN_AUTH_VK: ValidityPredicateVerifyingKey = - SignatureVerificationValidityPredicateCircuit::default().get_vp_vk(); + pub static ref TOKEN_AUTH_VK: ResourceLogicVerifyingKey = + SignatureVerificationResourceLogicCircuit::default().get_resource_logic_vk(); pub static ref COMPRESSED_TOKEN_AUTH_VK: pallas::Base = TOKEN_AUTH_VK.get_compressed(); } @@ -91,33 +91,33 @@ impl SchnorrSignature { } } -// SignatureVerificationValidityPredicateCircuit uses the schnorr signature. +// SignatureVerificationResourceLogicCircuit uses the schnorr signature. #[derive(Clone, Debug, Default)] -pub struct SignatureVerificationValidityPredicateCircuit { +pub struct SignatureVerificationResourceLogicCircuit { pub owned_resource_id: pallas::Base, pub input_resources: [Resource; NUM_RESOURCE], pub output_resources: [Resource; NUM_RESOURCE], - pub vp_vk: pallas::Base, + pub resource_logic_vk: pallas::Base, pub signature: SchnorrSignature, - pub receiver_vp_vk: pallas::Base, + pub receiver_resource_logic_vk: pallas::Base, } -impl SignatureVerificationValidityPredicateCircuit { +impl SignatureVerificationResourceLogicCircuit { pub fn new( owned_resource_id: pallas::Base, input_resources: [Resource; NUM_RESOURCE], output_resources: [Resource; NUM_RESOURCE], - vp_vk: pallas::Base, + resource_logic_vk: pallas::Base, signature: SchnorrSignature, - receiver_vp_vk: pallas::Base, + receiver_resource_logic_vk: pallas::Base, ) -> Self { Self { owned_resource_id, input_resources, output_resources, - vp_vk, + resource_logic_vk, signature, - receiver_vp_vk, + receiver_resource_logic_vk, } } @@ -126,9 +126,9 @@ impl SignatureVerificationValidityPredicateCircuit { owned_resource_id: pallas::Base, input_resources: [Resource; NUM_RESOURCE], output_resources: [Resource; NUM_RESOURCE], - vp_vk: pallas::Base, + resource_logic_vk: pallas::Base, sk: pallas::Scalar, - receiver_vp_vk: pallas::Base, + receiver_resource_logic_vk: pallas::Base, ) -> Self { assert_eq!(NUM_RESOURCE, 2); let mut message = vec![]; @@ -146,15 +146,15 @@ impl SignatureVerificationValidityPredicateCircuit { owned_resource_id, input_resources, output_resources, - vp_vk, + resource_logic_vk, signature, - receiver_vp_vk, + receiver_resource_logic_vk, } } - pub fn to_bytecode(&self) -> ValidityPredicateByteCode { - ValidityPredicateByteCode::new( - ValidityPredicateRepresentation::SignatureVerification, + pub fn to_bytecode(&self) -> ResourceLogicByteCode { + ResourceLogicByteCode::new( + ResourceLogicRepresentation::SignatureVerification, self.to_bytes(), ) } @@ -168,13 +168,13 @@ impl SignatureVerificationValidityPredicateCircuit { } } -impl ValidityPredicateCircuit for SignatureVerificationValidityPredicateCircuit { +impl ResourceLogicCircuit for SignatureVerificationResourceLogicCircuit { // Add custom constraints fn custom_constraints( &self, config: Self::Config, mut layouter: impl Layouter, - basic_variables: BasicValidityPredicateVariables, + basic_variables: BasicResourceLogicVariables, ) -> Result<(), Error> { // Construct an ECC chip let ecc_chip = EccChip::construct(config.ecc_config); @@ -194,22 +194,27 @@ impl ValidityPredicateCircuit for SignatureVerificationValidityPredicateCircuit &basic_variables.get_value_searchable_pairs(), )?; - let auth_vp_vk = assign_free_advice( - layouter.namespace(|| "witness auth vp vk"), + let auth_resource_logic_vk = assign_free_advice( + layouter.namespace(|| "witness auth resource_logic vk"), config.advices[0], - Value::known(self.vp_vk), + Value::known(self.resource_logic_vk), )?; - let receiver_vp_vk = assign_free_advice( - layouter.namespace(|| "witness receiver vp vk"), + let receiver_resource_logic_vk = assign_free_advice( + layouter.namespace(|| "witness receiver resource_logic vk"), config.advices[0], - Value::known(self.receiver_vp_vk), + Value::known(self.receiver_resource_logic_vk), )?; // Decode the value, and check the value encoding let encoded_value = poseidon_hash_gadget( config.poseidon_config.clone(), layouter.namespace(|| "value encoding"), - [pk.inner().x(), pk.inner().y(), auth_vp_vk, receiver_vp_vk], + [ + pk.inner().x(), + pk.inner().y(), + auth_resource_logic_vk, + receiver_resource_logic_vk, + ], )?; layouter.assign_region( @@ -265,8 +270,8 @@ impl ValidityPredicateCircuit for SignatureVerificationValidityPredicateCircuit s_g.constrain_equal(layouter.namespace(|| "s*G = R + Hash(r||P||m)*P"), &rhs)?; - // Publicize the dynamic vp commitments with default value - publicize_default_dynamic_vp_commitments( + // Publicize the dynamic resource_logic commitments with default value + publicize_default_dynamic_resource_logic_commitments( &mut layouter, config.advices[0], config.instances, @@ -283,13 +288,13 @@ impl ValidityPredicateCircuit for SignatureVerificationValidityPredicateCircuit &self.output_resources } - fn get_public_inputs(&self, mut rng: impl RngCore) -> ValidityPredicatePublicInputs { + fn get_public_inputs(&self, mut rng: impl RngCore) -> ResourceLogicPublicInputs { let mut public_inputs = self.get_mandatory_public_inputs(); - let default_vp_cm: [pallas::Base; 2] = - ValidityPredicateCommitment::default().to_public_inputs(); - public_inputs.extend(default_vp_cm); - public_inputs.extend(default_vp_cm); - let padding = ValidityPredicatePublicInputs::get_public_input_padding( + let default_resource_logic_cm: [pallas::Base; 2] = + ResourceLogicCommitment::default().to_public_inputs(); + public_inputs.extend(default_resource_logic_cm); + public_inputs.extend(default_resource_logic_cm); + let padding = ResourceLogicPublicInputs::get_public_input_padding( public_inputs.len(), &RandomSeed::random(&mut rng), ); @@ -302,10 +307,10 @@ impl ValidityPredicateCircuit for SignatureVerificationValidityPredicateCircuit } } -vp_circuit_impl!(SignatureVerificationValidityPredicateCircuit); -vp_verifying_info_impl!(SignatureVerificationValidityPredicateCircuit); +resource_logic_circuit_impl!(SignatureVerificationResourceLogicCircuit); +resource_logic_verifying_info_impl!(SignatureVerificationResourceLogicCircuit); -impl BorshSerialize for SignatureVerificationValidityPredicateCircuit { +impl BorshSerialize for SignatureVerificationResourceLogicCircuit { fn serialize(&self, writer: &mut W) -> std::io::Result<()> { writer.write_all(&self.owned_resource_id.to_repr())?; for input in self.input_resources.iter() { @@ -316,15 +321,15 @@ impl BorshSerialize for SignatureVerificationValidityPredicateCircuit { output.serialize(writer)?; } - writer.write_all(&self.vp_vk.to_repr())?; + writer.write_all(&self.resource_logic_vk.to_repr())?; self.signature.serialize(writer)?; - writer.write_all(&self.receiver_vp_vk.to_repr())?; + writer.write_all(&self.receiver_resource_logic_vk.to_repr())?; Ok(()) } } -impl BorshDeserialize for SignatureVerificationValidityPredicateCircuit { +impl BorshDeserialize for SignatureVerificationResourceLogicCircuit { fn deserialize_reader(reader: &mut R) -> std::io::Result { let owned_resource_id = read_base_field(reader)?; let input_resources: Vec<_> = (0..NUM_RESOURCE) @@ -333,16 +338,16 @@ impl BorshDeserialize for SignatureVerificationValidityPredicateCircuit { let output_resources: Vec<_> = (0..NUM_RESOURCE) .map(|_| Resource::deserialize_reader(reader)) .collect::>()?; - let vp_vk = read_base_field(reader)?; + let resource_logic_vk = read_base_field(reader)?; let signature = SchnorrSignature::deserialize_reader(reader)?; - let receiver_vp_vk = read_base_field(reader)?; + let receiver_resource_logic_vk = read_base_field(reader)?; Ok(Self { owned_resource_id, input_resources: input_resources.try_into().unwrap(), output_resources: output_resources.try_into().unwrap(), - vp_vk, + resource_logic_vk, signature, - receiver_vp_vk, + receiver_resource_logic_vk, }) } } @@ -367,11 +372,11 @@ impl BorshDeserialize for SchnorrSignature { } #[test] -fn test_halo2_sig_verification_vp_circuit() { - use crate::circuit::vp_examples::{ - receiver_vp::COMPRESSED_RECEIVER_VK, token::TokenAuthorization, +fn test_halo2_sig_verification_resource_logic_circuit() { + use crate::circuit::resource_logic_examples::{ + receiver_resource_logic::COMPRESSED_RECEIVER_VK, token::TokenAuthorization, }; - use crate::constant::VP_CIRCUIT_PARAMS_SIZE; + use crate::constant::RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE; use crate::resource::tests::random_resource; use halo2_proofs::dev::MockProver; use rand::rngs::OsRng; @@ -385,7 +390,7 @@ fn test_halo2_sig_verification_vp_circuit() { let auth = TokenAuthorization::from_sk_vk(&sk, &auth_vk); input_resources[0].value = auth.to_value(); let owned_resource_id = input_resources[0].get_nf().unwrap().inner(); - SignatureVerificationValidityPredicateCircuit::from_sk_and_sign( + SignatureVerificationResourceLogicCircuit::from_sk_and_sign( &mut rng, owned_resource_id, input_resources, @@ -399,13 +404,13 @@ fn test_halo2_sig_verification_vp_circuit() { // Test serialization let circuit = { let circuit_bytes = circuit.to_bytes(); - SignatureVerificationValidityPredicateCircuit::from_bytes(&circuit_bytes) + SignatureVerificationResourceLogicCircuit::from_bytes(&circuit_bytes) }; let public_inputs = circuit.get_public_inputs(&mut rng); let prover = MockProver::::run( - VP_CIRCUIT_PARAMS_SIZE, + RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE, &circuit, vec![public_inputs.to_vec()], ) diff --git a/taiga_halo2/src/circuit/vp_examples/token.rs b/taiga_halo2/src/circuit/resource_logic_examples/token.rs similarity index 69% rename from taiga_halo2/src/circuit/vp_examples/token.rs rename to taiga_halo2/src/circuit/resource_logic_examples/token.rs index 1b3329b0..571afba6 100644 --- a/taiga_halo2/src/circuit/vp_examples/token.rs +++ b/taiga_halo2/src/circuit/resource_logic_examples/token.rs @@ -1,33 +1,37 @@ use crate::{ circuit::{ - blake2s::{vp_commitment_gadget, Blake2sChip}, + blake2s::{resource_logic_commitment_gadget, Blake2sChip}, gadgets::{ assign_free_advice, assign_free_constant, poseidon_hash::poseidon_hash_gadget, target_resource_variable::{get_is_input_resource_flag, get_owned_resource_variable}, }, - vp_bytecode::{ValidityPredicateByteCode, ValidityPredicateRepresentation}, - vp_circuit::{ - BasicValidityPredicateVariables, VPVerifyingInfo, ValidityPredicateCircuit, - ValidityPredicateConfig, ValidityPredicatePublicInputs, ValidityPredicateVerifyingInfo, + resource_logic_bytecode::{ResourceLogicByteCode, ResourceLogicRepresentation}, + resource_logic_circuit::{ + BasicResourceLogicVariables, ResourceLogicCircuit, ResourceLogicConfig, + ResourceLogicPublicInputs, ResourceLogicVerifyingInfo, ResourceLogicVerifyingInfoTrait, }, - vp_examples::receiver_vp::{ReceiverValidityPredicateCircuit, COMPRESSED_RECEIVER_VK}, - vp_examples::signature_verification::{ - SignatureVerificationValidityPredicateCircuit, COMPRESSED_TOKEN_AUTH_VK, + resource_logic_examples::receiver_resource_logic::{ + ReceiverResourceLogicCircuit, COMPRESSED_RECEIVER_VK, + }, + resource_logic_examples::signature_verification::{ + SignatureVerificationResourceLogicCircuit, COMPRESSED_TOKEN_AUTH_VK, }, }, constant::{ - NUM_RESOURCE, PRF_EXPAND_DYNAMIC_VP_1_CM_R, SETUP_PARAMS_MAP, - VP_CIRCUIT_FIRST_DYNAMIC_VP_CM_1, VP_CIRCUIT_FIRST_DYNAMIC_VP_CM_2, - VP_CIRCUIT_SECOND_DYNAMIC_VP_CM_1, VP_CIRCUIT_SECOND_DYNAMIC_VP_CM_2, + NUM_RESOURCE, PRF_EXPAND_DYNAMIC_RESOURCE_LOGIC_1_CM_R, + RESOURCE_LOGIC_CIRCUIT_FIRST_DYNAMIC_RESOURCE_LOGIC_CM_1, + RESOURCE_LOGIC_CIRCUIT_FIRST_DYNAMIC_RESOURCE_LOGIC_CM_2, + RESOURCE_LOGIC_CIRCUIT_SECOND_DYNAMIC_RESOURCE_LOGIC_CM_1, + RESOURCE_LOGIC_CIRCUIT_SECOND_DYNAMIC_RESOURCE_LOGIC_CM_2, SETUP_PARAMS_MAP, }, error::TransactionError, nullifier::Nullifier, proof::Proof, - resource::{RandomSeed, Resource, ResourceValidityPredicates}, + resource::{RandomSeed, Resource, ResourceLogics}, + resource_logic_commitment::ResourceLogicCommitment, + resource_logic_vk::ResourceLogicVerifyingKey, utils::{poseidon_hash_n, read_base_field, read_point}, - vp_commitment::ValidityPredicateCommitment, - vp_vk::ValidityPredicateVerifyingKey, }; use borsh::{BorshDeserialize, BorshSerialize}; use ff::Field; @@ -43,8 +47,8 @@ use pasta_curves::{group::ff::PrimeField, pallas}; use rand::{rngs::OsRng, Rng, RngCore}; lazy_static! { - pub static ref TOKEN_VK: ValidityPredicateVerifyingKey = - TokenValidityPredicateCircuit::default().get_vp_vk(); + pub static ref TOKEN_VK: ResourceLogicVerifyingKey = + TokenResourceLogicCircuit::default().get_resource_logic_vk(); pub static ref COMPRESSED_TOKEN_VK: pallas::Base = TOKEN_VK.get_compressed(); } @@ -178,32 +182,32 @@ impl TokenResource { &self.resource } - pub fn generate_input_token_vps( + pub fn generate_input_token_resource_logics( &self, mut rng: R, auth: TokenAuthorization, auth_sk: pallas::Scalar, input_resources: [Resource; NUM_RESOURCE], output_resources: [Resource; NUM_RESOURCE], - ) -> ResourceValidityPredicates { + ) -> ResourceLogics { let TokenResource { token_name, resource, } = self; - // token VP + // token resource logic let nf = resource.get_nf().unwrap().inner(); - let token_vp = TokenValidityPredicateCircuit { + let token_resource_logic = TokenResourceLogicCircuit { owned_resource_id: nf, input_resources, output_resources, token_name: token_name.clone(), auth, - receiver_vp_vk: *COMPRESSED_RECEIVER_VK, + receiver_resource_logic_vk: *COMPRESSED_RECEIVER_VK, rseed: RandomSeed::random(&mut rng), }; - // token auth VP - let token_auth_vp = SignatureVerificationValidityPredicateCircuit::from_sk_and_sign( + // token auth resource logic + let token_auth_resource_logic = SignatureVerificationResourceLogicCircuit::from_sk_and_sign( &mut rng, nf, input_resources, @@ -213,52 +217,58 @@ impl TokenResource { *COMPRESSED_RECEIVER_VK, ); - ResourceValidityPredicates::new(Box::new(token_vp), vec![Box::new(token_auth_vp)]) + ResourceLogics::new( + Box::new(token_resource_logic), + vec![Box::new(token_auth_resource_logic)], + ) } - pub fn generate_output_token_vps( + pub fn generate_output_token_resource_logics( &self, mut rng: R, auth: TokenAuthorization, input_resources: [Resource; NUM_RESOURCE], output_resources: [Resource; NUM_RESOURCE], - ) -> ResourceValidityPredicates { + ) -> ResourceLogics { let TokenResource { token_name, resource, } = self; let owned_resource_id = resource.commitment().inner(); - // token VP - let token_vp = TokenValidityPredicateCircuit { + // token resource logic + let token_resource_logic = TokenResourceLogicCircuit { owned_resource_id, input_resources, output_resources, token_name: token_name.clone(), auth, - receiver_vp_vk: *COMPRESSED_RECEIVER_VK, + receiver_resource_logic_vk: *COMPRESSED_RECEIVER_VK, rseed: RandomSeed::random(&mut rng), }; - // receiver VP - let receiver_vp = ReceiverValidityPredicateCircuit { + // receiver resource logic + let receiver_resource_logic = ReceiverResourceLogicCircuit { owned_resource_id, input_resources, output_resources, - vp_vk: *COMPRESSED_RECEIVER_VK, + resource_logic_vk: *COMPRESSED_RECEIVER_VK, encrypt_nonce: pallas::Base::from_u128(rng.gen()), sk: pallas::Base::random(&mut rng), rcv_pk: auth.pk, - auth_vp_vk: *COMPRESSED_TOKEN_AUTH_VK, + auth_resource_logic_vk: *COMPRESSED_TOKEN_AUTH_VK, }; - ResourceValidityPredicates::new(Box::new(token_vp), vec![Box::new(receiver_vp)]) + ResourceLogics::new( + Box::new(token_resource_logic), + vec![Box::new(receiver_resource_logic)], + ) } } -// TokenValidityPredicateCircuit +// TokenResourceLogicCircuit #[derive(Clone, Debug)] -pub struct TokenValidityPredicateCircuit { +pub struct TokenResourceLogicCircuit { pub owned_resource_id: pallas::Base, pub input_resources: [Resource; NUM_RESOURCE], pub output_resources: [Resource; NUM_RESOURCE], @@ -266,8 +276,8 @@ pub struct TokenValidityPredicateCircuit { pub token_name: TokenName, // The auth goes to value and defines how to consume and create the resource. pub auth: TokenAuthorization, - pub receiver_vp_vk: pallas::Base, - // rseed is to generate the randomness for vp commitment + pub receiver_resource_logic_vk: pallas::Base, + // rseed is to generate the randomness for resource_logic commitment pub rseed: RandomSeed, } @@ -286,9 +296,9 @@ impl Default for TokenAuthorization { } } -impl TokenValidityPredicateCircuit { - pub fn to_bytecode(&self) -> ValidityPredicateByteCode { - ValidityPredicateByteCode::new(ValidityPredicateRepresentation::Token, self.to_bytes()) +impl TokenResourceLogicCircuit { + pub fn to_bytecode(&self) -> ResourceLogicByteCode { + ResourceLogicByteCode::new(ResourceLogicRepresentation::Token, self.to_bytes()) } pub fn to_bytes(&self) -> Vec { @@ -300,7 +310,7 @@ impl TokenValidityPredicateCircuit { } } -impl Default for TokenValidityPredicateCircuit { +impl Default for TokenResourceLogicCircuit { fn default() -> Self { Self { owned_resource_id: pallas::Base::zero(), @@ -308,19 +318,19 @@ impl Default for TokenValidityPredicateCircuit { output_resources: [(); NUM_RESOURCE].map(|_| Resource::default()), token_name: TokenName("Token_name".to_string()), auth: TokenAuthorization::default(), - receiver_vp_vk: pallas::Base::zero(), + receiver_resource_logic_vk: pallas::Base::zero(), rseed: RandomSeed::default(), } } } -impl ValidityPredicateCircuit for TokenValidityPredicateCircuit { +impl ResourceLogicCircuit for TokenResourceLogicCircuit { // Add custom constraints fn custom_constraints( &self, config: Self::Config, mut layouter: impl Layouter, - basic_variables: BasicValidityPredicateVariables, + basic_variables: BasicResourceLogicVariables, ) -> Result<(), Error> { let owned_resource_id = basic_variables.get_owned_resource_id(); @@ -355,8 +365,8 @@ impl ValidityPredicateCircuit for TokenValidityPredicateCircuit { Value::known(self.auth.pk.to_affine()), )?; - let auth_vp_vk = assign_free_advice( - layouter.namespace(|| "witness auth vp vk"), + let auth_resource_logic_vk = assign_free_advice( + layouter.namespace(|| "witness auth resource_logic vk"), config.advices[0], Value::known(self.auth.vk), )?; @@ -369,10 +379,10 @@ impl ValidityPredicateCircuit for TokenValidityPredicateCircuit { &basic_variables.get_value_searchable_pairs(), )?; - let receiver_vp_vk = assign_free_advice( - layouter.namespace(|| "witness receiver vp vk"), + let receiver_resource_logic_vk = assign_free_advice( + layouter.namespace(|| "witness receiver resource_logic vk"), config.advices[0], - Value::known(self.receiver_vp_vk), + Value::known(self.receiver_resource_logic_vk), )?; // Decode the value, and check the value encoding @@ -382,8 +392,8 @@ impl ValidityPredicateCircuit for TokenValidityPredicateCircuit { [ pk.inner().x(), pk.inner().y(), - auth_vp_vk.clone(), - receiver_vp_vk.clone(), + auth_resource_logic_vk.clone(), + receiver_resource_logic_vk.clone(), ], )?; @@ -409,10 +419,10 @@ impl ValidityPredicateCircuit for TokenValidityPredicateCircuit { |mut region| region.constrain_equal(is_ephemeral.cell(), constant_zero.cell()), )?; - // VP Commitment - // Commt the sender(authorization method included) vp if it's an input resource; - // Commit the receiver(resource encryption constraints included) vp if it's an output resource. - let first_dynamic_vp = { + // Resource Logic Commitment + // Commt the sender(authorization method included) resource_logic if it's an input resource; + // Commit the receiver(resource encryption constraints included) resource_logic if it's an output resource. + let first_dynamic_resource_logic = { let is_input_resource = get_is_input_resource_flag( config.get_is_input_resource_flag_config, layouter.namespace(|| "get is_input_resource_flag"), @@ -425,8 +435,8 @@ impl ValidityPredicateCircuit for TokenValidityPredicateCircuit { |mut region| { config.conditional_select_config.assign_region( &is_input_resource, - &auth_vp_vk, - &receiver_vp_vk, + &auth_resource_logic_vk, + &receiver_resource_logic_vk, 0, &mut region, ) @@ -436,48 +446,55 @@ impl ValidityPredicateCircuit for TokenValidityPredicateCircuit { // Construct a blake2s chip let blake2s_chip = Blake2sChip::construct(config.blake2s_config); - let vp_cm_r = assign_free_advice( - layouter.namespace(|| "vp_cm_r"), + let resource_logic_cm_r = assign_free_advice( + layouter.namespace(|| "resource_logic_cm_r"), config.advices[0], - Value::known(self.rseed.get_vp_cm_r(PRF_EXPAND_DYNAMIC_VP_1_CM_R)), + Value::known( + self.rseed + .get_resource_logic_cm_r(PRF_EXPAND_DYNAMIC_RESOURCE_LOGIC_1_CM_R), + ), + )?; + let first_dynamic_resource_logic_cm = resource_logic_commitment_gadget( + &mut layouter, + &blake2s_chip, + first_dynamic_resource_logic, + resource_logic_cm_r, )?; - let first_dynamic_vp_cm = - vp_commitment_gadget(&mut layouter, &blake2s_chip, first_dynamic_vp, vp_cm_r)?; layouter.constrain_instance( - first_dynamic_vp_cm[0].cell(), + first_dynamic_resource_logic_cm[0].cell(), config.instances, - VP_CIRCUIT_FIRST_DYNAMIC_VP_CM_1, + RESOURCE_LOGIC_CIRCUIT_FIRST_DYNAMIC_RESOURCE_LOGIC_CM_1, )?; layouter.constrain_instance( - first_dynamic_vp_cm[1].cell(), + first_dynamic_resource_logic_cm[1].cell(), config.instances, - VP_CIRCUIT_FIRST_DYNAMIC_VP_CM_2, + RESOURCE_LOGIC_CIRCUIT_FIRST_DYNAMIC_RESOURCE_LOGIC_CM_2, )?; - // Publicize the second dynamic vp commitment with default value - let vp_cm_fields: [pallas::Base; 2] = - ValidityPredicateCommitment::default().to_public_inputs(); - let vp_cm_1 = assign_free_advice( - layouter.namespace(|| "vp_cm 1"), + // Publicize the second dynamic resource_logic commitment with default value + let resource_logic_cm_fields: [pallas::Base; 2] = + ResourceLogicCommitment::default().to_public_inputs(); + let resource_logic_cm_1 = assign_free_advice( + layouter.namespace(|| "resource_logic_cm 1"), config.advices[0], - Value::known(vp_cm_fields[0]), + Value::known(resource_logic_cm_fields[0]), )?; - let vp_cm_2 = assign_free_advice( - layouter.namespace(|| "vp_cm 2"), + let resource_logic_cm_2 = assign_free_advice( + layouter.namespace(|| "resource_logic_cm 2"), config.advices[0], - Value::known(vp_cm_fields[1]), + Value::known(resource_logic_cm_fields[1]), )?; layouter.constrain_instance( - vp_cm_1.cell(), + resource_logic_cm_1.cell(), config.instances, - VP_CIRCUIT_SECOND_DYNAMIC_VP_CM_1, + RESOURCE_LOGIC_CIRCUIT_SECOND_DYNAMIC_RESOURCE_LOGIC_CM_1, )?; layouter.constrain_instance( - vp_cm_2.cell(), + resource_logic_cm_2.cell(), config.instances, - VP_CIRCUIT_SECOND_DYNAMIC_VP_CM_2, + RESOURCE_LOGIC_CIRCUIT_SECOND_DYNAMIC_RESOURCE_LOGIC_CM_2, )?; Ok(()) @@ -491,25 +508,29 @@ impl ValidityPredicateCircuit for TokenValidityPredicateCircuit { &self.output_resources } - fn get_public_inputs(&self, mut rng: impl RngCore) -> ValidityPredicatePublicInputs { + fn get_public_inputs(&self, mut rng: impl RngCore) -> ResourceLogicPublicInputs { let mut public_inputs = self.get_mandatory_public_inputs(); - let dynamic_vp = if self.owned_resource_id == self.output_resources[0].commitment().inner() + let dynamic_resource_logic = if self.owned_resource_id + == self.output_resources[0].commitment().inner() || self.owned_resource_id == self.output_resources[1].commitment().inner() { - self.receiver_vp_vk + self.receiver_resource_logic_vk } else { self.auth.vk }; - let vp_com_r = self.rseed.get_vp_cm_r(PRF_EXPAND_DYNAMIC_VP_1_CM_R); - let vp_com: [pallas::Base; 2] = - ValidityPredicateCommitment::commit(&dynamic_vp, &vp_com_r).to_public_inputs(); - - public_inputs.extend(vp_com); - let default_vp_cm: [pallas::Base; 2] = - ValidityPredicateCommitment::default().to_public_inputs(); - public_inputs.extend(default_vp_cm); - let padding = ValidityPredicatePublicInputs::get_public_input_padding( + let resource_logic_com_r = self + .rseed + .get_resource_logic_cm_r(PRF_EXPAND_DYNAMIC_RESOURCE_LOGIC_1_CM_R); + let resource_logic_com: [pallas::Base; 2] = + ResourceLogicCommitment::commit(&dynamic_resource_logic, &resource_logic_com_r) + .to_public_inputs(); + + public_inputs.extend(resource_logic_com); + let default_resource_logic_cm: [pallas::Base; 2] = + ResourceLogicCommitment::default().to_public_inputs(); + public_inputs.extend(default_resource_logic_cm); + let padding = ResourceLogicPublicInputs::get_public_input_padding( public_inputs.len(), &RandomSeed::random(&mut rng), ); @@ -522,10 +543,10 @@ impl ValidityPredicateCircuit for TokenValidityPredicateCircuit { } } -vp_circuit_impl!(TokenValidityPredicateCircuit); -vp_verifying_info_impl!(TokenValidityPredicateCircuit); +resource_logic_circuit_impl!(TokenResourceLogicCircuit); +resource_logic_verifying_info_impl!(TokenResourceLogicCircuit); -impl BorshSerialize for TokenValidityPredicateCircuit { +impl BorshSerialize for TokenResourceLogicCircuit { fn serialize(&self, writer: &mut W) -> std::io::Result<()> { writer.write_all(&self.owned_resource_id.to_repr())?; for input in self.input_resources.iter() { @@ -538,14 +559,14 @@ impl BorshSerialize for TokenValidityPredicateCircuit { self.token_name.serialize(writer)?; self.auth.serialize(writer)?; - writer.write_all(&self.receiver_vp_vk.to_repr())?; + writer.write_all(&self.receiver_resource_logic_vk.to_repr())?; self.rseed.serialize(writer)?; Ok(()) } } -impl BorshDeserialize for TokenValidityPredicateCircuit { +impl BorshDeserialize for TokenResourceLogicCircuit { fn deserialize_reader(reader: &mut R) -> std::io::Result { let owned_resource_id = read_base_field(reader)?; let input_resources: Vec<_> = (0..NUM_RESOURCE) @@ -556,7 +577,7 @@ impl BorshDeserialize for TokenValidityPredicateCircuit { .collect::>()?; let token_name = TokenName::deserialize_reader(reader)?; let auth = TokenAuthorization::deserialize_reader(reader)?; - let receiver_vp_vk = read_base_field(reader)?; + let receiver_resource_logic_vk = read_base_field(reader)?; let rseed = RandomSeed::deserialize_reader(reader)?; Ok(Self { owned_resource_id, @@ -564,7 +585,7 @@ impl BorshDeserialize for TokenValidityPredicateCircuit { output_resources: output_resources.try_into().unwrap(), token_name, auth, - receiver_vp_vk, + receiver_resource_logic_vk, rseed, }) } @@ -617,8 +638,8 @@ impl TokenAuthorization { } #[test] -fn test_halo2_token_vp_circuit() { - use crate::constant::VP_CIRCUIT_PARAMS_SIZE; +fn test_halo2_token_resource_logic_circuit() { + use crate::constant::RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE; use crate::resource::tests::random_resource; use halo2_proofs::dev::MockProver; use rand::rngs::OsRng; @@ -631,13 +652,13 @@ fn test_halo2_token_vp_circuit() { let auth = TokenAuthorization::random(&mut rng); input_resources[0].kind.label = token_name.encode(); input_resources[0].value = auth.to_value(); - TokenValidityPredicateCircuit { + TokenResourceLogicCircuit { owned_resource_id: input_resources[0].get_nf().unwrap().inner(), input_resources, output_resources, token_name, auth, - receiver_vp_vk: *COMPRESSED_RECEIVER_VK, + receiver_resource_logic_vk: *COMPRESSED_RECEIVER_VK, rseed: RandomSeed::random(&mut rng), } }; @@ -645,13 +666,13 @@ fn test_halo2_token_vp_circuit() { // Test serialization let circuit = { let circuit_bytes = circuit.to_bytes(); - TokenValidityPredicateCircuit::from_bytes(&circuit_bytes) + TokenResourceLogicCircuit::from_bytes(&circuit_bytes) }; let public_inputs = circuit.get_public_inputs(&mut rng); let prover = MockProver::::run( - VP_CIRCUIT_PARAMS_SIZE, + RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE, &circuit, vec![public_inputs.to_vec()], ) diff --git a/taiga_halo2/src/circuit/vp_bytecode.rs b/taiga_halo2/src/circuit/vp_bytecode.rs deleted file mode 100644 index 10b9653f..00000000 --- a/taiga_halo2/src/circuit/vp_bytecode.rs +++ /dev/null @@ -1,258 +0,0 @@ -#[cfg(feature = "borsh")] -use crate::circuit::vp_examples::TrivialValidityPredicateCircuit; -#[cfg(feature = "examples")] -use crate::circuit::vp_examples::{ - cascade_intent::CascadeIntentValidityPredicateCircuit, - or_relation_intent::OrRelationIntentValidityPredicateCircuit, - partial_fulfillment_intent::PartialFulfillmentIntentValidityPredicateCircuit, - receiver_vp::ReceiverValidityPredicateCircuit, - signature_verification::SignatureVerificationValidityPredicateCircuit, - token::TokenValidityPredicateCircuit, -}; -use crate::error::TransactionError; -use crate::shielded_ptx::ResourceVPVerifyingInfoSet; -use crate::{ - circuit::vp_circuit::{ - VPVerifyingInfo, ValidityPredicateVerifyingInfo, VampIRValidityPredicateCircuit, - }, - constant::{ - VP_CIRCUIT_NULLIFIER_ONE_PUBLIC_INPUT_IDX, VP_CIRCUIT_NULLIFIER_TWO_PUBLIC_INPUT_IDX, - VP_CIRCUIT_OUTPUT_CM_ONE_PUBLIC_INPUT_IDX, VP_CIRCUIT_OUTPUT_CM_TWO_PUBLIC_INPUT_IDX, - VP_CIRCUIT_OWNED_RESOURCE_ID_PUBLIC_INPUT_IDX, - }, - nullifier::Nullifier, - resource::ResourceCommitment, -}; - -#[cfg(feature = "borsh")] -use borsh::{BorshDeserialize, BorshSerialize}; -use pasta_curves::pallas; -#[cfg(feature = "serde")] -use serde; -use std::path::PathBuf; - -#[derive(Clone, Debug)] -#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub enum ValidityPredicateRepresentation { - // vampir has a unified circuit representation. - VampIR(Vec), - // Native halo2 circuits don't have a unified representatioin, enumerate the vp circuit examples for the moment. - // TODO: figure out if we can have a unified circuit presentation. In theory, it's possible to separate the circuit system and proving system. - Trivial, - Token, - SignatureVerification, - Receiver, - PartialFulfillmentIntent, - OrRelationIntent, - CascadeIntent, - // Add other native vp types here if needed -} - -#[derive(Clone, Debug)] -#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub struct ValidityPredicateByteCode { - circuit: ValidityPredicateRepresentation, - inputs: Vec, -} - -#[derive(Clone, Debug)] -#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub struct ApplicationByteCode { - app_vp_bytecode: ValidityPredicateByteCode, - dynamic_vp_bytecode: Vec, -} - -impl ValidityPredicateByteCode { - pub fn new(circuit: ValidityPredicateRepresentation, inputs: Vec) -> Self { - Self { circuit, inputs } - } - - pub fn generate_proof(self) -> Result { - match self.circuit { - ValidityPredicateRepresentation::VampIR(circuit) => { - // TDDO: use the file_name api atm, - // request vamp_ir to provide a api to generate circuit from bytes. - let vamp_ir_circuit_file = - PathBuf::from(String::from_utf8_lossy(&circuit).to_string()); - let inputs_file = PathBuf::from(String::from_utf8_lossy(&self.inputs).to_string()); - let vp_circuit = VampIRValidityPredicateCircuit::from_vamp_ir_file( - &vamp_ir_circuit_file, - &inputs_file, - ); - Ok(vp_circuit.get_verifying_info()) - } - #[cfg(feature = "borsh")] - ValidityPredicateRepresentation::Trivial => { - let vp = TrivialValidityPredicateCircuit::from_bytes(&self.inputs); - Ok(vp.get_verifying_info()) - } - #[cfg(feature = "examples")] - ValidityPredicateRepresentation::Token => { - let vp = TokenValidityPredicateCircuit::from_bytes(&self.inputs); - Ok(vp.get_verifying_info()) - } - #[cfg(feature = "examples")] - ValidityPredicateRepresentation::SignatureVerification => { - let vp = SignatureVerificationValidityPredicateCircuit::from_bytes(&self.inputs); - Ok(vp.get_verifying_info()) - } - #[cfg(feature = "examples")] - ValidityPredicateRepresentation::Receiver => { - let vp = ReceiverValidityPredicateCircuit::from_bytes(&self.inputs); - Ok(vp.get_verifying_info()) - } - #[cfg(feature = "examples")] - ValidityPredicateRepresentation::PartialFulfillmentIntent => { - let vp = PartialFulfillmentIntentValidityPredicateCircuit::from_bytes(&self.inputs); - Ok(vp.get_verifying_info()) - } - #[cfg(feature = "examples")] - ValidityPredicateRepresentation::OrRelationIntent => { - let vp = OrRelationIntentValidityPredicateCircuit::from_bytes(&self.inputs); - Ok(vp.get_verifying_info()) - } - #[cfg(feature = "examples")] - ValidityPredicateRepresentation::CascadeIntent => { - let vp = CascadeIntentValidityPredicateCircuit::from_bytes(&self.inputs); - Ok(vp.get_verifying_info()) - } - #[allow(unreachable_patterns)] - _ => Err(TransactionError::InvalidValidityPredicateRepresentation), - } - } - - // Verify vp circuit transparently and return owned resource PubID for further checking - pub fn verify_transparently( - &self, - compliance_nfs: &[Nullifier], - compliance_cms: &[ResourceCommitment], - ) -> Result { - // check VP transparently - let public_inputs = match &self.circuit { - ValidityPredicateRepresentation::VampIR(circuit) => { - // TDDO: use the file_name api atm, - // request vamp_ir to provide a api to generate circuit from bytes. - let vamp_ir_circuit_file = - PathBuf::from(String::from_utf8_lossy(circuit).to_string()); - let inputs_file = PathBuf::from(String::from_utf8_lossy(&self.inputs).to_string()); - let vp_circuit = VampIRValidityPredicateCircuit::from_vamp_ir_file( - &vamp_ir_circuit_file, - &inputs_file, - ); - vp_circuit.verify_transparently()? - } - #[cfg(feature = "borsh")] - ValidityPredicateRepresentation::Trivial => { - let vp = TrivialValidityPredicateCircuit::from_bytes(&self.inputs); - vp.verify_transparently()? - } - #[cfg(feature = "examples")] - ValidityPredicateRepresentation::Token => { - let vp = TokenValidityPredicateCircuit::from_bytes(&self.inputs); - vp.verify_transparently()? - } - #[cfg(feature = "examples")] - ValidityPredicateRepresentation::SignatureVerification => { - let vp = SignatureVerificationValidityPredicateCircuit::from_bytes(&self.inputs); - vp.verify_transparently()? - } - #[cfg(feature = "examples")] - ValidityPredicateRepresentation::Receiver => { - let vp = ReceiverValidityPredicateCircuit::from_bytes(&self.inputs); - vp.verify_transparently()? - } - #[cfg(feature = "examples")] - ValidityPredicateRepresentation::PartialFulfillmentIntent => { - let vp = PartialFulfillmentIntentValidityPredicateCircuit::from_bytes(&self.inputs); - vp.verify_transparently()? - } - #[cfg(feature = "examples")] - ValidityPredicateRepresentation::OrRelationIntent => { - let vp = OrRelationIntentValidityPredicateCircuit::from_bytes(&self.inputs); - vp.verify_transparently()? - } - #[cfg(feature = "examples")] - ValidityPredicateRepresentation::CascadeIntent => { - let vp = CascadeIntentValidityPredicateCircuit::from_bytes(&self.inputs); - vp.verify_transparently()? - } - #[allow(unreachable_patterns)] - _ => return Err(TransactionError::InvalidValidityPredicateRepresentation), - }; - - // check nullifiers - // Check the vp actually uses the input resources from compliance circuits. - let vp_nfs = [ - public_inputs.get_from_index(VP_CIRCUIT_NULLIFIER_ONE_PUBLIC_INPUT_IDX), - public_inputs.get_from_index(VP_CIRCUIT_NULLIFIER_TWO_PUBLIC_INPUT_IDX), - ]; - - if !((compliance_nfs[0].inner() == vp_nfs[0] && compliance_nfs[1].inner() == vp_nfs[1]) - || (compliance_nfs[0].inner() == vp_nfs[1] && compliance_nfs[1].inner() == vp_nfs[0])) - { - return Err(TransactionError::InconsistentNullifier); - } - - // check resource_commitments - // Check the vp actually uses the output resources from compliance circuits. - let vp_cms = [ - public_inputs.get_from_index(VP_CIRCUIT_OUTPUT_CM_ONE_PUBLIC_INPUT_IDX), - public_inputs.get_from_index(VP_CIRCUIT_OUTPUT_CM_TWO_PUBLIC_INPUT_IDX), - ]; - if !((compliance_cms[0].inner() == vp_cms[0] && compliance_cms[1].inner() == vp_cms[1]) - || (compliance_cms[0].inner() == vp_cms[1] && compliance_cms[1].inner() == vp_cms[0])) - { - return Err(TransactionError::InconsistentOutputResourceCommitment); - } - - Ok(public_inputs.get_from_index(VP_CIRCUIT_OWNED_RESOURCE_ID_PUBLIC_INPUT_IDX)) - } -} - -impl ApplicationByteCode { - pub fn new( - app_vp_bytecode: ValidityPredicateByteCode, - dynamic_vp_bytecode: Vec, - ) -> Self { - Self { - app_vp_bytecode, - dynamic_vp_bytecode, - } - } - - pub fn generate_proofs(self) -> Result { - let app_vp_verifying_info = self.app_vp_bytecode.generate_proof()?; - - let app_dynamic_vp_verifying_info: Result, _> = self - .dynamic_vp_bytecode - .into_iter() - .map(|bytecode| bytecode.generate_proof()) - .collect(); - Ok(ResourceVPVerifyingInfoSet::new( - app_vp_verifying_info, - app_dynamic_vp_verifying_info?, - )) - } - - // Verify vp circuits transparently and return owned resource PubID for further checking - pub fn verify_transparently( - &self, - compliance_nfs: &[Nullifier], - compliance_cms: &[ResourceCommitment], - ) -> Result { - let owned_resource_id = self - .app_vp_bytecode - .verify_transparently(compliance_nfs, compliance_cms)?; - for dynamic_vp in self.dynamic_vp_bytecode.iter() { - let id = dynamic_vp.verify_transparently(compliance_nfs, compliance_cms)?; - // check: the app_vp and dynamic_vps belong to the resource - if id != owned_resource_id { - return Err(TransactionError::InconsistentOwnedResourceID); - } - } - Ok(owned_resource_id) - } -} diff --git a/taiga_halo2/src/compliance.rs b/taiga_halo2/src/compliance.rs index 4221a44e..b3fb4753 100644 --- a/taiga_halo2/src/compliance.rs +++ b/taiga_halo2/src/compliance.rs @@ -5,12 +5,12 @@ /// other required proofs use crate::{ circuit::compliance_circuit::ComplianceCircuit, - constant::{PRF_EXPAND_INPUT_VP_CM_R, PRF_EXPAND_OUTPUT_VP_CM_R}, + constant::{PRF_EXPAND_INPUT_RESOURCE_LOGIC_CM_R, PRF_EXPAND_OUTPUT_RESOURCE_LOGIC_CM_R}, delta_commitment::DeltaCommitment, merkle_tree::{Anchor, MerklePath}, nullifier::Nullifier, resource::{RandomSeed, Resource, ResourceCommitment}, - vp_commitment::ValidityPredicateCommitment, + resource_logic_commitment::ResourceLogicCommitment, }; use pasta_curves::pallas; use rand::RngCore; @@ -38,10 +38,10 @@ pub struct CompliancePublicInputs { pub cm: ResourceCommitment, /// Resource delta is used to reason about total quantities of different kinds of resources. pub delta: DeltaCommitment, - /// The commitment to input resource application(static) vp - pub input_vp_commitment: ValidityPredicateCommitment, - /// The commitment to output resource application(static) vp - pub output_vp_commitment: ValidityPredicateCommitment, + /// The commitment to input resource logic + pub input_resource_logic_commitment: ResourceLogicCommitment, + /// The commitment to output resource logic + pub output_resource_logic_commitment: ResourceLogicCommitment, } /// The information to build CompliancePublicInputs and ComplianceCircuit. @@ -53,24 +53,27 @@ pub struct ComplianceInfo { input_merkle_path: MerklePath, input_anchor: Anchor, output_resource: Resource, - // rseed is to generate the randomness of the delta commitment and vp commitments + // rseed is to generate the randomness of the delta commitment and resource + // logic commitments rseed: RandomSeed, } impl CompliancePublicInputs { pub fn to_instance(&self) -> Vec { - let input_vp_commitment = self.input_vp_commitment.to_public_inputs(); - let output_vp_commitment = self.output_vp_commitment.to_public_inputs(); + let input_resource_logic_commitment = + self.input_resource_logic_commitment.to_public_inputs(); + let output_resource_logic_commitment = + self.output_resource_logic_commitment.to_public_inputs(); vec![ self.nf.inner(), self.anchor.inner(), self.cm.inner(), self.delta.get_x(), self.delta.get_y(), - input_vp_commitment[0], - input_vp_commitment[1], - output_vp_commitment[0], - output_vp_commitment[1], + input_resource_logic_commitment[0], + input_resource_logic_commitment[1], + output_resource_logic_commitment[0], + output_resource_logic_commitment[1], ] } } @@ -82,8 +85,8 @@ impl BorshSerialize for CompliancePublicInputs { writer.write_all(&self.nf.to_bytes())?; writer.write_all(&self.cm.to_bytes())?; writer.write_all(&self.delta.to_bytes())?; - writer.write_all(&self.input_vp_commitment.to_bytes())?; - writer.write_all(&self.output_vp_commitment.to_bytes())?; + writer.write_all(&self.input_resource_logic_commitment.to_bytes())?; + writer.write_all(&self.output_resource_logic_commitment.to_bytes())?; Ok(()) } } @@ -104,20 +107,20 @@ impl BorshDeserialize for CompliancePublicInputs { let detla_bytes = <[u8; 32]>::deserialize_reader(reader)?; let delta = Option::from(DeltaCommitment::from_bytes(detla_bytes)) .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "delta not in field"))?; - let input_vp_commitment_bytes = <[u8; 32]>::deserialize_reader(reader)?; - let input_vp_commitment = - ValidityPredicateCommitment::from_bytes(input_vp_commitment_bytes); - let output_vp_commitment_bytes = <[u8; 32]>::deserialize_reader(reader)?; - let output_vp_commitment = - ValidityPredicateCommitment::from_bytes(output_vp_commitment_bytes); + let input_resource_logic_commitment_bytes = <[u8; 32]>::deserialize_reader(reader)?; + let input_resource_logic_commitment = + ResourceLogicCommitment::from_bytes(input_resource_logic_commitment_bytes); + let output_resource_logic_commitment_bytes = <[u8; 32]>::deserialize_reader(reader)?; + let output_resource_logic_commitment = + ResourceLogicCommitment::from_bytes(output_resource_logic_commitment_bytes); Ok(CompliancePublicInputs { anchor, nf, cm, delta, - input_vp_commitment, - output_vp_commitment, + input_resource_logic_commitment, + output_resource_logic_commitment, }) } } @@ -154,14 +157,16 @@ impl ComplianceInfo { self.rseed.get_rcv() } - // Get the randomness of input resource application vp commitment - pub fn get_input_vp_com_r(&self) -> pallas::Base { - self.rseed.get_vp_cm_r(PRF_EXPAND_INPUT_VP_CM_R) + // Get the randomness of input resource application resource logic commitment + pub fn get_input_resource_logic_com_r(&self) -> pallas::Base { + self.rseed + .get_resource_logic_cm_r(PRF_EXPAND_INPUT_RESOURCE_LOGIC_CM_R) } - // Get the randomness of output resource application vp commitment - pub fn get_output_vp_com_r(&self) -> pallas::Base { - self.rseed.get_vp_cm_r(PRF_EXPAND_OUTPUT_VP_CM_R) + // Get the randomness of output resource application resource logic commitment + pub fn get_output_resource_logic_com_r(&self) -> pallas::Base { + self.rseed + .get_resource_logic_cm_r(PRF_EXPAND_OUTPUT_RESOURCE_LOGIC_CM_R) } // Only used in transparent scenario: the anchor is untrusted, recalculate root when executing it transparently. @@ -194,21 +199,25 @@ impl ComplianceInfo { let rcv = self.get_rcv(); let delta = self.get_delta_commitment(&rcv); - let input_vp_cm_r = self.get_input_vp_com_r(); - let input_vp_commitment = - ValidityPredicateCommitment::commit(&self.input_resource.get_logic(), &input_vp_cm_r); + let input_resource_logic_cm_r = self.get_input_resource_logic_com_r(); + let input_resource_logic_commitment = ResourceLogicCommitment::commit( + &self.input_resource.get_logic(), + &input_resource_logic_cm_r, + ); - let output_vp_cm_r = self.get_output_vp_com_r(); - let output_vp_commitment = - ValidityPredicateCommitment::commit(&self.output_resource.get_logic(), &output_vp_cm_r); + let output_resource_logic_cm_r = self.get_output_resource_logic_com_r(); + let output_resource_logic_commitment = ResourceLogicCommitment::commit( + &self.output_resource.get_logic(), + &output_resource_logic_cm_r, + ); let compliance = CompliancePublicInputs { nf, cm, anchor: self.input_anchor, delta, - input_vp_commitment, - output_vp_commitment, + input_resource_logic_commitment, + output_resource_logic_commitment, }; let compliance_circuit = ComplianceCircuit { @@ -216,8 +225,8 @@ impl ComplianceInfo { merkle_path: self.input_merkle_path.get_path().try_into().unwrap(), output_resource: self.output_resource, rcv, - input_vp_cm_r, - output_vp_cm_r, + input_resource_logic_cm_r, + output_resource_logic_cm_r, }; (compliance, compliance_circuit) diff --git a/taiga_halo2/src/constant.rs b/taiga_halo2/src/constant.rs index 23164f1f..1f626846 100644 --- a/taiga_halo2/src/constant.rs +++ b/taiga_halo2/src/constant.rs @@ -24,7 +24,7 @@ pub const RESOURCE_COMMITMENT_PERSONALIZATION: &str = "Taiga-NoteCommit"; pub const TRANSACTION_BINDING_HASH_PERSONALIZATION: &[u8; 16] = b"TxBindingSigHash"; -pub const VP_COMMITMENT_PERSONALIZATION: &[u8; 8] = b"VPCommit"; +pub const RESOURCE_LOGIC_COMMITMENT_PERSONALIZATION: &[u8; 8] = b"VPCommit"; pub const PRF_EXPAND_PERSONALIZATION: &[u8; 16] = b"Taiga_ExpandSeed"; lazy_static! { @@ -36,10 +36,10 @@ pub const PRF_EXPAND_PSI: u8 = 0; pub const PRF_EXPAND_RCM: u8 = 1; pub const PRF_EXPAND_PUBLIC_INPUT_PADDING: u8 = 2; pub const PRF_EXPAND_VCM_R: u8 = 3; -pub const PRF_EXPAND_INPUT_VP_CM_R: u8 = 4; -pub const PRF_EXPAND_OUTPUT_VP_CM_R: u8 = 5; -pub const PRF_EXPAND_DYNAMIC_VP_1_CM_R: u8 = 6; -pub const PRF_EXPAND_DYNAMIC_VP_2_CM_R: u8 = 7; +pub const PRF_EXPAND_INPUT_RESOURCE_LOGIC_CM_R: u8 = 4; +pub const PRF_EXPAND_OUTPUT_RESOURCE_LOGIC_CM_R: u8 = 5; +pub const PRF_EXPAND_DYNAMIC_RESOURCE_LOGIC_1_CM_R: u8 = 6; +pub const PRF_EXPAND_DYNAMIC_RESOURCE_LOGIC_2_CM_R: u8 = 7; /// Commitment merkle tree depth pub const TAIGA_COMMITMENT_TREE_DEPTH: usize = 32; @@ -54,39 +54,42 @@ pub const COMPLIANCE_ANCHOR_PUBLIC_INPUT_ROW_IDX: usize = 1; pub const COMPLIANCE_OUTPUT_CM_PUBLIC_INPUT_ROW_IDX: usize = 2; pub const COMPLIANCE_DELTA_CM_X_PUBLIC_INPUT_ROW_IDX: usize = 3; pub const COMPLIANCE_DELTA_CM_Y_PUBLIC_INPUT_ROW_IDX: usize = 4; -pub const COMPLIANCE_INPUT_VP_CM_1_ROW_IDX: usize = 5; -pub const COMPLIANCE_INPUT_VP_CM_2_ROW_IDX: usize = 6; -pub const COMPLIANCE_OUTPUT_VP_CM_1_ROW_IDX: usize = 7; -pub const COMPLIANCE_OUTPUT_VP_CM_2_ROW_IDX: usize = 8; +pub const COMPLIANCE_INPUT_RESOURCE_LOGIC_CM_1_ROW_IDX: usize = 5; +pub const COMPLIANCE_INPUT_RESOURCE_LOGIC_CM_2_ROW_IDX: usize = 6; +pub const COMPLIANCE_OUTPUT_RESOURCE_LOGIC_CM_1_ROW_IDX: usize = 7; +pub const COMPLIANCE_OUTPUT_RESOURCE_LOGIC_CM_2_ROW_IDX: usize = 8; pub const POSEIDON_TO_CURVE_INPUT_LEN: usize = 3; pub const CURVE_ID: &str = "pallas"; pub const VALUE_BASE_DOMAIN_POSTFIX: &str = "Taiga-NoteType"; -pub const VP_CIRCUIT_PUBLIC_INPUT_NUM: usize = VP_CIRCUIT_MANDATORY_PUBLIC_INPUT_NUM - + VP_CIRCUIT_CUSTOM_PUBLIC_INPUT_NUM - + VP_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_NUM; -pub const VP_CIRCUIT_MANDATORY_PUBLIC_INPUT_NUM: usize = 9; -pub const VP_CIRCUIT_CUSTOM_PUBLIC_INPUT_NUM: usize = 2; -pub const VP_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_NUM: usize = +pub const RESOURCE_LOGIC_CIRCUIT_PUBLIC_INPUT_NUM: usize = + RESOURCE_LOGIC_CIRCUIT_MANDATORY_PUBLIC_INPUT_NUM + + RESOURCE_LOGIC_CIRCUIT_CUSTOM_PUBLIC_INPUT_NUM + + RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_NUM; +pub const RESOURCE_LOGIC_CIRCUIT_MANDATORY_PUBLIC_INPUT_NUM: usize = 9; +pub const RESOURCE_LOGIC_CIRCUIT_CUSTOM_PUBLIC_INPUT_NUM: usize = 2; +pub const RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_NUM: usize = RESOURCE_ENCRYPTION_CIPHERTEXT_NUM + 2; // ciphertext(12) + public_key(2) -pub const VP_CIRCUIT_NULLIFIER_ONE_PUBLIC_INPUT_IDX: usize = 0; -pub const VP_CIRCUIT_OUTPUT_CM_ONE_PUBLIC_INPUT_IDX: usize = 1; -pub const VP_CIRCUIT_NULLIFIER_TWO_PUBLIC_INPUT_IDX: usize = 2; -pub const VP_CIRCUIT_OUTPUT_CM_TWO_PUBLIC_INPUT_IDX: usize = 3; -pub const VP_CIRCUIT_OWNED_RESOURCE_ID_PUBLIC_INPUT_IDX: usize = 4; -pub const VP_CIRCUIT_FIRST_DYNAMIC_VP_CM_1: usize = 5; -pub const VP_CIRCUIT_FIRST_DYNAMIC_VP_CM_2: usize = 6; -pub const VP_CIRCUIT_SECOND_DYNAMIC_VP_CM_1: usize = 7; -pub const VP_CIRCUIT_SECOND_DYNAMIC_VP_CM_2: usize = 8; -pub const VP_CIRCUIT_CUSTOM_PUBLIC_INPUT_BEGIN_IDX: usize = VP_CIRCUIT_MANDATORY_PUBLIC_INPUT_NUM; -pub const VP_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX: usize = - VP_CIRCUIT_MANDATORY_PUBLIC_INPUT_NUM + VP_CIRCUIT_CUSTOM_PUBLIC_INPUT_NUM; -pub const VP_CIRCUIT_RESOURCE_ENCRYPTION_NONCE_IDX: usize = 21; -pub const VP_CIRCUIT_RESOURCE_ENCRYPTION_MAC_IDX: usize = 22; -pub const VP_CIRCUIT_RESOURCE_ENCRYPTION_PK_X_IDX: usize = 23; -pub const VP_CIRCUIT_RESOURCE_ENCRYPTION_PK_Y_IDX: usize = 24; +pub const RESOURCE_LOGIC_CIRCUIT_NULLIFIER_ONE_PUBLIC_INPUT_IDX: usize = 0; +pub const RESOURCE_LOGIC_CIRCUIT_OUTPUT_CM_ONE_PUBLIC_INPUT_IDX: usize = 1; +pub const RESOURCE_LOGIC_CIRCUIT_NULLIFIER_TWO_PUBLIC_INPUT_IDX: usize = 2; +pub const RESOURCE_LOGIC_CIRCUIT_OUTPUT_CM_TWO_PUBLIC_INPUT_IDX: usize = 3; +pub const RESOURCE_LOGIC_CIRCUIT_OWNED_RESOURCE_ID_PUBLIC_INPUT_IDX: usize = 4; +pub const RESOURCE_LOGIC_CIRCUIT_FIRST_DYNAMIC_RESOURCE_LOGIC_CM_1: usize = 5; +pub const RESOURCE_LOGIC_CIRCUIT_FIRST_DYNAMIC_RESOURCE_LOGIC_CM_2: usize = 6; +pub const RESOURCE_LOGIC_CIRCUIT_SECOND_DYNAMIC_RESOURCE_LOGIC_CM_1: usize = 7; +pub const RESOURCE_LOGIC_CIRCUIT_SECOND_DYNAMIC_RESOURCE_LOGIC_CM_2: usize = 8; +pub const RESOURCE_LOGIC_CIRCUIT_CUSTOM_PUBLIC_INPUT_BEGIN_IDX: usize = + RESOURCE_LOGIC_CIRCUIT_MANDATORY_PUBLIC_INPUT_NUM; +pub const RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PUBLIC_INPUT_BEGIN_IDX: usize = + RESOURCE_LOGIC_CIRCUIT_MANDATORY_PUBLIC_INPUT_NUM + + RESOURCE_LOGIC_CIRCUIT_CUSTOM_PUBLIC_INPUT_NUM; +pub const RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_NONCE_IDX: usize = 21; +pub const RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_MAC_IDX: usize = 22; +pub const RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PK_X_IDX: usize = 23; +pub const RESOURCE_LOGIC_CIRCUIT_RESOURCE_ENCRYPTION_PK_Y_IDX: usize = 24; // Resource encryption pub const RESOURCE_ENCRYPTION_PLAINTEXT_NUM: usize = 10; @@ -119,7 +122,7 @@ lazy_static! { pub const PARAMS_SIZE: u32 = 15; pub const COMPLIANCE_CIRCUIT_PARAMS_SIZE: u32 = PARAMS_SIZE; -pub const VP_CIRCUIT_PARAMS_SIZE: u32 = PARAMS_SIZE; +pub const RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE: u32 = PARAMS_SIZE; // Setup params map lazy_static! { @@ -155,7 +158,7 @@ lazy_static! { CommitDomain::new(RESOURCE_COMMITMENT_PERSONALIZATION); pub static ref RESOURCE_COMMITMENT_GENERATOR: pallas::Affine = RESOURCE_COMMIT_DOMAIN.Q().to_affine(); pub static ref RESOURCE_COMMITMENT_R_GENERATOR: pallas::Affine = RESOURCE_COMMIT_DOMAIN.R().to_affine(); - // Generator used in NullifierK and VP + // The Generator is used in NullifierK and resource logic pub static ref GENERATOR: pallas::Affine = pallas::Point::generator().to_affine(); // pub static ref R_ZS_AND_US: Vec<(u64, [pallas::Base; H])> = // find_zs_and_us(*RESOURCE_COMMITMENT_R_GENERATOR, NUM_WINDOWS).unwrap(); @@ -6097,7 +6100,7 @@ impl FixedPoint for Short { } } -pub const MAX_DYNAMIC_VP_NUM: usize = 2; +pub const MAX_DYNAMIC_RESOURCE_LOGIC_NUM: usize = 2; #[ignore] #[test] @@ -6158,17 +6161,17 @@ fn export_params() { // #[ignore] // #[test] -// fn export_trivial_vp_proving_key() { -// use crate::circuit::vp_examples::TrivialValidityPredicateCircuit; +// fn export_trivial_resource_logic_proving_key() { +// use crate::circuit::resource_logic_examples::TrivialResourceLogicCircuit; // use std::io::Write; -// let params = SETUP_PARAMS_MAP.get(&VP_CIRCUIT_PARAMS_SIZE).unwrap(); -// let empty_circuit = TrivialValidityPredicateCircuit::default(); +// let params = SETUP_PARAMS_MAP.get(&RESOURCE_LOGIC_CIRCUIT_PARAMS_SIZE).unwrap(); +// let empty_circuit = TrivialResourceLogicCircuit::default(); // let vk = keygen_vk(params, &empty_circuit).expect("keygen_vk should not fail"); // let pk = keygen_pk(params, vk, &empty_circuit).expect("keygen_pk should not fail"); // let mut bytes = vec![]; // pk.write(&mut bytes).unwrap(); -// let mut file = std::fs::File::create("./params/trivial_vp_proving_key") -// .unwrap_or_else(|err| panic!("cannot create trivial_vp_proving_key with {}", err)); +// let mut file = std::fs::File::create("./params/trivial_resource_logic_proving_key") +// .unwrap_or_else(|err| panic!("cannot create trivial_resource_logic_proving_key with {}", err)); // file.write_all(&bytes).unwrap(); // } diff --git a/taiga_halo2/src/error.rs b/taiga_halo2/src/error.rs index c583dc11..9a7a2efc 100644 --- a/taiga_halo2/src/error.rs +++ b/taiga_halo2/src/error.rs @@ -10,11 +10,11 @@ pub enum TransactionError { InvalidBindingSignature, /// Binding signature is missing. MissingBindingSignatures, - /// Nullifier is not consistent between the compliance and the vp. + /// Nullifier is inconsistent between the compliance and the resource logic. InconsistentNullifier, - /// Output resource commitment is not consistent between the compliance and the vp. + /// Output resource commitment is inconsistent between the compliance and the resource logic. InconsistentOutputResourceCommitment, - /// Owned resource id is not consistent between the compliance and the vp. + /// Owned resource id is inconsistent between the compliance and the resource logic. InconsistentOwnedResourceID, /// IO error IoError(std::io::Error), @@ -24,8 +24,8 @@ pub enum TransactionError { MissingTransparentResourceMerklePath, /// Shielded partial Tx binding signature r is missing MissingPartialTxBindingSignatureR, - /// ValidityPredicateRepresentation is not valid - InvalidValidityPredicateRepresentation, + /// ResourceLogicRepresentation is not valid + InvalidResourceLogicRepresentation, } impl Display for TransactionError { @@ -36,13 +36,13 @@ impl Display for TransactionError { InvalidBindingSignature => f.write_str("Binding signature was invalid"), MissingBindingSignatures => f.write_str("Binding signature is missing"), InconsistentNullifier => { - f.write_str("Nullifier is not consistent between the compliance and the vp") + f.write_str("Nullifier is not consistent between the compliance and the resource logic") } InconsistentOutputResourceCommitment => f.write_str( - "Output resource commitment is not consistent between the compliance and the vp", + "Output resource commitment is not consistent between the compliance and the resource logic", ), InconsistentOwnedResourceID => { - f.write_str("Owned resource id is not consistent between the compliance and the vp") + f.write_str("Owned resource id is not consistent between the compliance and the resource logic") } IoError(e) => f.write_str(&format!("IoError error: {e}")), MissingTransparentResourceNullifierKey => { @@ -54,8 +54,8 @@ impl Display for TransactionError { MissingPartialTxBindingSignatureR => { f.write_str("Shielded partial Tx binding signature r is missing") } - InvalidValidityPredicateRepresentation => { - f.write_str("ValidityPredicateRepresentation is not valid, add borsh feature if using native vp examples ") + InvalidResourceLogicRepresentation => { + f.write_str("ResourceLogicRepresentation is not valid, add borsh feature if using native resource logic examples ") } } } diff --git a/taiga_halo2/src/lib.rs b/taiga_halo2/src/lib.rs index 974533c2..e03fd487 100644 --- a/taiga_halo2/src/lib.rs +++ b/taiga_halo2/src/lib.rs @@ -13,10 +13,10 @@ pub mod nullifier; pub mod proof; pub mod resource; pub mod resource_encryption; +pub mod resource_logic_commitment; +pub mod resource_logic_vk; pub mod shielded_ptx; pub mod taiga_api; pub mod transaction; pub mod transparent_ptx; pub mod utils; -pub mod vp_commitment; -pub mod vp_vk; diff --git a/taiga_halo2/src/resource.rs b/taiga_halo2/src/resource.rs index f8c28cdf..200f5d66 100644 --- a/taiga_halo2/src/resource.rs +++ b/taiga_halo2/src/resource.rs @@ -1,7 +1,9 @@ use crate::{ circuit::{ - vp_circuit::ValidityPredicate, - vp_examples::{TrivialValidityPredicateCircuit, COMPRESSED_TRIVIAL_VP_VK}, + resource_logic_circuit::ResourceLogic, + resource_logic_examples::{ + TrivialResourceLogicCircuit, COMPRESSED_TRIVIAL_RESOURCE_LOGIC_VK, + }, }, constant::{ NUM_RESOURCE, POSEIDON_TO_CURVE_INPUT_LEN, PRF_EXPAND_PERSONALIZATION, @@ -10,7 +12,7 @@ use crate::{ }, merkle_tree::{Anchor, MerklePath, Node}, nullifier::{Nullifier, NullifierKeyContainer}, - shielded_ptx::ResourceVPVerifyingInfoSet, + shielded_ptx::ResourceLogicVerifyingInfoSet, utils::{poseidon_hash_n, poseidon_to_curve}, }; use blake2b_simd::Params as Blake2bParams; @@ -86,7 +88,7 @@ impl Hash for ResourceCommitment { pub struct Resource { pub kind: ResourceKind, /// value is the fungible data of the resource - /// sub-vps and any other data can be encoded to the value + /// sub-resource_logics and any other data can be encoded to the value pub value: pallas::Base, /// the quantity of the resource. pub quantity: u64, @@ -117,11 +119,11 @@ pub struct ResourceKind { #[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] pub struct RandomSeed([u8; 32]); -/// ResourceValidityPredicates includes one application(static) VP and a few dynamic VPs. +/// ResourceLogics consists of one application(static) resource logic and a few user(dynamic) resource logics. #[derive(Clone)] -pub struct ResourceValidityPredicates { - application_vp: Box, - dynamic_vps: Vec>, +pub struct ResourceLogics { + application_resource_logic: Box, + dynamic_resource_logics: Vec>, } impl Resource { @@ -195,7 +197,7 @@ impl Resource { } pub fn random_padding_resource(mut rng: R) -> Self { - let logic = *COMPRESSED_TRIVIAL_VP_VK; + let logic = *COMPRESSED_TRIVIAL_RESOURCE_LOGIC_VK; let label = pallas::Base::random(&mut rng); let kind = ResourceKind::new(logic, label); let value = pallas::Base::random(&mut rng); @@ -435,7 +437,7 @@ impl RandomSeed { pallas::Scalar::from_uniform_bytes(&bytes) } - pub fn get_vp_cm_r(&self, tag: u8) -> pallas::Base { + pub fn get_resource_logic_cm_r(&self, tag: u8) -> pallas::Base { let mut h = Blake2bParams::new() .hash_length(64) .personal(PRF_EXPAND_PERSONALIZATION) @@ -447,63 +449,67 @@ impl RandomSeed { } } -impl ResourceValidityPredicates { +impl ResourceLogics { pub fn new( - application_vp: Box, - dynamic_vps: Vec>, + application_resource_logic: Box, + dynamic_resource_logics: Vec>, ) -> Self { Self { - application_vp, - dynamic_vps, + application_resource_logic, + dynamic_resource_logics, } } - // Generate vp proofs - pub fn build(&self) -> ResourceVPVerifyingInfoSet { - let app_vp_verifying_info = self.application_vp.get_verifying_info(); + // Generate resource logic proofs + pub fn build(&self) -> ResourceLogicVerifyingInfoSet { + let app_resource_logic_verifying_info = + self.application_resource_logic.get_verifying_info(); - let app_dynamic_vp_verifying_info = self - .dynamic_vps + let app_dynamic_resource_logic_verifying_info = self + .dynamic_resource_logics .iter() .map(|verifying_info| verifying_info.get_verifying_info()) .collect(); - ResourceVPVerifyingInfoSet::new(app_vp_verifying_info, app_dynamic_vp_verifying_info) + ResourceLogicVerifyingInfoSet::new( + app_resource_logic_verifying_info, + app_dynamic_resource_logic_verifying_info, + ) } - // Create an input padding resource vps - pub fn create_input_padding_resource_vps( + // Create resource logics for an input padding resource + pub fn create_input_padding_resource_resource_logics( resource: &Resource, input_resources: [Resource; NUM_RESOURCE], output_resources: [Resource; NUM_RESOURCE], ) -> Self { let owned_resource_id = resource.get_nf().unwrap().inner(); - let application_vp = Box::new(TrivialValidityPredicateCircuit::new( + let application_resource_logic = Box::new(TrivialResourceLogicCircuit::new( owned_resource_id, input_resources, output_resources, )); Self { - application_vp, - dynamic_vps: vec![], + application_resource_logic, + dynamic_resource_logics: vec![], } } - // Create an output padding resource vps - pub fn create_output_padding_resource_vps( + // Create resource logics for an output padding resource + pub fn create_output_padding_resource_resource_logics( resource: &Resource, input_resources: [Resource; NUM_RESOURCE], output_resources: [Resource; NUM_RESOURCE], ) -> Self { let owned_resource_id = resource.commitment().inner(); - let application_vp = Box::new(TrivialValidityPredicateCircuit::new( + let application_resource_logic = Box::new(TrivialResourceLogicCircuit::new( owned_resource_id, input_resources, output_resources, )); Self { - application_vp, - dynamic_vps: vec![], + application_resource_logic, + dynamic_resource_logics: vec![], } } } diff --git a/taiga_halo2/src/vp_commitment.rs b/taiga_halo2/src/resource_logic_commitment.rs similarity index 72% rename from taiga_halo2/src/vp_commitment.rs rename to taiga_halo2/src/resource_logic_commitment.rs index ef71c813..a491977e 100644 --- a/taiga_halo2/src/vp_commitment.rs +++ b/taiga_halo2/src/resource_logic_commitment.rs @@ -1,4 +1,4 @@ -use crate::constant::VP_COMMITMENT_PERSONALIZATION; +use crate::constant::RESOURCE_LOGIC_COMMITMENT_PERSONALIZATION; use blake2s_simd::Params; use byteorder::{ByteOrder, LittleEndian}; use ff::PrimeField; @@ -10,15 +10,15 @@ use serde; #[derive(Clone, Debug)] #[cfg_attr(feature = "nif", derive(NifTuple))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub struct ValidityPredicateCommitment(Vec); +pub struct ResourceLogicCommitment(Vec); -impl ValidityPredicateCommitment { - pub fn commit(vp: &F, rcm: &F) -> Self { +impl ResourceLogicCommitment { + pub fn commit(resource_logic: &F, rcm: &F) -> Self { let hash = Params::new() .hash_length(32) - .personal(VP_COMMITMENT_PERSONALIZATION) + .personal(RESOURCE_LOGIC_COMMITMENT_PERSONALIZATION) .to_state() - .update(vp.to_repr().as_ref()) + .update(resource_logic.to_repr().as_ref()) .update(rcm.to_repr().as_ref()) .finalize(); Self(hash.as_bytes().to_vec()) @@ -46,8 +46,8 @@ impl ValidityPredicateCommitment { } } -impl Default for ValidityPredicateCommitment { - fn default() -> ValidityPredicateCommitment { - ValidityPredicateCommitment([0u8; 32].to_vec()) +impl Default for ResourceLogicCommitment { + fn default() -> ResourceLogicCommitment { + ResourceLogicCommitment([0u8; 32].to_vec()) } } diff --git a/taiga_halo2/src/vp_vk.rs b/taiga_halo2/src/resource_logic_vk.rs similarity index 59% rename from taiga_halo2/src/vp_vk.rs rename to taiga_halo2/src/resource_logic_vk.rs index b11ab3de..3bd67c9e 100644 --- a/taiga_halo2/src/vp_vk.rs +++ b/taiga_halo2/src/resource_logic_vk.rs @@ -7,14 +7,14 @@ use pasta_curves::{ use std::hash::Hash; #[derive(Debug, Clone)] -pub enum ValidityPredicateVerifyingKey { +pub enum ResourceLogicVerifyingKey { // VK. Uncompressed(VerifyingKey), // Compress vk into one element. Compressed(pallas::Base), } -impl ValidityPredicateVerifyingKey { +impl ResourceLogicVerifyingKey { pub fn from_vk(vk: VerifyingKey) -> Self { Self::Uncompressed(vk) } @@ -25,14 +25,14 @@ impl ValidityPredicateVerifyingKey { pub fn get_vk(&self) -> Option> { match self { - ValidityPredicateVerifyingKey::Uncompressed(vk) => Some(vk.clone()), - ValidityPredicateVerifyingKey::Compressed(_) => None, + ResourceLogicVerifyingKey::Uncompressed(vk) => Some(vk.clone()), + ResourceLogicVerifyingKey::Compressed(_) => None, } } pub fn get_compressed(&self) -> pallas::Base { match self { - ValidityPredicateVerifyingKey::Uncompressed(vk) => { + ResourceLogicVerifyingKey::Uncompressed(vk) => { let mut hasher = Blake2bParams::new() .hash_length(64) .personal(b"Halo2-Verify-Key") @@ -46,35 +46,35 @@ impl ValidityPredicateVerifyingKey { // Hash in final Blake2bState pallas::Base::from_uniform_bytes(hasher.finalize().as_array()) } - ValidityPredicateVerifyingKey::Compressed(v) => *v, + ResourceLogicVerifyingKey::Compressed(v) => *v, } } } -impl Default for ValidityPredicateVerifyingKey { - fn default() -> ValidityPredicateVerifyingKey { - ValidityPredicateVerifyingKey::Compressed(pallas::Base::one()) +impl Default for ResourceLogicVerifyingKey { + fn default() -> ResourceLogicVerifyingKey { + ResourceLogicVerifyingKey::Compressed(pallas::Base::one()) } } -impl Hash for ValidityPredicateVerifyingKey { +impl Hash for ResourceLogicVerifyingKey { fn hash(&self, state: &mut H) { let compressed = self.get_compressed(); compressed.to_repr().as_ref().hash(state); } } -impl PartialEq for ValidityPredicateVerifyingKey { +impl PartialEq for ResourceLogicVerifyingKey { fn eq(&self, other: &Self) -> bool { self.get_compressed() == other.get_compressed() } } -impl Eq for ValidityPredicateVerifyingKey {} +impl Eq for ResourceLogicVerifyingKey {} #[test] -fn test_vpd_hashing() { - use crate::circuit::vp_examples::tests::random_trivial_vp_circuit; +fn test_resource_logicd_hashing() { + use crate::circuit::resource_logic_examples::tests::random_trivial_resource_logic_circuit; use halo2_proofs::plonk; use rand::rngs::OsRng; use std::{collections::hash_map::DefaultHasher, hash::Hasher}; @@ -85,39 +85,45 @@ fn test_vpd_hashing() { s.finish() } - let circuit1 = random_trivial_vp_circuit(&mut OsRng); - let circuit2 = random_trivial_vp_circuit(&mut OsRng); - let circuit3 = random_trivial_vp_circuit(&mut OsRng); + let circuit1 = random_trivial_resource_logic_circuit(&mut OsRng); + let circuit2 = random_trivial_resource_logic_circuit(&mut OsRng); + let circuit3 = random_trivial_resource_logic_circuit(&mut OsRng); let params1 = halo2_proofs::poly::commitment::Params::new(12); let vk1 = plonk::keygen_vk(¶ms1, &circuit1).unwrap(); - let vpd1 = ValidityPredicateVerifyingKey::from_vk(vk1.clone()); + let resource_logicd1 = ResourceLogicVerifyingKey::from_vk(vk1.clone()); let vk1s = format!("{:?}", vk1.pinned()); let params2 = halo2_proofs::poly::commitment::Params::new(12); let vk2 = plonk::keygen_vk(¶ms2, &circuit2).unwrap(); - let vpd2 = ValidityPredicateVerifyingKey::from_vk(vk2.clone()); + let resource_logicd2 = ResourceLogicVerifyingKey::from_vk(vk2.clone()); let vk2s = format!("{:?}", vk2.pinned()); // Same circuit, same param => same key assert_eq!(vk1s, vk2s); // check that the keys are actually the same - assert_eq!(calculate_hash(&vpd1), calculate_hash(&vpd2)); // check that the hashes are the same - assert_eq!(vpd1, vpd2); // check that the vpd's are equal + assert_eq!( + calculate_hash(&resource_logicd1), + calculate_hash(&resource_logicd2) + ); // check that the hashes are the same + assert_eq!(resource_logicd1, resource_logicd2); // check that the resource_logicd's are equal let params3 = halo2_proofs::poly::commitment::Params::new(13); // different param => different key let vk3 = plonk::keygen_vk(¶ms3, &circuit3).unwrap(); - let vpd3 = ValidityPredicateVerifyingKey::from_vk(vk3.clone()); + let resource_logicd3 = ResourceLogicVerifyingKey::from_vk(vk3.clone()); let vk3s = format!("{:?}", vk3.pinned()); // Same circuit, different param => different key assert_ne!(vk1s, vk3s); // check that the keys are actually different - assert_ne!(calculate_hash(&vpd1), calculate_hash(&vpd3)); // check that the hashes are different - assert_ne!(vpd1, vpd3); // check that the vpd's are not equal + assert_ne!( + calculate_hash(&resource_logicd1), + calculate_hash(&resource_logicd3) + ); // check that the hashes are different + assert_ne!(resource_logicd1, resource_logicd3); // check that the resource_logicd's are not equal // test with actual hashset use std::collections::HashSet; let mut set = HashSet::new(); - assert!(set.insert(vpd1)); - assert!(!set.insert(vpd2)); - assert!(set.insert(vpd3)); + assert!(set.insert(resource_logicd1)); + assert!(!set.insert(resource_logicd2)); + assert!(set.insert(resource_logicd3)); } diff --git a/taiga_halo2/src/shielded_ptx.rs b/taiga_halo2/src/shielded_ptx.rs index 68b1735d..f3ab9b31 100644 --- a/taiga_halo2/src/shielded_ptx.rs +++ b/taiga_halo2/src/shielded_ptx.rs @@ -1,8 +1,8 @@ -use crate::circuit::vp_circuit::{VPVerifyingInfo, ValidityPredicate}; +use crate::circuit::resource_logic_circuit::{ResourceLogic, ResourceLogicVerifyingInfo}; use crate::compliance::{ComplianceInfo, CompliancePublicInputs}; use crate::constant::{ COMPLIANCE_CIRCUIT_PARAMS_SIZE, COMPLIANCE_PROVING_KEY, COMPLIANCE_VERIFYING_KEY, - MAX_DYNAMIC_VP_NUM, NUM_RESOURCE, SETUP_PARAMS_MAP, + MAX_DYNAMIC_RESOURCE_LOGIC_NUM, NUM_RESOURCE, SETUP_PARAMS_MAP, }; use crate::delta_commitment::DeltaCommitment; use crate::error::TransactionError; @@ -10,7 +10,7 @@ use crate::executable::Executable; use crate::merkle_tree::Anchor; use crate::nullifier::Nullifier; use crate::proof::Proof; -use crate::resource::{ResourceCommitment, ResourceValidityPredicates}; +use crate::resource::{ResourceCommitment, ResourceLogics}; use halo2_proofs::plonk::Error; use pasta_curves::pallas; use rand::RngCore; @@ -21,7 +21,7 @@ use rustler::{Decoder, Encoder, Env, NifResult, NifStruct, Term}; #[cfg(feature = "serde")] use serde; -use crate::circuit::vp_bytecode::ApplicationByteCode; +use crate::circuit::resource_logic_bytecode::ApplicationByteCode; #[cfg(feature = "borsh")] use borsh::{BorshDeserialize, BorshSerialize}; #[cfg(feature = "borsh")] @@ -31,8 +31,8 @@ use ff::PrimeField; #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ShieldedPartialTransaction { compliances: [ComplianceVerifyingInfo; NUM_RESOURCE], - inputs: [ResourceVPVerifyingInfoSet; NUM_RESOURCE], - outputs: [ResourceVPVerifyingInfoSet; NUM_RESOURCE], + inputs: [ResourceLogicVerifyingInfoSet; NUM_RESOURCE], + outputs: [ResourceLogicVerifyingInfoSet; NUM_RESOURCE], binding_sig_r: Option, hints: Vec, } @@ -52,11 +52,11 @@ pub struct ComplianceVerifyingInfo { #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "nif", derive(NifStruct))] #[cfg_attr(feature = "nif", module = "Taiga.Resource.VerifyingInfo")] -pub struct ResourceVPVerifyingInfoSet { - app_vp_verifying_info: VPVerifyingInfo, - app_dynamic_vp_verifying_info: Vec, - // TODO: add verifier proof and according public inputs. - // When the verifier proof is added, we may need to reconsider the structure of `VPVerifyingInfo` +pub struct ResourceLogicVerifyingInfoSet { + app_resource_logic_verifying_info: ResourceLogicVerifyingInfo, + app_dynamic_resource_logic_verifying_info: Vec, + // TODO function privacy: add verifier proof and the corresponding public inputs. + // When the verifier proof is added, we may need to reconsider the structure of `ResourceLogicVerifyingInfo` } // Is easier to derive traits for @@ -65,8 +65,8 @@ pub struct ResourceVPVerifyingInfoSet { #[cfg_attr(feature = "nif", module = "Taiga.Shielded.PTX")] struct ShieldedPartialTransactionProxy { compliances: Vec, - inputs: Vec, - outputs: Vec, + inputs: Vec, + outputs: Vec, binding_sig_r: Option, hints: Vec, } @@ -107,8 +107,8 @@ impl ShieldedPartialTransaction { pub fn build( compliance_pairs: Vec, - input_resource_vps: Vec, - output_resource_vps: Vec, + input_resource_resource_logics: Vec, + output_resource_resource_logics: Vec, hints: Vec, mut rng: R, ) -> Result { @@ -122,16 +122,16 @@ impl ShieldedPartialTransaction { }) .collect(); - // Generate input vp proofs - let inputs: Vec = input_resource_vps + // Generate input resource logic proofs + let inputs: Vec = input_resource_resource_logics .iter() - .map(|input_resource_vp| input_resource_vp.build()) + .map(|input_resource_resource_logic| input_resource_resource_logic.build()) .collect(); - // Generate output vp proofs - let outputs: Vec = output_resource_vps + // Generate output resource logic proofs + let outputs: Vec = output_resource_resource_logics .iter() - .map(|output_resource_vp| output_resource_vp.build()) + .map(|output_resource_resource_logic| output_resource_resource_logic.build()) .collect(); Ok(Self { @@ -150,11 +150,11 @@ impl ShieldedPartialTransaction { verifying_info.verify()?; } - // Verify vp proofs from input resources + // Verify resource logic proofs of input resources for verifying_info in self.inputs.iter() { verifying_info.verify()?; } - // Verify vp proofs from output resources + // Verify resource logic proofs of output resources for verifying_info in self.outputs.iter() { verifying_info.verify()?; } @@ -166,9 +166,9 @@ impl ShieldedPartialTransaction { fn check_nullifiers(&self) -> Result<(), TransactionError> { assert_eq!(NUM_RESOURCE, 2); let compliance_nfs = self.get_nullifiers(); - for vp_info in self.inputs.iter().chain(self.outputs.iter()) { - for nfs in vp_info.get_nullifiers().iter() { - // Check the vp actually uses the input resources from compliance circuits. + for resource_logic_info in self.inputs.iter().chain(self.outputs.iter()) { + for nfs in resource_logic_info.get_nullifiers().iter() { + // Check the resource logic actually uses the input resources from compliance circuits. if !((compliance_nfs[0].inner() == nfs[0] && compliance_nfs[1].inner() == nfs[1]) || (compliance_nfs[0].inner() == nfs[1] && compliance_nfs[1].inner() == nfs[0])) { @@ -177,16 +177,22 @@ impl ShieldedPartialTransaction { } } - for (vp_info, compliance_nf) in self.inputs.iter().zip(compliance_nfs.iter()) { - // Check the app vp and the sub vps use the same owned_resource_id in one resource - let owned_resource_id = vp_info.app_vp_verifying_info.get_owned_resource_id(); - for logic_vp_verifying_info in vp_info.app_dynamic_vp_verifying_info.iter() { - if owned_resource_id != logic_vp_verifying_info.get_owned_resource_id() { + for (resource_logic_info, compliance_nf) in self.inputs.iter().zip(compliance_nfs.iter()) { + // Check the app resource logic and the sub resource logics use the same owned_resource_id in one resource + let owned_resource_id = resource_logic_info + .app_resource_logic_verifying_info + .get_owned_resource_id(); + for logic_resource_logic_verifying_info in resource_logic_info + .app_dynamic_resource_logic_verifying_info + .iter() + { + if owned_resource_id != logic_resource_logic_verifying_info.get_owned_resource_id() + { return Err(TransactionError::InconsistentOwnedResourceID); } } - // Check the owned_resource_id that vp uses is consistent with the nf from the compliance circuit + // Check the owned_resource_id that resource logic uses is consistent with the nf from the compliance circuit if owned_resource_id != compliance_nf.inner() { return Err(TransactionError::InconsistentOwnedResourceID); } @@ -198,9 +204,9 @@ impl ShieldedPartialTransaction { fn check_resource_commitments(&self) -> Result<(), TransactionError> { assert_eq!(NUM_RESOURCE, 2); let compliance_cms = self.get_output_cms(); - for vp_info in self.inputs.iter().chain(self.outputs.iter()) { - for cms in vp_info.get_resource_commitments().iter() { - // Check the vp actually uses the output resources from compliance circuits. + for resource_logic_info in self.inputs.iter().chain(self.outputs.iter()) { + for cms in resource_logic_info.get_resource_commitments().iter() { + // Check the resource logic actually uses the output resources from compliance circuits. if !((compliance_cms[0] == cms[0] && compliance_cms[1] == cms[1]) || (compliance_cms[0] == cms[1] && compliance_cms[1] == cms[0])) { @@ -209,16 +215,22 @@ impl ShieldedPartialTransaction { } } - for (vp_info, compliance_cm) in self.outputs.iter().zip(compliance_cms.iter()) { - // Check that the app vp and the sub vps use the same owned_resource_id in one resource - let owned_resource_id = vp_info.app_vp_verifying_info.get_owned_resource_id(); - for logic_vp_verifying_info in vp_info.app_dynamic_vp_verifying_info.iter() { - if owned_resource_id != logic_vp_verifying_info.get_owned_resource_id() { + for (resource_logic_info, compliance_cm) in self.outputs.iter().zip(compliance_cms.iter()) { + // Check that the app resource logic and the sub resource_logics use the same owned_resource_id in one resource + let owned_resource_id = resource_logic_info + .app_resource_logic_verifying_info + .get_owned_resource_id(); + for logic_resource_logic_verifying_info in resource_logic_info + .app_dynamic_resource_logic_verifying_info + .iter() + { + if owned_resource_id != logic_resource_logic_verifying_info.get_owned_resource_id() + { return Err(TransactionError::InconsistentOwnedResourceID); } } - // Check the owned_resource_id that vp uses is consistent with the cm from the compliance circuit + // Check the owned_resource_id that resource logic uses is consistent with the cm from the compliance circuit if owned_resource_id != compliance_cm.inner() { return Err(TransactionError::InconsistentOwnedResourceID); } @@ -344,10 +356,10 @@ impl BorshDeserialize for ShieldedPartialTransaction { .map(|_| ComplianceVerifyingInfo::deserialize_reader(reader)) .collect::>()?; let inputs: Vec<_> = (0..NUM_RESOURCE) - .map(|_| ResourceVPVerifyingInfoSet::deserialize_reader(reader)) + .map(|_| ResourceLogicVerifyingInfoSet::deserialize_reader(reader)) .collect::>()?; let outputs: Vec<_> = (0..NUM_RESOURCE) - .map(|_| ResourceVPVerifyingInfoSet::deserialize_reader(reader)) + .map(|_| ResourceLogicVerifyingInfoSet::deserialize_reader(reader)) .collect::>()?; let binding_sig_r_type = reader.read_u8()?; let binding_sig_r = if binding_sig_r_type == 0 { @@ -415,66 +427,70 @@ impl ComplianceVerifyingInfo { } } -impl ResourceVPVerifyingInfoSet { +impl ResourceLogicVerifyingInfoSet { pub fn new( - app_vp_verifying_info: VPVerifyingInfo, - app_dynamic_vp_verifying_info: Vec, + app_resource_logic_verifying_info: ResourceLogicVerifyingInfo, + app_dynamic_resource_logic_verifying_info: Vec, ) -> Self { - assert!(app_dynamic_vp_verifying_info.len() <= MAX_DYNAMIC_VP_NUM); + assert!(app_dynamic_resource_logic_verifying_info.len() <= MAX_DYNAMIC_RESOURCE_LOGIC_NUM); Self { - app_vp_verifying_info, - app_dynamic_vp_verifying_info, + app_resource_logic_verifying_info, + app_dynamic_resource_logic_verifying_info, } } // TODO: remove it. pub fn build( - application_vp: Box, - dynamic_vps: Vec>, + application_resource_logic: Box, + dynamic_resource_logics: Vec>, ) -> Self { - assert!(dynamic_vps.len() <= MAX_DYNAMIC_VP_NUM); + assert!(dynamic_resource_logics.len() <= MAX_DYNAMIC_RESOURCE_LOGIC_NUM); - let app_vp_verifying_info = application_vp.get_verifying_info(); + let app_resource_logic_verifying_info = application_resource_logic.get_verifying_info(); - let app_dynamic_vp_verifying_info = dynamic_vps + let app_dynamic_resource_logic_verifying_info = dynamic_resource_logics .into_iter() .map(|verifying_info| verifying_info.get_verifying_info()) .collect(); Self { - app_vp_verifying_info, - app_dynamic_vp_verifying_info, + app_resource_logic_verifying_info, + app_dynamic_resource_logic_verifying_info, } } pub fn verify(&self) -> Result<(), Error> { - // Verify application vp proof - self.app_vp_verifying_info.verify()?; + // Verify the application resource logic proof + self.app_resource_logic_verifying_info.verify()?; - // Verify application dynamic vp proofs - for verify_info in self.app_dynamic_vp_verifying_info.iter() { + // Verify application dynamic resource logic proofs + for verify_info in self.app_dynamic_resource_logic_verifying_info.iter() { verify_info.verify()?; } - // TODO: Verify vp verifier proofs + // TODO function privacy: Verify resource logic verifier proofs Ok(()) } pub fn get_nullifiers(&self) -> Vec<[pallas::Base; NUM_RESOURCE]> { - let mut nfs = vec![self.app_vp_verifying_info.get_nullifiers()]; - self.app_dynamic_vp_verifying_info + let mut nfs = vec![self.app_resource_logic_verifying_info.get_nullifiers()]; + self.app_dynamic_resource_logic_verifying_info .iter() - .for_each(|vp_info| nfs.push(vp_info.get_nullifiers())); + .for_each(|resource_logic_info| nfs.push(resource_logic_info.get_nullifiers())); nfs } pub fn get_resource_commitments(&self) -> Vec<[ResourceCommitment; NUM_RESOURCE]> { - let mut cms = vec![self.app_vp_verifying_info.get_resource_commitments()]; - self.app_dynamic_vp_verifying_info + let mut cms = vec![self + .app_resource_logic_verifying_info + .get_resource_commitments()]; + self.app_dynamic_resource_logic_verifying_info .iter() - .for_each(|vp_info| cms.push(vp_info.get_resource_commitments())); + .for_each(|resource_logic_info| { + cms.push(resource_logic_info.get_resource_commitments()) + }); cms } } @@ -482,13 +498,13 @@ impl ResourceVPVerifyingInfoSet { #[cfg(test)] pub mod testing { use crate::{ - circuit::vp_circuit::{ValidityPredicate, ValidityPredicateVerifyingInfo}, - circuit::vp_examples::TrivialValidityPredicateCircuit, + circuit::resource_logic_circuit::{ResourceLogic, ResourceLogicVerifyingInfoTrait}, + circuit::resource_logic_examples::TrivialResourceLogicCircuit, compliance::ComplianceInfo, constant::TAIGA_COMMITMENT_TREE_DEPTH, merkle_tree::MerklePath, nullifier::Nullifier, - resource::{Resource, ResourceValidityPredicates}, + resource::{Resource, ResourceLogics}, shielded_ptx::ShieldedPartialTransaction, utils::poseidon_hash, }; @@ -499,27 +515,33 @@ pub mod testing { pub fn create_shielded_ptx() -> ShieldedPartialTransaction { let mut rng = OsRng; - // Create empty VP circuit without resource info - let trivial_vp_circuit = TrivialValidityPredicateCircuit::default(); - let trivial_vp_vk = trivial_vp_circuit.get_vp_vk(); - let compressed_trivial_vp_vk = trivial_vp_vk.get_compressed(); + // Create empty resource logic circuit without resource info + let trivial_resource_logic_circuit = TrivialResourceLogicCircuit::default(); + let trivial_resource_logic_vk = trivial_resource_logic_circuit.get_resource_logic_vk(); + let compressed_trivial_resource_logic_vk = trivial_resource_logic_vk.get_compressed(); // Generate resources let input_resource_1 = { let label = pallas::Base::zero(); - // TODO: add real application dynamic VPs and encode them to value later. - let app_dynamic_vp_vk = [compressed_trivial_vp_vk, compressed_trivial_vp_vk]; - // Encode the app_dynamic_vp_vk into value - // The encoding method is flexible and defined in the application vp. - // Use poseidon hash to encode the two dynamic VPs here - let value = poseidon_hash(app_dynamic_vp_vk[0], app_dynamic_vp_vk[1]); + // TODO: add real application dynamic resource logics and encode them to value later. + let app_dynamic_resource_logic_vk = [ + compressed_trivial_resource_logic_vk, + compressed_trivial_resource_logic_vk, + ]; + // Encode the app_dynamic_resource_logic_vk into value + // The encoding method is flexible and defined in the application resource logic. + // Use poseidon hash to encode the two dynamic resource logics here + let value = poseidon_hash( + app_dynamic_resource_logic_vk[0], + app_dynamic_resource_logic_vk[1], + ); let nonce = Nullifier::from(pallas::Base::random(&mut rng)); let quantity = 5000u64; let nk = pallas::Base::random(&mut rng); let rseed = pallas::Base::random(&mut rng); let is_ephemeral = false; Resource::new_input_resource( - compressed_trivial_vp_vk, + compressed_trivial_resource_logic_vk, label, value, quantity, @@ -531,15 +553,15 @@ pub mod testing { }; let mut output_resource_1 = { let label = pallas::Base::zero(); - // TODO: add real application dynamic VPs and encode them to value later. - // If the dynamic VP is not used, set value pallas::Base::zero() by default. + // TODO: add real application dynamic resource logics and encode them to value later. + // If the dynamic resource logic is not used, set value pallas::Base::zero() by default. let value = pallas::Base::zero(); let quantity = 5000u64; let npk = pallas::Base::random(&mut rng); let rseed = pallas::Base::random(&mut rng); let is_ephemeral = false; Resource::new_output_resource( - compressed_trivial_vp_vk, + compressed_trivial_resource_logic_vk, label, value, quantity, @@ -569,7 +591,7 @@ pub mod testing { let rseed = pallas::Base::random(&mut rng); let is_ephemeral = false; Resource::new_input_resource( - compressed_trivial_vp_vk, + compressed_trivial_resource_logic_vk, label, value, quantity, @@ -587,7 +609,7 @@ pub mod testing { let rseed = pallas::Base::random(&mut rng); let is_ephemeral = false; Resource::new_output_resource( - compressed_trivial_vp_vk, + compressed_trivial_resource_logic_vk, label, value, quantity, @@ -607,39 +629,50 @@ pub mod testing { &mut rng, ); - // Create vp circuit and fill the resource info - let mut trivial_vp_circuit = TrivialValidityPredicateCircuit { + // Create resource logic circuit and fill the resource info + let mut trivial_resource_logic_circuit = TrivialResourceLogicCircuit { owned_resource_id: input_resource_1.get_nf().unwrap().inner(), input_resources: [input_resource_1, input_resource_2], output_resources: [output_resource_1, output_resource_2], }; - let input_application_vp_1 = Box::new(trivial_vp_circuit.clone()); - let trivial_app_logic_1: Box = Box::new(trivial_vp_circuit.clone()); - let trivial_app_logic_2 = Box::new(trivial_vp_circuit.clone()); - let trivial_dynamic_vps = vec![trivial_app_logic_1, trivial_app_logic_2]; - let input_resource_1_vps = - ResourceValidityPredicates::new(input_application_vp_1, trivial_dynamic_vps); - - // The following resources use empty logic vps and use value with pallas::Base::zero() by default. - trivial_vp_circuit.owned_resource_id = input_resource_2.get_nf().unwrap().inner(); - let input_application_vp_2 = Box::new(trivial_vp_circuit.clone()); - let input_resource_2_vps = ResourceValidityPredicates::new(input_application_vp_2, vec![]); - - trivial_vp_circuit.owned_resource_id = output_resource_1.commitment().inner(); - let output_application_vp_1 = Box::new(trivial_vp_circuit.clone()); - let output_resource_1_vps = - ResourceValidityPredicates::new(output_application_vp_1, vec![]); - - trivial_vp_circuit.owned_resource_id = output_resource_2.commitment().inner(); - let output_application_vp_2 = Box::new(trivial_vp_circuit); - let output_resource_2_vps = - ResourceValidityPredicates::new(output_application_vp_2, vec![]); + let input_application_resource_logic_1 = Box::new(trivial_resource_logic_circuit.clone()); + let trivial_app_logic_1: Box = + Box::new(trivial_resource_logic_circuit.clone()); + let trivial_app_logic_2 = Box::new(trivial_resource_logic_circuit.clone()); + let trivial_dynamic_resource_logics = vec![trivial_app_logic_1, trivial_app_logic_2]; + let input_resource_1_resource_logics = ResourceLogics::new( + input_application_resource_logic_1, + trivial_dynamic_resource_logics, + ); + + // The following resources use empty logic resource_logics and use value with pallas::Base::zero() by default. + trivial_resource_logic_circuit.owned_resource_id = + input_resource_2.get_nf().unwrap().inner(); + let input_application_resource_logic_2 = Box::new(trivial_resource_logic_circuit.clone()); + let input_resource_2_resource_logics = + ResourceLogics::new(input_application_resource_logic_2, vec![]); + + trivial_resource_logic_circuit.owned_resource_id = output_resource_1.commitment().inner(); + let output_application_resource_logic_1 = Box::new(trivial_resource_logic_circuit.clone()); + let output_resource_1_resource_logics = + ResourceLogics::new(output_application_resource_logic_1, vec![]); + + trivial_resource_logic_circuit.owned_resource_id = output_resource_2.commitment().inner(); + let output_application_resource_logic_2 = Box::new(trivial_resource_logic_circuit); + let output_resource_2_resource_logics = + ResourceLogics::new(output_application_resource_logic_2, vec![]); // Create shielded partial tx ShieldedPartialTransaction::build( vec![compliance_1, compliance_2], - vec![input_resource_1_vps, input_resource_2_vps], - vec![output_resource_1_vps, output_resource_2_vps], + vec![ + input_resource_1_resource_logics, + input_resource_2_resource_logics, + ], + vec![ + output_resource_1_resource_logics, + output_resource_2_resource_logics, + ], vec![], &mut rng, ) diff --git a/taiga_halo2/src/taiga_api.rs b/taiga_halo2/src/taiga_api.rs index 4a1742dc..e2840b3c 100644 --- a/taiga_halo2/src/taiga_api.rs +++ b/taiga_halo2/src/taiga_api.rs @@ -1,6 +1,6 @@ #[cfg(feature = "borsh")] use crate::{ - circuit::vp_bytecode::ApplicationByteCode, compliance::ComplianceInfo, + circuit::resource_logic_bytecode::ApplicationByteCode, compliance::ComplianceInfo, transaction::TransactionResult, }; use crate::{ @@ -52,7 +52,6 @@ pub fn create_input_resource( ) } -/// pub fn create_output_resource( logic: pallas::Base, label: pallas::Base, @@ -108,22 +107,22 @@ pub fn resource_deserialize(bytes: Vec) -> std::io::Result { /// | Parameters | type | size(bytes) | /// | - | - | - | /// | 2 compliance proofs | ComplianceVerifyingInfo| 4676 * 2 | -/// | input1 static vp proof | VPVerifyingInfo | 158216 | -/// | input1 dynamic vp num(by borsh) | u32 | 4 | -/// | input1 dynamic vp proof | VPVerifyingInfo | 158216 * num | -/// | input2 static vp proof | VPVerifyingInfo | 158216 | -/// | input2 dynamic vp num(by borsh) | u32 | 4 | -/// | input2 dynamic vp proof | VPVerifyingInfo | 158216 * num | -/// | output1 static vp proof | VPVerifyingInfo | 158216 | -/// | output1 dynamic vp num(by borsh) | u32 | 4 | -/// | output1 dynamic vp proofs | VPVerifyingInfo | 158216 * num | -/// | output2 static vp proof | VPVerifyingInfo | 158216 | -/// | output2 dynamic vp num(by borsh) | u32 | 4 | -/// | output2 dynamic vp proofs | VPVerifyingInfo | 158216 * num | +/// | input1 static resource_logic proof | ResourceLogicVerifyingInfo | 158216 | +/// | input1 dynamic resource_logic num(by borsh) | u32 | 4 | +/// | input1 dynamic resource_logic proof | ResourceLogicVerifyingInfo | 158216 * num | +/// | input2 static resource_logic proof | ResourceLogicVerifyingInfo | 158216 | +/// | input2 dynamic resource_logic num(by borsh) | u32 | 4 | +/// | input2 dynamic resource_logic proof | ResourceLogicVerifyingInfo | 158216 * num | +/// | output1 static resource_logic proof | ResourceLogicVerifyingInfo | 158216 | +/// | output1 dynamic resource_logic num(by borsh) | u32 | 4 | +/// | output1 dynamic resource_logic proofs | ResourceLogicVerifyingInfo | 158216 * num | +/// | output2 static resource_logic proof | ResourceLogicVerifyingInfo | 158216 | +/// | output2 dynamic resource_logic num(by borsh) | u32 | 4 | +/// | output2 dynamic resource_logic proofs | ResourceLogicVerifyingInfo | 158216 * num | /// | binding_sig_r | Option| 1 or (1 + 32) | /// | hints | Vec | - | /// -/// Resource: Ultimately, vp proofs won't go to the ptx. It's verifier proofs instead. +/// Resource: Ultimately, resource_logic proofs won't go to the ptx. It's verifier proofs instead. /// The verifier proof may have a much smaller size since the verifier verifying-key /// is a constant and can be cached. #[cfg(feature = "borsh")] @@ -160,7 +159,7 @@ pub fn transaction_deserialize(bytes: Vec) -> std::io::Result { BorshDeserialize::deserialize(&mut bytes.as_ref()) } -/// Create a shielded partial transaction from vp bytecode +/// Create a shielded partial transaction from resource_logic bytecode #[cfg(feature = "borsh")] pub fn create_shielded_partial_transaction( compliances: Vec, @@ -255,7 +254,7 @@ pub mod tests { // #[ignore] #[test] fn ptx_example_test() { - use crate::circuit::vp_examples::TrivialValidityPredicateCircuit; + use crate::circuit::resource_logic_examples::TrivialResourceLogicCircuit; use crate::compliance::ComplianceInfo; use crate::constant::TAIGA_COMMITMENT_TREE_DEPTH; use crate::merkle_tree::MerklePath; @@ -290,43 +289,43 @@ pub mod tests { // construct applications let input_resource_1_app = { - let app_vp = TrivialValidityPredicateCircuit::new( + let app_resource_logic = TrivialResourceLogicCircuit::new( input_resource_1_nf.inner(), [input_resource_1, input_resource_2], [output_resource_1, output_resource_2], ); - ApplicationByteCode::new(app_vp.to_bytecode(), vec![]) + ApplicationByteCode::new(app_resource_logic.to_bytecode(), vec![]) }; let input_resource_2_app = { - let app_vp = TrivialValidityPredicateCircuit::new( + let app_resource_logic = TrivialResourceLogicCircuit::new( input_resource_2_nf.inner(), [input_resource_1, input_resource_2], [output_resource_1, output_resource_2], ); - ApplicationByteCode::new(app_vp.to_bytecode(), vec![]) + ApplicationByteCode::new(app_resource_logic.to_bytecode(), vec![]) }; let output_resource_1_app = { - let app_vp = TrivialValidityPredicateCircuit::new( + let app_resource_logic = TrivialResourceLogicCircuit::new( output_resource_1.commitment().inner(), [input_resource_1, input_resource_2], [output_resource_1, output_resource_2], ); - ApplicationByteCode::new(app_vp.to_bytecode(), vec![]) + ApplicationByteCode::new(app_resource_logic.to_bytecode(), vec![]) }; let output_resource_2_app = { - let app_vp = TrivialValidityPredicateCircuit::new( + let app_resource_logic = TrivialResourceLogicCircuit::new( output_resource_2.commitment().inner(), [input_resource_1, input_resource_2], [output_resource_1, output_resource_2], ); - ApplicationByteCode::new(app_vp.to_bytecode(), vec![]) + ApplicationByteCode::new(app_resource_logic.to_bytecode(), vec![]) }; // construct ptx diff --git a/taiga_halo2/src/transparent_ptx.rs b/taiga_halo2/src/transparent_ptx.rs index e074b859..937e8241 100644 --- a/taiga_halo2/src/transparent_ptx.rs +++ b/taiga_halo2/src/transparent_ptx.rs @@ -1,7 +1,8 @@ use crate::{ - circuit::vp_bytecode::ApplicationByteCode, compliance::ComplianceInfo, constant::NUM_RESOURCE, - delta_commitment::DeltaCommitment, error::TransactionError, executable::Executable, - merkle_tree::Anchor, nullifier::Nullifier, resource::ResourceCommitment, + circuit::resource_logic_bytecode::ApplicationByteCode, compliance::ComplianceInfo, + constant::NUM_RESOURCE, delta_commitment::DeltaCommitment, error::TransactionError, + executable::Executable, merkle_tree::Anchor, nullifier::Nullifier, + resource::ResourceCommitment, }; use pasta_curves::pallas; @@ -43,20 +44,22 @@ impl TransparentPartialTransaction { impl Executable for TransparentPartialTransaction { fn execute(&self) -> Result<(), TransactionError> { - // check VPs, nullifiers, and resource commitments + // check resource logics, nullifiers, and resource commitments let compliance_nfs = self.get_nullifiers(); let compliance_cms = self.get_output_cms(); - for (vp, nf) in self.input_resource_app.iter().zip(compliance_nfs.iter()) { - let owned_resource_id = vp.verify_transparently(&compliance_nfs, &compliance_cms)?; - // Check all resources are checked + for (resource_logic, nf) in self.input_resource_app.iter().zip(compliance_nfs.iter()) { + let owned_resource_id = + resource_logic.verify_transparently(&compliance_nfs, &compliance_cms)?; + // Make sure all resource logics are checked if owned_resource_id != nf.inner() { return Err(TransactionError::InconsistentOwnedResourceID); } } - for (vp, cm) in self.output_resource_app.iter().zip(compliance_cms.iter()) { - let owned_resource_id = vp.verify_transparently(&compliance_nfs, &compliance_cms)?; - // Check all resources are checked + for (resource_logic, cm) in self.output_resource_app.iter().zip(compliance_cms.iter()) { + let owned_resource_id = + resource_logic.verify_transparently(&compliance_nfs, &compliance_cms)?; + // Make sure all resource logics are checked if owned_resource_id != cm.inner() { return Err(TransactionError::InconsistentOwnedResourceID); } @@ -102,7 +105,7 @@ impl Executable for TransparentPartialTransaction { #[cfg(feature = "borsh")] pub mod testing { use crate::{ - circuit::vp_examples::TrivialValidityPredicateCircuit, + circuit::resource_logic_examples::TrivialResourceLogicCircuit, constant::TAIGA_COMMITMENT_TREE_DEPTH, merkle_tree::MerklePath, resource::tests::random_resource, transparent_ptx::*, }; @@ -145,43 +148,43 @@ pub mod testing { // construct applications let input_resource_1_app = { - let app_vp = TrivialValidityPredicateCircuit::new( + let app_resource_logic = TrivialResourceLogicCircuit::new( input_resource_1.get_nf().unwrap().inner(), [input_resource_1, input_resource_2], [output_resource_1, output_resource_2], ); - ApplicationByteCode::new(app_vp.to_bytecode(), vec![]) + ApplicationByteCode::new(app_resource_logic.to_bytecode(), vec![]) }; let input_resource_2_app = { - let app_vp = TrivialValidityPredicateCircuit::new( + let app_resource_logic = TrivialResourceLogicCircuit::new( input_resource_2.get_nf().unwrap().inner(), [input_resource_1, input_resource_2], [output_resource_1, output_resource_2], ); - ApplicationByteCode::new(app_vp.to_bytecode(), vec![]) + ApplicationByteCode::new(app_resource_logic.to_bytecode(), vec![]) }; let output_resource_1_app = { - let app_vp = TrivialValidityPredicateCircuit::new( + let app_resource_logic = TrivialResourceLogicCircuit::new( output_resource_1.commitment().inner(), [input_resource_1, input_resource_2], [output_resource_1, output_resource_2], ); - ApplicationByteCode::new(app_vp.to_bytecode(), vec![]) + ApplicationByteCode::new(app_resource_logic.to_bytecode(), vec![]) }; let output_resource_2_app = { - let app_vp = TrivialValidityPredicateCircuit::new( + let app_resource_logic = TrivialResourceLogicCircuit::new( output_resource_2.commitment().inner(), [input_resource_1, input_resource_2], [output_resource_1, output_resource_2], ); - ApplicationByteCode::new(app_vp.to_bytecode(), vec![]) + ApplicationByteCode::new(app_resource_logic.to_bytecode(), vec![]) }; TransparentPartialTransaction::new(