From 75573d331a83dc2b78b302c9e0b85cb07570b4b1 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Fri, 23 Apr 2021 12:53:13 -0600 Subject: [PATCH] Add canonical byte conversions for value commitments. --- src/bundle.rs | 2 +- src/value.rs | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/bundle.rs b/src/bundle.rs index 9489d578..55834c45 100644 --- a/src/bundle.rs +++ b/src/bundle.rs @@ -7,7 +7,7 @@ use crate::{ note::{ExtractedNoteCommitment, Nullifier, TransmittedNoteCiphertext}, primitives::redpallas::{self, Binding, SpendAuth}, tree::Anchor, - value::{ValueCommitment}, + value::ValueCommitment, }; /// An action applied to the global ledger. diff --git a/src/value.rs b/src/value.rs index 08c35fe2..20b9f1c8 100644 --- a/src/value.rs +++ b/src/value.rs @@ -14,7 +14,7 @@ //! [`Action`]: crate::bundle::Action //! [`Bundle`]: crate::bundle::Bundle -use std::convert::{TryInto, TryFrom}; +use std::convert::{TryFrom, TryInto}; use std::fmt::{self, Debug}; use std::iter::Sum; use std::ops::{Add, Sub}; @@ -27,6 +27,7 @@ use pasta_curves::{ pallas, }; use rand::RngCore; +use subtle::CtOption; use crate::primitives::redpallas::{self, Binding}; @@ -214,6 +215,16 @@ impl ValueCommitment { // TODO: impl From for redpallas::VerificationKey. self.0.to_bytes().try_into().unwrap() } + + /// Deserialize a value commitment from its byte representation + pub fn from_bytes(bytes: &[u8; 32]) -> CtOption { + 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)]