Accept an Amount<NonNegative> for the value arg of a ValueCommitment constructor

This commit is contained in:
Deirdre Connolly 2020-08-07 04:42:46 -04:00 committed by Deirdre Connolly
parent e024e43896
commit be7ea200c8
2 changed files with 12 additions and 3 deletions

View File

@ -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<T>(csprng: &mut T, value_bytes: [u8; 32]) -> Self
pub fn new<T>(csprng: &mut T, value: Amount<NonNegative>) -> 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");

View File

@ -132,6 +132,16 @@ impl From<Amount<NonNegative>> for u64 {
}
}
impl<C> From<Amount<C>> for jubjub::Fr {
fn from(a: Amount<C>) -> jubjub::Fr {
if a.0 < 0 {
jubjub::Fr::from(a.0.abs() as u64).neg()
} else {
jubjub::Fr::from(a.0 as u64)
}
}
}
impl<C> TryFrom<i64> for Amount<C>
where
C: AmountConstraint,