github.com/Evanesco-Labs/go-evanesco@v1.0.1/zkpminer/problem/circuit.go (about)

     1  package problem
     2  
     3  import (
     4  	"github.com/consensys/gnark-crypto/ecc"
     5  	bn254 "github.com/consensys/gnark-crypto/ecc/bn254/fr/mimc"
     6  	"github.com/consensys/gnark/frontend"
     7  	"github.com/consensys/gnark/std/hash/mimc"
     8  	"hash"
     9  	"sync"
    10  )
    11  
    12  const SEED = "Go Evanesco"
    13  
    14  type Hasher struct {
    15  	mu   sync.Mutex
    16  	hash hash.Hash
    17  }
    18  
    19  var MimcHasher = Hasher{
    20  	mu:   sync.Mutex{},
    21  	hash: bn254.NewMiMC(SEED),
    22  }
    23  
    24  func (h *Hasher) Hash(m []byte) []byte {
    25  	h.mu.Lock()
    26  	defer h.mu.Unlock()
    27  	h.hash.Reset()
    28  	h.hash.Write(m)
    29  	return h.hash.Sum(nil)
    30  }
    31  
    32  type Circuit struct {
    33  	PreImage frontend.Variable `gnark:",public"`
    34  	Hash     frontend.Variable `gnark:",public"`
    35  }
    36  
    37  // Define declares the circuit's constraints
    38  // Hash = mimc(PreImage)
    39  func (circuit *Circuit) Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error {
    40  	// hash function
    41  	mimcIns, _ := mimc.NewMiMC(SEED, curveID)
    42  
    43  	// specify constraints
    44  	// mimc(preImage) == hash
    45  	cs.AssertIsEqual(circuit.Hash, mimcIns.Hash(cs, circuit.PreImage))
    46  	return nil
    47  }