浏览代码

use big-integer (#383)

Ahmad Malik 2 年之前
父节点
当前提交
07979d3697
共有 4 个文件被更改,包括 23 次插入22 次删除
  1. 4 4
      gramjs/Helpers.ts
  2. 15 15
      gramjs/Password.ts
  3. 3 2
      gramjs/network/MTProtoPlainSender.ts
  4. 1 1
      gramjs/network/MTProtoState.ts

+ 4 - 4
gramjs/Helpers.ts

@@ -133,11 +133,11 @@ export function readBufferFromBigInt(
     if (bytesNumber < bytes) {
         throw new Error("OverflowError: int too big to convert");
     }
-    if (!signed && bigIntVar.lesser(BigInt(0))) {
+    if (!signed && bigIntVar.lesser(bigInt(0))) {
         throw new Error("Cannot convert to unsigned");
     }
     let below = false;
-    if (bigIntVar.lesser(BigInt(0))) {
+    if (bigIntVar.lesser(bigInt(0))) {
         below = true;
         bigIntVar = bigIntVar.abs();
     }
@@ -358,8 +358,8 @@ export function modExp(
     let result = bigInt.one;
     let x = a;
     while (b.greater(bigInt.zero)) {
-        const leastSignificantBit = b.remainder(BigInt(2));
-        b = b.divide(BigInt(2));
+        const leastSignificantBit = b.remainder(bigInt(2));
+        b = b.divide(bigInt(2));
         if (leastSignificantBit.eq(bigInt.one)) {
             result = result.multiply(x);
             result = result.remainder(n);

+ 15 - 15
gramjs/Password.ts

@@ -32,33 +32,33 @@ function checkPrimeAndGoodCheck(prime, g) {
     if (Factorizator.factorize(prime)[0] !== 1) {
         throw new Error('give "prime" is not prime')
     }
-    if (g.eq(BigInt(2))) {
-        if ((prime.remainder(BigInt(8))).neq(BigInt(7))) {
+    if (g.eq(bigInt(2))) {
+        if ((prime.remainder(bigInt(8))).neq(bigInt(7))) {
             throw new Error(`bad g ${g}, mod8 ${prime % 8}`)
         }
-    } else if (g.eq(BigInt(3))) {
-        if ((prime.remainder(BigInt(3))).neq(BigInt(2))) {
+    } else if (g.eq(bigInt(3))) {
+        if ((prime.remainder(bigInt(3))).neq(bigInt(2))) {
             throw new Error(`bad g ${g}, mod3 ${prime % 3}`)
         }
         // eslint-disable-next-line no-empty
-    } else if (g.eq(BigInt(4))) {
+    } else if (g.eq(bigInt(4))) {
 
-    } else if (g.eq(BigInt(5))) {
-        if (!([ BigInt(1), BigInt(4) ].includes(prime.remainder(BigInt(5))))) {
+    } else if (g.eq(bigInt(5))) {
+        if (!([ bigInt(1), bigInt(4) ].includes(prime.remainder(bigInt(5))))) {
             throw new Error(`bad g ${g}, mod8 ${prime % 5}`)
         }
-    } else if (g.eq(BigInt(6))) {
-        if (!([ BigInt(19), BigInt(23) ].includes(prime.remainder(BigInt(24))))) {
+    } else if (g.eq(bigInt(6))) {
+        if (!([ bigInt(19), bigInt(23) ].includes(prime.remainder(bigInt(24))))) {
             throw new Error(`bad g ${g}, mod8 ${prime % 24}`)
         }
-    } else if (g.eq(BigInt(7))) {
-        if (!([ BigInt(3), BigInt(5), BigInt(6) ].includes(prime.remainder(BigInt(7))))) {
+    } else if (g.eq(bigInt(7))) {
+        if (!([ bigInt(3), bigInt(5), bigInt(6) ].includes(prime.remainder(bigInt(7))))) {
             throw new Error(`bad g ${g}, mod8 ${prime % 7}`)
         }
     } else {
         throw new Error(`bad g ${g}`)
     }
-    const primeSub1Div2 = (prime.subtract(BigInt(1))).divide(BigInt(2))
+    const primeSub1Div2 = (prime.subtract(bigInt(1))).divide(bigInt(2))
     if (Factorizator.factorize(primeSub1Div2)[0] !== 1) {
         throw new Error('(prime - 1) // 2 is not prime')
     }
@@ -110,7 +110,7 @@ function checkPrimeAndGood(primeBytes: Buffer, g: number) {
  * @returns {boolean}
  */
 function isGoodLarge(number: bigInt.BigInteger, p: bigInt.BigInteger) {
-    return number.greater(BigInt(0)) && p.subtract(number).greater(BigInt(0));
+    return number.greater(bigInt(0)) && p.subtract(number).greater(bigInt(0));
 }
 
 /**
@@ -147,7 +147,7 @@ function isGoodModExpFirst(
     const maxModExpSize = 256;
 
     return !(
-        diff.lesser(BigInt(0)) ||
+        diff.lesser(bigInt(0)) ||
         diff.bitLength().toJSNumber() < minDiffBitsCount ||
         modexp.bitLength().toJSNumber() < minDiffBitsCount ||
         Math.floor((modexp.bitLength().toJSNumber() + 7) / 8) > maxModExpSize
@@ -272,7 +272,7 @@ async function computeCheck(request: Api.account.Password, password: string) {
                     await sha256(Buffer.concat([aForHash, bForHash])),
                     false
                 );
-                if (u.greater(BigInt(0))) {
+                if (u.greater(bigInt(0))) {
                     return {
                         a: a,
                         aForHash: aForHash,

+ 3 - 2
gramjs/network/MTProtoPlainSender.ts

@@ -2,6 +2,7 @@
  *  This module contains the class used to communicate with Telegram's servers
  *  in plain text, when no authorization key has been created yet.
  */
+import bigInt from "big-integer";
 import { MTProtoState } from "./MTProtoState";
 import { Api } from "../tl";
 import { toSignedLittleBuffer } from "../Helpers";
@@ -50,11 +51,11 @@ export class MTProtoPlainSender {
         }
         const reader = new BinaryReader(body);
         const authKeyId = reader.readLong();
-        if (authKeyId.neq(BigInt(0))) {
+        if (authKeyId.neq(bigInt(0))) {
             throw new Error("Bad authKeyId");
         }
         msgId = reader.readLong();
-        if (msgId.eq(BigInt(0))) {
+        if (msgId.eq(bigInt(0))) {
             throw new Error("Bad msgId");
         }
         /** ^ We should make sure that the read ``msg_id`` is greater

+ 1 - 1
gramjs/network/MTProtoState.ts

@@ -284,7 +284,7 @@ export class MTProtoState {
         const bad = this._getNewMsgId();
         const old = this.timeOffset;
         const now = Math.floor(new Date().getTime() / 1000);
-        const correct = correctMsgId.shiftRight(BigInt(32)).toJSNumber();
+        const correct = correctMsgId.shiftRight(bigInt(32)).toJSNumber();
         this.timeOffset = correct - now;
 
         if (this.timeOffset !== old) {