Skip to content

Commit

Permalink
Format Sprout commitments 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 a5c19cc commit 490a30a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
2 changes: 2 additions & 0 deletions zebra-chain/src/primitives/proofs/bctv14.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! BCTV14 proofs for Zebra.

use std::{fmt, io};

use serde::{Deserialize, Serialize};
Expand Down
2 changes: 2 additions & 0 deletions zebra-chain/src/primitives/proofs/groth16.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Groth16 proofs for Zebra.

use std::{fmt, io};

use serde::{Deserialize, Serialize};
Expand Down
18 changes: 11 additions & 7 deletions zebra-chain/src/sprout/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
}
}

Expand All @@ -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())
}
}

Expand All @@ -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
}
}
7 changes: 4 additions & 3 deletions zebra-chain/src/sprout/joinsplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
}

Expand Down
16 changes: 11 additions & 5 deletions zebra-chain/src/sprout/note/mac.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
}

Expand All @@ -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()))
}
}

Expand Down

0 comments on commit 490a30a

Please sign in to comment.