PageSourceSearch

https://docs.polyhedra.network/assets/js/76f64055.1a6a59dc.js

js polyhedra.network collected 2026-10-03 21:57:30 UTC 5,177 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkpolyhedra_docs=self.webpackChunkpolyhedra_docs||[]).push([[8210],{6054:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>a,contentTitle:()=>c,default:()=>d,frontMatter:()=>o,metadata:()=>s,toc:()=>l});var r=t(4848),i=t(8453);const o={sidebar_position:2},c="Example",s={id:"expander/go/example",title:"Example",description:"Consider the following gnark circuit as an example:",source:"@site/docs/expander/go/example.md",sourceDirName:"expander/go",slug:"/expander/go/example",permalink:"/expander/go/example",draft:!1,unlisted:!1,tags:[],version:"current",sidebarPosition:2,frontMatter:{sidebar_position:2},sidebar:"expanderSidebar",previous:{title:"Introduction",permalink:"/expander/go/intro"},next:{title:"Compiler APIs",permalink:"/expander/go/apis"}},a={},l=[];function p(e){const n={a:"a",code:"code",h1:"h1",header:"header",p:"p",pre:"pre",...(0,i.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(n.header,{children:(0,r.jsx)(n.h1,{id:"example",children:"Example"})}),"\n",(0,r.jsx)(n.p,{children:"Consider the following gnark circuit as an example:"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-go",children:'package main\n\nimport (\n\t"github.com/consensys/gnark-crypto/ecc"\n\t"github.com/consensys/gnark/backend/groth16"\n\t"github.com/consensys/gnark/frontend"\n\t"github.com/consensys/gnark/frontend/cs/r1cs"\n)\n\ntype Circuit struct {\n\tX frontend.Variable\n\tY frontend.Variable\n}\n\nfunc (circuit *Circuit) Define(api frontend.API) error {\n\tapi.AssertIsEqual(circuit.X, circuit.Y)\n\treturn nil\n}\n\nfunc main() {\n\tassignment := &Circuit{X: 1, Y: 1}\n\n\tr1cs, _ := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &Circuit{})\n\twitness, _ := frontend.NewWitness(assignment, ecc.BN254.ScalarField())\n\tpublicWitness, _ := witness.Public()\n\tpk, vk, _ := groth16.Setup(r1cs)\n\tproof, _ := groth16.Prove(r1cs, pk, witness)\n\terr := groth16.Verify(proof, vk, publicWitness)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n'})}),"\n",(0,r.jsx)(n.p,{children:"To adapt this circuit for our compiler, we only need to modify the compilation and execution sections within the main function. Here's how:"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-go",children:'package main\n\nimport (\n\t"os"\n\n\t"github.com/consensys/gnark-crypto/ecc"\n\t"github.com/consensys/gnark/frontend"\n\n\t"github.com/PolyhedraZK/ExpanderCompilerCollection/ecgo"\n\t"github.com/PolyhedraZK/ExpanderCompilerCollection/ecgo/test"\n)\n\ntype Circuit struct {\n\tX frontend.Variable\n\tY frontend.Variable\n}\n\nfunc (circuit *Circuit) Define(api frontend.API) error {\n\tapi.AssertIsEqual(circuit.X, circuit.Y)\n\treturn nil\n}\n\nfunc main() {\n\tassignment := &Circuit{X: 1, Y: 1}\n\n\tcircuit, _ := ecgo.Compile(ecc.BN254.ScalarField(), &Circuit{})\n\tc := circuit.GetLayeredCircuit()\n\tos.WriteFile("circuit.txt", c.Serialize(), 0o644)\n\tinputSolver := circuit.GetInputSolver()\n\twitness, _ := inputSolver.SolveInputAuto(assignment)\n\tos.WriteFile("witness.txt", witness.Serialize(), 0o644)\n\tif !test.CheckCircuit(c, witness) {\n\t\tpanic("verification failed")\n\t}\n}\n'})}),"\n",(0,r.jsxs)(n.p,{children:["Please note that the Prover's implementation resides in ",(0,r.jsx)(n.a,{href:"https://github.com/PolyhedraZK/Expander",children:"Expander"}),". As such, the ",(0,r.jsx)(n.code,{children:"CheckCircuit"})," function in this context merely evaluates the circuit once to confirm its correctness. To generate and verify the actual proof, supply ",(0,r.jsx)(n.code,{children:"circuit.txt"})," and ",(0,r.jsx)(n.code,{children:"witness.txt"})," to Expander."]}),"\n",(0,r.jsxs)(n.p,{children:["In contrast to gnark, where the prover handles a portion of the witness generation, our compiler employs an ",(0,r.jsx)(n.code,{children:"InputSolver"})," to perform this task. The ",(0,r.jsx)(n.code,{children:"InputSolver"})," also supports serialization and deserialization. For more information, please consult the examples in ",(0,r.jsx)(n.a,{href:"https://github.com/PolyhedraZK/ExpanderCompilerCollection/blob/master/ecgo/examples/mimc",children:"MIMC"})," and ",(0,r.jsx)(n.a,{href:"https://github.com/PolyhedraZK/ExpanderCompilerCollection/blob/master/ecgo/mimc_deserialize",children:"MIMC Deserialize"}),"."]}),"\n",(0,r.jsxs)(n.p,{children:["For additional features, please refer to ",(0,r.jsx)(n.a,{href:"./apis",children:"APIs"}),"."]}),"\n",(0,r.jsxs)(n.p,{children:["For more examples, please visit ",(0,r.jsx)(n.a,{href:"https://github.com/PolyhedraZK/ExpanderCompilerCollection/blob/master/ecgo/examples",children:"examples"}),"."]})]})}function d(e={}){const{wrapper:n}={...(0,i.R)(),...e.components};return n?(0,r.jsx)(n,{...e,children:(0,r.jsx)(p,{...e})}):p(e)}},8453:(e,n,t)=>{t.d(n,{R:()=>c,x:()=>s});var r=t(6540);const i={},o=r.createContext(i);function c(e){const n=r.useContext(o);return r.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function s(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:c(e.components),r.createElement(o.Provider,{value:n},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.