From 7a8afe9eccb19ce169bdfddf05666e90e17d1f20 Mon Sep 17 00:00:00 2001 From: Evgenus Date: Mon, 4 Aug 2014 19:40:22 +0300 Subject: [PATCH] definitions for Big Integer library by Tom Wu --- CONTRIBUTORS.md | 1 + jsbn/jsbn-tests.ts | 93 +++++++++++++++++ jsbn/jsbn.d.ts | 250 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 344 insertions(+) create mode 100644 jsbn/jsbn-tests.ts create mode 100644 jsbn/jsbn.d.ts diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index af326634b1..8d25e840db 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -193,6 +193,7 @@ All definitions files include a header with the author and editors, so at some p * [js-git](https://github.com/creationix/js-git) (by [Bart van der Schoor](https://github.com/Bartvds)) * [js-url](https://github.com/websanova/js-url) (by [MIZUNE Pine](https://github.com/pine613)) * [js-yaml](https://github.com/nodeca/js-yaml) (by [Bart van der Schoor](https://github.com/Bartvds/)) +* [jsbn](http://www-cs-students.stanford.edu/%7Etjw/jsbn/) (by [Eugene Chernyshov](https://github.com/Evgenus)) * [jScrollPane](http://jscrollpane.kelvinluck.com) (by [Dániel Tar](https://github.com/qcz)) * [JSDeferred](http://cho45.stfuawsc.com/jsdeferred/) (by [Daisuke Mino](https://github.com/minodisk)) * [JSONEditorOnline](https://github.com/josdejong/jsoneditoronline) (by [Vincent Bortone](https://github.com/vbortone/)) diff --git a/jsbn/jsbn-tests.ts b/jsbn/jsbn-tests.ts new file mode 100644 index 0000000000..667f785538 --- /dev/null +++ b/jsbn/jsbn-tests.ts @@ -0,0 +1,93 @@ +/// +var BigInteger = jsbn.BigInteger; + +// constructor tests +var x = new BigInteger("AABB", 16); +x = new BigInteger("75643564363473453456342378564387956906736546456235345"); + +// method tests +var isBigInteger: jsbn.BigInteger; +var isNumber: number; +var isBoolean: boolean; +var isString: string; +var isDivmod: jsbn.BigInteger[]; +var isByteArray: number[]; + +x.copyTo(x); +x.fromInt(0); +x.fromString("CAFEBABE", 16); +x.clamp(); +isString = x.toString(); +isString = x.toString(16); +isBigInteger = x.negate(); +isBigInteger = x.abs(); +isNumber = x.compareTo(x); +isNumber = x.bitLength(); +x.dlShiftTo(0, isBigInteger); +x.drShiftTo(0, isBigInteger); +x.lShiftTo(0, isBigInteger); +x.rShiftTo(0, isBigInteger); +x.subTo(x, isBigInteger); +x.multiplyTo(x, isBigInteger); +x.squareTo(isBigInteger); +x.divRemTo(x, isBigInteger, isBigInteger); +isBigInteger = x.mod(x); +isNumber = x.invDigit(); +isBoolean = x.isEven(); +isBigInteger = x.exp(0, { + convert: (x) => x, + revert: (x) => x, + reduce: (x) => x, + mulTo: (x) => x, + sqrTo: (x) => x +}); +isBigInteger = x.modPowInt(0, x); +isBigInteger = x.clone(); +isNumber = x.intValue(); +isNumber = x.byteValue(); +isNumber = x.shortValue(); +isNumber = x.chunkSize(0); +isNumber = x.signum(); +isString = x.toRadix(10); +x.fromRadix("123", 10); +x.fromNumber(1); +x.fromNumber(1, 2); +x.fromNumber(1, 2, 3); +isByteArray = x.toByteArray(); +isBoolean = x.equals(x); +isBigInteger = x.min(x); +isBigInteger = x.max(x); +x.bitwiseTo(x, (x, y) => x + y, isBigInteger); +isBigInteger = x.and(x); +isBigInteger = x.or(x); +isBigInteger = x.xor(x); +isBigInteger = x.andNot(x); +isBigInteger = x.not(); +isBigInteger = x.shiftLeft(0); +isBigInteger = x.shiftRight(0); +isNumber = x.getLowestSetBit(); +isNumber = x.bitCount(); +isBoolean = x.testBit(0); +isBigInteger = x.changeBit(0, (x, y) => x * y); +isBigInteger = x.setBit(0); +isBigInteger = x.clearBit(0); +isBigInteger = x.flipBit(0); +x.addTo(x, isBigInteger); +isBigInteger = x.add(x); +isBigInteger = x.subtract(x); +isBigInteger = x.multiply(x); +isBigInteger = x.square(); +isBigInteger = x.divide(x); +isBigInteger = x.remainder(x); +isDivmod = x.divideAndRemainder(x); +x.dMultiply(0); +x.dAddOffset(0, 0); +isBigInteger = x.pow(0); +x.multiplyLowerTo(x, 0, isBigInteger); +x.multiplyUpperTo(x, 0, isBigInteger); +isBigInteger = x.modPow(x, x); +isBigInteger = x.gcd(x); +isNumber = x.modInt(0); +isBigInteger = x.modInverse(x); +isBoolean = x.isProbablePrime(0); +isBoolean = x.millerRabin(0); diff --git a/jsbn/jsbn.d.ts b/jsbn/jsbn.d.ts new file mode 100644 index 0000000000..bd75bd8766 --- /dev/null +++ b/jsbn/jsbn.d.ts @@ -0,0 +1,250 @@ +// Type definitions for jsbn v1.2 +// Project: http://www-cs-students.stanford.edu/%7Etjw/jsbn/ +// Definitions by: Eugene Chernyshov +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module jsbn { + + interface RandomGenerator { + nextBytes(bytes: number[]): void; + } + + export class BigInteger { + constructor(a: number, c: RandomGenerator); + constructor(a: number, b: number, c: RandomGenerator); + constructor(a: string, b?: number); + constructor(a: number[], b?: number); + constructor(a: BigInteger); + + s: number; + t: number; + data: number[]; // forge specific + + DB: number; + DM: number; + DV: number; + + FV: number; + F1: number; + F2: number; + + // am: Compute w_j += (x*this_i), propagate carries, + am(i: number, x: number, w: BigInteger, j: number, c: number, n: number): number; + + // (protected) copy this to r + copyTo(r: BigInteger): void; + + // (protected) set from integer value x, -DV <= x < DV + fromInt(x: number): void; + + // (protected) set from string and radix + fromString(x: string, b: number): void; + + // (protected) clamp off excess high words + clamp(): void; + + // (public) return string representation in given radix + toString(b?: number): string; + + // (public) -this + negate(): BigInteger; + + // (public) |this| + abs(): BigInteger; + + // (public) return + if this > a, - if this < a, 0 if equal + compareTo(a: BigInteger): number; + + // (public) return the number of bits in "this" + bitLength(): number; + + // (protected) r = this << n*DB + dlShiftTo(n: number, r: BigInteger): void; + + // (protected) r = this >> n*DB + drShiftTo(n: number, r: BigInteger): void; + + // (protected) r = this << n + lShiftTo(n: number, r: BigInteger): void; + + // (protected) r = this >> n + rShiftTo(n: number, r: BigInteger): void; + + // (protected) r = this - a + subTo(a: BigInteger, r: BigInteger): void; + + // (protected) r = this * a, r != this,a (HAC 14.12) + multiplyTo(a: BigInteger, r: BigInteger): void; + + // (protected) r = this^2, r != this (HAC 14.16) + squareTo(r: BigInteger): void; + + // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20) + // r != q, this != m. q or r may be null. + divRemTo(m: BigInteger, q: BigInteger, r: BigInteger): void; + + // (public) this mod a + mod(a: BigInteger): BigInteger; + + // (protected) return "-1/this % 2^DB"; useful for Mont. reduction + invDigit(): number; + + // (protected) true iff this is even + isEven(): boolean; + + // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79) + exp(e: number, z: Reduction): BigInteger; + + // (public) this^e % m, 0 <= e < 2^32 + modPowInt(e: number, m: BigInteger): BigInteger; + + // (public) + clone(): BigInteger; + + // (public) return value as integer + intValue(): number; + + // (public) return value as byte + byteValue(): number; + + // (public) return value as short (assumes DB>=16) + shortValue(): number; + + // (protected) return x s.t. r^x < DV + chunkSize(r: number): number; + + // (public) 0 if this == 0, 1 if this > 0 + signum(): number; + + // (protected) convert to radix string + toRadix(b: number): string; + + // (protected) convert from radix string + fromRadix(s: string, b: number): void; + + // (protected) alternate constructor + fromNumber(a: number, b?: number, c?: number): void; + + // (public) convert to bigendian byte array + toByteArray(): number[]; + + equals(a: BigInteger): boolean; + + min(a: BigInteger): BigInteger; + + max(a: BigInteger): BigInteger; + + // (protected) r = this op a (bitwise) + bitwiseTo(a: BigInteger, op: (x: number, y: number) => number, r: BigInteger): void; + + // (public) this & a + and(a: BigInteger): BigInteger; + + // (public) this | a + or(a: BigInteger): BigInteger; + + // (public) this ^ a + xor(a: BigInteger): BigInteger; + + // (public) this & ~a + andNot(a: BigInteger): BigInteger; + + // (public) ~this + not(): BigInteger; + + // (public) this << n + shiftLeft(n: number): BigInteger; + + // (public) this >> n + shiftRight(n: number): BigInteger; + + // (public) returns index of lowest 1-bit (or -1 if none) + getLowestSetBit(): number; + + // (public) return number of set bits + bitCount(): number; + + // (public) true iff nth bit is set + testBit(n: number): boolean; + + // (protected) this op (1< number): BigInteger; + + // (protected) this op (1<= 0, 1 < n < DV + dMultiply(n: number): void; + + // (protected) this += n << w words, this >= 0 + dAddOffset(n: number, w: number): void; + + // (public) this^e + pow(e: number): BigInteger; + + // (protected) r = lower n words of "this * a", a.t <= n + multiplyLowerTo(a: BigInteger, n: number, r: BigInteger): void; + + // (protected) r = "this * a" without lower n words, n > 0 + multiplyUpperTo(a: BigInteger, n: number, r: BigInteger): void; + + // (public) this^e % m (HAC 14.85) + modPow(e: BigInteger, m: BigInteger): BigInteger; + + // (public) gcd(this,a) (HAC 14.54) + gcd(a: BigInteger): BigInteger; + + // (protected) this % n, n < 2^26 + modInt(n: number): number; + + // (public) 1/this % m (HAC 14.61) + modInverse(m: BigInteger): BigInteger; + + // (public) test primality with certainty >= 1-.5^t + isProbablePrime(t: number): boolean; + + // (protected) true if probably prime (HAC 4.24, Miller-Rabin) + millerRabin(t: number): boolean; + + static ZERO: BigInteger; + static ONE: BigInteger; + } + + interface Reduction { + convert(x: BigInteger): BigInteger; + revert(x: BigInteger): BigInteger; + reduce(x: BigInteger): void; + mulTo(x: BigInteger, y: BigInteger, r: BigInteger): void; + sqrTo(x: BigInteger, r: BigInteger): void; + } +} \ No newline at end of file