diff --git a/zebra-chain/src/orchard/arbitrary.rs b/zebra-chain/src/orchard/arbitrary.rs index 78b8141d0..28f1201f2 100644 --- a/zebra-chain/src/orchard/arbitrary.rs +++ b/zebra-chain/src/orchard/arbitrary.rs @@ -74,8 +74,8 @@ impl Arbitrary for Signature { fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy { (array::uniform32(any::()), array::uniform32(any::())) .prop_map(|(r_bytes, s_bytes)| Self { - r_bytes, - s_bytes, + r_bytes: r_bytes.into(), + s_bytes: s_bytes.into(), _marker: PhantomData, }) .boxed() diff --git a/zebra-chain/src/primitives/proofs/bctv14.rs b/zebra-chain/src/primitives/proofs/bctv14.rs index 9d7e51d0c..abef38534 100644 --- a/zebra-chain/src/primitives/proofs/bctv14.rs +++ b/zebra-chain/src/primitives/proofs/bctv14.rs @@ -1,3 +1,5 @@ +//! BCTV14 proofs for Zebra. + use std::{fmt, io}; use serde::{Deserialize, Serialize}; diff --git a/zebra-chain/src/primitives/proofs/groth16.rs b/zebra-chain/src/primitives/proofs/groth16.rs index f2bca69e0..8153b2fb3 100644 --- a/zebra-chain/src/primitives/proofs/groth16.rs +++ b/zebra-chain/src/primitives/proofs/groth16.rs @@ -1,3 +1,5 @@ +//! Groth16 proofs for Zebra. + use std::{fmt, io}; use serde::{Deserialize, Serialize}; diff --git a/zebra-chain/src/primitives/redpallas/batch.rs b/zebra-chain/src/primitives/redpallas/batch.rs index a8bb91759..95ecbcbf1 100644 --- a/zebra-chain/src/primitives/redpallas/batch.rs +++ b/zebra-chain/src/primitives/redpallas/batch.rs @@ -229,7 +229,7 @@ impl Verifier { let s = { // XXX-pallas: should not use CtOption here - let maybe_scalar = pallas::Scalar::from_repr(s_bytes); + let maybe_scalar = pallas::Scalar::from_repr(*s_bytes); if maybe_scalar.is_some().into() { maybe_scalar.unwrap() } else { @@ -258,10 +258,10 @@ impl Verifier { // // This validates the `rk` element, whose type is // SpendAuthSig^{Orchard}.Public, i.e. ℙ. - VerificationKey::::try_from(vk_bytes.bytes)?.point + VerificationKey::::try_from(*vk_bytes.bytes)?.point } Inner::Binding { vk_bytes, .. } => { - VerificationKey::::try_from(vk_bytes.bytes)?.point + VerificationKey::::try_from(*vk_bytes.bytes)?.point } }; diff --git a/zebra-chain/src/primitives/redpallas/signature.rs b/zebra-chain/src/primitives/redpallas/signature.rs index aae7f30e9..8f8d3a07f 100644 --- a/zebra-chain/src/primitives/redpallas/signature.rs +++ b/zebra-chain/src/primitives/redpallas/signature.rs @@ -12,13 +12,16 @@ use std::{io, marker::PhantomData}; use super::SigType; -use crate::serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize}; +use crate::{ + fmt::HexDebug, + serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize}, +}; /// A RedPallas signature. #[derive(Copy, Clone, Debug, Eq, PartialEq, Deserialize, Serialize)] pub struct Signature { - pub(crate) r_bytes: [u8; 32], - pub(crate) s_bytes: [u8; 32], + pub(crate) r_bytes: HexDebug<[u8; 32]>, + pub(crate) s_bytes: HexDebug<[u8; 32]>, pub(crate) _marker: PhantomData, } @@ -29,8 +32,8 @@ impl From<[u8; 64]> for Signature { let mut s_bytes = [0; 32]; s_bytes.copy_from_slice(&bytes[32..64]); Signature { - r_bytes, - s_bytes, + r_bytes: r_bytes.into(), + s_bytes: s_bytes.into(), _marker: PhantomData, } } diff --git a/zebra-chain/src/primitives/redpallas/signing_key.rs b/zebra-chain/src/primitives/redpallas/signing_key.rs index bce9a3b0b..0570ee275 100644 --- a/zebra-chain/src/primitives/redpallas/signing_key.rs +++ b/zebra-chain/src/primitives/redpallas/signing_key.rs @@ -1,4 +1,5 @@ -use std::convert::{TryFrom, TryInto}; +//! Redpallas signing keys for Zebra. + use std::marker::PhantomData; use group::{ff::PrimeField, GroupEncoding}; @@ -117,8 +118,8 @@ impl SigningKey { let s_bytes = (nonce + (c * self.sk)).to_repr(); Signature { - r_bytes, - s_bytes, + r_bytes: r_bytes.into(), + s_bytes: s_bytes.into(), _marker: PhantomData, } } diff --git a/zebra-chain/src/primitives/redpallas/verification_key.rs b/zebra-chain/src/primitives/redpallas/verification_key.rs index c047382c3..c523ab887 100644 --- a/zebra-chain/src/primitives/redpallas/verification_key.rs +++ b/zebra-chain/src/primitives/redpallas/verification_key.rs @@ -1,8 +1,12 @@ +//! Redpallas verification keys for Zebra. + use std::marker::PhantomData; use group::{cofactor::CofactorGroup, ff::PrimeField, GroupEncoding}; use halo2::pasta::pallas; +use crate::fmt::HexDebug; + use super::*; /// A refinement type for `[u8; 32]` indicating that the bytes represent @@ -13,14 +17,14 @@ use super::*; /// used in signature verification. #[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] pub struct VerificationKeyBytes { - pub(crate) bytes: [u8; 32], + pub(crate) bytes: HexDebug<[u8; 32]>, pub(crate) _marker: PhantomData, } impl From<[u8; 32]> for VerificationKeyBytes { fn from(bytes: [u8; 32]) -> VerificationKeyBytes { VerificationKeyBytes { - bytes, + bytes: bytes.into(), _marker: PhantomData, } } @@ -28,7 +32,7 @@ impl From<[u8; 32]> for VerificationKeyBytes { impl From> for [u8; 32] { fn from(refined: VerificationKeyBytes) -> [u8; 32] { - refined.bytes + *refined.bytes } } @@ -65,7 +69,7 @@ impl From> for VerificationKeyBytes { impl From> for [u8; 32] { fn from(pk: VerificationKey) -> [u8; 32] { - pk.bytes.bytes + *pk.bytes.bytes } } @@ -107,7 +111,7 @@ impl VerificationKey { use super::private::Sealed; let point = self.point + (SpendAuth::basepoint() * randomizer); let bytes = VerificationKeyBytes { - bytes: point.to_bytes(), + bytes: point.to_bytes().into(), _marker: PhantomData, }; VerificationKey { point, bytes } @@ -118,7 +122,7 @@ impl VerificationKey { pub(crate) fn from_scalar(s: &pallas::Scalar) -> VerificationKey { let point = T::basepoint() * s; let bytes = VerificationKeyBytes { - bytes: point.to_bytes(), + bytes: point.to_bytes().into(), _marker: PhantomData, }; VerificationKey { point, bytes } @@ -154,7 +158,7 @@ impl VerificationKey { let s = { // XXX-pasta_curves: should not use CtOption here - let maybe_scalar = pallas::Scalar::from_repr(signature.s_bytes); + let maybe_scalar = pallas::Scalar::from_repr(*signature.s_bytes); if maybe_scalar.is_some().into() { maybe_scalar.unwrap() } else { diff --git a/zebra-chain/src/sapling/note/nullifiers.rs b/zebra-chain/src/sapling/note/nullifiers.rs index 78b845534..16103cb02 100644 --- a/zebra-chain/src/sapling/note/nullifiers.rs +++ b/zebra-chain/src/sapling/note/nullifiers.rs @@ -1,22 +1,24 @@ //! Sapling nullifiers. +use crate::fmt::HexDebug; + /// A Nullifier for Sapling transactions #[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)] #[cfg_attr( any(test, feature = "proptest-impl"), derive(proptest_derive::Arbitrary) )] -pub struct Nullifier(pub [u8; 32]); +pub struct Nullifier(pub HexDebug<[u8; 32]>); impl From<[u8; 32]> for Nullifier { fn from(buf: [u8; 32]) -> Self { - Self(buf) + Self(buf.into()) } } impl From for [u8; 32] { fn from(n: Nullifier) -> Self { - n.0 + *n.0 } } diff --git a/zebra-chain/src/sprout/commitment.rs b/zebra-chain/src/sprout/commitment.rs index 05f46a09a..5d58a0964 100644 --- a/zebra-chain/src/sprout/commitment.rs +++ b/zebra-chain/src/sprout/commitment.rs @@ -2,6 +2,8 @@ use sha2::{Digest, Sha256}; +use crate::fmt::HexDebug; + use super::note::Note; /// The randomness used in the Pedersen Hash for note commitment. @@ -10,11 +12,11 @@ use super::note::Note; any(test, feature = "proptest-impl"), derive(proptest_derive::Arbitrary) )] -pub struct CommitmentRandomness(pub [u8; 32]); +pub struct CommitmentRandomness(pub HexDebug<[u8; 32]>); impl AsRef<[u8]> for CommitmentRandomness { fn as_ref(&self) -> &[u8] { - &self.0 + self.0.as_ref() } } @@ -24,11 +26,11 @@ impl AsRef<[u8]> for CommitmentRandomness { any(test, feature = "proptest-impl"), derive(proptest_derive::Arbitrary) )] -pub struct NoteCommitment(pub(crate) [u8; 32]); +pub struct NoteCommitment(pub(crate) HexDebug<[u8; 32]>); impl From<[u8; 32]> for NoteCommitment { fn from(bytes: [u8; 32]) -> Self { - Self(bytes) + Self(bytes.into()) } } @@ -44,18 +46,20 @@ impl From for NoteCommitment { hasher.update(note.value.to_bytes()); hasher.update(note.rho); hasher.update(note.rcm); - NoteCommitment(hasher.finalize().into()) + + let commitment: [u8; 32] = hasher.finalize().into(); + NoteCommitment(commitment.into()) } } impl From for [u8; 32] { fn from(cm: NoteCommitment) -> [u8; 32] { - cm.0 + *cm.0 } } impl From<&NoteCommitment> for [u8; 32] { fn from(cm: &NoteCommitment) -> [u8; 32] { - cm.0 + *cm.0 } } diff --git a/zebra-chain/src/sprout/joinsplit.rs b/zebra-chain/src/sprout/joinsplit.rs index c18de56e0..059ac4be5 100644 --- a/zebra-chain/src/sprout/joinsplit.rs +++ b/zebra-chain/src/sprout/joinsplit.rs @@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize}; use crate::{ amount::{Amount, NegativeAllowed, NonNegative}, block::MAX_BLOCK_BYTES, + fmt::HexDebug, primitives::{x25519, Bctv14Proof, Groth16Proof, ZkSnarkProof}, serialization::{ ReadZcashExt, SerializationError, TrustedPreallocate, WriteZcashExt, ZcashDeserialize, @@ -25,17 +26,17 @@ use super::{commitment, note, tree}; any(test, feature = "proptest-impl"), derive(proptest_derive::Arbitrary) )] -pub struct RandomSeed([u8; 32]); +pub struct RandomSeed(HexDebug<[u8; 32]>); impl From<[u8; 32]> for RandomSeed { fn from(bytes: [u8; 32]) -> Self { - Self(bytes) + Self(bytes.into()) } } impl From for [u8; 32] { fn from(rt: RandomSeed) -> [u8; 32] { - rt.0 + *rt.0 } } diff --git a/zebra-chain/src/sprout/note/mac.rs b/zebra-chain/src/sprout/note/mac.rs index c32fba581..b2a05ac29 100644 --- a/zebra-chain/src/sprout/note/mac.rs +++ b/zebra-chain/src/sprout/note/mac.rs @@ -1,6 +1,12 @@ -use crate::serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize}; +//! Sprout message authentication codes. + use std::io::{self, Read}; +use crate::{ + fmt::HexDebug, + serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize}, +}; + /// A sequence of message authentication tags ... /// /// binding h_sig to each a_sk of the JoinSplit description, computed as @@ -10,17 +16,17 @@ use std::io::{self, Read}; any(test, feature = "proptest-impl"), derive(proptest_derive::Arbitrary) )] -pub struct Mac([u8; 32]); +pub struct Mac(HexDebug<[u8; 32]>); impl From<[u8; 32]> for Mac { fn from(bytes: [u8; 32]) -> Self { - Self(bytes) + Self(bytes.into()) } } impl From for [u8; 32] { fn from(rt: Mac) -> [u8; 32] { - rt.0 + *rt.0 } } @@ -34,7 +40,7 @@ impl ZcashDeserialize for Mac { fn zcash_deserialize(mut reader: R) -> Result { let bytes = reader.read_32_bytes()?; - Ok(Self(bytes)) + Ok(Self(bytes.into())) } } diff --git a/zebra-chain/src/sprout/note/nullifiers.rs b/zebra-chain/src/sprout/note/nullifiers.rs index c25d9d072..fb5c47854 100644 --- a/zebra-chain/src/sprout/note/nullifiers.rs +++ b/zebra-chain/src/sprout/note/nullifiers.rs @@ -2,6 +2,8 @@ use serde::{Deserialize, Serialize}; +use crate::fmt::HexDebug; + /// Nullifier seed, named rho in the [spec][ps]. /// /// [ps]: https://zips.z.cash/protocol/protocol.pdf#sproutkeycomponents @@ -11,23 +13,23 @@ use serde::{Deserialize, Serialize}; any(test, feature = "proptest-impl"), derive(proptest_derive::Arbitrary) )] -pub struct NullifierSeed(pub(crate) [u8; 32]); +pub struct NullifierSeed(pub(crate) HexDebug<[u8; 32]>); impl AsRef<[u8]> for NullifierSeed { fn as_ref(&self) -> &[u8] { - &self.0 + self.0.as_ref() } } impl From<[u8; 32]> for NullifierSeed { fn from(bytes: [u8; 32]) -> Self { - Self(bytes) + Self(bytes.into()) } } impl From for [u8; 32] { fn from(rho: NullifierSeed) -> Self { - rho.0 + *rho.0 } } @@ -37,22 +39,22 @@ impl From for [u8; 32] { any(test, feature = "proptest-impl"), derive(proptest_derive::Arbitrary) )] -pub struct Nullifier(pub [u8; 32]); +pub struct Nullifier(pub HexDebug<[u8; 32]>); impl From<[u8; 32]> for Nullifier { fn from(bytes: [u8; 32]) -> Self { - Self(bytes) + Self(bytes.into()) } } impl From for [u8; 32] { fn from(n: Nullifier) -> Self { - n.0 + *n.0 } } impl From<&Nullifier> for [u8; 32] { fn from(n: &Nullifier) -> Self { - n.0 + *n.0 } } diff --git a/zebra-consensus/src/transaction/tests.rs b/zebra-consensus/src/transaction/tests.rs index e0d8738e2..b819a7291 100644 --- a/zebra-consensus/src/transaction/tests.rs +++ b/zebra-consensus/src/transaction/tests.rs @@ -963,7 +963,7 @@ fn v4_transaction_with_conflicting_sprout_nullifier_across_joinsplits_is_rejecte // Add a new joinsplit with the duplicate nullifier let mut new_joinsplit = joinsplit_data.first.clone(); new_joinsplit.nullifiers[0] = duplicate_nullifier; - new_joinsplit.nullifiers[1] = sprout::note::Nullifier([2u8; 32]); + new_joinsplit.nullifiers[1] = sprout::note::Nullifier([2u8; 32].into()); joinsplit_data.rest.push(new_joinsplit); @@ -1981,8 +1981,8 @@ fn mock_sprout_join_split_data() -> (JoinSplitData, ed25519::Signi .try_into() .expect("Invalid JoinSplit transparent input"); let anchor = sprout::tree::Root::default(); - let first_nullifier = sprout::note::Nullifier([0u8; 32]); - let second_nullifier = sprout::note::Nullifier([1u8; 32]); + let first_nullifier = sprout::note::Nullifier([0u8; 32].into()); + let second_nullifier = sprout::note::Nullifier([1u8; 32].into()); let commitment = sprout::commitment::NoteCommitment::from([0u8; 32]); let ephemeral_key = x25519::PublicKey::from(&x25519::EphemeralSecret::new(rand::thread_rng())); let random_seed = sprout::RandomSeed::from([0u8; 32]); diff --git a/zebra-state/src/service/finalized_state/disk_format/shielded.rs b/zebra-state/src/service/finalized_state/disk_format/shielded.rs index 4cd758989..8836549c3 100644 --- a/zebra-state/src/service/finalized_state/disk_format/shielded.rs +++ b/zebra-state/src/service/finalized_state/disk_format/shielded.rs @@ -15,7 +15,7 @@ impl IntoDisk for sprout::Nullifier { type Bytes = [u8; 32]; fn as_bytes(&self) -> Self::Bytes { - self.0 + *self.0 } } @@ -23,7 +23,7 @@ impl IntoDisk for sapling::Nullifier { type Bytes = [u8; 32]; fn as_bytes(&self) -> Self::Bytes { - self.0 + *self.0 } }