zebra/zebra-chain/src/sprout/tree.rs

578 lines
25 KiB
Rust
Raw Normal View History

//! Note Commitment Trees.
//!
//! A note commitment tree is an incremental Merkle tree of fixed depth
//! used to store note commitments that JoinSplit transfers or Spend
//! transfers produce. Just as the unspent transaction output set (UTXO
//! set) used in Bitcoin, it is used to express the existence of value and
//! the capability to spend it. However, unlike the UTXO set, it is not
//! the job of this tree to protect against double-spending, as it is
//! append-only.
//!
//! A root of a note commitment tree is associated with each treestate.
#![allow(clippy::unit_arg)]
use std::fmt;
2020-10-02 19:49:15 -07:00
use byteorder::{BigEndian, ByteOrder, LittleEndian};
use lazy_static::lazy_static;
#[cfg(any(test, feature = "proptest-impl"))]
use proptest_derive::Arbitrary;
use sha2::digest::generic_array::GenericArray;
use super::commitment::NoteCommitment;
const MERKLE_DEPTH: usize = 29;
/// MerkleCRH^Sprout Hash Function
///
/// MerkleCRH^Sprout(layer, left, right) := SHA256Compress(left || right)
///
/// `layer` is unused for Sprout but used for the Sapling equivalent.
///
/// https://zips.z.cash/protocol/protocol.pdf#merklecrh
fn merkle_crh_sprout(left: [u8; 32], right: [u8; 32]) -> [u8; 32] {
let mut other_block = [0u8; 64];
other_block[..32].copy_from_slice(&left[..]);
other_block[32..].copy_from_slice(&right[..]);
2020-10-02 19:49:15 -07:00
// H256: Sha256 initial state
// https://github.com/RustCrypto/hashes/blob/master/sha2/src/consts.rs#L170
let mut state = [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab,
0x5be0cd19,
];
sha2::compress256(&mut state, &[GenericArray::clone_from_slice(&other_block)]);
2020-10-02 19:49:15 -07:00
// Yes, sha256 does big endian here.
// https://github.com/RustCrypto/hashes/blob/master/sha2/src/sha256.rs#L40
let mut derived_bytes = [0u8; 32];
2020-10-02 19:49:15 -07:00
BigEndian::write_u32_into(&state, &mut derived_bytes);
derived_bytes
}
lazy_static! {
/// Sprout note commitment trees have a max depth of 29.
///
/// https://zips.z.cash/protocol/canopy.pdf#constants
static ref EMPTY_ROOTS: Vec<[u8; 32]> = {
// Uncommitted^Sprout = = [0]^l_MerkleSprout
let mut v = vec![[0u8; 32]];
for d in 0..MERKLE_DEPTH {
2020-10-02 20:54:52 -07:00
v.push(merkle_crh_sprout(v[d], v[d]));
}
v
};
}
/// The index of a notes commitment at the leafmost layer of its Note
/// Commitment Tree.
///
/// https://zips.z.cash/protocol/protocol.pdf#merkletree
pub struct Position(pub(crate) u64);
/// Sprout note commitment tree root node hash.
///
/// The root hash in LEBS2OSP256(rt) encoding of the Sprout note
/// commitment tree corresponding to the final Sprout treestate of
/// this block. A root of a note commitment tree is associated with
/// each treestate.
#[derive(Clone, Copy, Default, Eq, PartialEq, Serialize, Deserialize, Hash)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
pub struct Root([u8; 32]);
impl fmt::Debug for Root {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("Root").field(&hex::encode(&self.0)).finish()
}
}
impl From<[u8; 32]> for Root {
fn from(bytes: [u8; 32]) -> Root {
Self(bytes)
}
}
impl From<Root> for [u8; 32] {
fn from(rt: Root) -> [u8; 32] {
rt.0
}
}
/// Sprout Note Commitment Tree
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[cfg_attr(test, derive(Arbitrary))]
struct NoteCommitmentTree {
/// The root node of the tree (often used as an anchor).
root: Root,
/// The height of the tree (maximum height for Sprout is 29).
height: u8,
/// The number of leaves (note commitments) in this tree.
count: u32,
}
impl From<Vec<NoteCommitment>> for NoteCommitmentTree {
fn from(values: Vec<NoteCommitment>) -> Self {
if values.is_empty() {
return NoteCommitmentTree {
root: Root::default(),
height: 0,
count: 0,
};
}
let count = values.len() as u32;
let mut height = 0u8;
let mut current_layer: Vec<[u8; 32]> = values.into_iter().map(|cm| cm.into()).collect();
while usize::from(height) < MERKLE_DEPTH {
let mut next_layer_up = vec![];
while !current_layer.is_empty() {
let left = current_layer.remove(0);
let right;
if current_layer.is_empty() {
right = EMPTY_ROOTS[height as usize];
} else {
right = current_layer.remove(0);
}
2020-10-02 20:54:52 -07:00
let node = merkle_crh_sprout(left, right);
next_layer_up.push(node);
}
height += 1;
current_layer = next_layer_up;
}
assert!(current_layer.len() == 1);
NoteCommitmentTree {
root: Root(current_layer.remove(0)),
height,
count,
}
}
}
impl NoteCommitmentTree {
/// Get the Jubjub-based Pedersen hash of root node of this merkle tree of
/// commitment notes.
pub fn hash(&self) -> [u8; 32] {
self.root.0
}
}
#[cfg(test)]
mod tests {
use super::*;
2020-09-24 22:13:32 -07:00
// From https://github.com/zcash/zcash/blob/master/src/zcash/IncrementalMerkleTree.cpp#L439
2020-10-02 19:49:15 -07:00
// These are the correct-byteorder (little endian)
2020-09-24 22:13:32 -07:00
const HEX_EMPTY_ROOTS: [[u8; 32]; 66] = [
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
],
[
0xda, 0x56, 0x98, 0xbe, 0x17, 0xb9, 0xb4, 0x69, 0x62, 0x33, 0x57, 0x99, 0x77, 0x9f,
0xbe, 0xca, 0x8c, 0xe5, 0xd4, 0x91, 0xc0, 0xd2, 0x62, 0x43, 0xba, 0xfe, 0xf9, 0xea,
0x18, 0x37, 0xa9, 0xd8,
],
[
0xdc, 0x76, 0x6f, 0xab, 0x49, 0x2c, 0xcf, 0x3d, 0x1e, 0x49, 0xd4, 0xf3, 0x74, 0xb5,
0x23, 0x5f, 0xa5, 0x65, 0x06, 0xaa, 0xc2, 0x22, 0x4d, 0x39, 0xf9, 0x43, 0xfc, 0xd4,
0x92, 0x02, 0x97, 0x4c,
],
[
0x3f, 0x0a, 0x40, 0x61, 0x81, 0x10, 0x59, 0x68, 0xfd, 0xae, 0xe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0xee, 0x8b, 0xae, 0x71,
0xf2, 0xf7, 0x79, 0x3d, 0x3a, 0xd5, 0xfe, 0x33, 0x83, 0xf9, 0x3a, 0xb6, 0x06, 0x1f,
0x2a, 0x11, 0xbb, 0x02,
],
];
#[test]
fn empty_roots() {
for i in 0..EMPTY_ROOTS.len() {
2020-09-24 22:13:32 -07:00
assert_eq!(EMPTY_ROOTS[i], HEX_EMPTY_ROOTS[i]);
}
}
#[test]
fn incremental_roots() {
// From https://github.com/zcash/zcash/blob/master/src/test/data/merkle_commitments.json
// Byte-reversed (?) from those ones because the original test vectors are loaded using uint256S()
let commitments = [
"bab6e8992959caf0ca94847c36b4e648a7f88a9b9c6a62ea387cf1fb9badfd62",
"43c9a4b21555b832a79fc12ce27a97d4f4eca1638e7161a780db1d5ebc35eb68",
"fb92a6142315bb3396b693222bf2d0e260b448cda74e189063cf774048456083",
"e44a57cd544018937680d385817be3a3e35bb5b87ceeea93d536ea95828a4992",
"43f48bfb9ab6f12ef91ce83e8f9190ce5dff2721784c90e08a50a67403367cff",
"fce910561c3c7ebf14ed5d712e6838cdc6f1145c87eec256b7181f9df6d0c468",
"b1e7016392805b227b11e58ba629f9a6684a0b4c34306e85e47548c43ecd168b",
"2d9a49d9425449a449cc62d16febaf9c7f8b32349752ecc39191c36130b4c050",
"53969b31a862b893dde857b8b7d4f53ce0e2c21a0f70d48ba1aef3a05fddff70",
"17f8fabd440fdf9e2eafd75a3407e8bbde048d2d2232cd803d5763004af61ed8",
"9b7805cb5e8ef337c13c73cab58ee719bf33a4a80ecc161bfe714269eca4928b",
"a3ebada94d4329899ae136391604799d8cea39c0c331f9aaaa4a1e73ab63e904",
"12091a20c9ebe67c2793bb71a6fdddb0ffe3ca781fcf1e192428161f186c3fbe",
"e9c65749638df548b8909c0ea1d0f79079a6bb3235c649a8806322c87f968018",
"8e8fddf0438a4263bc926fcfa6733dc201633959f294103533a2cb9328bb65c4",
"206a202bd08dd31f77afc7114b17850192b83948cff5828df0d638cbe734c884",
];
2020-10-02 20:54:52 -07:00
// Calculated by the above implementation for MERKLE_DEPTH = 29 by the
// same code confirmed to produce the test vectors from
// https://github.com/zcash/zcash/blob/master/src/test/data/merkle_roots.json
// when MERKLE_DEPTH = 4.
let roots = [
2020-10-02 20:54:52 -07:00
"b8e10b6c157be92c43a733e2c9bddb963a2fb9ea80ebcb307acdcc5fc89f1656",
"83a7754b8240699dd1b63bf70cf70db28ffeb74ef87ce2f4dd32c28ae5009f4f",
"c45297124f50dcd3f78eed017afd1e30764cd74cdf0a57751978270fd0721359",
"b61f588fcba9cea79e94376adae1c49583f716d2f20367141f1369a235b95c98",
"a3165c1708f0cc028014b9bf925a81c30091091ca587624de853260cd151b524",
"6bb8c538c550abdd26baa2a7510a4ae50a03dc00e52818b9db3e4ffaa29c1f41",
"e04e4731085ba95e3fa7c8f3d5eb9a56af63363403b783bc68802629c3fe505b",
"c3714ab74d8e3984e8b58a2b4806934d20f6e67d7246cf8f5b2762305294a0ea",
"63657edeead4bc45610b6d5eb80714a0622aad5788119b7d9961453e3aacda21",
"e31b80819221718440c5351525dbb902d60ed16b74865a2528510959a1960077",
"872f13df2e12f5503c39100602930b0f91ea360e5905a9f5ceb45d459efc36b2",
"bdd7105febb3590832e946aa590d07377d1366cf5e7267507efa399dd0febdbc",
"0f45f4adcb846a8bb56833ca0cae96f2fb8747958daa191a46d0f9d93268260a",
"41c6e456e2192ab74f72cb27c444a2734ca8ade5a4788c1bc2546118dda01778",
"8261355fd9bafc52a08d738fed29a859fbe15f2e74a5353954b150be200d0e16",
"90665cb8a43001f0655169952399590cd17f99165587c1dd842eb674fb9f0afe",
];
let mut leaves = vec![];
for (i, cm) in commitments.iter().enumerate() {
let mut bytes = [0u8; 32];
2020-09-24 22:13:32 -07:00
let _ = hex::decode_to_slice(cm, &mut bytes);
2020-10-02 20:54:52 -07:00
// Byte-reverse
bytes.reverse();
leaves.push(NoteCommitment::from(bytes));
let tree = NoteCommitmentTree::from(leaves.clone());
assert_eq!(hex::encode(tree.hash()), roots[i]);
}
}
}