From 7439d2424b1ab425ea74f257f77c92f42a1f6801 Mon Sep 17 00:00:00 2001 From: Sam Kim Date: Tue, 4 Jan 2022 07:31:24 -0500 Subject: [PATCH] feat: add a separate TranscriptError --- zk-token-sdk/src/errors.rs | 6 ++++++ zk-token-sdk/src/range_proof/errors.rs | 11 ++++++++++- zk-token-sdk/src/range_proof/mod.rs | 11 +++-------- zk-token-sdk/src/transcript.rs | 8 ++++---- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/zk-token-sdk/src/errors.rs b/zk-token-sdk/src/errors.rs index cd846199c..d494fde0c 100644 --- a/zk-token-sdk/src/errors.rs +++ b/zk-token-sdk/src/errors.rs @@ -10,3 +10,9 @@ pub enum ProofError { )] InconsistentCTData, } + +#[derive(Error, Clone, Debug, Eq, PartialEq)] +pub enum TranscriptError { + #[error("point is the identity")] + ValidationError, +} diff --git a/zk-token-sdk/src/range_proof/errors.rs b/zk-token-sdk/src/range_proof/errors.rs index fd33bb58f..f9f7b3410 100644 --- a/zk-token-sdk/src/range_proof/errors.rs +++ b/zk-token-sdk/src/range_proof/errors.rs @@ -1,8 +1,9 @@ //! Errors related to proving and verifying proofs. use thiserror::Error; +use crate::errors::TranscriptError; #[derive(Error, Clone, Debug, Eq, PartialEq)] -pub enum ProofError { +pub enum RangeProofError { #[error("the required algebraic relation does not hold")] AlgebraicRelationError, #[error("malformed proof")] @@ -11,6 +12,14 @@ pub enum ProofError { InvalidBitsize, #[error("insufficient generators for the proof")] InvalidGeneratorsLength, + #[error("transcript failed to produce a challenge")] + TranscriptError, #[error("number of blinding factors do not match the number of values")] WrongNumBlindingFactors, } + +impl From for RangeProofError { + fn from(err: TranscriptError) -> Self { + Self::TranscriptError + } +} diff --git a/zk-token-sdk/src/range_proof/mod.rs b/zk-token-sdk/src/range_proof/mod.rs index f827bf9dd..37fbd89f8 100644 --- a/zk-token-sdk/src/range_proof/mod.rs +++ b/zk-token-sdk/src/range_proof/mod.rs @@ -8,8 +8,7 @@ use { use { crate::{ encryption::pedersen::PedersenBase, - errors::ProofError, - range_proof::{generators::BulletproofGens, inner_product::InnerProductProof}, + range_proof::{errors::RangeProofError, generators::BulletproofGens, inner_product::InnerProductProof}, transcript::TranscriptProtocol, }, core::iter, @@ -222,7 +221,7 @@ impl RangeProof { comms: Vec<&CompressedRistretto>, bit_lengths: Vec, transcript: &mut Transcript, - ) -> Result<(), ProofError> { + ) -> Result<(), RangeProofError> { let G = PedersenBase::default().G; let H = PedersenBase::default().H; @@ -231,11 +230,7 @@ impl RangeProof { let bp_gens = BulletproofGens::new(nm); if !nm.is_power_of_two() { - return Err(ProofError::InvalidBitsize); - } - - if !(nm == 8 || nm == 16 || nm == 32 || nm == 64 || nm == 128) { - return Err(ProofError::InvalidBitsize); + return Err(RangeProofError::InvalidBitsize); } transcript.validate_and_append_point(b"A", &self.A)?; diff --git a/zk-token-sdk/src/transcript.rs b/zk-token-sdk/src/transcript.rs index 836ef2635..d989e2c44 100644 --- a/zk-token-sdk/src/transcript.rs +++ b/zk-token-sdk/src/transcript.rs @@ -1,5 +1,5 @@ use { - crate::errors::ProofError, + crate::errors::TranscriptError, curve25519_dalek::{ristretto::CompressedRistretto, scalar::Scalar, traits::IsIdentity}, merlin::Transcript, }; @@ -40,7 +40,7 @@ pub trait TranscriptProtocol { &mut self, label: &'static [u8], point: &CompressedRistretto, - ) -> Result<(), ProofError>; + ) -> Result<(), TranscriptError>; /// Compute a `label`ed challenge variable. fn challenge_scalar(&mut self, label: &'static [u8]) -> Scalar; @@ -90,9 +90,9 @@ impl TranscriptProtocol for Transcript { &mut self, label: &'static [u8], point: &CompressedRistretto, - ) -> Result<(), ProofError> { + ) -> Result<(), TranscriptError> { if point.is_identity() { - Err(ProofError::VerificationError) + Err(TranscriptError::ValidationError) } else { self.append_message(label, point.as_bytes()); Ok(())