How to use the big-integer.one function in big-integer

To help you get started, we’ve selected a few big-integer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github iden3 / websnark / test / int.js View on Github external
it("It should profile int", async () => {

        const pA = pbInt.alloc();
        const pB = pbInt.alloc();
        const pC = pbInt.alloc(64);

        let start, end, time;

        const A = bigInt.one.shiftLeft(256).minus(1);
        const B = bigInt.one.shiftLeft(256).minus(1);

        pbInt.set(pA, A);
        pbInt.set(pB, B);

        start = new Date().getTime();
        pbInt.test_int_mul(pA, pB, pC, 50000000);
        end = new Date().getTime();
        time = end - start;

        const c1 = pbInt.get(pC, 1, 64);
        assert(c1.equals(A.times(B)));

        console.log("Mul Time (ms): " + time);

        // start = new Date().getTime();
        // pbInt.test_int_mulOld(pA, pB, pC, 50000000);
github thetrime / proscript2 / src / arithmetic.js View on Github external
function toRationals(args) // Takes an array of Objects and returns an array of Rationals
{
    var r = new Array(args.length);
    for (var i = 0; i < args.length; i++)
    {
        var v = evaluate_expression(args[i]);
        if (v instanceof RationalTerm)
            r[i] = v.value;
        else if (v instanceof BigIntegerTerm)
            r[i] = new Rational(v.value, BigInteger.one);
        else if (v instanceof IntegerTerm)
            r[i] = new Rational(new BigInteger(v.value), BigInteger.one);
        else
            throw new Error("Illegal BigInteger conversion from " + v.getClass());
    }
    return r;
}
github jeffijoe / snicket / src / postgres / pg-stream-store.ts View on Github external
if (messages.length === count + 1) {
      // We intentionally included another message to see if we are at the end.
      // We are not.
      isEnd = false
      messages.splice(messages.length - 1, 1)
    }

    const lastMessage = messages[messages.length - 1]
    const nextPosition = forward
      ? BigInteger(lastMessage.position)
          .plus(BigInteger.one)
          .toString()
      : // nextVersion will be 0 at the end, but that always includes the first message in
        // the stream. There's no way around this that does not skip the first message.
        BigInteger.max(
          BigInteger(lastMessage.position).minus(BigInteger.one),
          BigInteger(-1)
        ).toString()

    return {
      isEnd,
      nextPosition,
      messages: await maybeFilterExpiredMessages(messages)
    }
  }
github iden3 / websnark / src / build_f1m.js View on Github external
module.exports = function buildF1m(module, _q, _prefix, _intPrefix) {
    const q = bigInt(_q);
    const n64 = Math.floor((q.minus(1).bitLength() - 1)/64) +1;
    const n32 = n64*2;
    const n8 = n64*8;

    const prefix = _prefix || "f1m";
    if (module.modules[prefix]) return prefix;  // already builded

    const intPrefix = buildInt(module, n64, _intPrefix);
    const pq = module.alloc(n8, utils.bigInt2BytesLE(q, n8));

    const pR = module.alloc(utils.bigInt2BytesLE(bigInt.one.shiftLeft(n64*64).mod(q), n8));
    const pR2 = module.alloc(utils.bigInt2BytesLE(bigInt.one.shiftLeft(n64*64).square().mod(q), n8));
    const pOne = module.alloc(utils.bigInt2BytesLE(bigInt.one.shiftLeft(n64*64).mod(q), n8));
    const pZero = module.alloc(utils.bigInt2BytesLE(bigInt.zero, n8));
    const _minusOne = q.minus(bigInt.one);
    const _e = _minusOne.shiftRight(1); // e = (p-1)/2
    const pe = module.alloc(n8, utils.bigInt2BytesLE(_e, n8));

    const _ePlusOne = _e.add(bigInt.one); // e = (p-1)/2
    const pePlusOne = module.alloc(n8, utils.bigInt2BytesLE(_ePlusOne, n8));

    module.modules[prefix] = {
        pq: pq,
        pR2: pR2,
        n64: n64,
        q: q,
        pOne: pOne,
        pZero: pZero,
        pePlusOne: pePlusOne
github iden3 / websnark / test / f1.js View on Github external
it("Should substract with 2 chunks overflow", async () => {
        const q = bigInt("10000000000000001", 16);
        const a = bigInt("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16).mod(q);
        const f1 = await buildF1(q);

        const pA = f1.allocInt(1);
        const pB = f1.allocInt(a);
        const pC = f1.allocInt();

        f1.f1_sub(pA, pB, pC);
        const c = f1.getInt(pC);

        let d = bigInt.one.minus(a).mod(q);
        if (d.isNegative()) d = d.add(q);

        assert(c.equals(d));
    });
    it("It should Substract a big number", async () => {
github opporty-com / Plasma-Cash / zksnark / pairing / ExNumber.js View on Github external
static testBit(bign, n) {
      return ( !bign.and( bigInt.one.shiftLeft(n) ).isZero() );
  }
}
github denysdovhan / rsa-labwork / index.js View on Github external
static randomPrime(bits) {
    const min = bigInt.one.shiftLeft(bits - 1);
    const max = bigInt.one.shiftLeft(bits).prev();
    
    while (true) {
      let p = bigInt.randBetween(min, max);
      if (p.isProbablePrime(256)) {
        return p;
      } 
    }
  }