Functions to create and manage Orchard commitment trees #99

Merged
pitmutt merged 30 commits from rav001 into milestone2 2024-11-05 18:28:46 +00:00
3 changed files with 18 additions and 9 deletions
Showing only changes of commit 1938e162f4 - Show all commits

View file

@ -1626,13 +1626,14 @@ pub extern "C" fn rust_wrapper_get_orchard_root(
out_len: &mut usize out_len: &mut usize
){ ){
let tree: CommitmentTree<MerkleHashOrchard, 32> = CommitmentTree::empty(); let tree: CommitmentTree<MerkleHashOrchard, 32> = CommitmentTree::empty();
let root = tree.root_at_depth(level, PathFiller::empty()); let root = tree.root();
let h = Hhex { bytes: root.to_bytes().to_vec() }; let h = Hhex { bytes: root.to_bytes().to_vec() };
marshall_to_haskell_var(&h, out, out_len, RW); marshall_to_haskell_var(&h, out, out_len, RW);
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn rust_wrapper_orchard_add_node( pub extern "C" fn rust_wrapper_orchard_add_node(
level: u8,
node: *const u8, node: *const u8,
node_len: usize, node_len: usize,
out: *mut u8, out: *mut u8,
@ -1643,10 +1644,17 @@ pub extern "C" fn rust_wrapper_orchard_add_node(
let orchard_note_comm = ExtractedNoteCommitment::from_bytes(&to_array(node_in)); let orchard_note_comm = ExtractedNoteCommitment::from_bytes(&to_array(node_in));
if orchard_note_comm.is_some().into() { if orchard_note_comm.is_some().into() {
let n = MerkleHashOrchard::from_cmx(&orchard_note_comm.unwrap()); let n = MerkleHashOrchard::from_cmx(&orchard_note_comm.unwrap());
tree.append(n); match tree.append(n) {
let root = tree.root(); Ok(()) => {
let root = tree.root_at_depth(level, PathFiller::empty());
let h = Hhex { bytes: root.to_bytes().to_vec() }; let h = Hhex { bytes: root.to_bytes().to_vec() };
marshall_to_haskell_var(&h, out, out_len, RW); marshall_to_haskell_var(&h, out, out_len, RW);
},
Err(_) => {
let h0 = Hhex { bytes: vec![0] };
marshall_to_haskell_var(&h0, out, out_len, RW);
}
}
} else { } else {
let h0 = Hhex { bytes: vec![0] }; let h0 = Hhex { bytes: vec![0] };
marshall_to_haskell_var(&h0, out, out_len, RW); marshall_to_haskell_var(&h0, out, out_len, RW);

View file

@ -312,7 +312,8 @@ import ZcashHaskell.Types
#} #}
{# fun unsafe rust_wrapper_orchard_add_node as rustWrapperOrchardAddNodeTest {# fun unsafe rust_wrapper_orchard_add_node as rustWrapperOrchardAddNodeTest
{ toBorshVar* `BS.ByteString'& { `Int8'
, toBorshVar* `BS.ByteString'&
, getVarBuffer `Buffer HexString'& , getVarBuffer `Buffer HexString'&
} }
-> `()' -> `()'

View file

@ -224,9 +224,9 @@ getOrchardRootTest :: Int -> HexString
getOrchardRootTest level = getOrchardRootTest level =
withPureBorshVarBuffer $ rustWrapperGetOrchardRootTest $ fromIntegral level withPureBorshVarBuffer $ rustWrapperGetOrchardRootTest $ fromIntegral level
addOrchardNodeGetRoot :: BS.ByteString -> HexString addOrchardNodeGetRoot :: Int -> BS.ByteString -> HexString
addOrchardNodeGetRoot n = addOrchardNodeGetRoot l n =
withPureBorshVarBuffer $ rustWrapperOrchardAddNodeTest n withPureBorshVarBuffer $ rustWrapperOrchardAddNodeTest (fromIntegral l) n
-- | Update a Orchard commitment tree -- | Update a Orchard commitment tree
updateOrchardCommitmentTree :: updateOrchardCommitmentTree ::