darkfi_sdk/wasm/merkle.rs
1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use darkfi_serial::Encodable;
20
21use crate::{
22 crypto::MerkleNode,
23 error::{ContractError, GenericResult},
24 pasta::pallas,
25 wasm::db::DbHandle,
26};
27
28/// Add given elements into a Merkle tree. Used for inclusion proofs.
29///
30/// * `db_info` is a handle for a database where the Merkle tree is stored.
31/// * `db_roots` is a handle for a database where all the new Merkle roots are stored.
32/// * `root_key` is the serialized key pointing to the latest Merkle root in `db_info`
33/// * `tree_key` is the serialized key pointing to the Merkle tree in `db_info`.
34/// * `elements` are the items we want to add to the Merkle tree.
35///
36/// There are 2 databases:
37///
38/// * `db_info` stores general metadata or info.
39/// * `db_roots` stores a log of all the merkle roots.
40///
41/// Inside `db_info` we store:
42///
43/// * The [latest root hash:32] under `root_key`.
44/// * The incremental merkle tree under `tree_key`.
45///
46/// Inside `db_roots` we store:
47///
48/// * All [merkle root:32]s as keys. The value is the current [tx_hash:32][call_idx:1].
49/// If no new values are added, then the root key is updated to the current (tx_hash, call_idx).
50pub fn merkle_add(
51 db_info: DbHandle,
52 db_roots: DbHandle,
53 root_key: &[u8],
54 tree_key: &[u8],
55 elements: &[MerkleNode],
56) -> GenericResult<()> {
57 let mut buf = vec![];
58 let mut len = 0;
59 len += db_info.encode(&mut buf)?;
60 len += db_roots.encode(&mut buf)?;
61 len += root_key.to_vec().encode(&mut buf)?;
62 len += tree_key.to_vec().encode(&mut buf)?;
63 len += elements.to_vec().encode(&mut buf)?;
64
65 match unsafe { merkle_add_(buf.as_ptr(), len as u32) } {
66 0 => Ok(()),
67 -1 => Err(ContractError::CallerAccessDenied),
68 -2 => Err(ContractError::DbSetFailed),
69 _ => unreachable!(),
70 }
71}
72
73/// Add given elements into a sparse Merkle tree. Used for exclusion proofs.
74///
75/// * `db_info` is a handle for a database where the latest root is stored.
76/// * `db_smt` is a handle for a database where all the actual tree is stored.
77/// * `db_roots` is a handle for a database where all the new roots are stored.
78/// * `root_key` is the serialized key pointing to the latest Merkle root in `db_info`
79/// * `elements` are the items we want to add to the tree.
80///
81/// There are 2 databases:
82///
83/// * `db_info` stores general metadata or info.
84/// * `db_roots` stores a log of all the merkle roots.
85///
86/// Inside `db_info` we store:
87///
88/// * The [latest root hash:32] under `root_key`.
89///
90/// Inside `db_roots` we store:
91///
92/// * All [merkle root:32]s as keys. The value is the current [tx_hash:32][call_idx:1].
93/// If no new values are added, then the root key is updated to the current (tx_hash, call_idx).
94pub fn sparse_merkle_insert_batch(
95 db_info: DbHandle,
96 db_smt: DbHandle,
97 db_roots: DbHandle,
98 root_key: &[u8],
99 elements: &[pallas::Base],
100) -> GenericResult<()> {
101 let mut buf = vec![];
102 let mut len = 0;
103 len += db_info.encode(&mut buf)?;
104 len += db_smt.encode(&mut buf)?;
105 len += db_roots.encode(&mut buf)?;
106 len += root_key.to_vec().encode(&mut buf)?;
107 len += elements.to_vec().encode(&mut buf)?;
108
109 match unsafe { sparse_merkle_insert_batch_(buf.as_ptr(), len as u32) } {
110 0 => Ok(()),
111 -1 => Err(ContractError::CallerAccessDenied),
112 -2 => Err(ContractError::DbSetFailed),
113 _ => unreachable!(),
114 }
115}
116
117extern "C" {
118 fn merkle_add_(ptr: *const u8, len: u32) -> i64;
119 fn sparse_merkle_insert_batch_(ptr: *const u8, len: u32) -> i64;
120}