Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change(nu5): update librustzcash #3832

Closed
wants to merge 2 commits into from
Closed
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
247 changes: 175 additions & 72 deletions Cargo.lock

Large diffs are not rendered by default.

32 changes: 9 additions & 23 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,17 @@ panic = "abort"

[patch.crates-io]

# TODO: replace with upstream orchard (#3056)
orchard = { git = "https://github.com/ZcashFoundation/orchard.git", rev = "568e24cd5f129158375d7ac7d98c89ebff4f982f" }

# TODO: replace with upstream librustzcash when these changes are merged (#3037)
# remove these after a new librustzcash release (#2982)

# Use the ZcashFoundation fork where possible, to avoid duplicate dependencies
equihash = { git = "https://github.com/ZcashFoundation/librustzcash.git", tag = "0.5.1-zebra-v1.0.0-beta.4" }
zcash_note_encryption = { git = "https://github.com/ZcashFoundation/librustzcash.git", tag = "0.5.1-zebra-v1.0.0-beta.4" }
zcash_primitives = { git = "https://github.com/ZcashFoundation/librustzcash.git", tag = "0.5.1-zebra-v1.0.0-beta.4" }
zcash_history = { git = "https://github.com/ZcashFoundation/librustzcash.git", tag = "0.5.1-zebra-v1.0.0-beta.4" }

# This zcash_proofs patch doesn't work, maybe because of features?
#zcash_proofs = { git = "https://github.com/ZcashFoundation/librustzcash.git", tag = "0.5.1-zebra-v1.0.0-beta.4" }

# These are librustzcash git requirements specified in its workspace Cargo.toml,
# that we must replicate here
incrementalmerkletree = { git = "https://github.com/zcash/incrementalmerkletree.git", rev = "b7bd6246122a6e9ace8edb51553fbf5228906cbb" }

# Replaced by the ZcashFoundation fork above
#orchard = { git = "https://github.com/zcash/orchard.git", rev = "2c8241f25b943aa05203eacf9905db117c69bd29" }


