45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package zk
|
|
|
|
import (
|
|
"encoding/hex"
|
|
|
|
"github.com/consensys/gnark-crypto/ecc"
|
|
"github.com/consensys/gnark/backend/groth16"
|
|
"github.com/consensys/gnark/frontend"
|
|
"github.com/consensys/gnark/frontend/cs/r1cs"
|
|
"github.com/consensys/gnark/std/hash/mimc"
|
|
)
|
|
|
|
type Circuit struct {
|
|
Secret frontend.Variable
|
|
Hash frontend.Variable
|
|
}
|
|
|
|
func (circuit *Circuit) Define(api frontend.API) error {
|
|
mimc, _ := mimc.NewMiMC(api)
|
|
mimc.Write(circuit.Secret)
|
|
|
|
api.AssertIsEqual(circuit.Hash, mimc.Sum())
|
|
|
|
return nil
|
|
}
|
|
|
|
func POC() {
|
|
var circuit Circuit
|
|
|
|
r1cs, err := frontend.Compile(ecc.BW6_633.ScalarField(), r1cs.NewBuilder, &circuit)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
var assignment Circuit
|
|
assignment.Secret = "0x" + hex.EncodeToString([]byte("Hello, World!"))
|
|
assignment.Hash = "27353532363483121849779111558069699868988271024434984431742151050156752877051628698253914937683"
|
|
|
|
witness, _ := frontend.NewWitness(&assignment, ecc.BW6_633.ScalarField())
|
|
pk, vk, _ := groth16.Setup(r1cs)
|
|
proof, _ := groth16.Prove(r1cs, pk, witness)
|
|
publicWitness, _ := witness.Public()
|
|
_ = groth16.Verify(proof, vk, publicWitness)
|
|
}
|