Skip to content

Commit

Permalink
fix: avoids copying in the commit method (#12)
Browse files Browse the repository at this point in the history
The PR improves `PedersenGens::commit` method:

- [x] Internal copying: avoid copying values inside the method (use iters)
- [x] External copying: avoid copying on calls (support `AsRef<Scalar>` args)

UPD: The `Borrow` trait used (instead of `AsRef`), because it's the requirement of the [`multiscalar_mul`][1] method in the `curve25519-dalek` crate.

[1]: https://github.com/dalek-cryptography/curve25519-dalek/blob/0d49dfacf66bed4b41e445d0e6942b3c27f3b263/src/traits.rs#L114

* fix: avoids copying in the commit method

* fix: allow to borrow items as Scalar

* fix: expect a reference to a value

* fix: tests for PedersenGens

* fix: benchmarks for PedersenGens
  • Loading branch information
therustmonk authored May 31, 2022
1 parent c4dffb2 commit ab4c432
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 20 deletions.
6 changes: 3 additions & 3 deletions benches/range_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn create_aggregated_rangeproof_helper(bit_length: usize, extension_degree: Exte
commitments.push(
generators
.pc_gens()
.commit(Scalar::from(value), blindings.as_slice())
.commit(&Scalar::from(value), blindings.as_slice())
.unwrap(),
);
openings.push(CommitmentOpening::new(value, blindings.clone()));
Expand Down Expand Up @@ -145,7 +145,7 @@ fn verify_aggregated_rangeproof_helper(bit_length: usize, extension_degree: Exte
commitments.push(
generators
.pc_gens()
.commit(Scalar::from(value), blindings.as_slice())
.commit(&Scalar::from(value), blindings.as_slice())
.unwrap(),
);
openings.push(CommitmentOpening::new(value, blindings.clone()));
Expand Down Expand Up @@ -223,7 +223,7 @@ fn verify_batched_rangeproofs_helper(bit_length: usize, extension_degree: Extens
generators.clone(),
vec![generators
.pc_gens()
.commit(Scalar::from(value), blindings.as_slice())
.commit(&Scalar::from(value), blindings.as_slice())
.unwrap()],
vec![Some(value / 3)],
seed_nonce,
Expand Down
20 changes: 7 additions & 13 deletions src/generators/pedersen_gens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Copyright (c) 2018 Chain, Inc.
// SPDX-License-Identifier: MIT

use std::convert::TryFrom;
use std::{borrow::Borrow, convert::TryFrom, iter::once};

use curve25519_dalek::{scalar::Scalar, traits::MultiscalarMul};

Expand Down Expand Up @@ -93,21 +93,15 @@ impl<P> PedersenGens<P>
where P: Compressable + MultiscalarMul<Point = P> + Clone
{
/// Creates a Pedersen commitment using the value scalar and a blinding factor vector
pub fn commit(&self, value: Scalar, blindings: &[Scalar]) -> Result<P, ProofError> {
pub fn commit<T>(&self, value: &T, blindings: &[T]) -> Result<P, ProofError>
where for<'a> &'a T: Borrow<Scalar> {
if blindings.is_empty() || blindings.len() > self.extension_degree as usize {
Err(ProofError::InvalidLength("blinding vector".to_string()))
} else {
let mut scalars = Vec::with_capacity(1 + blindings.len());
scalars.push(value);
for item in blindings {
scalars.push(*item);
}
let mut points = Vec::with_capacity(1 + blindings.len());
points.push(self.h_base.clone());
for item in self.g_base_vec.iter().take(blindings.len()) {
points.push(item.clone());
}
Ok(P::multiscalar_mul(&scalars, &points))
let scalars = once(value).chain(blindings);
let g_base_head = self.g_base_vec.iter().take(blindings.len());
let points = once(&self.h_base).chain(g_base_head);
Ok(P::multiscalar_mul(scalars, points))
}
}
}
2 changes: 1 addition & 1 deletion src/range_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub const MAX_RANGE_PROOF_BIT_LENGTH: usize = 64;
/// commitments.push(
/// generators
/// .pc_gens()
/// .commit(Scalar::from(value), blindings.as_slice())
/// .commit(&Scalar::from(value), blindings.as_slice())
/// .unwrap(),
/// );
/// openings.push(CommitmentOpening::new(value, blindings.clone()));
Expand Down
4 changes: 2 additions & 2 deletions src/ristretto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ mod tests {
// All commitments where enough extended generators are available to enable multi-exponentiation
// multiplication of the blinding factor vector will be ok
if i > 0 && i <= extension_degree as usize {
assert!(pc_gens.commit(value, blindings[..i].to_owned().as_slice()).is_ok());
assert!(pc_gens.commit(&value, &blindings[..i]).is_ok());
} else {
assert!(pc_gens.commit(value, blindings[..i].to_owned().as_slice()).is_err());
assert!(pc_gens.commit(&value, &blindings[..i]).is_err());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ristretto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn prove_and_verify(
commitments.push(
generators
.pc_gens()
.commit(Scalar::from(value), blindings.as_slice())
.commit(&Scalar::from(value), blindings.as_slice())
.unwrap(),
);
openings.push(CommitmentOpening::new(value, blindings.clone()));
Expand Down

0 comments on commit ab4c432

Please sign in to comment.