Skip to content

Commit

Permalink
Format redpallas keys as hex when debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Jan 16, 2023
1 parent 60e368c commit 910f9b7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
4 changes: 2 additions & 2 deletions zebra-chain/src/orchard/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ impl Arbitrary for Signature<SpendAuth> {
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(array::uniform32(any::<u8>()), array::uniform32(any::<u8>()))
.prop_map(|(r_bytes, s_bytes)| Self {
r_bytes,
s_bytes,
r_bytes: r_bytes.into(),
s_bytes: s_bytes.into(),
_marker: PhantomData,
})
.boxed()
Expand Down
6 changes: 3 additions & 3 deletions zebra-chain/src/primitives/redpallas/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -258,10 +258,10 @@ impl Verifier {
//
// This validates the `rk` element, whose type is
// SpendAuthSig^{Orchard}.Public, i.e. ℙ.
VerificationKey::<SpendAuth>::try_from(vk_bytes.bytes)?.point
VerificationKey::<SpendAuth>::try_from(*vk_bytes.bytes)?.point
}
Inner::Binding { vk_bytes, .. } => {
VerificationKey::<Binding>::try_from(vk_bytes.bytes)?.point
VerificationKey::<Binding>::try_from(*vk_bytes.bytes)?.point
}
};

Expand Down
13 changes: 8 additions & 5 deletions zebra-chain/src/primitives/redpallas/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: SigType> {
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<T>,
}

Expand All @@ -29,8 +32,8 @@ impl<T: SigType> From<[u8; 64]> for Signature<T> {
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,
}
}
Expand Down
7 changes: 4 additions & 3 deletions zebra-chain/src/primitives/redpallas/signing_key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::convert::{TryFrom, TryInto};
//! Redpallas signing keys for Zebra.

use std::marker::PhantomData;

use group::{ff::PrimeField, GroupEncoding};
Expand Down Expand Up @@ -117,8 +118,8 @@ impl<T: SigType> SigningKey<T> {
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,
}
}
Expand Down
18 changes: 11 additions & 7 deletions zebra-chain/src/primitives/redpallas/verification_key.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,22 +17,22 @@ use super::*;
/// used in signature verification.
#[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct VerificationKeyBytes<T: SigType> {
pub(crate) bytes: [u8; 32],
pub(crate) bytes: HexDebug<[u8; 32]>,
pub(crate) _marker: PhantomData<T>,
}

impl<T: SigType> From<[u8; 32]> for VerificationKeyBytes<T> {
fn from(bytes: [u8; 32]) -> VerificationKeyBytes<T> {
VerificationKeyBytes {
bytes,
bytes: bytes.into(),
_marker: PhantomData,
}
}
}

impl<T: SigType> From<VerificationKeyBytes<T>> for [u8; 32] {
fn from(refined: VerificationKeyBytes<T>) -> [u8; 32] {
refined.bytes
*refined.bytes
}
}

Expand Down Expand Up @@ -65,7 +69,7 @@ impl<T: SigType> From<VerificationKey<T>> for VerificationKeyBytes<T> {

impl<T: SigType> From<VerificationKey<T>> for [u8; 32] {
fn from(pk: VerificationKey<T>) -> [u8; 32] {
pk.bytes.bytes
*pk.bytes.bytes
}
}

Expand Down Expand Up @@ -107,7 +111,7 @@ impl VerificationKey<SpendAuth> {
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 }
Expand All @@ -118,7 +122,7 @@ impl<T: SigType> VerificationKey<T> {
pub(crate) fn from_scalar(s: &pallas::Scalar) -> VerificationKey<T> {
let point = T::basepoint() * s;
let bytes = VerificationKeyBytes {
bytes: point.to_bytes(),
bytes: point.to_bytes().into(),
_marker: PhantomData,
};
VerificationKey { point, bytes }
Expand Down Expand Up @@ -154,7 +158,7 @@ impl<T: SigType> VerificationKey<T> {

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 {
Expand Down

0 comments on commit 910f9b7

Please sign in to comment.