Skip to content

Commit 391d135

Browse files
committed
nit: simplify code for range proofs.
1 parent 39f5f80 commit 391d135

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

src/linear_relation/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,24 @@ impl<G: PrimeGroup> LinearRelation<G> {
407407
vars
408408
}
409409

410+
411+
/// Allocates a vector of new scalar variables.
412+
///
413+
/// # Returns
414+
/// A vector of [`ScalarVar`] representing the newly allocated scalar indices.
415+
/// /// # Example
416+
/// ```
417+
/// # use sigma_rs::LinearRelation;
418+
/// use curve25519_dalek::RistrettoPoint as G;
419+
///
420+
/// let mut relation = LinearRelation::<G>::new();
421+
/// let [var_x, var_y] = relation.allocate_scalars();
422+
/// let vars = relation.allocate_scalars_vec(10);
423+
/// ```
424+
pub fn allocate_scalars_vec(&mut self, n: usize) -> Vec<ScalarVar<G>> {
425+
(0..n).map(|_| self.allocate_scalar()).collect()
426+
}
427+
410428
/// Allocates a point variable (group element) for use in the linear map.
411429
pub fn allocate_element(&mut self) -> GroupVar<G> {
412430
self.linear_map.num_elements += 1;
@@ -442,6 +460,24 @@ impl<G: PrimeGroup> LinearRelation<G> {
442460
vars
443461
}
444462

463+
/// Allocates a vector of new point variables (group elements).
464+
///
465+
/// # Returns
466+
/// A vector of [`GroupVar`] representing the newly allocated group element indices.
467+
///
468+
/// # Example
469+
/// ```
470+
/// # use sigma_rs::LinearRelation;
471+
/// use curve25519_dalek::RistrettoPoint as G;
472+
/// let mut relation = LinearRelation::<G>::new();
473+
/// let [var_g, var_h
474+
/// ] = relation.allocate_elements();
475+
/// let vars = relation.allocate_elements_vec(10);
476+
/// ```
477+
pub fn allocate_elements_vec(&mut self, n: usize) -> Vec<GroupVar<G>> {
478+
(0..n).map(|_| self.allocate_element()).collect()
479+
}
480+
445481
/// Allocates a point variable (group element) and sets it immediately to the given value.
446482
pub fn allocate_elements_with(&mut self, elements: &[G]) -> Vec<GroupVar<G>> {
447483
elements

src/tests/test_relations.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ pub fn pedersen_commitment_dleq<G: PrimeGroup, R: RngCore>(
201201

202202
/// Test that a Pedersen commitment is in the given range.
203203
#[allow(non_snake_case)]
204-
pub fn test_range_for_input_and_range<G: PrimeGroup, R: RngCore>(
204+
pub fn range_instance_generation<G: PrimeGroup, R: RngCore>(
205205
mut rng: &mut R,
206206
input: u64,
207207
range: std::ops::Range<u64>,
@@ -222,18 +222,10 @@ pub fn test_range_for_input_and_range<G: PrimeGroup, R: RngCore>(
222222
let mut instance = LinearRelation::new();
223223
let [var_G, var_H] = instance.allocate_elements();
224224
let [var_x, var_r] = instance.allocate_scalars();
225-
let vars_b = std::iter::repeat_with(|| instance.allocate_scalar())
226-
.take(bases.len())
227-
.collect::<Vec<_>>();
228-
let vars_s = std::iter::repeat_with(|| instance.allocate_scalar())
229-
.take(bases.len())
230-
.collect::<Vec<_>>();
231-
let var_s2 = std::iter::repeat_with(|| instance.allocate_scalar())
232-
.take(bases.len())
233-
.collect::<Vec<_>>();
234-
let var_Ds = std::iter::repeat_with(|| instance.allocate_element())
235-
.take(bases.len())
236-
.collect::<Vec<_>>();
225+
let vars_b = instance.allocate_scalars_vec(bases.len());
226+
let vars_s = instance.allocate_scalars_vec(bases.len());
227+
let var_s2 = instance.allocate_scalars_vec(bases.len());
228+
let var_Ds = instance.allocate_elements_vec(bases.len());
237229

238230
// `var_Ds[i]` are bit commitments.
239231
for i in 0..bases.len() {
@@ -311,7 +303,7 @@ pub fn test_range_for_input_and_range<G: PrimeGroup, R: RngCore>(
311303
pub fn test_range<G: PrimeGroup, R: RngCore>(
312304
mut rng: &mut R,
313305
) -> (CanonicalLinearRelation<G>, Vec<G::Scalar>) {
314-
test_range_for_input_and_range(&mut rng, 822, 0..1337)
306+
range_instance_generation(&mut rng, 822, 0..1337)
315307
}
316308

317309
/// LinearMap for knowledge of an opening for use in a BBS commitment.

0 commit comments

Comments
 (0)