]> Cypherpunks.ru repositories - gogost.git/blobdiff - gost3410/curve.go
gost3410.PublicKey.Equal method, necessary for Go 1.17
[gogost.git] / gost3410 / curve.go
index 696beabed9a68889675f9b96237a924fff30ea91..1aa783c98e9e452f96e972ecb288aafce07a8f3b 100644 (file)
@@ -1,5 +1,5 @@
 // GoGOST -- Pure Go GOST cryptographic functions library
-// Copyright (C) 2015-2020 Sergey Matveev <stargrave@stargrave.org>
+// Copyright (C) 2015-2021 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
@@ -25,6 +25,7 @@ var (
        bigInt1 *big.Int = big.NewInt(1)
        bigInt2 *big.Int = big.NewInt(2)
        bigInt3 *big.Int = big.NewInt(3)
+       bigInt4 *big.Int = big.NewInt(4)
 )
 
 type Curve struct {
@@ -33,6 +34,8 @@ type Curve struct {
        P *big.Int // Characteristic of the underlying prime field
        Q *big.Int // Elliptic curve subgroup order
 
+       Co *big.Int // Cofactor
+
        // Equation coefficients of the elliptic curve in canonical form
        A *big.Int
        B *big.Int
@@ -55,7 +58,7 @@ type Curve struct {
        edT *big.Int
 }
 
-func NewCurve(p, q, a, b, x, y, e, d *big.Int) (*Curve, error) {
+func NewCurve(p, q, a, b, x, y, e, d, co *big.Int) (*Curve, error) {
        c := Curve{
                Name: "unknown",
                P:    p,
@@ -85,9 +88,18 @@ func NewCurve(p, q, a, b, x, y, e, d *big.Int) (*Curve, error) {
                c.E = e
                c.D = d
        }
+       if co == nil {
+               c.Co = bigInt1
+       } else {
+               c.Co = co
+       }
        return &c, nil
 }
 
+func (c *Curve) PointSize() int {
+       return PointSize(c.P)
+}
+
 func (c *Curve) pos(v *big.Int) {
        if v.Cmp(zero) < 0 {
                v.Add(v, c.P)
@@ -147,3 +159,15 @@ func (c *Curve) Exp(degree, xS, yS *big.Int) (*big.Int, *big.Int, error) {
        }
        return tx, ty, nil
 }
+
+func (our *Curve) Equal(their *Curve) bool {
+       return our.P.Cmp(their.P) == 0 &&
+               our.Q.Cmp(their.Q) == 0 &&
+               our.A.Cmp(their.A) == 0 &&
+               our.B.Cmp(their.B) == 0 &&
+               our.X.Cmp(their.X) == 0 &&
+               our.Y.Cmp(their.Y) == 0 &&
+               ((our.E == nil && their.E == nil) || our.E.Cmp(their.E) == 0) &&
+               ((our.D == nil && their.D == nil) || our.D.Cmp(their.D) == 0) &&
+               our.Co.Cmp(their.Co) == 0
+}