UNPKG

9.07 kBTypeScriptView Raw
1import BN = require("bn.js");
2
3// incomplete typings
4export const utils: any;
5export const rand: any;
6
7export type BNInput = string | BN | number | Buffer | Uint8Array | readonly number[];
8export type SignatureInput = ec.Signature | ec.SignatureOptions | Uint8Array | readonly number[] | string;
9
10export const version: number;
11
12export namespace curve {
13 /**
14 * @description Base class for the curves
15 */
16 class base {
17 p: BN;
18 type: string;
19 red: any; // ?
20 zero: BN;
21 one: BN;
22 two: BN;
23 n: BN;
24 g: base.BasePoint;
25 redN: BN;
26
27 constructor(type: string, conf: base.BaseCurveOptions);
28
29 validate(point: base.BasePoint): boolean;
30 decodePoint(bytes: Buffer | string, enc?: "hex"): base.BasePoint;
31 }
32
33 namespace base {
34 class BasePoint {
35 curve: base;
36 type: string;
37 precomputed: PrecomputedValues | null;
38
39 constructor(curve: base, type: string);
40
41 encode(enc: "hex", compact: boolean): string;
42 encode(enc: "array" | undefined, compact: boolean): number[];
43 encodeCompressed(enc: "hex"): string;
44 encodeCompressed(enc?: "array"): number[];
45 validate(): boolean;
46 precompute(power: number): BasePoint;
47 dblp(k: number): BasePoint;
48 inspect(): string;
49 isInfinity(): boolean;
50 add(p: BasePoint): BasePoint;
51 mul(k: BN): BasePoint;
52 dbl(): BasePoint;
53 getX(): BN;
54 getY(): BN;
55 eq(p: BasePoint): boolean;
56 neg(): BasePoint;
57 }
58
59 interface BaseCurveOptions {
60 p: number | string | number[] | Buffer | BN;
61 prime?: BN | string | undefined;
62 n?: number | BN | Buffer | undefined;
63 g?: BasePoint | undefined;
64 gRed?: any; // ?
65 }
66
67 interface PrecomputedValues {
68 doubles: any; // ?
69 naf: any; // ?
70 beta: any; // ?
71 }
72 }
73
74 class edwards extends base {
75 a: BN;
76 c: BN;
77 c2: BN;
78 d: BN;
79 dd: BN;
80
81 constructor(conf: edwards.EdwardsConf);
82
83 point(
84 x: BNInput,
85 y: BNInput,
86 z?: BNInput,
87 t?: BNInput,
88 ): edwards.EdwardsPoint;
89 pointFromX(x: BNInput, odd?: boolean): edwards.EdwardsPoint;
90 pointFromY(y: BNInput, odd?: boolean): edwards.EdwardsPoint;
91 pointFromJSON(obj: BNInput[]): edwards.EdwardsPoint;
92 }
93
94 namespace edwards {
95 interface EdwardsConf extends base.BaseCurveOptions {
96 a: BNInput;
97 c: BNInput;
98 d: BNInput;
99 }
100
101 class EdwardsPoint extends base.BasePoint {
102 x: BN;
103 y: BN;
104 z: BN;
105 t: BN;
106
107 normalize(): EdwardsPoint;
108 eqXToP(x: BN): boolean;
109 }
110 }
111
112 class short extends base {
113 a: BNInput;
114 b: BNInput;
115 g: short.ShortPoint;
116
117 constructor(conf: short.ShortConf);
118
119 point(x: BNInput, y: BNInput, isRed?: boolean): short.ShortPoint;
120 pointFromX(x: BNInput, odd?: boolean): short.ShortPoint;
121 pointFromJSON(obj: BNInput[], red: boolean): short.ShortPoint;
122 }
123
124 namespace short {
125 interface ShortConf extends base.BaseCurveOptions {
126 a: BNInput;
127 b: BNInput;
128 beta?: BNInput | undefined;
129 lambda?: BNInput | undefined;
130 }
131
132 class ShortPoint extends base.BasePoint {
133 x: BN | null;
134 y: BN | null;
135 inf: boolean;
136
137 toJSON(): BNInput[];
138 }
139 }
140}
141
142export namespace curves {
143 class PresetCurve {
144 type: string;
145 g: any; // ?
146 n: BN | undefined | null;
147 hash: any; // ?
148
149 constructor(options: PresetCurve.Options);
150 }
151
152 namespace PresetCurve {
153 interface Options {
154 type: string;
155 prime: string | null;
156 p: string;
157 a: string;
158 b: string;
159 n: string;
160 hash: any;
161 gRed: boolean;
162 g: any; // ?
163 beta?: string | undefined;
164 lambda?: string | undefined;
165 basis?: any; // ?
166 }
167 }
168}
169
170export class ec {
171 curve: any;
172 n: BN | undefined | null;
173 nh: any;
174 g: any;
175 hash: any;
176
177 constructor(options: string | curves.PresetCurve);
178
179 keyPair(options: ec.KeyPairOptions): ec.KeyPair;
180 keyFromPrivate(
181 priv: Uint8Array | Buffer | string | number[] | ec.KeyPair,
182 enc?: string,
183 ): ec.KeyPair;
184 keyFromPublic(
185 pub: Uint8Array | Buffer | string | number[] | { x: string; y: string } | ec.KeyPair,
186 enc?: string,
187 ): ec.KeyPair;
188 genKeyPair(options?: ec.GenKeyPairOptions): ec.KeyPair;
189 sign(
190 msg: BNInput,
191 key: Buffer | ec.KeyPair,
192 enc: string,
193 options?: ec.SignOptions,
194 ): ec.Signature;
195 sign(
196 msg: BNInput,
197 key: Buffer | ec.KeyPair,
198 options?: ec.SignOptions,
199 ): ec.Signature;
200 verify(
201 msg: BNInput,
202 signature: SignatureInput,
203 key: Buffer | ec.KeyPair,
204 enc?: string,
205 ): boolean;
206 recoverPubKey(
207 msg: BNInput,
208 signature: SignatureInput,
209 j: number,
210 enc?: string,
211 ): any;
212 getKeyRecoveryParam(
213 e: Error | undefined,
214 signature: SignatureInput,
215 Q: BN,
216 enc?: string,
217 ): number;
218}
219
220export namespace ec {
221 interface GenKeyPairOptions {
222 pers?: any;
223 entropy: any;
224 persEnc?: string | undefined;
225 entropyEnc?: string | undefined;
226 }
227
228 interface SignOptions {
229 pers?: any;
230 persEnc?: string | undefined;
231 canonical?: boolean | undefined;
232 k?: BN | undefined;
233 }
234
235 class KeyPair {
236 static fromPublic(
237 ec: ec,
238 pub: Buffer | string | { x: string; y: string } | KeyPair,
239 enc?: string,
240 ): KeyPair;
241 static fromPrivate(
242 ec: ec,
243 priv: Buffer | string | KeyPair,
244 enc?: string,
245 ): KeyPair;
246
247 ec: ec;
248
249 constructor(ec: ec, options: KeyPairOptions);
250
251 validate(): { readonly result: boolean; readonly reason: string };
252 getPublic(compact: boolean, enc: "hex"): string;
253 getPublic(compact: boolean, enc: "array"): number[];
254 getPublic(enc: "hex"): string;
255 getPublic(enc: "array"): number[];
256 getPublic(): curve.base.BasePoint;
257 getPrivate(enc: "hex"): string;
258 getPrivate(): BN;
259 derive(pub: curve.base.BasePoint): BN;
260 sign(msg: BNInput, enc: string, options?: SignOptions): Signature;
261 sign(msg: BNInput, options?: SignOptions): Signature;
262 verify(
263 msg: BNInput,
264 signature: SignatureInput,
265 ): boolean;
266 inspect(): string;
267 }
268
269 interface Signature {
270 r: BN;
271 s: BN;
272 recoveryParam: number | null;
273
274 toDER(): number[];
275 toDER(enc: "hex"): string;
276 }
277
278 interface SignatureOptions {
279 r: BNInput;
280 s: BNInput;
281 recoveryParam?: number | undefined;
282 }
283
284 interface KeyPairOptions {
285 priv?: Buffer | undefined;
286 privEnc?: string | undefined;
287 pub?: Buffer | undefined;
288 pubEnc?: string | undefined;
289 }
290}
291
292export class eddsa {
293 curve: curve.edwards;
294
295 constructor(name: "ed25519");
296
297 sign(message: eddsa.Bytes, secret: eddsa.Bytes): eddsa.Signature;
298 verify(
299 message: eddsa.Bytes,
300 sig: eddsa.Bytes | eddsa.Signature,
301 pub: eddsa.Bytes | eddsa.Point | eddsa.KeyPair,
302 ): boolean;
303 hashInt(): BN;
304 keyFromPublic(pub: eddsa.Bytes | eddsa.KeyPair | eddsa.Point): eddsa.KeyPair;
305 keyFromSecret(secret: eddsa.Bytes): eddsa.KeyPair;
306 makeSignature(sig: eddsa.Signature | Buffer | string): eddsa.Signature;
307 encodePoint(point: eddsa.Point): Buffer;
308 decodePoint(bytes: eddsa.Bytes): eddsa.Point;
309 encodeInt(num: BN): Buffer;
310 decodeInt(bytes: BNInput): BN;
311 isPoint(val: any): boolean;
312}
313
314export namespace eddsa {
315 type Point = curve.base.BasePoint;
316 type Bytes = string | Buffer;
317
318 class Signature {
319 constructor(eddsa: eddsa, sig: Signature | Bytes);
320
321 toBytes(): Buffer;
322 toHex(): string;
323 }
324
325 class KeyPair {
326 constructor(eddsa: eddsa, params: KeyPairOptions);
327
328 static fromPublic(eddsa: eddsa, pub: Bytes): KeyPair;
329 static fromSecret(eddsa: eddsa, secret: Bytes): KeyPair;
330
331 secret(): Buffer;
332 sign(message: Bytes): Signature;
333 verify(message: Bytes, sig: Signature | Bytes): boolean;
334 getSecret(enc: "hex"): string;
335 getSecret(): Buffer;
336 getPublic(enc: "hex"): string;
337 getPublic(): Buffer;
338 }
339
340 interface KeyPairOptions {
341 secret: Buffer;
342 pub: Buffer | Point;
343 }
344}