eq_common/
lib.rs

1use celestia_types::{
2    nmt::{Namespace, NamespaceProof},
3    RowProof,
4};
5use serde::{Deserialize, Serialize};
6
7#[cfg(feature = "host")]
8mod error;
9#[cfg(feature = "host")]
10pub use error::InclusionServiceError;
11
12#[cfg(feature = "grpc")]
13/// gRPC generated bindings
14pub mod eqs {
15    include!("generated/eqs.rs");
16}
17
18/*
19    The types of proofs we expect to support:
20    1. KeccakInclusionToDataRootProof
21    2. KeccakInclusionToBlockHashProof
22    3. PayyPoseidonToDataRootProof
23    4. PayyPoseidonToBlockHashProof
24*/
25
26#[derive(Serialize, Deserialize, Clone, Debug)]
27pub struct KeccakInclusionToDataRootProofInput {
28    #[serde(rename = "blob_data")]
29    pub data: Vec<u8>,
30
31    #[serde(rename = "blob_namespace")]
32    pub namespace_id: Namespace,
33
34    #[serde(rename = "nmt_multiproofs")]
35    pub share_proofs: Vec<NamespaceProof>,
36
37    #[serde(rename = "row_root_multiproof")]
38    pub row_proof: RowProof,
39
40    pub data_root: [u8; 32],   // already matches
41    pub keccak_hash: [u8; 32], // already matches
42}
43
44/// Expecting bytes:
45/// (keccak_hash: [u8; 32], pub data_root: [u8; 32])
46pub struct KeccakInclusionToDataRootProofOutput {
47    pub keccak_hash: [u8; 32],
48    pub data_root: [u8; 32],
49}
50impl KeccakInclusionToDataRootProofOutput {
51    // Simple encoding, rather than use any Ethereum libraries
52    pub fn to_vec(&self) -> Vec<u8> {
53        let mut encoded = Vec::new();
54        encoded.extend_from_slice(&self.keccak_hash);
55        encoded.extend_from_slice(&self.data_root);
56        encoded
57    }
58
59    #[cfg(feature = "host")]
60    pub fn from_bytes(data: &[u8]) -> Result<Self, InclusionServiceError> {
61        if data.len() != 64 {
62            return Err(InclusionServiceError::OutputDeserializationError);
63        }
64        let decoded = KeccakInclusionToDataRootProofOutput {
65            keccak_hash: data[0..32]
66                .try_into()
67                .map_err(|_| InclusionServiceError::OutputDeserializationError)?,
68            data_root: data[32..64]
69                .try_into()
70                .map_err(|_| InclusionServiceError::OutputDeserializationError)?,
71        };
72        Ok(decoded)
73    }
74}
75
76#[cfg(test)]
77mod test {
78    use super::*;
79
80    #[test]
81    #[cfg(feature = "host")]
82    fn test_abi_encoding() {
83        let output = KeccakInclusionToDataRootProofOutput {
84            keccak_hash: [0; 32],
85            data_root: [0; 32],
86        };
87        let encoded = output.to_vec();
88        let decoded = KeccakInclusionToDataRootProofOutput::from_bytes(&encoded).unwrap();
89        assert_eq!(output.keccak_hash, decoded.keccak_hash);
90        assert_eq!(output.data_root, decoded.data_root);
91    }
92}