diff --git a/masp_note_encryption/Cargo.toml b/masp_note_encryption/Cargo.toml index ca734e89..1b4c075e 100644 --- a/masp_note_encryption/Cargo.toml +++ b/masp_note_encryption/Cargo.toml @@ -24,7 +24,7 @@ chacha20 = { version = "0.9", default-features = false } chacha20poly1305 = { version = "0.10", default-features = false } rand_core = { version = "0.6", default-features = false } subtle = { version = "2.3", default-features = false } -borsh = { version = "0.9", features = ["const-generics"] } +borsh = {version = "1.0.0-alpha.4", features = ["schema", "derive"]} [features] default = ["alloc"] diff --git a/masp_primitives/Cargo.toml b/masp_primitives/Cargo.toml index 93485b0f..babd4e09 100644 --- a/masp_primitives/Cargo.toml +++ b/masp_primitives/Cargo.toml @@ -73,7 +73,7 @@ blake2s_simd = "1" aes = "0.8" fpe = "0.6" -borsh = { version = "0.9", features = ["const-generics"] } +borsh = {version = "1.0.0-alpha.4", features = ["schema", "derive"]} [dependencies.masp_note_encryption] version = "0.2" path = "../masp_note_encryption" diff --git a/masp_primitives/src/asset_type.rs b/masp_primitives/src/asset_type.rs index 1492dabe..0a5981ba 100644 --- a/masp_primitives/src/asset_type.rs +++ b/masp_primitives/src/asset_type.rs @@ -17,7 +17,7 @@ use std::{ #[derive(Debug, BorshSerialize, BorshDeserialize, Clone, Copy, Eq)] pub struct AssetType { identifier: [u8; ASSET_IDENTIFIER_LENGTH], //32 byte asset type preimage - #[borsh_skip] + #[borsh(skip)] nonce: Option, } diff --git a/masp_primitives/src/convert.rs b/masp_primitives/src/convert.rs index 5a345cf5..d9fd728e 100644 --- a/masp_primitives/src/convert.rs +++ b/masp_primitives/src/convert.rs @@ -111,7 +111,7 @@ impl From for AllowedConversion { } impl BorshSerialize for AllowedConversion { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> io::Result<()> { self.assets.write(writer)?; writer.write_all(&self.generator.to_bytes())?; Ok(()) @@ -122,10 +122,10 @@ impl BorshDeserialize for AllowedConversion { /// This deserialization is unsafe because it does not do the expensive /// computation of checking whether the asset generator corresponds to the /// deserialized amount. - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - let assets = I32Sum::read(buf)?; + fn deserialize_reader(reader: &mut R) -> io::Result { + let assets = I32Sum::read(reader)?; let gen_bytes = - <::Repr as BorshDeserialize>::deserialize(buf)?; + <::Repr as BorshDeserialize>::deserialize_reader(reader)?; let generator = Option::from(jubjub::ExtendedPoint::from_bytes(&gen_bytes)) .ok_or_else(|| io::Error::from(io::ErrorKind::InvalidData))?; let allowed_conversion: AllowedConversion = assets.clone().into(); diff --git a/masp_primitives/src/merkle_tree.rs b/masp_primitives/src/merkle_tree.rs index e9a82e51..fbdfd247 100644 --- a/masp_primitives/src/merkle_tree.rs +++ b/masp_primitives/src/merkle_tree.rs @@ -255,14 +255,14 @@ impl FrozenCommitmentTree { } impl BorshSerialize for FrozenCommitmentTree { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> io::Result<()> { (&self.0, self.1).serialize(writer) } } impl BorshDeserialize for FrozenCommitmentTree { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - let tup: (Vec, usize) = BorshDeserialize::deserialize(buf)?; + fn deserialize_reader(reader: &mut R) -> io::Result { + let tup: (Vec, usize) = BorshDeserialize::deserialize_reader(reader)?; Ok(Self(tup.0, tup.1)) } } @@ -470,14 +470,14 @@ impl CommitmentTree { } impl BorshSerialize for CommitmentTree { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> io::Result<()> { self.write(writer) } } impl BorshDeserialize for CommitmentTree { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - Self::read(buf) + fn deserialize_reader(reader: &mut R) -> io::Result { + Self::read(reader) } } @@ -698,14 +698,14 @@ impl IncrementalWitness { } impl BorshSerialize for IncrementalWitness { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> io::Result<()> { self.write(writer) } } impl BorshDeserialize for IncrementalWitness { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - Self::read(buf) + fn deserialize_reader(reader: &mut R) -> io::Result { + Self::read(reader) } } @@ -761,17 +761,18 @@ impl MerklePath { } impl BorshDeserialize for MerklePath { - fn deserialize(witness: &mut &[u8]) -> Result { + fn deserialize_reader(witness: &mut R) -> Result { // Skip the first byte, which should be "depth" to signify the length of // the following vector of Pedersen hashes. - let depth = witness[0] as usize; - *witness = &witness[1..]; + let depth = witness.read_u8()? as usize; // Begin to construct the authentication path // Do not use any data in the witness after the expected depth - let iter = witness[..33 * depth + 8].chunks_exact(33); - // Update the witness to its final position - *witness = &witness[33 * depth + 8..]; + let mut iter = vec![]; + let _ = witness + .take((33 * depth + 8usize) as u64) + .read_to_end(&mut iter)?; + let iter = iter.chunks_exact(33); // Read the position from the witness let position = iter.remainder().read_u64::()?; diff --git a/masp_primitives/src/sapling.rs b/masp_primitives/src/sapling.rs index a7b244be..52d78191 100644 --- a/masp_primitives/src/sapling.rs +++ b/masp_primitives/src/sapling.rs @@ -301,14 +301,14 @@ impl ViewingKey { } impl BorshSerialize for ViewingKey { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> io::Result<()> { self.write(writer) } } impl BorshDeserialize for ViewingKey { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - Self::read(buf) + fn deserialize_reader(reader: &mut R) -> io::Result { + Self::read(reader) } } @@ -477,18 +477,16 @@ impl Hash for PaymentAddress { } impl BorshSerialize for PaymentAddress { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> io::Result<()> { writer.write(self.to_bytes().as_ref()).and(Ok(())) } } impl BorshDeserialize for PaymentAddress { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - let data = buf - .get(..43) - .ok_or_else(|| io::Error::from(io::ErrorKind::UnexpectedEof))?; - let res = Self::from_bytes(data.try_into().unwrap()); + fn deserialize_reader(reader: &mut R) -> io::Result { + let mut data = [0u8; 43]; + reader.read_exact(&mut data)?; + let res = Self::from_bytes(&data); let pa = res.ok_or_else(|| io::Error::from(io::ErrorKind::InvalidData))?; - *buf = &buf[43..]; Ok(pa) } } @@ -684,7 +682,7 @@ impl Note { } impl BorshSerialize for Note { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> io::Result<()> { // Write asset type self.asset_type.serialize(writer)?; // Write note value @@ -712,23 +710,23 @@ impl BorshSerialize for Note { } impl BorshDeserialize for Note { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { + fn deserialize_reader(reader: &mut R) -> io::Result { // Read asset type - let asset_type = AssetType::deserialize(buf)?; + let asset_type = AssetType::deserialize_reader(reader)?; // Read note value - let value = buf.read_u64::()?; + let value = reader.read_u64::()?; // Read diversified base - let g_d_bytes = <[u8; 32]>::deserialize(buf)?; + let g_d_bytes = <[u8; 32]>::deserialize_reader(reader)?; let g_d = Option::from(jubjub::SubgroupPoint::from_bytes(&g_d_bytes)) .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "g_d not in field"))?; // Read diversified transmission key - let pk_d_bytes = <[u8; 32]>::deserialize(buf)?; + let pk_d_bytes = <[u8; 32]>::deserialize_reader(reader)?; let pk_d = Option::from(jubjub::SubgroupPoint::from_bytes(&pk_d_bytes)) .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "pk_d not in field"))?; // Read note plaintext lead byte - let rseed_type = buf.read_u8()?; + let rseed_type = reader.read_u8()?; // Read rseed - let rseed_bytes = <[u8; 32]>::deserialize(buf)?; + let rseed_bytes = <[u8; 32]>::deserialize_reader(reader)?; let rseed = if rseed_type == 0x01 { let data = Option::from(jubjub::Fr::from_bytes(&rseed_bytes)) .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "rseed not in field"))?; @@ -820,7 +818,7 @@ mod tests { sapling::Note, transaction::components::amount::MAX_MONEY, }; - use borsh::{BorshDeserialize, BorshSerialize}; + use borsh::BorshDeserialize; use proptest::prelude::*; proptest! { @@ -828,7 +826,7 @@ mod tests { #[test] fn note_serialization(note in arb_positive_note_value(MAX_MONEY).prop_flat_map(arb_note)) { // BorshSerialize - let borsh = note.try_to_vec().unwrap(); + let borsh = borsh::to_vec(¬e).unwrap(); // BorshDeserialize let de_note: Note = BorshDeserialize::deserialize(&mut borsh.as_ref()).unwrap(); prop_assert_eq!(note, de_note); diff --git a/masp_primitives/src/sapling/redjubjub.rs b/masp_primitives/src/sapling/redjubjub.rs index 57c476b0..86659a44 100644 --- a/masp_primitives/src/sapling/redjubjub.rs +++ b/masp_primitives/src/sapling/redjubjub.rs @@ -58,25 +58,25 @@ impl Hash for PublicKey { } impl BorshDeserialize for PublicKey { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - Ok(Self(read_point(buf, "public key")?)) + fn deserialize_reader(reader: &mut R) -> io::Result { + Ok(Self(read_point(reader, "public key")?)) } } impl BorshSerialize for PublicKey { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> io::Result<()> { BorshSerialize::serialize(&self.0.to_bytes(), writer) } } impl BorshDeserialize for Signature { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - Self::read(buf) + fn deserialize_reader(reader: &mut R) -> io::Result { + Self::read(reader) } } impl BorshSerialize for Signature { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> io::Result<()> { self.write(writer) } } diff --git a/masp_primitives/src/transaction.rs b/masp_primitives/src/transaction.rs index 3aaa4e74..3f55ab9d 100644 --- a/masp_primitives/src/transaction.rs +++ b/masp_primitives/src/transaction.rs @@ -10,6 +10,7 @@ use blake2b_simd::Hash as Blake2bHash; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use ff::PrimeField; use memuse::DynamicUsage; +use std::collections::BTreeMap; use std::{ fmt::{self, Debug}, hash::Hash, @@ -283,23 +284,20 @@ impl TransactionData { } impl BorshSerialize for Transaction { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> io::Result<()> { self.write(writer) } } impl BorshDeserialize for Transaction { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - Self::read(buf, BranchId::MASP) + fn deserialize_reader(reader: &mut R) -> io::Result { + Self::read(reader, BranchId::MASP) } } impl borsh::BorshSchema for Transaction { fn add_definitions_recursively( - _definitions: &mut std::collections::HashMap< - borsh::schema::Declaration, - borsh::schema::Definition, - >, + _definitions: &mut BTreeMap, ) { } diff --git a/masp_primitives/src/transaction/builder.rs b/masp_primitives/src/transaction/builder.rs index fc65dcfd..f4583da6 100644 --- a/masp_primitives/src/transaction/builder.rs +++ b/masp_primitives/src/transaction/builder.rs @@ -116,14 +116,14 @@ impl Progress { /// Generates a [`Transaction`] from its inputs and outputs. #[derive(Clone, Debug, BorshSerialize, BorshDeserialize)] -pub struct Builder> { +pub struct Builder> { params: P, - rng: R, + rng: RN, target_height: BlockHeight, expiry_height: BlockHeight, transparent_builder: TransparentBuilder, sapling_builder: SaplingBuilder, - #[borsh_skip] + #[borsh(skip)] progress_notifier: Option, } diff --git a/masp_primitives/src/transaction/components/sapling.rs b/masp_primitives/src/transaction/components/sapling.rs index db9d104a..4b9c5873 100644 --- a/masp_primitives/src/transaction/components/sapling.rs +++ b/masp_primitives/src/transaction/components/sapling.rs @@ -85,7 +85,7 @@ impl MapAuth for () { } } -#[derive(Clone, Debug, BorshSerialize, BorshDeserialize, PartialEq)] +#[derive(Clone, Debug, PartialEq)] pub struct Bundle { pub shielded_spends: Vec>, pub shielded_converts: Vec>, diff --git a/masp_primitives/src/transaction/components/sapling/builder.rs b/masp_primitives/src/transaction/components/sapling/builder.rs index f29dd2e6..c68449f9 100644 --- a/masp_primitives/src/transaction/components/sapling/builder.rs +++ b/masp_primitives/src/transaction/components/sapling/builder.rs @@ -34,8 +34,8 @@ use crate::{ }, zip32::ExtendedSpendingKey, }; -use borsh::maybestd::io::Write; use borsh::{BorshDeserialize, BorshSerialize}; +use std::io::Write; /// If there are any shielded inputs, always have at least two shielded outputs, padding /// with dummy outputs if necessary. See . @@ -76,7 +76,7 @@ pub struct SpendDescriptionInfo { } impl BorshSerialize for SpendDescriptionInfo { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> std::io::Result<()> { self.extsk.serialize(writer)?; self.diversifier.serialize(writer)?; self.note.serialize(writer)?; @@ -86,13 +86,14 @@ impl BorshSerialize for SpendDescriptionInfo { } impl BorshDeserialize for SpendDescriptionInfo { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - let extsk = Key::deserialize(buf)?; - let diversifier = Diversifier::deserialize(buf)?; - let note = Note::deserialize(buf)?; - let alpha: Option<_> = jubjub::Fr::from_bytes(&<[u8; 32]>::deserialize(buf)?).into(); + fn deserialize_reader(reader: &mut R) -> std::io::Result { + let extsk = Key::deserialize_reader(reader)?; + let diversifier = Diversifier::deserialize_reader(reader)?; + let note = Note::deserialize_reader(reader)?; + let alpha: Option<_> = + jubjub::Fr::from_bytes(&<[u8; 32]>::deserialize_reader(reader)?).into(); let alpha = alpha.ok_or_else(|| std::io::Error::from(std::io::ErrorKind::InvalidData))?; - let merkle_path = MerklePath::::deserialize(buf)?; + let merkle_path = MerklePath::::deserialize_reader(reader)?; Ok(SpendDescriptionInfo { extsk, diversifier, @@ -280,7 +281,7 @@ pub struct SaplingBuilder { } impl BorshSerialize for SaplingBuilder { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> std::io::Result<()> { self.params.serialize(writer)?; self.spend_anchor.map(|x| x.to_bytes()).serialize(writer)?; self.target_height.serialize(writer)?; @@ -295,23 +296,23 @@ impl BorshSerialize for SaplingBuilder

BorshDeserialize for SaplingBuilder { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - let params = P::deserialize(buf)?; - let spend_anchor: Option> = - Option::<[u8; 32]>::deserialize(buf)?.map(|x| bls12_381::Scalar::from_bytes(&x).into()); + fn deserialize_reader(reader: &mut R) -> std::io::Result { + let params = P::deserialize_reader(reader)?; + let spend_anchor: Option> = Option::<[u8; 32]>::deserialize_reader(reader)? + .map(|x| bls12_381::Scalar::from_bytes(&x).into()); let spend_anchor = spend_anchor .map(|x| x.ok_or_else(|| std::io::Error::from(std::io::ErrorKind::InvalidData))) .transpose()?; - let target_height = BlockHeight::deserialize(buf)?; - let value_balance = I128Sum::deserialize(buf)?; - let convert_anchor: Option> = - Option::<[u8; 32]>::deserialize(buf)?.map(|x| bls12_381::Scalar::from_bytes(&x).into()); + let target_height = BlockHeight::deserialize_reader(reader)?; + let value_balance = I128Sum::deserialize_reader(reader)?; + let convert_anchor: Option> = Option::<[u8; 32]>::deserialize_reader(reader)? + .map(|x| bls12_381::Scalar::from_bytes(&x).into()); let convert_anchor = convert_anchor .map(|x| x.ok_or_else(|| std::io::Error::from(std::io::ErrorKind::InvalidData))) .transpose()?; - let spends = Vec::>::deserialize(buf)?; - let converts = Vec::::deserialize(buf)?; - let outputs = Vec::::deserialize(buf)?; + let spends = Vec::>::deserialize_reader(reader)?; + let converts = Vec::::deserialize_reader(reader)?; + let outputs = Vec::::deserialize_reader(reader)?; Ok(SaplingBuilder { params, spend_anchor, diff --git a/masp_primitives/src/transaction/components/transparent.rs b/masp_primitives/src/transaction/components/transparent.rs index c06c1012..96305ee6 100644 --- a/masp_primitives/src/transaction/components/transparent.rs +++ b/masp_primitives/src/transaction/components/transparent.rs @@ -172,13 +172,13 @@ impl TxOut { } impl BorshDeserialize for TxOut { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - Self::read(buf) + fn deserialize_reader(reader: &mut R) -> io::Result { + Self::read(reader) } } impl BorshSerialize for TxOut { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> io::Result<()> { self.write(writer) } } diff --git a/masp_primitives/src/zip32/sapling.rs b/masp_primitives/src/zip32/sapling.rs index 13ce9072..d8643c3f 100644 --- a/masp_primitives/src/zip32/sapling.rs +++ b/masp_primitives/src/zip32/sapling.rs @@ -516,13 +516,13 @@ impl std::fmt::Debug for ExtendedFullViewingKey { } impl BorshDeserialize for ExtendedSpendingKey { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - Self::read(buf) + fn deserialize_reader(reader: &mut R) -> io::Result { + Self::read(reader) } } impl BorshSerialize for ExtendedSpendingKey { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> io::Result<()> { self.write(writer) } } @@ -541,13 +541,13 @@ impl<'a> From<&'a ExtendedSpendingKey> for ExtendedFullViewingKey { } impl BorshDeserialize for ExtendedFullViewingKey { - fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - Self::read(buf) + fn deserialize_reader(reader: &mut R) -> io::Result { + Self::read(reader) } } impl BorshSerialize for ExtendedFullViewingKey { - fn serialize(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> { + fn serialize(&self, writer: &mut W) -> io::Result<()> { self.write(writer) } } @@ -560,12 +560,8 @@ impl PartialOrd for ExtendedFullViewingKey { impl Ord for ExtendedFullViewingKey { fn cmp(&self, other: &Self) -> Ordering { - let a = self - .try_to_vec() - .expect("unable to canonicalize ExtendedFullViewingKey"); - let b = other - .try_to_vec() - .expect("unable to canonicalize ExtendedFullViewingKey"); + let a = borsh::to_vec(self).expect("unable to canonicalize ExtendedFullViewingKey"); + let b = borsh::to_vec(other).expect("unable to canonicalize ExtendedFullViewingKey"); a.cmp(&b) } } @@ -878,12 +874,8 @@ impl PartialOrd for ExtendedSpendingKey { impl Ord for ExtendedSpendingKey { fn cmp(&self, other: &Self) -> Ordering { - let a = self - .try_to_vec() - .expect("unable to canonicalize ExtendedSpendingKey"); - let b = other - .try_to_vec() - .expect("unable to canonicalize ExtendedSpendingKey"); + let a = borsh::to_vec(self).expect("unable to canonicalize ExtendedSpendingKey"); + let b = borsh::to_vec(other).expect("unable to canonicalize ExtendedSpendingKey"); a.cmp(&b) } }