# These are librustzcash file requirements specified in its workspace Cargo.toml,
# that we must replace with git requirements
#
# Replaced by the ZcashFoundation fork above
#zcash_note_encryption = { git = "https://github.com/zcash/librustzcash.git", rev = "53d0a51d33a421cb76d3e3124d1e4c1c9036068e" }
equihash = { git = "https://github.com/zcash/librustzcash.git", rev = "ff243b4f0055d89d3abb526e234688a09c3d8cb7" }
hdwallet = { git = "https://github.com/nuttycom/hdwallet", rev = "576683b9f2865f1118c309017ff36e01f84420c9" }
zcash_encoding = { git = "https://github.com/zcash/librustzcash.git", rev = "ff243b4f0055d89d3abb526e234688a09c3d8cb7" }
zcash_note_encryption = { git = "https://github.com/zcash/librustzcash.git", rev = "ff243b4f0055d89d3abb526e234688a09c3d8cb7" }
zcash_primitives = { git = "https://github.com/zcash/librustzcash.git", rev = "ff243b4f0055d89d3abb526e234688a09c3d8cb7" }
# Currently pointing to `download-sprout-params-new` branch which contains the
# changes from `download-sprout-params-new` rebased on top of ff243b4f as
# required by `zcash/Cargo.toml`.
zcash_proofs = { git = "https://github.com/ZcashFoundation/librustzcash.git", rev = "d0e187e64d69d8c6d7df9bf10becec296c23b06f" }
39 changes: 23 additions & 16 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ allow = [

# Certain crates/versions that will be skipped when doing duplicate detection.
skip = [
#{ name = "ansi_term", version = "=0.11.0" },
# wait for zcash_proofs fork be merged back into upstream
# https://github.com/ZcashFoundation/zebra/issues/3831
{ name = "equihash", version = "=0.1.0" },
{ name = "zcash_encoding", version = "=0.0.0" },
{ name = "zcash_note_encryption", version = "=0.1.0" },
{ name = "zcash_primitives", version = "=0.5.0" },
]
# Similarly to `skip` allows you to skip certain crates during duplicate
# detection. Unlike skip, it also includes the entire tree of transitive
Expand All @@ -37,20 +42,29 @@ skip-tree = [
# ticket #3000: tower-fallback dependencies
{ name = "pin-project", version = "=0.4.29" },

# ticket #3063: redjubjub dependencies
{ name = "redjubjub", version = "=0.4.0" },

# ticket #3315: ordered-map dependencies that should be dev-dependencies
{ name = "ordered-map", version = "=0.4.2" },

# ticket #2984: owo-colors dependencies
{ name = "color-eyre", version = "=0.5.11" },

# ticket #2980: inferno and orchard/cryptographic dependencies
{ name = "orchard", version = "=0.0.0" },
# wait for bellman to upgrade
{ name = "blake2s_simd", version = "=0.5.11" },

# wait for halo2 to upgrade
{ name = "blake2b_simd", version = "=0.5.11" },

# wait for zcash_primitives to remove duplicated dependency
{ name = "block-buffer", version = "=0.9.0" },

# upgrade orchard from deprecated `bigint` to `uint`: https://github.com/zcash/orchard/issues/219
{ name = "bigint", version = "=4.4.3" },
# wait for orchard -> bigint to upgrade
{ name = "crunchy", version = "=0.1.6" },

# wait for curve25519-dalek to upgrade
{ name = "digest", version = "=0.9.0" },

# Wait for inferno -> num-format to upgrade
{ name = "arrayvec", version = "=0.4.12" },

# upgrade sentry, metrics-exporter-prometheus, reqwest, hyper,
# which needs #2953: upgrade tracing to the latest major version
Expand All @@ -61,12 +75,6 @@ skip-tree = [

# upgrade abscissa and arti
{ name = "darling", version = "=0.10.2" },

# recent major version bumps
# we should re-check these dependencies in February 2022

# wait for lots of crates in the cryptographic ecosystem to upgrade
{ name = "rand", version = "=0.7.3" },
]

# This section is considered when running `cargo deny check sources`.
Expand All @@ -84,8 +92,7 @@ unknown-git = "deny"
allow-registry = ["https://github.com/rust-lang/crates.io-index"]
# List of URLs for allowed Git repositories
allow-git = [
# ticket #2982: librustzcash and orchard git versions
"https://github.com/str4d/redjubjub",
"https://github.com/nuttycom/hdwallet",
]

[sources.allow-org]
Expand Down
17 changes: 8 additions & 9 deletions zebra-chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ bigint = "4.4.3"
bitvec = "0.22"
bitflags = "1.3.2"
blake2b_simd = "1.0.0"
blake2s_simd = "0.5.11"
blake2s_simd = "1.0.0"
bls12_381 = "0.6.0"
bs58 = { version = "0.4.0", features = ["check"] }
byteorder = "1.4.3"
Expand All @@ -35,9 +35,10 @@ itertools = "0.10.3"
jubjub = "0.8.0"
lazy_static = "1.4.0"
rand_core = "0.6.3"
ripemd160 = "0.9"
ripemd = "0.1.1"
serde = { version = "1.0.136", features = ["serde_derive", "rc"] }
secp256k1 = { version = "0.21.3", features = ["serde"] }
# Matches version used by hdwallet
secp256k1 = { version = "0.20.3", features = ["serde"] }
serde-big-array = "0.3.2"
sha2 = { version = "0.9.9", features=["compress"] }
static_assertions = "1.1.0"
Expand All @@ -46,13 +47,11 @@ thiserror = "1.0.30"
uint = "0.9.3"
x25519-dalek = { version = "1.2.0", features = ["serde"] }

# TODO: replace with upstream orchard (#3056)
orchard = { git = "https://github.com/ZcashFoundation/orchard.git", rev = "568e24cd5f129158375d7ac7d98c89ebff4f982f" }
orchard = "=0.1.0-beta.1"

# TODO: replace with upstream librustzcash when these changes are merged (#3037)
equihash = { git = "https://github.com/ZcashFoundation/librustzcash.git", tag = "0.5.1-zebra-v1.0.0-beta.4" }
zcash_note_encryption = { git = "https://github.com/ZcashFoundation/librustzcash.git", tag = "0.5.1-zebra-v1.0.0-beta.4" }
zcash_primitives = { git = "https://github.com/ZcashFoundation/librustzcash.git", tag = "0.5.1-zebra-v1.0.0-beta.4" }
equihash = "0.1.0"
zcash_note_encryption = "0.1"
zcash_primitives = { version = "0.5", features = ["transparent-inputs"] }
zcash_history = { git = "https://github.com/ZcashFoundation/librustzcash.git", tag = "0.5.1-zebra-v1.0.0-beta.4" }

proptest = { version = "0.10.1", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion zebra-chain/src/primitives/zcash_note_encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn decrypts_successfully(transaction: &Transaction, network: Network, height
.expect("zcash_primitives and Zebra transaction formats must be compatible");

let alt_height = height.0.into();
let null_sapling_ovk = zcash_primitives::sapling::keys::OutgoingViewingKey([0u8; 32]);
let null_sapling_ovk = zcash_primitives::keys::OutgoingViewingKey([0u8; 32]);

if let Some(bundle) = alt_tx.sapling_bundle() {
for output in bundle.shielded_outputs.iter() {
Expand Down
156 changes: 144 additions & 12 deletions zebra-chain/src/primitives/zcash_primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,131 @@ use crate::{
transparent::{self, Script},
};

// Used by boilerplate code below.

#[derive(Clone, Debug)]
struct TransparentAuth<'a> {
all_prev_outputs: &'a [transparent::Output],
}

impl zcash_primitives::transaction::components::transparent::Authorization for TransparentAuth<'_> {
type ScriptSig = zcash_primitives::legacy::Script;
}

// In this block we convert our Output to a librustzcash to TxOut.
// (We could do the serialize/deserialize route but it's simple enough to convert manually)
impl zcash_primitives::transaction::sighash::TransparentAuthorizingContext for TransparentAuth<'_> {
fn input_amounts(&self) -> Vec<zcash_primitives::transaction::components::amount::Amount> {
self.all_prev_outputs
.iter()
.map(|prevout| {
zcash_primitives::transaction::components::amount::Amount::from_nonnegative_i64_le_bytes(
prevout.value.to_bytes(),
).expect("will not fail since it was previously validated")
})
.collect()
}

fn input_scriptpubkeys(&self) -> Vec<zcash_primitives::legacy::Script> {
self.all_prev_outputs
.iter()
.map(|prevout| {
zcash_primitives::legacy::Script(prevout.lock_script.as_raw_bytes().into())
})
.collect()
}
}

// Boilerplate mostly copied from `zcash/src/rust/src/transaction_ffi.rs` which is required
// to compute sighash.
// TODO: remove/change if they improve the API to not require this.

struct MapTransparent<'a> {
auth: TransparentAuth<'a>,
}

impl<'a>
zcash_primitives::transaction::components::transparent::MapAuth<
zcash_primitives::transaction::components::transparent::Authorized,
TransparentAuth<'a>,
> for MapTransparent<'a>
{
fn map_script_sig(
&self,
s: <zcash_primitives::transaction::components::transparent::Authorized as zcash_primitives::transaction::components::transparent::Authorization>::ScriptSig,
) -> <TransparentAuth as zcash_primitives::transaction::components::transparent::Authorization>::ScriptSig{
s
}

fn map_authorization(
&self,
_: zcash_primitives::transaction::components::transparent::Authorized,
) -> TransparentAuth<'a> {
// TODO: This map should consume self, so we can move self.auth
self.auth.clone()
}
}

struct IdentityMap;

impl
zcash_primitives::transaction::components::sapling::MapAuth<
zcash_primitives::transaction::components::sapling::Authorized,
zcash_primitives::transaction::components::sapling::Authorized,
> for IdentityMap
{
fn map_proof(
&self,
p: <zcash_primitives::transaction::components::sapling::Authorized as zcash_primitives::transaction::components::sapling::Authorization>::Proof,
) -> <zcash_primitives::transaction::components::sapling::Authorized as zcash_primitives::transaction::components::sapling::Authorization>::Proof{
p
}

fn map_auth_sig(
&self,
s: <zcash_primitives::transaction::components::sapling::Authorized as zcash_primitives::transaction::components::sapling::Authorization>::AuthSig,
) -> <zcash_primitives::transaction::components::sapling::Authorized as zcash_primitives::transaction::components::sapling::Authorization>::AuthSig{
s
}

fn map_authorization(
&self,
a: zcash_primitives::transaction::components::sapling::Authorized,
) -> zcash_primitives::transaction::components::sapling::Authorized {
a
}
}

impl
zcash_primitives::transaction::components::orchard::MapAuth<
orchard::bundle::Authorized,
orchard::bundle::Authorized,
> for IdentityMap
{
fn map_spend_auth(
&self,
s: <orchard::bundle::Authorized as orchard::bundle::Authorization>::SpendAuth,
) -> <orchard::bundle::Authorized as orchard::bundle::Authorization>::SpendAuth {
s
}

fn map_authorization(&self, a: orchard::bundle::Authorized) -> orchard::bundle::Authorized {
a
}
}

struct PrecomputedAuth<'a> {
_phantom: std::marker::PhantomData<&'a ()>,
}

