github.com/consensys/gnark-crypto@v0.14.0/ecc/bn254/fr/mimc/test_vectors/main.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/consensys/gnark-crypto/ecc/bn254/fr" 7 "github.com/consensys/gnark-crypto/ecc/bn254/fr/mimc" 8 "os" 9 ) 10 11 type numericalMiMCTestCase struct { 12 In []string `json:"in"` 13 Out string `json:"out"` 14 } 15 16 func assertNoError(err error) { 17 if err != nil { 18 fmt.Println(err) 19 os.Exit(-1) 20 } 21 } 22 23 //go:generate go run main.go 24 func main() { 25 var tests []numericalMiMCTestCase 26 27 bytes, err := os.ReadFile("./vectors.json") 28 assertNoError(err) 29 assertNoError(json.Unmarshal(bytes, &tests)) 30 31 hsh := mimc.NewMiMC() 32 33 for i := range tests { 34 35 hsh.Reset() 36 var x fr.Element 37 for j := range tests[i].In { 38 _, err = x.SetString(tests[i].In[j]) 39 assertNoError(err) 40 41 b := x.Bytes() 42 _, err = hsh.Write(b[:]) 43 assertNoError(err) 44 } 45 46 bytes = hsh.Sum(nil) 47 48 x.SetBytes(bytes) 49 tests[i].Out = "0x" + x.Text(16) 50 } 51 52 bytes, err = json.MarshalIndent(tests, "", "\t") 53 assertNoError(err) 54 err = os.WriteFile("./vectors.json", bytes, 0) 55 assertNoError(err) 56 }