Skip to content

Commit

Permalink
make point and field reading easy
Browse files Browse the repository at this point in the history
  • Loading branch information
XuyangSong committed Dec 6, 2023
1 parent 43357b6 commit acd68fc
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 217 deletions.
11 changes: 2 additions & 9 deletions taiga_halo2/src/circuit/vp_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
proof::Proof,
resource::{RandomSeed, Resource, ResourceCommitment},
resource_encryption::{ResourceCiphertext, SecretKey},
utils::mod_r_p,
utils::{mod_r_p, read_base_field},

Check warning on line 34 in taiga_halo2/src/circuit/vp_circuit.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

unused import: `read_base_field`
vp_vk::ValidityPredicateVerifyingKey,
};
use dyn_clone::{clone_trait_object, DynClone};
Expand Down Expand Up @@ -204,8 +204,6 @@ impl BorshSerialize for VPVerifyingInfo {
#[cfg(feature = "borsh")]
impl BorshDeserialize for VPVerifyingInfo {
fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
use ff::PrimeField;
use std::io;
// Read vk
use crate::circuit::vp_examples::TrivialValidityPredicateCircuit;
let params = SETUP_PARAMS_MAP.get(&VP_CIRCUIT_PARAMS_SIZE).unwrap();
Expand All @@ -214,12 +212,7 @@ impl BorshDeserialize for VPVerifyingInfo {
let proof = Proof::deserialize_reader(reader)?;
// Read public inputs
let public_inputs: Vec<_> = (0..VP_CIRCUIT_PUBLIC_INPUT_NUM)
.map(|_| {
let bytes = <[u8; 32]>::deserialize_reader(reader)?;
Option::from(pallas::Base::from_repr(bytes)).ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidData, "public input not in field")
})
})
.map(|_| read_base_field(reader))
.collect::<Result<_, _>>()?;
Ok(VPVerifyingInfo {
vk,
Expand Down
11 changes: 2 additions & 9 deletions taiga_halo2/src/circuit/vp_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
error::TransactionError,
proof::Proof,
resource::{RandomSeed, Resource},
utils::read_base_field,

Check warning on line 12 in taiga_halo2/src/circuit/vp_examples.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

unused import: `utils::read_base_field`
vp_commitment::ValidityPredicateCommitment,
vp_vk::ValidityPredicateVerifyingKey,
};
Expand Down Expand Up @@ -133,15 +134,7 @@ impl BorshSerialize for TrivialValidityPredicateCircuit {
#[cfg(feature = "borsh")]
impl BorshDeserialize for TrivialValidityPredicateCircuit {
fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
use ff::PrimeField;
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 owned_resource_id = read_base_field(reader)?;
let input_resources: Vec<_> = (0..NUM_RESOURCE)
.map(|_| Resource::deserialize_reader(reader))
.collect::<Result<_, _>>()?;
Expand Down
19 changes: 3 additions & 16 deletions taiga_halo2/src/circuit/vp_examples/cascade_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{
nullifier::Nullifier,
proof::Proof,
resource::{RandomSeed, Resource},
utils::read_base_field,
vp_commitment::ValidityPredicateCommitment,
vp_vk::ValidityPredicateVerifyingKey,
};
Expand Down Expand Up @@ -184,28 +185,14 @@ impl BorshSerialize for CascadeIntentValidityPredicateCircuit {

impl BorshDeserialize for CascadeIntentValidityPredicateCircuit {
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 owned_resource_id = read_base_field(reader)?;
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 cascade_resource_cm_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let cascade_resource_cm = Option::from(pallas::Base::from_repr(cascade_resource_cm_bytes))
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
"cascade_resource_cm not in field",
)
})?;
let cascade_resource_cm = read_base_field(reader)?;
Ok(Self {
owned_resource_id,
input_resources: input_resources.try_into().unwrap(),
Expand Down
25 changes: 4 additions & 21 deletions taiga_halo2/src/circuit/vp_examples/or_relation_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
proof::Proof,
resource::{RandomSeed, Resource},
utils::poseidon_hash_n,
utils::read_base_field,
vp_commitment::ValidityPredicateCommitment,
vp_vk::ValidityPredicateVerifyingKey,
};
Expand Down Expand Up @@ -317,14 +318,7 @@ impl BorshSerialize for OrRelationIntentValidityPredicateCircuit {

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 owned_resource_id = read_base_field(reader)?;
let input_resources: Vec<_> = (0..NUM_RESOURCE)
.map(|_| Resource::deserialize_reader(reader))
.collect::<Result<_, _>>()?;
Expand All @@ -333,19 +327,8 @@ impl BorshDeserialize for OrRelationIntentValidityPredicateCircuit {
.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",
)
})?;
let receiver_npk = read_base_field(reader)?;
let receiver_value = read_base_field(reader)?;
Ok(Self {
owned_resource_id,
input_resources: input_resources.try_into().unwrap(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
error::TransactionError,
proof::Proof,
resource::{RandomSeed, Resource},
utils::read_base_field,
vp_commitment::ValidityPredicateCommitment,
vp_vk::ValidityPredicateVerifyingKey,
};
Expand Down Expand Up @@ -216,14 +217,7 @@ impl BorshSerialize for PartialFulfillmentIntentValidityPredicateCircuit {

impl BorshDeserialize for PartialFulfillmentIntentValidityPredicateCircuit {
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 owned_resource_id = read_base_field(reader)?;
let input_resources: Vec<_> = (0..NUM_RESOURCE)
.map(|_| Resource::deserialize_reader(reader))
.collect::<Result<_, _>>()?;
Expand Down
42 changes: 7 additions & 35 deletions taiga_halo2/src/circuit/vp_examples/receiver_vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
proof::Proof,
resource::{RandomSeed, Resource},
resource_encryption::{ResourceCiphertext, ResourcePlaintext, SecretKey},
utils::mod_r_p,
utils::{mod_r_p, read_base_field, read_point},
vp_commitment::ValidityPredicateCommitment,
vp_vk::ValidityPredicateVerifyingKey,
};
Expand Down Expand Up @@ -321,46 +321,18 @@ impl BorshSerialize for ReceiverValidityPredicateCircuit {

impl BorshDeserialize for ReceiverValidityPredicateCircuit {
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 owned_resource_id = read_base_field(reader)?;
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 vp_vk_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let vp_vk = Option::from(pallas::Base::from_repr(vp_vk_bytes)).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "vp_vk not in field")
})?;
let encrypt_nonce_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let encrypt_nonce =
Option::from(pallas::Base::from_repr(encrypt_nonce_bytes)).ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
"encrypt_nonce not in field",
)
})?;
let sk_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let sk = Option::from(pallas::Base::from_repr(sk_bytes)).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "sk not in field")
})?;
let rcv_pk_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let rcv_pk = Option::from(pallas::Point::from_bytes(&rcv_pk_bytes)).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "rcv_pk not in point")
})?;

