Merge pull request #32197 from Gilthoniel/improve_elliptic

update(elliptic): add some missing definitions
This commit is contained in:
Daniel Rosenwasser
2019-01-18 17:01:41 -08:00
committed by GitHub
2 changed files with 305 additions and 122 deletions
+47
View File
@@ -1,4 +1,5 @@
import elliptic = require('elliptic');
import BN = require('bn.js');
const ec = new elliptic.ec('secp256k1');
@@ -45,3 +46,49 @@ const newKey = ec.keyFromPublic(pub, 'hex');
// 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();
+258 -122
View File
@@ -1,6 +1,7 @@
// Type definitions for elliptic 6.4
// Project: https://github.com/indutny/elliptic
// Definitions by: Daniel Byrne <https://github.com/danwbyrne>
// Gaylor Bosson <https://github.com/Gilthoniel>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import BN = require('bn.js');
@@ -8,151 +9,286 @@ import BN = require('bn.js');
// incomplete typings
export const utils: any;
export const rand: any;
export const eddsa: any;
export type BNInput = string | BN | number | Buffer | number[];
export const version: number;
export namespace curve {
class BaseCurve {
constructor(type: string, conf: BaseCurve.BaseCurveOptions)
p: BN;
type: string;
red: any; // ?
zero: any; // ?
one: any; // ?
two: any; // ?
n: BN | undefined | null;
g: any; // ?
redN: any; // ?
decodePoint(bytes: any, enc: string): any; // ?
}
/**
* @description Base class for the curves
*/
class base {
p: BN;
type: string;
red: any; // ?
zero: BN;
one: BN;
two: BN;
n: BN;
g: base.BasePoint;
redN: BN;
namespace BaseCurve {
class BasePoint {
constructor(curve: any, type: string);
curve: any;
type: string;
precomputed: PrecomputedValues | null;
encode(enc: string, compact: boolean): BN;
encodeCompressed(enc: string): BN;
validate(): boolean;
precompute(power: number): BasePoint;
dblp(k: any): any; // ?
}
constructor(type: string, conf: base.BaseCurveOptions)
interface BaseCurveOptions {
p: number | string | number[] | Buffer | BN;
prime?: BN | string;
n?: number | BN | Buffer;
g?: any; // ?
gRed?: any; // ?
}
validate(point: base.BasePoint): boolean;
decodePoint(bytes: Buffer | string, enc?: 'hex'): base.BasePoint;
}
interface PrecomputedValues {
doubles: any; // ?
naf: any; // ?
beta: any; // ?
}
}
namespace base {
class BasePoint {
curve: base;
type: string;
precomputed: PrecomputedValues | null;
constructor(curve: base, type: string);
encode(enc: string, compact: boolean): string | Buffer;
encodeCompressed(enc: string): BN;
validate(): boolean;
precompute(power: number): BasePoint;
dblp(k: number): BasePoint;
inspect(): string;
isInfinity(): boolean;
add(p: BasePoint): BasePoint;
mul(k: BN): BasePoint;
dbl(): BasePoint;
getX(): BN;
getY(): BN;
eq(p: BasePoint): boolean;
neg(): BasePoint;
}
interface BaseCurveOptions {
p: number | string | number[] | Buffer | BN;
prime?: BN | string;
n?: number | BN | Buffer;
g?: BasePoint;
gRed?: any; // ?
}
interface PrecomputedValues {
doubles: any; // ?
naf: any; // ?
beta: any; // ?
}
}
class edwards extends base {
a: BN;
c: BN;
c2: BN;
d: BN;
dd: BN;
constructor(conf: edwards.EdwardsConf);
point(x: BNInput, y: BNInput, z?: BNInput, t?: BNInput): edwards.EdwardsPoint;
pointFromX(x: BNInput, odd?: boolean): edwards.EdwardsPoint;
pointFromY(y: BNInput, odd?: boolean): edwards.EdwardsPoint;
pointFromJSON(obj: BNInput[]): edwards.EdwardsPoint;
}
namespace edwards {
interface EdwardsConf extends base.BaseCurveOptions {
a: BNInput;
c: BNInput;
d: BNInput;
}
class EdwardsPoint extends base.BasePoint {
x: BN;
y: BN;
z: BN;
t: BN;
normalize(): EdwardsPoint;
eqXToP(x: BN): boolean;
}
}
class short extends base {
a: BNInput;
b: BNInput;
g: short.ShortPoint;
constructor(conf: short.ShortConf);
point(x: BNInput, y: BNInput, isRed?: boolean): short.ShortPoint;
pointFromX(x: BNInput, odd?: boolean): short.ShortPoint;
pointFromJSON(obj: BNInput[], red: boolean): short.ShortPoint;
}
namespace short {
interface ShortConf extends base.BaseCurveOptions {
a: BNInput;
b: BNInput;
beta?: BNInput;
lambda?: BNInput;
}
class ShortPoint extends base.BasePoint {
x: BN;
y: BN;
inf: boolean;
toJSON(): BNInput[];
}
}
}
export namespace curves {
class PresetCurve {
constructor(options: PresetCurve.Options)
type: string;
g: any; // ?
n: BN | undefined | null;
hash: any; // ?
}
class PresetCurve {
type: string;
g: any; // ?
n: BN | undefined | null;
hash: any; // ?
namespace PresetCurve {
interface Options {
type: string;
prime: string | null;
p: string;
a: string;
b: string;
n: string;
hash: any;
gRed: boolean;
g: any; // ?
beta?: string;
lambda?: string;
basis?: any; // ?
}
}
constructor(options: PresetCurve.Options)
}
namespace PresetCurve {
interface Options {
type: string;
prime: string | null;
p: string;
a: string;
b: string;
n: string;
hash: any;
gRed: boolean;
g: any; // ?
beta?: string;
lambda?: string;
basis?: any; // ?
}
}
}
export class ec {
constructor(options: string | curves.PresetCurve)
curve: any;
n: BN | undefined | null;
nh: any;
g: any;
hash: any;
curve: any;
n: BN | undefined | null;
nh: any;
g: any;
hash: any;
keyPair(options: ec.KeyPairOptions): ec.KeyPair;
keyFromPrivate(priv: Buffer | ec.KeyPair, enc?: string): ec.KeyPair;
keyFromPublic(pub: Buffer | ec.KeyPair, enc?: string): ec.KeyPair;
genKeyPair(options?: ec.GenKeyPairOptions): ec.KeyPair;
sign(msg: BNInput, key: Buffer | ec.KeyPair, enc: string, options?: ec.SignOptions): ec.Signature;
sign(msg: BNInput, key: Buffer | ec.KeyPair, options?: ec.SignOptions): ec.Signature;
verify(msg: BNInput, signature: ec.Signature | ec.SignatureOptions, key: Buffer | ec.KeyPair, enc?: string): boolean;
recoverPubKey(msg: BNInput, signature: ec.Signature | ec.SignatureOptions, j: number, enc?: string): any;
getKeyRecoveryParam(e: Error | undefined, signature: ec.Signature | ec.SignatureOptions, Q: BN, enc?: string): number;
constructor(options: string | curves.PresetCurve)
keyPair(options: ec.KeyPairOptions): ec.KeyPair;
keyFromPrivate(priv: Buffer | ec.KeyPair, enc?: string): ec.KeyPair;
keyFromPublic(pub: Buffer | ec.KeyPair, enc?: string): ec.KeyPair;
genKeyPair(options?: ec.GenKeyPairOptions): ec.KeyPair;
sign(msg: BNInput, key: Buffer | ec.KeyPair, enc: string, options?: ec.SignOptions): ec.Signature;
sign(msg: BNInput, key: Buffer | ec.KeyPair, options?: ec.SignOptions): ec.Signature;
verify(msg: BNInput, signature: ec.Signature | ec.SignatureOptions, key: Buffer | ec.KeyPair, enc?: string): boolean;
recoverPubKey(msg: BNInput, signature: ec.Signature | ec.SignatureOptions, j: number, enc?: string): any;
getKeyRecoveryParam(e: Error | undefined, signature: ec.Signature | ec.SignatureOptions, Q: BN, enc?: string): number;
}
export namespace ec {
interface GenKeyPairOptions {
pers: any;
entropy: any;
persEnc?: string;
entropyEnc?: string;
}
interface GenKeyPairOptions {
pers: any;
entropy: any;
persEnc?: string;
entropyEnc?: string;
}
interface SignOptions {
pers: any;
persEnc?: string;
canonical?: boolean;
k?: BN;
}
interface SignOptions {
pers: any;
persEnc?: string;
canonical?: boolean;
k?: BN;
}
class KeyPair {
static fromPublic(ec: ec, pub: Buffer | KeyPair, enc?: string): KeyPair;
static fromPrivate(ec: ec, priv: Buffer | KeyPair, enc?: string): KeyPair;
constructor(ec: ec, options: KeyPairOptions)
ec: ec;
validate(): { readonly result: boolean, readonly reason: string };
getPublic(compact: boolean, enc?: string): any; // ?
getPublic(enc?: string): any; // ?
getPrivate(enc?: 'hex'): Buffer | BN | string;
derive(pub: any): any; // ?
sign(msg: BNInput, enc: string, options?: SignOptions): Signature;
sign(msg: BNInput, options?: SignOptions): Signature;
verify(msg: BNInput, signature: Signature | SignatureOptions): boolean;
inspect(): string;
}
class KeyPair {
static fromPublic(ec: ec, pub: Buffer | KeyPair, enc?: string): KeyPair;
static fromPrivate(ec: ec, priv: Buffer | KeyPair, enc?: string): KeyPair;
class Signature {
constructor(options: SignatureOptions | Signature, enc?: string)
r: BN;
s: BN;
recoveryParam: number | null;
toDER(enc?: string | null): any; // ?
}
ec: ec;
interface SignatureOptions {
r: BNInput;
s: BNInput;
recoveryParam?: number;
}
constructor(ec: ec, options: KeyPairOptions)
interface KeyPairOptions {
priv?: Buffer;
privEnc?: string;
pub?: Buffer;
pubEnc?: string;
}
validate(): { readonly result: boolean, readonly reason: string };
getPublic(compact: boolean, enc?: string): any; // ?
getPublic(enc?: string): any; // ?
getPrivate(enc?: 'hex'): Buffer | BN | string;
derive(pub: any): any; // ?
sign(msg: BNInput, enc: string, options?: SignOptions): Signature;
sign(msg: BNInput, options?: SignOptions): Signature;
verify(msg: BNInput, signature: Signature | SignatureOptions): boolean;
inspect(): string;
}
class Signature {
r: BN;
s: BN;
recoveryParam: number | null;
constructor(options: SignatureOptions | Signature, enc?: string)
toDER(enc?: string | null): any; // ?
}
interface SignatureOptions {
r: BNInput;
s: BNInput;
recoveryParam?: number;
}
interface KeyPairOptions {
priv?: Buffer;
privEnc?: string;
pub?: Buffer;
pubEnc?: string;
}
}
export class eddsa {
curve: curve.edwards;
constructor(name: 'ed25519');
sign(message: eddsa.Bytes, secret: eddsa.Bytes): eddsa.Signature;
verify(message: eddsa.Bytes, sig: eddsa.Bytes | eddsa.Signature, pub: eddsa.Bytes | eddsa.Point | eddsa.KeyPair): boolean;
hashInt(): BN;
keyFromPublic(pub: eddsa.Bytes): eddsa.KeyPair;
keyFromSecret(secret: eddsa.Bytes): eddsa.KeyPair;
makeSignature(sig: eddsa.Signature | Buffer | string): eddsa.Signature;
encodePoint(point: eddsa.Point): Buffer;
decodePoint(bytes: eddsa.Bytes): eddsa.Point;
encodeInt(num: BN): Buffer;
decodeInt(bytes: BNInput): BN;
isPoint(val: any): boolean;
}
export namespace eddsa {
type Point = curve.base.BasePoint;
type Bytes = string | Buffer;
class Signature {
constructor(eddsa: eddsa, sig: Signature | Bytes);
toBytes(): Buffer;
toHex(): string;
}
class KeyPair {
constructor(eddsa: eddsa, params: KeyPairOptions);
static fromPublic(eddsa: eddsa, pub: Bytes): KeyPair;
static fromSecret(eddsa: eddsa, secret: Bytes): KeyPair;
secret(): Buffer;
sign(message: Bytes): Signature;
verify(message: Bytes, sig: Signature | Bytes): boolean;
getSecret(enc: 'hex'): string;
getSecret(): Buffer;
getPublic(enc: 'hex'): string;
getPublic(): Buffer;
}
interface KeyPairOptions {
secret: Buffer;
pub: Buffer | Point;
}
}