X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=gost3410%2Fcurve.go;h=c516fc9236858afd716055544879197460a9d0c6;hb=HEAD;hp=a5f8d5125fe48da5d0f5573527e3a2a359da42f2;hpb=7bed9561c7c09958ad1268b397058431fd3362bc;p=gogost.git diff --git a/gost3410/curve.go b/gost3410/curve.go index a5f8d51..c1b93ed 100644 --- a/gost3410/curve.go +++ b/gost3410/curve.go @@ -1,5 +1,5 @@ // GoGOST -- Pure Go GOST cryptographic functions library -// Copyright (C) 2015-2021 Sergey Matveev +// Copyright (C) 2015-2024 Sergey Matveev // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -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,8 +78,25 @@ 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 { - return PointSize(c.P) + return pointSize(c.P) } func (c *Curve) pos(v *big.Int) { @@ -164,3 +171,7 @@ func (our *Curve) Equal(their *Curve) bool { ((our.D == nil && their.D == nil) || our.D.Cmp(their.D) == 0) && our.Co.Cmp(their.Co) == 0 } + +func (c *Curve) String() string { + return c.Name +}