-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (122 loc) · 3.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Package ec
// Copyright 2023 Oleg Fomenko. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ec
import (
"crypto/elliptic"
"math/big"
)
// Curve implements elliptic curve with equation: y^2 =x^3 + ax + b
type Curve struct {
P *big.Int
N *big.Int
A *big.Int
B *big.Int
Gx, Gy *big.Int
BitSize int
}
// SECP256K1 returns an Ethereum secp256k1 curve
func SECP256K1() *Curve {
curve := &Curve{}
curve.P, _ = new(big.Int).SetString("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 0)
curve.N, _ = new(big.Int).SetString("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 0)
curve.A = new(big.Int).SetInt64(0)
curve.B = new(big.Int).SetInt64(7)
curve.Gx, _ = new(big.Int).SetString("0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 0)
curve.Gy, _ = new(big.Int).SetString("0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 0)
curve.BitSize = 256
return curve
}
var _ elliptic.Curve = &Curve{}
func (c *Curve) Params() *elliptic.CurveParams {
return &elliptic.CurveParams{
P: c.P,
N: c.N,
B: c.B,
Gx: c.Gx,
Gy: c.Gy,
BitSize: c.BitSize,
Name: "secp256k1",
}
}
func (c *Curve) IsOnCurve(x, y *big.Int) bool {
if x == nil && y == nil {
return true
}
yy := mul(y, y, c.P)
xxx := mul(mul(x, x, c.P), x, c.P)
ax := mul(c.A, x, c.P)
return yy.Cmp(add(xxx, ax, c.P)) == 0
}
func (c *Curve) Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int) {
if x1 == nil && y1 == nil {
return x2, y2
}
if x2 == nil && y2 == nil {
return x1, y1
}
s := div(sub(y1, y2, c.P), sub(x1, x2, c.P), c.P)
x = sub(sub(mul(s, s, c.P), x1, c.P), x2, c.P)
y = sub(mul(s, sub(x1, x, c.P), c.P), y1, c.P)
return
}
func (c *Curve) Double(x1, y1 *big.Int) (x, y *big.Int) {
if x1 == nil && y1 == nil {
return nil, nil
}
s := div(add(mul(fromInt(3), mul(x1, x1, c.P), c.P), c.A, c.P), mul(fromInt(2), y1, c.P), c.P)
x = sub(mul(s, s, c.P), mul(fromInt(2), x1, c.P), c.P)
y = sub(mul(s, sub(x1, x, c.P), c.P), y1, c.P)
return
}
func (c *Curve) ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int) {
if len(k) > 32 {
panic("K have to be 256 bits maximum")
}
if x1 == nil && y1 == nil {
return nil, nil
}
scalar := new(big.Int).Mod(new(big.Int).SetBytes(k), c.N)
bits := scalar.Text(2)
for i := len(bits) - 1; i >= 0; i-- {
bit := bits[i]
if bit == '1' {
x, y = c.Add(x, y, x1, y1)
}
x1, y1 = c.Double(x1, y1)
}
return
}
func (c *Curve) mult(k, x1, y1 *big.Int) (x, y *big.Int) {
if k.Cmp(fromInt(0)) == 0 {
return nil, nil
}
if k.Cmp(fromInt(1)) == 0 {
return x1, y1
}
x, y = c.mult(new(big.Int).Div(k, fromInt(2)), x1, y1)
x, y = c.Double(x, y)
if k.Bytes()[0] == 0 {
return x, y
}
return c.Add(x, y, x1, y1)
}
func (c *Curve) ScalarBaseMult(k []byte) (x, y *big.Int) {
return c.ScalarMult(c.Gx, c.Gx, k)
}
func add(x *big.Int, y *big.Int, mod *big.Int) *big.Int {
return new(big.Int).Mod(new(big.Int).Add(x, y), mod)
}
func sub(x *big.Int, y *big.Int, mod *big.Int) *big.Int {
return new(big.Int).Mod(new(big.Int).Sub(x, y), mod)
}
func mul(x *big.Int, y *big.Int, mod *big.Int) *big.Int {
return new(big.Int).Mod(new(big.Int).Mul(x, y), mod)
}
func div(x *big.Int, y *big.Int, mod *big.Int) *big.Int {
return new(big.Int).Mod(new(big.Int).Mul(x, new(big.Int).ModInverse(y, mod)), mod)
}
func fromInt(val int64) *big.Int {
return new(big.Int).SetInt64(val)
}