github.com/consensys/gnark-crypto@v0.14.0/internal/generator/gkr/template/gkr.test.vectors.gen.go.tmpl (about) 1 import ( 2 "encoding/json" 3 "fmt" 4 fiatshamir "github.com/consensys/gnark-crypto/fiat-shamir" 5 "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational" 6 "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational/gkr" 7 "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational/polynomial" 8 "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational/sumcheck" 9 "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational/test_vector_utils" 10 "os" 11 "path/filepath" 12 "reflect" 13 "hash" 14 ) 15 16 func main() { 17 if err := GenerateVectors(); err != nil { 18 fmt.Println(err.Error()) 19 os.Exit(-1) 20 } 21 } 22 23 func GenerateVectors() error { 24 testDirPath, err := filepath.Abs("gkr/test_vectors") 25 if err != nil { 26 return err 27 } 28 29 fmt.Printf("generating GKR test cases: scanning directory %s for test specs\n", testDirPath) 30 31 dirEntries, err := os.ReadDir(testDirPath) 32 if err != nil { 33 return err 34 } 35 for _, dirEntry := range dirEntries { 36 if !dirEntry.IsDir() { 37 38 if filepath.Ext(dirEntry.Name()) == ".json" { 39 fmt.Println("\tprocessing", dirEntry.Name()) 40 path := filepath.Join(testDirPath, dirEntry.Name()) 41 if err = run(path); err != nil { 42 return err 43 } 44 } 45 } 46 } 47 48 return nil 49 } 50 51 func run(absPath string) error { 52 testCase, err := newTestCase(absPath) 53 if err != nil { 54 return err 55 } 56 57 transcriptSetting := fiatshamir.WithHash(testCase.Hash) 58 59 var proof gkr.Proof 60 proof, err = gkr.Prove(testCase.Circuit, testCase.FullAssignment, transcriptSetting) 61 if err != nil { 62 return err 63 } 64 65 if testCase.Info.Proof, err = toPrintableProof(proof); err != nil { 66 return err 67 } 68 var outBytes []byte 69 if outBytes, err = json.MarshalIndent(testCase.Info, "", "\t"); err == nil { 70 if err = os.WriteFile(absPath, outBytes, 0); err != nil { 71 return err 72 } 73 } else { 74 return err 75 } 76 77 testCase, err = newTestCase(absPath) 78 if err != nil { 79 return err 80 } 81 82 err = gkr.Verify(testCase.Circuit, testCase.InOutAssignment, proof, transcriptSetting) 83 if err != nil { 84 return err 85 } 86 87 testCase, err = newTestCase(absPath) 88 if err != nil { 89 return err 90 } 91 92 err = gkr.Verify(testCase.Circuit, testCase.InOutAssignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(2, 0))) 93 if err == nil { 94 return fmt.Errorf("bad proof accepted") 95 } 96 return nil 97 } 98 99 func toPrintableProof(proof gkr.Proof) (PrintableProof, error) { 100 res := make(PrintableProof, len(proof)) 101 102 for i := range proof { 103 104 partialSumPolys := make([][]interface{}, len(proof[i].PartialSumPolys)) 105 for k, partialK := range proof[i].PartialSumPolys { 106 partialSumPolys[k] = test_vector_utils.ElementSliceToInterfaceSlice(partialK) 107 } 108 109 res[i] = PrintableSumcheckProof{ 110 FinalEvalProof: test_vector_utils.ElementSliceToInterfaceSlice(proof[i].FinalEvalProof), 111 PartialSumPolys: partialSumPolys, 112 } 113 } 114 return res, nil 115 } 116 117 var Gates = gkr.Gates 118 119 {{template "gkrTestVectors" .}}