@ -0,0 +1,72 @@ |
|||||
|
// Copyright 2020 ConsenSys AG
|
||||
|
//
|
||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
// you may not use this file except in compliance with the License.
|
||||
|
// You may obtain a copy of the License at
|
||||
|
//
|
||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
//
|
||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
// See the License for the specific language governing permissions and
|
||||
|
// limitations under the License.
|
||||
|
|
||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"os" |
||||
|
"github.com/consensys/gnark/frontend" |
||||
|
"github.com/consensys/gnark-crypto/ecc" |
||||
|
"github.com/consensys/gnark/frontend/cs/r1cs" |
||||
|
"github.com/consensys/gnark/backend/groth16" |
||||
|
) |
||||
|
|
||||
|
// Circuit defines a simple circuit
|
||||
|
// x**3 + x + 5 == y
|
||||
|
type Circuit struct { |
||||
|
// struct tags on a variable is optional
|
||||
|
// default uses variable name and secret visibility.
|
||||
|
X frontend.Variable `gnark:"x"` |
||||
|
Y frontend.Variable `gnark:",public"` |
||||
|
} |
||||
|
|
||||
|
// Define declares the circuit constraints
|
||||
|
// x**3 + x + 5 == y
|
||||
|
func (circuit *Circuit) Define(api frontend.API) error { |
||||
|
x3 := api.Mul(circuit.X, circuit.X, circuit.X) |
||||
|
api.AssertIsEqual(circuit.Y, api.Add(x3, circuit.X, 5)) |
||||
|
return nil |
||||
|
} |
||||
|
|
||||
|
func main() { |
||||
|
err := mainImpl() |
||||
|
if err != nil { |
||||
|
fmt.Println(err) |
||||
|
os.Exit(1) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func mainImpl() error { |
||||
|
var myCircuit Circuit |
||||
|
r1cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &myCircuit) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
|
||||
|
assignment := &Circuit{ |
||||
|
X: "2", |
||||
|
Y: "15", |
||||
|
} |
||||
|
witness, _ := frontend.NewWitness(assignment, ecc.BN254.ScalarField()) |
||||
|
publicWitness, _ := witness.Public() |
||||
|
pk, vk, err := groth16.Setup(r1cs) |
||||
|
proof, err := groth16.Prove(r1cs, pk, witness) |
||||
|
err = groth16.Verify(proof, vk, publicWitness) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
fmt.Println(proof) |
||||
|
return nil |
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
module cubic |
||||
|
|
||||
|
go 1.19 |
||||
|
|
||||
|
require ( |
||||
|
github.com/blang/semver/v4 v4.0.0 // indirect |
||||
|
github.com/consensys/bavard v0.1.13 // indirect |
||||
|
github.com/consensys/gnark v0.7.2-0.20220915193524-b33f88c79af6 // indirect |
||||
|
github.com/consensys/gnark-crypto v0.8.1-0.20220819163559-143c75519b0e // indirect |
||||
|
github.com/fxamacker/cbor/v2 v2.2.0 // indirect |
||||
|
github.com/google/pprof v0.0.0-20220729232143-a41b82acbcb1 // indirect |
||||
|
github.com/mattn/go-colorable v0.1.13 // indirect |
||||
|
github.com/mattn/go-isatty v0.0.16 // indirect |
||||
|
github.com/mmcloughlin/addchain v0.4.0 // indirect |
||||
|
github.com/rs/zerolog v1.28.0 // indirect |
||||
|
github.com/x448/float16 v0.8.4 // indirect |
||||
|
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect |
||||
|
rsc.io/tmplfunc v0.0.3 // indirect |
||||
|
) |
@ -0,0 +1,43 @@ |
|||||
|
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM= |
||||
|
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ= |
||||
|
github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/YjhQ= |
||||
|
github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI= |
||||
|
github.com/consensys/gnark v0.7.0 h1:zyI8zPAhSazrZPeQpKIFUHLgrnAuxI+EDrFLNizKb9I= |
||||
|
github.com/consensys/gnark v0.7.0/go.mod h1:oQnMurInsfe+9rG4l8qh8AFVihfuRCS5H3XPJH/6HPM= |
||||
|
github.com/consensys/gnark v0.7.2-0.20220915193524-b33f88c79af6 h1:T19XI5vxnMPOdoyyubM81cYr4MHll0n67EDrRjtA2Pg= |
||||
|
github.com/consensys/gnark v0.7.2-0.20220915193524-b33f88c79af6/go.mod h1:iN7/IRE6Eu2QKnTJEsoS48suBV0O5XrM+Nzo1ipp6Zw= |
||||
|
github.com/consensys/gnark-crypto v0.7.0 h1:rwdy8+ssmLYRqKp+ryRRgQJl/rCq2uv+n83cOydm5UE= |
||||
|
github.com/consensys/gnark-crypto v0.7.0/go.mod h1:KPSuJzyxkJA8xZ/+CV47tyqkr9MmpZA3PXivK4VPrVg= |
||||
|
github.com/consensys/gnark-crypto v0.7.1-0.20220603201101-938eff486457 h1:GCxBDfMg0bM1ZN2fi3OfywjDDr19N+SThchczg5kqJQ= |
||||
|
github.com/consensys/gnark-crypto v0.7.1-0.20220603201101-938eff486457/go.mod h1:9pJ7c3Pq7Rnw3iYoZldpbPRRGNJaRz47YgMiaavwyRY= |
||||
|
github.com/consensys/gnark-crypto v0.8.1-0.20220819163559-143c75519b0e h1:FyL5vcbvCIoD20M4Fvy+TXRbXKjT16gq7Uq0QDkyytM= |
||||
|
github.com/consensys/gnark-crypto v0.8.1-0.20220819163559-143c75519b0e/go.mod h1:ZTnSzNlt98CpwYIJyk6q/KVcshYWr3fOXXFrrY8a0QQ= |
||||
|
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= |
||||
|
github.com/fxamacker/cbor/v2 v2.2.0 h1:6eXqdDDe588rSYAi1HfZKbx6YYQO4mxQ9eC6xYpU/JQ= |
||||
|
github.com/fxamacker/cbor/v2 v2.2.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= |
||||
|
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= |
||||
|
github.com/google/pprof v0.0.0-20220729232143-a41b82acbcb1 h1:8pyqKJvrJqUYaKS851Ule26pwWvey6IDMiczaBLDKLQ= |
||||
|
github.com/google/pprof v0.0.0-20220729232143-a41b82acbcb1/go.mod h1:gSuNB+gJaOiQKLEZ+q+PK9Mq3SOzhRcw2GsGS/FhYDk= |
||||
|
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= |
||||
|
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= |
||||
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= |
||||
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= |
||||
|
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= |
||||
|
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= |
||||
|
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= |
||||
|
github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY= |
||||
|
github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU= |
||||
|
github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU= |
||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= |
||||
|
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= |
||||
|
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY= |
||||
|
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0= |
||||
|
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= |
||||
|
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= |
||||
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||
|
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||
|
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec h1:BkDtF2Ih9xZ7le9ndzTA7KJow28VbQW3odyk/8drmuI= |
||||
|
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||
|
rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU= |
||||
|
rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA= |