Skip to content

Commit

Permalink
Auto merge of zcash#5251 - nuttycom:manual_incrementalmerkletree_ser,…
Browse files Browse the repository at this point in the history
… r=str4d

Use manual serialization for Merkle frontiers rather than bincode.
  • Loading branch information
zkbot committed Aug 24, 2021
2 parents 4e93c62 + b8e86dd commit a496afd
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 58 deletions.
111 changes: 82 additions & 29 deletions Cargo.lock

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

15 changes: 7 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ crate-type = ["staticlib"]

[dependencies]
bellman = "0.10"
bincode = "1.3"
blake2b_simd = "0.5"
blake2s_simd = "0.5"
bls12_381 = "0.5"
Expand Down Expand Up @@ -65,11 +64,11 @@ codegen-units = 1
[patch.crates-io]
ed25519-zebra = { git = "https://github.com/ZcashFoundation/ed25519-zebra.git", rev = "d3512400227a362d08367088ffaa9bd4142a69c7" }
halo2 = { git = "https://github.com/zcash/halo2.git", rev = "27c4187673a9c6ade13fbdbd4f20955530c22d7f" }
incrementalmerkletree = { git = "https://github.com/zcash/incrementalmerkletree", rev = "8b59049f1746827ffa3763efa8af948f680491d0" }
incrementalmerkletree = { git = "https://github.com/zcash/incrementalmerkletree", rev = "b7bd6246122a6e9ace8edb51553fbf5228906cbb" }
jubjub = { git = "https://github.com/zkcrypto/jubjub.git", rev = "96ab4162b83303378eae32a326b54d88b75bffc2" }
orchard = { git = "https://github.com/zcash/orchard.git", rev = "8454f86d423edbf0b53a1d5d32df1c691f8b7188" }
zcash_address = { git = "https://github.com/zcash/librustzcash.git", rev = "98dc330cdb1c96e980e62b5bc901edbaa128fea2" }
zcash_history = { git = "https://github.com/zcash/librustzcash.git", rev = "98dc330cdb1c96e980e62b5bc901edbaa128fea2" }
zcash_note_encryption = { git = "https://github.com/zcash/librustzcash.git", rev = "98dc330cdb1c96e980e62b5bc901edbaa128fea2" }
zcash_primitives = { git = "https://github.com/zcash/librustzcash.git", rev = "98dc330cdb1c96e980e62b5bc901edbaa128fea2" }
zcash_proofs = { git = "https://github.com/zcash/librustzcash.git", rev = "98dc330cdb1c96e980e62b5bc901edbaa128fea2" }
orchard = { git = "https://github.com/zcash/orchard.git", rev = "d0baa18fc6105df4a7847de2b6dc50c5919b3123" }
zcash_address = { git = "https://github.com/zcash/librustzcash.git", rev = "19a97f16945c68c33aedcc89f2a4f4d398658b05" }
zcash_history = { git = "https://github.com/zcash/librustzcash.git", rev = "19a97f16945c68c33aedcc89f2a4f4d398658b05" }
zcash_note_encryption = { git = "https://github.com/zcash/librustzcash.git", rev = "19a97f16945c68c33aedcc89f2a4f4d398658b05" }
zcash_primitives = { git = "https://github.com/zcash/librustzcash.git", rev = "19a97f16945c68c33aedcc89f2a4f4d398658b05" }
zcash_proofs = { git = "https://github.com/zcash/librustzcash.git", rev = "19a97f16945c68c33aedcc89f2a4f4d398658b05" }
4 changes: 2 additions & 2 deletions src/rust/src/address_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl FromAddress for UnifiedAddressHelper {
}

impl UnifiedAddressHelper {
fn to_cpp(
fn into_cpp(
self,
network: Network,
ua_obj: Option<UnifiedAddressObj>,
Expand Down Expand Up @@ -145,7 +145,7 @@ pub extern "C" fn zcash_address_parse_unified(
}
};

ua.to_cpp(network, ua_obj, sapling_cb, p2sh_cb, p2pkh_cb, unknown_cb)
ua.into_cpp(network, ua_obj, sapling_cb, p2sh_cb, p2pkh_cb, unknown_cb)
}

#[no_mangle]
Expand Down
1 change: 1 addition & 0 deletions src/rust/src/history_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub extern "system" fn librustzcash_mmr_append(
)
}

