Add canonical byte conversions for value commitments.

This commit is contained in:
Kris Nuttycombe 2021-04-23 12:53:13 -06:00
parent fc0f55d82b
commit 75573d331a
2 changed files with 13 additions and 2 deletions

View File

@ -7,7 +7,7 @@ use crate::{
note::{ExtractedNoteCommitment, Nullifier, TransmittedNoteCiphertext}, note::{ExtractedNoteCommitment, Nullifier, TransmittedNoteCiphertext},
primitives::redpallas::{self, Binding, SpendAuth}, primitives::redpallas::{self, Binding, SpendAuth},
tree::Anchor, tree::Anchor,
value::{ValueCommitment}, value::ValueCommitment,
}; };
/// An action applied to the global ledger. /// An action applied to the global ledger.

View File

@ -14,7 +14,7 @@
//! [`Action`]: crate::bundle::Action //! [`Action`]: crate::bundle::Action
//! [`Bundle`]: crate::bundle::Bundle //! [`Bundle`]: crate::bundle::Bundle
use std::convert::{TryInto, TryFrom}; use std::convert::{TryFrom, TryInto};
use std::fmt::{self, Debug}; use std::fmt::{self, Debug};
use std::iter::Sum; use std::iter::Sum;
use std::ops::{Add, Sub}; use std::ops::{Add, Sub};
@ -27,6 +27,7 @@ use pasta_curves::{
pallas, pallas,
}; };
use rand::RngCore; use rand::RngCore;
use subtle::CtOption;
use crate::primitives::redpallas::{self, Binding}; use crate::primitives::redpallas::{self, Binding};
@ -214,6 +215,16 @@ impl ValueCommitment {
// TODO: impl From<pallas::Point> for redpallas::VerificationKey. // TODO: impl From<pallas::Point> for redpallas::VerificationKey.
self.0.to_bytes().try_into().unwrap() self.0.to_bytes().try_into().unwrap()
} }
/// Deserialize a value commitment from its byte representation
pub fn from_bytes(bytes: &[u8; 32]) -> CtOption<ValueCommitment> {
pallas::Point::from_bytes(bytes).map(ValueCommitment)
}
/// Serialize this value commitment to its canonical byte representation.
pub fn to_bytes(&self) -> [u8; 32] {
self.0.to_bytes()
}
} }
#[cfg(test)] #[cfg(test)]