Skip to content

Commit 858ba91

Browse files
committed
Use of PointVar and ScalarVar structures in the GroupMorphismPreimage API instead of indices for clarity and readability
1 parent dfb2927 commit 858ba91

File tree

6 files changed

+66
-46
lines changed

6 files changed

+66
-46
lines changed

src/group_morphism.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010
1111
use group::{Group, GroupEncoding};
1212

13+
#[derive(Copy, Clone)]
14+
pub struct ScalarVar(pub usize);
15+
16+
#[derive(Copy, Clone)]
17+
pub struct PointVar(pub usize);
18+
1319
/// A sparse linear combination of scalars and group elements.
1420
///
1521
/// Stores indices into external lists of scalars and group elements.
1622
/// Used to define individual constraints inside a Morphism.
1723
pub struct LinearCombination {
18-
pub scalar_indices: Vec<usize>,
19-
pub element_indices: Vec<usize>,
24+
pub scalar_indices: Vec<ScalarVar>,
25+
pub element_indices: Vec<PointVar>,
2026
}
2127

2228
/// A Morphism represents a list of linear combinations over group elements.
@@ -73,11 +79,11 @@ impl<G: Group> Morphism<G> {
7379
self.linear_combination
7480
.iter()
7581
.map(|lc| {
76-
let coefficients: Vec<_> = lc.scalar_indices.iter().map(|&i| scalars[i]).collect();
82+
let coefficients: Vec<_> = lc.scalar_indices.iter().map(|&i| scalars[i.0]).collect();
7783
let elements: Vec<_> = lc
7884
.element_indices
7985
.iter()
80-
.map(|&i| self.group_elements[i])
86+
.map(|&i| self.group_elements[i.0])
8187
.collect();
8288
msm_pr(&coefficients, &elements)
8389
})
@@ -97,7 +103,7 @@ where
97103
/// The underlying morphism describing the structure of the statement.
98104
pub morphism: Morphism<G>,
99105
/// Indices pointing to elements representing the "target" images for each constraint.
100-
pub image: Vec<usize>,
106+
pub image: Vec<PointVar>,
101107
}
102108

103109
impl<G> Default for GroupMorphismPreimage<G>
@@ -129,8 +135,8 @@ where
129135

