Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
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;
}
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)
}
}
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
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 () => {
static testBit(bign, n) {
return ( !bign.and( bigInt.one.shiftLeft(n) ).isZero() );
}
}
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;
}
}
}