gnark-plonky2-verifier/cubic.go
cubic.goRaw
1// Copyright 2020 ConsenSys AG
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18 "fmt"
19 "os"
20 "github.com/consensys/gnark/frontend"
21 "github.com/consensys/gnark-crypto/ecc"
22 "github.com/consensys/gnark/frontend/cs/r1cs"
23 "github.com/consensys/gnark/backend/groth16"
24 _ "gnark-ed25519/edwards_curve"
25 _ "gnark-ed25519/sha512"
26)
27
28// Circuit defines a simple circuit
29// x**3 + x + 5 == y
30type Circuit struct {
31 // struct tags on a variable is optional
32 // default uses variable name and secret visibility.
33 X frontend.Variable `gnark:"x"`
34 Y frontend.Variable `gnark:",public"`
35}
36
37// Define declares the circuit constraints
38// x**3 + x + 5 == y
39func (circuit *Circuit) Define(api frontend.API) error {
40 x3 := api.Mul(circuit.X, circuit.X, circuit.X)
41 api.AssertIsEqual(circuit.Y, api.Add(x3, circuit.X, 5))
42 return nil
43}
44
45func main() {
46 err := mainImpl()
47 if err != nil {
48 fmt.Println(err)
49 os.Exit(1)
50 }
51}
52
53func mainImpl() error {
54 var myCircuit Circuit
55 r1cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &myCircuit)
56 if err != nil {
57 return err
58 }
59
60 assignment := &Circuit{
61 X: "2",
62 Y: "15",
63 }
64 witness, _ := frontend.NewWitness(assignment, ecc.BN254.ScalarField())
65 publicWitness, _ := witness.Public()
66 pk, vk, err := groth16.Setup(r1cs)
67 proof, err := groth16.Prove(r1cs, pk, witness)
68 err = groth16.Verify(proof, vk, publicWitness)
69 if err != nil {
70 return err
71 }
72 fmt.Println(proof)
73 return nil
74}