Address various clippy warnings/errors in zcash_primitives

This commit is contained in:
Jack Grigg 2019-08-02 11:25:00 +01:00
parent d65fe2cda9
commit 3a8efd9e67
No known key found for this signature in database
GPG Key ID: 9E8255172BBF9898
18 changed files with 97 additions and 107 deletions

View File

@ -13,7 +13,7 @@ pub struct BlockHash(pub [u8; 32]);
impl fmt::Display for BlockHash {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut data = self.0.clone();
let mut data = self.0;
data.reverse();
formatter.write_str(&hex::encode(data))
}

View File

@ -60,10 +60,7 @@ impl Node {
indices.extend(a.indices.iter());
indices
};
Node {
hash: hash,
indices: indices,
}
Node { hash, indices }
}
fn from_children_ref(a: &Node, b: &Node, trim: usize) -> Self {
@ -82,10 +79,7 @@ impl Node {
indices.extend(b.indices.iter());
indices.extend(a.indices.iter());
}
Node {
hash: hash,
indices: indices,
}
Node { hash, indices }
}
fn indices_before(&self, other: &Node) -> bool {
@ -141,7 +135,7 @@ fn expand_array(vin: &[u8], bit_len: usize, byte_pad: usize) -> Vec<u8> {
let mut j = 0;
for b in vin {
acc_value = (acc_value << 8) | *b as u32;
acc_value = (acc_value << 8) | u32::from(*b);
acc_bits += 8;
// When we have bit_len or more bits in the accumulator, write the next
@ -197,7 +191,7 @@ fn distinct_indices(a: &Node, b: &Node) -> bool {
}
}
}
return true;
true
}
fn validate_subtrees(p: &Params, a: &Node, b: &Node) -> bool {
@ -222,7 +216,7 @@ pub fn is_valid_solution_iterative(
nonce: &[u8],
indices: &[u32],
) -> bool {
let p = Params { n: n, k: k };
let p = Params { n, k };
let mut state = initialise_state(p.n, p.k, p.hash_output());
state.update(input);
@ -249,7 +243,7 @@ pub fn is_valid_solution_iterative(
}
assert!(rows.len() == 1);
return rows[0].is_zero(hash_len);
rows[0].is_zero(hash_len)
}
fn tree_validator(p: &Params, state: &Blake2bState, indices: &[u32]) -> Option<Node> {
@ -281,7 +275,7 @@ pub fn is_valid_solution_recursive(
nonce: &[u8],
indices: &[u32],
) -> bool {
let p = Params { n: n, k: k };
let p = Params { n, k };
let mut state = initialise_state(p.n, p.k, p.hash_output());
state.update(input);
@ -297,7 +291,7 @@ pub fn is_valid_solution_recursive(
}
pub fn is_valid_solution(n: u32, k: u32, input: &[u8], nonce: &[u8], soln: &[u8]) -> bool {
let p = Params { n: n, k: k };
let p = Params { n, k };
let indices = indices_from_minimal(soln, p.collision_bit_length());
// Recursive validation is faster

View File

@ -2,31 +2,31 @@
/// This is chosen to be some random string that we couldn't have anticipated when we designed
/// the algorithm, for rigidity purposes.
/// We deliberately use an ASCII hex string of 32 bytes here.
pub const GH_FIRST_BLOCK: &'static [u8; 64] =
pub const GH_FIRST_BLOCK: &[u8; 64] =
b"096b36a5804bfacef1691e173c366a47ff5ba84a44f26ddd7e8d9f79d5b42df0";
// BLAKE2s invocation personalizations
/// BLAKE2s Personalization for CRH^ivk = BLAKE2s(ak | nk)
pub const CRH_IVK_PERSONALIZATION: &'static [u8; 8] = b"Zcashivk";
pub const CRH_IVK_PERSONALIZATION: &[u8; 8] = b"Zcashivk";
/// BLAKE2s Personalization for PRF^nf = BLAKE2s(nk | rho)
pub const PRF_NF_PERSONALIZATION: &'static [u8; 8] = b"Zcash_nf";
pub const PRF_NF_PERSONALIZATION: &[u8; 8] = b"Zcash_nf";
// Group hash personalizations
/// BLAKE2s Personalization for Pedersen hash generators.
pub const PEDERSEN_HASH_GENERATORS_PERSONALIZATION: &'static [u8; 8] = b"Zcash_PH";
pub const PEDERSEN_HASH_GENERATORS_PERSONALIZATION: &[u8; 8] = b"Zcash_PH";
/// BLAKE2s Personalization for the group hash for key diversification
pub const KEY_DIVERSIFICATION_PERSONALIZATION: &'static [u8; 8] = b"Zcash_gd";
pub const KEY_DIVERSIFICATION_PERSONALIZATION: &[u8; 8] = b"Zcash_gd";
/// BLAKE2s Personalization for the spending key base point
pub const SPENDING_KEY_GENERATOR_PERSONALIZATION: &'static [u8; 8] = b"Zcash_G_";
pub const SPENDING_KEY_GENERATOR_PERSONALIZATION: &[u8; 8] = b"Zcash_G_";
/// BLAKE2s Personalization for the proof generation key base point
pub const PROOF_GENERATION_KEY_BASE_GENERATOR_PERSONALIZATION: &'static [u8; 8] = b"Zcash_H_";
pub const PROOF_GENERATION_KEY_BASE_GENERATOR_PERSONALIZATION: &[u8; 8] = b"Zcash_H_";
/// BLAKE2s Personalization for the value commitment generator for the value
pub const VALUE_COMMITMENT_GENERATOR_PERSONALIZATION: &'static [u8; 8] = b"Zcash_cv";
pub const VALUE_COMMITMENT_GENERATOR_PERSONALIZATION: &[u8; 8] = b"Zcash_cv";
/// BLAKE2s Personalization for the nullifier position generator (for computing rho)
pub const NULLIFIER_POSITION_IN_TREE_GENERATOR_PERSONALIZATION: &'static [u8; 8] = b"Zcash_J_";
pub const NULLIFIER_POSITION_IN_TREE_GENERATOR_PERSONALIZATION: &[u8; 8] = b"Zcash_J_";

View File

@ -132,9 +132,9 @@ impl<E: JubjubEngine> Point<E, Unknown> {
t.mul_assign(&y);
Some(Point {
x: x,
y: y,
t: t,
x,
y,
t,
z: E::Fr::one(),
_marker: PhantomData,
})
@ -277,8 +277,8 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
Point {
x: u,
y: v,
t: t,
z: z,
t,
z,
_marker: PhantomData,
}
}
@ -412,7 +412,7 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
b.mul_assign(&other.y);
// C = d * t1 * t2
let mut c = params.edwards_d().clone();
let mut c = *params.edwards_d();
c.mul_assign(&self.t);
c.mul_assign(&other.t);

View File

@ -321,8 +321,8 @@ impl Field for Fs {
loop {
let mut tmp = {
let mut repr = [0u64; 4];
for i in 0..4 {
repr[i] = rng.next_u64();
for limb in &mut repr {
*limb = rng.next_u64();
}
Fs(FsRepr(repr))
};

View File

@ -199,9 +199,9 @@ impl JubjubBls12 {
)
.unwrap(),
// A = 40962
montgomery_a: montgomery_a,
montgomery_a,
// 2A = 2.A
montgomery_2a: montgomery_2a,
montgomery_2a,
// scaling factor = sqrt(4 / (a - d))
scale: Fr::from_str(
"17814886934372412843466061268024708274627479829237077604635722030778476050649",

View File

@ -64,12 +64,12 @@ impl<E: JubjubEngine> Point<E, Unknown> {
y.negate();
}
return Some(Point {
x: x,
y: y,
Some(Point {
x,
y,
infinity: false,
_marker: PhantomData,
});
})
}
None => None,
}
@ -88,9 +88,8 @@ impl<E: JubjubEngine> Point<E, Unknown> {
let x = E::Fr::random(rng);
let sign = rng.next_u32() % 2 != 0;
match Self::get_for_x(x, sign, params) {
Some(p) => return p,
None => {}
if let Some(p) = Self::get_for_x(x, sign, params) {
return p;
}
}
}
@ -214,7 +213,7 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
let mut delta = E::Fr::one();
{
let mut tmp = params.montgomery_a().clone();
let mut tmp = *params.montgomery_a();
tmp.mul_assign(&self.x);
tmp.double();
delta.add_assign(&tmp);

View File

@ -10,11 +10,11 @@ use blake2b_simd::{Hash as Blake2bHash, Params as Blake2bParams};
use ff::{PrimeField, PrimeFieldRepr};
use std::io::{self, Read, Write};
pub const PRF_EXPAND_PERSONALIZATION: &'static [u8; 16] = b"Zcash_ExpandSeed";
pub const PRF_EXPAND_PERSONALIZATION: &[u8; 16] = b"Zcash_ExpandSeed";
/// PRF^expand(sk, t) := BLAKE2b-512("Zcash_ExpandSeed", sk || t)
pub fn prf_expand(sk: &[u8], t: &[u8]) -> Blake2bHash {
prf_expand_vec(sk, &vec![t])
prf_expand_vec(sk, &[t])
}
pub fn prf_expand_vec(sk: &[u8], ts: &[&[u8]]) -> Blake2bHash {
@ -111,7 +111,7 @@ impl<E: JubjubEngine> Clone for FullViewingKey<E> {
ak: self.vk.ak.clone(),
nk: self.vk.nk.clone(),
},
ovk: self.ovk.clone(),
ovk: self.ovk,
}
}
}

View File

@ -486,8 +486,10 @@ impl<Node: Hashable> CommitmentTreeWitness<Node> {
// Given the position, let's finish constructing the authentication
// path
let mut tmp = position;
for i in 0..depth {
auth_path[i].as_mut().map(|p| p.1 = (tmp & 1) == 1);
for entry in auth_path.iter_mut() {
if let Some(p) = entry {
p.1 = (tmp & 1) == 1;
}
tmp >>= 1;
}

View File

@ -19,8 +19,8 @@ use std::str;
use crate::{keys::OutgoingViewingKey, JUBJUB};
pub const KDF_SAPLING_PERSONALIZATION: &'static [u8; 16] = b"Zcash_SaplingKDF";
pub const PRF_OCK_PERSONALIZATION: &'static [u8; 16] = b"Zcash_Derive_ock";
pub const KDF_SAPLING_PERSONALIZATION: &[u8; 16] = b"Zcash_SaplingKDF";
pub const PRF_OCK_PERSONALIZATION: &[u8; 16] = b"Zcash_Derive_ock";
const COMPACT_NOTE_SIZE: usize = (
1 + // version
@ -85,7 +85,7 @@ impl Default for Memo {
impl PartialEq for Memo {
fn eq(&self, rhs: &Memo) -> bool {
&self.0[..] == &rhs.0[..]
self.0[..] == rhs.0[..]
}
}

View File

@ -97,10 +97,7 @@ impl<E: JubjubEngine> ViewingKey<E> {
diversifier.g_d(params).map(|g_d| {
let pk_d = g_d.mul(self.ivk(), params);
PaymentAddress {
pk_d: pk_d,
diversifier: diversifier,
}
PaymentAddress { pk_d, diversifier }
})
}
}
@ -145,9 +142,9 @@ impl<E: JubjubEngine> PaymentAddress<E> {
params: &E::Params,
) -> Option<Note<E>> {
self.g_d(params).map(|g_d| Note {
value: value,
value,
r: randomness,
g_d: g_d,
g_d,
pk_d: self.pk_d.clone(),
})
}

View File

@ -37,9 +37,9 @@ pub fn merkle_hash(depth: usize, lhs: &FrRepr, rhs: &FrRepr) -> FrRepr {
pedersen_hash::<Bls12, _>(
Personalization::MerkleTree(depth),
lhs.iter()
.map(|&x| x)
.copied()
.take(Fr::NUM_BITS as usize)
.chain(rhs.iter().map(|&x| x).take(Fr::NUM_BITS as usize)),
.chain(rhs.iter().copied().take(Fr::NUM_BITS as usize)),
&JUBJUB,
)
.into_xy()

View File

@ -70,7 +70,7 @@ impl Vector {
F: Fn(&mut R) -> io::Result<E>,
{
let count = CompactSize::read(&mut reader)?;
(0..count).into_iter().map(|_| func(&mut reader)).collect()
(0..count).map(|_| func(&mut reader)).collect()
}
pub fn write<W: Write, E, F>(mut writer: W, vec: &[E], func: F) -> io::Result<()>

View File

@ -153,7 +153,7 @@ impl TransactionMetadata {
/// they added (via the first call to [`Builder::add_sapling_spend`]) is the first
/// [`SpendDescription`] in the transaction.
pub fn spend_index(&self, n: usize) -> Option<usize> {
self.spend_indices.get(n).map(|i| *i)
self.spend_indices.get(n).copied()
}
/// Returns the index within the transaction of the [`OutputDescription`] corresponding
@ -164,7 +164,7 @@ impl TransactionMetadata {
/// they added (via the first call to [`Builder::add_sapling_output`]) is the first
/// [`OutputDescription`] in the transaction.
pub fn output_index(&self, n: usize) -> Option<usize> {
self.output_indices.get(n).map(|i| *i)
self.output_indices.get(n).copied()
}
}
@ -414,7 +414,7 @@ impl<R: RngCore + CryptoRng> Builder<R> {
self.mtx.shielded_spends.push(SpendDescription {
cv,
anchor: anchor,
anchor,
nullifier,
rk,
zkproof,

View File

@ -166,12 +166,10 @@ impl SpendDescription {
writer.write_all(&self.zkproof)?;
match self.spend_auth_sig {
Some(sig) => sig.write(&mut writer),
None => {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Missing spend auth signature",
));
}
None => Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Missing spend auth signature",
)),
}
}
}
@ -347,23 +345,20 @@ impl JSDescription {
.map(|mac| reader.read_exact(mac))
.collect::<io::Result<()>>()?;
let proof = match use_groth {
true => {
// Consensus rules (§4.3):
// - Canonical encoding is enforced in librustzcash_sprout_verify()
// - Proof validity is enforced in librustzcash_sprout_verify()
let mut proof = [0; GROTH_PROOF_SIZE];
reader.read_exact(&mut proof)?;
SproutProof::Groth(proof)
}
false => {
// Consensus rules (§4.3):
// - Canonical encoding is enforced by PHGRProof in zcashd
// - Proof validity is enforced by JSDescription::Verify() in zcashd
let mut proof = [0; PHGR_PROOF_SIZE];
reader.read_exact(&mut proof)?;
SproutProof::PHGR(proof)
}
let proof = if use_groth {
// Consensus rules (§4.3):
// - Canonical encoding is enforced in librustzcash_sprout_verify()
// - Proof validity is enforced in librustzcash_sprout_verify()
let mut proof = [0; GROTH_PROOF_SIZE];
reader.read_exact(&mut proof)?;
SproutProof::Groth(proof)
} else {
// Consensus rules (§4.3):
// - Canonical encoding is enforced by PHGRProof in zcashd
// - Proof validity is enforced by JSDescription::Verify() in zcashd
let mut proof = [0; PHGR_PROOF_SIZE];
reader.read_exact(&mut proof)?;
SproutProof::PHGR(proof)
};
let mut ciphertexts = [[0; 601]; ZC_NUM_JS_OUTPUTS];

View File

@ -29,7 +29,7 @@ pub struct TxId(pub [u8; 32]);
impl fmt::Display for TxId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut data = self.0.clone();
let mut data = self.0;
data.reverse();
formatter.write_str(&hex::encode(data))
}
@ -164,9 +164,10 @@ impl Transaction {
let overwintered = (header >> 31) == 1;
let version = header & 0x7FFFFFFF;
let version_group_id = match overwintered {
true => reader.read_u32::<LittleEndian>()?,
false => 0,
let version_group_id = if overwintered {
reader.read_u32::<LittleEndian>()?
} else {
0
};
let is_overwinter_v3 = overwintered
@ -185,9 +186,10 @@ impl Transaction {
let vin = Vector::read(&mut reader, TxIn::read)?;
let vout = Vector::read(&mut reader, TxOut::read)?;
let lock_time = reader.read_u32::<LittleEndian>()?;
let expiry_height = match is_overwinter_v3 || is_sapling_v4 {
true => reader.read_u32::<LittleEndian>()?,
false => 0,
let expiry_height = if is_overwinter_v3 || is_sapling_v4 {
reader.read_u32::<LittleEndian>()?
} else {
0
};
let (value_balance, shielded_spends, shielded_outputs) = if is_sapling_v4 {
@ -223,9 +225,10 @@ impl Transaction {
};
let binding_sig =
match is_sapling_v4 && !(shielded_spends.is_empty() && shielded_outputs.is_empty()) {
true => Some(Signature::read(&mut reader)?),
false => None,
if is_sapling_v4 && !(shielded_spends.is_empty() && shielded_outputs.is_empty()) {
Some(Signature::read(&mut reader)?)
} else {
None
};
Transaction::from_data(TransactionData {

View File

@ -9,13 +9,13 @@ use super::{
};
use crate::legacy::Script;
const ZCASH_SIGHASH_PERSONALIZATION_PREFIX: &'static [u8; 12] = b"ZcashSigHash";
const ZCASH_PREVOUTS_HASH_PERSONALIZATION: &'static [u8; 16] = b"ZcashPrevoutHash";
const ZCASH_SEQUENCE_HASH_PERSONALIZATION: &'static [u8; 16] = b"ZcashSequencHash";
const ZCASH_OUTPUTS_HASH_PERSONALIZATION: &'static [u8; 16] = b"ZcashOutputsHash";
const ZCASH_JOINSPLITS_HASH_PERSONALIZATION: &'static [u8; 16] = b"ZcashJSplitsHash";
const ZCASH_SHIELDED_SPENDS_HASH_PERSONALIZATION: &'static [u8; 16] = b"ZcashSSpendsHash";
const ZCASH_SHIELDED_OUTPUTS_HASH_PERSONALIZATION: &'static [u8; 16] = b"ZcashSOutputHash";
const ZCASH_SIGHASH_PERSONALIZATION_PREFIX: &[u8; 12] = b"ZcashSigHash";
const ZCASH_PREVOUTS_HASH_PERSONALIZATION: &[u8; 16] = b"ZcashPrevoutHash";
const ZCASH_SEQUENCE_HASH_PERSONALIZATION: &[u8; 16] = b"ZcashSequencHash";
const ZCASH_OUTPUTS_HASH_PERSONALIZATION: &[u8; 16] = b"ZcashOutputsHash";
const ZCASH_JOINSPLITS_HASH_PERSONALIZATION: &[u8; 16] = b"ZcashJSplitsHash";
const ZCASH_SHIELDED_SPENDS_HASH_PERSONALIZATION: &[u8; 16] = b"ZcashSSpendsHash";
const ZCASH_SHIELDED_OUTPUTS_HASH_PERSONALIZATION: &[u8; 16] = b"ZcashSOutputHash";
pub const SIGHASH_ALL: u32 = 1;
const SIGHASH_NONE: u32 = 2;

View File

@ -16,8 +16,8 @@ use crate::{
JUBJUB,
};
pub const ZIP32_SAPLING_MASTER_PERSONALIZATION: &'static [u8; 16] = b"ZcashIP32Sapling";
pub const ZIP32_SAPLING_FVFP_PERSONALIZATION: &'static [u8; 16] = b"ZcashSaplingFVFP";
pub const ZIP32_SAPLING_MASTER_PERSONALIZATION: &[u8; 16] = b"ZcashIP32Sapling";
pub const ZIP32_SAPLING_FVFP_PERSONALIZATION: &[u8; 16] = b"ZcashSaplingFVFP";
// Common helper functions
@ -83,9 +83,9 @@ impl ChildIndex {
}
fn to_index(&self) -> u32 {
match self {
&ChildIndex::Hardened(i) => i + (1 << 31),
&ChildIndex::NonHardened(i) => i,
match *self {
ChildIndex::Hardened(i) => i + (1 << 31),
ChildIndex::NonHardened(i) => i,
}
}
}