Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 73 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions crates/contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ keywords = ["simplicity", "liquid", "bitcoin", "elements", "contracts"]
categories = ["cryptography::cryptocurrencies"]

[features]
default = ["sdk-basic", "finance-options", "finance-dcd", "finance-option-offer", "simple-storage", "bytes32-tr-storage", "array-tr-storage"]
default = ["sdk-basic", "finance-options", "finance-dcd", "finance-option-offer", "simple-storage", "bytes32-tr-storage", "array-tr-storage", "smt-storage"]
sdk-basic = []
finance-options = []
finance-dcd = []
finance-option-offer = []
simple-storage = []
bytes32-tr-storage = []
array-tr-storage = []
smt-storage = []
swap-with-change = []

[lints]
workspace = true
Expand All @@ -37,4 +39,5 @@ simplicityhl = { workspace = true }
simplicityhl-core = { workspace = true }

[dev-dependencies]
anyhow = "1"
anyhow = "1"
rand = "0.9.2"
3 changes: 2 additions & 1 deletion crates/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ pub mod bytes32_tr_storage;
pub mod finance;
#[cfg(feature = "simple-storage")]
pub mod simple_storage;

#[cfg(feature = "smt-storage")]
pub mod smt_storage;
#[cfg(feature = "finance-dcd")]
pub use finance::dcd;
#[cfg(feature = "finance-option-offer")]
Expand Down
66 changes: 66 additions & 0 deletions crates/contracts/src/smt_storage/build_witness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::collections::HashMap;

use simplicityhl::num::U256;
use simplicityhl::types::{ResolvedType, TypeConstructible, UIntType};
use simplicityhl::value::{UIntValue, ValueConstructible};
use simplicityhl::{WitnessValues, str::WitnessName};

#[allow(non_camel_case_types)]
pub type u256 = [u8; 32];
pub const DEPTH: usize = 7;

#[derive(Debug, Clone, bincode::Encode, bincode::Decode, PartialEq, Eq)]
pub struct SMTWitness {
leaf: u256,
merkle_data: [(u256, bool); DEPTH],
}

impl SMTWitness {
#[must_use]
pub fn new(leaf: &u256, merkle_data: &[(u256, bool); DEPTH]) -> Self {
Self {
leaf: *leaf,
merkle_data: *merkle_data,
}
}
}

impl Default for SMTWitness {
fn default() -> Self {
Self {
leaf: [0u8; 32],
merkle_data: [([0u8; 32], false); DEPTH],
}
}
}

#[must_use]
pub fn build_smt_storage_witness(witness: &SMTWitness) -> WitnessValues {
let values: Vec<simplicityhl::Value> = witness
.merkle_data
.iter()
.map(|(value, is_right)| {
let hash_val =
simplicityhl::Value::from(UIntValue::U256(U256::from_byte_array(*value)));
let direction_val = simplicityhl::Value::from(*is_right);

simplicityhl::Value::product(hash_val, direction_val)
})
.collect();

let element_type = simplicityhl::types::TypeConstructible::product(
UIntType::U256.into(),
ResolvedType::boolean(),
);

simplicityhl::WitnessValues::from(HashMap::from([
(
WitnessName::from_str_unchecked("LEAF"),
simplicityhl::Value::from(UIntValue::U256(U256::from_byte_array(witness.leaf))),
),
(
WitnessName::from_str_unchecked("MERKLE_DATA"),
simplicityhl::Value::array(values, element_type),
),
]))
}
Loading