Skip to content

Commit

Permalink
feature: add conditional support for serde
Browse files Browse the repository at this point in the history
  • Loading branch information
hackaugusto committed Aug 8, 2023
1 parent b3e7578 commit 19f7a68
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ harness = false
[features]
default = ["blake3/default", "std", "winter_crypto/default", "winter_math/default", "winter_utils/default"]
std = ["blake3/std", "winter_crypto/std", "winter_math/std", "winter_utils/std"]
serde = ["winter_math/serde", "dep:serde", "dep:serde_arrays", "serde/alloc"]

[dependencies]
blake3 = { version = "1.4", default-features = false }
winter_crypto = { version = "0.6", package = "winter-crypto", default-features = false }
winter_math = { version = "0.6", package = "winter-math", default-features = false }
winter_utils = { version = "0.6", package = "winter-utils", default-features = false }
serde = { version = "1.0", features = [ "derive" ], optional = true, default-features = false }
serde_arrays = { version = "0.1", optional = true }

[patch.crates-io]
winter_math = { git = "https://github.com/hackaugusto/winterfell/", branch = "hacka-conditional-support-for-serde", package = "winter-math" }
winter_utils = { git = "https://github.com/hackaugusto/winterfell/", branch = "hacka-conditional-support-for-serde", package = "winter-utils" }

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
Expand Down
8 changes: 7 additions & 1 deletion src/hash/blake/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use core::{
#[cfg(test)]
mod tests;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// CONSTANTS
// ================================================================================================

Expand All @@ -24,7 +27,10 @@ const DIGEST20_BYTES: usize = 20;
/// Note: `N` can't be greater than `32` because [`Digest::as_bytes`] currently supports only 32
/// bytes.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Blake3Digest<const N: usize>([u8; N]);
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Blake3Digest<const N: usize>(
#[cfg_attr(feature = "serde", serde(with = "serde_arrays"))] [u8; N],
);

impl<const N: usize> Default for Blake3Digest<N> {
fn default() -> Self {
Expand Down
4 changes: 4 additions & 0 deletions src/hash/rpo/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ use crate::utils::{
};
use core::{cmp::Ordering, fmt::Display, ops::Deref};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// DIGEST TRAIT IMPLEMENTATIONS
// ================================================================================================

#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct RpoDigest([Felt; DIGEST_SIZE]);

impl RpoDigest {
Expand Down
6 changes: 6 additions & 0 deletions src/merkle/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use super::{
};
use crate::utils::collections::Diff;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(test)]
use super::{empty_roots::EMPTY_WORD, Felt, SimpleSmt};

Expand All @@ -13,6 +16,7 @@ use super::{empty_roots::EMPTY_WORD, Felt, SimpleSmt};
/// [RpoDigest] represents the root of the Merkle tree and [MerkleTreeDelta] represents the
/// differences between the initial and final Merkle tree states.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct MerkleStoreDelta(pub Vec<(RpoDigest, MerkleTreeDelta)>);

// MERKLE TREE DELTA
Expand All @@ -26,6 +30,7 @@ pub struct MerkleStoreDelta(pub Vec<(RpoDigest, MerkleTreeDelta)>);
/// - updated_slots: index-value pairs of slots where values were set to non [ZERO; 4] values.
#[cfg(not(test))]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct MerkleTreeDelta {
depth: u8,
cleared_slots: Vec<u64>,
Expand Down Expand Up @@ -107,6 +112,7 @@ pub fn merkle_tree_delta<T: KvMap<RpoDigest, StoreNode>>(
// --------------------------------------------------------------------------------------------
#[cfg(test)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct MerkleTreeDelta {
pub depth: u8,
pub cleared_slots: Vec<u64>,
Expand Down
4 changes: 4 additions & 0 deletions src/merkle/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use super::{Felt, MerkleError, RpoDigest, StarkField};
use crate::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
use core::fmt::Display;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// NODE INDEX
// ================================================================================================

Expand All @@ -21,6 +24,7 @@ use core::fmt::Display;
/// The root is represented by the pair $(0, 0)$, its left child is $(1, 0)$ and its right child
/// $(1, 1)$.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct NodeIndex {
depth: u8,
value: u64,
Expand Down
4 changes: 4 additions & 0 deletions src/merkle/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ use crate::utils::{string::String, uninit_vector, word_to_hex};
use core::{fmt, ops::Deref, slice};
use winter_math::log2;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// MERKLE TREE
// ================================================================================================

/// A fully-balanced binary Merkle tree (i.e., a tree where the number of leaves is a power of two).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct MerkleTree {
nodes: Vec<RpoDigest>,
}
Expand Down
4 changes: 4 additions & 0 deletions src/merkle/mmr/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use super::{
Felt, MmrProof, Rpo256, Word,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct MmrPeaks {
/// The number of leaves is used to differentiate accumulators that have the same number of
/// peaks. This happens because the number of peaks goes up-and-down as the structure is used
Expand Down
4 changes: 4 additions & 0 deletions src/merkle/mmr/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use super::{
};
use core::fmt::{Display, Formatter};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "std")]
use std::error::Error;

Expand All @@ -29,6 +32,7 @@ use std::error::Error;
/// Since this is a full representation of the MMR, elements are never removed and the MMR will
/// grow roughly `O(2n)` in number of leaf elements.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Mmr {
/// Refer to the `forest` method documentation for details of the semantics of this value.
pub(super) forest: usize,
Expand Down
4 changes: 4 additions & 0 deletions src/merkle/mmr/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
use super::super::MerklePath;
use super::full::{high_bitmask, leaf_to_corresponding_tree};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct MmrProof {
/// The state of the MMR when the MmrProof was created.
pub forest: usize,
Expand Down
4 changes: 4 additions & 0 deletions src/merkle/node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::hash::rpo::RpoDigest;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Representation of a node with two children used for iterating over containers.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct InnerNodeInfo {
pub value: RpoDigest,
pub left: RpoDigest,
Expand Down
4 changes: 4 additions & 0 deletions src/merkle/partial_mt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use crate::utils::{
};
use core::fmt;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(test)]
mod tests;

Expand All @@ -28,6 +31,7 @@ const EMPTY_DIGEST: RpoDigest = RpoDigest::new([ZERO; 4]);
///
/// The root of the tree is recomputed on each new leaf update.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct PartialMerkleTree {
max_depth: u8,
nodes: BTreeMap<NodeIndex, RpoDigest>,
Expand Down
4 changes: 4 additions & 0 deletions src/merkle/path.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use super::{vec, InnerNodeInfo, MerkleError, NodeIndex, Rpo256, RpoDigest, Vec};
use core::ops::{Deref, DerefMut};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// MERKLE PATH
// ================================================================================================

/// A merkle path container, composed of a sequence of nodes of a Merkle tree.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct MerklePath {
nodes: Vec<RpoDigest>,
}
Expand Down
5 changes: 5 additions & 0 deletions src/merkle/simple_smt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use super::{
NodeIndex, Rpo256, RpoDigest, StoreNode, TryApplyDiff, Vec, Word,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(test)]
mod tests;

Expand All @@ -13,6 +16,7 @@ mod tests;
///
/// The root of the tree is recomputed on each new leaf update.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct SimpleSmt {
depth: u8,
root: RpoDigest,
Expand Down Expand Up @@ -265,6 +269,7 @@ impl SimpleSmt {
// ================================================================================================

#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
struct BranchNode {
left: RpoDigest,
right: RpoDigest,
Expand Down
5 changes: 5 additions & 0 deletions src/merkle/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use super::{
use crate::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
use core::borrow::Borrow;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(test)]
mod tests;

Expand All @@ -19,6 +22,7 @@ pub type DefaultMerkleStore = MerkleStore<BTreeMap<RpoDigest, StoreNode>>;
pub type RecordingMerkleStore = MerkleStore<RecordingMap<RpoDigest, StoreNode>>;

#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct StoreNode {
left: RpoDigest,
right: RpoDigest,
Expand Down Expand Up @@ -87,6 +91,7 @@ pub struct StoreNode {
/// assert_eq!(store.num_internal_nodes() - 255, 10);
/// ```
#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct MerkleStore<T: KvMap<RpoDigest, StoreNode> = BTreeMap<RpoDigest, StoreNode>> {
nodes: T,
}
Expand Down
4 changes: 4 additions & 0 deletions src/merkle/tiered_smt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub use proof::TieredSmtProof;
mod error;
pub use error::TieredSmtProofError;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(test)]
mod tests;

Expand All @@ -43,6 +46,7 @@ mod tests;
/// - Leaf node at depths 16, 32, or 64: hash(key, value, domain=depth).
/// - Leaf node at depth 64: hash([key_0, value_0, ..., key_n, value_n], domain=64).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct TieredSmt {
root: RpoDigest,
nodes: NodeStore,
Expand Down
4 changes: 4 additions & 0 deletions src/merkle/tiered_smt/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use super::{
NodeIndex, Rpo256, RpoDigest, Vec,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// CONSTANTS
// ================================================================================================

Expand All @@ -24,6 +27,7 @@ const MAX_DEPTH: u8 = super::TieredSmt::MAX_DEPTH;
/// represent leaf nodes in a Tiered Sparse Merkle tree. In the current implementation, [BTreeSet]s
/// are used to determine the position of the leaves in the tree.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct NodeStore {
nodes: BTreeMap<NodeIndex, RpoDigest>,
upper_leaves: BTreeSet<NodeIndex>,
Expand Down
5 changes: 5 additions & 0 deletions src/merkle/tiered_smt/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use core::{
};
use winter_utils::collections::btree_map::Entry;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// CONSTANTS
// ================================================================================================

Expand All @@ -26,6 +29,7 @@ const MAX_DEPTH: u8 = super::TieredSmt::MAX_DEPTH;
/// The store supports lookup by the full key (i.e. [RpoDigest]) as well as by the 64-bit key
/// prefix.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct ValueStore {
values: BTreeMap<u64, StoreEntry>,
}
Expand Down Expand Up @@ -173,6 +177,7 @@ impl ValueStore {
/// An entry can contain either a single key-value pair or a vector of key-value pairs sorted by
/// key.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum StoreEntry {
Single((RpoDigest, Word)),
List(Vec<(RpoDigest, Word)>),
Expand Down

0 comments on commit 19f7a68

Please sign in to comment.