#[allow(clippy::too_many_arguments)]
fn librustzcash_mmr_append_inner<V: Version>(
// Consensus branch id
cbranch: u32,
Expand Down
25 changes: 14 additions & 11 deletions src/rust/src/orchard_ffi/incremental_sinsemilla_tree_ffi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bincode;
use incrementalmerkletree::{
bridgetree::{self, BridgeTree},
Altitude, Frontier, Hashable, Tree,
Expand All @@ -8,7 +7,10 @@ use std::ptr;

use orchard::{bundle::Authorized, tree::MerkleCrhOrchardOutput};

use zcash_primitives::transaction::components::Amount;
use zcash_primitives::{
merkle_tree::incremental::{read_frontier_v1, read_tree, write_frontier_v1, write_tree},
transaction::components::Amount,
};

use crate::orchard_ffi::{error, CppStreamReader, CppStreamWriter, ReadCb, StreamObj, WriteCb};

Expand All @@ -22,7 +24,7 @@ pub const MAX_CHECKPOINTS: usize = 100;
#[no_mangle]
pub extern "C" fn orchard_merkle_frontier_empty(
) -> *mut bridgetree::Frontier<MerkleCrhOrchardOutput, MERKLE_DEPTH> {
let empty_tree = bridgetree::Frontier::<MerkleCrhOrchardOutput, MERKLE_DEPTH>::new();
let empty_tree = bridgetree::Frontier::<MerkleCrhOrchardOutput, MERKLE_DEPTH>::empty();
Box::into_raw(Box::new(empty_tree))
}

Expand Down Expand Up @@ -51,7 +53,7 @@ pub extern "C" fn orchard_merkle_frontier_parse(
) -> *mut bridgetree::Frontier<MerkleCrhOrchardOutput, MERKLE_DEPTH> {
let reader = CppStreamReader::from_raw_parts(stream, read_cb.unwrap());

match bincode::deserialize_from(reader) {
match read_frontier_v1(reader) {
Ok(parsed) => Box::into_raw(Box::new(parsed)),
Err(e) => {
error!("Failed to parse Orchard bundle: {}", e);
Expand All @@ -62,17 +64,18 @@ pub extern "C" fn orchard_merkle_frontier_parse(

#[no_mangle]
pub extern "C" fn orchard_merkle_frontier_serialize(
tree: *const bridgetree::Frontier<MerkleCrhOrchardOutput, MERKLE_DEPTH>,
frontier: *const bridgetree::Frontier<MerkleCrhOrchardOutput, MERKLE_DEPTH>,
stream: Option<StreamObj>,
write_cb: Option<WriteCb>,
) -> bool {
let tree = unsafe {
tree.as_ref()
let frontier = unsafe {
frontier
.as_ref()
.expect("Orchard note commitment tree pointer may not be null.")
};

let writer = CppStreamWriter::from_raw_parts(stream, write_cb.unwrap());
match bincode::serialize_into(writer, tree) {
match write_frontier_v1(writer, frontier) {
Ok(()) => true,
Err(e) => {
error!("{}", e);
Expand Down Expand Up @@ -130,7 +133,7 @@ pub extern "C" fn orchard_merkle_frontier_num_leaves(
.expect("Orchard note commitment tree pointer may not be null.")
};

tree.position().map_or(0, |p| <usize>::from(p) + 1)
tree.position().map_or(0, |p| (<u64>::from(p) + 1) as usize)
}

#[no_mangle]
Expand Down Expand Up @@ -182,7 +185,7 @@ pub extern "C" fn incremental_sinsemilla_tree_parse(
) -> *mut BridgeTree<MerkleCrhOrchardOutput, MERKLE_DEPTH> {
let reader = CppStreamReader::from_raw_parts(stream, read_cb.unwrap());

match bincode::deserialize_from(reader) {
match read_tree(reader) {
Ok(parsed) => Box::into_raw(Box::new(parsed)),
Err(e) => {
error!("Failed to parse Orchard bundle: {}", e);
Expand All @@ -203,7 +206,7 @@ pub extern "C" fn incremental_sinsemilla_tree_serialize(
};

let writer = CppStreamWriter::from_raw_parts(stream, write_cb.unwrap());
match bincode::serialize_into(writer, tree) {
match write_tree(writer, tree) {
Ok(()) => true,
Err(e) => {
error!("{}", e);
Expand Down
Loading

0 comments on commit a496afd

Please sign in to comment.