Merge branch 'hex-debug' into base-zcash-test-block-template

This commit is contained in:
teor 2023-01-16 12:36:23 +10:00
commit a243549d69
14 changed files with 78 additions and 51 deletions

View File

@ -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()

View File

@ -1,3 +1,5 @@
//! BCTV14 proofs for Zebra.
use std::{fmt, io};
use serde::{Deserialize, Serialize};

View File

@ -1,3 +1,5 @@
//! Groth16 proofs for Zebra.
use std::{fmt, io};
use serde::{Deserialize, Serialize};

View File

@ -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::<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
}
};

View File

@ -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>,
}
@ -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,
}
}

View File

@ -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<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,
}
}

View File

@ -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<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,
}
}
@ -28,7 +32,7 @@ impl<T: SigType> From<[u8; 32]> for VerificationKeyBytes<T> {
impl<T: SigType> From<VerificationKeyBytes<T>> for [u8; 32] {
fn from(refined: VerificationKeyBytes<T>) -> [u8; 32] {
refined.bytes
*refined.bytes
}
}
@ -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
}
}
@ -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 }
@ -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 }
@ -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 {

View File

@ -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<Nullifier> for [u8; 32] {
fn from(n: Nullifier) -> Self {
n.0
*n.0
}
}

View File

@ -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<Note> 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<NoteCommitment> 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
}
}

View File

@ -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<RandomSeed> for [u8; 32] {
fn from(rt: RandomSeed) -> [u8; 32] {
rt.0
*rt.0
}
}

View File

@ -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<Mac> for [u8; 32] {
fn from(rt: Mac) -> [u8; 32] {
rt.0
*rt.0
}
}
@ -34,7 +40,7 @@ impl ZcashDeserialize for Mac {
fn zcash_deserialize<R: Read>(mut reader: R) -> Result<Self, SerializationError> {
let bytes = reader.read_32_bytes()?;
Ok(Self(bytes))
Ok(Self(bytes.into()))
}
}

View File

@ -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<NullifierSeed> for [u8; 32] {
fn from(rho: NullifierSeed) -> Self {
rho.0
*rho.0
}
}
@ -37,22 +39,22 @@ impl From<NullifierSeed> 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<Nullifier> 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
}
}

View File

@ -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<Groth16Proof>, 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]);

View File

@ -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
}
}