Impl TryFrom's for Diversifier and use those to construct Sapling NoteCommitments
This commit is contained in:
parent
c8771ef620
commit
75cad3bb0a
|
|
@ -82,7 +82,7 @@ impl NoteCommitment {
|
||||||
diversifier: Diversifier,
|
diversifier: Diversifier,
|
||||||
transmission_key: TransmissionKey,
|
transmission_key: TransmissionKey,
|
||||||
value: Amount<NonNegative>,
|
value: Amount<NonNegative>,
|
||||||
) -> (CommitmentRandomness, Self)
|
) -> Option<(CommitmentRandomness, Self)>
|
||||||
where
|
where
|
||||||
T: RngCore + CryptoRng,
|
T: RngCore + CryptoRng,
|
||||||
{
|
{
|
||||||
|
|
@ -95,9 +95,15 @@ impl NoteCommitment {
|
||||||
// Jubjub repr_J canonical byte encoding
|
// Jubjub repr_J canonical byte encoding
|
||||||
// https://zips.z.cash/protocol/protocol.pdf#jubjub
|
// 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.
|
// 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 pk_d_bytes = <[u8; 32]>::from(transmission_key);
|
||||||
let v_bytes = value.to_bytes();
|
let v_bytes = value.to_bytes();
|
||||||
|
|
||||||
|
|
@ -107,10 +113,10 @@ impl NoteCommitment {
|
||||||
|
|
||||||
let rcm = CommitmentRandomness(generate_trapdoor(csprng));
|
let rcm = CommitmentRandomness(generate_trapdoor(csprng));
|
||||||
|
|
||||||
(
|
Some((
|
||||||
rcm,
|
rcm,
|
||||||
NoteCommitment::from(windowed_pedersen_commitment(rcm.0, &s)),
|
NoteCommitment::from(windowed_pedersen_commitment(rcm.0, &s)),
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Hash Extractor for Jubjub (?)
|
/// Hash Extractor for Jubjub (?)
|
||||||
|
|
|
||||||
|
|
@ -629,19 +629,31 @@ impl From<Diversifier> for [u8; 11] {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Diversifier> for jubjub::AffinePoint {
|
impl TryFrom<Diversifier> for jubjub::AffinePoint {
|
||||||
/// Get a diversified base point from a diversifier value in
|
type Error = &'static str;
|
||||||
/// affine representation
|
|
||||||
fn from(d: Diversifier) -> jubjub::AffinePoint {
|
/// Get a diversified base point from a diversifier value in affine
|
||||||
jubjub::ExtendedPoint::from(d).into()
|
/// 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 {
|
impl TryFrom<Diversifier> for jubjub::ExtendedPoint {
|
||||||
/// Get a diversified base point from a diversifier value in
|
type Error = &'static str;
|
||||||
/// extended representation
|
|
||||||
fn from(d: Diversifier) -> jubjub::ExtendedPoint {
|
fn try_from(d: Diversifier) -> Result<Self, Self::Error> {
|
||||||
diversify_hash(d.0).unwrap()
|
let possible_point = diversify_hash(d.0);
|
||||||
|
|
||||||
|
if let Some(point) = possible_point {
|
||||||
|
Ok(point)
|
||||||
|
} else {
|
||||||
|
Err("Invalid Diversifier -> jubjub::ExtendedPoint")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue