Skip to content

Commit f8339a8

Browse files
nategrafmmaker
andauthored
Comments, lints, and formatting from on July 29 (#69)
Signed-off-by: Michele Orrù <[email protected]> Co-authored-by: Michele Orrù <[email protected]>
1 parent d49612a commit f8339a8

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

src/composition.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,13 @@ impl<G: PrimeGroup> ComposedRelation<G> {
225225
// Calculate the real challenge by subtracting all simulated challenges
226226
let (child_states, simulated_challenges, simulated_responses) = prover_state;
227227

228-
let mut real_challenge = challenge;
229-
for some_challenge in simulated_challenges.iter() {
230-
if let Some(challenge) = some_challenge {
231-
real_challenge -= challenge;
232-
}
233-
}
228+
let real_challenge = challenge - simulated_challenges.iter().flatten().sum::<G::Scalar>();
234229

235230
let it = instances
236-
.into_iter()
237-
.zip(child_states.into_iter())
238-
.zip(simulated_challenges.into_iter())
239-
.zip(simulated_responses.into_iter());
231+
.iter()
232+
.zip(child_states)
233+
.zip(simulated_challenges)
234+
.zip(simulated_responses);
240235
for (((i, prover_state), simulated_challenge), simulated_response) in it {
241236
if let Some(state) = prover_state {
242237
// Real case: compute response with real challenge
@@ -310,7 +305,7 @@ impl<G: PrimeGroup> SigmaProtocol for ComposedRelation<G> {
310305
ComposedRelation::Simple(p),
311306
ComposedCommitment::Simple(c),
312307
ComposedResponse::Simple(r),
313-
) => p.verifier(c, &challenge, r),
308+
) => p.verifier(c, challenge, r),
314309
(
315310
ComposedRelation::And(ps),
316311
ComposedCommitment::And(commitments),
@@ -328,10 +323,10 @@ impl<G: PrimeGroup> SigmaProtocol for ComposedRelation<G> {
328323
let last_challenge = *challenge - challenges.iter().sum::<G::Scalar>();
329324
ps.iter()
330325
.zip(commitments)
331-
.zip(challenges.into_iter().chain(&Some(last_challenge)))
326+
.zip(challenges.iter().chain(&Some(last_challenge)))
332327
.zip(responses)
333328
.try_for_each(|(((p, commitment), challenge), response)| {
334-
p.verifier(commitment, &challenge, response)
329+
p.verifier(commitment, challenge, response)
335330
})
336331
}
337332
_ => Err(Error::InvalidInstanceWitnessPair),
@@ -548,7 +543,7 @@ impl<G: PrimeGroup> SigmaProtocolSimulator for ComposedRelation<G> {
548543
let mut challenges = Vec::with_capacity(ps.len());
549544
let mut responses = Vec::with_capacity(ps.len());
550545
for _ in 0..ps.len() {
551-
challenges.push(G::Scalar::random(&mut *rng).into());
546+
challenges.push(G::Scalar::random(&mut *rng));
552547
}
553548
for p in ps.iter() {
554549
responses.push(p.simulate_response(&mut *rng));

src/linear_relation/canonical.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ pub struct CanonicalLinearRelation<G: PrimeGroup> {
2626
pub num_scalars: usize,
2727
}
2828

29-
type GroupExpr<G> = Vec<(<G as group::Group>::Scalar, GroupVar<G>)>;
29+
/// Private type alias used to simplify function signatures below.
30+
///
31+
/// The cache is essentially a mapping (GroupVar, Scalar) => GroupVar, which maps the original
32+
/// weighted group vars to a new assignment, such that if a pair appears more than once, it will
33+
/// map to the same group variable in the canonical linear relation.
34+
type WeightedGroupCache<G> = HashMap<GroupVar<G>, Vec<(<G as group::Group>::Scalar, GroupVar<G>)>>;
3035

3136
impl<G: PrimeGroup> CanonicalLinearRelation<G> {
3237
/// Create a new empty canonical linear relation
@@ -45,7 +50,7 @@ impl<G: PrimeGroup> CanonicalLinearRelation<G> {
4550
group_var: GroupVar<G>,
4651
weight: &G::Scalar,
4752
original_group_elements: &GroupMap<G>,
48-
weighted_group_cache: &mut HashMap<GroupVar<G>, GroupExpr<G>>,
53+
weighted_group_cache: &mut WeightedGroupCache<G>,
4954
) -> Result<GroupVar<G>, InvalidInstance> {
5055
// Check if we already have this (weight, group_var) combination
5156
let entry = weighted_group_cache.entry(group_var).or_default();
@@ -74,7 +79,7 @@ impl<G: PrimeGroup> CanonicalLinearRelation<G> {
7479
&image_var: &GroupVar<G>,
7580
equation: &LinearCombination<G>,
7681
original_relation: &LinearRelation<G>,
77-
weighted_group_cache: &mut HashMap<GroupVar<G>, GroupExpr<G>>,
82+
weighted_group_cache: &mut WeightedGroupCache<G>,
7883
) -> Result<(), InvalidInstance> {
7984
let mut rhs_terms = Vec::new();
8085

@@ -130,7 +135,12 @@ impl<G: PrimeGroup> CanonicalLinearRelation<G> {
130135
pub fn label(&self) -> Vec<u8> {
131136
let mut out = Vec::new();
132137

133-
// Replicate the original LinearRelationReprBuilder ordering behavior
138+
// Create an ordered list of unique group element representations. Elements are ordered
139+
// based on the order they appear in the canonical linear relation, as seen by the loop
140+
// below. Note that this is dependent on the building order in TryFrom<LinearRelation>.
141+
// QUESTION: Does anything depend on this order being stable? This seems difficult to
142+
// maintain across versions of this library, and changes to the relation definition may
143+
// have difficult to predict effects on the order.
134144
let mut group_repr_mapping: HashMap<Box<[u8]>, u32> = HashMap::new();
135145
let mut group_elements_ordered = Vec::new();
136146

@@ -146,8 +156,9 @@ impl<G: PrimeGroup> CanonicalLinearRelation<G> {
146156
new_index
147157
};
148158

149-
// Build constraint data in the same order as original
150-
let mut constraint_data = Vec::new();
159+
// Build constraint data in the same order as original, as a nested list of group and
160+
// scalar indices. Note that the group indices are into group_elements_ordered.
161+
let mut constraint_data = Vec::<(u32, Vec<(u32, u32)>)>::new();
151162

152163
for (image_elem, constraint_terms) in iter::zip(&self.image, &self.linear_combinations) {
153164
// First, add the left-hand side (image) element

src/linear_relation/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<G: PrimeGroup> GroupMap<G> {
160160

161161
/// Get the element value assigned to the given point var.
162162
///
163-
/// Returns [`Error::UnassignedGroupVar`] if a value is not assigned.
163+
/// Returns [`InvalidInstance`] if a value is not assigned.
164164
pub fn get(&self, var: GroupVar<G>) -> Result<G, InvalidInstance> {
165165
match self.0.get(var.0) {
166166
Some(Some(elem)) => Ok(*elem),

src/linear_relation/ops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,9 +784,9 @@ mod tests {
784784

785785
let diff = x - y;
786786
assert_eq!(diff.terms().len(), 2);
787-
assert_eq!(diff.terms()[0].term, y.into());
787+
assert_eq!(diff.terms()[0].term, y);
788788
assert_eq!(diff.terms()[0].weight, -Scalar::ONE);
789-
assert_eq!(diff.terms()[1].term, x.into());
789+
assert_eq!(diff.terms()[1].term, x);
790790
assert_eq!(diff.terms()[1].weight, Scalar::ONE);
791791
}
792792

0 commit comments

Comments
 (0)