Skip to content

Commit 5f94471

Browse files
committed
Test for invalid encodings of BLS12381.
1 parent 456fe41 commit 5f94471

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

ecc/bls12381/g1.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (g *G1) SetBytes(b []byte) error {
4444
l = G1SizeCompressed
4545
}
4646
zeros := make([]byte, l-1)
47-
if (b[0]&0x1F) != 0 || subtle.ConstantTimeCompare(b[1:], zeros) != 1 {
47+
if (b[0]&0x1F) != 0 || subtle.ConstantTimeCompare(b[1:l], zeros) != 1 {
4848
return errEncoding
4949
}
5050
g.SetIdentity()

ecc/bls12381/g1_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ func TestG1Serial(t *testing.T) {
143143
want = *randomG1(t)
144144
}
145145
})
146+
t.Run("badPrefix", func(t *testing.T) {
147+
q := new(G1)
148+
b := make([]byte, G1Size)
149+
for _, b[0] = range []byte{0x20, 0x60, 0xE0} {
150+
test.CheckIsErr(t, q.SetBytes(b), mustErr)
151+
}
152+
})
146153
t.Run("badLength", func(t *testing.T) {
147154
q := new(G1)
148155
p := randomG1(t)

ecc/bls12381/g2.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (g *G2) SetBytes(b []byte) error {
4242
l = G2SizeCompressed
4343
}
4444
zeros := make([]byte, l-1)
45-
if (b[0]&0x1F) != 0 || subtle.ConstantTimeCompare(b[1:], zeros) != 1 {
45+
if (b[0]&0x1F) != 0 || subtle.ConstantTimeCompare(b[1:l], zeros) != 1 {
4646
return errEncoding
4747
}
4848
g.SetIdentity()

ecc/bls12381/g2_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ func TestG2Serial(t *testing.T) {
102102
want = *randomG2(t)
103103
}
104104
})
105+
t.Run("badPrefix", func(t *testing.T) {
106+
q := new(G2)
107+
b := make([]byte, G2Size)
108+
for _, b[0] = range []byte{0x20, 0x60, 0xE0} {
109+
test.CheckIsErr(t, q.SetBytes(b), mustErr)
110+
}
111+
})
105112
t.Run("badLength", func(t *testing.T) {
106113
q := new(G2)
107114
p := randomG2(t)

0 commit comments

Comments
 (0)