Skip to content

Commit

Permalink
add or_relation_intent_vp (de)serialization and add or_relation_inten…
Browse files Browse the repository at this point in the history
…t_vp to vp_bytecode
  • Loading branch information
XuyangSong committed Dec 5, 2023
1 parent 9642c29 commit f19db1f
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
12 changes: 12 additions & 0 deletions taiga_halo2/src/circuit/vp_bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::circuit::vp_examples::TrivialValidityPredicateCircuit;
#[cfg(feature = "examples")]
use crate::circuit::vp_examples::{
or_relation_intent::OrRelationIntentValidityPredicateCircuit,
partial_fulfillment_intent::PartialFulfillmentIntentValidityPredicateCircuit,
receiver_vp::ReceiverValidityPredicateCircuit,
signature_verification::SignatureVerificationValidityPredicateCircuit,
Expand Down Expand Up @@ -42,6 +43,7 @@ pub enum ValidityPredicateRepresentation {
SignatureVerification,
Receiver,
PartialFulfillmentIntent,
OrRelationIntent,
// Add other native vp types here if needed
}

Expand Down Expand Up @@ -105,6 +107,11 @@ impl ValidityPredicateByteCode {
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())
}
#[allow(unreachable_patterns)]
_ => Err(TransactionError::InvalidValidityPredicateRepresentation),
}
Expand Down Expand Up @@ -155,6 +162,11 @@ impl ValidityPredicateByteCode {
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()?
}
#[allow(unreachable_patterns)]
_ => return Err(TransactionError::InvalidValidityPredicateRepresentation),
};
Expand Down
90 changes: 89 additions & 1 deletion taiga_halo2/src/circuit/vp_examples/or_relation_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
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,
Expand All @@ -25,13 +26,14 @@ use crate::{
vp_commitment::ValidityPredicateCommitment,
vp_vk::ValidityPredicateVerifyingKey,
};
use borsh::{BorshDeserialize, BorshSerialize};
use halo2_proofs::arithmetic::Field;
use halo2_proofs::{
circuit::{floor_planner, Layouter, Value},
plonk::{keygen_pk, keygen_vk, Circuit, ConstraintSystem, Error},
};
use lazy_static::lazy_static;
use pasta_curves::pallas;
use pasta_curves::{group::ff::PrimeField, pallas};
use rand::rngs::OsRng;
use rand::RngCore;

Expand Down Expand Up @@ -75,6 +77,21 @@ impl OrRelationIntentValidityPredicateCircuit {
receiver_value,
])
}

pub fn to_bytecode(&self) -> ValidityPredicateByteCode {
ValidityPredicateByteCode::new(
ValidityPredicateRepresentation::OrRelationIntent,
self.to_bytes(),
)
}

pub fn to_bytes(&self) -> Vec<u8> {
borsh::to_vec(&self).unwrap()
}

pub fn from_bytes(bytes: &Vec<u8>) -> Self {
BorshDeserialize::deserialize(&mut bytes.as_ref()).unwrap()
}
}

impl ValidityPredicateCircuit for OrRelationIntentValidityPredicateCircuit {
Expand Down Expand Up @@ -277,6 +294,70 @@ impl ValidityPredicateCircuit for OrRelationIntentValidityPredicateCircuit {
vp_circuit_impl!(OrRelationIntentValidityPredicateCircuit);
vp_verifying_info_impl!(OrRelationIntentValidityPredicateCircuit);

impl BorshSerialize for OrRelationIntentValidityPredicateCircuit {
fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_all(&self.owned_resource_id.to_repr())?;
for input in self.input_resources.iter() {
input.serialize(writer)?;
}

for output in self.output_resources.iter() {
output.serialize(writer)?;
}

self.token_1.serialize(writer)?;
self.token_2.serialize(writer)?;

writer.write_all(&self.receiver_npk.to_repr())?;
writer.write_all(&self.receiver_value.to_repr())?;

Ok(())
}
}

impl BorshDeserialize for OrRelationIntentValidityPredicateCircuit {
fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
let owned_resource_id_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let owned_resource_id = Option::from(pallas::Base::from_repr(owned_resource_id_bytes))
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
"owned_resource_id not in field",
)
})?;
let input_resources: Vec<_> = (0..NUM_RESOURCE)
.map(|_| Resource::deserialize_reader(reader))
.collect::<Result<_, _>>()?;
let output_resources: Vec<_> = (0..NUM_RESOURCE)
.map(|_| Resource::deserialize_reader(reader))
.collect::<Result<_, _>>()?;
let token_1 = Token::deserialize_reader(reader)?;
let token_2 = Token::deserialize_reader(reader)?;
let receiver_npk_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let receiver_npk =
Option::from(pallas::Base::from_repr(receiver_npk_bytes)).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "receiver_npk not in field")
})?;
let receiver_value_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let receiver_value = Option::from(pallas::Base::from_repr(receiver_value_bytes))
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
"receiver_value not in field",
)
})?;
Ok(Self {
owned_resource_id,
input_resources: input_resources.try_into().unwrap(),
output_resources: output_resources.try_into().unwrap(),
token_1,
token_2,
receiver_npk,
receiver_value,
})
}
}

pub fn create_intent_resource<R: RngCore>(
mut rng: R,
token_1: &Token,
Expand Down Expand Up @@ -346,6 +427,13 @@ fn test_halo2_or_relation_intent_vp_circuit() {
receiver_value: output_resources[0].value,
}
};

// Test serialization
let circuit = {
let circuit_bytes = circuit.to_bytes();
OrRelationIntentValidityPredicateCircuit::from_bytes(&circuit_bytes)
};

let public_inputs = circuit.get_public_inputs(&mut rng);

let prover = MockProver::<pallas::Base>::run(
Expand Down

0 comments on commit f19db1f

Please sign in to comment.