From 65126b504a0dad5f94608acdb64b3e543c23be30 Mon Sep 17 00:00:00 2001 From: bazzilic Date: Mon, 7 Aug 2023 16:02:13 +0800 Subject: [PATCH 1/4] some version bumps --- taiga_halo2/Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/taiga_halo2/Cargo.toml b/taiga_halo2/Cargo.toml index 6cb0e43c..3a41fd2b 100644 --- a/taiga_halo2/Cargo.toml +++ b/taiga_halo2/Cargo.toml @@ -17,13 +17,13 @@ subtle = { version = "2.3", default-features = false } dyn-clone = "1.0" reddsa = "0.5" vamp-ir = { git = "https://github.com/anoma/vamp-ir.git", rev = "6d401f8a479951727586ef0c44c42edab3139090"} -bincode = "2.0.0-rc.1" -borsh = {version = "0.9", features = ["const-generics"]} +bincode = "2.0.0-rc.3" +borsh = {version = "0.9.3", features = ["const-generics"]} byteorder = "1" num-bigint = "0.4.3" [dev-dependencies] -criterion = "0.3" +criterion = "0.5.1" proptest = "1.0.0" [[bench]] From 805a01e4171819ff86e5f2bdcd3cbf9c908199f5 Mon Sep 17 00:00:00 2001 From: bazzilic Date: Mon, 7 Aug 2023 16:47:57 +0800 Subject: [PATCH 2/4] borsh ver bump --- taiga_halo2/Cargo.toml | 2 +- taiga_halo2/src/action.rs | 24 ++++++++++ taiga_halo2/src/binding_signature.rs | 14 ++++++ taiga_halo2/src/circuit/vp_circuit.rs | 23 +++++++++ taiga_halo2/src/note.rs | 69 +++++++++++++++++++++++++++ taiga_halo2/src/shielded_ptx.rs | 17 +++++++ 6 files changed, 148 insertions(+), 1 deletion(-) diff --git a/taiga_halo2/Cargo.toml b/taiga_halo2/Cargo.toml index 3a41fd2b..714d195e 100644 --- a/taiga_halo2/Cargo.toml +++ b/taiga_halo2/Cargo.toml @@ -18,7 +18,7 @@ dyn-clone = "1.0" reddsa = "0.5" vamp-ir = { git = "https://github.com/anoma/vamp-ir.git", rev = "6d401f8a479951727586ef0c44c42edab3139090"} bincode = "2.0.0-rc.3" -borsh = {version = "0.9.3", features = ["const-generics"]} +borsh = {version = "0.10.3", features = ["const-generics"]} byteorder = "1" num-bigint = "0.4.3" diff --git a/taiga_halo2/src/action.rs b/taiga_halo2/src/action.rs index 5684df3b..2ac4b557 100644 --- a/taiga_halo2/src/action.rs +++ b/taiga_halo2/src/action.rs @@ -78,6 +78,30 @@ impl BorshDeserialize for ActionInstance { cv_net, }) } + + fn deserialize_reader(reader: &mut R) -> io::Result { + let anchor_bytes = <[u8; 32]>::deserialize_reader(reader)?; + let anchor = Option::from(pallas::Base::from_repr(anchor_bytes)) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "anchor not in field"))?; + let nf_bytes = <[u8; 32]>::deserialize_reader(reader)?; + let nf = Option::from(Nullifier::from_bytes(nf_bytes)) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "nf not in field"))?; + let cm_x_bytes = <[u8; 32]>::deserialize_reader(reader)?; + let cm_x = Option::from(pallas::Base::from_repr(cm_x_bytes)) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "cm_x not in field"))?; + let cv_net_bytes = <[u8; 32]>::deserialize_reader(reader)?; + let cv_net = Option::from(ValueCommitment::from_bytes(cv_net_bytes)) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "cv_net not in field"))?; + + Ok(ActionInstance { + anchor, + nf, + cm_x, + cv_net, + }) + } + + } impl ActionInfo { diff --git a/taiga_halo2/src/binding_signature.rs b/taiga_halo2/src/binding_signature.rs index afd275ad..ca493d32 100644 --- a/taiga_halo2/src/binding_signature.rs +++ b/taiga_halo2/src/binding_signature.rs @@ -59,6 +59,12 @@ impl BorshDeserialize for BindingSignature { let sig_bytes = <[u8; 64]>::deserialize(buf)?; Ok(Self::from_bytes(sig_bytes)) } + + fn deserialize_reader(reader: &mut R) -> io::Result { + let mut sig_bytes = [0u8; 64]; + reader.read_exact(&mut sig_bytes)?; + Ok(Self::from_bytes(sig_bytes)) + } } impl BindingSigningKey { @@ -93,6 +99,14 @@ impl BorshDeserialize for BindingSigningKey { io::Error::new(io::ErrorKind::InvalidData, "BindingSigningKey not in field") }) } + + fn deserialize_reader(reader: &mut R) -> io::Result { + let mut key_bytes = [0u8; 32]; + reader.read_exact(&mut key_bytes)?; + Self::from_bytes(key_bytes).map_err(|_| { + io::Error::new(io::ErrorKind::InvalidData, "BindingSigningKey not in field") + }) + } } impl From for BindingSigningKey { diff --git a/taiga_halo2/src/circuit/vp_circuit.rs b/taiga_halo2/src/circuit/vp_circuit.rs index 17055bdd..12fbbaa6 100644 --- a/taiga_halo2/src/circuit/vp_circuit.rs +++ b/taiga_halo2/src/circuit/vp_circuit.rs @@ -134,6 +134,29 @@ impl BorshDeserialize for VPVerifyingInfo { public_inputs: public_inputs.into(), }) } + + fn deserialize_reader(reader: &mut R) -> io::Result { + // Read vk + use crate::circuit::vp_examples::TrivialValidityPredicateCircuit; + let params = SETUP_PARAMS_MAP.get(&VP_CIRCUIT_PARAMS_SIZE).unwrap(); + let vk = VerifyingKey::read::<_, TrivialValidityPredicateCircuit>(reader, params)?; + // Read proof + 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") + }) + }) + .collect::>()?; + Ok(VPVerifyingInfo { + vk, + proof, + public_inputs: public_inputs.into(), + }) + } } impl ValidityPredicatePublicInputs { diff --git a/taiga_halo2/src/note.rs b/taiga_halo2/src/note.rs index ee84e294..ea0cebb9 100644 --- a/taiga_halo2/src/note.rs +++ b/taiga_halo2/src/note.rs @@ -377,6 +377,75 @@ impl BorshDeserialize for Note { rcm, )) } + + fn deserialize_reader(reader: &mut R) -> io::Result { + // Read app_vk + let mut app_vk_bytes = [0u8; 32]; + reader.read_exact(&mut app_vk_bytes)?; + let app_vk = Option::from(pallas::Base::from_repr(app_vk_bytes)) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "app_vk not in field"))?; + // Read app_data_static + let mut app_data_static_bytes = [0u8; 32]; + reader.read_exact(&mut app_data_static_bytes)?; + let app_data_static = Option::from(pallas::Base::from_repr(app_data_static_bytes)) + .ok_or_else(|| { + io::Error::new(io::ErrorKind::InvalidData, "app_data_static not in field") + })?; + // Read app_data_dynamic + let mut app_data_dynamic_bytes = [0u8; 32]; + reader.read_exact(&mut app_data_dynamic_bytes)?; + let app_data_dynamic = Option::from(pallas::Base::from_repr(app_data_dynamic_bytes)) + .ok_or_else(|| { + io::Error::new(io::ErrorKind::InvalidData, "app_data_dynamic not in field") + })?; + // Read note value + let value = reader.read_u64::()?; + // Read nk_container + let mut nk_container_type = [0u8; 1]; + reader.read_exact(&mut nk_container_type)?; + let nk_container_type = nk_container_type[0]; + let mut nk_container_bytes = [0u8; 32]; + reader.read_exact(&mut nk_container_bytes)?; + let nk = Option::from(pallas::Base::from_repr(nk_container_bytes)) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "nk not in field"))?; + let nk_container = if nk_container_type == 0x01 { + NullifierKeyContainer::from_commitment(nk) + } else { + NullifierKeyContainer::from_key(nk) + }; + // Read rho + let mut rho_bytes = [0u8; 32]; + reader.read_exact(&mut rho_bytes)?; + let rho = Option::from(Nullifier::from_bytes(rho_bytes)) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "rho not in field"))?; + // Read psi + let mut psi_bytes = [0u8; 32]; + reader.read_exact(&mut psi_bytes)?; + let psi = Option::from(pallas::Base::from_repr(psi_bytes)) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "psi not in field"))?; + // Read rcm + let mut rcm_bytes = [0u8; 32]; + reader.read_exact(&mut rcm_bytes)?; + let rcm = Option::from(pallas::Base::from_repr(rcm_bytes)) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "rcm not in field"))?; + // Read is_merkle_checked + let mut is_merkle_checked_byte = [0u8; 1]; + reader.read_exact(&mut is_merkle_checked_byte)?; + let is_merkle_checked_byte = is_merkle_checked_byte[0]; + let is_merkle_checked = is_merkle_checked_byte == 0x01; + // Construct note + Ok(Note::from_full( + app_vk, + app_data_static, + app_data_dynamic, + value, + nk_container, + rho, + is_merkle_checked, + psi, + rcm, + )) + } } impl NoteType { diff --git a/taiga_halo2/src/shielded_ptx.rs b/taiga_halo2/src/shielded_ptx.rs index 22507be0..678f5752 100644 --- a/taiga_halo2/src/shielded_ptx.rs +++ b/taiga_halo2/src/shielded_ptx.rs @@ -237,6 +237,23 @@ impl BorshDeserialize for ShieldedPartialTransaction { outputs: outputs.try_into().unwrap(), }) } + + fn deserialize_reader(reader: &mut R) -> std::io::Result { + let actions: Vec<_> = (0..NUM_NOTE) + .map(|_| ActionVerifyingInfo::deserialize_reader(reader)) + .collect::>()?; + let inputs: Vec<_> = (0..NUM_NOTE) + .map(|_| NoteVPVerifyingInfoSet::deserialize_reader(reader)) + .collect::>()?; + let outputs: Vec<_> = (0..NUM_NOTE) + .map(|_| NoteVPVerifyingInfoSet::deserialize_reader(reader)) + .collect::>()?; + Ok(ShieldedPartialTransaction { + actions: actions.try_into().unwrap(), + inputs: inputs.try_into().unwrap(), + outputs: outputs.try_into().unwrap(), + }) + } } impl ActionVerifyingInfo { pub fn create(action_info: ActionInfo, mut rng: R) -> Result { From d50df907ae5f224fe97a359a1272cc86fca9fef5 Mon Sep 17 00:00:00 2001 From: bazzilic Date: Mon, 7 Aug 2023 16:57:09 +0800 Subject: [PATCH 3/4] fmt --- taiga_halo2/src/action.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/taiga_halo2/src/action.rs b/taiga_halo2/src/action.rs index 2ac4b557..8f25deac 100644 --- a/taiga_halo2/src/action.rs +++ b/taiga_halo2/src/action.rs @@ -100,8 +100,6 @@ impl BorshDeserialize for ActionInstance { cv_net, }) } - - } impl ActionInfo { From 54a78197027c63ec15da0cf9be64bc38860eda40 Mon Sep 17 00:00:00 2001 From: bazzilic Date: Mon, 7 Aug 2023 18:29:11 +0800 Subject: [PATCH 4/4] remove deserialize --- taiga_halo2/src/action.rs | 22 ---------- taiga_halo2/src/binding_signature.rs | 12 ------ taiga_halo2/src/circuit/vp_circuit.rs | 23 ----------- taiga_halo2/src/note.rs | 58 --------------------------- taiga_halo2/src/shielded_ptx.rs | 17 -------- 5 files changed, 132 deletions(-) diff --git a/taiga_halo2/src/action.rs b/taiga_halo2/src/action.rs index 8f25deac..3efdb029 100644 --- a/taiga_halo2/src/action.rs +++ b/taiga_halo2/src/action.rs @@ -57,28 +57,6 @@ impl BorshSerialize for ActionInstance { } impl BorshDeserialize for ActionInstance { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - let anchor_bytes = <[u8; 32]>::deserialize(buf)?; - let anchor = Option::from(pallas::Base::from_repr(anchor_bytes)) - .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "anchor not in field"))?; - let nf_bytes = <[u8; 32]>::deserialize(buf)?; - let nf = Option::from(Nullifier::from_bytes(nf_bytes)) - .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "nf not in field"))?; - let cm_x_bytes = <[u8; 32]>::deserialize(buf)?; - let cm_x = Option::from(pallas::Base::from_repr(cm_x_bytes)) - .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "cm_x not in field"))?; - let cv_net_bytes = <[u8; 32]>::deserialize(buf)?; - let cv_net = Option::from(ValueCommitment::from_bytes(cv_net_bytes)) - .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "cv_net not in field"))?; - - Ok(ActionInstance { - anchor, - nf, - cm_x, - cv_net, - }) - } - fn deserialize_reader(reader: &mut R) -> io::Result { let anchor_bytes = <[u8; 32]>::deserialize_reader(reader)?; let anchor = Option::from(pallas::Base::from_repr(anchor_bytes)) diff --git a/taiga_halo2/src/binding_signature.rs b/taiga_halo2/src/binding_signature.rs index ca493d32..5dcf5af3 100644 --- a/taiga_halo2/src/binding_signature.rs +++ b/taiga_halo2/src/binding_signature.rs @@ -55,11 +55,6 @@ impl BorshSerialize for BindingSignature { } impl BorshDeserialize for BindingSignature { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - let sig_bytes = <[u8; 64]>::deserialize(buf)?; - Ok(Self::from_bytes(sig_bytes)) - } - fn deserialize_reader(reader: &mut R) -> io::Result { let mut sig_bytes = [0u8; 64]; reader.read_exact(&mut sig_bytes)?; @@ -93,13 +88,6 @@ impl BorshSerialize for BindingSigningKey { } impl BorshDeserialize for BindingSigningKey { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - let key_bytes = <[u8; 32]>::deserialize(buf)?; - Self::from_bytes(key_bytes).map_err(|_| { - io::Error::new(io::ErrorKind::InvalidData, "BindingSigningKey not in field") - }) - } - fn deserialize_reader(reader: &mut R) -> io::Result { let mut key_bytes = [0u8; 32]; reader.read_exact(&mut key_bytes)?; diff --git a/taiga_halo2/src/circuit/vp_circuit.rs b/taiga_halo2/src/circuit/vp_circuit.rs index 12fbbaa6..341e0a4f 100644 --- a/taiga_halo2/src/circuit/vp_circuit.rs +++ b/taiga_halo2/src/circuit/vp_circuit.rs @@ -112,29 +112,6 @@ impl BorshSerialize for VPVerifyingInfo { } impl BorshDeserialize for VPVerifyingInfo { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - // Read vk - use crate::circuit::vp_examples::TrivialValidityPredicateCircuit; - let params = SETUP_PARAMS_MAP.get(&VP_CIRCUIT_PARAMS_SIZE).unwrap(); - let vk = VerifyingKey::read::<_, TrivialValidityPredicateCircuit>(buf, params)?; - // Read proof - let proof = Proof::deserialize(buf)?; - // Read public inputs - let public_inputs: Vec<_> = (0..VP_CIRCUIT_PUBLIC_INPUT_NUM) - .map(|_| { - let bytes = <[u8; 32]>::deserialize(buf)?; - Option::from(pallas::Base::from_repr(bytes)).ok_or_else(|| { - io::Error::new(io::ErrorKind::InvalidData, "public input not in field") - }) - }) - .collect::>()?; - Ok(VPVerifyingInfo { - vk, - proof, - public_inputs: public_inputs.into(), - }) - } - fn deserialize_reader(reader: &mut R) -> io::Result { // Read vk use crate::circuit::vp_examples::TrivialValidityPredicateCircuit; diff --git a/taiga_halo2/src/note.rs b/taiga_halo2/src/note.rs index ea0cebb9..7d8438ac 100644 --- a/taiga_halo2/src/note.rs +++ b/taiga_halo2/src/note.rs @@ -320,64 +320,6 @@ impl BorshSerialize for Note { } impl BorshDeserialize for Note { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - // Read app_vk - let app_vk_bytes = <[u8; 32]>::deserialize(buf)?; - let app_vk = Option::from(pallas::Base::from_repr(app_vk_bytes)) - .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "app_vk not in field"))?; - // Read app_data_static - let app_data_static_bytes = <[u8; 32]>::deserialize(buf)?; - let app_data_static = Option::from(pallas::Base::from_repr(app_data_static_bytes)) - .ok_or_else(|| { - io::Error::new(io::ErrorKind::InvalidData, "app_data_static not in field") - })?; - // Read app_data_dynamic - let app_data_dynamic_bytes = <[u8; 32]>::deserialize(buf)?; - let app_data_dynamic = Option::from(pallas::Base::from_repr(app_data_dynamic_bytes)) - .ok_or_else(|| { - io::Error::new(io::ErrorKind::InvalidData, "app_data_dynamic not in field") - })?; - // Read note value - let value = buf.read_u64::()?; - // Read nk_container - let nk_container_type = buf.read_u8()?; - let nk_container_bytes = <[u8; 32]>::deserialize(buf)?; - let nk = Option::from(pallas::Base::from_repr(nk_container_bytes)) - .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "nk not in field"))?; - let nk_container = if nk_container_type == 0x01 { - NullifierKeyContainer::from_commitment(nk) - } else { - NullifierKeyContainer::from_key(nk) - }; - // Read rho - let rho_bytes = <[u8; 32]>::deserialize(buf)?; - let rho = Option::from(Nullifier::from_bytes(rho_bytes)) - .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "rho not in field"))?; - // Read psi - let psi_bytes = <[u8; 32]>::deserialize(buf)?; - let psi = Option::from(pallas::Base::from_repr(psi_bytes)) - .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "psi not in field"))?; - // Read rcm - let rcm_bytes = <[u8; 32]>::deserialize(buf)?; - let rcm = Option::from(pallas::Base::from_repr(rcm_bytes)) - .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "rcm not in field"))?; - // Read is_merkle_checked - let is_merkle_checked_byte = buf.read_u8()?; - let is_merkle_checked = is_merkle_checked_byte == 0x01; - // Construct note - Ok(Note::from_full( - app_vk, - app_data_static, - app_data_dynamic, - value, - nk_container, - rho, - is_merkle_checked, - psi, - rcm, - )) - } - fn deserialize_reader(reader: &mut R) -> io::Result { // Read app_vk let mut app_vk_bytes = [0u8; 32]; diff --git a/taiga_halo2/src/shielded_ptx.rs b/taiga_halo2/src/shielded_ptx.rs index 678f5752..d6b21bb8 100644 --- a/taiga_halo2/src/shielded_ptx.rs +++ b/taiga_halo2/src/shielded_ptx.rs @@ -221,23 +221,6 @@ impl BorshSerialize for ShieldedPartialTransaction { } impl BorshDeserialize for ShieldedPartialTransaction { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - let actions: Vec<_> = (0..NUM_NOTE) - .map(|_| ActionVerifyingInfo::deserialize(buf)) - .collect::>()?; - let inputs: Vec<_> = (0..NUM_NOTE) - .map(|_| NoteVPVerifyingInfoSet::deserialize(buf)) - .collect::>()?; - let outputs: Vec<_> = (0..NUM_NOTE) - .map(|_| NoteVPVerifyingInfoSet::deserialize(buf)) - .collect::>()?; - Ok(ShieldedPartialTransaction { - actions: actions.try_into().unwrap(), - inputs: inputs.try_into().unwrap(), - outputs: outputs.try_into().unwrap(), - }) - } - fn deserialize_reader(reader: &mut R) -> std::io::Result { let actions: Vec<_> = (0..NUM_NOTE) .map(|_| ActionVerifyingInfo::deserialize_reader(reader))