diff --git a/types/bn.js/bn.js-tests.ts b/types/bn.js/bn.js-tests.ts index a5d37dfdeb..738d5220d4 100644 --- a/types/bn.js/bn.js-tests.ts +++ b/types/bn.js/bn.js-tests.ts @@ -8,3 +8,11 @@ bn.byteLength; bn.toArrayLike(Buffer, 'le', 2); const test = new BN(1, 'le'); + +const ctx = BN.red('p224'); +ctx.prime.name; + +const red = bn.toRed(ctx); +const newRed = red.redAdd(new BN(1)); +newRed.cmp(bn); +newRed.fromRed(); diff --git a/types/bn.js/index.d.ts b/types/bn.js/index.d.ts index db4c9f8b9e..be04dbcc5f 100644 --- a/types/bn.js/index.d.ts +++ b/types/bn.js/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/indutny/bn.js // Definitions by: Leonid Logvinov // Henry Nguyen +// Gaylor Bosson // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// @@ -9,36 +10,16 @@ type Endianness = 'le' | 'be'; type IPrimeName = 'k256' | 'p224' | 'p192' | 'p25519'; -declare class RedBN { - redAdd(b: RedBN): RedBN; - redIAdd(b: RedBN): RedBN; - redSub(b: RedBN): RedBN; - redISub(b: RedBN): RedBN; - redShl(num: number): RedBN; - redMul(b: RedBN): RedBN; - redIMul(b: RedBN): RedBN; - redSqr(): RedBN; - redISqr(): RedBN; - /** - * @description square root modulo reduction context's prime - */ - redSqrt(): RedBN; - /** - * @description modular inverse of the number - */ - redInvm(): RedBN; - redNeg(): RedBN; - /** - * @description modular exponentiation - */ - redPow(b: RedBN): RedBN; - fromRed(): BN; +interface MPrime { + name: string; + p: BN; + n: number; + k: BN; } -// FIXME: not sure how to specify the reduction context here interface ReductionContext { m: number; - prime: any; + prime: MPrime; [key: string]: any; } @@ -78,11 +59,6 @@ declare class BN { */ static min(left: BN, right: BN): BN; - /** - * @description Convert number to red - */ - toRed(reductionContext: ReductionContext): RedBN; - /** * @description clone number */ @@ -522,6 +498,87 @@ declare class BN { * @description inverse `a` modulo `b` */ invm(b: BN): BN; + + /** + * @description Convert number to red + */ + toRed(reductionContext: ReductionContext): RedBN; +} + +/** + * Big-Number class with additionnal methods that are using modular + * operation. + */ +declare class RedBN extends BN { + /** + * @description Convert back a number using a reduction context + */ + fromRed(): BN; + + /** + * @description modular addition + */ + redAdd(b: BN): RedBN; + + /** + * @description in-place modular addition + */ + redIAdd(b: BN): RedBN; + + /** + * @description modular subtraction + */ + redSub(b: BN): RedBN; + + /** + * @description in-place modular subtraction + */ + redISub(b: BN): RedBN; + + /** + * @description modular shift left + */ + redShl(num: number): RedBN; + + /** + * @description modular multiplication + */ + redMul(b: BN): RedBN; + + /** + * @description in-place modular multiplication + */ + redIMul(b: BN): RedBN; + + /** + * @description modular square + */ + redSqr(): RedBN; + + /** + * @description in-place modular square + */ + redISqr(): RedBN; + + /** + * @description modular square root + */ + redSqrt(): RedBN; + + /** + * @description modular inverse of the number + */ + redInvm(): RedBN; + + /** + * @description modular negation + */ + redNeg(): RedBN; + + /** + * @description modular exponentiation + */ + redPow(b: BN): RedBN; } export = BN;