From be7ea200c89ec5068b53dbb46fbbb0f44c52cfd1 Mon Sep 17 00:00:00 2001 From: Deirdre Connolly Date: Fri, 7 Aug 2020 04:42:46 -0400 Subject: [PATCH] Accept an Amount for the value arg of a ValueCommitment constructor --- zebra-chain/src/commitments/sapling.rs | 5 ++--- zebra-chain/src/types/amount.rs | 10 ++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/zebra-chain/src/commitments/sapling.rs b/zebra-chain/src/commitments/sapling.rs index b56e1e2a..11cc6fa2 100644 --- a/zebra-chain/src/commitments/sapling.rs +++ b/zebra-chain/src/commitments/sapling.rs @@ -316,13 +316,12 @@ impl ValueCommitment { /// Generate a new _ValueCommitment_. /// /// https://zips.z.cash/protocol/protocol.pdf#concretehomomorphiccommit - // TODO: accept an Amount instead? #[allow(non_snake_case)] - pub fn new(csprng: &mut T, value_bytes: [u8; 32]) -> Self + pub fn new(csprng: &mut T, value: Amount) -> Self where T: RngCore + CryptoRng, { - let v = jubjub::Fr::from_bytes(&value_bytes).unwrap(); + let v = jubjub::Fr::from(value); let rcv = generate_trapdoor(csprng); let V = find_group_hash(*b"Zcash_cv", b"v"); diff --git a/zebra-chain/src/types/amount.rs b/zebra-chain/src/types/amount.rs index 7efe8740..3747a05e 100644 --- a/zebra-chain/src/types/amount.rs +++ b/zebra-chain/src/types/amount.rs @@ -132,6 +132,16 @@ impl From> for u64 { } } +impl From> for jubjub::Fr { + fn from(a: Amount) -> jubjub::Fr { + if a.0 < 0 { + jubjub::Fr::from(a.0.abs() as u64).neg() + } else { + jubjub::Fr::from(a.0 as u64) + } + } +} + impl TryFrom for Amount where C: AmountConstraint,