jf_merkle_tree/
examples.rs

1// Copyright (c) 2022 Espresso Systems (espressosys.com)
2// This file is part of the Jellyfish library.
3
4// You should have received a copy of the MIT License
5// along with the Jellyfish library. If not, see <https://mit-license.org/>.
6
7//! Provides sample instantiations of merkle tree.
8//! E.g. Sparse merkle tree with BigUInt index.
9
10use super::{append_only::MerkleTree, prelude::RescueHash, DigestAlgorithm};
11use crate::errors::MerkleTreeError;
12use ark_ff::Field;
13use ark_std::{format, vec};
14use jf_rescue::{crhf::RescueCRHF, RescueParameter};
15
16/// Element type for interval merkle tree
17#[derive(PartialEq, Eq, Copy, Clone, Hash)]
18pub struct Interval<F: Field>(pub F, pub F);
19// impl<F: Field> Element for Interval<F> {}
20
21impl<F: RescueParameter> DigestAlgorithm<Interval<F>, u64, F> for RescueHash<F> {
22    fn digest(data: &[F]) -> Result<F, MerkleTreeError> {
23        let mut input = vec![F::zero()];
24        input.extend(data.iter());
25        Ok(RescueCRHF::<F>::sponge_no_padding(&input, 1)
26            .map_err(|err| MerkleTreeError::DigestError(format!("{}", err)))?[0])
27    }
28
29    fn digest_leaf(pos: &u64, elem: &Interval<F>) -> Result<F, MerkleTreeError> {
30        let data = [F::one(), F::from(*pos), elem.0, elem.1];
31        Ok(RescueCRHF::<F>::sponge_no_padding(&data, 1)
32            .map_err(|err| MerkleTreeError::DigestError(format!("{}", err)))?[0])
33    }
34}
35
36/// Interval merkle tree instantiation for interval merkle tree using Rescue
37/// hash function.
38pub type IntervalMerkleTree<F> = MerkleTree<Interval<F>, RescueHash<F>, u64, 3, F>;