Skip to content

Commit

Permalink
[zk-token-sdk] Add a length check on range proof commitment length (#…
Browse files Browse the repository at this point in the history
…34165)

add a length check on range proof commitment length
  • Loading branch information
samkim-crypto committed Nov 20, 2023
1 parent c73f226 commit e251b86
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions zk-token-sdk/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub enum ProofVerificationError {
ElGamal(#[from] ElGamalError),
#[error("Invalid proof context")]
ProofContext,
#[error("illegal commitment length")]
IllegalCommitmentLength,
}

#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
crate::{
encryption::pedersen::{PedersenCommitment, PedersenOpening},
errors::{ProofGenerationError, ProofVerificationError},
instruction::batched_range_proof::MAX_COMMITMENTS,
range_proof::RangeProof,
},
std::convert::TryInto,
Expand Down Expand Up @@ -77,6 +78,12 @@ impl ZkProofData<BatchedRangeProofContext> for BatchedRangeProofU128Data {
#[cfg(not(target_os = "solana"))]
fn verify_proof(&self) -> Result<(), ProofVerificationError> {
let (commitments, bit_lengths) = self.context.try_into()?;
let num_commitments = commitments.len();

if num_commitments > MAX_COMMITMENTS || num_commitments != bit_lengths.len() {
return Err(ProofVerificationError::IllegalCommitmentLength);
}

let mut transcript = self.context_data().new_transcript();
let proof: RangeProof = self.proof.try_into()?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
crate::{
encryption::pedersen::{PedersenCommitment, PedersenOpening},
errors::{ProofGenerationError, ProofVerificationError},
instruction::batched_range_proof::MAX_COMMITMENTS,
range_proof::RangeProof,
},
std::convert::TryInto,
Expand Down Expand Up @@ -74,6 +75,12 @@ impl ZkProofData<BatchedRangeProofContext> for BatchedRangeProofU256Data {
#[cfg(not(target_os = "solana"))]
fn verify_proof(&self) -> Result<(), ProofVerificationError> {
let (commitments, bit_lengths) = self.context.try_into()?;
let num_commitments = commitments.len();

if num_commitments > MAX_COMMITMENTS || num_commitments != bit_lengths.len() {
return Err(ProofVerificationError::IllegalCommitmentLength);
}

let mut transcript = self.context_data().new_transcript();
let proof: RangeProof = self.proof.try_into()?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
crate::{
encryption::pedersen::{PedersenCommitment, PedersenOpening},
errors::{ProofGenerationError, ProofVerificationError},
instruction::batched_range_proof::MAX_COMMITMENTS,
range_proof::RangeProof,
},
std::convert::TryInto,
Expand Down Expand Up @@ -76,6 +77,12 @@ impl ZkProofData<BatchedRangeProofContext> for BatchedRangeProofU64Data {
#[cfg(not(target_os = "solana"))]
fn verify_proof(&self) -> Result<(), ProofVerificationError> {
let (commitments, bit_lengths) = self.context.try_into()?;
let num_commitments = commitments.len();

if num_commitments > MAX_COMMITMENTS || num_commitments != bit_lengths.len() {
return Err(ProofVerificationError::IllegalCommitmentLength);
}

let mut transcript = self.context_data().new_transcript();
let proof: RangeProof = self.proof.try_into()?;

Expand Down

0 comments on commit e251b86

Please sign in to comment.