gnark-plonky2-verifier/plonky2_verifier/utils.go
plonky2_verifier/utils.goRaw
1package plonky2_verifier
2
3import (
4 "fmt"
5 . "gnark-ed25519/field"
6 "math/bits"
7
8 "github.com/consensys/gnark/frontend"
9)
10
11func reduceWithPowers(qe *QuadraticExtensionAPI, terms []QuadraticExtension, scalar QuadraticExtension) QuadraticExtension {
12 sum := qe.ZERO_QE
13
14 for i := len(terms) - 1; i >= 0; i-- {
15 sum = qe.AddExtension(
16 qe.MulExtension(
17 sum,
18 scalar,
19 ),
20 terms[i],
21 )
22 }
23
24 return sum
25}
26
27// Computes `log_2(n)`, panicking if `n` is not a power of two.
28func log2Strict(n uint) int {
29 res := bits.TrailingZeros(n)
30 if n>>res != 1 {
31 panic(fmt.Sprintf("Not a power of two: %d", n))
32 }
33 return res
34}
35
36func SelectHash(fieldAPI frontend.API, bit frontend.Variable, leftHash, rightHash Hash) Hash {
37 var returnHash Hash
38 for i := 0; i < 4; i++ {
39 returnHash[i] = fieldAPI.Select(bit, leftHash[i], rightHash[i]).(F)
40 }
41
42 return returnHash
43}