130136
/// Append a new equation relating scalars to group elements.
131137
///
132-
/// `lhs` is the index of the image, and `rhs` is a list of (scalar_idx, element_idx) pairs.
133-
pub fn append_equation(&mut self, lhs: usize, rhs: &[(usize, usize)]) {
138+
/// `lhs` is the image, and `rhs` is a list of (scalar, element) pairs.
139+
pub fn append_equation(&mut self, lhs: PointVar, rhs: &[(ScalarVar, PointVar)]) {
134140
let lc = LinearCombination {
135141
scalar_indices: rhs.iter().map(|&(s, _)| s).collect(),
136142
element_indices: rhs.iter().map(|&(_, e)| e).collect(),
@@ -139,39 +145,47 @@ where
139145
self.image.push(lhs);
140146
}
141147

142-
/// Allocate space for `n` new scalars and return their indices.
143-
pub fn allocate_scalars(&mut self, n: usize) -> Vec<usize> {
148+
/// Allocate space for `n` new scalars and return their ScalarVar.
149+
pub fn allocate_scalars(&mut self, n: usize) -> Vec<ScalarVar> {
144150
let start = self.morphism.num_scalars;
145151
let indices: Vec<usize> = (start..start + n).collect();
152+
let mut scalars = Vec::new();
153+
for i in indices.iter() {
154+
scalars.push(ScalarVar(*i));
155+
}
146156
self.morphism.num_scalars += n;
147-
indices
157+
scalars
148158
}
149159

150-
/// Allocate space for `n` new group elements and return their indices.
160+
/// Allocate space for `n` new group elements and return their PointVar.
151161
///
152162
/// The allocated elements are initially set to the identity.
153-
pub fn allocate_elements(&mut self, n: usize) -> Vec<usize> {
163+
pub fn allocate_elements(&mut self, n: usize) -> Vec<PointVar> {
154164
let start = self.morphism.num_elements;
155165
let indices: Vec<usize> = (start..start + n).collect();
156166
for _ in 0..n {
157167
self.morphism.group_elements.push(G::identity());
158168
}
169+
let mut points = Vec::new();
170+
for i in indices.iter() {
171+
points.push(PointVar(*i));
172+
}
159173
self.morphism.num_elements += n;
160-
indices
174+
points
161175
}
162176

163177
/// Set the value of group elements at a given index, inside the list of allocated group elements.
164-
pub fn set_elements(&mut self, elements: &[(usize, G)]) {
178+
pub fn set_elements(&mut self, elements: &[(PointVar, G)]) {
165179
for &(i, ref elt) in elements {
166-
self.morphism.group_elements[i] = *elt;
180+
self.morphism.group_elements[i.0] = *elt;
167181
}
168182
}
169183

170184
/// Return the group elements corresponding to the image indices.
171185
pub fn image(&self) -> Vec<G> {
172186
let mut result = Vec::new();
173187
for i in &self.image {
174-
result.push(self.morphism.group_elements[*i]);
188+
result.push(self.morphism.group_elements[i.0]);
175189
}
176190
result
177191
}

src/schnorr_proof.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! through a group morphism abstraction (see Maurer09).
66
77
use crate::{
8-
GroupMorphismPreimage,
8+
GroupMorphismPreimage,
99
GroupSerialisation,
1010
SigmaProtocol,
1111
ProofError,
@@ -79,7 +79,7 @@ where
7979
.take(self.0.morphism.num_statements())
8080
{
8181
rhs.push(
82-
self.0.morphism.group_elements[self.0.image[i]] * challenge + g,
82+
self.0.morphism.group_elements[self.0.image[i].0] * challenge + g,
8383
);
8484
}
8585

tests/non_interactive_protocol.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use rand::rngs::OsRng;
55
use sigma_rs::{
66
NISigmaProtocol,
77
GroupMorphismPreimage,
8+
PointVar,
9+
ScalarVar,
810
SchnorrProof,
911
codec::ShakeCodec
1012
};
@@ -28,14 +30,14 @@ fn fiat_shamir_schnorr_proof_ristretto() {
2830
// Scalars and Points bases settings
2931
morphismp.allocate_scalars(1);
3032
morphismp.allocate_elements(2);
31-
morphismp.set_elements(&[(0, G), (1, H)]);
33+
morphismp.set_elements(&[(PointVar(0), G), (PointVar(1), H)]);
3234

3335
// Set the witness Vec
3436
let mut witness = Vec::new();
3537
witness.push(w);
3638

3739
// The H = z * G equation where z is the unique scalar variable
38-
morphismp.append_equation(1, &[(0, 0)]);
40+
morphismp.append_equation(PointVar(1), &[(ScalarVar(0), PointVar(0))]);
3941

4042
// The SigmaProtocol induced by morphismp
4143
let protocol = SchnorrProof(morphismp);

tests/spec/custom_schnorr_proof.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ where
6464

6565
let mut rhs = Vec::new();
6666
for (i, g) in commitment.iter().enumerate().take(self.morphismp.morphism.num_statements()) {
67-
rhs.push(*g + self.morphismp.morphism.group_elements[self.morphismp.image[i]] * *challenge);
67+
rhs.push(*g + self.morphismp.morphism.group_elements[self.morphismp.image[i].0] * *challenge);
6868
}
6969

7070
match lhs == rhs {

tests/spec/sage_test_vectors.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use group::{Group, GroupEncoding};
66
use sigma_rs::{
77
codec::{ByteSchnorrCodec, KeccakDuplexSponge},
88
GroupMorphismPreimage,
9+
ScalarVar,
10+
PointVar,
911
NISigmaProtocol
1012
};
1113

@@ -38,8 +40,8 @@ fn discrete_logarithm<G: SRandom + Group + GroupEncoding>(
3840
) -> (Preimage<G>, Vec<G::Scalar>) {
3941
let mut morphismp: Preimage<G> = Preimage::new();
4042

41-
let var_x: usize = 0;
42-
let (var_G, var_X): (usize, usize) = (0, 1);
43+
let var_x= ScalarVar(0);
44+
let (var_G, var_X) = (PointVar(0), PointVar(1));
4345
morphismp.allocate_scalars(1);
4446
morphismp.allocate_elements(2);
4547
morphismp.append_equation(var_X, &[(var_x, var_G)]);
@@ -67,8 +69,8 @@ fn dleq<G: Group + GroupEncoding + SRandom>(
6769
let X = G * x;
6870
let Y = H * x;
6971

70-
let var_x: usize = 0;
71-
let (var_G, var_H, var_X, var_Y) = (0, 1, 2, 3);
72+
let var_x = ScalarVar(0);
73+
let (var_G, var_H, var_X, var_Y) = (PointVar(0), PointVar(1), PointVar(2), PointVar(3));
7274
morphismp.allocate_scalars(1);
7375
morphismp.allocate_elements(4);
7476
morphismp.set_elements(&[(var_G, G), (var_H, H), (var_X, X), (var_Y, Y)]);
@@ -94,8 +96,8 @@ fn pedersen_commitment<G: Group + GroupEncoding + SRandom>(
9496

9597
let C = G*x + H*r;
9698

97-
let (var_x, var_r) = (0, 1);
98-
let (var_G, var_H, var_C) = (0, 1, 2);
99+
let (var_x, var_r) = (ScalarVar(0), ScalarVar(1));
100+
let (var_G, var_H, var_C) = (PointVar(0), PointVar(1), PointVar(2));
99101
morphismp.allocate_scalars(2);
100102
morphismp.allocate_elements(3);
101103
morphismp.set_elements(&[(var_H, H), (var_G, G), (var_C, C)]);
@@ -125,9 +127,9 @@ fn pedersen_commitment_dleq<G: Group + GroupEncoding + SRandom>(
125127
let X = msm_pr::<G>(&witness, &[generators[0], generators[1]]);
126128
let Y = msm_pr::<G>(&witness, &[generators[2], generators[3]]);
127129

128-
let (var_x, var_r) = (0, 1);
129-
let var_Gs = (0, 1, 2, 3);
130-
let (var_X, var_Y) = (4, 5);
130+
let (var_x, var_r) = (ScalarVar(0), ScalarVar(1));
131+
let var_Gs = (PointVar(0), PointVar(1), PointVar(2), PointVar(3));
132+
let (var_X, var_Y) = (PointVar(4), PointVar(5));
131133
morphismp.allocate_scalars(2);
132134
morphismp.allocate_elements(4);
133135
morphismp.allocate_elements(2);
@@ -161,9 +163,9 @@ fn bbs_blind_commitment_computation<G: Group + GroupEncoding + SRandom>(
161163
let C = Q_2*secret_prover_blind + J_1*msg_1 + J_2*msg_2 + J_3*msg_3;
162164

163165
// This is the part that needs to be changed in the specification of blind bbs.
164-
let (var_secret_prover_blind, var_msg_1, var_msg_2, var_msg_3) = (0, 1, 2, 3);
165-
let (var_Q_2, var_J_1, var_J_2, var_J_3) = (0, 1, 2, 3);
166-
let var_C = M+1;
166+
let (var_secret_prover_blind, var_msg_1, var_msg_2, var_msg_3) = (ScalarVar(0), ScalarVar(1), ScalarVar(2), ScalarVar(3));
167+
let (var_Q_2, var_J_1, var_J_2, var_J_3) = (PointVar(0), PointVar(1), PointVar(2), PointVar(3));
168+
let var_C = PointVar(M + 1);
167169

168170
morphismp.allocate_scalars(M+1);
169171
morphismp.allocate_elements(M+1);

tests/various_tests.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use rand::{
77

88
use sigma_rs::{
99
codec::ShakeCodec,
10-
GroupMorphismPreimage,
10+
GroupMorphismPreimage,
11+
PointVar,
12+
ScalarVar,
1113
NISigmaProtocol,
1214
SchnorrProof,
1315
};
@@ -28,8 +30,8 @@ fn discrete_logarithm<G: Group + GroupEncoding>(
2830
) -> (GroupMorphismPreimage<G>, Vec<G::Scalar>) {
2931
let mut morphismp: GroupMorphismPreimage<G> = GroupMorphismPreimage::new();
3032

31-
let var_x: usize = 0;
32-
let (var_G, var_X): (usize, usize) = (0, 1);
33+
let var_x= ScalarVar(0);
34+
let (var_G, var_X) = (PointVar(0), PointVar(1));
3335
morphismp.allocate_scalars(1);
3436
morphismp.allocate_elements(2);
3537
morphismp.append_equation(var_X, &[(var_x, var_G)]);
@@ -57,8 +59,8 @@ fn dleq<G: Group + GroupEncoding>(
5759
let X = G * x;
5860
let Y = H * x;
5961

60-
let var_x: usize = 0;
61-
let (var_G, var_H, var_X, var_Y) = (0, 1, 2, 3);
62+
let var_x = ScalarVar(0);
63+
let (var_G, var_H, var_X, var_Y) = (PointVar(0), PointVar(1), PointVar(2), PointVar(3));
6264
morphismp.allocate_scalars(1);
6365
morphismp.allocate_elements(4);
6466
morphismp.set_elements(&[(var_G, G), (var_H, H), (var_X, X), (var_Y, Y)]);
@@ -83,8 +85,8 @@ fn pedersen_commitment<G: Group + GroupEncoding>(
8385

8486
let C = G * x + H * r;
8587

86-
let (var_x, var_r) = (0, 1);
87-
let (var_G, var_H, var_C) = (0, 1, 2);
88+
let (var_x, var_r) = (ScalarVar(0), ScalarVar(1));
89+
let (var_G, var_H, var_C) = (PointVar(0), PointVar(1), PointVar(2));
8890
morphismp.allocate_scalars(2);
8991
morphismp.allocate_elements(3);
9092
morphismp.set_elements(&[(var_H, H), (var_G, G), (var_C, C)]);
@@ -113,9 +115,9 @@ fn pedersen_commitment_dleq<G: Group + GroupEncoding>(
113115
let X = msm_pr::<G>(&witness, &[generators[0], generators[1]]);
114116
let Y = msm_pr::<G>(&witness, &[generators[2], generators[3]]);
115117

116-
let (var_x, var_r) = (0, 1);
117-
let var_Gs = (0, 1, 2, 3);
118-
let (var_X, var_Y) = (4, 5);
118+
let (var_x, var_r) = (ScalarVar(0), ScalarVar(1));
119+
let var_Gs = (PointVar(0), PointVar(1), PointVar(2), PointVar(3));
120+
let (var_X, var_Y) = (PointVar(4), PointVar(5));
119121
morphismp.allocate_scalars(2);
120122
morphismp.allocate_elements(4);
121123
morphismp.allocate_elements(2);
@@ -162,9 +164,9 @@ fn bbs_blind_commitment_computation<G: Group + GroupEncoding>(
162164
let C = Q_2 * secret_prover_blind + J_1 * msg_1 + J_2 * msg_2 + J_3 * msg_3;
163165

164166
// This is the part that needs to be changed in the specification of blind bbs.
165-
let (var_secret_prover_blind, var_msg_1, var_msg_2, var_msg_3) = (0, 1, 2, 3);
166-
let (var_Q_2, var_J_1, var_J_2, var_J_3) = (0, 1, 2, 3);
167-
let var_C = M + 1;
167+
let (var_secret_prover_blind, var_msg_1, var_msg_2, var_msg_3) = (ScalarVar(0), ScalarVar(1), ScalarVar(2), ScalarVar(3));
168+
let (var_Q_2, var_J_1, var_J_2, var_J_3) = (PointVar(0), PointVar(1), PointVar(2), PointVar(3));
169+
let var_C = PointVar(M + 1);
168170

169171
morphismp.allocate_scalars(M + 1);
170172
morphismp.allocate_elements(M + 1);

0 commit comments

Comments
 (0)