]> Cypherpunks.ru repositories - gogost.git/blobdiff - src/cypherpunks.ru/gogost/gost3410/curve.go
Reuse pos() method
[gogost.git] / src / cypherpunks.ru / gogost / gost3410 / curve.go
index 70f5a66c3596f11b89df39fea74ee1a0a4119da1..0ad10c9ff747dcc4d3b3b5087f735d4bf2ee98a8 100644 (file)
@@ -1,5 +1,5 @@
 // GoGOST -- Pure Go GOST cryptographic functions library
-// Copyright (C) 2015-2016 Sergey Matveev <stargrave@stargrave.org>
+// Copyright (C) 2015-2019 Sergey Matveev <stargrave@stargrave.org>
 //
 // 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
@@ -29,14 +29,16 @@ var (
 )
 
 type Curve struct {
-       P *big.Int
-       Q *big.Int
+       P *big.Int // Characteristic of the underlying prime field
+       Q *big.Int // Elliptic curve subgroup order
+
+       // Equation coefficients of the elliptic curve in canonical form
        A *big.Int
        B *big.Int
 
        // Basic point X and Y coordinates
-       Bx *big.Int
-       By *big.Int
+       X *big.Int
+       Y *big.Int
 
        // Temporary variable for the add method
        t  *big.Int
@@ -58,19 +60,18 @@ func NewCurve(p, q, a, b, bx, by []byte) (*Curve, error) {
        }
        r1 := big.NewInt(0)
        r2 := big.NewInt(0)
-       r1.Mul(c.By, c.By)
+       r1.Mul(c.Y, c.Y)
        r1.Mod(r1, c.P)
-       r2.Mul(c.Bx, c.Bx)
+       r2.Mul(c.X, c.X)
        r2.Add(r2, c.A)
-       r2.Mul(r2, c.Bx)
+       r2.Mul(r2, c.X)
        r2.Add(r2, c.B)
        r2.Mod(r2, c.P)
-       if r2.Cmp(big.NewInt(0)) == -1 {
-               r2.Add(r2, c.P)
-       }
+       c.pos(r2)
        if r1.Cmp(r2) != 0 {
                return nil, errors.New("Invalid curve parameters")
        }
+       }
        return &c, nil
 }
 
@@ -116,14 +117,14 @@ func (c *Curve) add(p1x, p1y, p2x, p2y *big.Int) {
 }
 
 func (c *Curve) Exp(degree, xS, yS *big.Int) (*big.Int, *big.Int, error) {
+       if degree.Cmp(zero) == 0 {
+               return nil, nil, errors.New("Bad degree value")
+       }
        dg := big.NewInt(0).Sub(degree, bigInt1)
        tx := big.NewInt(0).Set(xS)
        ty := big.NewInt(0).Set(yS)
        cx := big.NewInt(0).Set(xS)
        cy := big.NewInt(0).Set(yS)
-       if dg.Cmp(zero) == 0 {
-               return nil, nil, errors.New("Bad degree value")
-       }
        for dg.Cmp(zero) != 0 {
                if dg.Bit(0) == 1 {
                        c.add(tx, ty, cx, cy)