Skip to content

Commit

Permalink
Created a BorshSchema for Transaction. Made BorshSerialization of Val…
Browse files Browse the repository at this point in the history
…ueSum consistent with read and write. Fixed minor bugs in Transaction reading.
  • Loading branch information
murisi committed Feb 16, 2024
1 parent 8838ae3 commit 243544f
Show file tree
Hide file tree
Showing 8 changed files with 507 additions and 14 deletions.
17 changes: 15 additions & 2 deletions masp_note_encryption/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use crate::alloc::string::ToString;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;

use core::convert::TryInto;
Expand All @@ -34,7 +36,7 @@ use cipher::KeyIvInit;

//use crate::constants::ASSET_IDENTIFIER_LENGTH;
pub const ASSET_IDENTIFIER_LENGTH: usize = 32;
use borsh::{BorshDeserialize, BorshSerialize};
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use rand_core::RngCore;
use subtle::{Choice, ConstantTimeEq};

Expand Down Expand Up @@ -77,7 +79,18 @@ impl AsRef<[u8]> for OutgoingCipherKey {
/// Newtype representing the byte encoding of an [`EphemeralPublicKey`].
///
/// [`EphemeralPublicKey`]: Domain::EphemeralPublicKey
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(
BorshSerialize,
BorshDeserialize,
BorshSchema,
Clone,
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
)]
pub struct EphemeralKeyBytes(pub [u8; 32]);

impl AsRef<[u8]> for EphemeralKeyBytes {
Expand Down
35 changes: 35 additions & 0 deletions masp_primitives/src/consensus.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
//! Consensus logic and parameters.

use borsh::schema::add_definition;
use borsh::schema::Definition;
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use memuse::DynamicUsage;
use std::cmp::{Ord, Ordering};
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::fmt;
use std::io::{Error, ErrorKind, Read, Write};
use std::ops::{Add, Bound, RangeBounds, Sub};

/// A wrapper type representing blockchain heights. Safe conversion from
Expand Down Expand Up @@ -267,6 +271,37 @@ impl From<BranchId> for u32 {
}
}

impl BorshSerialize for BranchId {
fn serialize<W: Write>(&self, writer: &mut W) -> std::io::Result<()> {
u32::from(*self).serialize(writer)
}
}

impl BorshDeserialize for BranchId {
fn deserialize_reader<R: Read>(reader: &mut R) -> std::io::Result<Self> {
u32::deserialize_reader(reader)?
.try_into()
.map_err(|x| Error::new(ErrorKind::InvalidInput, x))
}
}

impl BorshSchema for BranchId {
fn add_definitions_recursively(
definitions: &mut BTreeMap<borsh::schema::Declaration, borsh::schema::Definition>,
) {
let definition = Definition::Enum {
tag_width: 4,
variants: vec![(0xe9ff_75a6, "MASP".into(), <()>::declaration())],
};
add_definition(Self::declaration(), definition, definitions);
<()>::add_definitions_recursively(definitions);
}

fn declaration() -> borsh::schema::Declaration {
"BranchId".into()
}
}

impl BranchId {
/// Returns the branch ID corresponding to the consensus rule set that is active at
/// the given height.
Expand Down
12 changes: 11 additions & 1 deletion masp_primitives/src/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,17 @@ impl BorshDeserialize for Rseed {

/// Typesafe wrapper for nullifier values.
#[derive(
Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, BorshSerialize, BorshDeserialize,
Copy,
Clone,
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
BorshSerialize,
BorshDeserialize,
BorshSchema,
)]
pub struct Nullifier(pub [u8; 32]);

Expand Down
24 changes: 22 additions & 2 deletions masp_primitives/src/sapling/redjubjub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
use crate::transaction::components::sapling::read_point;

use super::util::hash_to_scalar;
use borsh::{BorshDeserialize, BorshSerialize};
use borsh::schema::add_definition;
use borsh::schema::Definition;
use borsh::schema::Fields;
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use ff::{Field, PrimeField};
use group::GroupEncoding;
use jubjub::{AffinePoint, ExtendedPoint, SubgroupPoint};
use rand_core::RngCore;
use std::collections::BTreeMap;
use std::{
cmp::Ordering,
hash::{Hash, Hasher},
Expand All @@ -34,7 +38,7 @@ fn h_star(a: &[u8], b: &[u8]) -> jubjub::Fr {
hash_to_scalar(b"MASP__RedJubjubH", a, b)
}

#[derive(Copy, Clone, Debug, PartialOrd, PartialEq, Ord, Eq, Hash)]
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq, Ord, Eq, Hash, BorshSchema)]
pub struct Signature {
rbar: [u8; 32],
sbar: [u8; 32],
Expand Down Expand Up @@ -69,6 +73,22 @@ impl BorshSerialize for PublicKey {
}
}

impl BorshSchema for PublicKey {
fn add_definitions_recursively(
definitions: &mut BTreeMap<borsh::schema::Declaration, borsh::schema::Definition>,
) {
let definition = Definition::Struct {
fields: Fields::UnnamedFields(vec![<[u8; 32]>::declaration()]),
};
add_definition(Self::declaration(), definition, definitions);
<[u8; 32]>::add_definitions_recursively(definitions);
}

fn declaration() -> borsh::schema::Declaration {
"PublicKey".into()
}
}

impl BorshDeserialize for Signature {
fn deserialize_reader<R: Read>(reader: &mut R) -> io::Result<Self> {
Self::read(reader)
Expand Down
Loading

0 comments on commit 243544f

Please sign in to comment.