]> Cypherpunks.ru repositories - gogost.git/blob - gost3410/edwards.go
Raised copyright years
[gogost.git] / gost3410 / edwards.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2022 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package gost3410
17
18 import (
19         "math/big"
20 )
21
22 func (c *Curve) IsEdwards() bool {
23         return c.E != nil
24 }
25
26 func (c *Curve) EdwardsST() (*big.Int, *big.Int) {
27         if c.edS != nil {
28                 return c.edS, c.edT
29         }
30         c.edS = big.NewInt(0)
31         c.edS.Set(c.E)
32         c.edS.Sub(c.edS, c.D)
33         c.pos(c.edS)
34         var t big.Int
35         t.SetUint64(4)
36         t.ModInverse(&t, c.P)
37         c.edS.Mul(c.edS, &t)
38         c.edS.Mod(c.edS, c.P)
39         c.edT = big.NewInt(0)
40         c.edT.Set(c.E)
41         c.edT.Add(c.edT, c.D)
42         t.SetUint64(6)
43         t.ModInverse(&t, c.P)
44         c.edT.Mul(c.edT, &t)
45         c.edT.Mod(c.edT, c.P)
46         return c.edS, c.edT
47 }
48
49 // Convert Weierstrass X,Y coordinates to twisted Edwards U,V
50 func XY2UV(c *Curve, x, y *big.Int) (*big.Int, *big.Int) {
51         if !c.IsEdwards() {
52                 panic("non twisted Edwards curve")
53         }
54         edS, edT := c.EdwardsST()
55         var t big.Int
56         t.Sub(x, edT)
57         c.pos(&t)
58         u := big.NewInt(0)
59         u.ModInverse(y, c.P)
60         u.Mul(u, &t)
61         u.Mod(u, c.P)
62         v := big.NewInt(0).Set(&t)
63         v.Sub(v, edS)
64         c.pos(v)
65         t.Add(&t, edS)
66         t.ModInverse(&t, c.P)
67         v.Mul(v, &t)
68         v.Mod(v, c.P)
69         return u, v
70 }
71
72 // Convert twisted Edwards U,V coordinates to Weierstrass X,Y
73 func UV2XY(c *Curve, u, v *big.Int) (*big.Int, *big.Int) {
74         if !c.IsEdwards() {
75                 panic("non twisted Edwards curve")
76         }
77         edS, edT := c.EdwardsST()
78         var tx, ty big.Int
79         tx.Add(bigInt1, v)
80         tx.Mul(&tx, edS)
81         tx.Mod(&tx, c.P)
82         ty.Sub(bigInt1, v)
83         c.pos(&ty)
84         x := big.NewInt(0)
85         x.ModInverse(&ty, c.P)
86         x.Mul(x, &tx)
87         x.Add(x, edT)
88         x.Mod(x, c.P)
89         y := big.NewInt(0)
90         y.Mul(u, &ty)
91         y.ModInverse(y, c.P)
92         y.Mul(y, &tx)
93         y.Mod(y, c.P)
94         return x, y
95 }