-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsecp256k1.dart
More file actions
184 lines (151 loc) · 4.65 KB
/
Copy pathsecp256k1.dart
File metadata and controls
184 lines (151 loc) · 4.65 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import 'dart:typed_data';
import 'package:witnet/utils.dart';
import '../number_theory.dart' show inverseMulti, positiveMod;
class Secp256k1 {
static final BigInt p = BigInt.parse(
'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f',
radix: 16);
static final BigInt a = BigInt.parse('0', radix: 16);
static final BigInt b = BigInt.parse('7', radix: 16);
static final BigInt n = BigInt.parse(
'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141',
radix: 16);
static final BigInt h = BigInt.parse('1', radix: 16);
static final Point G = Point(
BigInt.parse(
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
radix: 16),
BigInt.parse(
'483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
radix: 16),
);
}
class CurveElement {
final BigInt value;
CurveElement(this.value);
}
class Point {
Point(
this.x,
this.y,
);
final BigInt x;
final BigInt y;
Uint8List encode({bool compressed = true}) {
return hexToBytes(pointToHexInCompress(this));
}
factory Point.fromHex(String data) {
return hexToPointFromCompress(data);
}
Point addSame() {
return addSamePoint(this, Secp256k1.n, Secp256k1.a);
}
Point operator +(Point other) {
return addDiffPoint(this, other, Secp256k1.p);
}
Point operator *(BigInt other) {
return pointMultiply(this, other, Secp256k1.p, Secp256k1.a);
}
}
Point bigIntToPoint(BigInt n) {
return hexToPoint(n.toRadixString(16));
}
Point hexToPoint(String hex) {
final len = 130;
if (hex.length != len) {
throw ('point length must be $len!');
}
if (hex.substring(0, 2) != '04') {
throw ('point prefix incorrect!');
}
return Point(
BigInt.parse(hex.substring(2, 66), radix: 16),
BigInt.parse(hex.substring(66, 130), radix: 16),
);
}
Point addSamePoint(Point point, BigInt modNum, BigInt a) {
var ru = positiveMod(
(BigInt.from(3) * point.x.pow(2) + a) *
inverseMulti(BigInt.two * point.y, modNum),
modNum);
var x3 = positiveMod(ru.pow(2) - (BigInt.two * point.x), modNum);
var y3 = positiveMod(ru * (point.x - x3) - point.y, modNum);
return Point(x3, y3);
}
Point addDiffPoint(Point point1, Point point2, BigInt modNum) {
var ru = positiveMod(
(point2.y - point1.y) * inverseMulti(point2.x - point1.x, modNum),
modNum);
var x3 = positiveMod(ru.pow(2) - point1.x - point2.x, modNum);
var y3 = positiveMod(ru * (point1.x - x3) - point1.y, modNum);
return Point(x3, y3);
}
/// double-and-add method for point multiplication.
Point pointMultiply(Point point, BigInt k, BigInt modNum, BigInt a) {
Point result = Point(BigInt.zero, BigInt.zero);
Point addend = point;
while (k > BigInt.zero) {
if (k.isOdd) {
if (result.x == BigInt.zero && result.y == BigInt.zero) {
result = addend;
} else {
result = addDiffPoint(result, addend, modNum);
}
}
addend = addSamePoint(addend, modNum, a);
k = k >> 1; // k = k / 2
}
return result;
}
Point getPointByBigInt(BigInt n, BigInt p, BigInt a, Point pointG) {
var bin = n.toRadixString(2);
var nextPoint = pointG;
Point? nowPoint;
for (var i = bin.length - 1; i >= 0; i--) {
if (bin[i] == '1') {
if (nowPoint == null) {
nowPoint = nextPoint;
} else {
nowPoint = addDiffPoint(nowPoint, nextPoint, p);
}
}
nextPoint = addSamePoint(nextPoint, p, a);
}
return nowPoint!;
}
Point hexToPointFromCompress(String hex) {
final len = 66;
if (hex.length != len) {
throw ('point length must be $len!');
}
var firstByte = int.parse(hex.substring(0, 2), radix: 16);
if ((firstByte & ~1) != 2) {
throw ('point prefix incorrect!');
}
var x = BigInt.parse(hex.substring(2, 66), radix: 16);
// The curve equation for secp256k1 is: y^2 = x^3 + 7.
var ySqared =
((x.modPow(BigInt.from(3), Secp256k1.p)) + BigInt.from(7)) % Secp256k1.p;
// power = (p+1) // 4
var p1 = Secp256k1.p + BigInt.from(1); // p+1
var power = (p1 - p1 % BigInt.from(4)) ~/ BigInt.from(4);
var y = ySqared.modPow(power, Secp256k1.p);
var sq = y.pow(2) % Secp256k1.p;
if (sq != ySqared) {
throw ('failed to retrieve y of public key from hex');
}
var firstBit = (y & BigInt.one).toInt();
if (firstBit != (firstByte & 1)) {
y = Secp256k1.p - y;
}
return Point(
x,
y,
);
}
String pointToHexInCompress(Point point) {
// var byteLen = 32; //(256 + 7) >> 3 // => so len of str is (32+1) * 2 = 66;
var firstBit = 2 + (point.y & BigInt.one).toInt();
var prefix = firstBit.toRadixString(16).padLeft(2, '0');
return prefix + point.x.toRadixString(16).padLeft(64, '0');
}