@@ -173,10 +173,7 @@ func (p *ipaProver) Prove() (*IPA, error) {
173173// of the left vector and right is a function of right vector.
174174// Both vectors are committed in com which is passed as a parameter to reduce
175175func (p * ipaProver ) reduce (X , com * mathlib.G1 ) (* mathlib.Zr , * mathlib.Zr , []* mathlib.G1 , []* mathlib.G1 , error ) {
176- leftGen := make ([]* mathlib.G1 , len (p .LeftGenerators ))
177- copy (leftGen , p .LeftGenerators )
178- rightGen := make ([]* mathlib.G1 , len (p .RightGenerators ))
179- copy (rightGen , p .RightGenerators )
176+ leftGen , rightGen := cloneGenerators (p .LeftGenerators , p .RightGenerators )
180177
181178 left := p .leftVector
182179 right := p .rightVector
@@ -186,8 +183,8 @@ func (p *ipaProver) reduce(X, com *mathlib.G1) (*mathlib.Zr, *mathlib.Zr, []*mat
186183 for i := range p .NumberOfRounds {
187184 // in each round the size of the vector is reduced by 2
188185 n := len (leftGen ) / 2
189- leftIP := innerProduct (left [:n ], right [n :], p .Curve )
190- rightIP := innerProduct (left [n :], right [:n ], p .Curve )
186+ leftIP := InnerProduct (left [:n ], right [n :], p .Curve )
187+ rightIP := InnerProduct (left [n :], right [:n ], p .Curve )
191188 // LArray[i] is a commitment to left[:n], right[n:] and their inner product
192189 LArray [i ] = commitVector (left [:n ], right [n :], leftGen [n :], rightGen [:n ], p .Curve )
193190 LArray [i ].Add (X .Mul (leftIP ))
@@ -298,10 +295,7 @@ func (v *ipaVerifier) Verify(proof *IPA) error {
298295
299296 X := v .Q .Mul (x )
300297
301- leftGen := make ([]* mathlib.G1 , len (v .LeftGenerators ))
302- copy (leftGen , v .LeftGenerators )
303- rightGen := make ([]* mathlib.G1 , len (v .RightGenerators ))
304- copy (rightGen , v .RightGenerators )
298+ leftGen , rightGen := cloneGenerators (v .LeftGenerators , v .RightGenerators )
305299
306300 for i := range v .NumberOfRounds {
307301 // check well-formedness
@@ -343,17 +337,17 @@ func (v *ipaVerifier) Verify(proof *IPA) error {
343337// reduceVectors reduces the size of the vectors passed in the parameters by 1/2,
344338// as a function of the old vectors, x and 1/x
345339func reduceVectors (left , right []* mathlib.Zr , x , xInv * mathlib.Zr , c * mathlib.Curve ) ([]* mathlib.Zr , []* mathlib.Zr ) {
346- leftPrime := make ([]* mathlib.Zr , len (left )/ 2 )
347- rightPrime := make ([]* mathlib.Zr , len (right )/ 2 )
348- for i := 0 ; i < len (leftPrime ); i ++ {
340+ l := len (left ) / 2
341+ leftPrime := make ([]* mathlib.Zr , l )
342+ rightPrime := make ([]* mathlib.Zr , l )
343+ for i := 0 ; i < l ; i ++ {
349344 // a_i = a_ix + a_{i+len(left)/2}x^{-1}
350- leftPrime [i ] = c .ModMul (left [i ], x , c .GroupOrder )
351- leftPrime [i ] = c .ModAdd (leftPrime [i ], c .ModMul (left [i + len (leftPrime )], xInv , c .GroupOrder ), c .GroupOrder )
345+ leftPrime [i ] = c .ModAddMul2 (left [i ], x , left [i + l ], xInv , c .GroupOrder )
352346
353347 // b_i = b_ix^{-1} + b_{i+len(right)/2}x
354- rightPrime [i ] = c .ModMul (right [i ], xInv , c .GroupOrder )
355- rightPrime [i ] = c .ModAdd (rightPrime [i ], c .ModMul (right [i + len (rightPrime )], x , c .GroupOrder ), c .GroupOrder )
348+ rightPrime [i ] = c .ModAddMul2 (right [i ], xInv , right [i + l ], x , c .GroupOrder )
356349 }
350+
357351 return leftPrime , rightPrime
358352}
359353
@@ -363,19 +357,20 @@ func reduceGenerators(leftGen, rightGen []*mathlib.G1, x, xInv *mathlib.Zr) ([]*
363357 l := len (leftGen ) / 2
364358 for i := 0 ; i < l ; i ++ {
365359 // G_i = G_i^x*G_{i+len(left)/2}^{1/x}
366- leftGen [i ] = leftGen [ i ]. Mul2 (xInv , leftGen [i + l ], x )
360+ leftGen [i ]. Mul2InPlace (xInv , leftGen [i + l ], x )
367361 // H_i = H_i^{1/x}*H_{i+len(right)/2}^{x}
368- rightGen [i ] = rightGen [ i ]. Mul2 (x , rightGen [i + l ], xInv )
362+ rightGen [i ]. Mul2InPlace (x , rightGen [i + l ], xInv )
369363 }
370364 return leftGen [:l ], rightGen [:l ]
371365}
372366
373- func innerProduct (left []* mathlib.Zr , right []* mathlib.Zr , c * mathlib.Curve ) * mathlib.Zr {
374- ip := c .NewZrFromInt (0 )
375- for i , l := range left {
376- ip = c .ModAdd (ip , c .ModMul (l , right [i ], c .GroupOrder ), c .GroupOrder )
377- }
378- return ip
367+ func InnerProduct (left []* mathlib.Zr , right []* mathlib.Zr , c * mathlib.Curve ) * mathlib.Zr {
368+ return c .ModAddMul (left , right , c .GroupOrder )
369+ // ip := c.NewZrFromInt(0)
370+ // for i, l := range left {
371+ // ip = c.ModAdd(ip, c.ModMul(l, right[i], c.GroupOrder), c.GroupOrder)
372+ // }
373+ // return ip
379374}
380375
381376func commitVector (left []* mathlib.Zr , right []* mathlib.Zr , leftgen []* mathlib.G1 , rightgen []* mathlib.G1 , c * mathlib.Curve ) * mathlib.G1 {
@@ -385,3 +380,15 @@ func commitVector(left []*mathlib.Zr, right []*mathlib.Zr, leftgen []*mathlib.G1
385380 }
386381 return com
387382}
383+
384+ func cloneGenerators (LeftGenerators , RightGenerators []* mathlib.G1 ) ([]* mathlib.G1 , []* mathlib.G1 ) {
385+ leftGen := make ([]* mathlib.G1 , len (LeftGenerators ))
386+ for i := 0 ; i < len (LeftGenerators ); i ++ {
387+ leftGen [i ] = LeftGenerators [i ].Copy ()
388+ }
389+ rightGen := make ([]* mathlib.G1 , len (RightGenerators ))
390+ for i := 0 ; i < len (RightGenerators ); i ++ {
391+ rightGen [i ] = RightGenerators [i ].Copy ()
392+ }
393+ return leftGen , rightGen
394+ }
0 commit comments