Skip to content

Commit 65181e8

Browse files
fjlucwong
authored andcommitted
crypto/secp256k1: fix coordinate check
1 parent 4d19ab9 commit 65181e8

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

crypto/secp256k1/curve.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ func (bitCurve *BitCurve) Params() *elliptic.CurveParams {
7373

7474
// IsOnCurve returns true if the given (x,y) lies on the BitCurve.
7575
func (bitCurve *BitCurve) IsOnCurve(x, y *big.Int) bool {
76+
if x.Cmp(bitCurve.P) >= 0 || y.Cmp(bitCurve.P) >= 0 {
77+
return false
78+
}
79+
7680
// y² = x³ + b
7781
y2 := new(big.Int).Mul(y, y) //y²
7882
y2.Mod(y2, bitCurve.P) //y²%P

crypto/secp256k1/ext.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, unsigned char *point,
109109
ARG_CHECK(scalar != NULL);
110110
(void)ctx;
111111

112-
secp256k1_fe_set_b32_limit(&feX, point);
113-
secp256k1_fe_set_b32_limit(&feY, point+32);
112+
if (!secp256k1_fe_set_b32_limit(&feX, point) ||
113+
!secp256k1_fe_set_b32_limit(&feY, point+32)) {
114+
return 0;
115+
}
114116
secp256k1_ge_set_xy(&ge, &feX, &feY);
115117
secp256k1_scalar_set_b32(&s, scalar, &overflow);
116118
if (overflow || secp256k1_scalar_is_zero(&s)) {

crypto/signature_nocgo.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,14 @@ type btCurve struct {
167167
*secp256k1.KoblitzCurve
168168
}
169169

170-
// Marshall converts a point given as (x, y) into a byte slice.
170+
func (curve btCurve) IsOnCurve(x, y *big.Int) bool {
171+
if x.Cmp(secp256k1.Params().P) >= 0 || y.Cmp(secp256k1.Params().P) >= 0 {
172+
return false
173+
}
174+
return curve.KoblitzCurve.IsOnCurve(x, y)
175+
}
176+
177+
// Marshal converts a point given as (x, y) into a byte slice.
171178
func (curve btCurve) Marshal(x, y *big.Int) []byte {
172179
byteLen := (curve.Params().BitSize + 7) / 8
173180

0 commit comments

Comments
 (0)