From 75cad3bb0a7cd2d8e6fd839e1a339fa085fa4fd4 Mon Sep 17 00:00:00 2001 From: Deirdre Connolly Date: Sun, 9 Aug 2020 21:08:38 -0400 Subject: [PATCH] Impl TryFrom's for Diversifier and use those to construct Sapling NoteCommitments --- zebra-chain/src/commitments/sapling.rs | 16 +++++++++---- zebra-chain/src/keys/sapling.rs | 32 ++++++++++++++++++-------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/zebra-chain/src/commitments/sapling.rs b/zebra-chain/src/commitments/sapling.rs index 6f2089d8..5a1195bb 100644 --- a/zebra-chain/src/commitments/sapling.rs +++ b/zebra-chain/src/commitments/sapling.rs @@ -82,7 +82,7 @@ impl NoteCommitment { diversifier: Diversifier, transmission_key: TransmissionKey, value: Amount, - ) -> (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` impls for the `jubjub::*Point`s handles + // The `TryFrom` 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 (?) diff --git a/zebra-chain/src/keys/sapling.rs b/zebra-chain/src/keys/sapling.rs index d1d922f8..860e7d0c 100644 --- a/zebra-chain/src/keys/sapling.rs +++ b/zebra-chain/src/keys/sapling.rs @@ -629,19 +629,31 @@ impl From for [u8; 11] { } } -impl From 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 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 { + if let Ok(extended_point) = jubjub::ExtendedPoint::try_from(d) { + Ok(extended_point.into()) + } else { + Err("Invalid Diversifier -> jubjub::AffinePoint") + } } } -impl From 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 for jubjub::ExtendedPoint { + type Error = &'static str; + + fn try_from(d: Diversifier) -> Result { + let possible_point = diversify_hash(d.0); + + if let Some(point) = possible_point { + Ok(point) + } else { + Err("Invalid Diversifier -> jubjub::ExtendedPoint") + } } }