Skip to content

Commit

Permalink
Merge pull request #64 from anoma/mariari/bump-borsh
Browse files Browse the repository at this point in the history
  • Loading branch information
joe authored Sep 21, 2023
2 parents 22288a9 + dedaf89 commit f4f1ce1
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 100 deletions.
2 changes: 1 addition & 1 deletion masp_note_encryption/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion masp_primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion masp_primitives/src/asset_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
}

Expand Down
8 changes: 4 additions & 4 deletions masp_primitives/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl From<I32Sum> for AllowedConversion {
}

impl BorshSerialize for AllowedConversion {
fn serialize<W: Write>(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> {
fn serialize<W: Write>(&self, writer: &mut W) -> io::Result<()> {
self.assets.write(writer)?;
writer.write_all(&self.generator.to_bytes())?;
Ok(())
Expand All @@ -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<Self> {
let assets = I32Sum::read(buf)?;
fn deserialize_reader<R: io::Read>(reader: &mut R) -> io::Result<Self> {
let assets = I32Sum::read(reader)?;
let gen_bytes =
<<jubjub::ExtendedPoint as GroupEncoding>::Repr as BorshDeserialize>::deserialize(buf)?;
<<jubjub::ExtendedPoint as GroupEncoding>::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();
Expand Down
31 changes: 16 additions & 15 deletions masp_primitives/src/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,14 @@ impl<Node: Hashable> FrozenCommitmentTree<Node> {
}

impl<Node: BorshSerialize> BorshSerialize for FrozenCommitmentTree<Node> {
fn serialize<W: Write>(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> {
fn serialize<W: Write>(&self, writer: &mut W) -> io::Result<()> {
(&self.0, self.1).serialize(writer)
}
}

impl<Node: BorshDeserialize> BorshDeserialize for FrozenCommitmentTree<Node> {
fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result<Self> {
let tup: (Vec<Node>, usize) = BorshDeserialize::deserialize(buf)?;
fn deserialize_reader<R: Read>(reader: &mut R) -> io::Result<Self> {
let tup: (Vec<Node>, usize) = BorshDeserialize::deserialize_reader(reader)?;
Ok(Self(tup.0, tup.1))
}
}
Expand Down Expand Up @@ -470,14 +470,14 @@ impl<Node: Hashable> CommitmentTree<Node> {
}

impl<Node: Hashable> BorshSerialize for CommitmentTree<Node> {
fn serialize<W: Write>(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> {
fn serialize<W: Write>(&self, writer: &mut W) -> io::Result<()> {
self.write(writer)
}
}

impl<Node: Hashable> BorshDeserialize for CommitmentTree<Node> {
fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result<Self> {
Self::read(buf)
fn deserialize_reader<R: Read>(reader: &mut R) -> io::Result<Self> {
Self::read(reader)
}
}

Expand Down Expand Up @@ -698,14 +698,14 @@ impl<Node: Hashable> IncrementalWitness<Node> {
}

impl<Node: Hashable> BorshSerialize for IncrementalWitness<Node> {
fn serialize<W: Write>(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> {
fn serialize<W: Write>(&self, writer: &mut W) -> io::Result<()> {
self.write(writer)
}
}

impl<Node: Hashable> BorshDeserialize for IncrementalWitness<Node> {
fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result<Self> {
Self::read(buf)
fn deserialize_reader<R: Read>(reader: &mut R) -> io::Result<Self> {
Self::read(reader)
}
}

Expand Down Expand Up @@ -761,17 +761,18 @@ impl<Node: Hashable> MerklePath<Node> {
}

impl<Node: Hashable> BorshDeserialize for MerklePath<Node> {
fn deserialize(witness: &mut &[u8]) -> Result<Self, std::io::Error> {
fn deserialize_reader<R: Read>(witness: &mut R) -> Result<Self, std::io::Error> {
// 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::<LittleEndian>()?;

Expand Down
38 changes: 18 additions & 20 deletions masp_primitives/src/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,14 @@ impl ViewingKey {
}

impl BorshSerialize for ViewingKey {
fn serialize<W: Write>(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> {
fn serialize<W: Write>(&self, writer: &mut W) -> io::Result<()> {
self.write(writer)
}
}

impl BorshDeserialize for ViewingKey {
fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result<Self> {
Self::read(buf)
fn deserialize_reader<R: Read>(reader: &mut R) -> io::Result<Self> {
Self::read(reader)
}
}

Expand Down Expand Up @@ -477,18 +477,16 @@ impl Hash for PaymentAddress {
}

impl BorshSerialize for PaymentAddress {
fn serialize<W: Write>(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> {
fn serialize<W: Write>(&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<Self> {
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<R: Read>(reader: &mut R) -> io::Result<Self> {
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)
}
}
Expand Down Expand Up @@ -684,7 +682,7 @@ impl Note {
}

impl BorshSerialize for Note {
fn serialize<W: std::io::Write>(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> {
fn serialize<W: std::io::Write>(&self, writer: &mut W) -> io::Result<()> {
// Write asset type
self.asset_type.serialize(writer)?;
// Write note value
Expand Down Expand Up @@ -712,23 +710,23 @@ impl BorshSerialize for Note {
}

impl BorshDeserialize for Note {
fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result<Self> {
fn deserialize_reader<R: Read>(reader: &mut R) -> io::Result<Self> {
// Read asset type
let asset_type = AssetType::deserialize(buf)?;
let asset_type = AssetType::deserialize_reader(reader)?;
// Read note value
let value = buf.read_u64::<LittleEndian>()?;
let value = reader.read_u64::<LittleEndian>()?;
// 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"))?;
Expand Down Expand Up @@ -820,15 +818,15 @@ mod tests {
sapling::Note,
transaction::components::amount::MAX_MONEY,
};
use borsh::{BorshDeserialize, BorshSerialize};
use borsh::BorshDeserialize;
use proptest::prelude::*;

proptest! {
#![proptest_config(ProptestConfig::with_cases(10))]
#[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(&note).unwrap();
// BorshDeserialize
let de_note: Note = BorshDeserialize::deserialize(&mut borsh.as_ref()).unwrap();
prop_assert_eq!(note, de_note);
Expand Down
12 changes: 6 additions & 6 deletions masp_primitives/src/sapling/redjubjub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,25 @@ impl Hash for PublicKey {
}

impl BorshDeserialize for PublicKey {
fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result<Self> {
Ok(Self(read_point(buf, "public key")?))
fn deserialize_reader<R: Read>(reader: &mut R) -> io::Result<Self> {
Ok(Self(read_point(reader, "public key")?))
}
}

impl BorshSerialize for PublicKey {
fn serialize<W: Write>(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> {
fn serialize<W: Write>(&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> {
Self::read(buf)
fn deserialize_reader<R: Read>(reader: &mut R) -> io::Result<Self> {
Self::read(reader)
}
}

impl BorshSerialize for Signature {
fn serialize<W: Write>(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> {
fn serialize<W: Write>(&self, writer: &mut W) -> io::Result<()> {
self.write(writer)
}
}
Expand Down
12 changes: 5 additions & 7 deletions masp_primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -283,23 +284,20 @@ impl TransactionData<Authorized> {
}

impl BorshSerialize for Transaction {
fn serialize<W: Write>(&self, writer: &mut W) -> borsh::maybestd::io::Result<()> {
fn serialize<W: Write>(&self, writer: &mut W) -> io::Result<()> {
self.write(writer)
}
}

impl BorshDeserialize for Transaction {
fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result<Self> {
Self::read(buf, BranchId::MASP)
fn deserialize_reader<R: Read>(reader: &mut R) -> io::Result<Self> {
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<borsh::schema::Declaration, borsh::schema::Definition>,
) {
}

Expand Down
6 changes: 3 additions & 3 deletions masp_primitives/src/transaction/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ impl Progress {

/// Generates a [`Transaction`] from its inputs and outputs.
#[derive(Clone, Debug, BorshSerialize, BorshDeserialize)]
pub struct Builder<P, R, Key = ExtendedSpendingKey, Notifier = Sender<Progress>> {
pub struct Builder<P, RN, Key = ExtendedSpendingKey, Notifier = Sender<Progress>> {
params: P,
rng: R,
rng: RN,
target_height: BlockHeight,
expiry_height: BlockHeight,
transparent_builder: TransparentBuilder,
sapling_builder: SaplingBuilder<P, Key>,
#[borsh_skip]
#[borsh(skip)]
progress_notifier: Option<Notifier>,
}

Expand Down
2 changes: 1 addition & 1 deletion masp_primitives/src/transaction/components/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl MapAuth<Authorized, Authorized> for () {
}
}

#[derive(Clone, Debug, BorshSerialize, BorshDeserialize, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub struct Bundle<A: Authorization + PartialEq + BorshSerialize + BorshDeserialize> {
pub shielded_spends: Vec<SpendDescription<A>>,
pub shielded_converts: Vec<ConvertDescription<A::Proof>>,
Expand Down
Loading

0 comments on commit f4f1ce1

Please sign in to comment.