From f3501333b7b006a40ed12b7d22d1cf1d47dac981 Mon Sep 17 00:00:00 2001 From: Deirdre Connolly Date: Mon, 15 Mar 2021 02:59:08 -0400 Subject: [PATCH] Orchard: tidy --- zebra-chain/src/orchard/keys.rs | 38 ++++++++++++++-------- zebra-chain/src/orchard/note/nullifiers.rs | 12 ++++--- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/zebra-chain/src/orchard/keys.rs b/zebra-chain/src/orchard/keys.rs index 4029ffdf8..250546336 100644 --- a/zebra-chain/src/orchard/keys.rs +++ b/zebra-chain/src/orchard/keys.rs @@ -74,7 +74,9 @@ fn prf_expand(sk: [u8; 32], t: Vec<&[u8]>) -> [u8; 64] { state.update(&sk[..]); - t.iter().map(|t_i| state.update(t_i)); + for t_i in t { + state.update(t_i); + } *state.finalize().as_array() } @@ -370,14 +372,6 @@ impl fmt::Debug for NullifierDerivingKey { } } -impl From<[u8; 32]> for NullifierDerivingKey { - fn from(bytes: [u8; 32]) -> Self { - Self(pallas::Base::from_bytes(&bytes).unwrap()) - } -} - -impl Eq for NullifierDerivingKey {} - impl From for [u8; 32] { fn from(nk: NullifierDerivingKey) -> [u8; 32] { nk.0.to_bytes() @@ -390,6 +384,18 @@ impl From<&NullifierDerivingKey> for [u8; 32] { } } +impl From for pallas::Base { + fn from(nk: NullifierDerivingKey) -> pallas::Base { + nk.0 + } +} + +impl From<[u8; 32]> for NullifierDerivingKey { + fn from(bytes: [u8; 32]) -> Self { + Self(pallas::Base::from_bytes(&bytes).unwrap()) + } +} + impl From for NullifierDerivingKey { /// nk = ToBase^Orchard(PRF^expand_sk ([7])) /// @@ -402,6 +408,8 @@ impl From for NullifierDerivingKey { } } +impl Eq for NullifierDerivingKey {} + impl PartialEq<[u8; 32]> for NullifierDerivingKey { fn eq(&self, other: &[u8; 32]) -> bool { <[u8; 32]>::from(*self) == *other @@ -501,7 +509,7 @@ impl fmt::Display for IncomingViewingKey { impl From<[u8; 32]> for IncomingViewingKey { /// Generate an _IncomingViewingKey_ from existing bytes. - fn from(mut bytes: [u8; 32]) -> Self { + fn from(bytes: [u8; 32]) -> Self { Self { // TODO: handle setting the Network better. network: Network::default(), @@ -663,9 +671,13 @@ impl FullViewingKey { // let K = I2LEBSP_l_sk(rivk) let K: [u8; 32] = self.ivk_commit_randomness.into(); - let t: Vec<&[u8]> = vec![&[0x82u8]]; - t.push(&<[u8; 32]>::from(self.spend_validating_key)); - t.push(&<[u8; 32]>::from(self.nullifier_deriving_key)); + let mut t: Vec<&[u8]> = vec![&[0x82u8]]; + + let ak_bytes = <[u8; 32]>::from(self.spend_validating_key); + t.push(&ak_bytes); + + let nk_bytes = <[u8; 32]>::from(self.nullifier_deriving_key); + t.push(&nk_bytes); // let R = PRF^expand_K( [0x82] || I2LEOSP256(ak) || I2LEOSP256(nk) ) prf_expand(K, t) diff --git a/zebra-chain/src/orchard/note/nullifiers.rs b/zebra-chain/src/orchard/note/nullifiers.rs index 59b307269..584e1983e 100644 --- a/zebra-chain/src/orchard/note/nullifiers.rs +++ b/zebra-chain/src/orchard/note/nullifiers.rs @@ -2,7 +2,7 @@ #![allow(dead_code)] use group::GroupEncoding; -use halo2::pasta::pallas; +use halo2::{arithmetic::FieldExt, pasta::pallas}; use super::super::{ commitment::NoteCommitment, keys::NullifierDerivingKey, sinsemilla::*, tree::Position, @@ -28,7 +28,7 @@ pub fn mixing_pedersen_hash(P: pallas::Point, x: pallas::Scalar) -> pallas::Poin /// PoseidonHash(x, y) = f([x, y, 0])_1 (using 1-based indexing). /// /// [poseidonhash]: https://zips.z.cash/protocol/nu5.pdf#poseidonhash -fn poseidon_hash(x: pallas::Base, y: pallas::Base) -> pallas::Base { +fn poseidon_hash(_x: pallas::Base, _y: pallas::Base) -> pallas::Base { unimplemented!() } @@ -41,8 +41,12 @@ fn poseidon_hash(x: pallas::Base, y: pallas::Base) -> pallas::Base { /// /// [concreteprfs]: https://zips.z.cash/protocol/protocol.pdf#concreteprfs /// [poseidonhash]: https://zips.z.cash/protocol/nu5.pdf#poseidonhash -fn prf_nf(nk: [u8; 32], rho: [u8; 32]) -> [u8; 32] { - poseidon_hash(nk.into(), rho.into()).into() +fn prf_nf(nk_bytes: [u8; 32], rho_bytes: [u8; 32]) -> [u8; 32] { + poseidon_hash( + pallas::Base::from_bytes(&nk_bytes).unwrap(), + pallas::Base::from_bytes(&rho_bytes).unwrap(), + ) + .into() } /// A Nullifier for Orchard transactions