-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (60 loc) · 1.49 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
"fmt"
)
type rangeCheckCircuit struct {
X frontend.Variable
Y, Bound frontend.Variable `gnark:",public"`
}
func (circuit *rangeCheckCircuit) Define(api frontend.API) error {
c1 := api.Mul(circuit.X, circuit.Y)
c2 := api.Mul(c1, circuit.Y)
c3 := api.Add(circuit.X, circuit.Y)
api.AssertIsLessOrEqual(c2, circuit.Bound)
api.AssertIsLessOrEqual(c3, circuit.Bound) // c3 is from a linear expression only
return nil
}
func main() {
ccs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &rangeCheckCircuit{})
if err != nil {
fmt.Println(err)
}
assignment := rangeCheckCircuit{
X: 4,
Y: 2,
Bound: 44,
}
witness, err := frontend.NewWitness(&assignment, ecc.BN254.ScalarField())
if err != nil {
fmt.Println(err)
}
pubW, err := witness.Public()
if err != nil {
fmt.Println(err)
}
pk, vk, err := groth16.Setup(ccs)
if err != nil {
fmt.Println(err)
}
proofIcicle, err := groth16.Prove(ccs, pk, witness, backend.WithIcicleAcceleration())
if err != nil {
fmt.Println(err)
}
err = groth16.Verify(proofIcicle, vk, pubW)
if err != nil {
fmt.Println(err)
}
proof, err := groth16.Prove(ccs, pk, witness)
if err != nil {
fmt.Println(err)
}
err = groth16.Verify(proof, vk, pubW)
if err != nil {
fmt.Println(err)
}
}