drive/verify/shielded/verify_nullifiers_branch_query/
mod.rs

1//! Module for verifying nullifiers branch chunk queries.
2
3mod v0;
4
5use crate::drive::Drive;
6use crate::error::drive::DriveError;
7use crate::error::Error;
8use grovedb::GroveBranchQueryResult;
9use platform_version::version::PlatformVersion;
10
11impl Drive {
12    /// Verifies a branch chunk proof for the nullifiers tree of a shielded pool.
13    ///
14    /// # Arguments
15    /// - `proof`: A byte slice containing the serialized branch chunk proof.
16    /// - `pool_type`: The shielded pool type (0 = credit, 1 = main token, 2 = individual token).
17    /// - `pool_identifier`: Optional 32-byte identifier for individual token pools.
18    /// - `key`: The key that was navigated to in the tree.
19    /// - `depth`: The depth of the branch that was returned.
20    /// - `expected_root_hash`: The expected root hash from the parent trunk/branch proof.
21    /// - `platform_version`: A reference to the platform version.
22    ///
23    /// # Returns
24    /// - `Ok(GroveBranchQueryResult)`: The verified branch query result.
25    /// - `Err(Error)`: If verification fails.
26    pub fn verify_nullifiers_branch_query(
27        proof: &[u8],
28        pool_type: u32,
29        pool_identifier: Option<&[u8]>,
30        key: Vec<u8>,
31        depth: u8,
32        expected_root_hash: [u8; 32],
33        platform_version: &PlatformVersion,
34    ) -> Result<GroveBranchQueryResult, Error> {
35        match platform_version
36            .drive
37            .methods
38            .verify
39            .shielded
40            .verify_nullifiers_branch_query
41        {
42            0 => Self::verify_nullifiers_branch_query_v0(
43                proof,
44                pool_type,
45                pool_identifier,
46                key,
47                depth,
48                expected_root_hash,
49                platform_version,
50            ),
51            version => Err(Error::Drive(DriveError::UnknownVersionMismatch {
52                method: "verify_nullifiers_branch_query".to_string(),
53                known_versions: vec![0],
54                received: version,
55            })),
56        }
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63    use platform_version::version::PlatformVersion;
64
65    #[test]
66    fn test_verify_nullifiers_branch_query_unknown_version_mismatch() {
67        let mut platform_version = PlatformVersion::latest().clone();
68        platform_version
69            .drive
70            .methods
71            .verify
72            .shielded
73            .verify_nullifiers_branch_query = 255;
74
75        let result = Drive::verify_nullifiers_branch_query(
76            &[],
77            0,
78            None,
79            vec![],
80            0,
81            [0u8; 32],
82            &platform_version,
83        );
84
85        assert!(
86            matches!(
87                result,
88                Err(Error::Drive(DriveError::UnknownVersionMismatch { .. }))
89            ),
90            "expected UnknownVersionMismatch, got {:?}",
91            result,
92        );
93    }
94}