UNPKG

10.9 kBTypeScriptView Raw
1/// <reference types="node"/>
2
3declare namespace BN {
4 type Endianness = "le" | "be";
5 type IPrimeName = "k256" | "p224" | "p192" | "p25519";
6
7 interface MPrime {
8 name: string;
9 p: BN;
10 n: number;
11 k: BN;
12 }
13
14 interface ReductionContext {
15 m: number;
16 prime: MPrime;
17 [key: string]: any;
18 }
19}
20
21declare class BN {
22 static BN: typeof BN;
23 static wordSize: 26;
24
25 constructor(
26 number: number | string | number[] | Uint8Array | Buffer | BN,
27 base?: number | "hex",
28 endian?: BN.Endianness,
29 );
30 constructor(
31 number: number | string | number[] | Uint8Array | Buffer | BN,
32 endian?: BN.Endianness,
33 );
34
35 /**
36 * @description create a reduction context
37 */
38 static red(reductionContext: BN | BN.IPrimeName): BN.ReductionContext;
39
40 /**
41 * @description create a reduction context with the Montgomery trick.
42 */
43 static mont(num: BN): BN.ReductionContext;
44
45 /**
46 * @description returns true if the supplied object is a BN.js instance
47 */
48 static isBN(b: any): b is BN;
49
50 /**
51 * @description returns the maximum of 2 BN instances.
52 */
53 static max(left: BN, right: BN): BN;
54
55 /**
56 * @description returns the minimum of 2 BN instances.
57 */
58 static min(left: BN, right: BN): BN;
59
60 /**
61 * @description clone number
62 */
63 clone(): BN;
64
65 /**
66 * @description convert to base-string and pad with zeroes
67 */
68 toString(base?: number | "hex", length?: number): string;
69
70 /**
71 * @description convert to Javascript Number (limited to 53 bits)
72 */
73 toNumber(): number;
74
75 /**
76 * @description convert to JSON compatible hex string (alias of toString(16))
77 */
78 toJSON(): string;
79
80 /**
81 * @description convert to byte Array, and optionally zero pad to length, throwing if already exceeding
82 */
83 toArray(endian?: BN.Endianness, length?: number): number[];
84
85 /**
86 * @description convert to an instance of `type`, which must behave like an Array
87 */
88 toArrayLike(
89 ArrayType: typeof Buffer,
90 endian?: BN.Endianness,
91 length?: number,
92 ): Buffer;
93
94 toArrayLike(
95 ArrayType: any[],
96 endian?: BN.Endianness,
97 length?: number,
98 ): any[];
99
100 /**
101 * @description convert to Node.js Buffer (if available). For compatibility with browserify and similar tools, use this instead: a.toArrayLike(Buffer, endian, length)
102 */
103 toBuffer(endian?: BN.Endianness, length?: number): Buffer;
104
105 /**
106 * @description get number of bits occupied
107 */
108 bitLength(): number;
109
110 /**
111 * @description return number of less-significant consequent zero bits (example: 1010000 has 4 zero bits)
112 */
113 zeroBits(): number;
114
115 /**
116 * @description return number of bytes occupied
117 */
118 byteLength(): number;
119
120 /**
121 * @description true if the number is negative
122 */
123 isNeg(): boolean;
124
125 /**
126 * @description check if value is even
127 */
128 isEven(): boolean;
129
130 /**
131 * @description check if value is odd
132 */
133 isOdd(): boolean;
134
135 /**
136 * @description check if value is zero
137 */
138 isZero(): boolean;
139
140 /**
141 * @description compare numbers and return `-1 (a < b)`, `0 (a == b)`, or `1 (a > b)` depending on the comparison result
142 */
143 cmp(b: BN): -1 | 0 | 1;
144
145 /**
146 * @description compare numbers and return `-1 (a < b)`, `0 (a == b)`, or `1 (a > b)` depending on the comparison result
147 */
148 ucmp(b: BN): -1 | 0 | 1;
149
150 /**
151 * @description compare numbers and return `-1 (a < b)`, `0 (a == b)`, or `1 (a > b)` depending on the comparison result
152 */
153 cmpn(b: number): -1 | 0 | 1;
154
155 /**
156 * @description a less than b
157 */
158 lt(b: BN): boolean;
159
160 /**
161 * @description a less than b
162 */
163 ltn(b: number): boolean;
164
165 /**
166 * @description a less than or equals b
167 */
168 lte(b: BN): boolean;
169
170 /**
171 * @description a less than or equals b
172 */
173 lten(b: number): boolean;
174
175 /**
176 * @description a greater than b
177 */
178 gt(b: BN): boolean;
179
180 /**
181 * @description a greater than b
182 */
183 gtn(b: number): boolean;
184
185 /**
186 * @description a greater than or equals b
187 */
188 gte(b: BN): boolean;
189
190 /**
191 * @description a greater than or equals b
192 */
193 gten(b: number): boolean;
194
195 /**
196 * @description a equals b
197 */
198 eq(b: BN): boolean;
199
200 /**
201 * @description a equals b
202 */
203 eqn(b: number): boolean;
204
205 /**
206 * @description convert to two's complement representation, where width is bit width
207 */
208 toTwos(width: number): BN;
209
210 /**
211 * @description convert from two's complement representation, where width is the bit width
212 */
213 fromTwos(width: number): BN;
214
215 /**
216 * @description negate sign
217 */
218 neg(): BN;
219
220 /**
221 * @description negate sign
222 */
223 ineg(): BN;
224
225 /**
226 * @description absolute value
227 */
228 abs(): BN;
229
230 /**
231 * @description absolute value
232 */
233 iabs(): BN;
234
235 /**
236 * @description addition
237 */
238 add(b: BN): BN;
239
240 /**
241 * @description addition
242 */
243 iadd(b: BN): BN;
244
245 /**
246 * @description addition
247 */
248 addn(b: number): BN;
249
250 /**
251 * @description addition
252 */
253 iaddn(b: number): BN;
254
255 /**
256 * @description subtraction
257 */
258 sub(b: BN): BN;
259
260 /**
261 * @description subtraction
262 */
263 isub(b: BN): BN;
264
265 /**
266 * @description subtraction
267 */
268 subn(b: number): BN;
269
270 /**
271 * @description subtraction
272 */
273 isubn(b: number): BN;
274
275 /**
276 * @description multiply
277 */
278 mul(b: BN): BN;
279
280 /**
281 * @description multiply
282 */
283 imul(b: BN): BN;
284
285 /**
286 * @description multiply
287 */
288 muln(b: number): BN;
289
290 /**
291 * @description multiply
292 */
293 imuln(b: number): BN;
294
295 /**
296 * @description square
297 */
298 sqr(): BN;
299
300 /**
301 * @description square
302 */
303 isqr(): BN;
304
305 /**
306 * @description raise `a` to the power of `b`
307 */
308 pow(b: BN): BN;
309
310 /**
311 * @description divide
312 */
313 div(b: BN): BN;
314
315 /**
316 * @description divide
317 */
318 divn(b: number): BN;
319
320 /**
321 * @description divide
322 */
323 idivn(b: number): BN;
324
325 /**
326 * @description division with remainder
327 */
328 divmod(b: BN, mode?: "div" | "mod", positive?: boolean): { div: BN; mod: BN };
329
330 /**
331 * @description reduct
332 */
333 mod(b: BN): BN;
334
335 /**
336 * @description reduct
337 */
338 umod(b: BN): BN;
339
340 /**
341 * @deprecated
342 * @description reduct
343 */
344 modn(b: number): number;
345
346 /**
347 * @description reduct
348 */
349 modrn(b: number): number;
350
351 /**
352 * @description rounded division
353 */
354 divRound(b: BN): BN;
355
356 /**
357 * @description or
358 */
359 or(b: BN): BN;
360
361 /**
362 * @description or
363 */
364 ior(b: BN): BN;
365
366 /**
367 * @description or
368 */
369 uor(b: BN): BN;
370
371 /**
372 * @description or
373 */
374 iuor(b: BN): BN;
375
376 /**
377 * @description and
378 */
379 and(b: BN): BN;
380
381 /**
382 * @description and
383 */
384 iand(b: BN): BN;
385
386 /**
387 * @description and
388 */
389 uand(b: BN): BN;
390
391 /**
392 * @description and
393 */
394 iuand(b: BN): BN;
395
396 /**
397 * @description and (NOTE: `andln` is going to be replaced with `andn` in future)
398 */
399 andln(b: number): BN;
400
401 /**
402 * @description xor
403 */
404 xor(b: BN): BN;
405
406 /**
407 * @description xor
408 */
409 ixor(b: BN): BN;
410
411 /**
412 * @description xor
413 */
414 uxor(b: BN): BN;
415
416 /**
417 * @description xor
418 */
419 iuxor(b: BN): BN;
420
421 /**
422 * @description set specified bit to value
423 */
424 setn(b: number, value: boolean | 0 | 1): BN;
425
426 /**
427 * @description shift left
428 */
429 shln(b: number): BN;
430
431 /**
432 * @description shift left
433 */
434 ishln(b: number): BN;
435
436 /**
437 * @description shift left
438 */
439 ushln(b: number): BN;
440
441 /**
442 * @description shift left
443 */
444 iushln(b: number): BN;
445
446 /**
447 * @description shift right
448 */
449 shrn(b: number): BN;
450
451 /**
452 * @description shift right (unimplemented https://github.com/indutny/bn.js/blob/master/lib/bn.js#L2086)
453 */
454 ishrn(b: number): BN;
455
456 /**
457 * @description shift right
458 */
459 ushrn(b: number): BN;
460 /**
461 * @description shift right
462 */
463
464 iushrn(b: number): BN;
465 /**
466 * @description test if specified bit is set
467 */
468
469 testn(b: number): boolean;
470 /**
471 * @description clear bits with indexes higher or equal to `b`
472 */
473
474 maskn(b: number): BN;
475 /**
476 * @description clear bits with indexes higher or equal to `b`
477 */
478
479 imaskn(b: number): BN;
480 /**
481 * @description add `1 << b` to the number
482 */
483 bincn(b: number): BN;
484
485 /**
486 * @description not (for the width specified by `w`)
487 */
488 notn(w: number): BN;
489
490 /**
491 * @description not (for the width specified by `w`)
492 */
493 inotn(w: number): BN;
494
495 /**
496 * @description GCD
497 */
498 gcd(b: BN): BN;
499
500 /**
501 * @description Extended GCD results `({ a: ..., b: ..., gcd: ... })`
502 */
503 egcd(b: BN): { a: BN; b: BN; gcd: BN };
504
505 /**
506 * @description inverse `a` modulo `b`
507 */
508 invm(b: BN): BN;
509
510 /**
511 * @description Convert number to red
512 */
513 toRed(reductionContext: BN.ReductionContext): RedBN;
514}
515
516/**
517 * BN operations in a reduction context.
518 */
519declare class RedBN extends BN {
520 /**
521 * @description Convert back a number using a reduction context
522 */
523 fromRed(): BN;
524
525 /**
526 * @description modular addition
527 */
528 redAdd(b: RedBN): RedBN;
529
530 /**
531 * @description in-place modular addition
532 */
533 redIAdd(b: RedBN): RedBN;
534
535 /**
536 * @description modular subtraction
537 */
538 redSub(b: RedBN): RedBN;
539
540 /**
541 * @description in-place modular subtraction
542 */
543 redISub(b: RedBN): RedBN;
544
545 /**
546 * @description modular shift left
547 */
548 redShl(num: number): RedBN;
549
550 /**
551 * @description modular multiplication
552 */
553 redMul(b: RedBN): RedBN;
554
555 /**
556 * @description in-place modular multiplication
557 */
558 redIMul(b: RedBN): RedBN;
559
560 /**
561 * @description modular square
562 */
563 redSqr(): RedBN;
564
565 /**
566 * @description in-place modular square
567 */
568 redISqr(): RedBN;
569
570 /**
571 * @description modular square root
572 */
573 redSqrt(): RedBN;
574
575 /**
576 * @description modular inverse of the number
577 */
578 redInvm(): RedBN;
579
580 /**
581 * @description modular negation
582 */
583 redNeg(): RedBN;
584
585 /**
586 * @description modular exponentiation
587 */
588 redPow(b: BN): RedBN;
589}
590
591export = BN;
592
\No newline at end of file