From 2e9e6d8831cda2507d5fd893aea2b040ac11a813 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Mon, 28 Oct 2024 11:59:07 -0500 Subject: [PATCH] feat: allow combining empty Orchard leaves --- librustzcash-wrapper/src/lib.rs | 40 +++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/librustzcash-wrapper/src/lib.rs b/librustzcash-wrapper/src/lib.rs index e7b5d04..df054c6 100644 --- a/librustzcash-wrapper/src/lib.rs +++ b/librustzcash-wrapper/src/lib.rs @@ -1590,26 +1590,32 @@ pub extern "C" fn rust_wrapper_combine_orchard_nodes( ){ let left_in: Vec = marshall_from_haskell_var(left, left_len, RW); let right_in: Vec = marshall_from_haskell_var(right, right_len, RW); - let left_node = MerkleHashOrchard::from_bytes(&to_array(left_in)); - if left_node.is_some().into() { - if right_in.len() > 1 { - let right_node = MerkleHashOrchard::from_bytes(&to_array(right_in)); - if right_node.is_some().into() { - let n = MerkleHashOrchard::combine(Level::new(level), &left_node.unwrap(), &right_node.unwrap()); - let h = Hhex { bytes: n.to_bytes().to_vec() }; - marshall_to_haskell_var(&h, out, out_len, RW); - } else { - let h0 = Hhex { bytes: vec![0] }; - marshall_to_haskell_var(&h0, out, out_len, RW); - } - } else { - let n = MerkleHashOrchard::combine(Level::new(level), &left_node.unwrap(), &MerkleHashOrchard::empty_leaf()); + if left_in.len() > 1 { + let n = MerkleHashOrchard::combine(Level::new(level), &MerkleHashOrchard::empty_leaf(), &MerkleHashOrchard::empty_leaf()); let h = Hhex { bytes: n.to_bytes().to_vec() }; marshall_to_haskell_var(&h, out, out_len, RW); - } } else { - let h0 = Hhex { bytes: vec![0] }; - marshall_to_haskell_var(&h0, out, out_len, RW); + let left_node = MerkleHashOrchard::from_bytes(&to_array(left_in)); + if left_node.is_some().into() { + if right_in.len() > 1 { + let right_node = MerkleHashOrchard::from_bytes(&to_array(right_in)); + if right_node.is_some().into() { + let n = MerkleHashOrchard::combine(Level::new(level), &left_node.unwrap(), &right_node.unwrap()); + let h = Hhex { bytes: n.to_bytes().to_vec() }; + marshall_to_haskell_var(&h, out, out_len, RW); + } else { + let h0 = Hhex { bytes: vec![0] }; + marshall_to_haskell_var(&h0, out, out_len, RW); + } + } else { + let n = MerkleHashOrchard::combine(Level::new(level), &left_node.unwrap(), &MerkleHashOrchard::empty_leaf()); + let h = Hhex { bytes: n.to_bytes().to_vec() }; + marshall_to_haskell_var(&h, out, out_len, RW); + } + } else { + let h0 = Hhex { bytes: vec![0] }; + marshall_to_haskell_var(&h0, out, out_len, RW); + } } }