stark_evm_adapter/
merkle_statement.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::sync::Arc;

use ethers::{
    contract::abigen,
    core::k256::ecdsa::SigningKey,
    middleware::SignerMiddleware,
    providers::{Http, Provider},
    signers::Wallet,
    types::{Address, U256},
};
use serde::{Deserialize, Serialize};

use crate::ContractFunctionCall;

/// Decommitment for a merkle statement
#[derive(Serialize, Deserialize, Debug)]
pub struct MerkleStatement {
    expected_root: U256,
    n_unique_queries: usize,
    merkle_height: usize,
    merkle_queue_indices: Vec<U256>,
    merkle_queue_values: Vec<U256>,
    proof: Vec<U256>,
}

abigen!(
    MerkleStatementContract,
    r#"[
        function verifyMerkle(uint256[] proof,uint256[] merkle_queue,uint256 merkle_height,uint256 expected_root)
    ]"#,
    derives(serde::Deserialize, serde::Serialize)
);

impl MerkleStatement {
    pub fn new(
        expected_root: U256,
        n_unique_queries: usize,
        merkle_height: usize,
        merkle_queue_indices: Vec<U256>,
        merkle_queue_values: Vec<U256>,
        proof: Vec<U256>,
    ) -> MerkleStatement {
        MerkleStatement {
            expected_root,
            n_unique_queries,
            merkle_height,
            merkle_queue_indices,
            merkle_queue_values,
            proof,
        }
    }

    /// Constructs the merkle_queue by interleaving indices and values.
    fn merkle_queue(&self) -> Vec<U256> {
        self.merkle_queue_indices
            .iter()
            .zip(self.merkle_queue_values.iter())
            .flat_map(|(&index, &value)| vec![index, value])
            .collect()
    }

    /// Constructs `verifyMerkle` contract function call.
    pub fn contract_function_call(&self) -> VerifyMerkleCall {
        VerifyMerkleCall {
            proof: self.proof.clone(),
            merkle_queue: self.merkle_queue(),
            merkle_height: U256::from(self.merkle_height),
            expected_root: self.expected_root,
        }
    }

    /// Initiates `verifyMerkle` contract call.
    pub fn verify(
        &self,
        address: Address,
        signer: Arc<SignerMiddleware<Provider<Http>, Wallet<SigningKey>>>,
    ) -> ContractFunctionCall {
        let contract = MerkleStatementContract::new(address, signer);

        let verify_merkle_call = self.contract_function_call();
        contract.method("verifyMerkle", verify_merkle_call).unwrap()
    }
}