Impl TryFrom's for Diversifier and use those to construct Sapling NoteCommitments

This commit is contained in:
Deirdre Connolly 2020-08-09 21:08:38 -04:00 committed by Deirdre Connolly
parent c8771ef620
commit 75cad3bb0a
2 changed files with 33 additions and 15 deletions

View File

@ -82,7 +82,7 @@ impl NoteCommitment {
diversifier: Diversifier,
transmission_key: TransmissionKey,
value: Amount<NonNegative>,
) -> (CommitmentRandomness, Self)
) -> Option<(CommitmentRandomness, Self)>
where
T: RngCore + CryptoRng,
{
@ -95,9 +95,15 @@ impl NoteCommitment {
// Jubjub repr_J canonical byte encoding
// https://zips.z.cash/protocol/protocol.pdf#jubjub
//
// The `From<Diversifier>` impls for the `jubjub::*Point`s handles
// The `TryFrom<Diversifier>` impls for the `jubjub::*Point`s handles
// calling `DiversifyHash` implicitly.
let g_d_bytes = jubjub::AffinePoint::from(diversifier).to_bytes();
let g_d_bytes: [u8; 32];
if let Ok(g_d) = jubjub::AffinePoint::try_from(diversifier) {
g_d_bytes = g_d.to_bytes();
} else {
return None;
}
let pk_d_bytes = <[u8; 32]>::from(transmission_key);
let v_bytes = value.to_bytes();
@ -107,10 +113,10 @@ impl NoteCommitment {
let rcm = CommitmentRandomness(generate_trapdoor(csprng));
(
Some((
rcm,
NoteCommitment::from(windowed_pedersen_commitment(rcm.0, &s)),
)
))
}
/// Hash Extractor for Jubjub (?)

View File

@ -629,19 +629,31 @@ impl From<Diversifier> for [u8; 11] {
}
}
impl From<Diversifier> for jubjub::AffinePoint {
/// Get a diversified base point from a diversifier value in
/// affine representation
fn from(d: Diversifier) -> jubjub::AffinePoint {
jubjub::ExtendedPoint::from(d).into()
impl TryFrom<Diversifier> for jubjub::AffinePoint {
type Error = &'static str;
/// Get a diversified base point from a diversifier value in affine
/// representation.
fn try_from(d: Diversifier) -> Result<Self, Self::Error> {
if let Ok(extended_point) = jubjub::ExtendedPoint::try_from(d) {
Ok(extended_point.into())
} else {
Err("Invalid Diversifier -> jubjub::AffinePoint")
}
}
}
impl From<Diversifier> for jubjub::ExtendedPoint {
/// Get a diversified base point from a diversifier value in
/// extended representation
fn from(d: Diversifier) -> jubjub::ExtendedPoint {
diversify_hash(d.0).unwrap()
impl TryFrom<Diversifier> for jubjub::ExtendedPoint {
type Error = &'static str;
fn try_from(d: Diversifier) -> Result<Self, Self::Error> {
let possible_point = diversify_hash(d.0);
if let Some(point) = possible_point {
Ok(point)
} else {
Err("Invalid Diversifier -> jubjub::ExtendedPoint")
}
}
}