1-
21use std:: collections:: HashMap ;
32use std:: iter;
43use std:: marker:: PhantomData ;
54
65use ff:: Field ;
76use group:: prime:: PrimeGroup ;
87
8+ use super :: { GroupMap , GroupVar , LinearCombination , LinearRelation , ScalarTerm , ScalarVar } ;
99use crate :: errors:: { Error , InvalidInstance } ;
10- use super :: { ScalarVar , GroupVar , GroupMap , LinearRelation , LinearCombination , ScalarTerm } ;
11-
1210
1311/// A normalized form of the [`LinearRelation`], which is used for serialization into the transcript.
1412///
@@ -28,6 +26,10 @@ pub struct CanonicalLinearRelation<G: PrimeGroup> {
2826 pub num_scalars : usize ,
2927}
3028
29+
30+ type GroupExpr < G > = Vec < ( <G as group:: Group >:: Scalar , GroupVar < G > ) > ;
31+
32+
3133impl < G : PrimeGroup > CanonicalLinearRelation < G > {
3234 /// Create a new empty canonical linear relation
3335 pub fn new ( ) -> Self {
@@ -45,7 +47,7 @@ impl<G: PrimeGroup> CanonicalLinearRelation<G> {
4547 group_var : GroupVar < G > ,
4648 weight : & G :: Scalar ,
4749 original_group_elements : & GroupMap < G > ,
48- weighted_group_cache : & mut HashMap < GroupVar < G > , Vec < ( G :: Scalar , GroupVar < G > ) > > ,
50+ weighted_group_cache : & mut HashMap < GroupVar < G > , GroupExpr < G > > ,
4951 ) -> Result < GroupVar < G > , InvalidInstance > {
5052 // Check if we already have this (weight, group_var) combination
5153 let entry = weighted_group_cache. entry ( group_var) . or_default ( ) ;
@@ -74,7 +76,7 @@ impl<G: PrimeGroup> CanonicalLinearRelation<G> {
7476 & image_var: & GroupVar < G > ,
7577 equation : & LinearCombination < G > ,
7678 original_relation : & LinearRelation < G > ,
77- weighted_group_cache : & mut HashMap < GroupVar < G > , Vec < ( G :: Scalar , GroupVar < G > ) > > ,
79+ weighted_group_cache : & mut HashMap < GroupVar < G > , GroupExpr < G > > ,
7880 ) -> Result < ( ) , InvalidInstance > {
7981 let mut rhs_terms = Vec :: new ( ) ;
8082
@@ -302,10 +304,7 @@ impl<G: PrimeGroup> CanonicalLinearRelation<G> {
302304 repr. as_mut ( ) . copy_from_slice ( elem_bytes) ;
303305
304306 let elem = Option :: < G > :: from ( G :: from_bytes ( & repr) ) . ok_or_else ( || {
305- Error :: from ( InvalidInstance :: new ( format ! (
306- "Invalid group element at index {}" ,
307- i
308- ) ) )
307+ Error :: from ( InvalidInstance :: new ( format ! ( "Invalid group element at index {i}" ) ) )
309308 } ) ?;
310309
311310 group_elements_ordered. push ( elem) ;
@@ -367,11 +366,13 @@ impl<G: PrimeGroup> TryFrom<&LinearRelation<G>> for CanonicalLinearRelation<G> {
367366 let mut weighted_group_cache = HashMap :: new ( ) ;
368367
369368 // Process each constraint using the modular helper method
370- for ( lhs, rhs) in
371- iter:: zip ( & relation. image , & relation. linear_map . linear_combinations )
372- {
369+ for ( lhs, rhs) in iter:: zip ( & relation. image , & relation. linear_map . linear_combinations ) {
373370 // If the linear combination is trivial, check it directly and skip processing.
374- if rhs. 0 . iter ( ) . all ( |weighted| matches ! ( weighted. term. scalar, ScalarTerm :: Unit ) ) {
371+ if rhs
372+ . 0
373+ . iter ( )
374+ . all ( |weighted| matches ! ( weighted. term. scalar, ScalarTerm :: Unit ) )
375+ {
375376 let lhs_value = relation
376377 . linear_map
377378 . group_elements
@@ -383,22 +384,21 @@ impl<G: PrimeGroup> TryFrom<&LinearRelation<G>> for CanonicalLinearRelation<G> {
383384 . linear_map
384385 . group_elements
385386 . get ( weighted. term . elem )
386- . unwrap_or_else ( |_| panic ! ( "Unassigned group variable in linear combination" ) )
387+ . unwrap_or_else ( |_| {
388+ panic ! ( "Unassigned group variable in linear combination" )
389+ } )
387390 * weighted. weight
388391 } ) ;
389392 if lhs_value != rhs_value {
390- return Err ( InvalidInstance :: new ( "Trivial linear combination does not match image" ) ) ;
393+ return Err ( InvalidInstance :: new (
394+ "Trivial linear combination does not match image" ,
395+ ) ) ;
391396 } else {
392397 continue ; // Skip processing trivial constraints
393398 }
394399 }
395400
396- canonical. process_constraint (
397- lhs,
398- rhs,
399- relation,
400- & mut weighted_group_cache,
401- ) ?;
401+ canonical. process_constraint ( lhs, rhs, relation, & mut weighted_group_cache) ?;
402402 }
403403
404404 Ok ( canonical)
0 commit comments