207 lines
6.3 KiB
JavaScript
207 lines
6.3 KiB
JavaScript
'use strict';
|
|
|
|
var curves = exports;
|
|
|
|
var hash = require('hash.js');
|
|
var curve = require('./curve');
|
|
var utils = require('./utils');
|
|
|
|
var assert = utils.assert;
|
|
|
|
function PresetCurve(options) {
|
|
if (options.type === 'short')
|
|
this.curve = new curve.short(options);
|
|
else if (options.type === 'edwards')
|
|
this.curve = new curve.edwards(options);
|
|
else
|
|
this.curve = new curve.mont(options);
|
|
this.g = this.curve.g;
|
|
this.n = this.curve.n;
|
|
this.hash = options.hash;
|
|
|
|
assert(this.g.validate(), 'Invalid curve');
|
|
assert(this.g.mul(this.n).isInfinity(), 'Invalid curve, G*N != O');
|
|
}
|
|
curves.PresetCurve = PresetCurve;
|
|
|
|
function defineCurve(name, options) {
|
|
Object.defineProperty(curves, name, {
|
|
configurable: true,
|
|
enumerable: true,
|
|
get: function() {
|
|
var curve = new PresetCurve(options);
|
|
Object.defineProperty(curves, name, {
|
|
configurable: true,
|
|
enumerable: true,
|
|
value: curve,
|
|
});
|
|
return curve;
|
|
},
|
|
});
|
|
}
|
|
|
|
defineCurve('p192', {
|
|
type: 'short',
|
|
prime: 'p192',
|
|
p: 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff',
|
|
a: 'ffffffff ffffffff ffffffff fffffffe ffffffff fffffffc',
|
|
b: '64210519 e59c80e7 0fa7e9ab 72243049 feb8deec c146b9b1',
|
|
n: 'ffffffff ffffffff ffffffff 99def836 146bc9b1 b4d22831',
|
|
hash: hash.sha256,
|
|
gRed: false,
|
|
g: [
|
|
'188da80e b03090f6 7cbf20eb 43a18800 f4ff0afd 82ff1012',
|
|
'07192b95 ffc8da78 631011ed 6b24cdd5 73f977a1 1e794811',
|
|
],
|
|
});
|
|
|
|
defineCurve('p224', {
|
|
type: 'short',
|
|
prime: 'p224',
|
|
p: 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001',
|
|
a: 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff fffffffe',
|
|
b: 'b4050a85 0c04b3ab f5413256 5044b0b7 d7bfd8ba 270b3943 2355ffb4',
|
|
n: 'ffffffff ffffffff ffffffff ffff16a2 e0b8f03e 13dd2945 5c5c2a3d',
|
|
hash: hash.sha256,
|
|
gRed: false,
|
|
g: [
|
|
'b70e0cbd 6bb4bf7f 321390b9 4a03c1d3 56c21122 343280d6 115c1d21',
|
|
'bd376388 b5f723fb 4c22dfe6 cd4375a0 5a074764 44d58199 85007e34',
|
|
],
|
|
});
|
|
|
|
defineCurve('p256', {
|
|
type: 'short',
|
|
prime: null,
|
|
p: 'ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff',
|
|
a: 'ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff fffffffc',
|
|
b: '5ac635d8 aa3a93e7 b3ebbd55 769886bc 651d06b0 cc53b0f6 3bce3c3e 27d2604b',
|
|
n: 'ffffffff 00000000 ffffffff ffffffff bce6faad a7179e84 f3b9cac2 fc632551',
|
|
hash: hash.sha256,
|
|
gRed: false,
|
|
g: [
|
|
'6b17d1f2 e12c4247 f8bce6e5 63a440f2 77037d81 2deb33a0 f4a13945 d898c296',
|
|
'4fe342e2 fe1a7f9b 8ee7eb4a 7c0f9e16 2bce3357 6b315ece cbb64068 37bf51f5',
|
|
],
|
|
});
|
|
|
|
defineCurve('p384', {
|
|
type: 'short',
|
|
prime: null,
|
|
p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
|
|
'fffffffe ffffffff 00000000 00000000 ffffffff',
|
|
a: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
|
|
'fffffffe ffffffff 00000000 00000000 fffffffc',
|
|
b: 'b3312fa7 e23ee7e4 988e056b e3f82d19 181d9c6e fe814112 0314088f ' +
|
|
'5013875a c656398d 8a2ed19d 2a85c8ed d3ec2aef',
|
|
n: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff c7634d81 ' +
|
|
'f4372ddf 581a0db2 48b0a77a ecec196a ccc52973',
|
|
hash: hash.sha384,
|
|
gRed: false,
|
|
g: [
|
|
'aa87ca22 be8b0537 8eb1c71e f320ad74 6e1d3b62 8ba79b98 59f741e0 82542a38 ' +
|
|
'5502f25d bf55296c 3a545e38 72760ab7',
|
|
'3617de4a 96262c6f 5d9e98bf 9292dc29 f8f41dbd 289a147c e9da3113 b5f0b8c0 ' +
|
|
'0a60b1ce 1d7e819d 7a431d7c 90ea0e5f',
|
|
],
|
|
});
|
|
|
|
defineCurve('p521', {
|
|
type: 'short',
|
|
prime: null,
|
|
p: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
|
|
'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
|
|
'ffffffff ffffffff ffffffff ffffffff ffffffff',
|
|
a: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
|
|
'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
|
|
'ffffffff ffffffff ffffffff ffffffff fffffffc',
|
|
b: '00000051 953eb961 8e1c9a1f 929a21a0 b68540ee a2da725b ' +
|
|
'99b315f3 b8b48991 8ef109e1 56193951 ec7e937b 1652c0bd ' +
|
|
'3bb1bf07 3573df88 3d2c34f1 ef451fd4 6b503f00',
|
|
n: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
|
|
'ffffffff ffffffff fffffffa 51868783 bf2f966b 7fcc0148 ' +
|
|
'f709a5d0 3bb5c9b8 899c47ae bb6fb71e 91386409',
|
|
hash: hash.sha512,
|
|
gRed: false,
|
|
g: [
|
|
'000000c6 858e06b7 0404e9cd 9e3ecb66 2395b442 9c648139 ' +
|
|
'053fb521 f828af60 6b4d3dba a14b5e77 efe75928 fe1dc127 ' +
|
|
'a2ffa8de 3348b3c1 856a429b f97e7e31 c2e5bd66',
|
|
'00000118 39296a78 9a3bc004 5c8a5fb4 2c7d1bd9 98f54449 ' +
|
|
'579b4468 17afbd17 273e662c 97ee7299 5ef42640 c550b901 ' +
|
|
'3fad0761 353c7086 a272c240 88be9476 9fd16650',
|
|
],
|
|
});
|
|
|
|
defineCurve('curve25519', {
|
|
type: 'mont',
|
|
prime: 'p25519',
|
|
p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
|
|
a: '76d06',
|
|
b: '1',
|
|
n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed',
|
|
hash: hash.sha256,
|
|
gRed: false,
|
|
g: [
|
|
'9',
|
|
],
|
|
});
|
|
|
|
defineCurve('ed25519', {
|
|
type: 'edwards',
|
|
prime: 'p25519',
|
|
p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
|
|
a: '-1',
|
|
c: '1',
|
|
// -121665 * (121666^(-1)) (mod P)
|
|
d: '52036cee2b6ffe73 8cc740797779e898 00700a4d4141d8ab 75eb4dca135978a3',
|
|
n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed',
|
|
hash: hash.sha256,
|
|
gRed: false,
|
|
g: [
|
|
'216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a',
|
|
|
|
// 4/5
|
|
'6666666666666666666666666666666666666666666666666666666666666658',
|
|
],
|
|
});
|
|
|
|
var pre;
|
|
try {
|
|
pre = require('./precomputed/secp256k1');
|
|
} catch (e) {
|
|
pre = undefined;
|
|
}
|
|
|
|
defineCurve('secp256k1', {
|
|
type: 'short',
|
|
prime: 'k256',
|
|
p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f',
|
|
a: '0',
|
|
b: '7',
|
|
n: 'ffffffff ffffffff ffffffff fffffffe baaedce6 af48a03b bfd25e8c d0364141',
|
|
h: '1',
|
|
hash: hash.sha256,
|
|
|
|
// Precomputed endomorphism
|
|
beta: '7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee',
|
|
lambda: '5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72',
|
|
basis: [
|
|
{
|
|
a: '3086d221a7d46bcde86c90e49284eb15',
|
|
b: '-e4437ed6010e88286f547fa90abfe4c3',
|
|
},
|
|
{
|
|
a: '114ca50f7a8e2f3f657c1108d9d44cfd8',
|
|
b: '3086d221a7d46bcde86c90e49284eb15',
|
|
},
|
|
],
|
|
|
|
gRed: false,
|
|
g: [
|
|
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
|
|
'483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
|
|
pre,
|
|
],
|
|
});
|