|
1 | 1 | package evmprecompiles |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "crypto/rand" |
| 6 | + "encoding/hex" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io/ioutil" |
5 | 10 | "math/big" |
6 | 11 | "testing" |
7 | 12 |
|
@@ -69,3 +74,72 @@ func TestP256VerifyCircuit(t *testing.T) { |
69 | 74 | err = test.IsSolved(&circuit, &witness, ecc.BN254.ScalarField()) |
70 | 75 | assert.NoError(err) |
71 | 76 | } |
| 77 | + |
| 78 | +func TestP256VerifyCircuitWithEIPVectors(t *testing.T) { |
| 79 | + assert := test.NewAssert(t) |
| 80 | + data, err := ioutil.ReadFile("test_vectors/p256verify_vectors_clean.json") |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("read vectors.json: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + var vecs []vector |
| 86 | + if err := json.Unmarshal(data, &vecs); err != nil { |
| 87 | + t.Fatalf("unmarshal: %v", err) |
| 88 | + } |
| 89 | + for i, v := range vecs { |
| 90 | + h, r, s, qx, qy := splitInput160(v.Input) |
| 91 | + verified := expectedBool(v.Expected) |
| 92 | + expected := frontend.Variable(0) |
| 93 | + if verified { |
| 94 | + expected = 1 |
| 95 | + } |
| 96 | + witness := p256verifyCircuit{ |
| 97 | + MsgHash: emulated.ValueOf[emulated.P256Fr](*h), |
| 98 | + R: emulated.ValueOf[emulated.P256Fr](*r), |
| 99 | + S: emulated.ValueOf[emulated.P256Fr](*s), |
| 100 | + Qx: emulated.ValueOf[emulated.P256Fp](*qx), |
| 101 | + Qy: emulated.ValueOf[emulated.P256Fp](*qy), |
| 102 | + Expected: expected, |
| 103 | + } |
| 104 | + |
| 105 | + circuit := p256verifyCircuit{} |
| 106 | + |
| 107 | + t.Run(fmt.Sprintf("vector_%03d_%s", i, v.Name), func(t *testing.T) { |
| 108 | + err := test.IsSolved(&circuit, &witness, ecc.BN254.ScalarField()) |
| 109 | + assert.NoError(err) |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// --- utils |
| 115 | +type vector struct { |
| 116 | + Name string `json:"Name,omitempty"` |
| 117 | + Input string `json:"Input"` |
| 118 | + Expected string `json:"Expected"` |
| 119 | +} |
| 120 | + |
| 121 | +func splitInput160(hexInput string) (h, r, s, qx, qy *big.Int) { |
| 122 | + raw, err := hex.DecodeString(hexInput) |
| 123 | + if err != nil { |
| 124 | + panic(err) |
| 125 | + } |
| 126 | + if len(raw) != 160 { |
| 127 | + return nil, nil, nil, nil, nil |
| 128 | + } |
| 129 | + h = new(big.Int).SetBytes(raw[0:32]) |
| 130 | + r = new(big.Int).SetBytes(raw[32:64]) |
| 131 | + s = new(big.Int).SetBytes(raw[64:96]) |
| 132 | + qx = new(big.Int).SetBytes(raw[96:128]) |
| 133 | + qy = new(big.Int).SetBytes(raw[128:160]) |
| 134 | + return |
| 135 | +} |
| 136 | + |
| 137 | +func expectedBool(s string) bool { |
| 138 | + raw, err := hex.DecodeString(s) |
| 139 | + if err != nil { |
| 140 | + panic(err) |
| 141 | + } |
| 142 | + one := make([]byte, 32) |
| 143 | + one[31] = 1 |
| 144 | + return bytes.Equal(raw, one) |
| 145 | +} |
0 commit comments