DefinitelyTyped/types/elliptic/elliptic-tests.ts
Yuki Akiyama 66b1826174 [elliptic] fix: overload encode and encodeCompressed (#38353)
Squashed commit of the following:

commit 9a4d09cdb90ddb90236715affbcde58df133756b
Author: yuki akiyama <you2197901@gmail.com>
Date:   Fri Sep 13 15:38:55 2019 +0900

    update

commit 47e917b16e686735de9e62fc89501f17c5ece19f
Author: yuki akiyama <you2197901@gmail.com>
Date:   Fri Sep 13 15:12:04 2019 +0900

    number[]

commit 27fd66ef728bb595488ad6f48af5d89aec25ee89
Author: yuki akiyama <you2197901@gmail.com>
Date:   Fri Sep 13 15:00:46 2019 +0900

    add test logic

commit 7fd1eb4fe1fb180d6bbc37fa3a8eb689e54ba96c
Author: yuki akiyama <you2197901@gmail.com>
Date:   Fri Sep 13 14:44:12 2019 +0900

    fix: lint advise

commit 485dffc2ddef734f2ee209de7709b2b681b73b34
Author: yuki akiyama <you2197901@gmail.com>
Date:   Fri Sep 13 13:33:10 2019 +0900

    fix: overload encode and encodeCompressed
2019-09-19 11:58:11 -07:00

100 lines
3.0 KiB
TypeScript

import elliptic = require('elliptic');
import BN = require('bn.js');
const ec = new elliptic.ec('secp256k1');
// Generate keys
const key = ec.genKeyPair();
// Sign the message's hash (input must be an array, or a hex-string)
const msgHash = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const signature = key.sign(msgHash);
// Export DER encoded signature in Array
const derSign = signature.toDER();
// Verify signature
console.log(key.verify(msgHash, derSign));
// CHECK WITH NO PRIVATE KEY
const pubPoint = key.getPublic();
const x = pubPoint.getX();
const y = pubPoint.getY();
// Public Key MUST be either:
// 1) '04' + hex string of x + hex string of y; or
// 2) object with two hex string properties (x and y); or
// 3) object with two buffer properties (x and y)
const pub = pubPoint.encode('hex', true); // case 1
const aPub = { x: x.toString('hex'), y: y.toString('hex') }; // case 2
const bPub = { x: x.toBuffer(), y: y.toBuffer() }; // case 3
const cPub = { x: x.toArrayLike(Buffer), y: y.toArrayLike(Buffer) }; // case 3
// Import public key
const newKey = ec.keyFromPublic(pub, 'hex');
// Import public key from array
const pubArray = pubPoint.encode('array', true);
const newKeyFromArray = ec.keyFromPublic(pubArray);
console.log(pub === newKeyFromArray.getPublic().encodeCompressed('hex'));
// Signature MUST be either:
// 1) DER-encoded signature as hex-string; or
// 2) DER-encoded signature as buffer; or
// 3) object with two hex-string properties (r and s); or
// 4) object with two buffer properties (r and s)
// const signature = '3046022100...'; // case 1
// const signature = new Buffer('...'); // case 2
// const signature = { r: 'b1fc...', s: '9c42...' }; // case 3
// Verify signature
console.log(key.verify(msgHash, signature));
// EDDSA tests
const eddsa = new elliptic.eddsa('ed25519');
const msg = Buffer.from('dead', 'hex');
const priv = 'deadbeef';
const sig = eddsa.sign(msg, priv);
sig.toHex();
sig.toBytes();
eddsa.sign(msg.toString('hex'), priv).toHex();
const edkey = eddsa.keyFromSecret(priv);
edkey.verify(msg, sig);
edkey.verify(msg.toString('hex'), sig.toBytes());
const edkey2 = eddsa.keyFromPublic(key.getPublic());
edkey2.verify(msg, sig);
eddsa.verify(msg, sig, edkey2.getPublic('hex'));
eddsa.verify(msg.toString('hex'), sig.toBytes(), edkey2.getPublic());
eddsa.verify(msg, sig.toHex(), edkey2.getPublic());
// Curves Tests
elliptic.curve.base.BasePoint;
const c = new elliptic.curve.edwards({
p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
a: -1,
c: 1,
d: '52036cee2b6ffe73 8cc740797779e898 00700a4d4141d8ab 75eb4dca135978a3',
});
const p = c.pointFromX(5555);
p.eq(p);
eddsa.isPoint(p);
c.validate(p.add(p).mul(new BN(3)).dbl());
const sc = new elliptic.curve.short({
a: 1,
b: 0,
p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
});
const p2 = sc.pointFromX(123456789);
sc.validate(p2.add(p2).mul(new BN(5)).dbl());
sc.pointFromJSON(p2.toJSON(), false).toJSON();