Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional arbitrary impls #87

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions masp_note_encryption/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ chacha20poly1305 = { version = "0.10", default-features = false }
rand_core = { version = "0.6", default-features = false }
subtle = { version = "2.3", default-features = false }
borsh = {version = "1.2.0", features = ["unstable__schema", "derive"]}
arbitrary = {version = "1.3", features = ["derive"], optional = true }

[features]
default = ["alloc"]
alloc = []
pre-zip-212 = []
arbitrary = ["dep:arbitrary"]

[lib]
bench = false
4 changes: 2 additions & 2 deletions masp_note_encryption/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! [in-band secret distribution scheme]: https://zips.z.cash/protocol/protocol.pdf#saplingandorchardinband
//! [`masp_primitives`]: https://github.com/anoma/masp

#![no_std]
#![cfg_attr(not(feature = "arbitrary"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
// Catch documentation errors caused by code changes.
#![deny(rustdoc::broken_intra_doc_links)]
Expand Down Expand Up @@ -75,10 +75,10 @@ impl AsRef<[u8]> for OutgoingCipherKey {
&self.0
}
}

/// Newtype representing the byte encoding of an [`EphemeralPublicKey`].
///
/// [`EphemeralPublicKey`]: Domain::EphemeralPublicKey
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(
BorshSerialize,
BorshDeserialize,
Expand Down
9 changes: 7 additions & 2 deletions masp_primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ num-traits = { version = "0.2.19", git = "https://github.com/heliaxdev/num-trait
subtle = "2.2.3"

# - Shielded protocols
bls12_381 = "0.8"
# branch "tomas/arbitrary"
bls12_381 = { git = "https://github.com/heliaxdev/bls12_381.git", rev = "d3ebe9dd6488fac1923db120a7498079e55dd838" }
ff = "0.13"
group = { version = "0.13", features = ["wnaf-memuse"] }
incrementalmerkletree = "0.3"
jubjub = "0.10"
# branch "tomas/arbitrary"
jubjub = { git = "https://github.com/heliaxdev/jubjub.git", rev = "a373686962f4e9d0edb3b4716f86ff6bbd9aa86c" }
nonempty = "0.7"

# - Static constants
Expand All @@ -74,6 +76,8 @@ aes = "0.8"
fpe = "0.6"

borsh = {version = "1.2.0", features = ["unstable__schema", "derive"]}
arbitrary = {version = "1.3", features = ["derive"], optional = true }

[dependencies.masp_note_encryption]
version = "1.0.0"
path = "../masp_note_encryption"
Expand All @@ -90,6 +94,7 @@ rand_xorshift = "0.3"
transparent-inputs = []
test-dependencies = ["proptest"]
default = ["transparent-inputs"]
arbitrary = ["dep:arbitrary", "masp_note_encryption/arbitrary", "bls12_381/arbitrary", "jubjub/arbitrary"]

[badges]
maintenance = { status = "actively-developed" }
1 change: 1 addition & 0 deletions masp_primitives/src/asset_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{
hash::{Hash, Hasher},
};

#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Debug, BorshSerialize, BorshDeserialize, Clone, Copy, Eq, BorshSchema)]
pub struct AssetType {
identifier: [u8; ASSET_IDENTIFIER_LENGTH], //32 byte asset type preimage
Expand Down
2 changes: 2 additions & 0 deletions masp_primitives/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::ops::{Add, Bound, RangeBounds, Sub};

/// A wrapper type representing blockchain heights. Safe conversion from
/// various integer types, as well as addition and subtraction, are provided.
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[repr(transparent)]
#[derive(
Clone, Copy, Debug, PartialEq, Eq, Hash, BorshSerialize, BorshDeserialize, BorshSchema,
Expand Down Expand Up @@ -244,6 +245,7 @@ pub const ZIP212_GRACE_PERIOD: u32 = 0;
/// See [ZIP 200](https://zips.z.cash/zip-0200) for more details.
///
/// [`signature_hash`]: crate::transaction::sighash::signature_hash
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BranchId {
/// The consensus rules deployed by [`NetworkUpgrade::MASP`].
Expand Down
1 change: 1 addition & 0 deletions masp_primitives/src/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ impl<Node: Hashable> BorshDeserialize for IncrementalWitness<Node> {
}

/// A path from a position in a particular commitment tree to the root of that tree.
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MerklePath<Node> {
pub auth_path: Vec<(Node, bool)>,
Expand Down
5 changes: 5 additions & 0 deletions masp_primitives/src/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub fn merkle_hash(depth: usize, lhs: &[u8; 32], rhs: &[u8; 32]) -> [u8; 32] {
}

/// A node within the Sapling commitment tree.
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, BorshSerialize, BorshDeserialize, Default)]
pub struct Node {
repr: [u8; 32],
Expand Down Expand Up @@ -450,6 +451,7 @@ impl SaplingIvk {
}
}

#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(
Copy, Clone, Debug, PartialEq, Eq, Hash, BorshSerialize, BorshDeserialize, BorshSchema,
)]
Expand Down Expand Up @@ -624,6 +626,7 @@ impl BorshSchema for PaymentAddress {
/// Before ZIP 212, the note commitment trapdoor `rcm` must be a scalar value.
/// After ZIP 212, the note randomness `rseed` is a 32-byte sequence, used to derive
/// both the note commitment trapdoor `rcm` and the ephemeral private key `esk`.
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Copy, Clone, Debug)]
pub enum Rseed {
BeforeZip212(jubjub::Fr),
Expand Down Expand Up @@ -686,6 +689,7 @@ impl BorshDeserialize for Rseed {
}

/// Typesafe wrapper for nullifier values.
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(
Copy,
Clone,
Expand Down Expand Up @@ -743,6 +747,7 @@ impl From<NoteValue> for u64 {
}
}

#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, Copy)]
pub struct Note<R = Rseed> {
/// The asset type that the note represents
Expand Down
2 changes: 2 additions & 0 deletions masp_primitives/src/sapling/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ pub enum DecodingError {
}

/// An outgoing viewing key
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(
Clone, Copy, Debug, PartialEq, Eq, Hash, BorshSerialize, BorshDeserialize, BorshSchema,
)]
pub struct OutgoingViewingKey(pub [u8; 32]);

/// A Sapling expanded spending key
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, PartialEq, Eq, Copy)]
pub struct ExpandedSpendingKey {
pub ask: jubjub::Fr,
Expand Down
2 changes: 2 additions & 0 deletions masp_primitives/src/sapling/redjubjub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn h_star(a: &[u8], b: &[u8]) -> jubjub::Fr {
hash_to_scalar(b"MASP__RedJubjubH", a, b)
}

#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq, Ord, Eq, Hash, BorshSchema)]
pub struct Signature {
rbar: [u8; 32],
Expand All @@ -46,6 +47,7 @@ pub struct Signature {

pub struct PrivateKey(pub jubjub::Fr);

#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub struct PublicKey(pub ExtendedPoint);

Expand Down
21 changes: 21 additions & 0 deletions masp_primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use borsh::schema::Fields;
use borsh::schema::{Declaration, Definition};
use std::ops::RangeInclusive;

#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(
Clone,
Copy,
Expand All @@ -60,6 +61,7 @@ pub type GrothProofBytes = [u8; GROTH_PROOF_SIZE];
const MASPV5_TX_VERSION: u32 = 2;
const MASPV5_VERSION_GROUP_ID: u32 = 0x26A7270A;

#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(
Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash, BorshSerialize, BorshDeserialize, BorshSchema,
)]
Expand Down Expand Up @@ -114,6 +116,7 @@ impl TxId {
/// transaction fields. Note that this is not dependent on epoch, only on transaction encoding.
/// For example, if a particular epoch defines a new transaction version but also allows the
/// previous version, then only the new version would be added to this enum.
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TxVersion {
MASPv5,
Expand Down Expand Up @@ -192,11 +195,27 @@ impl BorshSchema for TxVersion {

/// Authorization state for a bundle of transaction data.
pub trait Authorization {
#[cfg(not(feature = "arbitrary"))]
type TransparentAuth: transparent::Authorization + PartialEq + BorshDeserialize + BorshSerialize;
#[cfg(not(feature = "arbitrary"))]
type SaplingAuth: sapling::Authorization + PartialEq + BorshDeserialize + BorshSerialize;

#[cfg(feature = "arbitrary")]
type TransparentAuth: transparent::Authorization
+ PartialEq
+ BorshDeserialize
+ BorshSerialize
+ for<'a> arbitrary::Arbitrary<'a>;
#[cfg(feature = "arbitrary")]
type SaplingAuth: sapling::Authorization
+ PartialEq
+ BorshDeserialize
+ BorshSerialize
+ for<'a> arbitrary::Arbitrary<'a>;
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Unproven;
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Authorized;

Expand All @@ -213,6 +232,7 @@ impl Authorization for Unauthorized {
}

/// A MASP transaction.
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Debug, Clone)]
pub struct Transaction {
txid: TxId,
Expand All @@ -233,6 +253,7 @@ impl PartialEq for Transaction {
}
}

#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Debug, PartialEq, Clone)]
pub struct TransactionData<A: Authorization> {
version: TxVersion,
Expand Down
1 change: 1 addition & 0 deletions masp_primitives/src/transaction/components/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub type I128Sum = ValueSum<AssetType, i128>;

pub type U128Sum = ValueSum<AssetType, u128>;

#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Default, Debug, PartialEq, Eq, Hash)]
pub struct ValueSum<
Unit: Hash + Ord + BorshSerialize + BorshDeserialize,
Expand Down
Loading
Loading