arnaucube
arnaucube/gnark-plonky2-verifierPublic Self-hosted repository
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)
25
26// Circuit defines a simple circuit
27// x**3 + x + 5 == y
28type Circuit struct {
29    // struct tags on a variable is optional
30    // default uses variable name and secret visibility.
31    X frontend.Variable `gnark:"x"`
32    Y frontend.Variable `gnark:",public"`
33}
34
35// Define declares the circuit constraints
36// x**3 + x + 5 == y
37func (circuit *Circuit) Define(api frontend.API) error {
38    x3 := api.Mul(circuit.X, circuit.X, circuit.X)
39    api.AssertIsEqual(circuit.Y, api.Add(x3, circuit.X, 5))
40    return nil
41}
42
43func main() {
44    err := main_impl()
45    if err != nil {
46        fmt.Println(err)
47        os.Exit(1)
48    }
49}
50
51func main_impl() error {
52    var myCircuit Circuit
53    r1cs, err := frontend.Compile(ecc.BN254, r1cs.NewBuilder, &myCircuit)
54    if err != nil {
55        return err
56    }
57
58    assignment := &Circuit{
59        X: "2",
60        Y: "15",
61    }
62    witness, _ := frontend.NewWitness(assignment, ecc.BN254)
63    publicWitness, _ := witness.Public()
64    pk, vk, err := groth16.Setup(r1cs)
65    proof, err := groth16.Prove(r1cs, pk, witness)
66    err = groth16.Verify(proof, vk, publicWitness)
67    if err != nil {
68        return err
69    }
70    fmt.Println(proof)
71    return nil
72}