From 432b680a513ada1e2afa764f98acd600dbf4c587 Mon Sep 17 00:00:00 2001 From: teor Date: Mon, 16 Jan 2023 11:50:29 +1000 Subject: [PATCH] Format redpallas keys as hex when debugging --- zebra-chain/src/orchard/arbitrary.rs | 4 ++-- zebra-chain/src/primitives/redpallas/batch.rs | 6 +++--- .../src/primitives/redpallas/signature.rs | 13 ++++++++----- .../src/primitives/redpallas/signing_key.rs | 7 ++++--- .../primitives/redpallas/verification_key.rs | 18 +++++++++++------- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/zebra-chain/src/orchard/arbitrary.rs b/zebra-chain/src/orchard/arbitrary.rs index 78b8141d039..28f1201f2aa 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/redpallas/batch.rs b/zebra-chain/src/primitives/redpallas/batch.rs index a8bb9175989..95ecbcbf150 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 aae7f30e962..8f8d3a07f7c 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 bce9a3b0b2d..0570ee27523 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 c047382c31d..c523ab88739 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 {