From cd1b35daa4b2db076ed1613fc16b7341ca0f2c52 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Mon, 12 Jun 2023 15:41:15 +0300 Subject: [PATCH] gost3410.Curve.Contains --- gogost.go | 2 +- gost3410/curve.go | 27 ++++++++++++++++----------- news.texi | 5 +++++ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/gogost.go b/gogost.go index 2aad3a2..d34624f 100644 --- a/gogost.go +++ b/gogost.go @@ -1,4 +1,4 @@ // Pure Go GOST cryptographic functions library. package gogost -const Version = "5.10.0" +const Version = "5.11.0" diff --git a/gost3410/curve.go b/gost3410/curve.go index 6cc1135..50d0071 100644 --- a/gost3410/curve.go +++ b/gost3410/curve.go @@ -63,17 +63,7 @@ func NewCurve(p, q, a, b, x, y, e, d, co *big.Int) (*Curve, error) { X: x, Y: y, } - r1 := big.NewInt(0) - r2 := big.NewInt(0) - r1.Mul(c.Y, c.Y) - r1.Mod(r1, c.P) - r2.Mul(c.X, c.X) - r2.Add(r2, c.A) - r2.Mul(r2, c.X) - r2.Add(r2, c.B) - r2.Mod(r2, c.P) - c.pos(r2) - if r1.Cmp(r2) != 0 { + if !c.Contains(c.X, c.Y) { return nil, errors.New("gogost/gost3410: invalid curve parameters") } if e != nil && d != nil { @@ -88,6 +78,21 @@ func NewCurve(p, q, a, b, x, y, e, d, co *big.Int) (*Curve, error) { return &c, nil } +// Is point on curve? +func (c *Curve) Contains(x, y *big.Int) bool { + r1 := big.NewInt(0) + r2 := big.NewInt(0) + r1.Mul(y, y) + r1.Mod(r1, c.P) + r2.Mul(x, x) + r2.Add(r2, c.A) + r2.Mul(r2, x) + r2.Add(r2, c.B) + r2.Mod(r2, c.P) + c.pos(r2) + return r1.Cmp(r2) == 0 +} + // Get the size of the point's coordinate in bytes. // 32 for 256-bit curves, 64 for 512-bit ones. func (c *Curve) PointSize() int { diff --git a/news.texi b/news.texi index 92f34a1..3b0a8af 100644 --- a/news.texi +++ b/news.texi @@ -3,6 +3,11 @@ @table @strong +@anchor{Release 5.11.0} +@item 5.11.0 +You can check if public key is on curve with +@code{gost3410.Curve.Contains} method now. + @anchor{Release 5.10.0} @item 5.10.0 @itemize -- 2.44.0