rust: Migrate to latest `zcash_primitives` revision

This commit is contained in:
Jack Grigg 2022-09-02 01:37:40 +00:00
parent 8930cf44d3
commit b05b1dafa6
10 changed files with 81 additions and 73 deletions

View File

@ -4,5 +4,10 @@ linker = "aarch64-linux-gnu-gcc"
[source.crates-io]
replace-with = "vendored-sources"
[source."https://github.com/zcash/librustzcash.git"]
git = "https://github.com/zcash/librustzcash.git"
rev = "e5dad037e9ac091d75dbc4e962f9e34e100f0c67"
replace-with = "vendored-sources"
[source.vendored-sources]
# The directory for this source is set to RUST_VENDORED_SOURCES by src/Makefile.am

25
Cargo.lock generated
View File

@ -539,8 +539,7 @@ checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "equihash"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab579d7cf78477773b03e80bc2f89702ef02d7112c711d54ca93dcdce68533d5"
source = "git+https://github.com/zcash/librustzcash.git?rev=e5dad037e9ac091d75dbc4e962f9e34e100f0c67#e5dad037e9ac091d75dbc4e962f9e34e100f0c67"
dependencies = [
"blake2b_simd",
"byteorder",
@ -549,8 +548,7 @@ dependencies = [
[[package]]
name = "f4jumble"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a83e8d7fd0c526af4aad893b7c9fe41e2699ed8a776a6c74aecdeafe05afc75"
source = "git+https://github.com/zcash/librustzcash.git?rev=e5dad037e9ac091d75dbc4e962f9e34e100f0c67#e5dad037e9ac091d75dbc4e962f9e34e100f0c67"
dependencies = [
"blake2b_simd",
]
@ -2310,8 +2308,7 @@ dependencies = [
[[package]]
name = "zcash_address"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1322a31b757f0087f110cc4a85dc5c6ccf83d0533bac04c4d3d1ce9112cc602"
source = "git+https://github.com/zcash/librustzcash.git?rev=e5dad037e9ac091d75dbc4e962f9e34e100f0c67#e5dad037e9ac091d75dbc4e962f9e34e100f0c67"
dependencies = [
"bech32",
"bs58",
@ -2322,8 +2319,7 @@ dependencies = [
[[package]]
name = "zcash_encoding"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fb61ea88eb539bc0ac2068e5da99411dd4978595b3d7ff6a4b1562ddc8e8710"
source = "git+https://github.com/zcash/librustzcash.git?rev=e5dad037e9ac091d75dbc4e962f9e34e100f0c67#e5dad037e9ac091d75dbc4e962f9e34e100f0c67"
dependencies = [
"byteorder",
"nonempty",
@ -2332,8 +2328,7 @@ dependencies = [
[[package]]
name = "zcash_history"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb611a28a4e13ac715ee712f4344d6b279b767daf6345dafefb2c4bf582b6679"
source = "git+https://github.com/zcash/librustzcash.git?rev=e5dad037e9ac091d75dbc4e962f9e34e100f0c67#e5dad037e9ac091d75dbc4e962f9e34e100f0c67"
dependencies = [
"blake2b_simd",
"byteorder",
@ -2343,8 +2338,7 @@ dependencies = [
[[package]]
name = "zcash_note_encryption"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33f84ae538f05a8ac74c82527f06b77045ed9553a0871d9db036166a4c344e3a"
source = "git+https://github.com/zcash/librustzcash.git?rev=e5dad037e9ac091d75dbc4e962f9e34e100f0c67#e5dad037e9ac091d75dbc4e962f9e34e100f0c67"
dependencies = [
"chacha20",
"chacha20poly1305",
@ -2355,8 +2349,7 @@ dependencies = [
[[package]]
name = "zcash_primitives"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fbb401f5dbc482b831954aaa7cba0a8fe148241db6d19fe7cebda78252ca680"
source = "git+https://github.com/zcash/librustzcash.git?rev=e5dad037e9ac091d75dbc4e962f9e34e100f0c67#e5dad037e9ac091d75dbc4e962f9e34e100f0c67"
dependencies = [
"aes",
"bip0039",
@ -2385,6 +2378,7 @@ dependencies = [
"secp256k1",
"sha2",
"subtle",
"zcash_address",
"zcash_encoding",
"zcash_note_encryption",
]
@ -2392,8 +2386,7 @@ dependencies = [
[[package]]
name = "zcash_proofs"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98bf5f6af051dd929263f279b21b9c04c1f30116c70f3c190de2566677f245ef"
source = "git+https://github.com/zcash/librustzcash.git?rev=e5dad037e9ac091d75dbc4e962f9e34e100f0c67#e5dad037e9ac091d75dbc4e962f9e34e100f0c67"
dependencies = [
"bellman",
"blake2b_simd",

View File

@ -109,3 +109,12 @@ features = ["ansi", "env-filter", "fmt", "time"]
lto = true
panic = 'abort'
codegen-units = 1
[patch.crates-io]
equihash = { git = "https://github.com/zcash/librustzcash.git", rev = "e5dad037e9ac091d75dbc4e962f9e34e100f0c67" }
zcash_address = { git = "https://github.com/zcash/librustzcash.git", rev = "e5dad037e9ac091d75dbc4e962f9e34e100f0c67" }
zcash_encoding = { git = "https://github.com/zcash/librustzcash.git", rev = "e5dad037e9ac091d75dbc4e962f9e34e100f0c67" }
zcash_history = { git = "https://github.com/zcash/librustzcash.git", rev = "e5dad037e9ac091d75dbc4e962f9e34e100f0c67" }
zcash_note_encryption = { git = "https://github.com/zcash/librustzcash.git", rev = "e5dad037e9ac091d75dbc4e962f9e34e100f0c67" }
zcash_primitives = { git = "https://github.com/zcash/librustzcash.git", rev = "e5dad037e9ac091d75dbc4e962f9e34e100f0c67" }
zcash_proofs = { git = "https://github.com/zcash/librustzcash.git", rev = "e5dad037e9ac091d75dbc4e962f9e34e100f0c67" }

View File

@ -1,6 +1,6 @@
use zcash_address::{
unified::{self, Container, Encoding},
Network, ToAddress, UnsupportedAddress, ZcashAddress,
ConversionError, Network, ToAddress, ZcashAddress,
};
enum AddressKind {
@ -16,36 +16,50 @@ struct Address {
kind: AddressKind,
}
impl zcash_address::FromAddress for Address {
fn from_sprout(net: Network, data: [u8; 64]) -> Result<Self, UnsupportedAddress> {
impl zcash_address::TryFromAddress for Address {
type Error = ();
fn try_from_sprout(net: Network, data: [u8; 64]) -> Result<Self, ConversionError<Self::Error>> {
Ok(Address {
net,
kind: AddressKind::Sprout(data),
})
}
fn from_sapling(net: Network, data: [u8; 43]) -> Result<Self, UnsupportedAddress> {
fn try_from_sapling(
net: Network,
data: [u8; 43],
) -> Result<Self, ConversionError<Self::Error>> {
Ok(Address {
net,
kind: AddressKind::Sapling(data),
})
}
fn from_unified(net: Network, data: unified::Address) -> Result<Self, UnsupportedAddress> {
fn try_from_unified(
net: Network,
data: unified::Address,
) -> Result<Self, ConversionError<Self::Error>> {
Ok(Address {
net,
kind: AddressKind::Unified(data),
})
}
fn from_transparent_p2pkh(net: Network, data: [u8; 20]) -> Result<Self, UnsupportedAddress> {
fn try_from_transparent_p2pkh(
net: Network,
data: [u8; 20],
) -> Result<Self, ConversionError<Self::Error>> {
Ok(Address {
net,
kind: AddressKind::P2pkh(data),
})
}
fn from_transparent_p2sh(net: Network, data: [u8; 20]) -> Result<Self, UnsupportedAddress> {
fn try_from_transparent_p2sh(
net: Network,
data: [u8; 20],
) -> Result<Self, ConversionError<Self::Error>> {
Ok(Address {
net,
kind: AddressKind::P2sh(data),

View File

@ -77,7 +77,7 @@ extern "C" {
/// Compute a Sapling nullifier.
///
/// The `diversifier` parameter must be 11 bytes in length.
/// The `pk_d`, `r`, `ak` and `nk` parameters must be of length 32.
/// The `pk_d`, `r`, and `nk` parameters must be of length 32.
/// The result is also of length 32 and placed in `result`.
/// Returns false if the diversifier or pk_d is not valid
bool librustzcash_sapling_compute_nf(
@ -85,7 +85,6 @@ extern "C" {
const unsigned char *pk_d,
const uint64_t value,
const unsigned char *rcm,
const unsigned char *ak,
const unsigned char *nk,
const uint64_t position,
unsigned char *result

View File

@ -7,7 +7,7 @@ use std::{
use libc::{c_char, c_void};
use zcash_address::{
unified::{self, Container, Encoding},
FromAddress, Network, ToAddress, ZcashAddress,
Network, ToAddress, TryFromAddress, ZcashAddress,
};
use zcash_primitives::sapling;
@ -45,11 +45,13 @@ struct UnifiedAddressHelper {
ua: unified::Address,
}
impl FromAddress for UnifiedAddressHelper {
fn from_unified(
impl TryFromAddress for UnifiedAddressHelper {
type Error = ();
fn try_from_unified(
net: Network,
ua: unified::Address,
) -> Result<Self, zcash_address::UnsupportedAddress> {
) -> Result<Self, zcash_address::ConversionError<Self::Error>> {
Ok(Self { net, ua })
}
}

View File

@ -47,7 +47,7 @@ use zcash_primitives::{
constants::{CRH_IVK_PERSONALIZATION, PROOF_GENERATION_KEY_GENERATOR, SPENDING_KEY_GENERATOR},
sapling::{
keys::FullViewingKey, note_encryption::sapling_ka_agree, redjubjub, Diversifier, Note,
Rseed, ViewingKey,
NullifierDerivingKey, Rseed,
},
sapling::{merkle_hash, spend_sig},
zip32::{self, sapling_address, sapling_derive_internal_fvk, sapling_find_address},
@ -401,7 +401,6 @@ pub extern "C" fn librustzcash_sapling_compute_nf(
pk_d: *const [c_uchar; 32],
value: u64,
rcm: *const [c_uchar; 32],
ak: *const [c_uchar; 32],
nk: *const [c_uchar; 32],
position: u64,
result: *mut [c_uchar; 32],
@ -413,28 +412,17 @@ pub extern "C" fn librustzcash_sapling_compute_nf(
Err(_) => return false,
};
let ak = match de_ct(jubjub::ExtendedPoint::from_bytes(unsafe { &*ak })) {
Some(p) => p,
None => return false,
};
let ak = match de_ct(ak.into_subgroup()) {
Some(ak) => ak,
None => return false,
};
let nk = match de_ct(jubjub::ExtendedPoint::from_bytes(unsafe { &*nk })) {
Some(p) => p,
None => return false,
};
let nk = match de_ct(nk.into_subgroup()) {
Some(nk) => nk,
Some(nk) => NullifierDerivingKey(nk),
None => return false,
};
let vk = ViewingKey { ak, nk };
let nf = note.nf(&vk, position);
let nf = note.nf(&nk, position);
let result = unsafe { &mut *result };
result.copy_from_slice(&nf.0);

View File

@ -664,7 +664,6 @@ fn notes() {
&tv.default_pk_d,
tv.note_v,
&tv.note_r,
&tv.ak,
&tv.nk,
tv.note_pos,
&mut result

View File

@ -149,6 +149,13 @@ impl consensus::Parameters for Network {
}
}
fn address_network(&self) -> Option<zcash_address::Network> {
match self {
Self::Consensus(params) => params.address_network(),
Self::RegTest { .. } => Some(zcash_address::Network::Regtest),
}
}
fn hrp_sapling_extended_spending_key(&self) -> &str {
match self {
Self::Consensus(params) => params.hrp_sapling_extended_spending_key(),
@ -227,6 +234,13 @@ type OutputReplier<D> = OutputIndex<channel::Sender<OutputIndex<Option<Decrypted
/// A batch of outputs to trial decrypt.
struct Batch<D: BatchDomain, Output: ShieldedOutput<D, ENC_CIPHERTEXT_SIZE>> {
ivks: Vec<D::IncomingViewingKey>,
/// We currently store outputs and repliers as parallel vectors, because
/// [`batch::try_note_decryption`] accepts a slice of domain/output pairs
/// rather than a value that implements `IntoIterator`, and therefore we
/// can't just use `map` to select the parts we need in order to perform
/// batch decryption. Ideally the domain, output, and output replier would
/// all be part of the same struct, which would also track the output index
/// (that is captured in the outer `OutputIndex` of each `OutputReplier`).
outputs: Vec<(D, Output)>,
repliers: Vec<OutputReplier<D>>,
}
@ -254,33 +268,20 @@ where
/// Runs the batch of trial decryptions, and reports the results.
fn run(self) {
assert_eq!(self.outputs.len(), self.repliers.len());
let decrypted = batch::try_note_decryption(&self.ivks, &self.outputs);
for (decrypted_note, (ivk, replier)) in decrypted.into_iter().zip(
// The output of `batch::try_note_decryption` corresponds to the stream of
// trial decryptions:
// (ivk0, out0), (ivk0, out1), ..., (ivk0, outN), (ivk1, out0), ...
// So we can use the position in the stream to figure out which output was
// decrypted and which ivk decrypted it.
self.ivks
.iter()
.flat_map(|ivk| self.repliers.iter().map(move |tx| (ivk, tx))),
) {
let value = decrypted_note.map(|(note, recipient, memo)| DecryptedNote {
ivk: ivk.clone(),
memo,
note,
recipient,
});
let decryption_results = batch::try_note_decryption(&self.ivks, &self.outputs);
for (decryption_result, replier) in decryption_results.into_iter().zip(self.repliers.iter())
{
let result = OutputIndex {
output_index: replier.output_index,
value: decryption_result.map(|((note, recipient, memo), ivk_idx)| DecryptedNote {
ivk: self.ivks[ivk_idx].clone(),
recipient,
note,
memo,
}),
};
let output_index = replier.output_index;
let tx = &replier.value;
if tx
.send(OutputIndex {
output_index,
value,
})
.is_err()
{
if replier.value.send(result).is_err() {
tracing::debug!("BatchRunner was dropped before batch finished");
return;
}

View File

@ -85,7 +85,6 @@ std::optional<uint256> SaplingNote::cmu() const {
// Call librustzcash to compute the nullifier
std::optional<uint256> SaplingNote::nullifier(const SaplingFullViewingKey& vk, const uint64_t position) const
{
auto ak = vk.ak;
auto nk = vk.nk;
uint256 result;
@ -95,7 +94,6 @@ std::optional<uint256> SaplingNote::nullifier(const SaplingFullViewingKey& vk, c
pk_d.begin(),
value(),
rcm_tmp.begin(),
ak.begin(),
nk.begin(),
position,
result.begin()