Add type definitions for bignum

This commit is contained in:
Patman64
2015-11-11 22:34:58 -05:00
parent 708609e076
commit 3a0122e166
2 changed files with 517 additions and 0 deletions
+248
View File
@@ -0,0 +1,248 @@
/// <reference path="bignum.d.ts" />
var bignum = require('bignum');
// Test constructors.
var instance = bignum(16);
bignum('16');
bignum(instance);
// Test `toNumber` function.
instance.toNumber();
bignum.toNumber(4);
bignum.toNumber('4');
bignum.toNumber(bignum(4));
// Test `toBuffer` function.
instance.toBuffer();
bignum.toBuffer(4);
bignum.toBuffer('4');
bignum.toBuffer(bignum(4));
// Test `add` function.
instance.add(4);
instance.add('4');
instance.add(bignum(4));
bignum.add(instance, 4);
bignum.add(instance, '4');
bignum.add(instance, bignum(4));
// Test `sub` function.
instance.sub(4);
instance.sub('4');
instance.sub(bignum(4));
bignum.sub(instance, 4);
bignum.sub(instance, '4');
bignum.sub(instance, bignum(4));
// Test `mul` function.
instance.mul(4);
instance.mul('4');
instance.mul(bignum(4));
bignum.mul(instance, 4);
bignum.mul(instance, '4');
bignum.mul(instance, bignum(4));
// Test `div` function.
instance.div(4);
instance.div('4');
instance.div(bignum(4));
bignum.div(instance, 4);
bignum.div(instance, '4');
bignum.div(instance, bignum(4));
// Test `abs` function.
instance.abs();
bignum.abs(instance);
// Test `neg` function.
instance.neg();
bignum.neg(instance);
// Test `cmp` function.
instance.cmp(4);
instance.cmp('4');
instance.cmp(bignum(4));
bignum.cmp(instance, 4);
bignum.cmp(instance, '4');
bignum.cmp(instance, bignum(4));
// Test `gt` function.
instance.gt(4);
instance.gt('4');
instance.gt(bignum(4));
bignum.gt(instance, 4);
bignum.gt(instance, '4');
bignum.gt(instance, bignum(4));
// Test `ge` function.
instance.ge(4);
instance.ge('4');
instance.ge(bignum(4));
bignum.ge(instance, 4);
bignum.ge(instance, '4');
bignum.ge(instance, bignum(4));
// Test `eq` function.
instance.eq(4);
instance.eq('4');
instance.eq(bignum(4));
bignum.eq(instance, 4);
bignum.eq(instance, '4');
bignum.eq(instance, bignum(4));
// Test `lt` function.
instance.lt(4);
instance.lt('4');
instance.lt(bignum(4));
bignum.lt(instance, 4);
bignum.lt(instance, '4');
bignum.lt(instance, bignum(4));
// Test `le` function.
instance.le(4);
instance.le('4');
instance.le(bignum(4));
bignum.le(instance, 4);
bignum.le(instance, '4');
bignum.le(instance, bignum(4));
// Test `and` function.
instance.and(4);
instance.and('4');
instance.and(bignum(4));
bignum.and(instance, 4);
bignum.and(instance, '4');
bignum.and(instance, bignum(4));
// Test `or` function.
instance.or(4);
instance.or('4');
instance.or(bignum(4));
bignum.or(instance, 4);
bignum.or(instance, '4');
bignum.or(instance, bignum(4));
// Test `xor` function.
instance.xor(4);
instance.xor('4');
instance.xor(bignum(4));
bignum.xor(instance, 4);
bignum.xor(instance, '4');
bignum.xor(instance, bignum(4));
// Test `mod` function.
instance.mod(4);
instance.mod('4');
instance.mod(bignum(4));
bignum.mod(instance, 4);
bignum.mod(instance, '4');
bignum.mod(instance, bignum(4));
// Test `pow` function.
instance.pow(4);
instance.pow('4');
instance.pow(bignum(4));
bignum.pow(instance, 4);
bignum.pow(instance, '4');
bignum.pow(instance, bignum(4));
// Test `powm` function.
instance.powm(4, 4);
instance.powm('4', 4);
instance.powm(bignum(4), 4);
bignum.powm(instance, 4, 4);
bignum.powm(instance, '4', 4);
bignum.powm(instance, bignum(4), 4);
instance.powm(4, '4');
instance.powm('4', '4');
instance.powm(bignum(4), '4');
bignum.powm(instance, 4, '4');
bignum.powm(instance, '4', '4');
bignum.powm(instance, bignum(4), '4');
instance.powm(4, bignum(4));
instance.powm('4', bignum(4));
instance.powm(bignum(4), bignum(4));
bignum.powm(instance, 4, bignum(4));
bignum.powm(instance, '4', bignum(4));
bignum.powm(instance, bignum(4), bignum(4));
// Test `invertm` function.
instance.invertm(4);
instance.invertm('4');
instance.invertm(bignum(4));
bignum.invertm(instance, 4);
bignum.invertm(instance, '4');
bignum.invertm(instance, bignum(4));
// Test `rand` function.
instance.rand();
instance.rand(20);
instance.rand('20');
instance.rand(bignum(20));
bignum.rand(instance);
bignum.rand(instance, 20);
bignum.rand(instance, '20');
bignum.rand(instance, bignum(20));
// Test `probPrime` function.
instance.probPrime();
bignum.probPrime(instance);
// Test `shiftLeft` function.
instance.shiftLeft(4);
instance.shiftLeft('4');
instance.shiftLeft(bignum(4));
bignum.shiftLeft(instance, 4);
bignum.shiftLeft(instance, '4');
bignum.shiftLeft(instance, bignum(4));
// Test `shiftRight` function.
instance.shiftRight(4);
instance.shiftRight('4');
instance.shiftRight(bignum(4));
bignum.shiftRight(instance, 4);
bignum.shiftRight(instance, '4');
bignum.shiftRight(instance, bignum(4));
// Test `gcd` function.
instance.gcd(bignum(4));
bignum.gcd(instance, bignum(4));
// Test `jacobi` function.
instance.jacobi(bignum(17));
bignum.jacobi(instance, bignum(17));
// Test `bitLength` function.
instance.bitLength();
bignum.bitLength(instance);
+269
View File
@@ -0,0 +1,269 @@
// Type definitions for BigNum
// Project: https://github.com/justmoon/node-BigNum
// Definitions by: Pat Smuk <https://github.com/Patman64>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../node/node.d.ts" />
declare namespace BigNum {
/** Anything that can be converted to BigNum. */
type BigNumCompatible = BigNum | number | string;
export interface BufferOptions {
/** Can be either 'big' or 'little'. Also accepts 1 for big and -1 for little. Doesn't matter when size = 1. */
endian: string | number;
/** Number of bytes per word, or 'auto' to flip entire Buffer. */
size: number | string;
}
export class BigNum {
/** Create a new BigNum from n. */
constructor(n: number|BigNum);
/** Create a new BigNum from n and a base. */
constructor(n: string, base?: number);
/**
* Create a new BigNum from a Buffer.
*
* The default options are: {endian: 'big', size: 1}.
*/
static fromBuffer(buffer: Buffer, options?: BufferOptions): BigNum;
/**
* Generate a probable prime of length bits.
*
* If safe is true, it will be a "safe" prime of the form p=2p'+1 where p' is also prime.
*/
static prime(bits: number, safe?: boolean): BigNum;
/** Return true if num is identified as a BigNum instance. Otherwise, return false. */
static isBigNum(num: any): boolean;
/** Print out the BigNum instance in the requested base as a string. Default: base 10 */
toString(base?: number): string;
/**
* Turn a BigNum into a Number.
*
* If the BigNum is too big you'll lose precision or you'll get ±Infinity.
*/
toNumber(): number;
/**
* Return a new Buffer with the data from the BigNum.
*
* The default options are: {endian: 'big', size: 1}.
*/
toBuffer(options?: BufferOptions): Buffer;
/** Return a new BigNum containing the instance value plus n. */
add(n: BigNumCompatible): BigNum;
/** Return a new BigNum containing the instance value minus n. */
sub(n: BigNumCompatible): BigNum;
/** Return a new BigNum containing the instance value multiplied by n. */
mul(n: BigNumCompatible): BigNum;
/** Return a new BigNum containing the instance value integrally divided by n. */
div(n: BigNumCompatible): BigNum;
/** Return a new BigNum with the absolute value of the instance. */
abs(): BigNum;
/** Return a new BigNum with the negative of the instance value. */
neg(): BigNum;
/**
* Compare the instance value to n.
*
* Return a positive integer if > n, a negative integer if < n, and 0 if == n.
*/
cmp(n: BigNumCompatible): number;
/** Return a boolean: whether the instance value is greater than n (> n). */
gt(n: BigNumCompatible): boolean;
/** Return a boolean: whether the instance value is greater than or equal to n (>= n). */
ge(n: BigNumCompatible): boolean;
/** Return a boolean: whether the instance value is equal to n (== n). */
eq(n: BigNumCompatible): boolean;
/** Return a boolean: whether the instance value is less than n (< n). */
lt(n: BigNumCompatible): boolean;
/** Return a boolean: whether the instance value is less than or equal to n (<= n). */
le(n: BigNumCompatible): boolean;
/** Return a new BigNum with the instance value bitwise AND (&)-ed with n. */
and(n: BigNumCompatible): BigNum;
/** Return a new BigNum with the instance value bitwise inclusive-OR (|)-ed with n. */
or(n: BigNumCompatible): BigNum;
/** Return a new BigNum with the instance value bitwise exclusive-OR (^)-ed with n. */
xor(n: BigNumCompatible): BigNum;
/** Return a new BigNum with the instance value modulo n. */
mod(n: BigNumCompatible): BigNum;
/** Return a new BigNum with the instance value raised to the nth power. */
pow(n: BigNumCompatible): BigNum;
/** Return a new BigNum with the instance value raised to the nth power modulo m. */
powm(n: BigNumCompatible, m: BigNumCompatible): BigNum;
/** Compute the multiplicative inverse modulo m. */
invertm(m: BigNumCompatible): BigNum;
/**
* If upperBound is supplied, return a random BigNum between the instance value and upperBound - 1, inclusive.
* Otherwise, return a random BigNum between 0 and the instance value - 1, inclusive.
*/
rand(upperBound?: BigNumCompatible): BigNum;
/**
* Return whether the BigNum is:
* - certainly prime (true)
* - probably prime ('maybe')
* - certainly composite (false)
*/
probPrime(): boolean|string;
/** Return a new BigNum that is the 2^n multiple. Equivalent of the << operator. */
shiftLeft(n: BigNumCompatible): BigNum;
/** Return a new BigNum of the value integer divided by 2^n. Equivalent of the >> operator. */
shiftRight(n: BigNumCompatible): BigNum;
/** Return the greatest common divisor of the current BigNum with n as a new BigNum. */
gcd(n: BigNum): BigNum;
/**
* Return the Jacobi symbol (or Legendre symbol if n is prime) of the current BigNum (= a) over n.
* Note that n must be odd and >= 3. 0 <= a < n.
*
* Returns -1 or 1 as an int (NOT a BigNum). Throws an error on failure.
*/
jacobi(n: BigNum): BigNum;
/** Return the number of bits used to represent the current BigNum. */
bitLength(): number;
}
/**
* Turn a BigNum into a Number.
*
* If the BigNum is too big you'll lose precision or you'll get ±Infinity.
*/
export function toNumber(n: BigNumCompatible): number;
/**
* Return a new Buffer with the data from the BigNum.
*
* The default options are: {endian: 'big', size: 1}.
*/
export function toBuffer(n: BigNumCompatible, options?: BufferOptions): Buffer;
/** Return a new BigNum containing the instance value plus n. */
export function add(left: BigNumCompatible, right: BigNumCompatible): BigNum;
/** Return a new BigNum containing the instance value minus n. */
export function sub(left: BigNumCompatible, right: BigNumCompatible): BigNum;
/** Return a new BigNum containing the instance value multiplied by n. */
export function mul(left: BigNumCompatible, right: BigNumCompatible): BigNum;
/** Return a new BigNum containing the instance value integrally divided by n. */
export function div(dividend: BigNumCompatible, divisor: BigNumCompatible): BigNum;
/** Return a new BigNum with the absolute value of the instance. */
export function abs(n: BigNumCompatible): BigNum;
/** Return a new BigNum with the negative of the instance value. */
export function neg(n: BigNumCompatible): BigNum;
/**
* Compare the instance value to n.
*
* Return a positive integer if > n, a negative integer if < n, and 0 if == n.
*/
export function cmp(left: BigNumCompatible, right: BigNumCompatible): number;
/** Return a boolean: whether the instance value is greater than n (> n). */
export function gt(left: BigNumCompatible, right: BigNumCompatible): boolean;
/** Return a boolean: whether the instance value is greater than or equal to n (>= n). */
export function ge(left: BigNumCompatible, right: BigNumCompatible): boolean;
/** Return a boolean: whether the instance value is equal to n (== n). */
export function eq(left: BigNumCompatible, right: BigNumCompatible): boolean;
/** Return a boolean: whether the instance value is less than n (< n). */
export function lt(left: BigNumCompatible, right: BigNumCompatible): boolean;
/** Return a boolean: whether the instance value is less than or equal to n (<= n). */
export function le(left: BigNumCompatible, right: BigNumCompatible): boolean;
/** Return a new BigNum with the instance value bitwise AND (&)-ed with n. */
export function and(left: BigNumCompatible, right: BigNumCompatible): BigNum;
/** Return a new BigNum with the instance value bitwise inclusive-OR (|)-ed with n. */
export function or(left: BigNumCompatible, right: BigNumCompatible): BigNum;
/** Return a new BigNum with the instance value bitwise exclusive-OR (^)-ed with n. */
export function xor(left: BigNumCompatible, right: BigNumCompatible): BigNum;
/** Return a new BigNum with the instance value modulo n. */
export function mod(left: BigNumCompatible, right: BigNumCompatible): BigNum;
/** Return a new BigNum with the instance value raised to the nth power. */
export function pow(base: BigNumCompatible, exponent: BigNumCompatible): BigNum;
/** Return a new BigNum with the instance value raised to the nth power modulo m. */
export function powm(base: BigNumCompatible, exponent: BigNumCompatible, m: BigNumCompatible): BigNum;
/** Compute the multiplicative inverse modulo m. */
export function invertm(n: BigNumCompatible, m: BigNumCompatible): BigNum;
/**
* If upperBound is supplied, return a random BigNum between the instance value and upperBound - 1, inclusive.
* Otherwise, return a random BigNum between 0 and the instance value - 1, inclusive.
*/
export function rand(n: BigNumCompatible, upperBound?: BigNumCompatible): BigNum;
/**
* Return whether the BigNum is:
* - certainly prime (true)
* - probably prime ('maybe')
* - certainly composite (false)
*/
export function probPrime(n: BigNumCompatible): boolean|string;
/** Return a new BigNum that is the 2^bits multiple. Equivalent of the << operator. */
export function shiftLeft(n: BigNumCompatible, bits: BigNumCompatible): BigNum;
/** Return a new BigNum of the value integer divided by 2^bits. Equivalent of the >> operator. */
export function shiftRight(n: BigNumCompatible, bits: BigNumCompatible): BigNum;
/** Return the greatest common divisor of the current BigNum with n as a new BigNum. */
export function gcd(left: BigNumCompatible, right: BigNum): BigNum;
/**
* Return the Jacobi symbol (or Legendre symbol if n is prime) of the current BigNum (= a) over n.
* Note that n must be odd and >= 3. 0 <= a < n.
*
* Returns -1 or 1 as an int (NOT a BigNum). Throws an error on failure.
*/
export function jacobi(a: BigNumCompatible, n: BigNum): BigNum;
/** Return the number of bits used to represent the current BigNum. */
export function bitLength(n: BigNumCompatible): number;
}
declare module "bignum" {
export = BigNum.BigNum;
}