dapol/
accumulators.rs

1//! Various accumulator variants of the DAPOL+ protocol.
2//!
3//! An accumulator defines how the binary tree is built. There are different
4//! types of accumulators, which can all be found under this module.
5
6use clap::ValueEnum;
7use curve25519_dalek::{ristretto::RistrettoPoint, scalar::Scalar};
8use primitive_types::H256;
9use serde::{Deserialize, Serialize};
10use std::fmt;
11
12mod ndm_smt;
13pub use ndm_smt::{NdmSmt, NdmSmtError, RandomXCoordGenerator};
14
15use crate::Height;
16
17/// Supported accumulators, with their linked data.
18#[derive(Debug, Serialize, Deserialize)]
19pub enum Accumulator {
20    NdmSmt(ndm_smt::NdmSmt),
21    // TODO add other accumulators..
22}
23
24impl Accumulator {
25    /// Height of the binary tree.
26    pub fn height(&self) -> &Height {
27        match self {
28            Accumulator::NdmSmt(ndm_smt) => ndm_smt.height(),
29        }
30    }
31
32    /// Return the accumulator type.
33    pub fn get_type(&self) -> AccumulatorType {
34        match self {
35            Self::NdmSmt(_) => AccumulatorType::NdmSmt,
36        }
37    }
38
39    #[doc = include_str!("./shared_docs/root_hash.md")]
40    pub fn root_hash(&self) -> &H256 {
41        match self {
42            Self::NdmSmt(ndm_smt) => ndm_smt.root_hash(),
43        }
44    }
45
46    #[doc = include_str!("./shared_docs/root_commitment.md")]
47    pub fn root_commitment(&self) -> &RistrettoPoint {
48        match self {
49            Self::NdmSmt(ndm_smt) => ndm_smt.root_commitment(),
50        }
51    }
52
53    #[doc = include_str!("./shared_docs/root_liability.md")]
54    pub fn root_liability(&self) -> u64 {
55        match self {
56            Self::NdmSmt(ndm_smt) => ndm_smt.root_liability(),
57        }
58    }
59
60    #[doc = include_str!("./shared_docs/root_blinding_factor.md")]
61    pub fn root_blinding_factor(&self) -> &Scalar {
62        match self {
63            Self::NdmSmt(ndm_smt) => ndm_smt.root_blinding_factor(),
64        }
65    }
66}
67
68/// Various supported accumulator types.
69#[derive(Clone, Deserialize, Debug, ValueEnum, PartialEq)]
70#[serde(rename_all = "kebab-case")]
71pub enum AccumulatorType {
72    NdmSmt,
73    // TODO add other accumulators..
74}
75
76impl fmt::Display for AccumulatorType {
77    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78        match self {
79            AccumulatorType::NdmSmt => write!(f, "NDM-SMT"),
80        }
81    }
82}