let auth_vp_vk_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let auth_vp_vk =
Option::from(pallas::Base::from_repr(auth_vp_vk_bytes)).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "auth_vp_vk not in field")
})?;
let vp_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)?;
Ok(Self {
owned_resource_id,
input_resources: input_resources.try_into().unwrap(),
Expand Down
40 changes: 7 additions & 33 deletions taiga_halo2/src/circuit/vp_examples/signature_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
error::TransactionError,
proof::Proof,
resource::{RandomSeed, Resource},
utils::{mod_r_p, poseidon_hash_n},
utils::{mod_r_p, poseidon_hash_n, read_base_field, read_point, read_scalar_field},
vp_commitment::ValidityPredicateCommitment,
vp_vk::ValidityPredicateVerifyingKey,
};
Expand Down Expand Up @@ -326,33 +326,16 @@ impl BorshSerialize for SignatureVerificationValidityPredicateCircuit {

impl BorshDeserialize for SignatureVerificationValidityPredicateCircuit {
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 owned_resource_id = read_base_field(reader)?;
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 vp_vk_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let vp_vk = Option::from(pallas::Base::from_repr(vp_vk_bytes)).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "vp_vk not in field")
})?;
let vp_vk = read_base_field(reader)?;
let signature = SchnorrSignature::deserialize_reader(reader)?;
let receiver_vp_vk_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let receiver_vp_vk = Option::from(pallas::Base::from_repr(receiver_vp_vk_bytes))
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
"receiver_vp_vk not in field",
)
})?;
let receiver_vp_vk = read_base_field(reader)?;
Ok(Self {
owned_resource_id,
input_resources: input_resources.try_into().unwrap(),
Expand All @@ -376,18 +359,9 @@ impl BorshSerialize for SchnorrSignature {

impl BorshDeserialize for SchnorrSignature {
fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
let pk_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let pk = Option::from(pallas::Point::from_bytes(&pk_bytes)).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "pk not in point")
})?;
let r_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let r = Option::from(pallas::Point::from_bytes(&r_bytes)).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "r not in point")
})?;
let s_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let s = Option::from(pallas::Scalar::from_repr(s_bytes)).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "s not in field")
})?;
let pk = read_point(reader)?;
let r = read_point(reader)?;
let s = read_scalar_field(reader)?;
Ok(Self { pk, r, s })
}
}
Expand Down
33 changes: 5 additions & 28 deletions taiga_halo2/src/circuit/vp_examples/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
nullifier::Nullifier,
proof::Proof,
resource::{RandomSeed, Resource, ResourceValidityPredicates},
utils::poseidon_hash_n,
utils::{poseidon_hash_n, read_base_field, read_point},
vp_commitment::ValidityPredicateCommitment,
vp_vk::ValidityPredicateVerifyingKey,
};
Expand Down Expand Up @@ -547,14 +547,7 @@ impl BorshSerialize for TokenValidityPredicateCircuit {

impl BorshDeserialize for TokenValidityPredicateCircuit {
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 owned_resource_id = read_base_field(reader)?;
let input_resources: Vec<_> = (0..NUM_RESOURCE)
.map(|_| Resource::deserialize_reader(reader))
.collect::<Result<_, _>>()?;
Expand All @@ -563,14 +556,7 @@ impl BorshDeserialize for TokenValidityPredicateCircuit {
.collect::<Result<_, _>>()?;
let token_name = TokenName::deserialize_reader(reader)?;
let auth = TokenAuthorization::deserialize_reader(reader)?;
let receiver_vp_vk_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let receiver_vp_vk = Option::from(pallas::Base::from_repr(receiver_vp_vk_bytes))
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
"receiver_vp_vk not in field",
)
})?;
let receiver_vp_vk = read_base_field(reader)?;
let rseed = RandomSeed::deserialize_reader(reader)?;
Ok(Self {
owned_resource_id,
Expand All @@ -594,17 +580,8 @@ impl BorshSerialize for TokenAuthorization {

impl BorshDeserialize for TokenAuthorization {
fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
let pk_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let pk = Option::from(pallas::Point::from_bytes(&pk_bytes)).ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
"owned_resource_id not in point",
)
})?;
let vk_bytes = <[u8; 32]>::deserialize_reader(reader)?;
let vk = Option::from(pallas::Base::from_repr(vk_bytes)).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "vk not in field")
})?;
let pk = read_point(reader)?;
let vk = read_base_field(reader)?;

