fix(bn.js): define RedBN as an extension of BN

Previous definition was considering this as a totally different class
but it is in fact the same class with extended functionality.
This commit is contained in:
Gaylor Bosson 2019-01-14 11:57:45 +01:00
parent 3a69de74e2
commit 0d06d2e220
2 changed files with 96 additions and 31 deletions

View File

@ -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();

119
types/bn.js/index.d.ts vendored
View File

@ -2,6 +2,7 @@
// Project: https://github.com/indutny/bn.js
// Definitions by: Leonid Logvinov <https://github.com/LogvinovLeon>
// Henry Nguyen <https://github.com/HenryNguyen5>
// Gaylor Bosson <https://github.com/Gilthoniel>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node"/>
@ -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;