impl<'a> zcash_primitives::transaction::Authorization for PrecomputedAuth<'a> {
type TransparentAuth = TransparentAuth<'a>;
type SaplingAuth = zcash_primitives::transaction::components::sapling::Authorized;
type OrchardAuth = orchard::bundle::Authorized;
}

// End of (mostly) copied code

impl TryFrom<&Transaction> for zcash_primitives::transaction::Transaction {
type Error = io::Error;

Expand Down Expand Up @@ -98,28 +223,35 @@ pub(crate) fn sighash(
Some(input_index) => {
let output = all_previous_outputs[input_index].clone();
script = (&output.lock_script).into();
zcash_primitives::transaction::sighash::SignableInput::Transparent(
zcash_primitives::transaction::sighash::TransparentInput::new(
input_index,
&script,
output
.value
.try_into()
.expect("amount was previously validated"),
),
)
zcash_primitives::transaction::sighash::SignableInput::Transparent {
hash_type: hash_type.bits() as _,
index: input_index,
script_code: &script,
script_pubkey: &script,
value: output
.value
.try_into()
.expect("amount was previously validated"),
}
}
None => zcash_primitives::transaction::sighash::SignableInput::Shielded,
};

let txid_parts = alt_tx
.deref()
.digest(zcash_primitives::transaction::txid::TxIdDigester);
let f_transparent = MapTransparent {
auth: TransparentAuth {
all_prev_outputs: all_previous_outputs,
},
};
let txdata: zcash_primitives::transaction::TransactionData<PrecomputedAuth> = alt_tx
.into_data()
.map_authorization(f_transparent, IdentityMap, IdentityMap);

SigHash(
*zcash_primitives::transaction::sighash::signature_hash(
alt_tx.deref(),
hash_type.bits(),
&txdata,
&signable_input,
&txid_parts,
)
Expand Down
Loading