Ok(Self { pk, vk })
}
Expand Down
14 changes: 3 additions & 11 deletions taiga_halo2/src/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::hash::{Hash, Hasher};

use crate::merkle_tree::LR::{L, R};
use crate::resource::ResourceCommitment;
use crate::utils::poseidon_hash;
use crate::utils::{poseidon_hash, read_base_field};

Check warning on line 5 in taiga_halo2/src/merkle_tree.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

unused import: `read_base_field`
use crate::{constant::TAIGA_COMMITMENT_TREE_DEPTH, resource::Resource};
use ff::PrimeField;
use halo2_proofs::arithmetic::Field;
Expand Down Expand Up @@ -68,11 +68,7 @@ impl BorshSerialize for Anchor {
#[cfg(feature = "borsh")]
impl BorshDeserialize for Anchor {
fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
let mut repr = [0u8; 32];
reader.read_exact(&mut repr)?;
let value = Option::from(pallas::Base::from_repr(repr)).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "Anchor not in field")
})?;
let value = read_base_field(reader)?;
Ok(Self(value))
}
}
Expand Down Expand Up @@ -208,11 +204,7 @@ impl BorshSerialize for Node {
#[cfg(feature = "borsh")]
impl BorshDeserialize for Node {
fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
let mut repr = [0u8; 32];
reader.read_exact(&mut repr)?;
let value = Option::from(pallas::Base::from_repr(repr)).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "Node value not in field")
})?;
let value = read_base_field(reader)?;
Ok(Self(value))
}
}
Expand Down
Loading

0 comments on commit acd68fc

Please sign in to comment.