Skip to content

Commit 608b20c

Browse files
committed
feat(morphism): add single allocation functions to Morphism
- feat: implement single allocate_x functions for Morphism structure
1 parent de3baa0 commit 608b20c

File tree

7 files changed

+56
-40
lines changed

7 files changed

+56
-40
lines changed

src/group_morphism.rs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
//! - [`GroupMorphismPreimage`]: a higher-level structure managing morphisms and their associated images
1010
1111
use group::{Group, GroupEncoding};
12-
use std::iter::repeat_n;
1312

1413
/// A wrapper representing an index for a scalar variable.
1514
///
@@ -168,10 +167,12 @@ where
168167
self.morphism.num_statements() * repr_len // total size of a commit
169168
}
170169

171-
/// Append a new equation relating scalars to group elements.
170+
/// Adds a new equation to the statement of the form:
171+
/// `lhs = Σ (scalar_i * point_i)`
172+
///
172173
/// # Parameters
173-
/// - `lhs`: The image group element variable (left-hand side of the equation).
174-
/// - `rhs`: A slice of `(ScalarVar, PointVar)` pairs representing the linear combination on the right-hand side.
174+
/// - `lhs`: The variable representing the left-hand group element
175+
/// - `rhs`: A list of (scalar variable, point variable) tuples for the linear combination
175176
pub fn append_equation(&mut self, lhs: PointVar, rhs: &[(ScalarVar, PointVar)]) {
176177
let lc = LinearCombination {
177178
scalar_indices: rhs.iter().map(|&(s, _)| s).collect(),
@@ -181,6 +182,12 @@ where
181182
self.image.push(lhs);
182183
}
183184

185+
/// Allocates a scalar variable for use in the morphism.
186+
pub fn allocate_scalar(&mut self) -> ScalarVar {
187+
self.morphism.num_scalars += 1;
188+
ScalarVar(self.morphism.num_scalars - 1)
189+
}
190+
184191
/// Allocates space for `n` new scalar variables.
185192
///
186193
/// # Parameters
@@ -189,9 +196,13 @@ where
189196
/// # Returns
190197
/// A vector of [`ScalarVar`] representing the newly allocated scalar indices.
191198
pub fn allocate_scalars(&mut self, n: usize) -> Vec<ScalarVar> {
192-
let start = self.morphism.num_scalars;
193-
self.morphism.num_scalars += n;
194-
(start..start + n).map(ScalarVar).collect()
199+
(0..n).map(|_| self.allocate_scalar()).collect()
200+
}
201+
202+
/// Allocates a point variable (group element) for use in the morphism.
203+
pub fn allocate_element(&mut self) -> PointVar {
204+
self.morphism.group_elements.push(G::identity());
205+
PointVar(self.morphism.group_elements.len() - 1)
195206
}
196207

197208
/// Allocates space for `n` new group elements, initialized to the identity element.
@@ -202,20 +213,25 @@ where
202213
/// # Returns
203214
/// A vector of [`PointVar`] representing the newly allocated group element indices.
204215
pub fn allocate_elements(&mut self, n: usize) -> Vec<PointVar> {
205-
let start = self.morphism.group_elements.len();
206-
self.morphism
207-
.group_elements
208-
.extend(repeat_n(G::identity(), n));
209-
(start..start + n).map(PointVar).collect::<Vec<_>>()
216+
(0..n).map(|_| self.allocate_element()).collect()
217+
}
218+
219+
/// Assign a group element value to a point variable.
220+
///
221+
/// # Parameters
222+
/// - `var`: The variable to assign.
223+
/// - `element`: The value to assign to the variable.
224+
pub fn assign_element(&mut self, var: PointVar, element: G) {
225+
self.morphism.group_elements[var.0] = element;
210226
}
211227

212-
/// Sets the values of group elements at specified indices.
228+
/// Assigns specific group elements to point variables (indices).
213229
///
214230
/// # Parameters
215-
/// - `elements`: A slice of `(PointVar, G)` tuples specifying which elements to update and their new values.
216-
pub fn set_elements(&mut self, elements: &[(PointVar, G)]) {
217-
for &(i, elt) in elements {
218-
self.morphism.group_elements[i.0] = elt;
231+
/// - `elements`: A list of `(PointVar, GroupElement)` pairs
232+
pub fn assign_elements(&mut self, elements: &[(PointVar, G)]) {
233+
for (var, element) in elements {
234+
self.assign_element(*var, *element)
219235
}
220236
}
221237

src/proof_builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ where
8181
///
8282
/// # Parameters
8383
/// - `elements`: A list of `(PointVar, GroupElement)` pairs.
84-
pub fn set_elements(&mut self, elements: &[(PointVar, G)]) {
85-
self.protocol.sigmap.set_elements(elements);
84+
pub fn assign_elements(&mut self, elements: &[(PointVar, G)]) {
85+
self.protocol.sigmap.assign_elements(elements);
8686
}
8787

8888
/// Returns the expected group element results (`lhs`) of the current equations.

src/schnorr_protocol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ impl<G: Group + GroupEncoding> SchnorrProtocol<G> {
5454
self.0.allocate_elements(n)
5555
}
5656

57-
pub fn set_elements(&mut self, elements: &[(PointVar, G)]) {
58-
self.0.set_elements(elements);
57+
pub fn assign_elements(&mut self, elements: &[(PointVar, G)]) {
58+
self.0.assign_elements(elements);
5959
}
6060

6161
pub fn evaluate(&self, scalars: &[<G as Group>::Scalar]) -> Vec<G> {

tests/morphism_preimage.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ fn discrete_logarithm<G: Group + GroupEncoding>(
2626
morphismp.append_equation(var_X, &[(var_x, var_G)]);
2727

2828
let G = G::generator();
29-
morphismp.set_elements(&[(var_G, G)]);
29+
morphismp.assign_elements(&[(var_G, G)]);
3030

3131
let x = G::Scalar::random(&mut *rng);
3232
let X = G * x;
3333
assert!(vec![X] == morphismp.morphism.evaluate(&[x]));
3434

35-
morphismp.set_elements(&[(var_X, X)]);
35+
morphismp.assign_elements(&[(var_X, X)]);
3636
(morphismp, vec![x])
3737
}
3838

@@ -54,7 +54,7 @@ fn dleq<G: Group + GroupEncoding>(
5454
let points = morphismp.allocate_elements(4);
5555
let (var_G, var_H, var_X, var_Y) = (points[0], points[1], points[2], points[3]);
5656

57-
morphismp.set_elements(&[(var_G, G), (var_H, H), (var_X, X), (var_Y, Y)]);
57+
morphismp.assign_elements(&[(var_G, G), (var_H, H), (var_X, X), (var_Y, Y)]);
5858
morphismp.append_equation(var_X, &[(var_x, var_G)]);
5959
morphismp.append_equation(var_Y, &[(var_x, var_H)]);
6060

@@ -82,7 +82,7 @@ fn pedersen_commitment<G: Group + GroupEncoding>(
8282
let points = cs.allocate_elements(3);
8383
let (var_G, var_H, var_C) = (points[0], points[1], points[2]);
8484

85-
cs.set_elements(&[(var_H, H), (var_G, G), (var_C, C)]);
85+
cs.assign_elements(&[(var_H, H), (var_G, G), (var_C, C)]);
8686
cs.append_equation(var_C, &[(var_x, var_G), (var_r, var_H)]);
8787

8888
assert!(vec![C] == cs.morphism.evaluate(&witness));
@@ -114,13 +114,13 @@ fn pedersen_commitment_dleq<G: Group + GroupEncoding>(
114114
let var_Gs = (points[0], points[1], points[2], points[3]);
115115
let (var_X, var_Y) = (points[4], points[5]);
116116

117-
morphismp.set_elements(&[
117+
morphismp.assign_elements(&[
118118
(var_Gs.0, generators[0]),
119119
(var_Gs.1, generators[1]),
120120
(var_Gs.2, generators[2]),
121121
(var_Gs.3, generators[3]),
122122
]);
123-
morphismp.set_elements(&[(var_X, X), (var_Y, Y)]);
123+
morphismp.assign_elements(&[(var_X, X), (var_Y, Y)]);
124124

125125
morphismp.append_equation(var_X, &[(var_x, var_Gs.0), (var_r, var_Gs.1)]);
126126
morphismp.append_equation(var_Y, &[(var_x, var_Gs.2), (var_r, var_Gs.3)]);
@@ -164,7 +164,7 @@ fn bbs_blind_commitment_computation<G: Group + GroupEncoding>(
164164
let (var_Q_2, var_J_1, var_J_2, var_J_3) = (points[0], points[1], points[2], points[3]);
165165
let var_C = points[M + 1];
166166

167-
morphismp.set_elements(&[
167+
morphismp.assign_elements(&[
168168
(var_Q_2, Q_2),
169169
(var_J_1, J_1),
170170
(var_J_2, J_2),

tests/proof_builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ fn discrete_logarithm() {
2222
proof_builder.append_equation(var_X, &[(var_x, var_G)]);
2323

2424
let G = G::generator();
25-
proof_builder.set_elements(&[(var_G, G)]);
25+
proof_builder.assign_elements(&[(var_G, G)]);
2626

2727
let witness = vec![Scalar::random(rng)];
2828

2929
let X = G * witness[0];
30-
proof_builder.set_elements(&[(var_X, X)]);
30+
proof_builder.assign_elements(&[(var_X, X)]);
3131

3232
// Prove and verify a proof
3333
let proof_bytes = proof_builder.prove(&witness, &mut rng).unwrap();

tests/proof_composition.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ fn DL_protocol<G: Group + GroupEncoding>(
2525
let points = preimage.allocate_elements(2);
2626

2727
preimage.append_equation(points[1], &[(scalars[0], points[0])]);
28-
preimage.set_elements(&[(points[0], G)]);
29-
preimage.set_elements(&[(points[1], G * x)]);
28+
preimage.assign_elements(&[(points[0], G)]);
29+
preimage.assign_elements(&[(points[1], G * x)]);
3030

3131
assert!(vec![G * x] == preimage.morphism.evaluate(&[x]));
3232
(SchnorrProtocol::from_preimage(preimage), vec![x])
@@ -49,7 +49,7 @@ fn pedersen_protocol<G: Group + GroupEncoding>(
4949
let scalars = preimage.allocate_scalars(2);
5050
let points = preimage.allocate_elements(3);
5151

52-
preimage.set_elements(&[(points[1], H), (points[0], G), (points[2], C)]);
52+
preimage.assign_elements(&[(points[1], H), (points[0], G), (points[2], C)]);
5353
preimage.append_equation(
5454
points[2],
5555
&[(scalars[0], points[0]), (scalars[1], points[1])],

tests/spec/sage_test_vectors.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ fn discrete_logarithm<G: SRandom + Group + GroupEncoding>(
8484
morphismp.append_equation(var_X, &[(var_x, var_G)]);
8585

8686
let G = G::generator();
87-
morphismp.set_elements(&[(var_G, G)]);
87+
morphismp.assign_elements(&[(var_G, G)]);
8888

8989
let x = G::srandom(rng);
9090
let X = G * x;
9191
assert!(vec![X] == morphismp.morphism.evaluate(&[x]));
92-
morphismp.set_elements(&[(var_X, X)]);
92+
morphismp.assign_elements(&[(var_X, X)]);
9393
(morphismp, vec![x])
9494
}
9595

@@ -111,7 +111,7 @@ fn dleq<G: Group + GroupEncoding + SRandom>(
111111
let points = morphismp.allocate_elements(4);
112112
let (var_G, var_H, var_X, var_Y) = (points[0], points[1], points[2], points[3]);
113113

114-
morphismp.set_elements(&[(var_G, G), (var_H, H), (var_X, X), (var_Y, Y)]);
114+
morphismp.assign_elements(&[(var_G, G), (var_H, H), (var_X, X), (var_Y, Y)]);
115115
morphismp.append_equation(var_X, &[(var_x, var_G)]);
116116
morphismp.append_equation(var_Y, &[(var_x, var_H)]);
117117

@@ -139,7 +139,7 @@ fn pedersen_commitment<G: Group + GroupEncoding + SRandom>(
139139
let points = morphismp.allocate_elements(3);
140140
let (var_G, var_H, var_C) = (points[0], points[1], points[2]);
141141

142-
morphismp.set_elements(&[(var_H, H), (var_G, G), (var_C, C)]);
142+
morphismp.assign_elements(&[(var_H, H), (var_G, G), (var_C, C)]);
143143
morphismp.append_equation(var_C, &[(var_x, var_G), (var_r, var_H)]);
144144

145145
assert!(vec![C] == morphismp.morphism.evaluate(&witness));
@@ -171,13 +171,13 @@ fn pedersen_commitment_dleq<G: Group + GroupEncoding + SRandom>(
171171
let var_Gs = (points[0], points[1], points[2], points[3]);
172172
let (var_X, var_Y) = (points[4], points[5]);
173173

174-
morphismp.set_elements(&[
174+
morphismp.assign_elements(&[
175175
(var_Gs.0, generators[0]),
176176
(var_Gs.1, generators[1]),
177177
(var_Gs.2, generators[2]),
178178
(var_Gs.3, generators[3]),
179179
]);
180-
morphismp.set_elements(&[(var_X, X), (var_Y, Y)]);
180+
morphismp.assign_elements(&[(var_X, X), (var_Y, Y)]);
181181

182182
morphismp.append_equation(var_X, &[(var_x, var_Gs.0), (var_r, var_Gs.1)]);
183183
morphismp.append_equation(var_Y, &[(var_x, var_Gs.2), (var_r, var_Gs.3)]);
@@ -217,7 +217,7 @@ fn bbs_blind_commitment_computation<G: Group + GroupEncoding + SRandom>(
217217
let (var_Q_2, var_J_1, var_J_2, var_J_3) = (points[0], points[1], points[2], points[3]);
218218
let var_C = points[M + 1];
219219

220-
morphismp.set_elements(&[
220+
morphismp.assign_elements(&[
221221
(var_Q_2, Q_2),
222222
(var_J_1, J_1),
223223
(var_J_2, J_2),

0 commit comments

Comments
 (0)