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