github.com/consensys/gnark-crypto@v0.14.0/ecc/bls12-381/bls12381_test.go (about) 1 package bls12381 2 3 import ( 4 "encoding/hex" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 "gopkg.in/yaml.v2" 11 ) 12 13 var ( 14 testDir = "testing/bls" 15 deserializationG1Tests = filepath.Join(testDir, "deserialization_G1/*") 16 deserializationG2Tests = filepath.Join(testDir, "deserialization_G2/*") 17 ) 18 19 func TestDeserializationG1(t *testing.T) { 20 21 type Test struct { 22 Input struct { 23 PubKeyHexStr string `yaml:"pubkey"` 24 } 25 IsValidPredicate *bool `yaml:"output"` 26 } 27 tests, err := filepath.Glob(deserializationG1Tests) 28 require.NoError(t, err) 29 for _, testPath := range tests { 30 t.Run(testPath, func(t *testing.T) { 31 testFile, err := os.Open(testPath) 32 require.NoError(t, err) 33 test := Test{} 34 err = yaml.NewDecoder(testFile).Decode(&test) 35 require.NoError(t, testFile.Close()) 36 require.NoError(t, err) 37 testCaseValid := test.IsValidPredicate != nil 38 byts, err := hex.DecodeString(test.Input.PubKeyHexStr) 39 if err != nil && testCaseValid { 40 panic(err) 41 } 42 43 var point G1Affine 44 _, err = point.SetBytes(byts[:]) 45 if err == nil && !testCaseValid { 46 panic("err should not be nil") 47 } 48 if err != nil && testCaseValid { 49 panic("err should be nil") 50 } 51 }) 52 } 53 } 54 55 func TestDeserializationG2(t *testing.T) { 56 57 type Test struct { 58 Input struct { 59 SignatureHexStr string `yaml:"signature"` 60 } 61 IsValidPredicate *bool `yaml:"output"` 62 } 63 tests, err := filepath.Glob(deserializationG2Tests) 64 require.NoError(t, err) 65 for _, testPath := range tests { 66 t.Run(testPath, func(t *testing.T) { 67 testFile, err := os.Open(testPath) 68 require.NoError(t, err) 69 test := Test{} 70 err = yaml.NewDecoder(testFile).Decode(&test) 71 require.NoError(t, testFile.Close()) 72 require.NoError(t, err) 73 testCaseValid := test.IsValidPredicate != nil 74 byts, err := hex.DecodeString(test.Input.SignatureHexStr) 75 if err != nil && testCaseValid { 76 panic(err) 77 } 78 79 var point G2Affine 80 _, err = point.SetBytes(byts[:]) 81 if err == nil && !testCaseValid { 82 panic("err should not be nil") 83 } 84 if err != nil && testCaseValid { 85 panic("err should be nil") 86 } 87 }) 88 } 89 }