Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80575 views
1
'use strict';
2
3
var curves = exports;
4
5
var hash = require('hash.js');
6
var elliptic = require('../elliptic');
7
8
var assert = elliptic.utils.assert;
9
10
function PresetCurve(options) {
11
if (options.type === 'short')
12
this.curve = new elliptic.curve.short(options);
13
else if (options.type === 'edwards')
14
this.curve = new elliptic.curve.edwards(options);
15
else
16
this.curve = new elliptic.curve.mont(options);
17
this.g = this.curve.g;
18
this.n = this.curve.n;
19
this.hash = options.hash;
20
21
assert(this.g.validate(), 'Invalid curve');
22
assert(this.g.mul(this.n).isInfinity(), 'Invalid curve, G*N != O');
23
}
24
curves.PresetCurve = PresetCurve;
25
26
function defineCurve(name, options) {
27
Object.defineProperty(curves, name, {
28
configurable: true,
29
enumerable: true,
30
get: function() {
31
var curve = new PresetCurve(options);
32
Object.defineProperty(curves, name, {
33
configurable: true,
34
enumerable: true,
35
value: curve
36
});
37
return curve;
38
}
39
});
40
}
41
42
defineCurve('p192', {
43
type: 'short',
44
prime: 'p192',
45
p: 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff',
46
a: 'ffffffff ffffffff ffffffff fffffffe ffffffff fffffffc',
47
b: '64210519 e59c80e7 0fa7e9ab 72243049 feb8deec c146b9b1',
48
n: 'ffffffff ffffffff ffffffff 99def836 146bc9b1 b4d22831',
49
hash: hash.sha256,
50
gRed: false,
51
g: [
52
'188da80e b03090f6 7cbf20eb 43a18800 f4ff0afd 82ff1012',
53
'07192b95 ffc8da78 631011ed 6b24cdd5 73f977a1 1e794811'
54
]
55
});
56
57
defineCurve('p224', {
58
type: 'short',
59
prime: 'p224',
60
p: 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001',
61
a: 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff fffffffe',
62
b: 'b4050a85 0c04b3ab f5413256 5044b0b7 d7bfd8ba 270b3943 2355ffb4',
63
n: 'ffffffff ffffffff ffffffff ffff16a2 e0b8f03e 13dd2945 5c5c2a3d',
64
hash: hash.sha256,
65
gRed: false,
66
g: [
67
'b70e0cbd 6bb4bf7f 321390b9 4a03c1d3 56c21122 343280d6 115c1d21',
68
'bd376388 b5f723fb 4c22dfe6 cd4375a0 5a074764 44d58199 85007e34'
69
]
70
});
71
72
defineCurve('p256', {
73
type: 'short',
74
prime: null,
75
p: 'ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff',
76
a: 'ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff fffffffc',
77
b: '5ac635d8 aa3a93e7 b3ebbd55 769886bc 651d06b0 cc53b0f6 3bce3c3e 27d2604b',
78
n: 'ffffffff 00000000 ffffffff ffffffff bce6faad a7179e84 f3b9cac2 fc632551',
79
hash: hash.sha256,
80
gRed: false,
81
g: [
82
'6b17d1f2 e12c4247 f8bce6e5 63a440f2 77037d81 2deb33a0 f4a13945 d898c296',
83
'4fe342e2 fe1a7f9b 8ee7eb4a 7c0f9e16 2bce3357 6b315ece cbb64068 37bf51f5'
84
]
85
});
86
87
defineCurve('curve25519', {
88
type: 'mont',
89
prime: 'p25519',
90
p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
91
a: '76d06',
92
b: '0',
93
n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed',
94
hash: hash.sha256,
95
gRed: false,
96
g: [
97
'9'
98
]
99
});
100
101
defineCurve('ed25519', {
102
type: 'edwards',
103
prime: 'p25519',
104
p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
105
a: '-1',
106
c: '1',
107
// -121665 * (121666^(-1)) (mod P)
108
d: '52036cee2b6ffe73 8cc740797779e898 00700a4d4141d8ab 75eb4dca135978a3',
109
n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed',
110
hash: hash.sha256,
111
gRed: false,
112
g: [
113
'216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a',
114
115
// 4/5
116
'6666666666666666666666666666666666666666666666666666666666666658'
117
]
118
});
119
120
var pre;
121
try {
122
pre = require('./precomputed/secp256k1');
123
} catch (e) {
124
pre = undefined;
125
}
126
127
defineCurve('secp256k1', {
128
type: 'short',
129
prime: 'k256',
130
p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f',
131
a: '0',
132
b: '7',
133
n: 'ffffffff ffffffff ffffffff fffffffe baaedce6 af48a03b bfd25e8c d0364141',
134
h: '1',
135
hash: hash.sha256,
136
137
// Precomputed endomorphism
138
beta: '7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee',
139
lambda: '5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72',
140
basis: [
141
{
142
a: '3086d221a7d46bcde86c90e49284eb15',
143
b: '-e4437ed6010e88286f547fa90abfe4c3'
144
},
145
{
146
a: '114ca50f7a8e2f3f657c1108d9d44cfd8',
147
b: '3086d221a7d46bcde86c90e49284eb15'
148
}
149
],
150
151
gRed: false,
152
g: [
153
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
154
'483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
155
pre
156
]
157
});
158
159