snarkjs/build/snarkjs.js

14737 lines
3.3 MiB
JavaScript
Raw Normal View History

2020-07-11 11:31:52 +03:00
var snarkjs = (function (exports) {
'use strict';
/* global BigInt */
const hexLen = [ 0, 1, 2, 2, 3, 3, 3, 3, 4 ,4 ,4 ,4 ,4 ,4 ,4 ,4];
function fromString(s, radix) {
if ((!radix)||(radix==10)) {
return BigInt(s);
} else if (radix==16) {
if (s.slice(0,2) == "0x") {
return BigInt(s);
} else {
return BigInt("0x"+s);
}
}
}
const e = fromString;
function fromArray(a, radix) {
let acc =0n;
radix = BigInt(radix);
for (let i=0; i<a.length; i++) {
acc = acc*radix + BigInt(a[i]);
}
return acc;
}
function bitLength(a) {
const aS =a.toString(16);
return (aS.length-1)*4 +hexLen[parseInt(aS[0], 16)];
}
function isNegative(a) {
return BigInt(a) < 0n;
}
function isZero(a) {
return !a;
}
function shiftLeft(a, n) {
return BigInt(a) << BigInt(n);
}
function shiftRight(a, n) {
return BigInt(a) >> BigInt(n);
}
const shl = shiftLeft;
const shr = shiftRight;
function isOdd(a) {
return (BigInt(a) & 1n) == 1n;
}
function naf(n) {
let E = BigInt(n);
const res = [];
while (E) {
if (E & 1n) {
const z = 2 - Number(E % 4n);
res.push( z );
E = E - BigInt(z);
} else {
res.push( 0 );
}
E = E >> 1n;
}
return res;
}
function bits(n) {
let E = BigInt(n);
const res = [];
while (E) {
if (E & 1n) {
res.push(1);
} else {
res.push( 0 );
}
E = E >> 1n;
}
return res;
}
function toNumber(s) {
if (s>BigInt(Number.MAX_SAFE_INTEGER )) {
throw new Error("Number too big");
}
return Number(s);
}
function toArray(s, radix) {
const res = [];
let rem = BigInt(s);
radix = BigInt(radix);
while (rem) {
res.unshift( Number(rem % radix));
rem = rem / radix;
}
return res;
}
function add(a, b) {
return BigInt(a) + BigInt(b);
}
function sub(a, b) {
return BigInt(a) - BigInt(b);
}
function neg(a) {
return -BigInt(a);
}
function mul(a, b) {
return BigInt(a) * BigInt(b);
}
function square(a) {
return BigInt(a) * BigInt(a);
}
function pow(a, b) {
return BigInt(a) ** BigInt(b);
}
function exp(a, b) {
return BigInt(a) ** BigInt(b);
}
function abs(a) {
return BigInt(a) >= 0 ? BigInt(a) : -BigInt(a);
}
function div(a, b) {
return BigInt(a) / BigInt(b);
}
function mod(a, b) {
return BigInt(a) % BigInt(b);
}
function eq(a, b) {
return BigInt(a) == BigInt(b);
}
function neq(a, b) {
return BigInt(a) != BigInt(b);
}
function lt(a, b) {
return BigInt(a) < BigInt(b);
}
function gt(a, b) {
return BigInt(a) > BigInt(b);
}
function leq(a, b) {
return BigInt(a) <= BigInt(b);
}
function geq(a, b) {
return BigInt(a) >= BigInt(b);
}
function band(a, b) {
return BigInt(a) & BigInt(b);
}
function bor(a, b) {
return BigInt(a) | BigInt(b);
}
function bxor(a, b) {
return BigInt(a) ^ BigInt(b);
}
function land(a, b) {
return BigInt(a) && BigInt(b);
}
function lor(a, b) {
return BigInt(a) || BigInt(b);
}
function lnot(a) {
return !BigInt(a);
}
var Scalar_native = /*#__PURE__*/Object.freeze({
__proto__: null,
fromString: fromString,
e: e,
fromArray: fromArray,
bitLength: bitLength,
isNegative: isNegative,
isZero: isZero,
shiftLeft: shiftLeft,
shiftRight: shiftRight,
shl: shl,
shr: shr,
isOdd: isOdd,
naf: naf,
bits: bits,
toNumber: toNumber,
toArray: toArray,
add: add,
sub: sub,
neg: neg,
mul: mul,
square: square,
pow: pow,
exp: exp,
abs: abs,
div: div,
mod: mod,
eq: eq,
neq: neq,
lt: lt,
gt: gt,
leq: leq,
geq: geq,
band: band,
bor: bor,
bxor: bxor,
land: land,
lor: lor,
lnot: lnot
});
2021-01-17 11:56:08 +03:00
function createCommonjsModule(fn) {
var module = { exports: {} };
return fn(module, module.exports), module.exports;
}
function commonjsRequire (target) {
throw new Error('Could not dynamically require "' + target + '". Please configure the dynamicRequireTargets option of @rollup/plugin-commonjs appropriately for this require call to behave properly.');
2020-07-11 11:31:52 +03:00
}
var BigInteger = createCommonjsModule(function (module) {
var bigInt = (function (undefined$1) {
var BASE = 1e7,
LOG_BASE = 7,
MAX_INT = 9007199254740992,
MAX_INT_ARR = smallToArray(MAX_INT),
DEFAULT_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz";
var supportsNativeBigInt = typeof BigInt === "function";
function Integer(v, radix, alphabet, caseSensitive) {
if (typeof v === "undefined") return Integer[0];
if (typeof radix !== "undefined") return +radix === 10 && !alphabet ? parseValue(v) : parseBase(v, radix, alphabet, caseSensitive);
return parseValue(v);
}
function BigInteger(value, sign) {
this.value = value;
this.sign = sign;
this.isSmall = false;
}
BigInteger.prototype = Object.create(Integer.prototype);
function SmallInteger(value) {
this.value = value;
this.sign = value < 0;
this.isSmall = true;
}
SmallInteger.prototype = Object.create(Integer.prototype);
function NativeBigInt(value) {
this.value = value;
}
NativeBigInt.prototype = Object.create(Integer.prototype);
function isPrecise(n) {
return -MAX_INT < n && n < MAX_INT;
}
function smallToArray(n) { // For performance reasons doesn't reference BASE, need to change this function if BASE changes
if (n < 1e7)
return [n];
if (n < 1e14)
return [n % 1e7, Math.floor(n / 1e7)];
return [n % 1e7, Math.floor(n / 1e7) % 1e7, Math.floor(n / 1e14)];
}
function arrayToSmall(arr) { // If BASE changes this function may need to change
trim(arr);
var length = arr.length;
if (length < 4 && compareAbs(arr, MAX_INT_ARR) < 0) {
switch (length) {
case 0: return 0;
case 1: return arr[0];
case 2: return arr[0] + arr[1] * BASE;
default: return arr[0] + (arr[1] + arr[2] * BASE) * BASE;
}
}
return arr;
}
function trim(v) {
var i = v.length;
while (v[--i] === 0);
v.length = i + 1;
}
function createArray(length) { // function shamelessly stolen from Yaffle's library https://github.com/Yaffle/BigInteger
var x = new Array(length);
var i = -1;
while (++i < length) {
x[i] = 0;
}
return x;
}
function truncate(n) {
if (n > 0) return Math.floor(n);
return Math.ceil(n);
}
function add(a, b) { // assumes a and b are arrays with a.length >= b.length
var l_a = a.length,
l_b = b.length,
r = new Array(l_a),
carry = 0,
base = BASE,
sum, i;
for (i = 0; i < l_b; i++) {
sum = a[i] + b[i] + carry;
carry = sum >= base ? 1 : 0;
r[i] = sum - carry * base;
}
while (i < l_a) {
sum = a[i] + carry;
carry = sum === base ? 1 : 0;
r[i++] = sum - carry * base;
}
if (carry > 0) r.push(carry);
return r;
}
function addAny(a, b) {
if (a.length >= b.length) return add(a, b);
return add(b, a);
}
function addSmall(a, carry) { // assumes a is array, carry is number with 0 <= carry < MAX_INT
var l = a.length,
r = new Array(l),
base = BASE,
sum, i;
for (i = 0; i < l; i++) {
sum = a[i] - base + carry;
carry = Math.floor(sum / base);
r[i] = sum - carry * base;
carry += 1;
}
while (carry > 0) {
r[i++] = carry % base;
carry = Math.floor(carry / base);
}
return r;
}
BigInteger.prototype.add = function (v) {
var n = parseValue(v);
if (this.sign !== n.sign) {
return this.subtract(n.negate());
}
var a = this.value, b = n.value;
if (n.isSmall) {
return new BigInteger(addSmall(a, Math.abs(b)), this.sign);
}
return new BigInteger(addAny(a, b), this.sign);
};
BigInteger.prototype.plus = BigInteger.prototype.add;
SmallInteger.prototype.add = function (v) {
var n = parseValue(v);
var a = this.value;
if (a < 0 !== n.sign) {
return this.subtract(n.negate());
}
var b = n.value;
if (n.isSmall) {
if (isPrecise(a + b)) return new SmallInteger(a + b);
b = smallToArray(Math.abs(b));
}
return new BigInteger(addSmall(b, Math.abs(a)), a < 0);
};
SmallInteger.prototype.plus = SmallInteger.prototype.add;
NativeBigInt.prototype.add = function (v) {
return new NativeBigInt(this.value + parseValue(v).value);
};
NativeBigInt.prototype.plus = NativeBigInt.prototype.add;
function subtract(a, b) { // assumes a and b are arrays with a >= b
var a_l = a.length,
b_l = b.length,
r = new Array(a_l),
borrow = 0,
base = BASE,
i, difference;
for (i = 0; i < b_l; i++) {
difference = a[i] - borrow - b[i];
if (difference < 0) {
difference += base;
borrow = 1;
} else borrow = 0;
r[i] = difference;
}
for (i = b_l; i < a_l; i++) {
difference = a[i] - borrow;
if (difference < 0) difference += base;
else {
r[i++] = difference;
break;
}
r[i] = difference;
}
for (; i < a_l; i++) {
r[i] = a[i];
}
trim(r);
return r;
}
function subtractAny(a, b, sign) {
var value;
if (compareAbs(a, b) >= 0) {
value = subtract(a, b);
} else {
value = subtract(b, a);
sign = !sign;
}
value = arrayToSmall(value);
if (typeof value === "number") {
if (sign) value = -value;
return new SmallInteger(value);
}
return new BigInteger(value, sign);
}
function subtractSmall(a, b, sign) { // assumes a is array, b is number with 0 <= b < MAX_INT
var l = a.length,
r = new Array(l),
carry = -b,
base = BASE,
i, difference;
for (i = 0; i < l; i++) {
difference = a[i] + carry;
carry = Math.floor(difference / base);
difference %= base;
r[i] = difference < 0 ? difference + base : difference;
}
r = arrayToSmall(r);
if (typeof r === "number") {
if (sign) r = -r;
return new SmallInteger(r);
} return new BigInteger(r, sign);
}
BigInteger.prototype.subtract = function (v) {
var n = parseValue(v);
if (this.sign !== n.sign) {
return this.add(n.negate());
}
var a = this.value, b = n.value;
if (n.isSmall)
return subtractSmall(a, Math.abs(b), this.sign);
return subtractAny(a, b, this.sign);
};
BigInteger.prototype.minus = BigInteger.prototype.subtract;
SmallInteger.prototype.subtract = function (v) {
var n = parseValue(v);
var a = this.value;
if (a < 0 !== n.sign) {
return this.add(n.negate());
}
var b = n.value;
if (n.isSmall) {
return new SmallInteger(a - b);
}
return subtractSmall(b, Math.abs(a), a >= 0);
};
SmallInteger.prototype.minus = SmallInteger.prototype.subtract;
NativeBigInt.prototype.subtract = function (v) {
return new NativeBigInt(this.value - parseValue(v).value);
};
NativeBigInt.prototype.minus = NativeBigInt.prototype.subtract;
BigInteger.prototype.negate = function () {
return new BigInteger(this.value, !this.sign);
};
SmallInteger.prototype.negate = function () {
var sign = this.sign;
var small = new SmallInteger(-this.value);
small.sign = !sign;
return small;
};
NativeBigInt.prototype.negate = function () {
return new NativeBigInt(-this.value);
};
BigInteger.prototype.abs = function () {
return new BigInteger(this.value, false);
};
SmallInteger.prototype.abs = function () {
return new SmallInteger(Math.abs(this.value));
};
NativeBigInt.prototype.abs = function () {
return new NativeBigInt(this.value >= 0 ? this.value : -this.value);
};
function multiplyLong(a, b) {
var a_l = a.length,
b_l = b.length,
l = a_l + b_l,
r = createArray(l),
base = BASE,
product, carry, i, a_i, b_j;
for (i = 0; i < a_l; ++i) {
a_i = a[i];
for (var j = 0; j < b_l; ++j) {
b_j = b[j];
product = a_i * b_j + r[i + j];
carry = Math.floor(product / base);
r[i + j] = product - carry * base;
r[i + j + 1] += carry;
}
}
trim(r);
return r;
}
function multiplySmall(a, b) { // assumes a is array, b is number with |b| < BASE
var l = a.length,
r = new Array(l),
base = BASE,
carry = 0,
product, i;
for (i = 0; i < l; i++) {
product = a[i] * b + carry;
carry = Math.floor(product / base);
r[i] = product - carry * base;
}
while (carry > 0) {
r[i++] = carry % base;
carry = Math.floor(carry / base);
}
return r;
}
function shiftLeft(x, n) {
var r = [];
while (n-- > 0) r.push(0);
return r.concat(x);
}
function multiplyKaratsuba(x, y) {
var n = Math.max(x.length, y.length);
if (n <= 30) return multiplyLong(x, y);
n = Math.ceil(n / 2);
var b = x.slice(n),
a = x.slice(0, n),
d = y.slice(n),
c = y.slice(0, n);
var ac = multiplyKaratsuba(a, c),
bd = multiplyKaratsuba(b, d),
abcd = multiplyKaratsuba(addAny(a, b), addAny(c, d));
var product = addAny(addAny(ac, shiftLeft(subtract(subtract(abcd, ac), bd), n)), shiftLeft(bd, 2 * n));
trim(product);
return product;
}
// The following function is derived from a surface fit of a graph plotting the performance difference
// between long multiplication and karatsuba multiplication versus the lengths of the two arrays.
function useKaratsuba(l1, l2) {
return -0.012 * l1 - 0.012 * l2 + 0.000015 * l1 * l2 > 0;
}
BigInteger.prototype.multiply = function (v) {
var n = parseValue(v),
a = this.value, b = n.value,
sign = this.sign !== n.sign,
abs;
if (n.isSmall) {
if (b === 0) return Integer[0];
if (b === 1) return this;
if (b === -1) return this.negate();
abs = Math.abs(b);
if (abs < BASE) {
return new BigInteger(multiplySmall(a, abs), sign);
}
b = smallToArray(abs);
}
if (useKaratsuba(a.length, b.length)) // Karatsuba is only faster for certain array sizes
return new BigInteger(multiplyKaratsuba(a, b), sign);
return new BigInteger(multiplyLong(a, b), sign);
};
BigInteger.prototype.times = BigInteger.prototype.multiply;
function multiplySmallAndArray(a, b, sign) { // a >= 0
if (a < BASE) {
return new BigInteger(multiplySmall(b, a), sign);
}
return new BigInteger(multiplyLong(b, smallToArray(a)), sign);
}
SmallInteger.prototype._multiplyBySmall = function (a) {
if (isPrecise(a.value * this.value)) {
return new SmallInteger(a.value * this.value);
}
return multiplySmallAndArray(Math.abs(a.value), smallToArray(Math.abs(this.value)), this.sign !== a.sign);
};
BigInteger.prototype._multiplyBySmall = function (a) {
if (a.value === 0) return Integer[0];
if (a.value === 1) return this;
if (a.value === -1) return this.negate();
return multiplySmallAndArray(Math.abs(a.value), this.value, this.sign !== a.sign);
};
SmallInteger.prototype.multiply = function (v) {
return parseValue(v)._multiplyBySmall(this);
};
SmallInteger.prototype.times = SmallInteger.prototype.multiply;
NativeBigInt.prototype.multiply = function (v) {
return new NativeBigInt(this.value * parseValue(v).value);
};
NativeBigInt.prototype.times = NativeBigInt.prototype.multiply;
function square(a) {
//console.assert(2 * BASE * BASE < MAX_INT);
var l = a.length,
r = createArray(l + l),
base = BASE,
product, carry, i, a_i, a_j;
for (i = 0; i < l; i++) {
a_i = a[i];
carry = 0 - a_i * a_i;
for (var j = i; j < l; j++) {
a_j = a[j];
product = 2 * (a_i * a_j) + r[i + j] + carry;
carry = Math.floor(product / base);
r[i + j] = product - carry * base;
}
r[i + l] = carry;
}
trim(r);
return r;
}
BigInteger.prototype.square = function () {
return new BigInteger(square(this.value), false);
};
SmallInteger.prototype.square = function () {
var value = this.value * this.value;
if (isPrecise(value)) return new SmallInteger(value);
return new BigInteger(square(smallToArray(Math.abs(this.value))), false);
};
NativeBigInt.prototype.square = function (v) {
return new NativeBigInt(this.value * this.value);
};
function divMod1(a, b) { // Left over from previous version. Performs faster than divMod2 on smaller input sizes.
var a_l = a.length,
b_l = b.length,
base = BASE,
result = createArray(b.length),
divisorMostSignificantDigit = b[b_l - 1],
// normalization
lambda = Math.ceil(base / (2 * divisorMostSignificantDigit)),
remainder = multiplySmall(a, lambda),
divisor = multiplySmall(b, lambda),
quotientDigit, shift, carry, borrow, i, l, q;
if (remainder.length <= a_l) remainder.push(0);
divisor.push(0);
divisorMostSignificantDigit = divisor[b_l - 1];
for (shift = a_l - b_l; shift >= 0; shift--) {
quotientDigit = base - 1;
if (remainder[shift + b_l] !== divisorMostSignificantDigit) {
quotientDigit = Math.floor((remainder[shift + b_l] * base + remainder[shift + b_l - 1]) / divisorMostSignificantDigit);
}
// quotientDigit <= base - 1
carry = 0;
borrow = 0;
l = divisor.length;
for (i = 0; i < l; i++) {
carry += quotientDigit * divisor[i];
q = Math.floor(carry / base);
borrow += remainder[shift + i] - (carry - q * base);
carry = q;
if (borrow < 0) {
remainder[shift + i] = borrow + base;
borrow = -1;
} else {
remainder[shift + i] = borrow;
borrow = 0;
}
}
while (borrow !== 0) {
quotientDigit -= 1;
carry = 0;
for (i = 0; i < l; i++) {
carry += remainder[shift + i] - base + divisor[i];
if (carry < 0) {
remainder[shift + i] = carry + base;
carry = 0;
} else {
remainder[shift + i] = carry;
carry = 1;
}
}
borrow += carry;
}
result[shift] = quotientDigit;
}
// denormalization
remainder = divModSmall(remainder, lambda)[0];
return [arrayToSmall(result), arrayToSmall(remainder)];
}
function divMod2(a, b) { // Implementation idea shamelessly stolen from Silent Matt's library http://silentmatt.com/biginteger/
// Performs faster than divMod1 on larger input sizes.
var a_l = a.length,
b_l = b.length,
result = [],
part = [],
base = BASE,
guess, xlen, highx, highy, check;
while (a_l) {
part.unshift(a[--a_l]);
trim(part);
if (compareAbs(part, b) < 0) {
result.push(0);
continue;
}
xlen = part.length;
highx = part[xlen - 1] * base + part[xlen - 2];
highy = b[b_l - 1] * base + b[b_l - 2];
if (xlen > b_l) {
highx = (highx + 1) * base;
}
guess = Math.ceil(highx / highy);
do {
check = multiplySmall(b, guess);
if (compareAbs(check, part) <= 0) break;
guess--;
} while (guess);
result.push(guess);
part = subtract(part, check);
}
result.reverse();
return [arrayToSmall(result), arrayToSmall(part)];
}
function divModSmall(value, lambda) {
var length = value.length,
quotient = createArray(length),
base = BASE,
i, q, remainder, divisor;
remainder = 0;
for (i = length - 1; i >= 0; --i) {
divisor = remainder * base + value[i];
q = truncate(divisor / lambda);
remainder = divisor - q * lambda;
quotient[i] = q | 0;
}
return [quotient, remainder | 0];
}
function divModAny(self, v) {
var value, n = parseValue(v);
if (supportsNativeBigInt) {
return [new NativeBigInt(self.value / n.value), new NativeBigInt(self.value % n.value)];
}
var a = self.value, b = n.value;
var quotient;
if (b === 0) throw new Error("Cannot divide by zero");
if (self.isSmall) {
if (n.isSmall) {
return [new SmallInteger(truncate(a / b)), new SmallInteger(a % b)];
}
return [Integer[0], self];
}
if (n.isSmall) {
if (b === 1) return [self, Integer[0]];
if (b == -1) return [self.negate(), Integer[0]];
var abs = Math.abs(b);
if (abs < BASE) {
value = divModSmall(a, abs);
quotient = arrayToSmall(value[0]);
var remainder = value[1];
if (self.sign) remainder = -remainder;
if (typeof quotient === "number") {
if (self.sign !== n.sign) quotient = -quotient;
return [new SmallInteger(quotient), new SmallInteger(remainder)];
}
return [new BigInteger(quotient, self.sign !== n.sign), new SmallInteger(remainder)];
}
b = smallToArray(abs);
}
var comparison = compareAbs(a, b);
if (comparison === -1) return [Integer[0], self];
if (comparison === 0) return [Integer[self.sign === n.sign ? 1 : -1], Integer[0]];
// divMod1 is faster on smaller input sizes
if (a.length + b.length <= 200)
value = divMod1(a, b);
else value = divMod2(a, b);
quotient = value[0];
var qSign = self.sign !== n.sign,
mod = value[1],
mSign = self.sign;
if (typeof quotient === "number") {
if (qSign) quotient = -quotient;
quotient = new SmallInteger(quotient);
} else quotient = new BigInteger(quotient, qSign);
if (typeof mod === "number") {
if (mSign) mod = -mod;
mod = new SmallInteger(mod);
} else mod = new BigInteger(mod, mSign);
return [quotient, mod];
}
BigInteger.prototype.divmod = function (v) {
var result = divModAny(this, v);
return {
quotient: result[0],
remainder: result[1]
};
};
NativeBigInt.prototype.divmod = SmallInteger.prototype.divmod = BigInteger.prototype.divmod;
BigInteger.prototype.divide = function (v) {
return divModAny(this, v)[0];
};
NativeBigInt.prototype.over = NativeBigInt.prototype.divide = function (v) {
return new NativeBigInt(this.value / parseValue(v).value);
};
SmallInteger.prototype.over = SmallInteger.prototype.divide = BigInteger.prototype.over = BigInteger.prototype.divide;
BigInteger.prototype.mod = function (v) {
return divModAny(this, v)[1];
};
NativeBigInt.prototype.mod = NativeBigInt.prototype.remainder = function (v) {
return new NativeBigInt(this.value % parseValue(v).value);
};
SmallInteger.prototype.remainder = SmallInteger.prototype.mod = BigInteger.prototype.remainder = BigInteger.prototype.mod;
BigInteger.prototype.pow = function (v) {
var n = parseValue(v),
a = this.value,
b = n.value,
value, x, y;
if (b === 0) return Integer[1];
if (a === 0) return Integer[0];
if (a === 1) return Integer[1];
if (a === -1) return n.isEven() ? Integer[1] : Integer[-1];
if (n.sign) {
return Integer[0];
}
if (!n.isSmall) throw new Error("The exponent " + n.toString() + " is too large.");
if (this.isSmall) {
if (isPrecise(value = Math.pow(a, b)))
return new SmallInteger(truncate(value));
}
x = this;
y = Integer[1];
while (true) {
if (b & 1 === 1) {
y = y.times(x);
--b;
}
if (b === 0) break;
b /= 2;
x = x.square();
}
return y;
};
SmallInteger.prototype.pow = BigInteger.prototype.pow;
NativeBigInt.prototype.pow = function (v) {
var n = parseValue(v);
var a = this.value, b = n.value;
var _0 = BigInt(0), _1 = BigInt(1), _2 = BigInt(2);
if (b === _0) return Integer[1];
if (a === _0) return Integer[0];
if (a === _1) return Integer[1];
if (a === BigInt(-1)) return n.isEven() ? Integer[1] : Integer[-1];
if (n.isNegative()) return new NativeBigInt(_0);
var x = this;
var y = Integer[1];
while (true) {
if ((b & _1) === _1) {
y = y.times(x);
--b;
}
if (b === _0) break;
b /= _2;
x = x.square();
}
return y;
};
BigInteger.prototype.modPow = function (exp, mod) {
exp = parseValue(exp);
mod = parseValue(mod);
if (mod.isZero()) throw new Error("Cannot take modPow with modulus 0");
var r = Integer[1],
base = this.mod(mod);
if (exp.isNegative()) {
exp = exp.multiply(Integer[-1]);
base = base.modInv(mod);
}
while (exp.isPositive()) {
if (base.isZero()) return Integer[0];
if (exp.isOdd()) r = r.multiply(base).mod(mod);
exp = exp.divide(2);
base = base.square().mod(mod);
}
return r;
};
NativeBigInt.prototype.modPow = SmallInteger.prototype.modPow = BigInteger.prototype.modPow;
function compareAbs(a, b) {
if (a.length !== b.length) {
return a.length > b.length ? 1 : -1;
}
for (var i = a.length - 1; i >= 0; i--) {
if (a[i] !== b[i]) return a[i] > b[i] ? 1 : -1;
}
return 0;
}
BigInteger.prototype.compareAbs = function (v) {
var n = parseValue(v),
a = this.value,
b = n.value;
if (n.isSmall) return 1;
return compareAbs(a, b);
};
SmallInteger.prototype.compareAbs = function (v) {
var n = parseValue(v),
a = Math.abs(this.value),
b = n.value;
if (n.isSmall) {
b = Math.abs(b);
return a === b ? 0 : a > b ? 1 : -1;
}
return -1;
};
NativeBigInt.prototype.compareAbs = function (v) {
var a = this.value;
var b = parseValue(v).value;
a = a >= 0 ? a : -a;
b = b >= 0 ? b : -b;
return a === b ? 0 : a > b ? 1 : -1;
};
BigInteger.prototype.compare = function (v) {
// See discussion about comparison with Infinity:
// https://github.com/peterolson/BigInteger.js/issues/61
if (v === Infinity) {
return -1;
}
if (v === -Infinity) {
return 1;
}
var n = parseValue(v),
a = this.value,
b = n.value;
if (this.sign !== n.sign) {
return n.sign ? 1 : -1;
}
if (n.isSmall) {
return this.sign ? -1 : 1;
}
return compareAbs(a, b) * (this.sign ? -1 : 1);
};
BigInteger.prototype.compareTo = BigInteger.prototype.compare;
SmallInteger.prototype.compare = function (v) {
if (v === Infinity) {
return -1;
}
if (v === -Infinity) {
return 1;
}
var n = parseValue(v),
a = this.value,
b = n.value;
if (n.isSmall) {
return a == b ? 0 : a > b ? 1 : -1;
}
if (a < 0 !== n.sign) {
return a < 0 ? -1 : 1;
}
return a < 0 ? 1 : -1;
};
SmallInteger.prototype.compareTo = SmallInteger.prototype.compare;
NativeBigInt.prototype.compare = function (v) {
if (v === Infinity) {
return -1;
}
if (v === -Infinity) {
return 1;
}
var a = this.value;
var b = parseValue(v).value;
return a === b ? 0 : a > b ? 1 : -1;
};
NativeBigInt.prototype.compareTo = NativeBigInt.prototype.compare;
BigInteger.prototype.equals = function (v) {
return this.compare(v) === 0;
};
NativeBigInt.prototype.eq = NativeBigInt.prototype.equals = SmallInteger.prototype.eq = SmallInteger.prototype.equals = BigInteger.prototype.eq = BigInteger.prototype.equals;
BigInteger.prototype.notEquals = function (v) {
return this.compare(v) !== 0;
};
NativeBigInt.prototype.neq = NativeBigInt.prototype.notEquals = SmallInteger.prototype.neq = SmallInteger.prototype.notEquals = BigInteger.prototype.neq = BigInteger.prototype.notEquals;
BigInteger.prototype.greater = function (v) {
return this.compare(v) > 0;
};
NativeBigInt.prototype.gt = NativeBigInt.prototype.greater = SmallInteger.prototype.gt = SmallInteger.prototype.greater = BigInteger.prototype.gt = BigInteger.prototype.greater;
BigInteger.prototype.lesser = function (v) {
return this.compare(v) < 0;
};
NativeBigInt.prototype.lt = NativeBigInt.prototype.lesser = SmallInteger.prototype.lt = SmallInteger.prototype.lesser = BigInteger.prototype.lt = BigInteger.prototype.lesser;
BigInteger.prototype.greaterOrEquals = function (v) {
return this.compare(v) >= 0;
};
NativeBigInt.prototype.geq = NativeBigInt.prototype.greaterOrEquals = SmallInteger.prototype.geq = SmallInteger.prototype.greaterOrEquals = BigInteger.prototype.geq = BigInteger.prototype.greaterOrEquals;
BigInteger.prototype.lesserOrEquals = function (v) {
return this.compare(v) <= 0;
};
NativeBigInt.prototype.leq = NativeBigInt.prototype.lesserOrEquals = SmallInteger.prototype.leq = SmallInteger.prototype.lesserOrEquals = BigInteger.prototype.leq = BigInteger.prototype.lesserOrEquals;
BigInteger.prototype.isEven = function () {
return (this.value[0] & 1) === 0;
};
SmallInteger.prototype.isEven = function () {
return (this.value & 1) === 0;
};
NativeBigInt.prototype.isEven = function () {
return (this.value & BigInt(1)) === BigInt(0);
};
BigInteger.prototype.isOdd = function () {
return (this.value[0] & 1) === 1;
};
SmallInteger.prototype.isOdd = function () {
return (this.value & 1) === 1;
};
NativeBigInt.prototype.isOdd = function () {
return (this.value & BigInt(1)) === BigInt(1);
};
BigInteger.prototype.isPositive = function () {
return !this.sign;
};
SmallInteger.prototype.isPositive = function () {
return this.value > 0;
};
NativeBigInt.prototype.isPositive = SmallInteger.prototype.isPositive;
BigInteger.prototype.isNegative = function () {
return this.sign;
};
SmallInteger.prototype.isNegative = function () {
return this.value < 0;
};
NativeBigInt.prototype.isNegative = SmallInteger.prototype.isNegative;
BigInteger.prototype.isUnit = function () {
return false;
};
SmallInteger.prototype.isUnit = function () {
return Math.abs(this.value) === 1;
};
NativeBigInt.prototype.isUnit = function () {
return this.abs().value === BigInt(1);
};
BigInteger.prototype.isZero = function () {
return false;
};
SmallInteger.prototype.isZero = function () {
return this.value === 0;
};
NativeBigInt.prototype.isZero = function () {
return this.value === BigInt(0);
};
BigInteger.prototype.isDivisibleBy = function (v) {
var n = parseValue(v);
if (n.isZero()) return false;
if (n.isUnit()) return true;
if (n.compareAbs(2) === 0) return this.isEven();
return this.mod(n).isZero();
};
NativeBigInt.prototype.isDivisibleBy = SmallInteger.prototype.isDivisibleBy = BigInteger.prototype.isDivisibleBy;
function isBasicPrime(v) {
var n = v.abs();
if (n.isUnit()) return false;
if (n.equals(2) || n.equals(3) || n.equals(5)) return true;
if (n.isEven() || n.isDivisibleBy(3) || n.isDivisibleBy(5)) return false;
if (n.lesser(49)) return true;
// we don't know if it's prime: let the other functions figure it out
}
function millerRabinTest(n, a) {
var nPrev = n.prev(),
b = nPrev,
r = 0,
d, i, x;
while (b.isEven()) b = b.divide(2), r++;
next: for (i = 0; i < a.length; i++) {
if (n.lesser(a[i])) continue;
x = bigInt(a[i]).modPow(b, n);
if (x.isUnit() || x.equals(nPrev)) continue;
for (d = r - 1; d != 0; d--) {
x = x.square().mod(n);
if (x.isUnit()) return false;
if (x.equals(nPrev)) continue next;
}
return false;
}
return true;
}
// Set "strict" to true to force GRH-supported lower bound of 2*log(N)^2
BigInteger.prototype.isPrime = function (strict) {
var isPrime = isBasicPrime(this);
if (isPrime !== undefined$1) return isPrime;
var n = this.abs();
var bits = n.bitLength();
if (bits <= 64)
return millerRabinTest(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]);
var logN = Math.log(2) * bits.toJSNumber();
var t = Math.ceil((strict === true) ? (2 * Math.pow(logN, 2)) : logN);
for (var a = [], i = 0; i < t; i++) {
a.push(bigInt(i + 2));
}
return millerRabinTest(n, a);
};
NativeBigInt.prototype.isPrime = SmallInteger.prototype.isPrime = BigInteger.prototype.isPrime;
BigInteger.prototype.isProbablePrime = function (iterations, rng) {
var isPrime = isBasicPrime(this);
if (isPrime !== undefined$1) return isPrime;
var n = this.abs();
var t = iterations === undefined$1 ? 5 : iterations;
for (var a = [], i = 0; i < t; i++) {
a.push(bigInt.randBetween(2, n.minus(2), rng));
}
return millerRabinTest(n, a);
};
NativeBigInt.prototype.isProbablePrime = SmallInteger.prototype.isProbablePrime = BigInteger.prototype.isProbablePrime;
BigInteger.prototype.modInv = function (n) {
var t = bigInt.zero, newT = bigInt.one, r = parseValue(n), newR = this.abs(), q, lastT, lastR;
while (!newR.isZero()) {
q = r.divide(newR);
lastT = t;
lastR = r;
t = newT;
r = newR;
newT = lastT.subtract(q.multiply(newT));
newR = lastR.subtract(q.multiply(newR));
}
if (!r.isUnit()) throw new Error(this.toString() + " and " + n.toString() + " are not co-prime");
if (t.compare(0) === -1) {
t = t.add(n);
}
if (this.isNegative()) {
return t.negate();
}
return t;
};
NativeBigInt.prototype.modInv = SmallInteger.prototype.modInv = BigInteger.prototype.modInv;
BigInteger.prototype.next = function () {
var value = this.value;
if (this.sign) {
return subtractSmall(value, 1, this.sign);
}
return new BigInteger(addSmall(value, 1), this.sign);
};
SmallInteger.prototype.next = function () {
var value = this.value;
if (value + 1 < MAX_INT) return new SmallInteger(value + 1);
return new BigInteger(MAX_INT_ARR, false);
};
NativeBigInt.prototype.next = function () {
return new NativeBigInt(this.value + BigInt(1));
};
BigInteger.prototype.prev = function () {
var value = this.value;
if (this.sign) {
return new BigInteger(addSmall(value, 1), true);
}
return subtractSmall(value, 1, this.sign);
};
SmallInteger.prototype.prev = function () {
var value = this.value;
if (value - 1 > -MAX_INT) return new SmallInteger(value - 1);
return new BigInteger(MAX_INT_ARR, true);
};
NativeBigInt.prototype.prev = function () {
return new NativeBigInt(this.value - BigInt(1));
};
var powersOfTwo = [1];
while (2 * powersOfTwo[powersOfTwo.length - 1] <= BASE) powersOfTwo.push(2 * powersOfTwo[powersOfTwo.length - 1]);
var powers2Length = powersOfTwo.length, highestPower2 = powersOfTwo[powers2Length - 1];
function shift_isSmall(n) {
return Math.abs(n) <= BASE;
}
BigInteger.prototype.shiftLeft = function (v) {
var n = parseValue(v).toJSNumber();
if (!shift_isSmall(n)) {
throw new Error(String(n) + " is too large for shifting.");
}
if (n < 0) return this.shiftRight(-n);
var result = this;
if (result.isZero()) return result;
while (n >= powers2Length) {
result = result.multiply(highestPower2);
n -= powers2Length - 1;
}
return result.multiply(powersOfTwo[n]);
};
NativeBigInt.prototype.shiftLeft = SmallInteger.prototype.shiftLeft = BigInteger.prototype.shiftLeft;
BigInteger.prototype.shiftRight = function (v) {
var remQuo;
var n = parseValue(v).toJSNumber();
if (!shift_isSmall(n)) {
throw new Error(String(n) + " is too large for shifting.");
}
if (n < 0) return this.shiftLeft(-n);
var result = this;
while (n >= powers2Length) {
if (result.isZero() || (result.isNegative() && result.isUnit())) return result;
remQuo = divModAny(result, highestPower2);
result = remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
n -= powers2Length - 1;
}
remQuo = divModAny(result, powersOfTwo[n]);
return remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
};
NativeBigInt.prototype.shiftRight = SmallInteger.prototype.shiftRight = BigInteger.prototype.shiftRight;
function bitwise(x, y, fn) {
y = parseValue(y);
var xSign = x.isNegative(), ySign = y.isNegative();
var xRem = xSign ? x.not() : x,
yRem = ySign ? y.not() : y;
var xDigit = 0, yDigit = 0;
var xDivMod = null, yDivMod = null;
var result = [];
while (!xRem.isZero() || !yRem.isZero()) {
xDivMod = divModAny(xRem, highestPower2);
xDigit = xDivMod[1].toJSNumber();
if (xSign) {
xDigit = highestPower2 - 1 - xDigit; // two's complement for negative numbers
}
yDivMod = divModAny(yRem, highestPower2);
yDigit = yDivMod[1].toJSNumber();
if (ySign) {
yDigit = highestPower2 - 1 - yDigit; // two's complement for negative numbers
}
xRem = xDivMod[0];
yRem = yDivMod[0];
result.push(fn(xDigit, yDigit));
}
var sum = fn(xSign ? 1 : 0, ySign ? 1 : 0) !== 0 ? bigInt(-1) : bigInt(0);
for (var i = result.length - 1; i >= 0; i -= 1) {
sum = sum.multiply(highestPower2).add(bigInt(result[i]));
}
return sum;
}
BigInteger.prototype.not = function () {
return this.negate().prev();
};
NativeBigInt.prototype.not = SmallInteger.prototype.not = BigInteger.prototype.not;
BigInteger.prototype.and = function (n) {
return bitwise(this, n, function (a, b) { return a & b; });
};
NativeBigInt.prototype.and = SmallInteger.prototype.and = BigInteger.prototype.and;
BigInteger.prototype.or = function (n) {
return bitwise(this, n, function (a, b) { return a | b; });
};
NativeBigInt.prototype.or = SmallInteger.prototype.or = BigInteger.prototype.or;
BigInteger.prototype.xor = function (n) {
return bitwise(this, n, function (a, b) { return a ^ b; });
};
NativeBigInt.prototype.xor = SmallInteger.prototype.xor = BigInteger.prototype.xor;
var LOBMASK_I = 1 << 30, LOBMASK_BI = (BASE & -BASE) * (BASE & -BASE) | LOBMASK_I;
function roughLOB(n) { // get lowestOneBit (rough)
// SmallInteger: return Min(lowestOneBit(n), 1 << 30)
// BigInteger: return Min(lowestOneBit(n), 1 << 14) [BASE=1e7]
var v = n.value,
x = typeof v === "number" ? v | LOBMASK_I :
typeof v === "bigint" ? v | BigInt(LOBMASK_I) :
v[0] + v[1] * BASE | LOBMASK_BI;
return x & -x;
}
function integerLogarithm(value, base) {
if (base.compareTo(value) <= 0) {
var tmp = integerLogarithm(value, base.square(base));
var p = tmp.p;
var e = tmp.e;
var t = p.multiply(base);
return t.compareTo(value) <= 0 ? { p: t, e: e * 2 + 1 } : { p: p, e: e * 2 };
}
return { p: bigInt(1), e: 0 };
}
BigInteger.prototype.bitLength = function () {
var n = this;
if (n.compareTo(bigInt(0)) < 0) {
n = n.negate().subtract(bigInt(1));
}
if (n.compareTo(bigInt(0)) === 0) {
return bigInt(0);
}
return bigInt(integerLogarithm(n, bigInt(2)).e).add(bigInt(1));
};
NativeBigInt.prototype.bitLength = SmallInteger.prototype.bitLength = BigInteger.prototype.bitLength;
function max(a, b) {
a = parseValue(a);
b = parseValue(b);
return a.greater(b) ? a : b;
}
function min(a, b) {
a = parseValue(a);
b = parseValue(b);
return a.lesser(b) ? a : b;
}
function gcd(a, b) {
a = parseValue(a).abs();
b = parseValue(b).abs();
if (a.equals(b)) return a;
if (a.isZero()) return b;
if (b.isZero()) return a;
var c = Integer[1], d, t;
while (a.isEven() && b.isEven()) {
d = min(roughLOB(a), roughLOB(b));
a = a.divide(d);
b = b.divide(d);
c = c.multiply(d);
}
while (a.isEven()) {
a = a.divide(roughLOB(a));
}
do {
while (b.isEven()) {
b = b.divide(roughLOB(b));
}
if (a.greater(b)) {
t = b; b = a; a = t;
}
b = b.subtract(a);
} while (!b.isZero());
return c.isUnit() ? a : a.multiply(c);
}
function lcm(a, b) {
a = parseValue(a).abs();
b = parseValue(b).abs();
return a.divide(gcd(a, b)).multiply(b);
}
function randBetween(a, b, rng) {
a = parseValue(a);
b = parseValue(b);
var usedRNG = rng || Math.random;
var low = min(a, b), high = max(a, b);
var range = high.subtract(low).add(1);
if (range.isSmall) return low.add(Math.floor(usedRNG() * range));
var digits = toBase(range, BASE).value;
var result = [], restricted = true;
for (var i = 0; i < digits.length; i++) {
var top = restricted ? digits[i] : BASE;
var digit = truncate(usedRNG() * top);
result.push(digit);
if (digit < top) restricted = false;
}
return low.add(Integer.fromArray(result, BASE, false));
}
var parseBase = function (text, base, alphabet, caseSensitive) {
alphabet = alphabet || DEFAULT_ALPHABET;
text = String(text);
if (!caseSensitive) {
text = text.toLowerCase();
alphabet = alphabet.toLowerCase();
}
var length = text.length;
var i;
var absBase = Math.abs(base);
var alphabetValues = {};
for (i = 0; i < alphabet.length; i++) {
alphabetValues[alphabet[i]] = i;
}
for (i = 0; i < length; i++) {
var c = text[i];
if (c === "-") continue;
if (c in alphabetValues) {
if (alphabetValues[c] >= absBase) {
if (c === "1" && absBase === 1) continue;
throw new Error(c + " is not a valid digit in base " + base + ".");
}
}
}
base = parseValue(base);
var digits = [];
var isNegative = text[0] === "-";
for (i = isNegative ? 1 : 0; i < text.length; i++) {
var c = text[i];
if (c in alphabetValues) digits.push(parseValue(alphabetValues[c]));
else if (c === "<") {
var start = i;
do { i++; } while (text[i] !== ">" && i < text.length);
digits.push(parseValue(text.slice(start + 1, i)));
}
else throw new Error(c + " is not a valid character");
}
return parseBaseFromArray(digits, base, isNegative);
};
function parseBaseFromArray(digits, base, isNegative) {
var val = Integer[0], pow = Integer[1], i;
for (i = digits.length - 1; i >= 0; i--) {
val = val.add(digits[i].times(pow));
pow = pow.times(base);
}
return isNegative ? val.negate() : val;
}
function stringify(digit, alphabet) {
alphabet = alphabet || DEFAULT_ALPHABET;
if (digit < alphabet.length) {
return alphabet[digit];
}
return "<" + digit + ">";
}
function toBase(n, base) {
base = bigInt(base);
if (base.isZero()) {
if (n.isZero()) return { value: [0], isNegative: false };
throw new Error("Cannot convert nonzero numbers to base 0.");
}
if (base.equals(-1)) {
if (n.isZero()) return { value: [0], isNegative: false };
if (n.isNegative())
return {
value: [].concat.apply([], Array.apply(null, Array(-n.toJSNumber()))
.map(Array.prototype.valueOf, [1, 0])
),
isNegative: false
};
var arr = Array.apply(null, Array(n.toJSNumber() - 1))
.map(Array.prototype.valueOf, [0, 1]);
arr.unshift([1]);
return {
value: [].concat.apply([], arr),
isNegative: false
};
}
var neg = false;
if (n.isNegative() && base.isPositive()) {
neg = true;
n = n.abs();
}
if (base.isUnit()) {
if (n.isZero()) return { value: [0], isNegative: false };
return {
value: Array.apply(null, Array(n.toJSNumber()))
.map(Number.prototype.valueOf, 1),
isNegative: neg
};
}
var out = [];
var left = n, divmod;
while (left.isNegative() || left.compareAbs(base) >= 0) {
divmod = left.divmod(base);
left = divmod.quotient;
var digit = divmod.remainder;
if (digit.isNegative()) {
digit = base.minus(digit).abs();
left = left.next();
}
out.push(digit.toJSNumber());
}
out.push(left.toJSNumber());
return { value: out.reverse(), isNegative: neg };
}
function toBaseString(n, base, alphabet) {
var arr = toBase(n, base);
return (arr.isNegative ? "-" : "") + arr.value.map(function (x) {
return stringify(x, alphabet);
}).join('');
}
BigInteger.prototype.toArray = function (radix) {
return toBase(this, radix);
};
SmallInteger.prototype.toArray = function (radix) {
return toBase(this, radix);
};
NativeBigInt.prototype.toArray = function (radix) {
return toBase(this, radix);
};
BigInteger.prototype.toString = function (radix, alphabet) {
if (radix === undefined$1) radix = 10;
if (radix !== 10) return toBaseString(this, radix, alphabet);
var v = this.value, l = v.length, str = String(v[--l]), zeros = "0000000", digit;
while (--l >= 0) {
digit = String(v[l]);
str += zeros.slice(digit.length) + digit;
}
var sign = this.sign ? "-" : "";
return sign + str;
};
SmallInteger.prototype.toString = function (radix, alphabet) {
if (radix === undefined$1) radix = 10;
if (radix != 10) return toBaseString(this, radix, alphabet);
return String(this.value);
};
NativeBigInt.prototype.toString = SmallInteger.prototype.toString;
NativeBigInt.prototype.toJSON = BigInteger.prototype.toJSON = SmallInteger.prototype.toJSON = function () { return this.toString(); };
BigInteger.prototype.valueOf = function () {
return parseInt(this.toString(), 10);
};
BigInteger.prototype.toJSNumber = BigInteger.prototype.valueOf;
SmallInteger.prototype.valueOf = function () {
return this.value;
};
SmallInteger.prototype.toJSNumber = SmallInteger.prototype.valueOf;
NativeBigInt.prototype.valueOf = NativeBigInt.prototype.toJSNumber = function () {
return parseInt(this.toString(), 10);
};
function parseStringValue(v) {
if (isPrecise(+v)) {
var x = +v;
if (x === truncate(x))
return supportsNativeBigInt ? new NativeBigInt(BigInt(x)) : new SmallInteger(x);
throw new Error("Invalid integer: " + v);
}
var sign = v[0] === "-";
if (sign) v = v.slice(1);
var split = v.split(/e/i);
if (split.length > 2) throw new Error("Invalid integer: " + split.join("e"));
if (split.length === 2) {
var exp = split[1];
if (exp[0] === "+") exp = exp.slice(1);
exp = +exp;
if (exp !== truncate(exp) || !isPrecise(exp)) throw new Error("Invalid integer: " + exp + " is not a valid exponent.");
var text = split[0];
var decimalPlace = text.indexOf(".");
if (decimalPlace >= 0) {
exp -= text.length - decimalPlace - 1;
text = text.slice(0, decimalPlace) + text.slice(decimalPlace + 1);
}
if (exp < 0) throw new Error("Cannot include negative exponent part for integers");
text += (new Array(exp + 1)).join("0");
v = text;
}
var isValid = /^([0-9][0-9]*)$/.test(v);
if (!isValid) throw new Error("Invalid integer: " + v);
if (supportsNativeBigInt) {
return new NativeBigInt(BigInt(sign ? "-" + v : v));
}
var r = [], max = v.length, l = LOG_BASE, min = max - l;
while (max > 0) {
r.push(+v.slice(min, max));
min -= l;
if (min < 0) min = 0;
max -= l;
}
trim(r);
return new BigInteger(r, sign);
}
function parseNumberValue(v) {
if (supportsNativeBigInt) {
return new NativeBigInt(BigInt(v));
}
if (isPrecise(v)) {
if (v !== truncate(v)) throw new Error(v + " is not an integer.");
return new SmallInteger(v);
}
return parseStringValue(v.toString());
}
function parseValue(v) {
if (typeof v === "number") {
return parseNumberValue(v);
}
if (typeof v === "string") {
return parseStringValue(v);
}
if (typeof v === "bigint") {
return new NativeBigInt(v);
}
return v;
}
// Pre-define numbers in range [-999,999]
for (var i = 0; i < 1000; i++) {
Integer[i] = parseValue(i);
if (i > 0) Integer[-i] = parseValue(-i);
}
// Backwards compatibility
Integer.one = Integer[1];
Integer.zero = Integer[0];
Integer.minusOne = Integer[-1];
Integer.max = max;
Integer.min = min;
Integer.gcd = gcd;
Integer.lcm = lcm;
Integer.isInstance = function (x) { return x instanceof BigInteger || x instanceof SmallInteger || x instanceof NativeBigInt; };
Integer.randBetween = randBetween;
Integer.fromArray = function (digits, base, isNegative) {
return parseBaseFromArray(digits.map(parseValue), parseValue(base || 10), isNegative);
};
return Integer;
})();
// Node.js check
if ( module.hasOwnProperty("exports")) {
module.exports = bigInt;
}
});
function fromString$1(s, radix) {
if (typeof s == "string") {
if (s.slice(0,2) == "0x") {
return BigInteger(s.slice(2), 16);
} else {
return BigInteger(s,radix);
}
} else {
return BigInteger(s, radix);
}
}
const e$1 = fromString$1;
function fromArray$1(a, radix) {
return BigInteger.fromArray(a, radix);
}
function bitLength$1(a) {
return BigInteger(a).bitLength();
}
function isNegative$1(a) {
return BigInteger(a).isNegative();
}
function isZero$1(a) {
return BigInteger(a).isZero();
}
function shiftLeft$1(a, n) {
return BigInteger(a).shiftLeft(n);
}
function shiftRight$1(a, n) {
return BigInteger(a).shiftRight(n);
}
const shl$1 = shiftLeft$1;
const shr$1 = shiftRight$1;
function isOdd$1(a) {
return BigInteger(a).isOdd();
}
function naf$1(n) {
let E = BigInteger(n);
const res = [];
while (E.gt(BigInteger.zero)) {
if (E.isOdd()) {
const z = 2 - E.mod(4).toJSNumber();
res.push( z );
E = E.minus(z);
} else {
res.push( 0 );
}
E = E.shiftRight(1);
}
return res;
}
function bits$1(n) {
let E = BigInteger(n);
const res = [];
while (E.gt(BigInteger.zero)) {
if (E.isOdd()) {
res.push(1);
} else {
res.push( 0 );
}
E = E.shiftRight(1);
}
return res;
}
function toNumber$1(s) {
if (!s.lt(BigInteger("9007199254740992", 10))) {
throw new Error("Number too big");
}
return s.toJSNumber();
}
function toArray$1(s, radix) {
return BigInteger(s).toArray(radix);
}
function add$1(a, b) {
return BigInteger(a).add(BigInteger(b));
}
function sub$1(a, b) {
return BigInteger(a).minus(BigInteger(b));
}
function neg$1(a) {
return BigInteger.zero.minus(BigInteger(a));
}
function mul$1(a, b) {
return BigInteger(a).times(BigInteger(b));
}
function square$1(a) {
return BigInteger(a).square();
}
function pow$1(a, b) {
return BigInteger(a).pow(BigInteger(b));
}
function exp$1(a, b) {
return BigInteger(a).pow(BigInteger(b));
}
function abs$1(a) {
return BigInteger(a).abs();
}
function div$1(a, b) {
return BigInteger(a).divide(BigInteger(b));
}
function mod$1(a, b) {
return BigInteger(a).mod(BigInteger(b));
}
function eq$1(a, b) {
return BigInteger(a).eq(BigInteger(b));
}
function neq$1(a, b) {
return BigInteger(a).neq(BigInteger(b));
}
function lt$1(a, b) {
return BigInteger(a).lt(BigInteger(b));
}
function gt$1(a, b) {
return BigInteger(a).gt(BigInteger(b));
}
function leq$1(a, b) {
return BigInteger(a).leq(BigInteger(b));
}
function geq$1(a, b) {
return BigInteger(a).geq(BigInteger(b));
}
function band$1(a, b) {
return BigInteger(a).and(BigInteger(b));
}
function bor$1(a, b) {
return BigInteger(a).or(BigInteger(b));
}
function bxor$1(a, b) {
return BigInteger(a).xor(BigInteger(b));
}
function land$1(a, b) {
return (!BigInteger(a).isZero()) && (!BigInteger(b).isZero());
}
function lor$1(a, b) {
return (!BigInteger(a).isZero()) || (!BigInteger(b).isZero());
}
function lnot$1(a) {
return BigInteger(a).isZero();
}
var Scalar_bigint = /*#__PURE__*/Object.freeze({
__proto__: null,
fromString: fromString$1,
e: e$1,
fromArray: fromArray$1,
bitLength: bitLength$1,
isNegative: isNegative$1,
isZero: isZero$1,
shiftLeft: shiftLeft$1,
shiftRight: shiftRight$1,
shl: shl$1,
shr: shr$1,
isOdd: isOdd$1,
naf: naf$1,
bits: bits$1,
toNumber: toNumber$1,
toArray: toArray$1,
add: add$1,
sub: sub$1,
neg: neg$1,
mul: mul$1,
square: square$1,
pow: pow$1,
exp: exp$1,
abs: abs$1,
div: div$1,
mod: mod$1,
eq: eq$1,
neq: neq$1,
lt: lt$1,
gt: gt$1,
leq: leq$1,
geq: geq$1,
band: band$1,
bor: bor$1,
bxor: bxor$1,
land: land$1,
lor: lor$1,
lnot: lnot$1
});
const supportsNativeBigInt = typeof BigInt === "function";
let Scalar = {};
if (supportsNativeBigInt) {
Object.assign(Scalar, Scalar_native);
} else {
Object.assign(Scalar, Scalar_bigint);
}
// Returns a buffer with Little Endian Representation
Scalar.toRprLE = function rprBE(buff, o, e, n8) {
const s = "0000000" + e.toString(16);
const v = new Uint32Array(buff.buffer, o, n8/4);
const l = (((s.length-7)*4 - 1) >> 5)+1; // Number of 32bit words;
for (let i=0; i<l; i++) v[i] = parseInt(s.substring(s.length-8*i-8, s.length-8*i), 16);
for (let i=l; i<v.length; i++) v[i] = 0;
for (let i=v.length*4; i<n8; i++) buff[i] = Scalar.toNumber(Scalar.band(Scalar.shiftRight(e, i*8), 0xFF));
};
// Returns a buffer with Big Endian Representation
Scalar.toRprBE = function rprLEM(buff, o, e, n8) {
const s = "0000000" + e.toString(16);
2020-08-21 19:29:55 +03:00
const v = new DataView(buff.buffer, buff.byteOffset + o, n8);
2020-07-11 11:31:52 +03:00
const l = (((s.length-7)*4 - 1) >> 5)+1; // Number of 32bit words;
for (let i=0; i<l; i++) v.setUint32(n8-i*4 -4, parseInt(s.substring(s.length-8*i-8, s.length-8*i), 16), false);
for (let i=0; i<n8/4-l; i++) v[i] = 0;
};
// Pases a buffer with Little Endian Representation
Scalar.fromRprLE = function rprLEM(buff, o, n8) {
n8 = n8 || buff.byteLength;
const v = new Uint32Array(buff.buffer, o, n8/4);
const a = new Array(n8/4);
v.forEach( (ch,i) => a[a.length-i-1] = ch.toString(16).padStart(8,"0") );
return Scalar.fromString(a.join(""), 16);
};
// Pases a buffer with Big Endian Representation
Scalar.fromRprBE = function rprLEM(buff, o, n8) {
n8 = n8 || buff.byteLength;
2020-08-21 19:29:55 +03:00
const v = new DataView(buff.buffer, buff.byteOffset + o, n8);
2020-07-11 11:31:52 +03:00
const a = new Array(n8/4);
for (let i=0; i<n8/4; i++) {
a[i] = v.getUint32(i*4, false).toString(16).padStart(8, "0");
}
return Scalar.fromString(a.join(""), 16);
};
Scalar.toString = function toString(a, radix) {
return a.toString(radix);
};
Scalar.toLEBuff = function toLEBuff(a) {
const buff = new Uint8Array(Math.floor((Scalar.bitLength(a) - 1) / 8) +1);
Scalar.toRprLE(buff, 0, a, buff.byteLength);
return buff;
};
Scalar.zero = Scalar.e(0);
Scalar.one = Scalar.e(1);
let {
toRprLE,
toRprBE,
fromRprLE,
fromRprBE,
toString,
toLEBuff,
zero,
one,
fromString: fromString$2,
e: e$2,
fromArray: fromArray$2,
bitLength: bitLength$2,
isNegative: isNegative$2,
isZero: isZero$2,
shiftLeft: shiftLeft$2,
shiftRight: shiftRight$2,
shl: shl$2,
shr: shr$2,
isOdd: isOdd$2,
naf: naf$2,
bits: bits$2,
toNumber: toNumber$2,
toArray: toArray$2,
add: add$2,
sub: sub$2,
neg: neg$2,
mul: mul$2,
square: square$2,
pow: pow$2,
exp: exp$2,
abs: abs$2,
div: div$2,
mod: mod$2,
eq: eq$2,
neq: neq$2,
lt: lt$2,
gt: gt$2,
leq: leq$2,
geq: geq$2,
band: band$2,
bor: bor$2,
bxor: bxor$2,
land: land$2,
lor: lor$2,
lnot: lnot$2,
} = Scalar;
var _Scalar = /*#__PURE__*/Object.freeze({
__proto__: null,
toRprLE: toRprLE,
toRprBE: toRprBE,
fromRprLE: fromRprLE,
fromRprBE: fromRprBE,
toString: toString,
toLEBuff: toLEBuff,
zero: zero,
one: one,
fromString: fromString$2,
e: e$2,
fromArray: fromArray$2,
bitLength: bitLength$2,
isNegative: isNegative$2,
isZero: isZero$2,
shiftLeft: shiftLeft$2,
shiftRight: shiftRight$2,
shl: shl$2,
shr: shr$2,
isOdd: isOdd$2,
naf: naf$2,
bits: bits$2,
toNumber: toNumber$2,
toArray: toArray$2,
add: add$2,
sub: sub$2,
neg: neg$2,
mul: mul$2,
square: square$2,
pow: pow$2,
exp: exp$2,
abs: abs$2,
div: div$2,
mod: mod$2,
eq: eq$2,
neq: neq$2,
lt: lt$2,
gt: gt$2,
leq: leq$2,
geq: geq$2,
band: band$2,
bor: bor$2,
bxor: bxor$2,
land: land$2,
lor: lor$2,
lnot: lnot$2
});
/*
Copyright 2018 0kims association.
This file is part of snarkjs.
snarkjs is a free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.
snarkjs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
snarkjs. If not, see <https://www.gnu.org/licenses/>.
*/
2020-08-29 15:12:24 +03:00
const _revTable = [];
for (let i=0; i<256; i++) {
_revTable[i] = _revSlow(i, 8);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function _revSlow(idx, bits) {
let res =0;
let a = idx;
for (let i=0; i<bits; i++) {
res <<= 1;
res = res | (a &1);
a >>=1;
}
return res;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
/*
Copyright 2018 0kims association.
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
This file is part of snarkjs.
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
snarkjs is a free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
snarkjs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
You should have received a copy of the GNU General Public License along with
snarkjs. If not, see <https://www.gnu.org/licenses/>.
*/
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
/*
exports.mulScalar = (F, base, e) =>{
let res = F.zero;
let rem = bigInt(e);
let exp = base;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
while (! rem.eq(bigInt.zero)) {
if (rem.and(bigInt.one).eq(bigInt.one)) {
res = F.add(res, exp);
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
exp = F.double(exp);
rem = rem.shiftRight(1);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return res;
};
*/
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function exp$3(F, base, e) {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (isZero$2(e)) return F.one;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const n = bits$2(e);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (n.legth==0) return F.one;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let res = base;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
for (let i=n.length-2; i>=0; i--) {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
res = F.square(res);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (n[i]) {
res = F.mul(res, base);
2020-08-24 10:32:47 +03:00
}
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return res;
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
// Check here: https://eprint.iacr.org/2012/685.pdf
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
function buildSqrt (F) {
if ((F.m % 2) == 1) {
if (eq$2(mod$2(F.p, 4), 1 )) {
if (eq$2(mod$2(F.p, 8), 1 )) {
if (eq$2(mod$2(F.p, 16), 1 )) {
// alg7_muller(F);
alg5_tonelliShanks(F);
} else if (eq$2(mod$2(F.p, 16), 9 )) {
alg4_kong(F);
} else {
throw new Error("Field withot sqrt");
}
} else if (eq$2(mod$2(F.p, 8), 5 )) {
alg3_atkin(F);
} else {
throw new Error("Field withot sqrt");
}
} else if (eq$2(mod$2(F.p, 4), 3 )) {
alg2_shanks(F);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
} else {
const pm2mod4 = mod$2(pow$2(F.p, F.m/2), 4);
if (pm2mod4 == 1) {
alg10_adj(F);
} else if (pm2mod4 == 3) {
alg9_adj(F);
} else {
alg8_complex(F);
2020-07-11 11:31:52 +03:00
}
}
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function alg5_tonelliShanks(F) {
F.sqrt_q = pow$2(F.p, F.m);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
F.sqrt_s = 0;
F.sqrt_t = sub$2(F.sqrt_q, 1);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
while (!isOdd$2(F.sqrt_t)) {
F.sqrt_s = F.sqrt_s + 1;
F.sqrt_t = div$2(F.sqrt_t, 2);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let c0 = F.one;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
while (F.eq(c0, F.one)) {
const c = F.random();
F.sqrt_z = F.pow(c, F.sqrt_t);
2020-09-25 08:38:22 +03:00
c0 = F.pow(F.sqrt_z, 2 ** (F.sqrt_s-1) );
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
F.sqrt_tm1d2 = div$2(sub$2(F.sqrt_t, 1),2);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
F.sqrt = function(a) {
const F=this;
if (F.isZero(a)) return F.zero;
let w = F.pow(a, F.sqrt_tm1d2);
2020-09-25 08:38:22 +03:00
const a0 = F.pow( F.mul(F.square(w), a), 2 ** (F.sqrt_s-1) );
2020-08-29 15:12:24 +03:00
if (F.eq(a0, F.negone)) return null;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let v = F.sqrt_s;
let x = F.mul(a, w);
let b = F.mul(x, w);
let z = F.sqrt_z;
while (!F.eq(b, F.one)) {
let b2k = F.square(b);
let k=1;
while (!F.eq(b2k, F.one)) {
b2k = F.square(b2k);
k++;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
w = z;
for (let i=0; i<v-k-1; i++) {
w = F.square(w);
}
z = F.square(w);
b = F.mul(b, z);
x = F.mul(x, w);
v = k;
}
return F.geq(x, F.zero) ? x : F.neg(x);
};
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function alg4_kong(F) {
F.sqrt = function() {
throw new Error("Sqrt alg 4 not implemented");
};
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function alg3_atkin(F) {
F.sqrt = function() {
throw new Error("Sqrt alg 3 not implemented");
};
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function alg2_shanks(F) {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
F.sqrt_q = pow$2(F.p, F.m);
F.sqrt_e1 = div$2( sub$2(F.sqrt_q, 3) , 4);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
F.sqrt = function(a) {
if (this.isZero(a)) return this.zero;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
// Test that have solution
const a1 = this.pow(a, this.sqrt_e1);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const a0 = this.mul(this.square(a1), a);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if ( this.eq(a0, this.negone) ) return null;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const x = this.mul(a1, a);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return F.geq(x, F.zero) ? x : F.neg(x);
};
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function alg10_adj(F) {
F.sqrt = function() {
throw new Error("Sqrt alg 10 not implemented");
};
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function alg9_adj(F) {
F.sqrt_q = pow$2(F.p, F.m/2);
F.sqrt_e34 = div$2( sub$2(F.sqrt_q, 3) , 4);
F.sqrt_e12 = div$2( sub$2(F.sqrt_q, 1) , 2);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
F.frobenius = function(n, x) {
if ((n%2) == 1) {
return F.conjugate(x);
} else {
return x;
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
F.sqrt = function(a) {
const F = this;
const a1 = F.pow(a, F.sqrt_e34);
const alfa = F.mul(F.square(a1), a);
const a0 = F.mul(F.frobenius(1, alfa), alfa);
if (F.eq(a0, F.negone)) return null;
const x0 = F.mul(a1, a);
let x;
if (F.eq(alfa, F.negone)) {
x = F.mul(x0, [F.F.zero, F.F.one]);
} else {
const b = F.pow(F.add(F.one, alfa), F.sqrt_e12);
x = F.mul(b, x0);
}
return F.geq(x, F.zero) ? x : F.neg(x);
};
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function alg8_complex(F) {
F.sqrt = function() {
throw new Error("Sqrt alg 8 not implemented");
};
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function quarterRound(st, a, b, c, d) {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
st[a] = (st[a] + st[b]) >>> 0;
st[d] = (st[d] ^ st[a]) >>> 0;
st[d] = ((st[d] << 16) | ((st[d]>>>16) & 0xFFFF)) >>> 0;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
st[c] = (st[c] + st[d]) >>> 0;
st[b] = (st[b] ^ st[c]) >>> 0;
st[b] = ((st[b] << 12) | ((st[b]>>>20) & 0xFFF)) >>> 0;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
st[a] = (st[a] + st[b]) >>> 0;
st[d] = (st[d] ^ st[a]) >>> 0;
st[d] = ((st[d] << 8) | ((st[d]>>>24) & 0xFF)) >>> 0;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
st[c] = (st[c] + st[d]) >>> 0;
st[b] = (st[b] ^ st[c]) >>> 0;
st[b] = ((st[b] << 7) | ((st[b]>>>25) & 0x7F)) >>> 0;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function doubleRound(st) {
quarterRound(st, 0, 4, 8,12);
quarterRound(st, 1, 5, 9,13);
quarterRound(st, 2, 6,10,14);
quarterRound(st, 3, 7,11,15);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
quarterRound(st, 0, 5,10,15);
quarterRound(st, 1, 6,11,12);
quarterRound(st, 2, 7, 8,13);
quarterRound(st, 3, 4, 9,14);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
class ChaCha {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
constructor(seed) {
seed = seed || [0,0,0,0,0,0,0,0];
this.state = [
0x61707865,
0x3320646E,
0x79622D32,
0x6B206574,
seed[0],
seed[1],
seed[2],
seed[3],
seed[4],
seed[5],
seed[6],
seed[7],
0,
0,
0,
0
];
this.idx = 16;
this.buff = new Array(16);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
nextU32() {
if (this.idx == 16) this.update();
return this.buff[this.idx++];
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
nextU64() {
return add$2(mul$2(this.nextU32(), 0x100000000), this.nextU32());
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
nextBool() {
return (this.nextU32() & 1) == 1;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
update() {
// Copy the state
for (let i=0; i<16; i++) this.buff[i] = this.state[i];
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
// Apply the rounds
for (let i=0; i<10; i++) doubleRound(this.buff);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
// Add to the initial
for (let i=0; i<16; i++) this.buff[i] = (this.buff[i] + this.state[i]) >>> 0;
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
this.idx = 0;
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
this.state[12] = (this.state[12] + 1) >>> 0;
if (this.state[12] != 0) return;
this.state[13] = (this.state[13] + 1) >>> 0;
if (this.state[13] != 0) return;
this.state[14] = (this.state[14] + 1) >>> 0;
if (this.state[14] != 0) return;
this.state[15] = (this.state[15] + 1) >>> 0;
2020-08-12 02:33:08 +03:00
}
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
var crypto = {};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
/* global window */
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function getRandomBytes(n) {
let array = new Uint8Array(n);
if (typeof window !== "undefined") { // Browser
if (typeof window.crypto !== "undefined") { // Supported
window.crypto.getRandomValues(array);
} else { // fallback
for (let i=0; i<n; i++) {
array[i] = (Math.random()*4294967296)>>>0;
}
}
}
else { // NodeJS
crypto.randomFillSync(array);
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
return array;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function getRandomSeed() {
const arr = getRandomBytes(32);
const arrV = new Uint32Array(arr.buffer);
const seed = [];
for (let i=0; i<8; i++) {
seed.push(arrV[i]);
}
return seed;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let threadRng = null;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function getThreadRng() {
if (threadRng) return threadRng;
threadRng = new ChaCha(getRandomSeed());
return threadRng;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
/* global BigInt */
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
class ZqField {
constructor(p) {
this.type="F1";
this.one = 1n;
this.zero = 0n;
this.p = BigInt(p);
this.m = 1;
this.negone = this.p-1n;
this.two = 2n;
this.half = this.p >> 1n;
this.bitLength = bitLength$2(this.p);
this.mask = (1n << BigInt(this.bitLength)) - 1n;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.n64 = Math.floor((this.bitLength - 1) / 64)+1;
this.n32 = this.n64*2;
this.n8 = this.n64*8;
this.R = this.e(1n << BigInt(this.n64*64));
this.Ri = this.inv(this.R);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const e = this.negone >> 1n;
this.nqr = this.two;
let r = this.pow(this.nqr, e);
while (!this.eq(r, this.negone)) {
this.nqr = this.nqr + 1n;
r = this.pow(this.nqr, e);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
this.s = 0;
this.t = this.negone;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
while ((this.t & 1n) == 0n) {
this.s = this.s + 1;
this.t = this.t >> 1n;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.nqr_to_t = this.pow(this.nqr, this.t);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
buildSqrt(this);
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
e(a,b) {
let res;
if (!b) {
res = BigInt(a);
} else if (b==16) {
res = BigInt("0x"+a);
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
if (res < 0) {
let nres = -res;
if (nres >= this.p) nres = nres % this.p;
return this.p - nres;
} else {
return (res>= this.p) ? res%this.p : res;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
add(a, b) {
const res = a + b;
return res >= this.p ? res-this.p : res;
2020-08-12 02:33:08 +03:00
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
sub(a, b) {
return (a >= b) ? a-b : this.p-b+a;
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
neg(a) {
return a ? this.p-a : a;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
mul(a, b) {
return (a*b)%this.p;
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
mulScalar(base, s) {
return (base * this.e(s)) % this.p;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
square(a) {
return (a*a) % this.p;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
eq(a, b) {
return a==b;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
neq(a, b) {
return a!=b;
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
lt(a, b) {
const aa = (a > this.half) ? a - this.p : a;
const bb = (b > this.half) ? b - this.p : b;
return aa < bb;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
gt(a, b) {
const aa = (a > this.half) ? a - this.p : a;
const bb = (b > this.half) ? b - this.p : b;
return aa > bb;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
leq(a, b) {
const aa = (a > this.half) ? a - this.p : a;
const bb = (b > this.half) ? b - this.p : b;
return aa <= bb;
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
geq(a, b) {
const aa = (a > this.half) ? a - this.p : a;
const bb = (b > this.half) ? b - this.p : b;
return aa >= bb;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
div(a, b) {
return this.mul(a, this.inv(b));
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
idiv(a, b) {
if (!b) throw new Error("Division by zero");
return a / b;
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
inv(a) {
if (!a) throw new Error("Division by zero");
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let t = 0n;
let r = this.p;
let newt = 1n;
let newr = a % this.p;
while (newr) {
let q = r/newr;
[t, newt] = [newt, t-q*newt];
[r, newr] = [newr, r-q*newr];
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
if (t<0n) t += this.p;
return t;
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
mod(a, b) {
return a % b;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
pow(b, e) {
return exp$3(this, b, e);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
exp(b, e) {
return exp$3(this, b, e);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
band(a, b) {
const res = ((a & b) & this.mask);
return res >= this.p ? res-this.p : res;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
bor(a, b) {
const res = ((a | b) & this.mask);
return res >= this.p ? res-this.p : res;
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
bxor(a, b) {
const res = ((a ^ b) & this.mask);
return res >= this.p ? res-this.p : res;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
bnot(a) {
const res = a ^ this.mask;
return res >= this.p ? res-this.p : res;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
shl(a, b) {
if (Number(b) < this.bitLength) {
const res = (a << b) & this.mask;
return res >= this.p ? res-this.p : res;
} else {
const nb = this.p - b;
if (Number(nb) < this.bitLength) {
return a >> nb;
} else {
return 0n;
}
2020-08-24 10:32:47 +03:00
}
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
shr(a, b) {
if (Number(b) < this.bitLength) {
return a >> b;
} else {
const nb = this.p - b;
if (Number(nb) < this.bitLength) {
const res = (a << nb) & this.mask;
return res >= this.p ? res-this.p : res;
} else {
return 0;
}
2020-08-12 02:33:08 +03:00
}
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
land(a, b) {
return (a && b) ? 1n : 0n;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
lor(a, b) {
return (a || b) ? 1n : 0n;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
lnot(a) {
return (a) ? 0n : 1n;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
sqrt_old(n) {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (n == 0n) return this.zero;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
// Test that have solution
const res = this.pow(n, this.negone >> this.one);
if ( res != 1n ) return null;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let m = this.s;
let c = this.nqr_to_t;
let t = this.pow(n, this.t);
let r = this.pow(n, this.add(this.t, this.one) >> 1n );
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
while ( t != 1n ) {
let sq = this.square(t);
let i = 1;
while (sq != 1n ) {
i++;
sq = this.square(sq);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
// b = c ^ m-i-1
let b = c;
for (let j=0; j< m-i-1; j ++) b = this.square(b);
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
m = i;
c = this.square(b);
t = this.mul(t, c);
r = this.mul(r, b);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
if (r > (this.p >> 1n)) {
r = this.neg(r);
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
return r;
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
normalize(a, b) {
a = BigInt(a,b);
if (a < 0) {
let na = -a;
if (na >= this.p) na = na % this.p;
return this.p - na;
2020-07-11 11:31:52 +03:00
} else {
2020-08-29 15:12:24 +03:00
return (a>= this.p) ? a%this.p : a;
2020-07-11 11:31:52 +03:00
}
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
random() {
const nBytes = (this.bitLength*2 / 8);
let res =0n;
for (let i=0; i<nBytes; i++) {
res = (res << 8n) + BigInt(getRandomBytes(1)[0]);
}
return res % this.p;
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
toString(a, base) {
let vs;
if (a > this.half) {
const v = this.p-a;
vs = "-"+v.toString(base);
} else {
vs = a.toString(base);
}
return vs;
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
isZero(a) {
return a == 0n;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
fromRng(rng) {
let v;
do {
v=0n;
for (let i=0; i<this.n64; i++) {
v += rng.nextU64() << BigInt(64 *i);
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
v &= this.mask;
} while (v >= this.p);
v = (v * this.Ri) % this.p; // Convert from montgomery
return v;
}
2020-07-11 11:31:52 +03:00
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
class ZqField$1 {
constructor(p) {
this.type="F1";
this.one = BigInteger.one;
this.zero = BigInteger.zero;
this.p = BigInteger(p);
this.m = 1;
this.negone = this.p.minus(BigInteger.one);
this.two = BigInteger(2);
this.half = this.p.shiftRight(1);
this.bitLength = this.p.bitLength();
this.mask = BigInteger.one.shiftLeft(this.bitLength).minus(BigInteger.one);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.n64 = Math.floor((this.bitLength - 1) / 64)+1;
this.n32 = this.n64*2;
this.n8 = this.n64*8;
this.R = BigInteger.one.shiftLeft(this.n64*64);
this.Ri = this.inv(this.R);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const e = this.negone.shiftRight(this.one);
this.nqr = this.two;
let r = this.pow(this.nqr, e);
while (!r.equals(this.negone)) {
this.nqr = this.nqr.add(this.one);
r = this.pow(this.nqr, e);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.s = this.zero;
this.t = this.negone;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
while (!this.t.isOdd()) {
this.s = this.s.add(this.one);
this.t = this.t.shiftRight(this.one);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.nqr_to_t = this.pow(this.nqr, this.t);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
buildSqrt(this);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
e(a,b) {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const res = BigInteger(a,b);
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
return this.normalize(res);
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
add(a, b) {
let res = a.add(b);
if (res.geq(this.p)) {
res = res.minus(this.p);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
return res;
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
sub(a, b) {
if (a.geq(b)) {
return a.minus(b);
2020-08-12 02:33:08 +03:00
} else {
2020-08-29 15:12:24 +03:00
return this.p.minus(b.minus(a));
2020-08-12 02:33:08 +03:00
}
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
neg(a) {
if (a.isZero()) return a;
return this.p.minus(a);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
mul(a, b) {
return a.times(b).mod(this.p);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
mulScalar(base, s) {
return base.times(BigInteger(s)).mod(this.p);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
square(a) {
return a.square().mod(this.p);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
eq(a, b) {
return a.eq(b);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
neq(a, b) {
return a.neq(b);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
lt(a, b) {
const aa = a.gt(this.half) ? a.minus(this.p) : a;
const bb = b.gt(this.half) ? b.minus(this.p) : b;
return aa.lt(bb);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
gt(a, b) {
const aa = a.gt(this.half) ? a.minus(this.p) : a;
const bb = b.gt(this.half) ? b.minus(this.p) : b;
return aa.gt(bb);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
leq(a, b) {
const aa = a.gt(this.half) ? a.minus(this.p) : a;
const bb = b.gt(this.half) ? b.minus(this.p) : b;
return aa.leq(bb);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
geq(a, b) {
const aa = a.gt(this.half) ? a.minus(this.p) : a;
const bb = b.gt(this.half) ? b.minus(this.p) : b;
return aa.geq(bb);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
div(a, b) {
if (b.isZero()) throw new Error("Division by zero");
return a.times(b.modInv(this.p)).mod(this.p);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
idiv(a, b) {
if (b.isZero()) throw new Error("Division by zero");
return a.divide(b);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
inv(a) {
if (a.isZero()) throw new Error("Division by zero");
return a.modInv(this.p);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
mod(a, b) {
return a.mod(b);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
pow(a, b) {
return a.modPow(b, this.p);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
exp(a, b) {
return a.modPow(b, this.p);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
band(a, b) {
return a.and(b).and(this.mask).mod(this.p);
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
bor(a, b) {
return a.or(b).and(this.mask).mod(this.p);
2020-07-11 11:31:52 +03:00
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
bxor(a, b) {
return a.xor(b).and(this.mask).mod(this.p);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
bnot(a) {
return a.xor(this.mask).mod(this.p);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
shl(a, b) {
if (b.lt(this.bitLength)) {
return a.shiftLeft(b).and(this.mask).mod(this.p);
} else {
const nb = this.p.minus(b);
if (nb.lt(this.bitLength)) {
return this.shr(a, nb);
} else {
return BigInteger.zero;
2020-08-24 10:32:47 +03:00
}
}
}
2020-08-29 15:12:24 +03:00
shr(a, b) {
if (b.lt(this.bitLength)) {
return a.shiftRight(b);
} else {
const nb = this.p.minus(b);
if (nb.lt(this.bitLength)) {
return this.shl(a, nb);
} else {
return BigInteger.zero;
}
}
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
land(a, b) {
return (a.isZero() || b.isZero()) ? BigInteger.zero : BigInteger.one;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
lor(a, b) {
return (a.isZero() && b.isZero()) ? BigInteger.zero : BigInteger.one;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
lnot(a) {
return a.isZero() ? BigInteger.one : BigInteger.zero;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
sqrt_old(n) {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (n.equals(this.zero)) return this.zero;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
// Test that have solution
const res = this.pow(n, this.negone.shiftRight(this.one));
if (!res.equals(this.one)) return null;
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
let m = parseInt(this.s);
let c = this.nqr_to_t;
let t = this.pow(n, this.t);
let r = this.pow(n, this.add(this.t, this.one).shiftRight(this.one) );
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
while (!t.equals(this.one)) {
let sq = this.square(t);
let i = 1;
while (!sq.equals(this.one)) {
i++;
sq = this.square(sq);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
// b = c ^ m-i-1
let b = c;
for (let j=0; j< m-i-1; j ++) b = this.square(b);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
m = i;
c = this.square(b);
t = this.mul(t, c);
r = this.mul(r, b);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (r.greater(this.p.shiftRight(this.one))) {
r = this.neg(r);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return r;
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
normalize(a) {
a = BigInteger(a);
if (a.isNegative()) {
return this.p.minus(a.abs().mod(this.p));
2020-08-24 10:32:47 +03:00
} else {
2020-08-29 15:12:24 +03:00
return a.mod(this.p);
2020-08-24 10:32:47 +03:00
}
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
random() {
let res = BigInteger(0);
let n = BigInteger(this.p.square());
while (!n.isZero()) {
res = res.shiftLeft(8).add(BigInteger(getRandomBytes(1)[0]));
n = n.shiftRight(8);
}
return res.mod(this.p);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
toString(a, base) {
let vs;
if (!a.lesserOrEquals(this.p.shiftRight(BigInteger(1)))) {
const v = this.p.minus(a);
vs = "-"+v.toString(base);
} else {
vs = a.toString(base);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return vs;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
isZero(a) {
return a.isZero();
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
fromRng(rng) {
let v;
do {
v = BigInteger(0);
for (let i=0; i<this.n64; i++) {
v = v.add(v, rng.nextU64().shiftLeft(64*i));
}
v = v.and(this.mask);
} while (v.geq(this.p));
v = v.times(this.Ri).mod(this.q);
return v;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const supportsNativeBigInt$1 = typeof BigInt === "function";
let _F1Field;
if (supportsNativeBigInt$1) {
_F1Field = ZqField;
} else {
_F1Field = ZqField$1;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
class F1Field extends _F1Field {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
// Returns a buffer with Little Endian Representation
toRprLE(buff, o, e) {
toRprLE(buff, o, e, this.n64*8);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
// Returns a buffer with Big Endian Representation
toRprBE(buff, o, e) {
toRprBE(buff, o, e, this.n64*8);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
// Returns a buffer with Big Endian Montgomery Representation
toRprBEM(buff, o, e) {
return this.toRprBE(buff, o, this.mul(this.R, e));
2020-07-11 11:31:52 +03:00
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
toRprLEM(buff, o, e) {
return this.toRprLE(buff, o, this.mul(this.R, e));
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
// Pases a buffer with Little Endian Representation
fromRprLE(buff, o) {
return fromRprLE(buff, o, this.n8);
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
// Pases a buffer with Big Endian Representation
fromRprBE(buff, o) {
return fromRprBE(buff, o, this.n8);
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
fromRprLEM(buff, o) {
return this.mul(this.fromRprLE(buff, o), this.Ri);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
fromRprBEM(buff, o) {
return this.mul(this.fromRprBE(buff, o), this.Ri);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2021-02-11 10:07:33 +03:00
var global = {};
2020-10-23 09:02:04 +03:00
var code = "AGFzbQEAAAABlAESYAJ/fwBgAX8AYAF/AX9gAn9/AX9gA39/fwF/YAN/f38AYAN/fn8AYAJ/fgBgBH9/f38AYAV/f39/fwBgBH9/f38Bf2AHf39/f39/fwBgBn9/f39/fwBgCn9/f39/f39/f38AYAV/f39/fwF/YAd/f39/f39/AX9gCX9/f39/f39/fwF/YAt/f39/f39/f39/fwF/Ag8BA2VudgZtZW1vcnkCABkDuQK3AgABAgEDAwQEBQAABgcIBQIFBQAABQAAAAACAgABBQgJBQUIAAICBQUAAAUAAAAAAgIAAQUICQUFCAACBQAAAgICAQEAAAADAwMAAAUFBQAABQUFAAAAAAACAgUABQAAAAAFBQUFBQoACwkKAAsJCAgDAAgIAgAACQwMBQUMAAgNCQgCAgEBAAUFAAUFAAAAAAMACAICCQgAAgICAQEAAAADAwMAAAUFBQAABQUFAAAAAAACAgUABQAAAAAFBQUFBQoACwkKAAsJCAgFAwAICAIAAAkMDAUFDAUDAAgIAgAACQwMBQUMBQUJCQkJCQACAgEBAAUABQUAAgAAAwAIAgkIAAICAQEABQUABQUAAAAAAwAIAgIJCAACBQAAAAAICAUAAAAAAAAAAAAAAAAAAAAABA4PEBEFB+0kqAIIaW50X2NvcHkAAAhpbnRfemVybwABB2ludF9vbmUAAwppbnRfaXNaZXJvAAIGaW50X2VxAAQHaW50X2d0ZQAFB2ludF9hZGQABgdpbnRfc3ViAAcHaW50X211bAAICmludF9zcXVhcmUACQ1pbnRfc3F1YXJlT2xkAAoHaW50X2RpdgANDmludF9pbnZlcnNlTW9kAA4IZjFtX2NvcHkAAAhmMW1femVybwABCmYxbV9pc1plcm8AAgZmMW1fZXEABAdmMW1fYWRkABAHZjFtX3N1YgARB2YxbV9uZWcAEg5mMW1faXNOZWdhdGl2ZQAZCWYxbV9pc09uZQAPCGYxbV9zaWduABoLZjFtX21SZWR1Y3QAEwdmMW1fbXVsABQKZjFtX3NxdWFyZQAVDWYxbV9zcXVhcmVPbGQAFhJmMW1fZnJvbU1vbnRnb21lcnkAGBBmMW1fdG9Nb250Z29tZXJ5ABcLZjFtX2ludmVyc2UAGwdmMW1fb25lABwIZjFtX2xvYWQAHQ9mMW1fdGltZXNTY2FsYXIAHgdmMW1fZXhwACIQZjFtX2JhdGNoSW52ZXJzZQAfCGYxbV9zcXJ0ACMMZjFtX2lzU3F1YXJlACQVZjFtX2JhdGNoVG9Nb250Z29tZXJ5ACAXZjFtX2JhdGNoRnJvbU1vbnRnb21lcnkAIQhmcm1fY29weQAACGZybV96ZXJvAAEKZnJtX2lzWmVybwACBmZybV9lcQAEB2ZybV9hZGQAJgdmcm1fc3ViACcHZnJtX25lZwAoDmZybV9pc05lZ2F0aXZlAC8JZnJtX2lzT25lACUIZnJtX3NpZ24AMAtmcm1fbVJlZHVjdAApB2ZybV9tdWwAKgpmcm1fc3F1YXJlACsNZnJtX3NxdWFyZU9sZAAsEmZybV9mcm9tTW9udGdvbWVyeQAuEGZybV90b01vbnRnb21lcnkALQtmcm1faW52ZXJzZQAxB2ZybV9vbmUAMghmcm1fbG9hZAAzD2ZybV90aW1lc1NjYWxhcgA0B2ZybV9leHAAOBBmcm1fYmF0Y2hJbnZlcnNlADUIZnJtX3NxcnQAOQxmcm1faXNTcXVhcmUAOhVmcm1fYmF0Y2hUb01vbnRnb21lcnkANhdmcm1fYmF0Y2hGcm9tTW9udGdvbWVyeQA3BmZyX2FkZAAmBmZyX3N1YgAnBmZyX25lZwAoBmZyX211bAA7CWZyX3NxdWFyZQA8CmZyX2ludmVyc2UAPQ1mcl9pc05lZ2F0aXZlAD4HZnJfY29weQAAB2ZyX3plcm8AAQZmcl9vbmUAMglmcl9pc1plcm8AAgVmcl9lcQAEDGcxbV9tdWx0aWV4cABpEmcxbV9tdWx0aWV4cF9jaHVuawBoEmcxbV9tdWx0aWV4cEFmZmluZQBtGGcxbV9tdWx0aWV4cEFmZmluZV9jaHVuawBsCmcxbV9pc1plcm8AQBBnMW1faXNaZXJvQWZmaW5lAD8GZzFtX2VxAEgLZzFtX2VxTWl4ZWQARwxnMW1fZXFBZmZpbmUARghnMW1fY29weQBEDmcxbV9jb3B5QWZmaW5lAEMIZzFtX3plcm8AQg5nMW1femVyb0FmZmluZQBBCmcxbV9kb3VibGUAShBnMW1fZG91YmxlQWZmaW5lAEkHZzFtX2FkZABNDGcxbV9hZGRNaXhlZABMDWcxbV9hZGRBZmZpbmUASwdnMW1fbmVnAE8NZzFtX25lZ0FmZmluZQBOB2cxbV9zdWIAUgxnMW1fc3ViTWl4ZWQAUQ1nMW1fc3ViQWZmaW5lAFASZzFtX2Zyb21Nb250Z29tZXJ5AFQYZzFtX2Zyb21Nb250Z29tZXJ5QWZmaW5lAFMQZzFtX3RvTW9udGdvbWVyeQBWFmcxbV90b01vbnRnb21lcnlBZmZpbmUAVQ9nMW1fdGltZXNTY2FsYXIAbhVnMW1fdGltZXNTY2FsYXJBZmZpbmUAbw1nMW1fbm9ybWFsaXplAFsKZzFtX0xFTXRvVQBdCmcxbV9MRU10b0MAXgpnMW1fVXRvTEVNAF8KZzFtX0N0b0xFTQBgD2cxbV9iYXRjaExFTXRvVQBhD2cxbV9iYXRjaExFTXRvQwBiD2cxbV9iYXRjaFV0b0xFTQBjD2cxbV9iYXRjaEN0b0xFTQBkDGcxbV90b0FmZmluZQBXDmcxbV90b0phY29iaWFuAEURZzFtX2JhdGNoVG9BZmZpbmUAWhNnMW1fYmF0Y2hUb0phY29iaWFuAGULZzFtX2luQ3VydmUAWRFnMW1faW5DdXJ2ZUFmZmluZQBYB2ZybV9mZnQAdQhmcm1faWZmdAB2CmZybV9yYXdmZnQAcwtmcm1fZmZ0Sm9pbgB3DmZybV9mZnRKb2luRXh0AHgRZnJtX2ZmdEpvaW5FeHRJbnYAeQpmcm1fZmZ0TWl4AHoMZnJtX2ZmdEZpbmFsAHsdZnJtX3ByZXBhcmVMYWdyYW5nZUV2YWx1YXRpb24AfAhwb2xfemVybwB9D3BvbF9jb25zdHJ1Y3RMQwB+DHFhcF9idWlsZEFCQwB/C3FhcF9qb2luQUJDAIABDHFhcF9iYXRjaEFkZACBAQpmMm1faXNaZXJvAIIBCWYybV9pc09uZQCDAQhmMm1femVybwCEAQdmMm1fb25lAIUBCGYybV9jb3B5AIYBB2YybV9tdWwAhwEIZjJtX211bDEAiAEKZjJtX3NxdWFyZQCJAQdmMm1fYWRkAIoBB2YybV9zdWIAiwEHZjJtX25lZwCMAQhmMm1fc2lnbgCTAQ1mMm1fY29uanVnYXRlAI0BEmYybV9mcm9tTW9udGdvbWVyeQCPARBmMm1fdG9Nb250Z29tZXJ5AI4BBmYybV9lcQCQAQtmMm1faW52ZXJzZQCRAQdmMm1fZXhwAJYBD2YybV90aW1lc1NjYWxhcgCSARBmMm1fYmF0Y2hJbnZlcnNlAJUBCGYybV9zcXJ0AJcBDGYybV9pc1NxdWFyZQCYAQ5mMm1faXNOZWdhdGl2ZQCUAQxnMm1fbXVsdGlleHAAwwESZzJtX211bHRpZXhwX2NodW5rAMIBEmcybV9tdWx0aWV4cEFmZmluZQDHARhnMm1fbXVsdGlleHBBZmZpbmVfY2h1bmsAxgEKZzJtX2lzWmVybwCaARBnMm1faXNaZXJvQWZmaW5lAJkBBmcybV9lcQCiAQtnMm1fZXFNaXhlZAChAQxnMm1fZXFBZmZpbmUAoAEIZzJtX2NvcHkAngEOZzJtX2NvcHlBZmZpbmUAnQEIZzJtX3plcm8AnAEOZzJtX3plcm9BZmZpbmUAmwEKZzJtX2RvdWJsZQCkARBnMm1fZG91YmxlQWZmaW5lAKMBB2cybV9hZGQApwEMZzJtX2FkZE1peGV
2020-08-29 15:12:24 +03:00
var pq = 520;
var pr = 1864;
2020-09-25 08:38:22 +03:00
var pG1gen = 31592;
var pG1zero = 31688;
2020-08-29 15:12:24 +03:00
var pG1b = 3240;
2020-09-25 08:38:22 +03:00
var pG2gen = 31784;
var pG2zero = 31976;
var pG2b = 12616;
var pOneT = 32168;
2020-08-29 15:12:24 +03:00
var prePSize = 192;
var preQSize = 19776;
var n8q = 32;
var n8r = 32;
var q = "21888242871839275222246405745257275088696311157297823662689037894645226208583";
var r = "21888242871839275222246405745257275088548364400416034343698204186575808495617";
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
var bn128_wasm = {
code: code,
pq: pq,
pr: pr,
pG1gen: pG1gen,
pG1zero: pG1zero,
pG1b: pG1b,
pG2gen: pG2gen,
pG2zero: pG2zero,
pG2b: pG2b,
pOneT: pOneT,
prePSize: prePSize,
preQSize: preQSize,
n8q: n8q,
n8r: n8r,
q: q,
r: r
};
2020-07-11 11:31:52 +03:00
2020-10-23 09:02:04 +03:00
var code$1 = "AGFzbQEAAAABlAESYAJ/fwBgAX8AYAF/AX9gAn9/AX9gA39/fwF/YAN/f38AYAN/fn8AYAJ/fgBgBH9/f38AYAV/f39/fwBgBH9/f38Bf2AHf39/f39/fwBgBn9/f39/fwBgCn9/f39/f39/f38AYAV/f39/fwF/YAd/f39/f39/AX9gCX9/f39/f39/fwF/YAt/f39/f39/f39/fwF/Ag8BA2VudgZtZW1vcnkCABkDywLJAgABAgEDAwQEBQAABgcIBQIFBQAABQAAAAACAgABBQgJBQUIAAIAAQIBAwMEBAUAAAYHCAUCBQUAAAUAAAAAAgIAAQUICQUFCAACBQAAAgICAQEAAAADAwMAAAUFBQAABQUFAAAAAAACAgUABQAAAAAFBQUFBQoACwkKAAsJCAgDAAgIAgAACQwMBQUMAAgNCQgCAgEBAAUFAAUFAAAAAAMACAICCQgAAgICAQEAAAADAwMAAAUFBQAABQUFAAAAAAACAgUABQAAAAAFBQUFBQoACwkKAAsJCAgFAwAICAIAAAkMDAUFDAUDAAgIAgAACQwMBQUMBQUJCQkJCQACAgEBAAUABQUAAgAAAwAIAgkIAAICAQEABQUABQUAAAAAAwAIAgIJCAACBQgJBQAAAAAAAAAAAAACAgICBQAAAAUAAAAABA4PEBEFB/gmugIJaW50cV9jb3B5AAAJaW50cV96ZXJvAAEIaW50cV9vbmUAAwtpbnRxX2lzWmVybwACB2ludHFfZXEABAhpbnRxX2d0ZQAFCGludHFfYWRkAAYIaW50cV9zdWIABwhpbnRxX211bAAIC2ludHFfc3F1YXJlAAkOaW50cV9zcXVhcmVPbGQACghpbnRxX2RpdgAND2ludHFfaW52ZXJzZU1vZAAOCGYxbV9jb3B5AAAIZjFtX3plcm8AAQpmMW1faXNaZXJvAAIGZjFtX2VxAAQHZjFtX2FkZAAQB2YxbV9zdWIAEQdmMW1fbmVnABIOZjFtX2lzTmVnYXRpdmUAGQlmMW1faXNPbmUADwhmMW1fc2lnbgAaC2YxbV9tUmVkdWN0ABMHZjFtX211bAAUCmYxbV9zcXVhcmUAFQ1mMW1fc3F1YXJlT2xkABYSZjFtX2Zyb21Nb250Z29tZXJ5ABgQZjFtX3RvTW9udGdvbWVyeQAXC2YxbV9pbnZlcnNlABsHZjFtX29uZQAcCGYxbV9sb2FkAB0PZjFtX3RpbWVzU2NhbGFyAB4HZjFtX2V4cAAiEGYxbV9iYXRjaEludmVyc2UAHwhmMW1fc3FydAAjDGYxbV9pc1NxdWFyZQAkFWYxbV9iYXRjaFRvTW9udGdvbWVyeQAgF2YxbV9iYXRjaEZyb21Nb250Z29tZXJ5ACEJaW50cl9jb3B5ACUJaW50cl96ZXJvACYIaW50cl9vbmUAKAtpbnRyX2lzWmVybwAnB2ludHJfZXEAKQhpbnRyX2d0ZQAqCGludHJfYWRkACsIaW50cl9zdWIALAhpbnRyX211bAAtC2ludHJfc3F1YXJlAC4OaW50cl9zcXVhcmVPbGQALwhpbnRyX2RpdgAyD2ludHJfaW52ZXJzZU1vZAAzCGZybV9jb3B5ACUIZnJtX3plcm8AJgpmcm1faXNaZXJvACcGZnJtX2VxACkHZnJtX2FkZAA1B2ZybV9zdWIANgdmcm1fbmVnADcOZnJtX2lzTmVnYXRpdmUAPglmcm1faXNPbmUANAhmcm1fc2lnbgA/C2ZybV9tUmVkdWN0ADgHZnJtX211bAA5CmZybV9zcXVhcmUAOg1mcm1fc3F1YXJlT2xkADsSZnJtX2Zyb21Nb250Z29tZXJ5AD0QZnJtX3RvTW9udGdvbWVyeQA8C2ZybV9pbnZlcnNlAEAHZnJtX29uZQBBCGZybV9sb2FkAEIPZnJtX3RpbWVzU2NhbGFyAEMHZnJtX2V4cABHEGZybV9iYXRjaEludmVyc2UARAhmcm1fc3FydABIDGZybV9pc1NxdWFyZQBJFWZybV9iYXRjaFRvTW9udGdvbWVyeQBFF2ZybV9iYXRjaEZyb21Nb250Z29tZXJ5AEYGZnJfYWRkADUGZnJfc3ViADYGZnJfbmVnADcGZnJfbXVsAEoJZnJfc3F1YXJlAEsKZnJfaW52ZXJzZQBMDWZyX2lzTmVnYXRpdmUATQdmcl9jb3B5ACUHZnJfemVybwAmBmZyX29uZQBBCWZyX2lzWmVybwAnBWZyX2VxACkMZzFtX211bHRpZXhwAHgSZzFtX211bHRpZXhwX2NodW5rAHcSZzFtX211bHRpZXhwQWZmaW5lAHwYZzFtX211bHRpZXhwQWZmaW5lX2NodW5rAHsKZzFtX2lzWmVybwBPEGcxbV9pc1plcm9BZmZpbmUATgZnMW1fZXEAVwtnMW1fZXFNaXhlZABWDGcxbV9lcUFmZmluZQBVCGcxbV9jb3B5AFMOZzFtX2NvcHlBZmZpbmUAUghnMW1femVybwBRDmcxbV96ZXJvQWZmaW5lAFAKZzFtX2RvdWJsZQBZEGcxbV9kb3VibGVBZmZpbmUAWAdnMW1fYWRkAFwMZzFtX2FkZE1peGVkAFsNZzFtX2FkZEFmZmluZQBaB2cxbV9uZWcAXg1nMW1fbmVnQWZmaW5lAF0HZzFtX3N1YgBhDGcxbV9zdWJNaXhlZABgDWcxbV9zdWJBZmZpbmUAXxJnMW1fZnJvbU1vbnRnb21lcnkAYxhnMW1fZnJvbU1vbnRnb21lcnlBZmZpbmUAYhBnMW1fdG9Nb250Z29tZXJ5AGUWZzFtX3RvTW9udGdvbWVyeUFmZmluZQBkD2cxbV90aW1lc1NjYWxhcgB9FWcxbV90aW1lc1NjYWxhckFmZmluZQB+DWcxbV9ub3JtYWxpemUAagpnMW1fTEVNdG9VAGwKZzFtX0xFTXRvQwBtCmcxbV9VdG9MRU0AbgpnMW1fQ3RvTEVNAG8PZzFtX2JhdGNoTEVNdG9VAHAPZzFtX2JhdGNoTEVNdG9DAHEPZzFtX2JhdGNoVXRvTEVNAHIPZzFtX2JhdGNoQ3RvTEVNAHMMZzFtX3RvQWZmaW5lAGYOZzFtX3RvSmFjb2JpYW4AVBFnMW1fYmF0Y2hUb0FmZmluZQBpE2cxbV9iYXRjaFRvSmFjb2JpYW4AdAtnMW1faW5DdXJ2ZQBoEWcxbV9pbkN1cnZlQWZmaW5lAGcHZnJtX2ZmdACEAQhmcm1faWZmdACFAQpmcm1fcmF3ZmZ0AIIBC2ZybV9mZnRKb2luAIYBDmZybV9mZnRKb2luRXh0AIcBEWZybV9mZnRKb2luRXh0SW52AIgBCmZybV9mZnRNaXgAiQEMZnJtX2ZmdEZpbmFsAIoBHWZybV9wcmVwYXJlTGFncmFuZ2VFdmFsdWF0aW9uAIsBCHBvbF96ZXJvAIwBD3BvbF9jb25zdHJ1Y3RMQwCNAQxxYXBfYnVpbGRBQkMAjgELcWFwX2pvaW5BQkMAjwEMcWFwX2JhdGNoQWRkAJABCmYybV9pc1plcm8AkQEJZjJtX2lzT25lAJIBCGYybV96ZXJvAJMBB2YybV9vbmUAlAEIZjJtX2NvcHkAlQEHZjJtX211bACWAQhmMm1fbXVsMQCXAQpmMm1fc3F1YXJlAJgBB2YybV9hZGQAmQEHZjJtX3N1YgCaAQdmMm1fbmVnAJsBCGYybV9zaWduAKIBDWYybV9jb25qdWdhdGUAnAESZjJtX2Zyb21Nb250Z29tZXJ5AJ4BEGYybV90b01vbnRnb21lcnkAnQEGZjJtX2VxAJ8BC2YybV9pbnZlcnNlAKABB2YybV9leHAApQEPZjJtX3RpbWVzU2NhbGFyAKEBEGYybV9iYXRjaEludmVyc2UApAEIZjJtX3NxcnQApgEMZjJtX2lzU3F1YXJlAKcBDmYybV9pc05lZ2F0aXZlAKMBDGcybV9tdWx0aWV4cADSARJnMm1fbXVsdGlleHBfY2h1bmsA0QESZzJtX211bHRpZXhwQWZmaW5lANYBGGcybV9tdWx0aWV4c
2020-08-29 15:12:24 +03:00
var pq$1 = 760;
var pr$1 = 3640;
2020-09-25 08:38:22 +03:00
var pG1gen$1 = 42344;
var pG1zero$1 = 42488;
2020-08-29 15:12:24 +03:00
var pG1b$1 = 5016;
2020-09-25 08:38:22 +03:00
var pG2gen$1 = 42632;
var pG2zero$1 = 42920;
var pG2b$1 = 17224;
var pOneT$1 = 43208;
2020-08-29 15:12:24 +03:00
var prePSize$1 = 288;
var preQSize$1 = 20448;
var n8q$1 = 48;
var n8r$1 = 32;
var q$1 = "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787";
var r$1 = "52435875175126190479447740508185965837690552500527637822603658699938581184513";
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
var bls12381_wasm = {
code: code$1,
pq: pq$1,
pr: pr$1,
pG1gen: pG1gen$1,
pG1zero: pG1zero$1,
pG1b: pG1b$1,
pG2gen: pG2gen$1,
pG2zero: pG2zero$1,
pG2b: pG2b$1,
pOneT: pOneT$1,
prePSize: prePSize$1,
preQSize: preQSize$1,
n8q: n8q$1,
n8r: n8r$1,
q: q$1,
r: r$1
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
/*
Copyright 2019 0KIMS association.
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
This file is part of wasmsnark (Web Assembly zkSnark Prover).
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
wasmsnark is a free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
wasmsnark is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
You should have received a copy of the GNU General Public License
along with wasmsnark. If not, see <https://www.gnu.org/licenses/>.
*/
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
// module.exports.buildF1 = require("./src/f1.js");
// module.exports.buildBn128 = require("./src/bn128.js");
// module.exports.buildMnt6753 = require("./src/mnt6753.js");
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
var bn128_wasm$1 = bn128_wasm;
var bls12381_wasm$1 = bls12381_wasm;
// module.exports.mnt6753_wasm = require("./build/mnt6753_wasm.js");
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
var wasmcurves = {
bn128_wasm: bn128_wasm$1,
bls12381_wasm: bls12381_wasm$1
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
/* global BigInt */
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function stringifyBigInts(o) {
if ((typeof(o) == "bigint") || o.eq !== undefined) {
return o.toString(10);
2020-10-08 12:43:05 +03:00
} else if (o instanceof Uint8Array) {
return fromRprLE(o, 0);
2020-08-29 15:12:24 +03:00
} else if (Array.isArray(o)) {
return o.map(stringifyBigInts);
} else if (typeof o == "object") {
const res = {};
const keys = Object.keys(o);
keys.forEach( (k) => {
res[k] = stringifyBigInts(o[k]);
});
return res;
} else {
return o;
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
function unstringifyBigInts(o) {
if ((typeof(o) == "string") && (/^[0-9]+$/.test(o) )) {
return BigInt(o);
} else if (Array.isArray(o)) {
return o.map(unstringifyBigInts);
} else if (typeof o == "object") {
if (o===null) return null;
const res = {};
const keys = Object.keys(o);
keys.forEach( (k) => {
res[k] = unstringifyBigInts(o[k]);
});
return res;
} else {
return o;
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function beBuff2int(buff) {
let res = 0n;
let i = buff.length;
let offset = 0;
const buffV = new DataView(buff.buffer, buff.byteOffset, buff.byteLength);
while (i>0) {
if (i >= 4) {
i -= 4;
res += BigInt(buffV.getUint32(i)) << BigInt(offset*8);
offset += 4;
} else if (i >= 2) {
i -= 2;
res += BigInt(buffV.getUint16(i)) << BigInt(offset*8);
offset += 2;
} else {
i -= 1;
res += BigInt(buffV.getUint8(i)) << BigInt(offset*8);
offset += 1;
2020-07-11 11:31:52 +03:00
}
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
return res;
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
function beInt2Buff(n, len) {
let r = n;
const buff = new Uint8Array(len);
const buffV = new DataView(buff.buffer);
let o = len;
while (o > 0) {
if (o-4 >= 0) {
o -= 4;
buffV.setUint32(o, Number(r & 0xFFFFFFFFn));
r = r >> 32n;
} else if (o-2 >= 0) {
o -= 2;
buffV.setUint16(o, Number(r & 0xFFFFn));
r = r >> 16n;
2020-08-24 10:32:47 +03:00
} else {
2020-08-29 15:12:24 +03:00
o -= 1;
buffV.setUint8(o, Number(r & 0xFFn));
r = r >> 8n;
2020-07-11 11:31:52 +03:00
}
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
if (r) {
throw new Error("Number does not fit in this length");
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
return buff;
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function leBuff2int(buff) {
let res = 0n;
let i = 0;
const buffV = new DataView(buff.buffer, buff.byteOffset, buff.byteLength);
while (i<buff.length) {
if (i + 4 <= buff.length) {
res += BigInt(buffV.getUint32(i, true)) << BigInt( i*8);
i += 4;
} else if (i + 4 <= buff.length) {
res += BigInt(buffV.getUint16(i, true)) << BigInt( i*8);
i += 2;
} else {
res += BigInt(buffV.getUint8(i, true)) << BigInt( i*8);
i += 1;
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
}
return res;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function leInt2Buff(n, len) {
let r = n;
if (typeof len === "undefined") {
len = Math.floor((bitLength$2(n) - 1) / 8) +1;
if (len==0) len = 1;
}
const buff = new Uint8Array(len);
const buffV = new DataView(buff.buffer);
let o = 0;
while (o < len) {
if (o+4 <= len) {
buffV.setUint32(o, Number(r & 0xFFFFFFFFn), true );
o += 4;
r = r >> 32n;
} else if (o+2 <= len) {
buffV.setUint16(Number(o, r & 0xFFFFn), true );
o += 2;
r = r >> 16n;
} else {
buffV.setUint8(Number(o, r & 0xFFn), true );
o += 1;
r = r >> 8n;
2020-07-11 11:31:52 +03:00
}
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
if (r) {
throw new Error("Number does not fit in this length");
}
return buff;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
var utils_native = /*#__PURE__*/Object.freeze({
__proto__: null,
stringifyBigInts: stringifyBigInts,
unstringifyBigInts: unstringifyBigInts,
beBuff2int: beBuff2int,
beInt2Buff: beInt2Buff,
leBuff2int: leBuff2int,
leInt2Buff: leInt2Buff
});
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
function stringifyBigInts$1(o) {
if ((typeof(o) == "bigint") || o.eq !== undefined) {
return o.toString(10);
} else if (Array.isArray(o)) {
return o.map(stringifyBigInts$1);
} else if (typeof o == "object") {
const res = {};
const keys = Object.keys(o);
keys.forEach( (k) => {
res[k] = stringifyBigInts$1(o[k]);
});
return res;
} else {
return o;
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function unstringifyBigInts$1(o) {
if ((typeof(o) == "string") && (/^[0-9]+$/.test(o) )) {
return BigInteger(o);
} else if (Array.isArray(o)) {
return o.map(unstringifyBigInts$1);
} else if (typeof o == "object") {
const res = {};
const keys = Object.keys(o);
keys.forEach( (k) => {
res[k] = unstringifyBigInts$1(o[k]);
});
2020-08-24 10:32:47 +03:00
return res;
2020-08-29 15:12:24 +03:00
} else {
return o;
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function beBuff2int$1(buff) {
let res = BigInteger.zero;
for (let i=0; i<buff.length; i++) {
const n = BigInteger(buff[buff.length - i - 1]);
res = res.add(n.shiftLeft(i*8));
2020-08-12 02:33:08 +03:00
}
2020-08-29 15:12:24 +03:00
return res;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function beInt2Buff$1(n, len) {
let r = n;
let o =len-1;
const buff = new Uint8Array(len);
while ((r.gt(BigInteger.zero))&&(o>=0)) {
let c = Number(r.and(BigInteger("255")));
buff[o] = c;
o--;
r = r.shiftRight(8);
2020-08-12 02:33:08 +03:00
}
2020-08-29 15:12:24 +03:00
if (!r.eq(BigInteger.zero)) {
throw new Error("Number does not fit in this length");
2020-08-12 02:33:08 +03:00
}
2020-08-29 15:12:24 +03:00
return buff;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function leBuff2int$1 (buff) {
let res = BigInteger.zero;
for (let i=0; i<buff.length; i++) {
const n = BigInteger(buff[i]);
res = res.add(n.shiftLeft(i*8));
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
return res;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function leInt2Buff$1(n, len) {
let r = n;
let o =0;
const buff = new Uint8Array(len);
while ((r.gt(BigInteger.zero))&&(o<buff.length)) {
let c = Number(r.and(BigInteger(255)));
buff[o] = c;
o++;
r = r.shiftRight(8);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
if (!r.eq(BigInteger.zero)) {
throw new Error("Number does not fit in this length");
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
return buff;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
var utils_bigint = /*#__PURE__*/Object.freeze({
__proto__: null,
stringifyBigInts: stringifyBigInts$1,
unstringifyBigInts: unstringifyBigInts$1,
beBuff2int: beBuff2int$1,
beInt2Buff: beInt2Buff$1,
leBuff2int: leBuff2int$1,
leInt2Buff: leInt2Buff$1
});
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let utils = {};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const supportsNativeBigInt$2 = typeof BigInt === "function";
if (supportsNativeBigInt$2) {
Object.assign(utils, utils_native);
} else {
Object.assign(utils, utils_bigint);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const _revTable$1 = [];
for (let i=0; i<256; i++) {
_revTable$1[i] = _revSlow$1(i, 8);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function _revSlow$1(idx, bits) {
let res =0;
let a = idx;
for (let i=0; i<bits; i++) {
res <<= 1;
res = res | (a &1);
a >>=1;
2020-08-12 02:33:08 +03:00
}
2020-08-29 15:12:24 +03:00
return res;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
utils.bitReverse = function bitReverse(idx, bits) {
return (
_revTable$1[idx >>> 24] |
(_revTable$1[(idx >>> 16) & 0xFF] << 8) |
(_revTable$1[(idx >>> 8) & 0xFF] << 16) |
(_revTable$1[idx & 0xFF] << 24)
) >>> (32-bits);
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
utils.log2 = function log2( V )
{
return( ( ( V & 0xFFFF0000 ) !== 0 ? ( V &= 0xFFFF0000, 16 ) : 0 ) | ( ( V & 0xFF00FF00 ) !== 0 ? ( V &= 0xFF00FF00, 8 ) : 0 ) | ( ( V & 0xF0F0F0F0 ) !== 0 ? ( V &= 0xF0F0F0F0, 4 ) : 0 ) | ( ( V & 0xCCCCCCCC ) !== 0 ? ( V &= 0xCCCCCCCC, 2 ) : 0 ) | ( ( V & 0xAAAAAAAA ) !== 0 ) );
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
utils.buffReverseBits = function buffReverseBits(buff, eSize) {
const n = buff.byteLength /eSize;
const bits = utils.log2(n);
if (n != (1 << bits)) {
throw new Error("Invalid number of pointers");
2020-08-12 02:33:08 +03:00
}
2020-08-29 15:12:24 +03:00
for (let i=0; i<n; i++) {
const r = utils.bitReverse(i,bits);
if (i>r) {
const tmp = buff.slice(i*eSize, (i+1)*eSize);
buff.set( buff.slice(r*eSize, (r+1)*eSize), i*eSize);
buff.set(tmp, r*eSize);
}
2020-08-12 02:33:08 +03:00
}
2020-08-29 15:12:24 +03:00
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let {
bitReverse,
log2,
buffReverseBits,
stringifyBigInts: stringifyBigInts$2,
unstringifyBigInts: unstringifyBigInts$2,
beBuff2int: beBuff2int$2,
beInt2Buff: beInt2Buff$2,
leBuff2int: leBuff2int$2,
leInt2Buff: leInt2Buff$2,
} = utils;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
var _utils = /*#__PURE__*/Object.freeze({
__proto__: null,
bitReverse: bitReverse,
log2: log2,
buffReverseBits: buffReverseBits,
stringifyBigInts: stringifyBigInts$2,
unstringifyBigInts: unstringifyBigInts$2,
beBuff2int: beBuff2int$2,
beInt2Buff: beInt2Buff$2,
leBuff2int: leBuff2int$2,
leInt2Buff: leInt2Buff$2
});
2020-07-11 11:31:52 +03:00
2020-09-10 16:02:19 +03:00
const PAGE_SIZE = 1<<30;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
class BigBuffer {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
constructor(size) {
this.buffers = [];
this.byteLength = size;
for (let i=0; i<size; i+= PAGE_SIZE) {
const n = Math.min(size-i, PAGE_SIZE);
this.buffers.push(new Uint8Array(n));
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
slice(fr, to) {
2020-10-21 12:35:18 +03:00
if ( to === undefined ) to = this.byteLength;
2020-10-21 13:42:48 +03:00
if ( fr === undefined ) fr = 0;
2020-08-29 15:12:24 +03:00
const len = to-fr;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const firstPage = Math.floor(fr / PAGE_SIZE);
2020-10-21 12:35:18 +03:00
const lastPage = Math.floor((fr+len-1) / PAGE_SIZE);
2020-10-22 12:40:20 +03:00
if ((firstPage == lastPage)||(len==0))
2020-10-21 12:35:18 +03:00
return this.buffers[firstPage].slice(fr%PAGE_SIZE, fr%PAGE_SIZE + len);
2020-07-11 11:31:52 +03:00
2020-09-07 13:43:50 +03:00
let buff;
2020-08-29 15:12:24 +03:00
let p = firstPage;
let o = fr % PAGE_SIZE;
// Remaining bytes to read
let r = len;
while (r>0) {
// bytes to copy from this page
const l = (o+r > PAGE_SIZE) ? (PAGE_SIZE -o) : r;
2020-09-10 16:02:19 +03:00
const srcView = new Uint8Array(this.buffers[p].buffer, this.buffers[p].byteOffset+o, l);
2020-10-20 20:24:45 +03:00
if (l == len) return srcView.slice();
if (!buff) {
if (len <= PAGE_SIZE) {
buff = new Uint8Array(len);
} else {
buff = new BigBuffer(len);
}
}
2020-08-29 15:12:24 +03:00
buff.set(srcView, len-r);
r = r-l;
p ++;
o = 0;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return buff;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
set(buff, offset) {
2020-10-21 13:42:48 +03:00
if (offset === undefined) offset = 0;
2020-10-21 12:35:18 +03:00
const len = buff.byteLength;
2020-07-11 11:31:52 +03:00
2020-10-22 12:40:20 +03:00
if (len==0) return;
2020-08-29 15:12:24 +03:00
const firstPage = Math.floor(offset / PAGE_SIZE);
2020-10-21 12:35:18 +03:00
const lastPage = Math.floor((offset+len-1) / PAGE_SIZE);
if (firstPage == lastPage)
return this.buffers[firstPage].set(buff, offset % PAGE_SIZE);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let p = firstPage;
let o = offset % PAGE_SIZE;
2020-10-21 12:35:18 +03:00
let r = len;
2020-08-29 15:12:24 +03:00
while (r>0) {
const l = (o+r > PAGE_SIZE) ? (PAGE_SIZE -o) : r;
2020-10-21 12:35:18 +03:00
const srcView = buff.slice( len -r, len -r+l);
2020-08-29 15:12:24 +03:00
const dstView = new Uint8Array(this.buffers[p].buffer, this.buffers[p].byteOffset + o, l);
dstView.set(srcView);
r = r-l;
p ++;
o = 0;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function buildBatchConvert(tm, fnName, sIn, sOut) {
return async function batchConvert(buffIn) {
const nPoints = Math.floor(buffIn.byteLength / sIn);
if ( nPoints * sIn !== buffIn.byteLength) {
throw new Error("Invalid buffer size");
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
const pointsPerChunk = Math.floor(nPoints/tm.concurrency);
const opPromises = [];
for (let i=0; i<tm.concurrency; i++) {
let n;
if (i< tm.concurrency-1) {
n = pointsPerChunk;
} else {
n = nPoints - i*pointsPerChunk;
}
if (n==0) continue;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const buffChunk = buffIn.slice(i*pointsPerChunk*sIn, i*pointsPerChunk*sIn + n*sIn);
const task = [
{cmd: "ALLOCSET", var: 0, buff:buffChunk},
{cmd: "ALLOC", var: 1, len:sOut * n},
{cmd: "CALL", fnName: fnName, params: [
{var: 0},
{val: n},
{var: 1}
]},
{cmd: "GET", out: 0, var: 1, len:sOut * n},
];
opPromises.push(
tm.queueAction(task)
);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const result = await Promise.all(opPromises);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let fullBuffOut;
if (buffIn instanceof BigBuffer) {
fullBuffOut = new BigBuffer(nPoints*sOut);
} else {
fullBuffOut = new Uint8Array(nPoints*sOut);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let p =0;
for (let i=0; i<result.length; i++) {
fullBuffOut.set(result[i][0], p);
p+=result[i][0].byteLength;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return fullBuffOut;
};
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
class WasmField1 {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
constructor(tm, prefix, n8, p) {
this.tm = tm;
this.prefix = prefix;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.p = p;
this.n8 = n8;
this.type = "F1";
this.m = 1;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.half = shiftRight$2(p, one);
this.bitLength = bitLength$2(p);
this.mask = sub$2(shiftLeft$2(one, this.bitLength), one);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.pOp1 = tm.alloc(n8);
this.pOp2 = tm.alloc(n8);
this.pOp3 = tm.alloc(n8);
this.tm.instance.exports[prefix + "_zero"](this.pOp1);
this.zero = this.tm.getBuff(this.pOp1, this.n8);
this.tm.instance.exports[prefix + "_one"](this.pOp1);
this.one = this.tm.getBuff(this.pOp1, this.n8);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.negone = this.neg(this.one);
this.two = this.add(this.one, this.one);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.n64 = Math.floor(n8/8);
this.n32 = Math.floor(n8/4);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if(this.n64*8 != this.n8) {
throw new Error("n8 must be a multiple of 8");
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.half = shiftRight$2(this.p, one);
this.nqr = this.two;
let r = this.exp(this.nqr, this.half);
while (!this.eq(r, this.negone)) {
this.nqr = this.add(this.nqr, this.one);
r = this.exp(this.nqr, this.half);
}
2020-07-11 11:31:52 +03:00
2020-09-07 13:43:50 +03:00
this.shift = this.mul(this.nqr, this.nqr);
this.shiftInv = this.inv(this.shift);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.s = 0;
let t = sub$2(this.p, one);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
while ( !isOdd$2(t) ) {
this.s = this.s + 1;
t = shiftRight$2(t, one);
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
this.w = [];
this.w[this.s] = this.exp(this.nqr, t);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
for (let i= this.s-1; i>=0; i--) {
this.w[i] = this.square(this.w[i+1]);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (!this.eq(this.w[0], this.one)) {
throw new Error("Error calculating roots of unity");
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.batchToMontgomery = buildBatchConvert(tm, prefix + "_batchToMontgomery", this.n8, this.n8);
this.batchFromMontgomery = buildBatchConvert(tm, prefix + "_batchFromMontgomery", this.n8, this.n8);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
op2(opName, a, b) {
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp2, this.pOp3);
return this.tm.getBuff(this.pOp3, this.n8);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
op2Bool(opName, a, b) {
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
return !!this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp2);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
op1(opName, a) {
this.tm.setBuff(this.pOp1, a);
this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp3);
return this.tm.getBuff(this.pOp3, this.n8);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
op1Bool(opName, a) {
this.tm.setBuff(this.pOp1, a);
return !!this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp3);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
add(a,b) {
return this.op2("_add", a, b);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
eq(a,b) {
return this.op2Bool("_eq", a, b);
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
isZero(a) {
return this.op1Bool("_isZero", a);
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
sub(a,b) {
return this.op2("_sub", a, b);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
neg(a) {
return this.op1("_neg", a);
2020-07-11 11:31:52 +03:00
}
inv(a) {
2020-08-29 15:12:24 +03:00
return this.op1("_inverse", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
toMontgomery(a) {
return this.op1("_toMontgomery", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
fromMontgomery(a) {
return this.op1("_fromMontgomery", a);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
mul(a,b) {
return this.op2("_mul", a, b);
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
div(a, b) {
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
this.tm.instance.exports[this.prefix + "_inverse"](this.pOp2, this.pOp2);
this.tm.instance.exports[this.prefix + "_mul"](this.pOp1, this.pOp2, this.pOp3);
return this.tm.getBuff(this.pOp3, this.n8);
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
square(a) {
return this.op1("_square", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
isSquare(a) {
return this.op1Bool("_isSquare", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
sqrt(a) {
return this.op1("_sqrt", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
exp(a, b) {
if (!(b instanceof Uint8Array)) {
b = toLEBuff(e$2(b));
}
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
this.tm.instance.exports[this.prefix + "_exp"](this.pOp1, this.pOp2, b.byteLength, this.pOp3);
return this.tm.getBuff(this.pOp3, this.n8);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
isNegative(a) {
return this.op1Bool("_isNegative", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
e(a, b) {
if (a instanceof Uint8Array) return a;
let ra = e$2(a, b);
if (isNegative$2(ra)) {
ra = neg$2(ra);
if (gt$2(ra, this.p)) {
ra = mod$2(ra, this.p);
}
ra = sub$2(this.p, ra);
} else {
if (gt$2(ra, this.p)) {
ra = mod$2(ra, this.p);
}
}
const buff = leInt2Buff$2(ra, this.n8);
return this.toMontgomery(buff);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
toString(a, radix) {
const an = this.fromMontgomery(a);
const s = fromRprLE(an, 0);
return toString(s, radix);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
fromRng(rng) {
let v;
const buff = new Uint8Array(this.n8);
do {
v = zero;
for (let i=0; i<this.n64; i++) {
v = add$2(v, shiftLeft$2(rng.nextU64(), 64*i));
}
v = band$2(v, this.mask);
} while (geq$2(v, this.p));
toRprLE(buff, 0, v, this.n8);
return buff;
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
random() {
return this.fromRng(getThreadRng());
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
toObject(a) {
const an = this.fromMontgomery(a);
return fromRprLE(an, 0);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
fromObject(a) {
const buff = new Uint8Array(this.n8);
toRprLE(buff, 0, a, this.n8);
return this.toMontgomery(buff);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
toRprLE(buff, offset, a) {
buff.set(this.fromMontgomery(a), offset);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
fromRprLE(buff, offset) {
2020-10-20 20:24:45 +03:00
offset = offset || 0;
2020-08-29 15:12:24 +03:00
const res = buff.slice(offset, offset + this.n8);
return this.toMontgomery(res);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
class WasmField2 {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
constructor(tm, prefix, F) {
this.tm = tm;
this.prefix = prefix;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.F = F;
this.type = "F2";
this.m = F.m * 2;
this.n8 = this.F.n8*2;
this.n32 = this.F.n32*2;
this.n64 = this.F.n64*2;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.pOp1 = tm.alloc(F.n8*2);
this.pOp2 = tm.alloc(F.n8*2);
this.pOp3 = tm.alloc(F.n8*2);
this.tm.instance.exports[prefix + "_zero"](this.pOp1);
this.zero = tm.getBuff(this.pOp1, this.n8);
this.tm.instance.exports[prefix + "_one"](this.pOp1);
this.one = tm.getBuff(this.pOp1, this.n8);
this.negone = this.neg(this.one);
this.two = this.add(this.one, this.one);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
op2(opName, a, b) {
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp2, this.pOp3);
return this.tm.getBuff(this.pOp3, this.n8);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
op2Bool(opName, a, b) {
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
return !!this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp2);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
op1(opName, a) {
this.tm.setBuff(this.pOp1, a);
this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp3);
return this.tm.getBuff(this.pOp3, this.n8);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
op1Bool(opName, a) {
this.tm.setBuff(this.pOp1, a);
return !!this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp3);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
add(a,b) {
return this.op2("_add", a, b);
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
eq(a,b) {
return this.op2Bool("_eq", a, b);
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
isZero(a) {
return this.op1Bool("_isZero", a);
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
sub(a,b) {
return this.op2("_sub", a, b);
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
neg(a) {
return this.op1("_neg", a);
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
inv(a) {
return this.op1("_inverse", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
isNegative(a) {
return this.op1Bool("_isNegative", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
toMontgomery(a) {
return this.op1("_toMontgomery", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
fromMontgomery(a) {
return this.op1("_fromMontgomery", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
mul(a,b) {
return this.op2("_mul", a, b);
2020-07-11 11:31:52 +03:00
}
2020-09-25 08:38:22 +03:00
mul1(a,b) {
return this.op2("_mul1", a, b);
}
2020-08-29 15:12:24 +03:00
div(a, b) {
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
this.tm.instance.exports[this.prefix + "_inverse"](this.pOp2, this.pOp2);
this.tm.instance.exports[this.prefix + "_mul"](this.pOp1, this.pOp2, this.pOp3);
return this.tm.getBuff(this.pOp3, this.n8);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
square(a) {
return this.op1("_square", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
isSquare(a) {
return this.op1Bool("_isSquare", a);
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
sqrt(a) {
return this.op1("_sqrt", a);
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
exp(a, b) {
if (!(b instanceof Uint8Array)) {
b = toLEBuff(e$2(b));
}
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
this.tm.instance.exports[this.prefix + "_exp"](this.pOp1, this.pOp2, b.byteLength, this.pOp3);
return this.tm.getBuff(this.pOp3, this.n8);
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
e(a, b) {
if (a instanceof Uint8Array) return a;
if ((Array.isArray(a)) && (a.length == 2)) {
const c1 = this.F.e(a[0], b);
const c2 = this.F.e(a[1], b);
const res = new Uint8Array(this.F.n8*2);
res.set(c1);
res.set(c2, this.F.n8*2);
return res;
} else {
throw new Error("invalid F2");
}
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
toString(a, radix) {
const s1 = this.F.toString(a.slice(0, this.F.n8), radix);
const s2 = this.F.toString(a.slice(this.F.n8), radix);
return `[${s1}, ${s2}]`;
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
fromRng(rng) {
const c1 = this.F.fromRng(rng);
const c2 = this.F.fromRng(rng);
const res = new Uint8Array(this.F.n8*2);
res.set(c1);
res.set(c2, this.F.n8);
return res;
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
random() {
return this.fromRng(getThreadRng());
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
toObject(a) {
const c1 = this.F.toObject(a.slice(0, this.F.n8));
const c2 = this.F.toObject(a.slice(this.F.n8, this.F.n8*2));
return [c1, c2];
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
fromObject(a) {
const buff = new Uint8Array(this.F.n8*2);
const b1 = this.F.fromObject(a[0]);
const b2 = this.F.fromObject(a[1]);
buff.set(b1);
buff.set(b2, this.F.n8);
return buff;
}
2020-08-24 10:32:47 +03:00
2020-09-25 08:38:22 +03:00
c1(a) {
return a.slice(0, this.F.n8);
}
c2(a) {
return a.slice(this.F.n8);
}
2020-08-29 15:12:24 +03:00
}
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
class WasmField3 {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
constructor(tm, prefix, F) {
this.tm = tm;
this.prefix = prefix;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.F = F;
this.type = "F3";
this.m = F.m * 3;
this.n8 = this.F.n8*3;
this.n32 = this.F.n32*3;
this.n64 = this.F.n64*3;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.pOp1 = tm.alloc(F.n8*3);
this.pOp2 = tm.alloc(F.n8*3);
this.pOp3 = tm.alloc(F.n8*3);
this.tm.instance.exports[prefix + "_zero"](this.pOp1);
this.zero = tm.getBuff(this.pOp1, this.n8);
this.tm.instance.exports[prefix + "_one"](this.pOp1);
this.one = tm.getBuff(this.pOp1, this.n8);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.negone = this.neg(this.one);
this.two = this.add(this.one, this.one);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
op2(opName, a, b) {
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp2, this.pOp3);
return this.tm.getBuff(this.pOp3, this.n8);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
op2Bool(opName, a, b) {
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
return !!this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp2);
2020-08-24 10:32:47 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
op1(opName, a) {
this.tm.setBuff(this.pOp1, a);
this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp3);
return this.tm.getBuff(this.pOp3, this.n8);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
op1Bool(opName, a) {
this.tm.setBuff(this.pOp1, a);
return !!this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp3);
2020-08-24 10:32:47 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
eq(a,b) {
return this.op2Bool("_eq", a, b);
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
isZero(a) {
return this.op1Bool("_isZero", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
add(a,b) {
return this.op2("_add", a, b);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
sub(a,b) {
return this.op2("_sub", a, b);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
neg(a) {
return this.op1("_neg", a);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
inv(a) {
return this.op1("_inverse", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
isNegative(a) {
return this.op1Bool("_isNegative", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
toMontgomery(a) {
return this.op1("_toMontgomery", a);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
fromMontgomery(a) {
return this.op1("_fromMontgomery", a);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
mul(a,b) {
return this.op2("_mul", a, b);
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
div(a, b) {
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
this.tm.instance.exports[this.prefix + "_inverse"](this.pOp2, this.pOp2);
this.tm.instance.exports[this.prefix + "_mul"](this.pOp1, this.pOp2, this.pOp3);
return this.tm.getBuff(this.pOp3, this.n8);
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
square(a) {
return this.op1("_square", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
isSquare(a) {
return this.op1Bool("_isSquare", a);
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
sqrt(a) {
return this.op1("_sqrt", a);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
exp(a, b) {
if (!(b instanceof Uint8Array)) {
b = toLEBuff(e$2(b));
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
this.tm.instance.exports[this.prefix + "_exp"](this.pOp1, this.pOp2, b.byteLength, this.pOp3);
return this.getBuff(this.pOp3, this.n8);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
e(a, b) {
if (a instanceof Uint8Array) return a;
if ((Array.isArray(a)) && (a.length == 3)) {
const c1 = this.F.e(a[0], b);
const c2 = this.F.e(a[1], b);
const c3 = this.F.e(a[2], b);
const res = new Uint8Array(this.F.n8*3);
res.set(c1);
res.set(c2, this.F.n8);
res.set(c3, this.F.n8*2);
return res;
} else {
throw new Error("invalid F3");
}
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
toString(a, radix) {
const s1 = this.F.toString(a.slice(0, this.F.n8), radix);
const s2 = this.F.toString(a.slice(this.F.n8, this.F.n8*2), radix);
const s3 = this.F.toString(a.slice(this.F.n8*2), radix);
return `[${s1}, ${s2}, ${s3}]`;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
fromRng(rng) {
const c1 = this.F.fromRng(rng);
const c2 = this.F.fromRng(rng);
const c3 = this.F.fromRng(rng);
const res = new Uint8Array(this.F.n8*3);
res.set(c1);
res.set(c2, this.F.n8);
res.set(c3, this.F.n8*2);
return res;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
random() {
return this.fromRng(getThreadRng());
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
toObject(a) {
const c1 = this.F.toObject(a.slice(0, this.F.n8));
const c2 = this.F.toObject(a.slice(this.F.n8, this.F.n8*2));
const c3 = this.F.toObject(a.slice(this.F.n8*2, this.F.n8*3));
return [c1, c2, c3];
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
fromObject(a) {
const buff = new Uint8Array(this.F.n8*3);
const b1 = this.F.fromObject(a[0]);
const b2 = this.F.fromObject(a[1]);
const b3 = this.F.fromObject(a[2]);
buff.set(b1);
buff.set(b2, this.F.n8);
buff.set(b3, this.F.n8*2);
return buff;
}
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
c1(a) {
return a.slice(0, this.F.n8);
}
c2(a) {
return a.slice(this.F.n8, this.F.n8*2);
}
c3(a) {
return a.slice(this.F.n8*2);
}
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
class WasmCurve {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
constructor(tm, prefix, F, pGen, pGb, cofactor) {
this.tm = tm;
this.prefix = prefix;
this.F = F;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.pOp1 = tm.alloc(F.n8*3);
this.pOp2 = tm.alloc(F.n8*3);
this.pOp3 = tm.alloc(F.n8*3);
this.tm.instance.exports[prefix + "_zero"](this.pOp1);
this.zero = this.tm.getBuff(this.pOp1, F.n8*3);
this.tm.instance.exports[prefix + "_zeroAffine"](this.pOp1);
this.zeroAffine = this.tm.getBuff(this.pOp1, F.n8*2);
this.one = this.tm.getBuff(pGen, F.n8*3);
this.g = this.one;
this.oneAffine = this.tm.getBuff(pGen, F.n8*2);
this.gAffine = this.oneAffine;
this.b = this.tm.getBuff(pGb, F.n8);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (cofactor) {
this.cofactor = toLEBuff(cofactor);
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.negone = this.neg(this.one);
this.two = this.add(this.one, this.one);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.batchLEMtoC = buildBatchConvert(tm, prefix + "_batchLEMtoC", F.n8*2, F.n8);
this.batchLEMtoU = buildBatchConvert(tm, prefix + "_batchLEMtoU", F.n8*2, F.n8*2);
this.batchCtoLEM = buildBatchConvert(tm, prefix + "_batchCtoLEM", F.n8, F.n8*2);
this.batchUtoLEM = buildBatchConvert(tm, prefix + "_batchUtoLEM", F.n8*2, F.n8*2);
this.batchToJacobian = buildBatchConvert(tm, prefix + "_batchToJacobian", F.n8*2, F.n8*3);
this.batchToAffine = buildBatchConvert(tm, prefix + "_batchToAffine", F.n8*3, F.n8*2);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
op2(opName, a, b) {
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp2, this.pOp3);
return this.tm.getBuff(this.pOp3, this.F.n8*3);
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
op2bool(opName, a, b) {
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, b);
return !!this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp2, this.pOp3);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
op1(opName, a) {
this.tm.setBuff(this.pOp1, a);
this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp3);
return this.tm.getBuff(this.pOp3, this.F.n8*3);
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
op1Affine(opName, a) {
this.tm.setBuff(this.pOp1, a);
this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp3);
return this.tm.getBuff(this.pOp3, this.F.n8*2);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
op1Bool(opName, a) {
this.tm.setBuff(this.pOp1, a);
return !!this.tm.instance.exports[this.prefix + opName](this.pOp1, this.pOp3);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
add(a,b) {
if (a.byteLength == this.F.n8*3) {
if (b.byteLength == this.F.n8*3) {
return this.op2("_add", a, b);
} else if (b.byteLength == this.F.n8*2) {
return this.op2("_addMixed", a, b);
} else {
throw new Error("invalid point size");
}
} else if (a.byteLength == this.F.n8*2) {
if (b.byteLength == this.F.n8*3) {
return this.op2("_addMixed", b, a);
} else if (b.byteLength == this.F.n8*2) {
return this.op2("_addAffine", a, b);
} else {
throw new Error("invalid point size");
}
} else {
throw new Error("invalid point size");
}
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
sub(a,b) {
if (a.byteLength == this.F.n8*3) {
if (b.byteLength == this.F.n8*3) {
return this.op2("_sub", a, b);
} else if (b.byteLength == this.F.n8*2) {
return this.op2("_subMixed", a, b);
} else {
throw new Error("invalid point size");
}
} else if (a.byteLength == this.F.n8*2) {
if (b.byteLength == this.F.n8*3) {
return this.op2("_subMixed", b, a);
} else if (b.byteLength == this.F.n8*2) {
return this.op2("_subAffine", a, b);
} else {
throw new Error("invalid point size");
}
} else {
throw new Error("invalid point size");
}
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
neg(a) {
if (a.byteLength == this.F.n8*3) {
return this.op1("_neg", a);
} else if (a.byteLength == this.F.n8*2) {
return this.op1Affine("_negAffine", a);
} else {
throw new Error("invalid point size");
}
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
double(a) {
if (a.byteLength == this.F.n8*3) {
return this.op1("_double", a);
} else if (a.byteLength == this.F.n8*2) {
return this.op1("_doubleAffine", a);
} else {
throw new Error("invalid point size");
}
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
isZero(a) {
if (a.byteLength == this.F.n8*3) {
return this.op1Bool("_isZero", a);
} else if (a.byteLength == this.F.n8*2) {
return this.op1Bool("_isZeroAffine", a);
2020-08-24 10:32:47 +03:00
} else {
2020-08-29 15:12:24 +03:00
throw new Error("invalid point size");
}
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
timesScalar(a, s) {
if (!(s instanceof Uint8Array)) {
s = toLEBuff(e$2(s));
}
let fnName;
if (a.byteLength == this.F.n8*3) {
fnName = this.prefix + "_timesScalar";
} else if (a.byteLength == this.F.n8*2) {
fnName = this.prefix + "_timesScalarAffine";
} else {
throw new Error("invalid point size");
}
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, s);
this.tm.instance.exports[fnName](this.pOp1, this.pOp2, s.byteLength, this.pOp3);
return this.tm.getBuff(this.pOp3, this.F.n8*3);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
timesFr(a, s) {
let fnName;
if (a.byteLength == this.F.n8*3) {
fnName = this.prefix + "_timesFr";
} else if (a.byteLength == this.F.n8*2) {
fnName = this.prefix + "_timesFrAffine";
} else {
throw new Error("invalid point size");
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
this.tm.setBuff(this.pOp1, a);
this.tm.setBuff(this.pOp2, s);
this.tm.instance.exports[fnName](this.pOp1, this.pOp2, this.pOp3);
return this.tm.getBuff(this.pOp3, this.F.n8*3);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
eq(a,b) {
if (a.byteLength == this.F.n8*3) {
if (b.byteLength == this.F.n8*3) {
return this.op2bool("_eq", a, b);
} else if (b.byteLength == this.F.n8*2) {
return this.op2bool("_eqMixed", a, b);
2020-08-12 02:33:08 +03:00
} else {
2020-08-29 15:12:24 +03:00
throw new Error("invalid point size");
2020-08-12 02:33:08 +03:00
}
2020-08-29 15:12:24 +03:00
} else if (a.byteLength == this.F.n8*2) {
if (b.byteLength == this.F.n8*3) {
return this.op2bool("_eqMixed", b, a);
} else if (b.byteLength == this.F.n8*2) {
return this.op2bool("_eqAffine", a, b);
2020-08-12 02:33:08 +03:00
} else {
2020-08-29 15:12:24 +03:00
throw new Error("invalid point size");
2020-08-12 02:33:08 +03:00
}
2020-08-29 15:12:24 +03:00
} else {
throw new Error("invalid point size");
2020-08-12 02:33:08 +03:00
}
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
toAffine(a) {
if (a.byteLength == this.F.n8*3) {
return this.op1Affine("_toAffine", a);
} else if (a.byteLength == this.F.n8*2) {
return a;
} else {
throw new Error("invalid point size");
}
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
toJacobian(a) {
if (a.byteLength == this.F.n8*3) {
return a;
} else if (a.byteLength == this.F.n8*2) {
return this.op1("_toJacobian", a);
} else {
throw new Error("invalid point size");
}
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
toRprUncompressed(arr, offset, a) {
this.tm.setBuff(this.pOp1, a);
if (a.byteLength == this.F.n8*3) {
this.tm.instance.exports[this.prefix + "_toAffine"](this.pOp1, this.pOp1);
} else if (a.byteLength != this.F.n8*2) {
throw new Error("invalid point size");
}
this.tm.instance.exports[this.prefix + "_LEMtoU"](this.pOp1, this.pOp1);
const res = this.tm.getBuff(this.pOp1, this.F.n8*2);
arr.set(res, offset);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
fromRprUncompressed(arr, offset) {
const buff = arr.slice(offset, offset + this.F.n8*2);
this.tm.setBuff(this.pOp1, buff);
this.tm.instance.exports[this.prefix + "_UtoLEM"](this.pOp1, this.pOp1);
return this.tm.getBuff(this.pOp1, this.F.n8*2);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
toRprCompressed(arr, offset, a) {
this.tm.setBuff(this.pOp1, a);
if (a.byteLength == this.F.n8*3) {
this.tm.instance.exports[this.prefix + "_toAffine"](this.pOp1, this.pOp1);
} else if (a.byteLength != this.F.n8*2) {
throw new Error("invalid point size");
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
this.tm.instance.exports[this.prefix + "_LEMtoC"](this.pOp1, this.pOp1);
const res = this.tm.getBuff(this.pOp1, this.F.n8);
arr.set(res, offset);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
fromRprCompressed(arr, offset) {
const buff = arr.slice(offset, offset + this.F.n8);
this.tm.setBuff(this.pOp1, buff);
this.tm.instance.exports[this.prefix + "_CtoLEM"](this.pOp1, this.pOp2);
return this.tm.getBuff(this.pOp2, this.F.n8*2);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
toUncompressed(a) {
const buff = new Uint8Array(this.F.n8*2);
this.toRprUncompressed(buff, 0, a);
return buff;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
toRprLEM(arr, offset, a) {
if (a.byteLength == this.F.n8*2) {
arr.set(a, offset);
2020-08-24 10:32:47 +03:00
return;
2020-08-29 15:12:24 +03:00
} else if (a.byteLength == this.F.n8*3) {
this.tm.setBuff(this.pOp1, a);
this.tm.instance.exports[this.prefix + "_toAffine"](this.pOp1, this.pOp1);
const res = this.tm.getBuff(this.pOp1, this.F.n8*2);
arr.set(res, offset);
} else {
throw new Error("invalid point size");
2020-08-12 02:33:08 +03:00
}
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
fromRprLEM(arr, offset) {
offset = offset || 0;
return arr.slice(offset, offset+this.F.n8*2);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
toString(a, radix) {
if (a.byteLength == this.F.n8*3) {
const x = this.F.toString(a.slice(0, this.F.n8), radix);
const y = this.F.toString(a.slice(this.F.n8, this.F.n8*2), radix);
const z = this.F.toString(a.slice(this.F.n8*2), radix);
return `[ ${x}, ${y}, ${z} ]`;
} else if (a.byteLength == this.F.n8*2) {
const x = this.F.toString(a.slice(0, this.F.n8), radix);
const y = this.F.toString(a.slice(this.F.n8), radix);
return `[ ${x}, ${y} ]`;
} else {
throw new Error("invalid point size");
2020-08-12 02:33:08 +03:00
}
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
fromRng(rng) {
const F = this.F;
let P = [];
let greatest;
let x3b;
do {
P[0] = F.fromRng(rng);
greatest = rng.nextBool();
x3b = F.add(F.mul(F.square(P[0]), P[0]), this.b);
} while (!F.isSquare(x3b));
P[1] = F.sqrt(x3b);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const s = F.isNegative(P[1]);
if (greatest ^ s) P[1] = F.neg(P[1]);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let Pbuff = new Uint8Array(this.F.n8*2);
Pbuff.set(P[0]);
Pbuff.set(P[1], this.F.n8);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (this.cofactor) {
Pbuff = this.timesScalar(Pbuff, this.cofactor);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return Pbuff;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
toObject(a) {
if (this.isZero(a)) {
return [
this.F.toObject(this.F.zero),
this.F.toObject(this.F.one),
this.F.toObject(this.F.zero),
];
}
const x = this.F.toObject(a.slice(0, this.F.n8));
const y = this.F.toObject(a.slice(this.F.n8, this.F.n8*2));
let z;
if (a.byteLength == this.F.n8*3) {
z = this.F.toObject(a.slice(this.F.n8*2, this.F.n8*3));
} else {
z = this.F.toObject(this.F.one);
2020-08-24 10:32:47 +03:00
}
return [x, y, z];
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
fromObject(a) {
const x = this.F.fromObject(a[0]);
const y = this.F.fromObject(a[1]);
let z;
if (a.length==3) {
z = this.F.fromObject(a[2]);
} else {
z = this.F.one;
}
if (this.F.isZero(z, this.F.one)) {
return this.zeroAffine;
} else if (this.F.eq(z, this.F.one)) {
const buff = new Uint8Array(this.F.n8*2);
buff.set(x);
buff.set(y, this.F.n8);
return buff;
} else {
const buff = new Uint8Array(this.F.n8*3);
buff.set(x);
buff.set(y, this.F.n8);
buff.set(z, this.F.n8*2);
return buff;
2020-08-24 10:32:47 +03:00
}
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
e(a) {
if (a instanceof Uint8Array) return a;
return this.fromObject(a);
}
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
x(a) {
const tmp = this.toAffine(a);
return tmp.slice(0, this.F.n8);
}
y(a) {
const tmp = this.toAffine(a);
return tmp.slice(this.F.n8);
}
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
/* global WebAssembly */
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function thread(self) {
2020-10-08 12:43:05 +03:00
const MAXMEM = 32767;
2020-08-29 15:12:24 +03:00
let instance;
let memory;
2020-08-13 01:44:40 +03:00
2020-08-29 15:12:24 +03:00
if (self) {
self.onmessage = function(e) {
let data;
if (e.data) {
data = e.data;
} else {
data = e;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (data[0].cmd == "INIT") {
init(data[0]).then(function() {
self.postMessage(data.result);
});
} else if (data[0].cmd == "TERMINATE") {
process.exit();
} else {
const res = runTask(data);
self.postMessage(res);
}
};
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async function init(data) {
const code = new Uint8Array(data.code);
const wasmModule = await WebAssembly.compile(code);
2020-10-08 12:43:05 +03:00
memory = new WebAssembly.Memory({initial:data.init, maximum: MAXMEM});
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
instance = await WebAssembly.instantiate(wasmModule, {
env: {
"memory": memory
}
});
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
function alloc(length) {
const u32 = new Uint32Array(memory.buffer, 0, 1);
while (u32[0] & 3) u32[0]++; // Return always aligned pointers
const res = u32[0];
u32[0] += length;
if (u32[0] + length > memory.buffer.byteLength) {
const currentPages = memory.buffer.byteLength / 0x10000;
let requiredPages = Math.floor((u32[0] + length) / 0x10000)+1;
2020-10-08 12:43:05 +03:00
if (requiredPages>MAXMEM) requiredPages=MAXMEM;
2020-08-29 15:12:24 +03:00
memory.grow(requiredPages-currentPages);
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
return res;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function allocBuffer(buffer) {
const p = alloc(buffer.byteLength);
setBuffer(p, buffer);
return p;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function getBuffer(pointer, length) {
const u8 = new Uint8Array(memory.buffer);
return new Uint8Array(u8.buffer, u8.byteOffset + pointer, length);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function setBuffer(pointer, buffer) {
const u8 = new Uint8Array(memory.buffer);
u8.set(new Uint8Array(buffer), pointer);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function runTask(task) {
if (task[0].cmd == "INIT") {
return init(task[0]);
}
const ctx = {
vars: [],
out: []
};
const u32a = new Uint32Array(memory.buffer, 0, 1);
const oldAlloc = u32a[0];
for (let i=0; i<task.length; i++) {
switch (task[i].cmd) {
case "ALLOCSET":
ctx.vars[task[i].var] = allocBuffer(task[i].buff);
break;
case "ALLOC":
ctx.vars[task[i].var] = alloc(task[i].len);
break;
case "SET":
setBuffer(ctx.vars[task[i].var], task[i].buff);
break;
case "CALL": {
const params = [];
for (let j=0; j<task[i].params.length; j++) {
const p = task[i].params[j];
if (typeof p.var !== "undefined") {
params.push(ctx.vars[p.var] + (p.offset || 0));
} else if (typeof p.val != "undefined") {
params.push(p.val);
}
}
instance.exports[task[i].fnName](...params);
break;
}
case "GET":
ctx.out[task[i].out] = getBuffer(ctx.vars[task[i].var], task[i].len).slice();
break;
default:
throw new Error("Invalid cmd");
}
}
const u32b = new Uint32Array(memory.buffer, 0, 1);
u32b[0] = oldAlloc;
return ctx.out;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return runTask;
}
2020-07-11 11:31:52 +03:00
2021-01-17 11:56:08 +03:00
var os = {};
2021-02-10 11:53:04 +03:00
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var browser = Worker;
2020-08-29 15:12:24 +03:00
/* global window, navigator, Blob, Worker, WebAssembly */
2020-08-24 10:32:47 +03:00
/*
Copyright 2019 0KIMS association.
2020-07-11 11:31:52 +03:00
2020-08-24 10:32:47 +03:00
This file is part of wasmsnark (Web Assembly zkSnark Prover).
2020-07-11 11:31:52 +03:00
2020-08-24 10:32:47 +03:00
wasmsnark is a free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2020-07-11 11:31:52 +03:00
2020-08-24 10:32:47 +03:00
wasmsnark is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
2020-07-11 11:31:52 +03:00
2020-08-24 10:32:47 +03:00
You should have received a copy of the GNU General Public License
along with wasmsnark. If not, see <https://www.gnu.org/licenses/>.
*/
2020-07-11 11:31:52 +03:00
2020-10-08 12:43:05 +03:00
// const MEM_SIZE = 1000; // Memory size in 64K Pakes (512Mb)
const MEM_SIZE = 25; // Memory size in 64K Pakes (1600Kb)
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
class Deferred {
constructor() {
this.promise = new Promise((resolve, reject)=> {
this.reject = reject;
this.resolve = resolve;
2020-08-24 10:32:47 +03:00
});
}
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function base64ToArrayBuffer(base64) {
{
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
return bytes;
2020-08-24 10:32:47 +03:00
}
}
2020-07-11 11:31:52 +03:00
2021-02-10 11:53:04 +03:00
function stringToBase64(str) {
{
return window.btoa(str);
}
}
const threadSource = stringToBase64("(" + thread.toString() + ")(self)");
const workerSource = "data:application/javascript;base64," + threadSource;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async function buildThreadManager(wasm, singleThread) {
const tm = new ThreadManager();
tm.memory = new WebAssembly.Memory({initial:MEM_SIZE});
tm.u8 = new Uint8Array(tm.memory.buffer);
tm.u32 = new Uint32Array(tm.memory.buffer);
const wasmModule = await WebAssembly.compile(base64ToArrayBuffer(wasm.code));
tm.instance = await WebAssembly.instantiate(wasmModule, {
env: {
"memory": tm.memory
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
});
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
tm.singleThread = singleThread;
tm.initalPFree = tm.u32[0]; // Save the Pointer to free space.
tm.pq = wasm.pq;
tm.pr = wasm.pr;
tm.pG1gen = wasm.pG1gen;
tm.pG1zero = wasm.pG1zero;
tm.pG2gen = wasm.pG2gen;
tm.pG2zero = wasm.pG2zero;
tm.pOneT = wasm.pOneT;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
// tm.pTmp0 = tm.alloc(curve.G2.F.n8*3);
// tm.pTmp1 = tm.alloc(curve.G2.F.n8*3);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (singleThread) {
tm.code = base64ToArrayBuffer(wasm.code);
tm.taskManager = thread();
await tm.taskManager([{
cmd: "INIT",
init: MEM_SIZE,
code: tm.code.slice()
}]);
tm.concurrency = 1;
2020-08-24 10:32:47 +03:00
} else {
2020-08-29 15:12:24 +03:00
tm.workers = [];
tm.pendingDeferreds = [];
tm.working = [];
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let concurrency;
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
if ((typeof(navigator) === "object") && navigator.hardwareConcurrency) {
concurrency = navigator.hardwareConcurrency;
} else {
2021-01-17 11:56:08 +03:00
concurrency = os.cpus().length;
2020-08-29 15:12:24 +03:00
}
2020-10-08 12:43:05 +03:00
// Limit to 64 threads for memory reasons.
if (concurrency>64) concurrency=64;
2020-08-29 15:12:24 +03:00
tm.concurrency = concurrency;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
for (let i = 0; i<concurrency; i++) {
2020-08-12 02:33:08 +03:00
2021-02-10 11:53:04 +03:00
tm.workers[i] = new browser(workerSource);
2020-08-12 02:33:08 +03:00
2021-02-10 11:53:04 +03:00
tm.workers[i].addEventListener("message", getOnMsg(i));
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
tm.working[i]=false;
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const initPromises = [];
for (let i=0; i<tm.workers.length;i++) {
const copyCode = base64ToArrayBuffer(wasm.code).slice();
initPromises.push(tm.postAction(i, [{
cmd: "INIT",
init: MEM_SIZE,
code: copyCode
}], [copyCode.buffer]));
}
await Promise.all(initPromises);
2020-08-12 02:33:08 +03:00
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
return tm;
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
function getOnMsg(i) {
return function(e) {
let data;
if ((e)&&(e.data)) {
data = e.data;
} else {
data = e;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
tm.working[i]=false;
tm.pendingDeferreds[i].resolve(data);
tm.processWorks();
};
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
class ThreadManager {
constructor() {
this.actionQueue = [];
this.oldPFree = 0;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
startSyncOp() {
if (this.oldPFree != 0) throw new Error("Sync operation in progress");
this.oldPFree = this.u32[0];
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
endSyncOp() {
if (this.oldPFree == 0) throw new Error("No sync operation in progress");
this.u32[0] = this.oldPFree;
this.oldPFree = 0;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
postAction(workerId, e, transfers, _deferred) {
if (this.working[workerId]) {
throw new Error("Posting a job t a working worker");
}
this.working[workerId] = true;
this.pendingDeferreds[workerId] = _deferred ? _deferred : new Deferred();
this.workers[workerId].postMessage(e, transfers);
return this.pendingDeferreds[workerId].promise;
}
processWorks() {
for (let i=0; (i<this.workers.length)&&(this.actionQueue.length > 0); i++) {
if (this.working[i] == false) {
const work = this.actionQueue.shift();
this.postAction(i, work.data, work.transfers, work.deferred);
}
}
}
queueAction(actionData, transfers) {
const d = new Deferred();
if (this.singleThread) {
const res = this.taskManager(actionData);
d.resolve(res);
} else {
this.actionQueue.push({
data: actionData,
transfers: transfers,
deferred: d
});
this.processWorks();
}
return d.promise;
}
resetMemory() {
this.u32[0] = this.initalPFree;
}
allocBuff(buff) {
const pointer = this.alloc(buff.byteLength);
this.setBuff(pointer, buff);
return pointer;
}
getBuff(pointer, length) {
return this.u8.slice(pointer, pointer+ length);
}
setBuff(pointer, buffer) {
this.u8.set(new Uint8Array(buffer), pointer);
}
alloc(length) {
while (this.u32[0] & 3) this.u32[0]++; // Return always aligned pointers
const res = this.u32[0];
this.u32[0] += length;
return res;
}
async terminate() {
for (let i=0; i<this.workers.length; i++) {
this.workers[i].postMessage([{cmd: "TERMINATE"}]);
}
await sleep(200);
}
}
function buildBatchApplyKey(curve, groupName) {
const G = curve[groupName];
const Fr = curve.Fr;
const tm = curve.tm;
curve[groupName].batchApplyKey = async function(buff, first, inc, inType, outType) {
inType = inType || "affine";
outType = outType || "affine";
let fnName, fnAffine;
let sGin, sGmid, sGout;
if (groupName == "G1") {
if (inType == "jacobian") {
sGin = G.F.n8*3;
fnName = "g1m_batchApplyKey";
} else {
sGin = G.F.n8*2;
fnName = "g1m_batchApplyKeyMixed";
}
sGmid = G.F.n8*3;
if (outType == "jacobian") {
sGout = G.F.n8*3;
} else {
fnAffine = "g1m_batchToAffine";
sGout = G.F.n8*2;
}
} else if (groupName == "G2") {
if (inType == "jacobian") {
sGin = G.F.n8*3;
fnName = "g2m_batchApplyKey";
} else {
sGin = G.F.n8*2;
fnName = "g2m_batchApplyKeyMixed";
}
sGmid = G.F.n8*3;
if (outType == "jacobian") {
sGout = G.F.n8*3;
} else {
fnAffine = "g2m_batchToAffine";
sGout = G.F.n8*2;
}
} else if (groupName == "Fr") {
fnName = "frm_batchApplyKey";
sGin = G.n8;
sGmid = G.n8;
sGout = G.n8;
} else {
throw new Error("Invalid group: " + groupName);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
const nPoints = Math.floor(buff.byteLength / sGin);
2020-08-24 10:32:47 +03:00
const pointsPerChunk = Math.floor(nPoints/tm.concurrency);
2020-08-12 02:33:08 +03:00
const opPromises = [];
2020-08-29 15:12:24 +03:00
inc = Fr.e(inc);
let t = Fr.e(first);
2020-08-12 02:33:08 +03:00
for (let i=0; i<tm.concurrency; i++) {
let n;
if (i< tm.concurrency-1) {
n = pointsPerChunk;
} else {
n = nPoints - i*pointsPerChunk;
}
if (n==0) continue;
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
const task = [];
task.push({
cmd: "ALLOCSET",
var: 0,
buff: buff.slice(i*pointsPerChunk*sGin, i*pointsPerChunk*sGin + n*sGin)
});
task.push({cmd: "ALLOCSET", var: 1, buff: t});
task.push({cmd: "ALLOCSET", var: 2, buff: inc});
task.push({cmd: "ALLOC", var: 3, len: n*Math.max(sGmid, sGout)});
task.push({
cmd: "CALL",
fnName: fnName,
params: [
2020-08-24 10:32:47 +03:00
{var: 0},
{val: n},
2020-08-29 15:12:24 +03:00
{var: 1},
{var: 2},
{var:3}
]
});
if (fnAffine) {
task.push({
cmd: "CALL",
fnName: fnAffine,
params: [
{var: 3},
{val: n},
{var: 3},
]
});
}
task.push({cmd: "GET", out: 0, var: 3, len: n*sGout});
opPromises.push(tm.queueAction(task));
t = Fr.mul(t, Fr.exp(inc, n));
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-12 02:33:08 +03:00
const result = await Promise.all(opPromises);
2020-07-11 11:31:52 +03:00
2020-10-25 12:26:07 +03:00
let outBuff;
if (buff instanceof BigBuffer) {
outBuff = new BigBuffer(nPoints*sGout);
} else {
outBuff = new Uint8Array(nPoints*sGout);
}
2020-08-29 15:12:24 +03:00
let p=0;
2020-08-24 10:32:47 +03:00
for (let i=0; i<result.length; i++) {
2020-08-29 15:12:24 +03:00
outBuff.set(result[i][0], p);
p += result[i][0].byteLength;
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
return outBuff;
2020-08-12 02:33:08 +03:00
};
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function buildPairing(curve) {
const tm = curve.tm;
curve.pairing = function pairing(a, b) {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
tm.startSyncOp();
const pA = tm.allocBuff(curve.G1.toJacobian(a));
const pB = tm.allocBuff(curve.G2.toJacobian(b));
const pRes = tm.alloc(curve.Gt.n8);
tm.instance.exports[curve.name + "_pairing"](pA, pB, pRes);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const res = tm.getBuff(pRes, curve.Gt.n8);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
tm.endSyncOp();
return res;
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
curve.pairingEq = async function pairingEq() {
let buffCt;
let nEqs;
if ((arguments.length % 2) == 1) {
buffCt = arguments[arguments.length-1];
nEqs = (arguments.length -1) /2;
} else {
buffCt = curve.Gt.one;
nEqs = arguments.length /2;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const opPromises = [];
for (let i=0; i<nEqs; i++) {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const task = [];
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const g1Buff = curve.G1.toJacobian(arguments[i*2]);
task.push({cmd: "ALLOCSET", var: 0, buff: g1Buff});
task.push({cmd: "ALLOC", var: 1, len: curve.prePSize});
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const g2Buff = curve.G2.toJacobian(arguments[i*2 +1]);
task.push({cmd: "ALLOCSET", var: 2, buff: g2Buff});
task.push({cmd: "ALLOC", var: 3, len: curve.preQSize});
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
task.push({cmd: "ALLOC", var: 4, len: curve.Gt.n8});
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
task.push({cmd: "CALL", fnName: curve.name + "_prepareG1", params: [
{var: 0},
{var: 1}
]});
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
task.push({cmd: "CALL", fnName: curve.name + "_prepareG2", params: [
{var: 2},
{var: 3}
]});
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
task.push({cmd: "CALL", fnName: curve.name + "_millerLoop", params: [
{var: 1},
{var: 3},
{var: 4}
]});
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
task.push({cmd: "GET", out: 0, var: 4, len: curve.Gt.n8});
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
opPromises.push(
tm.queueAction(task)
);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const result = await Promise.all(opPromises);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
tm.startSyncOp();
const pRes = tm.alloc(curve.Gt.n8);
tm.instance.exports.ftm_one(pRes);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
for (let i=0; i<result.length; i++) {
const pMR = tm.allocBuff(result[i][0]);
tm.instance.exports.ftm_mul(pRes, pMR, pRes);
}
tm.instance.exports[curve.name + "_finalExponentiation"](pRes, pRes);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const pCt = tm.allocBuff(buffCt);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const r = !!tm.instance.exports.ftm_eq(pRes, pCt);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
tm.endSyncOp();
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return r;
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
curve.prepareG1 = function(p) {
this.tm.startSyncOp();
const pP = this.tm.allocBuff(p);
const pPrepP = this.tm.alloc(this.prePSize);
this.tm.instance.exports[this.name + "_prepareG1"](pP, pPrepP);
const res = this.tm.getBuff(pPrepP, this.prePSize);
this.tm.endSyncOp();
return res;
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
curve.prepareG2 = function(q) {
this.tm.startSyncOp();
const pQ = this.tm.allocBuff(q);
const pPrepQ = this.tm.alloc(this.preQSize);
this.tm.instance.exports[this.name + "_prepareG2"](pQ, pPrepQ);
const res = this.tm.getBuff(pPrepQ, this.preQSize);
this.tm.endSyncOp();
return res;
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
curve.millerLoop = function(preP, preQ) {
this.tm.startSyncOp();
const pPreP = this.tm.allocBuff(preP);
const pPreQ = this.tm.allocBuff(preQ);
const pRes = this.tm.alloc(this.Gt.n8);
this.tm.instance.exports[this.name + "_millerLoop"](pPreP, pPreQ, pRes);
const res = this.tm.getBuff(pRes, this.Gt.n8);
this.tm.endSyncOp();
return res;
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
curve.finalExponentiation = function(a) {
this.tm.startSyncOp();
const pA = this.tm.allocBuff(a);
const pRes = this.tm.alloc(this.Gt.n8);
this.tm.instance.exports[this.name + "_finalExponentiation"](pA, pRes);
const res = this.tm.getBuff(pRes, this.Gt.n8);
this.tm.endSyncOp();
return res;
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const pTSizes = [
1 , 1, 1, 1, 2, 3, 4, 5,
6 , 7, 7, 8, 9, 10, 11, 12,
13, 13, 14, 15, 16, 16, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17
];
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function buildMultiexp(curve, groupName) {
const G = curve[groupName];
const tm = G.tm;
2020-09-13 11:42:48 +03:00
async function _multiExpChunk(buffBases, buffScalars, inType, logger, logText) {
if ( ! (buffBases instanceof Uint8Array) ) {
if (logger) logger.error(`${logText} _multiExpChunk buffBases is not Uint8Array`);
throw new Error(`${logText} _multiExpChunk buffBases is not Uint8Array`);
}
if ( ! (buffScalars instanceof Uint8Array) ) {
if (logger) logger.error(`${logText} _multiExpChunk buffScalars is not Uint8Array`);
throw new Error(`${logText} _multiExpChunk buffScalars is not Uint8Array`);
}
2020-08-29 15:12:24 +03:00
inType = inType || "affine";
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let sGIn;
let fnName;
if (groupName == "G1") {
if (inType == "affine") {
fnName = "g1m_multiexpAffine_chunk";
sGIn = G.F.n8*2;
} else {
fnName = "g1m_multiexp_chunk";
sGIn = G.F.n8*3;
}
} else if (groupName == "G2") {
if (inType == "affine") {
fnName = "g2m_multiexpAffine_chunk";
sGIn = G.F.n8*2;
} else {
fnName = "g2m_multiexp_chunk";
sGIn = G.F.n8*3;
}
} else {
throw new Error("Invalid group");
}
const nPoints = Math.floor(buffBases.byteLength / sGIn);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (nPoints == 0) return G.zero;
const sScalar = Math.floor(buffScalars.byteLength / nPoints);
if( sScalar * nPoints != buffScalars.byteLength) {
throw new Error("Scalar size does not match");
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const bitChunkSize = pTSizes[log2(nPoints)];
const nChunks = Math.floor((sScalar*8 - 1) / bitChunkSize) +1;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const opPromises = [];
for (let i=0; i<nChunks; i++) {
const task = [
{cmd: "ALLOCSET", var: 0, buff: buffBases},
{cmd: "ALLOCSET", var: 1, buff: buffScalars},
{cmd: "ALLOC", var: 2, len: G.F.n8*3},
{cmd: "CALL", fnName: fnName, params: [
{var: 0},
{var: 1},
{val: sScalar},
{val: nPoints},
{val: i*bitChunkSize},
{val: Math.min(sScalar*8 - i*bitChunkSize, bitChunkSize)},
{var: 2}
]},
{cmd: "GET", out: 0, var: 2, len: G.F.n8*3}
];
opPromises.push(
G.tm.queueAction(task)
);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const result = await Promise.all(opPromises);
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
let res = G.zero;
for (let i=result.length-1; i>=0; i--) {
if (!G.isZero(res)) {
for (let j=0; j<bitChunkSize; j++) res = G.double(res);
}
res = G.add(res, result[i][0]);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
return res;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async function _multiExp(buffBases, buffScalars, inType, logger, logText) {
const MAX_CHUNK_SIZE = 1 << 22;
const MIN_CHUNK_SIZE = 1 << 10;
let sGIn;
if (groupName == "G1") {
if (inType == "affine") {
sGIn = G.F.n8*2;
} else {
sGIn = G.F.n8*3;
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
} else if (groupName == "G2") {
if (inType == "affine") {
sGIn = G.F.n8*2;
} else {
sGIn = G.F.n8*3;
}
} else {
throw new Error("Invalid group");
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const nPoints = Math.floor(buffBases.byteLength / sGIn);
const sScalar = Math.floor(buffScalars.byteLength / nPoints);
if( sScalar * nPoints != buffScalars.byteLength) {
throw new Error("Scalar size does not match");
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const bitChunkSize = pTSizes[log2(nPoints)];
const nChunks = Math.floor((sScalar*8 - 1) / bitChunkSize) +1;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let chunkSize;
chunkSize = Math.floor(nPoints / (tm.concurrency /nChunks));
if (chunkSize>MAX_CHUNK_SIZE) chunkSize = MAX_CHUNK_SIZE;
if (chunkSize<MIN_CHUNK_SIZE) chunkSize = MIN_CHUNK_SIZE;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const opPromises = [];
for (let i=0; i<nPoints; i += chunkSize) {
2020-09-09 10:03:41 +03:00
if (logger) logger.debug(`Multiexp start: ${logText}: ${i}/${nPoints}`);
2020-08-29 15:12:24 +03:00
const n= Math.min(nPoints - i, chunkSize);
2020-09-09 10:03:41 +03:00
const buffBasesChunk = buffBases.slice(i*sGIn, (i+n)*sGIn);
const buffScalarsChunk = buffScalars.slice(i*sScalar, (i+n)*sScalar);
2020-09-13 11:42:48 +03:00
opPromises.push(_multiExpChunk(buffBasesChunk, buffScalarsChunk, inType, logger, logText).then( (r) => {
2020-09-09 10:03:41 +03:00
if (logger) logger.debug(`Multiexp end: ${logText}: ${i}/${nPoints}`);
return r;
}));
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const result = await Promise.all(opPromises);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let res = G.zero;
for (let i=result.length-1; i>=0; i--) {
res = G.add(res, result[i]);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return res;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
G.multiExp = async function multiExpAffine(buffBases, buffScalars, logger, logText) {
return await _multiExp(buffBases, buffScalars, "jacobian", logger, logText);
};
G.multiExpAffine = async function multiExpAffine(buffBases, buffScalars, logger, logText) {
return await _multiExp(buffBases, buffScalars, "affine", logger, logText);
};
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function buildFFT(curve, groupName) {
const G = curve[groupName];
const Fr = curve.Fr;
const tm = G.tm;
2020-09-07 13:43:50 +03:00
async function _fft(buff, inverse, inType, outType, logger, loggerTxt) {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
inType = inType || "affine";
outType = outType || "affine";
2020-09-07 13:43:50 +03:00
const MAX_BITS_THREAD = 14;
2020-07-11 11:31:52 +03:00
2020-09-07 13:43:50 +03:00
let sIn, sMid, sOut, fnIn2Mid, fnMid2Out, fnFFTMix, fnFFTJoin, fnFFTFinal;
2020-08-29 15:12:24 +03:00
if (groupName == "G1") {
if (inType == "affine") {
sIn = G.F.n8*2;
fnIn2Mid = "g1m_batchToJacobian";
} else {
sIn = G.F.n8*3;
}
sMid = G.F.n8*3;
if (inverse) {
fnFFTFinal = "g1m_fftFinal";
}
fnFFTJoin = "g1m_fftJoin";
fnFFTMix = "g1m_fftMix";
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (outType == "affine") {
sOut = G.F.n8*2;
fnMid2Out = "g1m_batchToAffine";
} else {
sOut = G.F.n8*3;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
} else if (groupName == "G2") {
if (inType == "affine") {
sIn = G.F.n8*2;
fnIn2Mid = "g2m_batchToJacobian";
} else {
sIn = G.F.n8*3;
}
sMid = G.F.n8*3;
if (inverse) {
fnFFTFinal = "g2m_fftFinal";
}
fnFFTJoin = "g2m_fftJoin";
fnFFTMix = "g2m_fftMix";
if (outType == "affine") {
sOut = G.F.n8*2;
fnMid2Out = "g2m_batchToAffine";
} else {
sOut = G.F.n8*3;
}
} else if (groupName == "Fr") {
sIn = G.n8;
sMid = G.n8;
sOut = G.n8;
if (inverse) {
fnFFTFinal = "frm_fftFinal";
}
fnFFTMix = "frm_fftMix";
fnFFTJoin = "frm_fftJoin";
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let returnArray = false;
if (Array.isArray(buff)) {
buff = curve.array2buffer(buff, sIn);
returnArray = true;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const nPoints = buff.byteLength / sIn;
const bits = log2(nPoints);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if ((1 << bits) != nPoints) {
throw new Error("fft must be multiple of 2" );
}
2020-07-11 11:31:52 +03:00
2020-09-07 13:43:50 +03:00
if (bits == Fr.s +1) {
let buffOut;
if (inverse) {
buffOut = await _fftExtInv(buff, inType, outType, logger, loggerTxt);
} else {
buffOut = await _fftExt(buff, inType, outType, logger, loggerTxt);
}
if (returnArray) {
return curve.buffer2array(buffOut, sOut);
} else {
return buffOut;
}
}
2020-08-29 15:12:24 +03:00
let inv;
if (inverse) {
inv = Fr.inv(Fr.e(nPoints));
}
2020-07-11 11:31:52 +03:00
2020-09-07 13:43:50 +03:00
let buffOut;
buffReverseBits(buff, sIn);
2020-07-11 11:31:52 +03:00
2020-09-07 13:43:50 +03:00
let chunks;
let pointsInChunk = Math.min(1 << MAX_BITS_THREAD, nPoints);
let nChunks = nPoints / pointsInChunk;
2020-07-11 11:31:52 +03:00
2020-09-07 13:43:50 +03:00
while ((nChunks < tm.concurrency)&&(pointsInChunk>=16)) {
nChunks *= 2;
pointsInChunk /= 2;
}
const l2Chunk = log2(pointsInChunk);
const promises = [];
for (let i = 0; i< nChunks; i++) {
2020-09-09 10:03:41 +03:00
if (logger) logger.debug(`${loggerTxt}: fft ${bits} mix start: ${i}/${nChunks}`);
2020-08-29 15:12:24 +03:00
const task = [];
2020-09-07 13:43:50 +03:00
task.push({cmd: "ALLOC", var: 0, len: sMid*pointsInChunk});
const buffChunk = buff.slice( (pointsInChunk * i)*sIn, (pointsInChunk * (i+1))*sIn);
task.push({cmd: "SET", var: 0, buff: buffChunk});
2020-08-29 15:12:24 +03:00
if (fnIn2Mid) {
2020-09-07 13:43:50 +03:00
task.push({cmd: "CALL", fnName:fnIn2Mid, params: [{var:0}, {val: pointsInChunk}, {var: 0}]});
2020-08-29 15:12:24 +03:00
}
2020-09-07 13:43:50 +03:00
for (let j=1; j<=l2Chunk;j++) {
task.push({cmd: "CALL", fnName:fnFFTMix, params: [{var:0}, {val: pointsInChunk}, {val: j}]});
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-09-07 13:43:50 +03:00
if (l2Chunk==bits) {
if (fnFFTFinal) {
task.push({cmd: "ALLOCSET", var: 1, buff: inv});
task.push({cmd: "CALL", fnName: fnFFTFinal, params:[
{var: 0},
{val: pointsInChunk},
{var: 1},
]});
2020-08-29 15:12:24 +03:00
}
2020-09-07 13:43:50 +03:00
if (fnMid2Out) {
task.push({cmd: "CALL", fnName:fnMid2Out, params: [{var:0}, {val: pointsInChunk}, {var: 0}]});
2020-08-29 15:12:24 +03:00
}
2020-09-07 13:43:50 +03:00
task.push({cmd: "GET", out: 0, var: 0, len: pointsInChunk*sOut});
} else {
2020-08-29 15:12:24 +03:00
task.push({cmd: "GET", out:0, var: 0, len: sMid*pointsInChunk});
}
2020-09-07 13:43:50 +03:00
promises.push(tm.queueAction(task).then( (r) => {
2020-09-09 10:03:41 +03:00
if (logger) logger.debug(`${loggerTxt}: fft ${bits} mix end: ${i}/${nChunks}`);
2020-09-07 13:43:50 +03:00
return r;
}));
}
2020-07-11 11:31:52 +03:00
2020-09-07 13:43:50 +03:00
chunks = await Promise.all(promises);
for (let i = 0; i< nChunks; i++) chunks[i] = chunks[i][0];
2020-08-12 02:33:08 +03:00
2020-09-07 13:43:50 +03:00
for (let i = l2Chunk+1; i<=bits; i++) {
if (logger) logger.debug(`${loggerTxt}: fft ${bits} join: ${i}/${bits}`);
const nGroups = 1 << (bits - i);
const nChunksPerGroup = nChunks / nGroups;
const opPromises = [];
for (let j=0; j<nGroups; j++) {
for (let k=0; k <nChunksPerGroup/2; k++) {
const first = Fr.exp( Fr.w[i], k*pointsInChunk);
const inc = Fr.w[i];
const o1 = j*nChunksPerGroup + k;
const o2 = j*nChunksPerGroup + k + nChunksPerGroup/2;
2020-07-11 11:31:52 +03:00
2020-09-07 13:43:50 +03:00
const task = [];
task.push({cmd: "ALLOCSET", var: 0, buff: chunks[o1]});
task.push({cmd: "ALLOCSET", var: 1, buff: chunks[o2]});
task.push({cmd: "ALLOCSET", var: 2, buff: first});
task.push({cmd: "ALLOCSET", var: 3, buff: inc});
task.push({cmd: "CALL", fnName: fnFFTJoin, params:[
{var: 0},
{var: 1},
{val: pointsInChunk},
{var: 2},
{var: 3}
]});
if (i==bits) {
if (fnFFTFinal) {
task.push({cmd: "ALLOCSET", var: 4, buff: inv});
task.push({cmd: "CALL", fnName: fnFFTFinal, params:[
{var: 0},
{val: pointsInChunk},
{var: 4},
]});
task.push({cmd: "CALL", fnName: fnFFTFinal, params:[
{var: 1},
{val: pointsInChunk},
{var: 4},
]});
2020-08-29 15:12:24 +03:00
}
2020-09-07 13:43:50 +03:00
if (fnMid2Out) {
task.push({cmd: "CALL", fnName:fnMid2Out, params: [{var:0}, {val: pointsInChunk}, {var: 0}]});
task.push({cmd: "CALL", fnName:fnMid2Out, params: [{var:1}, {val: pointsInChunk}, {var: 1}]});
}
task.push({cmd: "GET", out: 0, var: 0, len: pointsInChunk*sOut});
task.push({cmd: "GET", out: 1, var: 1, len: pointsInChunk*sOut});
} else {
task.push({cmd: "GET", out: 0, var: 0, len: pointsInChunk*sMid});
task.push({cmd: "GET", out: 1, var: 1, len: pointsInChunk*sMid});
2020-08-29 15:12:24 +03:00
}
2020-09-09 10:03:41 +03:00
opPromises.push(tm.queueAction(task).then( (r) => {
2020-09-10 16:02:19 +03:00
if (logger) logger.debug(`${loggerTxt}: fft ${bits} join ${i}/${bits} ${j+1}/${nGroups} ${k}/${nChunksPerGroup/2}`);
2020-09-09 10:03:41 +03:00
return r;
}));
2020-08-29 15:12:24 +03:00
}
2020-09-07 13:43:50 +03:00
}
2020-07-11 11:31:52 +03:00
2020-09-07 13:43:50 +03:00
const res = await Promise.all(opPromises);
for (let j=0; j<nGroups; j++) {
for (let k=0; k <nChunksPerGroup/2; k++) {
const o1 = j*nChunksPerGroup + k;
const o2 = j*nChunksPerGroup + k + nChunksPerGroup/2;
const resChunk = res.shift();
chunks[o1] = resChunk[0];
chunks[o2] = resChunk[1];
2020-08-29 15:12:24 +03:00
}
}
2020-09-07 13:43:50 +03:00
}
2020-08-12 02:33:08 +03:00
2020-09-07 13:43:50 +03:00
if (buff instanceof BigBuffer) {
buffOut = new BigBuffer(nPoints*sOut);
} else {
buffOut = new Uint8Array(nPoints*sOut);
}
if (inverse) {
buffOut.set(chunks[0].slice((pointsInChunk-1)*sOut));
let p= sOut;
for (let i=nChunks-1; i>0; i--) {
buffOut.set(chunks[i], p);
p += pointsInChunk*sOut;
delete chunks[i]; // Liberate mem
2020-08-29 15:12:24 +03:00
}
2020-09-07 13:43:50 +03:00
buffOut.set(chunks[0].slice(0, (pointsInChunk-1)*sOut), p);
delete chunks[0];
} else {
for (let i=0; i<nChunks; i++) {
buffOut.set(chunks[i], pointsInChunk*sOut*i);
delete chunks[i];
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
if (returnArray) {
return curve.buffer2array(buffOut, sOut);
2020-08-24 10:32:47 +03:00
} else {
2020-08-29 15:12:24 +03:00
return buffOut;
2020-08-12 02:33:08 +03:00
}
}
2020-07-11 11:31:52 +03:00
2020-09-07 13:43:50 +03:00
async function _fftExt(buff, inType, outType, logger, loggerTxt) {
let b1, b2;
b1 = buff.slice( 0 , buff.byteLength/2);
b2 = buff.slice( buff.byteLength/2, buff.byteLength);
const promises = [];
2020-09-07 19:24:09 +03:00
[b1, b2] = await _fftJoinExt(b1, b2, "fftJoinExt", Fr.one, Fr.shift, inType, "jacobian", logger, loggerTxt);
2020-09-07 13:43:50 +03:00
promises.push( _fft(b1, false, "jacobian", outType, logger, loggerTxt));
promises.push( _fft(b2, false, "jacobian", outType, logger, loggerTxt));
const res1 = await Promise.all(promises);
let buffOut;
if (res1[0].byteLength > (1<<28)) {
buffOut = new BigBuffer(res1[0].byteLength*2);
} else {
buffOut = new Uint8Array(res1[0].byteLength*2);
}
buffOut.set(res1[0]);
buffOut.set(res1[1], res1[0].byteLength);
return buffOut;
}
async function _fftExtInv(buff, inType, outType, logger, loggerTxt) {
let b1, b2;
b1 = buff.slice( 0 , buff.byteLength/2);
b2 = buff.slice( buff.byteLength/2, buff.byteLength);
const promises = [];
promises.push( _fft(b1, true, inType, "jacobian", logger, loggerTxt));
promises.push( _fft(b2, true, inType, "jacobian", logger, loggerTxt));
[b1, b2] = await Promise.all(promises);
2020-09-07 19:24:09 +03:00
const res1 = await _fftJoinExt(b1, b2, "fftJoinExtInv", Fr.one, Fr.shiftInv, "jacobian", outType, logger, loggerTxt);
2020-09-07 13:43:50 +03:00
let buffOut;
if (res1[0].byteLength > (1<<28)) {
buffOut = new BigBuffer(res1[0].byteLength*2);
} else {
buffOut = new Uint8Array(res1[0].byteLength*2);
}
buffOut.set(res1[0]);
buffOut.set(res1[1], res1[0].byteLength);
return buffOut;
}
2020-09-07 19:24:09 +03:00
async function _fftJoinExt(buff1, buff2, fn, first, inc, inType, outType, logger, loggerTxt) {
2020-09-07 13:43:50 +03:00
const MAX_CHUNK_SIZE = 1<<16;
2020-09-13 11:42:48 +03:00
const MIN_CHUNK_SIZE = 1<<4;
2020-09-07 13:43:50 +03:00
let fnName;
let fnIn2Mid, fnMid2Out;
2020-09-07 19:24:09 +03:00
let sOut, sIn, sMid;
2020-09-07 13:43:50 +03:00
if (groupName == "G1") {
if (inType == "affine") {
sIn = G.F.n8*2;
fnIn2Mid = "g1m_batchToJacobian";
} else {
sIn = G.F.n8*3;
}
2020-09-07 19:24:09 +03:00
sMid = G.F.n8*3;
2020-09-07 13:43:50 +03:00
fnName = "g1m_"+fn;
if (outType == "affine") {
fnMid2Out = "g1m_batchToAffine";
sOut = G.F.n8*2;
} else {
sOut = G.F.n8*3;
}
} else if (groupName == "G2") {
if (inType == "affine") {
sIn = G.F.n8*2;
fnIn2Mid = "g2m_batchToJacobian";
} else {
sIn = G.F.n8*3;
}
fnName = "g2m_"+fn;
2020-09-07 19:24:09 +03:00
sMid = G.F.n8*3;
2020-09-07 13:43:50 +03:00
if (outType == "affine") {
fnMid2Out = "g2m_batchToAffine";
sOut = G.F.n8*2;
} else {
sOut = G.F.n8*3;
}
} else if (groupName == "Fr") {
sIn = Fr.n8;
sOut = Fr.n8;
2020-09-07 19:24:09 +03:00
sMid = Fr.n8;
2020-09-07 13:43:50 +03:00
fnName = "frm_" + fn;
} else {
throw new Error("Invalid group");
}
if (buff1.byteLength != buff2.byteLength) {
throw new Error("Invalid buffer size");
}
const nPoints = Math.floor(buff1.byteLength / sIn);
if (nPoints != 1 << log2(nPoints)) {
throw new Error("Invalid number of points");
}
2020-09-13 11:42:48 +03:00
let chunkSize = Math.floor(nPoints /tm.concurrency);
if (chunkSize < MIN_CHUNK_SIZE) chunkSize = MIN_CHUNK_SIZE;
if (chunkSize > MAX_CHUNK_SIZE) chunkSize = MAX_CHUNK_SIZE;
2020-09-07 13:43:50 +03:00
const opPromises = [];
2020-09-13 11:42:48 +03:00
for (let i=0; i<nPoints; i += chunkSize) {
2020-09-09 10:03:41 +03:00
if (logger) logger.debug(`${loggerTxt}: fftJoinExt Start: ${i}/${nPoints}`);
2020-09-13 11:42:48 +03:00
const n= Math.min(nPoints - i, chunkSize);
2020-09-07 13:43:50 +03:00
2020-09-13 11:42:48 +03:00
const firstChunk = Fr.mul(first, Fr.exp( inc, i));
2020-09-07 13:43:50 +03:00
const task = [];
const b1 = buff1.slice(i*sIn, (i+n)*sIn);
const b2 = buff2.slice(i*sIn, (i+n)*sIn);
2020-09-07 19:24:09 +03:00
task.push({cmd: "ALLOC", var: 0, len: sMid*n});
task.push({cmd: "SET", var: 0, buff: b1});
task.push({cmd: "ALLOC", var: 1, len: sMid*n});
task.push({cmd: "SET", var: 1, buff: b2});
task.push({cmd: "ALLOCSET", var: 2, buff: firstChunk});
2020-09-07 13:43:50 +03:00
task.push({cmd: "ALLOCSET", var: 3, buff: inc});
if (fnIn2Mid) {
task.push({cmd: "CALL", fnName:fnIn2Mid, params: [{var:0}, {val: n}, {var: 0}]});
task.push({cmd: "CALL", fnName:fnIn2Mid, params: [{var:1}, {val: n}, {var: 1}]});
}
task.push({cmd: "CALL", fnName: fnName, params: [
{var: 0},
{var: 1},
{val: n},
{var: 2},
2020-09-13 11:42:48 +03:00
{var: 3},
{val: Fr.s},
2020-09-07 13:43:50 +03:00
]});
if (fnMid2Out) {
task.push({cmd: "CALL", fnName:fnMid2Out, params: [{var:0}, {val: n}, {var: 0}]});
task.push({cmd: "CALL", fnName:fnMid2Out, params: [{var:1}, {val: n}, {var: 1}]});
}
task.push({cmd: "GET", out: 0, var: 0, len: n*sOut});
task.push({cmd: "GET", out: 1, var: 1, len: n*sOut});
opPromises.push(
2020-09-09 10:03:41 +03:00
tm.queueAction(task).then( (r) => {
if (logger) logger.debug(`${loggerTxt}: fftJoinExt End: ${i}/${nPoints}`);
return r;
})
2020-09-07 13:43:50 +03:00
);
}
const result = await Promise.all(opPromises);
let fullBuffOut1;
let fullBuffOut2;
if (nPoints * sOut > 1<<28) {
fullBuffOut1 = new BigBuffer(nPoints*sOut);
fullBuffOut2 = new BigBuffer(nPoints*sOut);
} else {
fullBuffOut1 = new Uint8Array(nPoints*sOut);
fullBuffOut2 = new Uint8Array(nPoints*sOut);
}
let p =0;
for (let i=0; i<result.length; i++) {
fullBuffOut1.set(result[i][0], p);
fullBuffOut2.set(result[i][1], p);
p+=result[i][0].byteLength;
}
return [fullBuffOut1, fullBuffOut2];
}
G.fft = async function(buff, inType, outType, logger, loggerTxt) {
return await _fft(buff, false, inType, outType, logger, loggerTxt);
2020-08-29 15:12:24 +03:00
};
2020-07-11 11:31:52 +03:00
2020-09-07 13:43:50 +03:00
G.ifft = async function(buff, inType, outType, logger, loggerTxt) {
return await _fft(buff, true, inType, outType, logger, loggerTxt);
2020-08-29 15:12:24 +03:00
};
2020-07-11 11:31:52 +03:00
2020-09-07 19:24:09 +03:00
G.lagrangeEvaluations = async function (buff, inType, outType, logger, loggerTxt) {
inType = inType || "affine";
outType = outType || "affine";
let sIn;
if (groupName == "G1") {
if (inType == "affine") {
sIn = G.F.n8*2;
} else {
sIn = G.F.n8*3;
}
} else if (groupName == "G2") {
if (inType == "affine") {
sIn = G.F.n8*2;
} else {
sIn = G.F.n8*3;
}
} else if (groupName == "Fr") {
sIn = Fr.n8;
} else {
throw new Error("Invalid group");
}
const nPoints = buff.byteLength /sIn;
const bits = log2(nPoints);
if ((2 ** bits)*sIn != buff.byteLength) {
if (logger) logger.error("lagrangeEvaluations iinvalid input size");
throw new Error("lagrangeEvaluations invalid Input size");
}
if (bits <= Fr.s) {
return await G.ifft(buff, inType, outType, logger, loggerTxt);
}
if (bits > Fr.s+1) {
if (logger) logger.error("lagrangeEvaluations input too big");
throw new Error("lagrangeEvaluations input too big");
}
let t0 = buff.slice(0, buff.byteLength/2);
let t1 = buff.slice(buff.byteLength/2, buff.byteLength);
const shiftToSmallM = Fr.exp(Fr.shift, nPoints/2);
const sConst = Fr.inv( Fr.sub(Fr.one, shiftToSmallM));
[t0, t1] = await _fftJoinExt(t0, t1, "prepareLagrangeEvaluation", sConst, Fr.shiftInv, inType, "jacobian", logger, loggerTxt + " prep");
const promises = [];
promises.push( _fft(t0, true, "jacobian", outType, logger, loggerTxt + " t0"));
promises.push( _fft(t1, true, "jacobian", outType, logger, loggerTxt + " t1"));
[t0, t1] = await Promise.all(promises);
let buffOut;
if (t0.byteLength > (1<<28)) {
buffOut = new BigBuffer(t0.byteLength*2);
} else {
buffOut = new Uint8Array(t0.byteLength*2);
}
buffOut.set(t0);
buffOut.set(t1, t0.byteLength);
return buffOut;
};
2020-08-29 15:12:24 +03:00
G.fftMix = async function fftMix(buff) {
const sG = G.F.n8*3;
let fnName, fnFFTJoin;
if (groupName == "G1") {
fnName = "g1m_fftMix";
fnFFTJoin = "g1m_fftJoin";
} else if (groupName == "G2") {
fnName = "g2m_fftMix";
fnFFTJoin = "g2m_fftJoin";
} else if (groupName == "Fr") {
fnName = "frm_fftMix";
fnFFTJoin = "frm_fftJoin";
} else {
throw new Error("Invalid group");
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const nPoints = Math.floor(buff.byteLength / sG);
const power = log2(nPoints);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let nChunks = 1 << log2(tm.concurrency);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (nPoints <= nChunks*2) nChunks = 1;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const pointsPerChunk = nPoints / nChunks;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const powerChunk = log2(pointsPerChunk);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const opPromises = [];
for (let i=0; i<nChunks; i++) {
const task = [];
const b = buff.slice((i* pointsPerChunk)*sG, ((i+1)* pointsPerChunk)*sG);
task.push({cmd: "ALLOCSET", var: 0, buff: b});
for (let j=1; j<=powerChunk; j++) {
task.push({cmd: "CALL", fnName: fnName, params: [
{var: 0},
{val: pointsPerChunk},
{val: j}
]});
}
task.push({cmd: "GET", out: 0, var: 0, len: pointsPerChunk*sG});
opPromises.push(
tm.queueAction(task)
);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const result = await Promise.all(opPromises);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const chunks = [];
for (let i=0; i<result.length; i++) chunks[i] = result[i][0];
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
for (let i = powerChunk+1; i<=power; i++) {
const nGroups = 1 << (power - i);
const nChunksPerGroup = nChunks / nGroups;
const opPromises = [];
for (let j=0; j<nGroups; j++) {
for (let k=0; k <nChunksPerGroup/2; k++) {
const first = Fr.exp( Fr.w[i], k*pointsPerChunk);
const inc = Fr.w[i];
const o1 = j*nChunksPerGroup + k;
const o2 = j*nChunksPerGroup + k + nChunksPerGroup/2;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const task = [];
task.push({cmd: "ALLOCSET", var: 0, buff: chunks[o1]});
task.push({cmd: "ALLOCSET", var: 1, buff: chunks[o2]});
task.push({cmd: "ALLOCSET", var: 2, buff: first});
task.push({cmd: "ALLOCSET", var: 3, buff: inc});
task.push({cmd: "CALL", fnName: fnFFTJoin, params:[
{var: 0},
{var: 1},
{val: pointsPerChunk},
{var: 2},
{var: 3}
]});
task.push({cmd: "GET", out: 0, var: 0, len: pointsPerChunk*sG});
task.push({cmd: "GET", out: 1, var: 1, len: pointsPerChunk*sG});
opPromises.push(tm.queueAction(task));
}
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const res = await Promise.all(opPromises);
for (let j=0; j<nGroups; j++) {
for (let k=0; k <nChunksPerGroup/2; k++) {
const o1 = j*nChunksPerGroup + k;
const o2 = j*nChunksPerGroup + k + nChunksPerGroup/2;
const resChunk = res.shift();
chunks[o1] = resChunk[0];
chunks[o2] = resChunk[1];
}
}
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let fullBuffOut;
if (buff instanceof BigBuffer) {
fullBuffOut = new BigBuffer(nPoints*sG);
} else {
fullBuffOut = new Uint8Array(nPoints*sG);
}
let p =0;
for (let i=0; i<nChunks; i++) {
fullBuffOut.set(chunks[i], p);
p+=chunks[i].byteLength;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return fullBuffOut;
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
G.fftJoin = async function fftJoin(buff1, buff2, first, inc) {
const sG = G.F.n8*3;
let fnName;
if (groupName == "G1") {
fnName = "g1m_fftJoin";
} else if (groupName == "G2") {
fnName = "g2m_fftJoin";
2020-09-07 13:43:50 +03:00
} else if (groupName == "Fr") {
fnName = "frm_fftJoin";
2020-08-29 15:12:24 +03:00
} else {
throw new Error("Invalid group");
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (buff1.byteLength != buff2.byteLength) {
throw new Error("Invalid buffer size");
}
const nPoints = Math.floor(buff1.byteLength / sG);
if (nPoints != 1 << log2(nPoints)) {
throw new Error("Invalid number of points");
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let nChunks = 1 << log2(tm.concurrency);
if (nPoints <= nChunks*2) nChunks = 1;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const pointsPerChunk = nPoints / nChunks;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const opPromises = [];
for (let i=0; i<nChunks; i++) {
const task = [];
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
const firstChunk = Fr.mul(first, Fr.exp(inc, i*pointsPerChunk));
const b1 = buff1.slice((i* pointsPerChunk)*sG, ((i+1)* pointsPerChunk)*sG);
const b2 = buff2.slice((i* pointsPerChunk)*sG, ((i+1)* pointsPerChunk)*sG);
task.push({cmd: "ALLOCSET", var: 0, buff: b1});
task.push({cmd: "ALLOCSET", var: 1, buff: b2});
task.push({cmd: "ALLOCSET", var: 2, buff: firstChunk});
task.push({cmd: "ALLOCSET", var: 3, buff: inc});
task.push({cmd: "CALL", fnName: fnName, params: [
{var: 0},
{var: 1},
{val: pointsPerChunk},
{var: 2},
{var: 3}
]});
task.push({cmd: "GET", out: 0, var: 0, len: pointsPerChunk*sG});
task.push({cmd: "GET", out: 1, var: 1, len: pointsPerChunk*sG});
opPromises.push(
tm.queueAction(task)
);
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
}
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
const result = await Promise.all(opPromises);
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
let fullBuffOut1;
let fullBuffOut2;
if (buff1 instanceof BigBuffer) {
fullBuffOut1 = new BigBuffer(nPoints*sG);
fullBuffOut2 = new BigBuffer(nPoints*sG);
} else {
fullBuffOut1 = new Uint8Array(nPoints*sG);
fullBuffOut2 = new Uint8Array(nPoints*sG);
}
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
let p =0;
for (let i=0; i<result.length; i++) {
fullBuffOut1.set(result[i][0], p);
fullBuffOut2.set(result[i][1], p);
p+=result[i][0].byteLength;
}
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
return [fullBuffOut1, fullBuffOut2];
};
2020-08-19 21:12:03 +03:00
2020-09-07 13:43:50 +03:00
2020-08-29 15:12:24 +03:00
G.fftFinal = async function fftFinal(buff, factor) {
const sG = G.F.n8*3;
const sGout = G.F.n8*2;
let fnName, fnToAffine;
if (groupName == "G1") {
fnName = "g1m_fftFinal";
fnToAffine = "g1m_batchToAffine";
} else if (groupName == "G2") {
fnName = "g2m_fftFinal";
fnToAffine = "g2m_batchToAffine";
} else {
throw new Error("Invalid group");
2020-08-19 21:12:03 +03:00
}
2020-08-29 15:12:24 +03:00
const nPoints = Math.floor(buff.byteLength / sG);
if (nPoints != 1 << log2(nPoints)) {
throw new Error("Invalid number of points");
2020-08-19 21:12:03 +03:00
}
2020-08-29 15:12:24 +03:00
const pointsPerChunk = Math.floor(nPoints / tm.concurrency);
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
const opPromises = [];
for (let i=0; i<tm.concurrency; i++) {
let n;
if (i< tm.concurrency-1) {
n = pointsPerChunk;
} else {
n = nPoints - i*pointsPerChunk;
}
if (n==0) continue;
const task = [];
const b = buff.slice((i* pointsPerChunk)*sG, (i*pointsPerChunk+n)*sG);
task.push({cmd: "ALLOCSET", var: 0, buff: b});
task.push({cmd: "ALLOCSET", var: 1, buff: factor});
task.push({cmd: "CALL", fnName: fnName, params: [
{var: 0},
{val: n},
{var: 1},
]});
task.push({cmd: "CALL", fnName: fnToAffine, params: [
{var: 0},
{val: n},
{var: 0},
]});
task.push({cmd: "GET", out: 0, var: 0, len: n*sGout});
opPromises.push(
tm.queueAction(task)
);
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
}
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
const result = await Promise.all(opPromises);
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
let fullBuffOut;
if (buff instanceof BigBuffer) {
fullBuffOut = new BigBuffer(nPoints*sGout);
} else {
fullBuffOut = new Uint8Array(nPoints*sGout);
}
let p =0;
for (let i=result.length-1; i>=0; i--) {
fullBuffOut.set(result[i][0], p);
p+=result[i][0].byteLength;
}
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
return fullBuffOut;
};
2020-08-24 10:32:47 +03:00
}
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
async function buildEngine(params) {
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
const tm = await buildThreadManager(params.wasm, params.singleThread);
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
const curve = {};
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
curve.q = e$2(params.wasm.q);
curve.r = e$2(params.wasm.r);
curve.name = params.name;
curve.tm = tm;
curve.prePSize = params.wasm.prePSize;
curve.preQSize = params.wasm.preQSize;
curve.Fr = new WasmField1(tm, "frm", params.n8r, params.r);
curve.F1 = new WasmField1(tm, "f1m", params.n8q, params.q);
curve.F2 = new WasmField2(tm, "f2m", curve.F1);
curve.G1 = new WasmCurve(tm, "g1m", curve.F1, params.wasm.pG1gen, params.wasm.pG1b, params.cofactorG1);
curve.G2 = new WasmCurve(tm, "g2m", curve.F2, params.wasm.pG2gen, params.wasm.pG2b, params.cofactorG2);
curve.F6 = new WasmField3(tm, "f6m", curve.F2);
curve.F12 = new WasmField2(tm, "ftm", curve.F6);
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
curve.Gt = curve.F12;
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
buildBatchApplyKey(curve, "G1");
buildBatchApplyKey(curve, "G2");
buildBatchApplyKey(curve, "Fr");
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
buildMultiexp(curve, "G1");
buildMultiexp(curve, "G2");
2020-08-19 21:12:03 +03:00
2020-08-29 15:12:24 +03:00
buildFFT(curve, "G1");
buildFFT(curve, "G2");
buildFFT(curve, "Fr");
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
buildPairing(curve);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
curve.array2buffer = function(arr, sG) {
const buff = new Uint8Array(sG*arr.length);
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
for (let i=0; i<arr.length; i++) {
buff.set(arr[i], i*sG);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return buff;
};
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
curve.buffer2array = function(buff , sG) {
const n= buff.byteLength / sG;
const arr = new Array(n);
for (let i=0; i<n; i++) {
arr[i] = buff.slice(i*sG, i*sG+sG);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
return arr;
};
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
return curve;
}
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
global.curve_bn128 = null;
2020-07-11 11:31:52 +03:00
2020-10-08 12:43:05 +03:00
async function buildBn128(singleThread) {
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
if ((!singleThread)&&(global.curve_bn128)) return global.curve_bn128;
2020-08-29 15:12:24 +03:00
const params = {
name: "bn128",
wasm: wasmcurves.bn128_wasm,
q: e$2("21888242871839275222246405745257275088696311157297823662689037894645226208583"),
r: e$2("21888242871839275222246405745257275088548364400416034343698204186575808495617"),
n8q: 32,
n8r: 32,
cofactorG2: e$2("30644e72e131a029b85045b68181585e06ceecda572a2489345f2299c0f9fa8d", 16),
2020-10-08 12:43:05 +03:00
singleThread: singleThread ? true : false
2020-08-29 15:12:24 +03:00
};
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
const curve = await buildEngine(params);
2020-08-29 15:12:24 +03:00
curve.terminate = async function() {
2020-09-07 13:43:50 +03:00
if (!params.singleThread) {
2020-10-20 20:24:45 +03:00
global.curve_bn128 = null;
2020-09-07 13:43:50 +03:00
await this.tm.terminate();
}
2020-08-29 15:12:24 +03:00
};
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
if (!singleThread) {
global.curve_bn128 = curve;
}
2020-08-29 15:12:24 +03:00
return curve;
}
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
global.curve_bls12381 = null;
2020-07-11 11:31:52 +03:00
2020-10-08 12:43:05 +03:00
async function buildBls12381(singleThread) {
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
if ((!singleThread)&&(global.curve_bls12381)) return global.curve_bls12381;
2020-08-29 15:12:24 +03:00
const params = {
name: "bls12381",
wasm: wasmcurves.bls12381_wasm,
q: e$2("1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab", 16),
r: e$2("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001", 16),
n8q: 48,
n8r: 32,
cofactorG1: e$2("0x396c8c005555e1568c00aaab0000aaab", 16),
cofactorG2: e$2("0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5", 16),
2020-10-08 12:43:05 +03:00
singleThread: singleThread ? true : false
2020-08-29 15:12:24 +03:00
};
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
const curve = await buildEngine(params);
curve.terminate = async function() {
if (!params.singleThread) {
global.curve_bls12381 = null;
await this.tm.terminate();
}
2020-08-29 15:12:24 +03:00
};
2020-10-20 20:24:45 +03:00
return curve;
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
const bls12381r = e$2("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001", 16);
const bn128r = e$2("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const bls12381q = e$2("1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab", 16);
const bn128q = e$2("21888242871839275222246405745257275088696311157297823662689037894645226208583");
2021-01-21 21:26:22 +03:00
async function getCurveFromR(r, singleThread) {
let curve;
if (eq$2(r, bn128r)) {
curve = await buildBn128(singleThread);
} else if (eq$2(r, bls12381r)) {
curve = await buildBn128(singleThread);
} else {
throw new Error(`Curve not supported: ${toString(r)}`);
}
return curve;
}
2020-08-29 15:12:24 +03:00
const Scalar$1=_Scalar;
const utils$1 = _utils;
2020-07-11 11:31:52 +03:00
2021-01-17 11:56:08 +03:00
var fs = {};
2020-08-29 15:12:24 +03:00
async function open(fileName, openFlags, cacheSize, pageSize) {
cacheSize = cacheSize || 4096*64;
if (["w+", "wx+", "r", "ax+", "a+"].indexOf(openFlags) <0)
throw new Error("Invalid open option");
2021-01-17 11:56:08 +03:00
const fd =await fs.promises.open(fileName, openFlags);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const stats = await fd.stat();
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return new FastFile(fd, stats, cacheSize, pageSize, fileName);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
class FastFile {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
constructor(fd, stats, cacheSize, pageSize, fileName) {
this.fileName = fileName;
this.fd = fd;
this.pos = 0;
this.pageSize = pageSize || (1 << 8);
2020-09-25 08:38:22 +03:00
while (this.pageSize < stats.blksize) {
2020-08-29 15:12:24 +03:00
this.pageSize *= 2;
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
this.totalSize = stats.size;
this.totalPages = Math.floor((stats.size -1) / this.pageSize)+1;
this.maxPagesLoaded = Math.floor( cacheSize / this.pageSize)+1;
this.pages = {};
this.pendingLoads = [];
this.writing = false;
this.reading = false;
2020-09-25 08:38:22 +03:00
this.avBuffs = [];
this.history = {};
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
_loadPage(p) {
const self = this;
2020-09-25 08:38:22 +03:00
const P = new Promise((resolve, reject)=> {
2020-08-29 15:12:24 +03:00
self.pendingLoads.push({
page: p,
resolve: resolve,
reject: reject
});
});
2020-09-25 08:38:22 +03:00
self.__statusPage("After Load request: ", p);
return P;
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
__statusPage(s, p) {
const logEntry = [];
const self=this;
if (!self.logHistory) return;
logEntry.push("==" + s+ " " +p);
let S = "";
for (let i=0; i<self.pendingLoads.length; i++) {
if (self.pendingLoads[i].page == p) S = S + " " + i;
}
if (S) logEntry.push("Pending loads:"+S);
if (typeof self.pages[p] != "undefined") {
const page = self.pages[p];
logEntry.push("Loaded");
logEntry.push("pendingOps: "+page.pendingOps);
if (page.loading) logEntry.push("loading: "+page.loading);
if (page.writing) logEntry.push("writing");
if (page.dirty) logEntry.push("dirty");
}
logEntry.push("==");
if (!self.history[p]) self.history[p] = [];
self.history[p].push(logEntry);
}
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
__printHistory(p) {
2020-08-29 15:12:24 +03:00
const self = this;
2020-09-25 08:38:22 +03:00
if (!self.history[p]) console.log("Empty History ", p);
console.log("History "+p);
for (let i=0; i<self.history[p].length; i++) {
for (let j=0; j<self.history[p][i].length; j++) {
console.log("-> " + self.history[p][i][j]);
2020-08-29 15:12:24 +03:00
}
2020-08-24 10:32:47 +03:00
}
2020-09-25 08:38:22 +03:00
}
_triggerLoad() {
const self = this;
if (self.reading) return;
if (self.pendingLoads.length==0) return;
const pageIdxs = Object.keys(self.pages);
const deletablePages = [];
for (let i=0; i<pageIdxs.length; i++) {
const page = self.pages[parseInt(pageIdxs[i])];
if ((page.dirty == false)&&(page.pendingOps==0)&&(!page.writing)&&(!page.loading)) deletablePages.push(parseInt(pageIdxs[i]));
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
let freePages = self.maxPagesLoaded - pageIdxs.length;
const ops = [];
// while pending loads and
// the page is loaded or I can recover one.
while (
(self.pendingLoads.length>0) &&
( (typeof self.pages[self.pendingLoads[0].page] != "undefined" )
||( (freePages>0)
||(deletablePages.length>0)))) {
const load = self.pendingLoads.shift();
if (typeof self.pages[load.page] != "undefined") {
self.pages[load.page].pendingOps ++;
const idx = deletablePages.indexOf(load.page);
if (idx>=0) deletablePages.splice(idx, 1);
if (self.pages[load.page].loading) {
self.pages[load.page].loading.push(load);
} else {
load.resolve();
}
self.__statusPage("After Load (cached): ", load.page);
} else {
if (freePages) {
freePages--;
} else {
const fp = deletablePages.shift();
self.__statusPage("Before Unload: ", fp);
self.avBuffs.unshift(self.pages[fp]);
delete self.pages[fp];
self.__statusPage("After Unload: ", fp);
}
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
if (load.page>=self.totalPages) {
self.pages[load.page] = getNewPage();
2020-08-29 15:12:24 +03:00
load.resolve();
2020-09-25 08:38:22 +03:00
self.__statusPage("After Load (new): ", load.page);
2020-08-29 15:12:24 +03:00
} else {
2020-09-25 08:38:22 +03:00
self.reading = true;
self.pages[load.page] = getNewPage();
self.pages[load.page].loading = [load];
ops.push(self.fd.read(self.pages[load.page].buff, 0, self.pageSize, load.page*self.pageSize).then((res)=> {
self.pages[load.page].size = res.bytesRead;
const loading = self.pages[load.page].loading;
delete self.pages[load.page].loading;
for (let i=0; i<loading.length; i++) {
loading[i].resolve();
}
self.__statusPage("After Load (loaded): ", load.page);
return res;
}, (err) => {
load.reject(err);
}));
self.__statusPage("After Load (loading): ", load.page);
2020-08-29 15:12:24 +03:00
}
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
}
2020-09-25 08:38:22 +03:00
// if (ops.length>1) console.log(ops.length);
Promise.all(ops).then( () => {
self.reading = false;
if (self.pendingLoads.length>0) setImmediate(self._triggerLoad.bind(self));
2021-01-21 21:26:22 +03:00
self._tryClose();
2020-09-25 08:38:22 +03:00
});
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
function getNewPage() {
if (self.avBuffs.length>0) {
const p = self.avBuffs.shift();
p.dirty = false;
p.pendingOps = 1;
p.size =0;
return p;
} else {
return {
dirty: false,
buff: new Uint8Array(self.pageSize),
pendingOps: 1,
size: 0
};
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
}
2020-09-25 08:38:22 +03:00
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
2020-08-29 15:12:24 +03:00
_triggerWrite() {
const self = this;
if (self.writing) return;
2020-09-25 08:38:22 +03:00
const pageIdxs = Object.keys(self.pages);
const ops = [];
for (let i=0; i<pageIdxs.length; i++) {
const page = self.pages[parseInt(pageIdxs[i])];
if (page.dirty) {
page.dirty = false;
page.writing = true;
self.writing = true;
ops.push( self.fd.write(page.buff, 0, page.size, parseInt(pageIdxs[i])*self.pageSize).then(() => {
page.writing = false;
return;
}, (err) => {
console.log("ERROR Writing: "+err);
self.error = err;
self._tryClose();
}));
}
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
if (self.writing) {
Promise.all(ops).then( () => {
self.writing = false;
setImmediate(self._triggerWrite.bind(self));
self._tryClose();
if (self.pendingLoads.length>0) setImmediate(self._triggerLoad.bind(self));
});
}
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
_getDirtyPage() {
for (let p in this.pages) {
if (this.pages[p].dirty) return p;
}
return -1;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async write(buff, pos) {
if (buff.byteLength == 0) return;
const self = this;
/*
if (buff.byteLength > self.pageSize*self.maxPagesLoaded*0.8) {
const cacheSize = Math.floor(buff.byteLength * 1.1);
this.maxPagesLoaded = Math.floor( cacheSize / self.pageSize)+1;
}
*/
if (typeof pos == "undefined") pos = self.pos;
self.pos = pos+buff.byteLength;
if (self.totalSize < pos + buff.byteLength) self.totalSize = pos + buff.byteLength;
if (self.pendingClose)
throw new Error("Writing a closing file");
const firstPage = Math.floor(pos / self.pageSize);
2020-09-25 08:38:22 +03:00
const lastPage = Math.floor((pos + buff.byteLength -1) / self.pageSize);
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
const pagePromises = [];
for (let i=firstPage; i<=lastPage; i++) pagePromises.push(self._loadPage(i));
self._triggerLoad();
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let p = firstPage;
let o = pos % self.pageSize;
let r = buff.byteLength;
while (r>0) {
2020-09-25 08:38:22 +03:00
await pagePromises[p-firstPage];
2020-08-29 15:12:24 +03:00
const l = (o+r > self.pageSize) ? (self.pageSize -o) : r;
2020-09-07 13:43:50 +03:00
const srcView = buff.slice( buff.byteLength - r, buff.byteLength - r + l);
2020-08-29 15:12:24 +03:00
const dstView = new Uint8Array(self.pages[p].buff.buffer, o, l);
dstView.set(srcView);
self.pages[p].dirty = true;
self.pages[p].pendingOps --;
self.pages[p].size = Math.max(o+l, self.pages[p].size);
if (p>=self.totalPages) {
self.totalPages = p+1;
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
r = r-l;
p ++;
o = 0;
2020-09-25 08:38:22 +03:00
if (!self.writing) setImmediate(self._triggerWrite.bind(self));
2020-08-24 10:32:47 +03:00
}
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async read(len, pos) {
const self = this;
let buff = new Uint8Array(len);
await self.readToBuffer(buff, 0, len, pos);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return buff;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async readToBuffer(buffDst, offset, len, pos) {
if (len == 0) {
return;
}
const self = this;
if (len > self.pageSize*self.maxPagesLoaded*0.8) {
const cacheSize = Math.floor(len * 1.1);
this.maxPagesLoaded = Math.floor( cacheSize / self.pageSize)+1;
}
if (typeof pos == "undefined") pos = self.pos;
self.pos = pos+len;
if (self.pendingClose)
throw new Error("Reading a closing file");
const firstPage = Math.floor(pos / self.pageSize);
2020-09-25 08:38:22 +03:00
const lastPage = Math.floor((pos + len -1) / self.pageSize);
const pagePromises = [];
for (let i=firstPage; i<=lastPage; i++) pagePromises.push(self._loadPage(i));
self._triggerLoad();
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let p = firstPage;
let o = pos % self.pageSize;
// Remaining bytes to read
let r = pos + len > self.totalSize ? len - (pos + len - self.totalSize): len;
while (r>0) {
2020-09-25 08:38:22 +03:00
await pagePromises[p - firstPage];
self.__statusPage("After Await (read): ", p);
2020-08-29 15:12:24 +03:00
// bytes to copy from this page
const l = (o+r > self.pageSize) ? (self.pageSize -o) : r;
2020-09-25 08:38:22 +03:00
const srcView = new Uint8Array(self.pages[p].buff.buffer, self.pages[p].buff.byteOffset + o, l);
2020-08-29 15:12:24 +03:00
buffDst.set(srcView, offset+len-r);
self.pages[p].pendingOps --;
2020-09-25 08:38:22 +03:00
self.__statusPage("After Op done: ", p);
2020-08-29 15:12:24 +03:00
r = r-l;
p ++;
o = 0;
2020-09-25 08:38:22 +03:00
if (self.pendingLoads.length>0) setImmediate(self._triggerLoad.bind(self));
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.pos = pos + len;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
_tryClose() {
const self = this;
if (!self.pendingClose) return;
if (self.error) {
self.pendingCloseReject(self.error);
}
const p = self._getDirtyPage();
if ((p>=0) || (self.writing) || (self.reading) || (self.pendingLoads.length>0)) return;
self.pendingClose();
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
close() {
const self = this;
if (self.pendingClose)
throw new Error("Closing the file twice");
return new Promise((resolve, reject) => {
self.pendingClose = resolve;
self.pendingCloseReject = reject;
self._tryClose();
}).then(()=> {
self.fd.close();
}, (err) => {
self.fd.close();
throw (err);
2020-08-24 10:32:47 +03:00
});
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async discard() {
const self = this;
await self.close();
2021-01-17 11:56:08 +03:00
await fs.promises.unlink(this.fileName);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async writeULE32(v, pos) {
const self = this;
2020-09-25 08:38:22 +03:00
const tmpBuff32 = new Uint8Array(4);
const tmpBuff32v = new DataView(tmpBuff32.buffer);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
tmpBuff32v.setUint32(0, v, true);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
await self.write(tmpBuff32, pos);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async writeUBE32(v, pos) {
const self = this;
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
const tmpBuff32 = new Uint8Array(4);
const tmpBuff32v = new DataView(tmpBuff32.buffer);
2020-08-29 15:12:24 +03:00
tmpBuff32v.setUint32(0, v, false);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
await self.write(tmpBuff32, pos);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async writeULE64(v, pos) {
const self = this;
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
const tmpBuff64 = new Uint8Array(8);
const tmpBuff64v = new DataView(tmpBuff64.buffer);
2020-08-29 15:12:24 +03:00
tmpBuff64v.setUint32(0, v & 0xFFFFFFFF, true);
tmpBuff64v.setUint32(4, Math.floor(v / 0x100000000) , true);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
await self.write(tmpBuff64, pos);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async readULE32(pos) {
const self = this;
const b = await self.read(4, pos);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const view = new Uint32Array(b.buffer);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return view[0];
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async readUBE32(pos) {
const self = this;
const b = await self.read(4, pos);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const view = new DataView(b.buffer);
2020-07-26 15:05:23 +03:00
2020-08-29 15:12:24 +03:00
return view.getUint32(0, false);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async readULE64(pos) {
const self = this;
const b = await self.read(8, pos);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const view = new Uint32Array(b.buffer);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return view[1] * 0x100000000 + view[0];
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function createNew(o) {
const initialSize = o.initialSize || 1<<20;
const fd = new MemFile();
fd.o = o;
fd.o.data = new Uint8Array(initialSize);
fd.allocSize = initialSize;
fd.totalSize = 0;
fd.readOnly = false;
fd.pos = 0;
return fd;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function readExisting(o) {
const fd = new MemFile();
fd.o = o;
fd.allocSize = o.data.byteLength;
fd.totalSize = o.data.byteLength;
fd.readOnly = true;
fd.pos = 0;
return fd;
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
const tmpBuff32 = new Uint8Array(4);
const tmpBuff32v = new DataView(tmpBuff32.buffer);
const tmpBuff64 = new Uint8Array(8);
const tmpBuff64v = new DataView(tmpBuff64.buffer);
2020-08-29 15:12:24 +03:00
class MemFile {
2020-08-24 10:32:47 +03:00
constructor() {
2020-08-29 15:12:24 +03:00
this.pageSize = 1 << 14; // for compatibility
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
_resizeIfNeeded(newLen) {
if (newLen > this.allocSize) {
const newAllocSize = Math.max(
this.allocSize + (1 << 20),
Math.floor(this.allocSize * 1.1),
newLen
);
const newData = new Uint8Array(newAllocSize);
newData.set(this.o.data);
this.o.data = newData;
this.allocSize = newAllocSize;
}
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async write(buff, pos) {
const self =this;
if (typeof pos == "undefined") pos = self.pos;
if (this.readOnly) throw new Error("Writing a read only file");
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this._resizeIfNeeded(pos + buff.byteLength);
2020-08-12 02:33:08 +03:00
2020-09-07 13:43:50 +03:00
this.o.data.set(buff.slice(), pos);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
if (pos + buff.byteLength > this.totalSize) this.totalSize = pos + buff.byteLength;
this.pos = pos + buff.byteLength;
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
async readToBuffer(buffDest, offset, len, pos) {
const self = this;
if (typeof pos == "undefined") pos = self.pos;
if (this.readOnly) {
if (pos + len > this.totalSize) throw new Error("Reading out of bounds");
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
this._resizeIfNeeded(pos + len);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const buffSrc = new Uint8Array(this.o.data.buffer, this.o.data.byteOffset + pos, len);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
buffDest.set(buffSrc, offset);
this.pos = pos + len;
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
async read(len, pos) {
const self = this;
const buff = new Uint8Array(len);
await self.readToBuffer(buff, 0, len, pos);
return buff;
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
close() {
if (this.o.data.byteLength != this.totalSize) {
this.o.data = this.o.data.slice(0, this.totalSize);
}
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
async discard() {
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
async writeULE32(v, pos) {
const self = this;
2020-09-25 08:38:22 +03:00
tmpBuff32v.setUint32(0, v, true);
2020-08-29 15:12:24 +03:00
2020-09-25 08:38:22 +03:00
await self.write(tmpBuff32, pos);
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async writeUBE32(v, pos) {
const self = this;
2020-09-25 08:38:22 +03:00
tmpBuff32v.setUint32(0, v, false);
2020-08-29 15:12:24 +03:00
2020-09-25 08:38:22 +03:00
await self.write(tmpBuff32, pos);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
async writeULE64(v, pos) {
const self = this;
2020-09-25 08:38:22 +03:00
tmpBuff64v.setUint32(0, v & 0xFFFFFFFF, true);
tmpBuff64v.setUint32(4, Math.floor(v / 0x100000000) , true);
2020-08-29 15:12:24 +03:00
2020-09-25 08:38:22 +03:00
await self.write(tmpBuff64, pos);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async readULE32(pos) {
const self = this;
const b = await self.read(4, pos);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const view = new Uint32Array(b.buffer);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return view[0];
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async readUBE32(pos) {
const self = this;
const b = await self.read(4, pos);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const view = new DataView(b.buffer);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return view.getUint32(0, false);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async readULE64(pos) {
const self = this;
const b = await self.read(8, pos);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const view = new Uint32Array(b.buffer);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return view[1] * 0x100000000 + view[0];
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const PAGE_SIZE$1 = 1<<22;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function createNew$1(o) {
const initialSize = o.initialSize || 0;
const fd = new BigMemFile();
fd.o = o;
const nPages = initialSize ? Math.floor((initialSize - 1) / PAGE_SIZE$1)+1 : 0;
fd.o.data = [];
for (let i=0; i<nPages-1; i++) {
fd.o.data.push( new Uint8Array(PAGE_SIZE$1));
}
if (nPages) fd.o.data.push( new Uint8Array(initialSize - PAGE_SIZE$1*(nPages-1)));
fd.totalSize = 0;
fd.readOnly = false;
fd.pos = 0;
return fd;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function readExisting$1(o) {
const fd = new BigMemFile();
fd.o = o;
fd.totalSize = (o.data.length-1)* PAGE_SIZE$1 + o.data[o.data.length-1].byteLength;
fd.readOnly = true;
fd.pos = 0;
return fd;
}
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
const tmpBuff32$1 = new Uint8Array(4);
const tmpBuff32v$1 = new DataView(tmpBuff32$1.buffer);
const tmpBuff64$1 = new Uint8Array(8);
const tmpBuff64v$1 = new DataView(tmpBuff64$1.buffer);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
class BigMemFile {
2020-07-26 20:50:50 +03:00
2020-08-29 15:12:24 +03:00
constructor() {
this.pageSize = 1 << 14; // for compatibility
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
_resizeIfNeeded(newLen) {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (newLen <= this.totalSize) return;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (this.readOnly) throw new Error("Reading out of file bounds");
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const nPages = Math.floor((newLen - 1) / PAGE_SIZE$1)+1;
for (let i= Math.max(this.o.data.length-1, 0); i<nPages; i++) {
const newSize = i<nPages-1 ? PAGE_SIZE$1 : newLen - (nPages-1)*PAGE_SIZE$1;
const p = new Uint8Array(newSize);
if (i == this.o.data.length-1) p.set(this.o.data[i]);
this.o.data[i] = p;
}
this.totalSize = newLen;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async write(buff, pos) {
const self =this;
if (typeof pos == "undefined") pos = self.pos;
if (this.readOnly) throw new Error("Writing a read only file");
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this._resizeIfNeeded(pos + buff.byteLength);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const firstPage = Math.floor(pos / PAGE_SIZE$1);
let p = firstPage;
let o = pos % PAGE_SIZE$1;
let r = buff.byteLength;
while (r>0) {
const l = (o+r > PAGE_SIZE$1) ? (PAGE_SIZE$1 -o) : r;
2020-09-07 13:43:50 +03:00
const srcView = buff.slice(buff.byteLength - r, buff.byteLength - r + l);
2020-08-29 15:12:24 +03:00
const dstView = new Uint8Array(self.o.data[p].buffer, o, l);
dstView.set(srcView);
r = r-l;
p ++;
o = 0;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.pos = pos + buff.byteLength;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async readToBuffer(buffDst, offset, len, pos) {
const self = this;
if (typeof pos == "undefined") pos = self.pos;
if (this.readOnly) {
if (pos + len > this.totalSize) throw new Error("Reading out of bounds");
}
this._resizeIfNeeded(pos + len);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const firstPage = Math.floor(pos / PAGE_SIZE$1);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let p = firstPage;
let o = pos % PAGE_SIZE$1;
// Remaining bytes to read
let r = len;
while (r>0) {
// bytes to copy from this page
const l = (o+r > PAGE_SIZE$1) ? (PAGE_SIZE$1 -o) : r;
const srcView = new Uint8Array(self.o.data[p].buffer, o, l);
buffDst.set(srcView, offset+len-r);
r = r-l;
p ++;
o = 0;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.pos = pos + len;
}
2020-07-26 20:50:50 +03:00
2020-08-29 15:12:24 +03:00
async read(len, pos) {
const self = this;
const buff = new Uint8Array(len);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
await self.readToBuffer(buff, 0, len, pos);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return buff;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
close() {
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async discard() {
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async writeULE32(v, pos) {
const self = this;
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
tmpBuff32v$1.setUint32(0, v, true);
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
await self.write(tmpBuff32$1, pos);
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async writeUBE32(v, pos) {
const self = this;
2020-07-26 20:50:50 +03:00
2020-09-25 08:38:22 +03:00
tmpBuff32v$1.setUint32(0, v, false);
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
await self.write(tmpBuff32$1, pos);
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async writeULE64(v, pos) {
const self = this;
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
tmpBuff64v$1.setUint32(0, v & 0xFFFFFFFF, true);
tmpBuff64v$1.setUint32(4, Math.floor(v / 0x100000000) , true);
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
await self.write(tmpBuff64$1, pos);
2020-07-11 11:31:52 +03:00
}
2020-08-29 15:12:24 +03:00
async readULE32(pos) {
const self = this;
const b = await self.read(4, pos);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const view = new Uint32Array(b.buffer);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return view[0];
}
2020-07-13 08:21:03 +03:00
2020-08-29 15:12:24 +03:00
async readUBE32(pos) {
const self = this;
const b = await self.read(4, pos);
2020-07-13 08:21:03 +03:00
2020-08-29 15:12:24 +03:00
const view = new DataView(b.buffer);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
return view.getUint32(0, false);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async readULE64(pos) {
const self = this;
const b = await self.read(8, pos);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const view = new Uint32Array(b.buffer);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
return view[1] * 0x100000000 + view[0];
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
/* global fetch */
2020-08-12 02:33:08 +03:00
2020-09-25 08:38:22 +03:00
const DEFAULT_CACHE_SIZE = (1 << 16);
const DEFAULT_PAGE_SIZE = (1 << 13);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async function createOverride(o, b, c) {
if (typeof o === "string") {
o = {
type: "file",
fileName: o,
2020-09-25 08:38:22 +03:00
cacheSize: b || DEFAULT_CACHE_SIZE,
pageSize: c || DEFAULT_PAGE_SIZE
2020-08-29 15:12:24 +03:00
};
}
if (o.type == "file") {
return await open(o.fileName, "w+", o.cacheSize, o.pageSize);
} else if (o.type == "mem") {
return createNew(o);
} else if (o.type == "bigMem") {
return createNew$1(o);
} else {
throw new Error("Invalid FastFile type: "+o.type);
}
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async function readExisting$2(o, b, c) {
if (o instanceof Uint8Array) {
o = {
type: "mem",
data: o
};
}
{
if (typeof o === "string") {
const buff = await fetch(o).then( function(res) {
return res.arrayBuffer();
}).then(function (ab) {
return new Uint8Array(ab);
});
o = {
type: "mem",
data: buff
};
}
}
if (o.type == "file") {
return await open(o.fileName, "r", o.cacheSize, o.pageSize);
} else if (o.type == "mem") {
return await readExisting(o);
} else if (o.type == "bigMem") {
return await readExisting$1(o);
} else {
throw new Error("Invalid FastFile type: "+o.type);
}
}
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
async function readBinFile(fileName, type, maxVersion, cacheSize, pageSize) {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const fd = await readExisting$2(fileName);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const b = await fd.read(4);
let readedType = "";
for (let i=0; i<4; i++) readedType += String.fromCharCode(b[i]);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (readedType != type) throw new Error(fileName + ": Invalid File format");
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let v = await fd.readULE32();
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
if (v>maxVersion) throw new Error("Version not supported");
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const nSections = await fd.readULE32();
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
// Scan sections
let sections = [];
for (let i=0; i<nSections; i++) {
let ht = await fd.readULE32();
let hl = await fd.readULE64();
if (typeof sections[ht] == "undefined") sections[ht] = [];
sections[ht].push({
p: fd.pos,
size: hl
});
fd.pos += hl;
2020-08-12 02:33:08 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return {fd, sections};
}
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
async function createBinFile(fileName, type, version, nSections, cacheSize, pageSize) {
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
const fd = await createOverride(fileName, cacheSize, pageSize);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const buff = new Uint8Array(4);
for (let i=0; i<4; i++) buff[i] = type.charCodeAt(i);
await fd.write(buff, 0); // Magic "r1cs"
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
await fd.writeULE32(version); // Version
await fd.writeULE32(nSections); // Number of Sections
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return fd;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async function startWriteSection(fd, idSection) {
if (typeof fd.writingSection !== "undefined") throw new Error("Already writing a section");
await fd.writeULE32(idSection); // Header type
fd.writingSection = {
pSectionSize: fd.pos
};
await fd.writeULE64(0); // Temporally set to 0 length
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async function endWriteSection(fd) {
if (typeof fd.writingSection === "undefined") throw new Error("Not writing a section");
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const sectionSize = fd.pos - fd.writingSection.pSectionSize - 8;
const oldPos = fd.pos;
fd.pos = fd.writingSection.pSectionSize;
await fd.writeULE64(sectionSize);
fd.pos = oldPos;
delete fd.writingSection;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async function startReadUniqueSection(fd, sections, idSection) {
if (typeof fd.readingSection !== "undefined") throw new Error("Already reading a section");
if (!sections[idSection]) throw new Error(fd.fileName + ": Missing section "+ idSection );
if (sections[idSection].length>1) throw new Error(fd.fileName +": Section Duplicated " +idSection);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
fd.pos = sections[idSection][0].p;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
fd.readingSection = sections[idSection][0];
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
async function endReadSection(fd, noCheck) {
if (typeof fd.readingSection === "undefined") throw new Error("Not reading a section");
if (!noCheck) {
if (fd.pos-fd.readingSection.p != fd.readingSection.size) throw new Error("Invalid section size reading");
}
delete fd.readingSection;
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
async function writeBigInt(fd, n, n8, pos) {
const buff = new Uint8Array(n8);
Scalar$1.toRprLE(buff, 0, n, n8);
await fd.write(buff, pos);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async function readBigInt(fd, n8, pos) {
const buff = await fd.read(n8, pos);
return Scalar$1.fromRprLE(buff, 0, n8);
}
2020-07-11 11:31:52 +03:00
2020-09-02 13:06:20 +03:00
async function copySection(fdFrom, sections, fdTo, sectionId, size) {
if (typeof size === "undefined") {
size = sections[sectionId][0].size;
}
2020-08-29 15:12:24 +03:00
const chunkSize = fdFrom.pageSize;
await startReadUniqueSection(fdFrom, sections, sectionId);
await startWriteSection(fdTo, sectionId);
2020-09-02 13:06:20 +03:00
for (let p=0; p<size; p+=chunkSize) {
const l = Math.min(size -p, chunkSize);
2020-08-29 15:12:24 +03:00
const buff = await fdFrom.read(l);
await fdTo.write(buff);
}
await endWriteSection(fdTo);
2020-09-02 13:06:20 +03:00
await endReadSection(fdFrom, size != sections[sectionId][0].size);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-10-11 11:32:31 +03:00
async function readSection(fd, sections, idSection, offset, length) {
offset = (typeof offset === "undefined") ? 0 : offset;
length = (typeof length === "undefined") ? sections[idSection][0].size - offset : length;
if (offset + length > sections[idSection][0].size) {
throw new Error("Reading out of the range of the section");
}
let buff;
if (length < (1 << 30) ) {
buff = new Uint8Array(length);
} else {
buff = new BigBuffer(length);
}
await fd.readToBuffer(buff, 0, length, sections[idSection][0].p + offset);
return buff;
}
2020-08-29 15:12:24 +03:00
async function sectionIsEqual(fd1, sections1, fd2, sections2, idSection) {
const MAX_BUFF_SIZE = fd1.pageSize * 16;
await startReadUniqueSection(fd1, sections1, idSection);
await startReadUniqueSection(fd2, sections2, idSection);
if (sections1[idSection][0].size != sections2[idSection][0].size) return false;
const totalBytes=sections1[idSection][0].size;
for (let i=0; i<totalBytes; i+= MAX_BUFF_SIZE) {
const n = Math.min(totalBytes-i, MAX_BUFF_SIZE);
const buff1 = await fd1.read(n);
const buff2 = await fd2.read(n);
for (let j=0; j<n; j++) if (buff1[j] != buff2[j]) return false;
}
await endReadSection(fd1);
await endReadSection(fd2);
return true;
}
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
const bls12381r$1 = Scalar$1.e("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001", 16);
const bn128r$1 = Scalar$1.e("21888242871839275222246405745257275088548364400416034343698204186575808495617");
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
const bls12381q$1 = Scalar$1.e("1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab", 16);
const bn128q$1 = Scalar$1.e("21888242871839275222246405745257275088696311157297823662689037894645226208583");
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async function getCurveFromQ(q) {
let curve;
2020-09-25 08:38:22 +03:00
if (Scalar$1.eq(q, bn128q$1)) {
2020-08-29 15:12:24 +03:00
curve = await buildBn128();
2020-09-25 08:38:22 +03:00
} else if (Scalar$1.eq(q, bls12381q$1)) {
2020-08-29 15:12:24 +03:00
curve = await buildBls12381();
} else {
throw new Error(`Curve not supported: ${Scalar$1.toString(q)}`);
}
return curve;
}
2020-09-07 13:43:50 +03:00
2020-08-29 15:12:24 +03:00
async function getCurveFromName(name) {
let curve;
const normName = normalizeName(name);
if (["BN128", "BN254", "ALTBN128"].indexOf(normName) >= 0) {
curve = await buildBn128();
} else if (["BLS12381"].indexOf(normName) >= 0) {
curve = await buildBls12381();
} else {
throw new Error(`Curve not supported: ${name}`);
}
return curve;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function normalizeName(n) {
return n.toUpperCase().match(/[A-Za-z0-9]+/g).join("");
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
assert.notEqual = notEqual;
assert.notOk = notOk;
assert.equal = equal;
assert.ok = assert;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
var nanoassert = assert;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function equal (a, b, m) {
assert(a == b, m); // eslint-disable-line eqeqeq
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function notEqual (a, b, m) {
assert(a != b, m); // eslint-disable-line eqeqeq
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function notOk (t, m) {
assert(!t, m);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function assert (t, m) {
if (!t) throw new Error(m || 'AssertionError')
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
var blake2b = loadWebAssembly;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
loadWebAssembly.supported = typeof WebAssembly !== 'undefined';
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function loadWebAssembly (opts) {
if (!loadWebAssembly.supported) return null
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
var imp = opts && opts.imports;
var wasm = toUint8Array('AGFzbQEAAAABEANgAn9/AGADf39/AGABfwADBQQAAQICBQUBAQroBwdNBQZtZW1vcnkCAAxibGFrZTJiX2luaXQAAA5ibGFrZTJiX3VwZGF0ZQABDWJsYWtlMmJfZmluYWwAAhBibGFrZTJiX2NvbXByZXNzAAMKvz8EwAIAIABCADcDACAAQgA3AwggAEIANwMQIABCADcDGCAAQgA3AyAgAEIANwMoIABCADcDMCAAQgA3AzggAEIANwNAIABCADcDSCAAQgA3A1AgAEIANwNYIABCADcDYCAAQgA3A2ggAEIANwNwIABCADcDeCAAQoiS853/zPmE6gBBACkDAIU3A4ABIABCu86qptjQ67O7f0EIKQMAhTcDiAEgAEKr8NP0r+68tzxBECkDAIU3A5ABIABC8e30+KWn/aelf0EYKQMAhTcDmAEgAELRhZrv+s+Uh9EAQSApAwCFNwOgASAAQp/Y+dnCkdqCm39BKCkDAIU3A6gBIABC6/qG2r+19sEfQTApAwCFNwOwASAAQvnC+JuRo7Pw2wBBOCkDAIU3A7gBIABCADcDwAEgAEIANwPIASAAQgA3A9ABC20BA38gAEHAAWohAyAAQcgBaiEEIAQpAwCnIQUCQANAIAEgAkYNASAFQYABRgRAIAMgAykDACAFrXw3AwBBACEFIAAQAwsgACAFaiABLQAAOgAAIAVBAWohBSABQQFqIQEMAAsLIAQgBa03AwALYQEDfyAAQcABaiEBIABByAFqIQIgASABKQMAIAIpAwB8NwMAIABCfzcD0AEgAikDAKchAwJAA0AgA0GAAUYNASAAIANqQQA6AAAgA0EBaiEDDAALCyACIAOtNwMAIAAQAwuqOwIgfgl/IABBgAFqISEgAEGIAWohIiAAQZABaiEjIABBmAFqISQgAEGgAWohJSAAQagBaiEmIABBsAFqIScgAEG4AWohKCAhKQMAIQEgIikDACECICMpAwAhAyAkKQMAIQQgJSkDACEFICYpAwAhBiAnKQMAIQcgKCkDACEIQoiS853/zPmE6gAhCUK7zqqm2NDrs7t/IQpCq/DT9K/uvLc8IQtC8e30+KWn/aelfyEMQtGFmu/6z5SH0QAhDUKf2PnZwpHagpt/IQ5C6/qG2r+19sEfIQ9C+cL4m5Gjs/DbACEQIAApAwAhESAAKQMIIRIgACkDECETIAApAxghFCAAKQMgIRUgACkDKCEWIAApAzAhFyAAKQM4IRggACkDQCEZIAApA0ghGiAAKQNQIRsgACkDWCEcIAApA2AhHSAAKQNoIR4gACkDcCEfIAApA3ghICANIAApA8ABhSENIA8gACkD0AGFIQ8gASAFIBF8fCEBIA0gAYVCIIohDSAJIA18IQkgBSAJhUIYiiEFIAEgBSASfHwhASANIAGFQhCKIQ0gCSANfCEJIAUgCYVCP4ohBSACIAYgE3x8IQIgDiAChUIgiiEOIAogDnwhCiAGIAqFQhiKIQYgAiAGIBR8fCECIA4gAoVCEIohDiAKIA58IQogBiAKhUI/iiEGIAMgByAVfHwhAyAPIAOFQiCKIQ8gCyAPfCELIAcgC4VCGIohByADIAcgFnx8IQMgDyADhUIQiiEPIAsgD3whCyAHIAuFQj+KIQcgBCAIIBd8fCEEIBAgBIVCIIohECAMIBB8IQwgCCAMhUIYiiEIIAQgCCAYfHwhBCAQIASFQhCKIRAgDCAQfCEMIAggDIVCP4ohCCABIAYgGXx8IQEgECABhUIgiiEQIAsgEHwhCyAGIAuFQhiKIQYgASAGIBp8fCEBIBAgAYVCEIohECALIBB8IQsgBiALhUI/iiEGIAIgByAbfHwhAiANIAKFQiCKIQ0gDCANfCEMIAcgDIVCGIohByACIAcgHHx8IQIgDSAChUIQiiENIAwgDXwhDCAHIAyFQj+KIQcgAyAIIB18fCEDIA4gA4VCIIohDiAJIA58IQkgCCAJhUIYiiEIIAMgCCAefHwhAyAOIAOFQhCKIQ4gCSAOfCEJIAggCYVCP4ohCCAEIAUgH3x8IQQgDyAEhUIgiiEPIAogD3whCiAFIAqFQhiKIQUgBCAFICB8fCEEIA8gBIVCEIohDyAKIA98IQogBSAKhUI/iiEFIAEgBSAffHwhASANIAGFQiCKIQ0gCSANfCEJIAUgCYVCGIohBSABIAUgG3x8IQEgDSABhUIQiiENIAkgDXwhCSAFIAmFQj+KIQUgAiAGIBV8fCECIA4gAoVCIIohDiAKIA58IQogBiAKhUIYiiEGIAIgBiAZfHwhAiAOIAKFQhCKIQ4gCiAOfCEKIAYgCoVCP4ohBiADIAcgGnx8IQMgDyADhUIgiiEPIAsgD3whCyAHIAuFQhiKIQcgAyAHICB8fCEDIA8gA4VCEIohDyALIA98IQsgByALhUI/iiEHIAQgCCAefHwhBCAQIASFQiCKIRAgDCAQfCEMIAggDIVCGIohCCAEIAggF3x8IQQgECAEhUIQiiEQIAwgEHwhDCAIIAyFQj+KIQggASAGIBJ8fCEBIBAgAYVCIIohECALIBB8IQsgBiALhUIYiiEGIAEgBiAdfHwhASAQIAGFQhCKIRAgCyAQfCELIAYgC4VCP4ohBiACIAcgEXx8IQIgDSAChUIgiiENIAwgDXwhDCAHIAyFQhiKIQcgAiAHIBN8fCECIA0gAoVCEIohDSAMIA18IQwgByAMhUI/iiEHIAMgCCAcfHwhAyAOIAOFQiCKIQ4gCSAOfCEJIAggCYVCGIohCCADIAggGHx8IQMgDiADhUIQiiEOIAkgDnwhCSAIIAmFQj+KIQggBCAFIBZ8fCEEIA8gBIVCIIohDyAKIA98IQogBSAKhUIYiiEFIAQgBSAUfHwhBCAPIASFQhCKIQ8gCiAPfCEKIAUgCoVCP4ohBSABIAUgHHx8IQEgDSABhUIgiiENIAkgDXwhCSAFIAmFQhiKIQUgASAFIBl8fCEBIA0gAYVCEIohDSAJIA18IQkgBSAJhUI/iiEFIAIgBiAdfHwhAiAOIAKFQiCKIQ4gCiAOfCEKIAYgCoVCGIohBiACIAYgEXx8IQIgDiAChUIQiiEOIAogDnwhCiAGIAqFQj+KIQYgAyAHIBZ8fCEDIA8gA4VCIIohDyALIA98IQsgByALhUIYiiEHIAMgByATfHwhAyAPIAOFQhCKIQ8gCyAPfCELIAcgC4VCP4ohByAEIAggIHx8IQQgECAEhUIgiiEQIAwgEHwhDCAIIAyFQhiKIQggBCAIIB58fCEEIBAgBIVCEIohECAMIBB8IQwgCCAMhUI/iiEIIAEgBiAbfHwhASAQIAGFQiCKIRAgCyAQfCELIAYgC4VCGIohBiABIAYgH3x8IQEgECABhUIQiiEQIAsgEHwhCyAGIAuFQj+KIQYgAiAHIBR8fCECIA0gAoVCIIohDSAMIA18IQwgByAMhUIYiiEHIAIgByAXfHwhAiANIAKFQhCKIQ0gDCANfCEMIAcgDIVCP4ohByADIAggGHx8IQMgDiADhUIgiiEOIAkgDnwhCSAIIAmFQhiKIQggAyAIIBJ8fCEDIA4gA4VCEIohDiAJIA58IQkgCCAJhUI/iiEIIAQgBSAafHwhBCAPIASFQiCKIQ8gCiAPfCEKIAUgCoVCGIohBSAEIAUgFXx8IQQgDyAEhUIQiiEPIAogD3whCiAFIAqFQj+KIQUgASAFIBh8fCEBIA0gAYVCIIohDSAJIA18IQkgBSAJhUIYiiEFIAEgBSAafHwhASANIAGFQhCKIQ0gCSANfCEJIAUgCYVCP4ohBSACIAYgFHx8IQIgDiAChUIgiiEOIAogDnwhCiAGIAqFQhiKIQYgAiAGIBJ8fCECIA4gAoVCEIohDiAKIA58IQogBiAKhUI/iiEGIAMgByAefHwhAyAPIAOFQiCKIQ8gCyAPfCELIAcgC4VCGIohByADIAcgHXx8IQMgDyADhUIQiiEPIAsgD3whCyAHIAuFQj+KIQcgBCAIIBx8fCEEIBAgBIVCIIohECAMIBB8IQwgCCAMhUIY
var ready = null;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
var mod = {
buffer: wasm,
memory: null,
exports: null,
realloc: realloc,
onload: onload
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
onload(function () {});
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return mod
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function realloc (size) {
mod.exports.memory.grow(Math.max(0, Math.ceil(Math.abs(size - mod.memory.length) / 65536)));
mod.memory = new Uint8Array(mod.exports.memory.buffer);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function onload (cb) {
if (mod.exports) return cb()
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (ready) {
ready.then(cb.bind(null, null)).catch(cb);
return
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
try {
if (opts && opts.async) throw new Error('async')
setup({instance: new WebAssembly.Instance(new WebAssembly.Module(wasm), imp)});
} catch (err) {
ready = WebAssembly.instantiate(wasm, imp).then(setup);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
onload(cb);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function setup (w) {
mod.exports = w.instance.exports;
mod.memory = mod.exports.memory && mod.exports.memory.buffer && new Uint8Array(mod.exports.memory.buffer);
}
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function toUint8Array (s) {
if (typeof atob === 'function') return new Uint8Array(atob(s).split('').map(charCodeAt))
2021-01-17 11:56:08 +03:00
return (commonjsRequire('buf' + 'fer').Buffer).from(s, 'base64')
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function charCodeAt (c) {
return c.charCodeAt(0)
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
var blake2bWasm = createCommonjsModule(function (module) {
var wasm = blake2b();
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
var head = 64;
var freeList = [];
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
module.exports = Blake2b;
var BYTES_MIN = module.exports.BYTES_MIN = 16;
var BYTES_MAX = module.exports.BYTES_MAX = 64;
var BYTES = module.exports.BYTES = 32;
var KEYBYTES_MIN = module.exports.KEYBYTES_MIN = 16;
var KEYBYTES_MAX = module.exports.KEYBYTES_MAX = 64;
var KEYBYTES = module.exports.KEYBYTES = 32;
var SALTBYTES = module.exports.SALTBYTES = 16;
var PERSONALBYTES = module.exports.PERSONALBYTES = 16;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function Blake2b (digestLength, key, salt, personal, noAssert) {
if (!(this instanceof Blake2b)) return new Blake2b(digestLength, key, salt, personal, noAssert)
if (!(wasm && wasm.exports)) throw new Error('WASM not loaded. Wait for Blake2b.ready(cb)')
if (!digestLength) digestLength = 32;
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
if (noAssert !== true) {
nanoassert(digestLength >= BYTES_MIN, 'digestLength must be at least ' + BYTES_MIN + ', was given ' + digestLength);
nanoassert(digestLength <= BYTES_MAX, 'digestLength must be at most ' + BYTES_MAX + ', was given ' + digestLength);
if (key != null) {
nanoassert(key instanceof Uint8Array, 'key must be Uint8Array or Buffer');
nanoassert(key.length >= KEYBYTES_MIN, 'key must be at least ' + KEYBYTES_MIN + ', was given ' + key.length);
nanoassert(key.length <= KEYBYTES_MAX, 'key must be at least ' + KEYBYTES_MAX + ', was given ' + key.length);
}
if (salt != null) {
nanoassert(salt instanceof Uint8Array, 'salt must be Uint8Array or Buffer');
nanoassert(salt.length === SALTBYTES, 'salt must be exactly ' + SALTBYTES + ', was given ' + salt.length);
}
if (personal != null) {
nanoassert(personal instanceof Uint8Array, 'personal must be Uint8Array or Buffer');
nanoassert(personal.length === PERSONALBYTES, 'personal must be exactly ' + PERSONALBYTES + ', was given ' + personal.length);
}
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (!freeList.length) {
freeList.push(head);
head += 216;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
this.digestLength = digestLength;
this.finalized = false;
this.pointer = freeList.pop();
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
wasm.memory.fill(0, 0, 64);
wasm.memory[0] = this.digestLength;
wasm.memory[1] = key ? key.length : 0;
wasm.memory[2] = 1; // fanout
wasm.memory[3] = 1; // depth
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (salt) wasm.memory.set(salt, 32);
if (personal) wasm.memory.set(personal, 48);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (this.pointer + 216 > wasm.memory.length) wasm.realloc(this.pointer + 216); // we need 216 bytes for the state
wasm.exports.blake2b_init(this.pointer, this.digestLength);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (key) {
this.update(key);
wasm.memory.fill(0, head, head + key.length); // whiteout key
wasm.memory[this.pointer + 200] = 128;
}
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
Blake2b.prototype.update = function (input) {
nanoassert(this.finalized === false, 'Hash instance finalized');
nanoassert(input instanceof Uint8Array, 'input must be Uint8Array or Buffer');
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (head + input.length > wasm.memory.length) wasm.realloc(head + input.length);
wasm.memory.set(input, head);
wasm.exports.blake2b_update(this.pointer, head, head + input.length);
return this
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
Blake2b.prototype.getPartialHash = function () {
return wasm.memory.slice(this.pointer, this.pointer+216);
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
Blake2b.prototype.setPartialHash = function (ph) {
wasm.memory.set(ph, this.pointer);
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
Blake2b.prototype.digest = function (enc) {
nanoassert(this.finalized === false, 'Hash instance finalized');
this.finalized = true;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
freeList.push(this.pointer);
wasm.exports.blake2b_final(this.pointer);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (!enc || enc === 'binary') {
return wasm.memory.slice(this.pointer + 128, this.pointer + 128 + this.digestLength)
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
if (enc === 'hex') {
return hexSlice(wasm.memory, this.pointer + 128, this.digestLength)
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
nanoassert(enc instanceof Uint8Array && enc.length >= this.digestLength, 'input must be Uint8Array or Buffer');
for (var i = 0; i < this.digestLength; i++) {
enc[i] = wasm.memory[this.pointer + 128 + i];
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return enc
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
// libsodium compat
Blake2b.prototype.final = Blake2b.prototype.digest;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
Blake2b.WASM = wasm && wasm.buffer;
Blake2b.SUPPORTED = typeof WebAssembly !== 'undefined';
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
Blake2b.ready = function (cb) {
if (!cb) cb = noop;
if (!wasm) return cb(new Error('WebAssembly not supported'))
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
// backwards compat, can be removed in a new major
var p = new Promise(function (reject, resolve) {
wasm.onload(function (err) {
if (err) resolve();
else reject();
cb(err);
});
});
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return p
};
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
Blake2b.prototype.ready = Blake2b.ready;
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function noop () {}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function hexSlice (buf, start, len) {
var str = '';
for (var i = 0; i < len; i++) str += toHex(buf[start + i]);
return str
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function toHex (n) {
if (n < 16) return '0' + n.toString(16)
return n.toString(16)
}
});
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
/* global window */
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const _revTable$2 = [];
for (let i=0; i<256; i++) {
_revTable$2[i] = _revSlow$2(i, 8);
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function _revSlow$2(idx, bits) {
let res =0;
let a = idx;
for (let i=0; i<bits; i++) {
res <<= 1;
res = res | (a &1);
a >>=1;
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
return res;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function log2$1( V )
{
return( ( ( V & 0xFFFF0000 ) !== 0 ? ( V &= 0xFFFF0000, 16 ) : 0 ) | ( ( V & 0xFF00FF00 ) !== 0 ? ( V &= 0xFF00FF00, 8 ) : 0 ) | ( ( V & 0xF0F0F0F0 ) !== 0 ? ( V &= 0xF0F0F0F0, 4 ) : 0 ) | ( ( V & 0xCCCCCCCC ) !== 0 ? ( V &= 0xCCCCCCCC, 2 ) : 0 ) | ( ( V & 0xAAAAAAAA ) !== 0 ) );
}
function formatHash(b, title) {
const a = new DataView(b.buffer, b.byteOffset, b.byteLength);
let S = "";
for (let i=0; i<4; i++) {
if (i>0) S += "\n";
S += "\t\t";
for (let j=0; j<4; j++) {
if (j>0) S += " ";
S += a.getUint32(i*16+j*4).toString(16).padStart(8, "0");
2020-08-24 10:32:47 +03:00
}
}
2020-08-29 15:12:24 +03:00
if (title) S = title + "\n" + S;
return S;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function hashIsEqual(h1, h2) {
if (h1.byteLength != h2.byteLength) return false;
var dv1 = new Int8Array(h1);
var dv2 = new Int8Array(h2);
for (var i = 0 ; i != h1.byteLength ; i++)
{
if (dv1[i] != dv2[i]) return false;
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
return true;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function cloneHasher(h) {
const ph = h.getPartialHash();
const res = blake2bWasm(64);
res.setPartialHash(ph);
return res;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async function sameRatio(curve, g1s, g1sx, g2s, g2sx) {
if (curve.G1.isZero(g1s)) return false;
if (curve.G1.isZero(g1sx)) return false;
if (curve.G2.isZero(g2s)) return false;
if (curve.G2.isZero(g2sx)) return false;
// return curve.F12.eq(curve.pairing(g1s, g2sx), curve.pairing(g1sx, g2s));
const res = await curve.pairingEq(g1s, g2sx, curve.G1.neg(g1sx), g2s);
return res;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function askEntropy() {
{
return window.prompt("Enter a random text. (Entropy): ", "");
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async function getRandomRng(entropy) {
// Generate a random Rng
while (!entropy) {
entropy = await askEntropy();
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
const hasher = blake2bWasm(64);
hasher.update(crypto.randomBytes(64));
const enc = new TextEncoder(); // always utf-8
hasher.update(enc.encode(entropy));
const hash = Buffer.from(hasher.digest());
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const seed = [];
for (let i=0;i<8;i++) {
seed[i] = hash.readUInt32BE(i*4);
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
const rng = new ChaCha(seed);
return rng;
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function rngFromBeaconParams(beaconHash, numIterationsExp) {
let nIterationsInner;
let nIterationsOuter;
if (numIterationsExp<32) {
nIterationsInner = (1 << numIterationsExp) >>> 0;
nIterationsOuter = 1;
} else {
nIterationsInner = 0x100000000;
nIterationsOuter = (1 << (numIterationsExp-32)) >>> 0;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
let curHash = beaconHash;
for (let i=0; i<nIterationsOuter; i++) {
for (let j=0; j<nIterationsInner; j++) {
curHash = crypto.createHash("sha256").update(curHash).digest();
}
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const curHashV = new DataView(curHash.buffer, curHash.byteOffset, curHash.byteLength);
const seed = [];
for (let i=0; i<8; i++) {
seed[i] = curHashV.getUint32(i*4, false);
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
const rng = new ChaCha(seed);
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
return rng;
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function hex2ByteArray(s) {
if (s instanceof Uint8Array) return s;
if (s.slice(0,2) == "0x") s= s.slice(2);
return new Uint8Array(s.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16);
}));
2020-08-24 10:32:47 +03:00
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
function byteArray2hex(byteArray) {
return Array.prototype.map.call(byteArray, function(byte) {
return ("0" + (byte & 0xFF).toString(16)).slice(-2);
}).join("");
}
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
async function writeHeader(fd, zkey) {
2020-07-11 11:31:52 +03:00
2020-08-29 15:12:24 +03:00
// Write the header
///////////
await startWriteSection(fd, 1);
await fd.writeULE32(1); // Groth
await endWriteSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
// Write the Groth header section
///////////
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const curve = await getCurveFromQ(zkey.q);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
await startWriteSection(fd, 2);
const primeQ = curve.q;
const n8q = (Math.floor( (Scalar$1.bitLength(primeQ) - 1) / 64) +1)*8;
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const primeR = curve.r;
const n8r = (Math.floor( (Scalar$1.bitLength(primeR) - 1) / 64) +1)*8;
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
await fd.writeULE32(n8q);
await writeBigInt(fd, primeQ, n8q);
await fd.writeULE32(n8r);
await writeBigInt(fd, primeR, n8r);
await fd.writeULE32(zkey.nVars); // Total number of bars
await fd.writeULE32(zkey.nPublic); // Total number of public vars (not including ONE)
await fd.writeULE32(zkey.domainSize); // domainSize
await writeG1(fd, curve, zkey.vk_alpha_1);
await writeG1(fd, curve, zkey.vk_beta_1);
await writeG2(fd, curve, zkey.vk_beta_2);
await writeG2(fd, curve, zkey.vk_gamma_2);
await writeG1(fd, curve, zkey.vk_delta_1);
await writeG2(fd, curve, zkey.vk_delta_2);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
await endWriteSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
async function writeG1(fd, curve, p) {
const buff = new Uint8Array(curve.G1.F.n8*2);
curve.G1.toRprLEM(buff, 0, p);
await fd.write(buff);
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
async function writeG2(fd, curve, p) {
const buff = new Uint8Array(curve.G2.F.n8*2);
curve.G2.toRprLEM(buff, 0, p);
await fd.write(buff);
}
2020-08-12 02:33:08 +03:00
2021-01-28 23:55:41 +03:00
async function readG1(fd, curve, toObject) {
2020-08-29 15:12:24 +03:00
const buff = await fd.read(curve.G1.F.n8*2);
2021-01-28 23:55:41 +03:00
const res = curve.G1.fromRprLEM(buff, 0);
return toObject ? curve.G1.toObject(res) : res;
2020-08-29 15:12:24 +03:00
}
2020-08-12 02:33:08 +03:00
2021-01-28 23:55:41 +03:00
async function readG2(fd, curve, toObject) {
2020-08-29 15:12:24 +03:00
const buff = await fd.read(curve.G2.F.n8*2);
2021-01-28 23:55:41 +03:00
const res = curve.G2.fromRprLEM(buff, 0);
return toObject ? curve.G2.toObject(res) : res;
2020-08-29 15:12:24 +03:00
}
2020-08-12 02:33:08 +03:00
2021-01-28 23:55:41 +03:00
async function readHeader(fd, sections, protocol, toObject) {
2020-08-29 15:12:24 +03:00
if (protocol != "groth16") throw new Error("Protocol not supported: "+protocol);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const zkey = {};
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
// Read Header
/////////////////////
await startReadUniqueSection(fd, sections, 1);
const protocolId = await fd.readULE32();
if (protocolId != 1) throw new Error("File is not groth");
zkey.protocol = "groth16";
await endReadSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
// Read Groth Header
/////////////////////
await startReadUniqueSection(fd, sections, 2);
const n8q = await fd.readULE32();
zkey.n8q = n8q;
zkey.q = await readBigInt(fd, n8q);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const n8r = await fd.readULE32();
zkey.n8r = n8r;
zkey.r = await readBigInt(fd, n8r);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
let curve = await getCurveFromQ(zkey.q);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
zkey.nVars = await fd.readULE32();
zkey.nPublic = await fd.readULE32();
zkey.domainSize = await fd.readULE32();
zkey.power = log2$1(zkey.domainSize);
2021-01-28 23:55:41 +03:00
zkey.vk_alpha_1 = await readG1(fd, curve, toObject);
zkey.vk_beta_1 = await readG1(fd, curve, toObject);
zkey.vk_beta_2 = await readG2(fd, curve, toObject);
zkey.vk_gamma_2 = await readG2(fd, curve, toObject);
zkey.vk_delta_1 = await readG1(fd, curve, toObject);
zkey.vk_delta_2 = await readG2(fd, curve, toObject);
2020-08-29 15:12:24 +03:00
await endReadSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
return zkey;
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
}
2020-08-12 02:33:08 +03:00
2021-01-28 23:55:41 +03:00
async function readZKey(fileName, toObject) {
2020-08-29 15:12:24 +03:00
const {fd, sections} = await readBinFile(fileName, "zkey", 1);
2020-08-12 02:33:08 +03:00
2021-01-28 23:55:41 +03:00
const zkey = await readHeader(fd, sections, "groth16", toObject);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const Fr = new F1Field(zkey.r);
const Rr = Scalar$1.mod(Scalar$1.shl(1, zkey.n8r*8), zkey.r);
const Rri = Fr.inv(Rr);
const Rri2 = Fr.mul(Rri, Rri);
2020-08-12 02:33:08 +03:00
2021-01-28 23:55:41 +03:00
let curve = await getCurveFromQ(zkey.q);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
// Read IC Section
///////////
await startReadUniqueSection(fd, sections, 3);
zkey.IC = [];
for (let i=0; i<= zkey.nPublic; i++) {
2021-01-28 23:55:41 +03:00
const P = await readG1(fd, curve, toObject);
2020-08-29 15:12:24 +03:00
zkey.IC.push(P);
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
await endReadSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
// Read Coefs
///////////
await startReadUniqueSection(fd, sections, 4);
const nCCoefs = await fd.readULE32();
zkey.ccoefs = [];
for (let i=0; i<nCCoefs; i++) {
const m = await fd.readULE32();
const c = await fd.readULE32();
const s = await fd.readULE32();
const v = await readFr2();
zkey.ccoefs.push({
matrix: m,
constraint: c,
signal: s,
value: v
});
}
await endReadSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
// Read A points
///////////
await startReadUniqueSection(fd, sections, 5);
zkey.A = [];
for (let i=0; i<zkey.nVars; i++) {
2021-01-28 23:55:41 +03:00
const A = await readG1(fd, curve, toObject);
2020-08-29 15:12:24 +03:00
zkey.A[i] = A;
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
await endReadSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
// Read B1
///////////
await startReadUniqueSection(fd, sections, 6);
zkey.B1 = [];
for (let i=0; i<zkey.nVars; i++) {
2021-01-28 23:55:41 +03:00
const B1 = await readG1(fd, curve, toObject);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
zkey.B1[i] = B1;
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
await endReadSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
// Read B2 points
///////////
await startReadUniqueSection(fd, sections, 7);
zkey.B2 = [];
for (let i=0; i<zkey.nVars; i++) {
2021-01-28 23:55:41 +03:00
const B2 = await readG2(fd, curve, toObject);
2020-08-29 15:12:24 +03:00
zkey.B2[i] = B2;
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
await endReadSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
// Read C points
///////////
await startReadUniqueSection(fd, sections, 8);
zkey.C = [];
for (let i=zkey.nPublic+1; i<zkey.nVars; i++) {
2021-01-28 23:55:41 +03:00
const C = await readG1(fd, curve, toObject);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
zkey.C[i] = C;
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
await endReadSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
// Read H points
///////////
await startReadUniqueSection(fd, sections, 9);
zkey.hExps = [];
for (let i=0; i<zkey.domainSize; i++) {
2021-01-28 23:55:41 +03:00
const H = await readG1(fd, curve, toObject);
2020-08-29 15:12:24 +03:00
zkey.hExps.push(H);
}
await endReadSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
await fd.close();
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
return zkey;
2020-08-12 02:33:08 +03:00
2021-01-28 23:55:41 +03:00
async function readFr2(toObject) {
2020-08-29 15:12:24 +03:00
const n = await readBigInt(fd, zkey.n8r);
return Fr.mul(n, Rri2);
2020-08-24 10:32:47 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
}
2020-08-12 02:33:08 +03:00
2021-01-28 23:55:41 +03:00
async function readContribution(fd, curve, toObject) {
2020-08-29 15:12:24 +03:00
const c = {delta:{}};
2021-01-28 23:55:41 +03:00
c.deltaAfter = await readG1(fd, curve, toObject);
c.delta.g1_s = await readG1(fd, curve, toObject);
c.delta.g1_sx = await readG1(fd, curve, toObject);
c.delta.g2_spx = await readG2(fd, curve, toObject);
2020-08-29 15:12:24 +03:00
c.transcript = await fd.read(64);
c.type = await fd.readULE32();
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const paramLength = await fd.readULE32();
const curPos = fd.pos;
let lastType =0;
while (fd.pos-curPos < paramLength) {
const buffType = await fd.read(1);
if (buffType[0]<= lastType) throw new Error("Parameters in the contribution must be sorted");
lastType = buffType[0];
if (buffType[0]==1) { // Name
const buffLen = await fd.read(1);
const buffStr = await fd.read(buffLen[0]);
c.name = new TextDecoder().decode(buffStr);
} else if (buffType[0]==2) {
const buffExp = await fd.read(1);
c.numIterationsExp = buffExp[0];
} else if (buffType[0]==3) {
const buffLen = await fd.read(1);
c.beaconHash = await fd.read(buffLen[0]);
} else {
throw new Error("Parameter not recognized");
}
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
if (fd.pos != curPos + paramLength) {
throw new Error("Parametes do not match");
2020-08-24 10:32:47 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
return c;
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
async function readMPCParams(fd, curve, sections) {
await startReadUniqueSection(fd, sections, 10);
const res = { contributions: []};
res.csHash = await fd.read(64);
const n = await fd.readULE32();
for (let i=0; i<n; i++) {
const c = await readContribution(fd, curve);
res.contributions.push(c);
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
await endReadSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
return res;
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
async function writeContribution(fd, curve, c) {
await writeG1(fd, curve, c.deltaAfter);
await writeG1(fd, curve, c.delta.g1_s);
await writeG1(fd, curve, c.delta.g1_sx);
await writeG2(fd, curve, c.delta.g2_spx);
await fd.write(c.transcript);
await fd.writeULE32(c.type || 0);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const params = [];
if (c.name) {
params.push(1); // Param Name
const nameData = new TextEncoder("utf-8").encode(c.name.substring(0,64));
params.push(nameData.byteLength);
for (let i=0; i<nameData.byteLength; i++) params.push(nameData[i]);
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
if (c.type == 1) {
params.push(2); // Param numIterationsExp
params.push(c.numIterationsExp);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
params.push(3); // Beacon Hash
params.push(c.beaconHash.byteLength);
for (let i=0; i<c.beaconHash.byteLength; i++) params.push(c.beaconHash[i]);
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
if (params.length>0) {
const paramsBuff = new Uint8Array(params);
await fd.writeULE32(paramsBuff.byteLength);
await fd.write(paramsBuff);
} else {
await fd.writeULE32(0);
2020-08-24 10:32:47 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
async function writeMPCParams(fd, curve, mpcParams) {
await startWriteSection(fd, 10);
await fd.write(mpcParams.csHash);
await fd.writeULE32(mpcParams.contributions.length);
for (let i=0; i<mpcParams.contributions.length; i++) {
await writeContribution(fd, curve,mpcParams.contributions[i]);
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
await endWriteSection(fd);
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
function hashG1(hasher, curve, p) {
const buff = new Uint8Array(curve.G1.F.n8*2);
curve.G1.toRprUncompressed(buff, 0, p);
hasher.update(buff);
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
function hashG2(hasher,curve, p) {
const buff = new Uint8Array(curve.G2.F.n8*2);
curve.G2.toRprUncompressed(buff, 0, p);
hasher.update(buff);
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
function hashPubKey(hasher, curve, c) {
hashG1(hasher, curve, c.deltaAfter);
hashG1(hasher, curve, c.delta.g1_s);
hashG1(hasher, curve, c.delta.g1_sx);
hashG2(hasher, curve, c.delta.g2_spx);
hasher.update(c.transcript);
2020-08-24 10:32:47 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
async function write(fd, witness, prime) {
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
await startWriteSection(fd, 1);
const n8 = (Math.floor( (Scalar$1.bitLength(prime) - 1) / 64) +1)*8;
await fd.writeULE32(n8);
await writeBigInt(fd, prime, n8);
await fd.writeULE32(witness.length);
await endWriteSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
await startWriteSection(fd, 2);
for (let i=0; i<witness.length; i++) {
await writeBigInt(fd, witness[i], n8);
2020-08-24 10:32:47 +03:00
}
2020-08-29 15:12:24 +03:00
await endWriteSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-24 10:32:47 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
async function writeBin(fd, witnessBin, prime) {
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
await startWriteSection(fd, 1);
const n8 = (Math.floor( (Scalar$1.bitLength(prime) - 1) / 64) +1)*8;
await fd.writeULE32(n8);
await writeBigInt(fd, prime, n8);
if (witnessBin.byteLength % n8 != 0) {
throw new Error("Invalid witness length");
}
await fd.writeULE32(witnessBin.byteLength / n8);
await endWriteSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
await startWriteSection(fd, 2);
await fd.write(witnessBin);
await endWriteSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
async function readHeader$1(fd, sections) {
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
await startReadUniqueSection(fd, sections, 1);
const n8 = await fd.readULE32();
const q = await readBigInt(fd, n8);
const nWitness = await fd.readULE32();
await endReadSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
return {n8, q, nWitness};
2020-08-12 02:33:08 +03:00
2020-08-24 10:32:47 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
async function read(fileName) {
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const {fd, sections} = await readBinFile(fileName, "wtns", 2);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const {n8, nWitness} = await readHeader$1(fd, sections);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
await startReadUniqueSection(fd, sections, 2);
const res = [];
for (let i=0; i<nWitness; i++) {
const v = await readBigInt(fd, n8);
res.push(v);
}
await endReadSection(fd);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
await fd.close();
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
return res;
2020-08-24 10:32:47 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const {stringifyBigInts: stringifyBigInts$3} = utils$1;
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
async function groth16Prove(zkeyFileName, witnessFileName, logger) {
const {fd: fdWtns, sections: sectionsWtns} = await readBinFile(witnessFileName, "wtns", 2);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const wtns = await readHeader$1(fdWtns, sectionsWtns);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const {fd: fdZKey, sections: sectionsZKey} = await readBinFile(zkeyFileName, "zkey", 2);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const zkey = await readHeader(fdZKey, sectionsZKey, "groth16");
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
if (!Scalar$1.eq(zkey.r, wtns.q)) {
throw new Error("Curve of the witness does not match the curve of the proving key");
2020-08-24 10:32:47 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
if (wtns.nWitness != zkey.nVars) {
throw new Error(`Invalid witness length. Circuit: ${zkey.nVars}, witness: ${wtns.nWitness}`);
2020-08-24 10:32:47 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const curve = await getCurveFromQ(zkey.q);
const Fr = curve.Fr;
const G1 = curve.G1;
const G2 = curve.G2;
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const power = log2$1(zkey.domainSize);
2020-08-12 02:33:08 +03:00
2020-10-22 15:58:12 +03:00
if (logger) logger.debug("Reading Wtns");
2020-10-22 15:52:18 +03:00
const buffWitness = await readSection(fdWtns, sectionsWtns, 2);
2020-10-22 15:58:12 +03:00
if (logger) logger.debug("Reading Coeffs");
2020-10-22 15:52:18 +03:00
const buffCoeffs = await readSection(fdZKey, sectionsZKey, 4);
2020-10-23 12:34:00 +03:00
if (logger) logger.debug("Building ABC");
const [buffA_T, buffB_T, buffC_T] = await buldABC1(curve, zkey, buffWitness, buffCoeffs, logger);
2020-09-07 13:43:50 +03:00
const inc = power == Fr.s ? curve.Fr.shift : curve.Fr.w[power+1];
2020-10-25 13:12:57 +03:00
const buffA = await Fr.ifft(buffA_T, "", "", logger, "IFFT_A");
2020-09-07 13:43:50 +03:00
const buffAodd = await Fr.batchApplyKey(buffA, Fr.e(1), inc);
2020-10-25 13:12:57 +03:00
const buffAodd_T = await Fr.fft(buffAodd, "", "", logger, "FFT_A");
2020-08-12 02:33:08 +03:00
2020-10-25 13:12:57 +03:00
const buffB = await Fr.ifft(buffB_T, "", "", logger, "IFFT_B");
2020-09-07 13:43:50 +03:00
const buffBodd = await Fr.batchApplyKey(buffB, Fr.e(1), inc);
2020-10-25 13:12:57 +03:00
const buffBodd_T = await Fr.fft(buffBodd, "", "", logger, "FFT_B");
2020-08-12 02:33:08 +03:00
2020-10-25 13:12:57 +03:00
const buffC = await Fr.ifft(buffC_T, "", "", logger, "IFFT_C");
2020-09-07 13:43:50 +03:00
const buffCodd = await Fr.batchApplyKey(buffC, Fr.e(1), inc);
2020-10-25 13:12:57 +03:00
const buffCodd_T = await Fr.fft(buffCodd, "", "", logger, "FFT_C");
2020-08-12 02:33:08 +03:00
2020-10-25 13:12:57 +03:00
if (logger) logger.debug("Join ABC");
const buffPodd_T = await joinABC(curve, zkey, buffAodd_T, buffBodd_T, buffCodd_T, logger);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
let proof = {};
2020-08-12 02:33:08 +03:00
2020-10-25 13:12:57 +03:00
if (logger) logger.debug("Reading A Points");
const buffBasesA = await readSection(fdZKey, sectionsZKey, 5);
proof.pi_a = await curve.G1.multiExpAffine(buffBasesA, buffWitness, logger, "multiexp A");
if (logger) logger.debug("Reading B1 Points");
const buffBasesB1 = await readSection(fdZKey, sectionsZKey, 6);
let pib1 = await curve.G1.multiExpAffine(buffBasesB1, buffWitness, logger, "multiexp B1");
if (logger) logger.debug("Reading B2 Points");
const buffBasesB2 = await readSection(fdZKey, sectionsZKey, 7);
proof.pi_b = await curve.G2.multiExpAffine(buffBasesB2, buffWitness, logger, "multiexp B2");
if (logger) logger.debug("Reading C Points");
const buffBasesC = await readSection(fdZKey, sectionsZKey, 8);
proof.pi_c = await curve.G1.multiExpAffine(buffBasesC, buffWitness.slice((zkey.nPublic+1)*curve.Fr.n8), logger, "multiexp C");
if (logger) logger.debug("Reading H Points");
const buffBasesH = await readSection(fdZKey, sectionsZKey, 9);
const resH = await curve.G1.multiExpAffine(buffBasesH, buffPodd_T, logger, "multiexp H");
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const r = curve.Fr.random();
const s = curve.Fr.random();
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
proof.pi_a = G1.add( proof.pi_a, zkey.vk_alpha_1 );
proof.pi_a = G1.add( proof.pi_a, G1.timesFr( zkey.vk_delta_1, r ));
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
proof.pi_b = G2.add( proof.pi_b, zkey.vk_beta_2 );
proof.pi_b = G2.add( proof.pi_b, G2.timesFr( zkey.vk_delta_2, s ));
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
pib1 = G1.add( pib1, zkey.vk_beta_1 );
pib1 = G1.add( pib1, G1.timesFr( zkey.vk_delta_1, s ));
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
proof.pi_c = G1.add(proof.pi_c, resH);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
proof.pi_c = G1.add( proof.pi_c, G1.timesFr( proof.pi_a, s ));
proof.pi_c = G1.add( proof.pi_c, G1.timesFr( pib1, r ));
proof.pi_c = G1.add( proof.pi_c, G1.timesFr( zkey.vk_delta_1, Fr.neg(Fr.mul(r,s) )));
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
let publicSignals = [];
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
for (let i=1; i<= zkey.nPublic; i++) {
const b = buffWitness.slice(i*Fr.n8, i*Fr.n8+Fr.n8);
publicSignals.push(Scalar$1.fromRprLE(b));
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
proof.pi_a = G1.toObject(G1.toAffine(proof.pi_a));
proof.pi_b = G2.toObject(G2.toAffine(proof.pi_b));
proof.pi_c = G1.toObject(G1.toAffine(proof.pi_c));
2020-08-24 10:32:47 +03:00
2020-08-29 15:12:24 +03:00
proof.protocol = "groth16";
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
await fdZKey.close();
await fdWtns.close();
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
proof = stringifyBigInts$3(proof);
publicSignals = stringifyBigInts$3(publicSignals);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
return {proof, publicSignals};
2020-08-12 02:33:08 +03:00
}
2020-10-23 12:34:00 +03:00
async function buldABC1(curve, zkey, witness, coeffs, logger) {
const n8 = curve.Fr.n8;
2020-08-29 15:12:24 +03:00
const sCoef = 4*3 + zkey.n8r;
2020-10-23 12:34:00 +03:00
const nCoef = (coeffs.byteLength-4) / sCoef;
const outBuffA = new BigBuffer(zkey.domainSize * n8);
const outBuffB = new BigBuffer(zkey.domainSize * n8);
const outBuffC = new BigBuffer(zkey.domainSize * n8);
const outBuf = [ outBuffA, outBuffB ];
for (let i=0; i<nCoef; i++) {
2020-10-25 12:26:07 +03:00
if ((logger)&&(i%1000000 == 0)) logger.debug(`QAP AB: ${i}/${nCoef}`);
2020-10-23 12:34:00 +03:00
const buffCoef = coeffs.slice(4+i*sCoef, 4+i*sCoef+sCoef);
const buffCoefV = new DataView(buffCoef.buffer);
const m= buffCoefV.getUint32(0, true);
const c= buffCoefV.getUint32(4, true);
const s= buffCoefV.getUint32(8, true);
2020-10-25 12:26:07 +03:00
const coef = buffCoef.slice(12, 12+n8);
2020-10-23 12:34:00 +03:00
outBuf[m].set(
curve.Fr.add(
outBuf[m].slice(c*n8, c*n8+n8),
curve.Fr.mul(coef, witness.slice(s*n8, s*n8+n8))
),
c*n8
);
2020-10-23 08:59:28 +03:00
}
2020-08-12 02:33:08 +03:00
2020-10-23 12:34:00 +03:00
for (let i=0; i<zkey.domainSize; i++) {
2020-10-25 12:26:07 +03:00
if ((logger)&&(i%1000000 == 0)) logger.debug(`QAP C: ${i}/${zkey.domainSize}`);
2020-10-23 12:34:00 +03:00
outBuffC.set(
curve.Fr.mul(
outBuffA.slice(i*n8, i*n8+n8),
outBuffB.slice(i*n8, i*n8+n8),
),
i*n8
);
2020-08-29 15:12:24 +03:00
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
return [outBuffA, outBuffB, outBuffC];
2020-08-12 02:33:08 +03:00
}
2020-10-25 13:12:57 +03:00
async function joinABC(curve, zkey, a, b, c, logger) {
const MAX_CHUNK_SIZE = 1 << 22;
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const n8 = curve.Fr.n8;
const nElements = Math.floor(a.byteLength / curve.Fr.n8);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const promises = [];
2020-08-12 02:33:08 +03:00
2020-10-25 13:12:57 +03:00
for (let i=0; i<nElements; i += MAX_CHUNK_SIZE) {
if (logger) logger.debug(`JoinABC: ${i}/${nElements}`);
const n= Math.min(nElements - i, MAX_CHUNK_SIZE);
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const task = [];
2020-08-12 02:33:08 +03:00
2020-10-25 13:12:57 +03:00
const aChunk = a.slice(i*n8, (i + n)*n8 );
const bChunk = b.slice(i*n8, (i + n)*n8 );
const cChunk = c.slice(i*n8, (i + n)*n8 );
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
task.push({cmd: "ALLOCSET", var: 0, buff: aChunk});
task.push({cmd: "ALLOCSET", var: 1, buff: bChunk});
task.push({cmd: "ALLOCSET", var: 2, buff: cChunk});
task.push({cmd: "ALLOC", var: 3, len: n*n8});
task.push({cmd: "CALL", fnName: "qap_joinABC", params:[
{var: 0},
{var: 1},
{var: 2},
{val: n},
{var: 3},
]});
task.push({cmd: "CALL", fnName: "frm_batchFromMontgomery", params:[
{var: 3},
{val: n},
{var: 3}
]});
task.push({cmd: "GET", out: 0, var: 3, len: n*n8});
promises.push(curve.tm.queueAction(task));
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
const result = await Promise.all(promises);
2020-08-12 02:33:08 +03:00
2020-10-25 14:22:07 +03:00
let outBuff;
if (a instanceof BigBuffer) {
outBuff = new BigBuffer(a.byteLength);
} else {
outBuff = new Uint8Array(a.byteLength);
}
2020-08-29 15:12:24 +03:00
let p=0;
for (let i=0; i<result.length; i++) {
outBuff.set(result[i][0], p);
p += result[i][0].byteLength;
}
2020-08-12 02:33:08 +03:00
2020-08-29 15:12:24 +03:00
return outBuff;
}
2020-08-12 02:33:08 +03:00
2021-01-21 21:26:22 +03:00
/**
* FNV-1a Hash implementation (32, 64, 128, 256, 512, and 1024 bit)
* @author Travis Webb <me@traviswebb.com>
* @see http://tools.ietf.org/html/draft-eastlake-fnv-06
*/
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
var fnvPlus = createCommonjsModule(function (module) {
var fnvplus = (function(){
var i, hl = [], hl16 = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'],
version = '1a',
useUTF8 = false,
_hash32, _hash52, _hash64, _hash128, _hash256, _hash512, _hash1024,
referenceSeed = 'chongo <Landon Curt Noll> /\\../\\',
defaultKeyspace = 52,
fnvConstants = {
32: {offset: 0},
64: {offset: [0,0,0,0]},
128: {offset: [0,0,0,0,0,0,0,0]},
256: {offset: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
512: {offset: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},
1024: {offset: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}};
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
for(i=0; i < 256; i++){
hl[i] = ((i >> 4) & 15).toString(16) + (i & 15).toString(16);
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
function hexToBase(hex, base){
var alphabet = '0123456789abcdefghijklmnopqrstuvwxyz',
digits = [0], carry, i, j, string = '';
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
for(i = 0; i < hex.length; i+=2){
carry = parseInt(hex.substr(i,2),16);
for(j = 0; j < digits.length; j++){
carry += digits[j] << 8;
digits[j] = carry % base;
carry = (carry / base) | 0;
}
while (carry > 0) {
digits.push(carry % base);
carry = (carry / base) | 0;
}
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
for (i = digits.length - 1; i >= 0; --i){
string += alphabet[digits[i]];
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
return string;
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
function hashValHex(value, keyspace) {
return {
bits: keyspace,
value: value,
dec: function(){return hexToBase(value, 10);},
hex: function(){return value;},
str: function(){return hexToBase(value, 36);}
};
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
function hashValInt32(value, keyspace) {
return {
bits: keyspace,
value: value,
dec: function(){return value.toString();},
hex: function(){return hl[value>>>24]+ hl[(value>>>16)&255]+hl[(value>>>8)&255]+hl[value&255];},
str: function(){return value.toString(36);}
};
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
function hashValInt52(value, keyspace) {
return {
bits: keyspace,
value: value,
dec: function(){return value.toString();},
hex: function(){return ('0000000000000000'+value.toString(16)).substr(-13);},
str: function(){return value.toString(36);}
};
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
function hash(message, keyspace) {
var str = (typeof message === 'object') ? JSON.stringify(message) : message;
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
switch(keyspace || defaultKeyspace){
case 32:
return _hash32(str);
case 64:
return _hash64(str);
case 128:
return _hash128(str);
case 256:
return _hash256(str);
case 512:
return _hash512(str);
case 1024:
return _hash1024(str);
default:
return _hash52(str);
}
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
function setKeyspace(keyspace) {
if (keyspace === 52 || fnvConstants[keyspace]) {
defaultKeyspace = keyspace;
} else {
throw new Error('Supported FNV keyspacs: 32, 52, 64, 128, 256, 512, and 1024 bit');
}
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
function setVersion(_version) {
if (_version === '1a' ) {
version = _version;
_hash32 = useUTF8 ? _hash32_1a_utf : _hash32_1a;
_hash52 = useUTF8 ? _hash52_1a_utf : _hash52_1a;
_hash64 = useUTF8 ? _hash64_1a_utf : _hash64_1a;
_hash128 = useUTF8 ? _hash128_1a_utf : _hash128_1a;
_hash256 = useUTF8 ? _hash256_1a_utf : _hash256_1a;
_hash512 = useUTF8 ? _hash512_1a_utf : _hash512_1a;
_hash1024 = useUTF8 ? _hash1024_1a_utf : _hash1024_1a;
} else if (_version === '1') {
version = _version;
_hash32 = useUTF8 ? _hash32_1_utf : _hash32_1;
_hash52 = useUTF8 ? _hash52_1_utf : _hash52_1;
_hash64 = useUTF8 ? _hash64_1_utf : _hash64_1;
_hash128 = useUTF8 ? _hash128_1_utf : _hash128_1;
_hash256 = useUTF8 ? _hash256_1_utf : _hash256_1;
_hash512 = useUTF8 ? _hash512_1_utf : _hash512_1;
_hash1024 = useUTF8 ? _hash1024_1_utf : _hash1024_1;
} else {
throw new Error('Supported FNV versions: 1, 1a');
}
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
function setUTF8(utf8) {
if (utf8) {
useUTF8 = true;
_hash32 = version == '1a' ? _hash32_1a_utf : _hash32_1_utf;
_hash52 = version == '1a' ? _hash52_1a_utf : _hash52_1_utf;
_hash64 = version == '1a' ? _hash64_1a_utf : _hash64_1_utf;
_hash128 = version == '1a' ? _hash128_1a_utf : _hash128_1_utf;
_hash256 = version == '1a' ? _hash256_1a_utf : _hash256_1_utf;
_hash512 = version == '1a' ? _hash512_1a_utf : _hash512_1_utf;
_hash1024 = version == '1a' ? _hash1024_1a_utf : _hash1024_1_utf;
} else {
useUTF8 = false;
_hash32 = version == '1a' ? _hash32_1a : _hash32_1;
_hash52 = version == '1a' ? _hash52_1a : _hash52_1;
_hash64 = version == '1a' ? _hash64_1a : _hash64_1;
_hash128 = version == '1a' ? _hash128_1a : _hash128_1;
_hash256 = version == '1a' ? _hash256_1a : _hash256_1;
_hash512 = version == '1a' ? _hash512_1a : _hash512_1;
_hash1024 = version == '1a' ? _hash1024_1a : _hash1024_1;
}
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
function seed(seed) {
var oldVersion = version, res, i;
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
seed = (seed || seed === 0) ? seed : referenceSeed;
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
if (seed === referenceSeed) setVersion('1');
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
for (var keysize in fnvConstants) {
fnvConstants[keysize].offset = [];
for(i = 0; i < keysize / 16; i++){
fnvConstants[keysize].offset[i] = 0;
}
res = hash(seed, parseInt(keysize, 10)).hex();
for(i = 0; i < keysize / 16; i++){
fnvConstants[keysize].offset[i] = parseInt(res.substr(i*4,4), 16);
}
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
setVersion(oldVersion);
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
/**
* Implementation without library overhead.
*/
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
function _hash32_1a_fast(str) {
var i, l = str.length-3, t0=0,v0=0x9dc5,t1=0,v1=0x811c;
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
return ((v1<<16)>>>0)+v0;
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
function _hash32_1a_fast_hex(str) {
var i, l = str.length-3, t0=0,v0=0x9dc5,t1=0,v1=0x811c;
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
return hl[(v1>>>8)&255]+hl[v1&255]+hl[(v0>>>8)&255]+hl[v0&255];
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
function _hash52_1a_fast(str){
var i,l=str.length-3,t0=0,v0=0x2325,t1=0,v1=0x8422,t2=0,v2=0x9ce4,t3=0,v3=0xcbf2;
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
return (v3&15) * 281474976710656 + v2 * 4294967296 + v1 * 65536 + (v0^(v3>>4));
}
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
function _hash52_1a_fast_hex(str){
var i,l=str.length-3,t0=0,v0=0x2325,t1=0,v1=0x8422,t2=0,v2=0x9ce4,t3=0,v3=0xcbf2;
2021-01-17 20:44:43 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2020-09-02 13:06:20 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2020-09-02 13:06:20 +03:00
2021-01-21 21:26:22 +03:00
return hl16[v3&15]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[(v0>>8)^(v3>>12)]+hl[(v0^(v3>>4))&255];
}
2020-09-02 13:06:20 +03:00
2021-01-21 21:26:22 +03:00
function _hash64_1a_fast(str){
var i,l=str.length-3,t0=0,v0=0x2325,t1=0,v1=0x8422,t2=0,v2=0x9ce4,t3=0,v3=0xcbf2;
2020-09-02 13:06:20 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2020-09-02 13:06:20 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2020-09-02 13:06:20 +03:00
2021-01-21 21:26:22 +03:00
return hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255];
}
2020-09-02 13:06:20 +03:00
2021-01-21 21:26:22 +03:00
function _hash32_1a_fast_utf(str) {
var c,i,l=str.length,t0=0,v0=0x9dc5,t1=0,v1=0x811c;
2020-09-07 13:43:50 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=((c>>12)&63)|128;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=((c>>6)&63)|128;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=((c>>6)&63)|128;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=(c&63)|128;
}
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
return ((v1<<16)>>>0)+v0;
}
2020-09-07 19:24:09 +03:00
2021-01-21 21:26:22 +03:00
function _hash32_1a_fast_hex_utf(str) {
var c,i,l=str.length,t0=0,v0=0x9dc5,t1=0,v1=0x811c;
2020-09-07 13:43:50 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=((c>>12)&63)|128;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=((c>>6)&63)|128;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=((c>>6)&63)|128;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=(c&63)|128;
}
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
}
2020-09-07 13:43:50 +03:00
2021-01-21 21:26:22 +03:00
return hl[(v1>>>8)&255]+hl[v1&255]+hl[(v0>>>8)&255]+hl[v0&255];
}
2020-09-07 13:43:50 +03:00
2021-01-21 21:26:22 +03:00
function _hash52_1a_fast_utf(str){
var c,i,l=str.length,t0=0,v0=0x2325,t1=0,v1=0x8422,t2=0,v2=0x9ce4,t3=0,v3=0xcbf2;
2020-09-07 13:43:50 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>12)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>6)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>6)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2020-09-07 13:43:50 +03:00
2021-01-21 21:26:22 +03:00
return (v3&15) * 281474976710656 + v2 * 4294967296 + v1 * 65536 + (v0^(v3>>4));
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
function _hash52_1a_fast_hex_utf (str){
var c,i,l=str.length,t0=0,v0=0x2325,t1=0,v1=0x8422,t2=0,v2=0x9ce4,t3=0,v3=0xcbf2;
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>12)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>6)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>6)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
return hl16[v3&15]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[(v0>>8)^(v3>>12)]+hl[(v0^(v3>>4))&255];
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
function _hash64_1a_fast_utf(str){
var c,i,l=str.length,t0=0,v0=0x2325,t1=0,v1=0x8422,t2=0,v2=0x9ce4,t3=0,v3=0xcbf2;
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>12)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>6)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>6)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
return hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255];
}
/**
* Regular functions. This versions are accessible through API
*/
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
function _hash32_1a(str){
var i,l=str.length-3,s=fnvConstants[32].offset,t0=0,v0=s[1]|0,t1=0,v1=s[0]|0;
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
return hashValInt32(((v1<<16)>>>0)+v0,32);
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
function _hash32_1(str){
var i,l=str.length-3,s=fnvConstants[32].offset,t0=0,v0=s[1]|0,t1=0,v1=s[0]|0;
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=str.charCodeAt(i++);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=str.charCodeAt(i++);
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=str.charCodeAt(i++);
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
return hashValInt32(((v1<<16)>>>0)+v0,32);
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
function _hash32_1a_utf(str){
var c,i,l=str.length,s=fnvConstants[32].offset,t0=0,v0=s[1]|0,t1=0,v1=s[0]|0;
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=((c>>12)&63)|128;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=((c>>6)&63)|128;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=((c>>6)&63)|128;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=(c&63)|128;
}
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
return hashValInt32(((v1<<16)>>>0)+v0,32);
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
function _hash32_1_utf(str){
var c,i,l=str.length,s=fnvConstants[32].offset,t0=0,v0=s[1]|0,t1=0,v1=s[0]|0;
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=((c>>12)&63)|128;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=((c>>6)&63)|128;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=((c>>6)&63)|128;
t0=v0*403;t1=v1*403;
t1+=v0<<8;
v1=(t1+(t0>>>16))&65535;v0=t0&65535;
v0^=(c&63)|128;
}
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
return hashValInt32(((v1<<16)>>>0)+v0,32);
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
_hash32 = _hash32_1a;
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
function _hash52_1a(str){
var i,l=str.length-3,s=fnvConstants[64].offset,t0=0,v0=s[3]|0,t1=0,v1=s[2]|0,t2=0,v2=s[1]|0,t3=0,v3=s[0]|0;
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
return hashValInt52((v3&15)*281474976710656+v2*4294967296+v1*65536+(v0^(v3>>4)),52);
}
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
function _hash52_1(str){
var i,l=str.length-3,s=fnvConstants[64].offset,t0=0,v0=s[3]|0,t1=0,v1=s[2]|0,t2=0,v2=s[1]|0,t3=0,v3=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValInt52((v3&15)*281474976710656+v2*4294967296+v1*65536+(v0^(v3>>4)),52);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash52_1a_utf(str){
var c,i,l=str.length,s=fnvConstants[64].offset,t0=0,v0=s[3]|0,t1=0,v1=s[2]|0,t2=0,v2=s[1]|0,t3=0,v3=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>12)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>6)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>6)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValInt52((v3&15)*281474976710656+v2*4294967296+v1*65536+(v0^(v3>>4)),52);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash52_1_utf(str){
var c,i,l=str.length,s=fnvConstants[64].offset,t0=0,v0=s[3]|0,t1=0,v1=s[2]|0,t2=0,v2=s[1]|0,t3=0,v3=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>12)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>6)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>6)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValInt52((v3&15)*281474976710656+v2*4294967296+v1*65536+(v0^(v3>>4)),52);
}
2020-08-12 02:33:08 +03:00
2021-01-21 21:26:22 +03:00
_hash52 = _hash52_1a;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash64_1a(str){
var i,l=str.length-3,s=fnvConstants[64].offset,t0=0,v0=s[3]|0,t1=0,v1=s[2]|0,t2=0,v2=s[1]|0,t3=0,v3=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2020-08-12 02:33:08 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2020-08-12 02:33:08 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],64);
}
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
function _hash64_1(str){
var i,l=str.length-3,s=fnvConstants[64].offset,t0=0,v0=s[3]|0,t1=0,v1=s[2]|0,t2=0,v2=s[1]|0,t3=0,v3=s[0]|0;
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
}
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=str.charCodeAt(i++);
}
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],64);
}
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
function _hash64_1a_utf(str){
var c,i,l=str.length,s=fnvConstants[64].offset,t0=0,v0=s[3]|0,t1=0,v1=s[2]|0,t2=0,v2=s[1]|0,t3=0,v3=s[0]|0;
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>12)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>6)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>6)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
}
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],64);
}
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
function _hash64_1_utf(str){
var c,i,l=str.length,s=fnvConstants[64].offset,t0=0,v0=s[3]|0,t1=0,v1=s[2]|0,t2=0,v2=s[1]|0,t3=0,v3=s[0]|0;
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>12)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>6)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=((c>>6)&63)|128;
t0=v0*435;t1=v1*435;t2=v2*435;t3=v3*435;
t2+=v0<<8;t3+=v1<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;v3=(t3+(t2>>>16))&65535;v2=t2&65535;
v0^=(c&63)|128;
}
}
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],64);
}
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
_hash64 = _hash64_1a;
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
function _hash128_1a(str){
var i,l=str.length-3,s=fnvConstants[128].offset,t0=0,v0=s[7]|0,t1=0,v1=s[6]|0,t2=0,v2=s[5]|0,t3=0,v3=s[4]|0,t4=0,v4=s[3]|0,t5=0,v5=s[2]|0,t6=0,v6=s[1]|0,t7=0,v7=s[0]|0;
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
v0^=str.charCodeAt(i++);
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=str.charCodeAt(i++);
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=str.charCodeAt(i++);
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=str.charCodeAt(i++);
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
}
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
v0^=str.charCodeAt(i++);
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
}
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],128);
}
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
function _hash128_1(str){
var i,l=str.length-3,s=fnvConstants[128].offset,t0=0,v0=s[7]|0,t1=0,v1=s[6]|0,t2=0,v2=s[5]|0,t3=0,v3=s[4]|0,t4=0,v4=s[3]|0,t5=0,v5=s[2]|0,t6=0,v6=s[1]|0,t7=0,v7=s[0]|0;
2020-10-22 15:52:18 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=str.charCodeAt(i++);
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=str.charCodeAt(i++);
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=str.charCodeAt(i++);
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=str.charCodeAt(i++);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=str.charCodeAt(i++);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],128);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash128_1a_utf(str){
var c,i,l=str.length,s=fnvConstants[128].offset,t0=0,v0=s[7]|0,t1=0,v1=s[6]|0,t2=0,v2=s[5]|0,t3=0,v3=s[4]|0,t4=0,v4=s[3]|0,t5=0,v5=s[2]|0,t6=0,v6=s[1]|0,t7=0,v7=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=((c>>12)&63)|128;
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=((c>>6)&63)|128;
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=((c>>6)&63)|128;
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=(c&63)|128;
}
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],128);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash128_1_utf(str){
var c,i,l=str.length,s=fnvConstants[128].offset,t0=0,v0=s[7]|0,t1=0,v1=s[6]|0,t2=0,v2=s[5]|0,t3=0,v3=s[4]|0,t4=0,v4=s[3]|0,t5=0,v5=s[2]|0,t6=0,v6=s[1]|0,t7=0,v7=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=((c>>12)&63)|128;
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=((c>>6)&63)|128;
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=((c>>6)&63)|128;
t0=v0*315;t1=v1*315;t2=v2*315;t3=v3*315;t4=v4*315;t5=v5*315;t6=v6*315;t7=v7*315;
t5+=v0<<8;t6+=v1<<8;t7+=v2<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;v7=(t7+(t6>>>16))&65535;v6=t6&65535;
v0^=(c&63)|128;
}
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],128);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
_hash128 = _hash128_1a;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash256_1a(str){
var i,l=str.length-3,s=fnvConstants[256].offset,t0=0,v0=s[15]|0,t1=0,v1=s[14]|0,t2=0,v2=s[13]|0,t3=0,v3=s[12]|0,t4=0,v4=s[11]|0,t5=0,v5=s[10]|0,t6=0,v6=s[9]|0,t7=0,v7=s[8]|0,t8=0,v8=s[7]|0,t9=0,v9=s[6]|0,t10=0,v10=s[5]|0,t11=0,v11=s[4]|0,t12=0,v12=s[3]|0,t13=0,v13=s[2]|0,t14=0,v14=s[1]|0,t15=0,v15=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
v0^=str.charCodeAt(i++);
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=str.charCodeAt(i++);
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=str.charCodeAt(i++);
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=str.charCodeAt(i++);
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
v0^=str.charCodeAt(i++);
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v15>>8]+hl[v15&255]+hl[v14>>8]+hl[v14&255]+hl[v13>>8]+hl[v13&255]+hl[v12>>8]+hl[v12&255]+hl[v11>>8]+hl[v11&255]+hl[v10>>8]+hl[v10&255]+hl[v9>>8]+hl[v9&255]+hl[v8>>8]+hl[v8&255]+hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],256);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash256_1(str){
var i,l=str.length-3,s=fnvConstants[256].offset,t0=0,v0=s[15]|0,t1=0,v1=s[14]|0,t2=0,v2=s[13]|0,t3=0,v3=s[12]|0,t4=0,v4=s[11]|0,t5=0,v5=s[10]|0,t6=0,v6=s[9]|0,t7=0,v7=s[8]|0,t8=0,v8=s[7]|0,t9=0,v9=s[6]|0,t10=0,v10=s[5]|0,t11=0,v11=s[4]|0,t12=0,v12=s[3]|0,t13=0,v13=s[2]|0,t14=0,v14=s[1]|0,t15=0,v15=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=str.charCodeAt(i++);
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=str.charCodeAt(i++);
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=str.charCodeAt(i++);
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=str.charCodeAt(i++);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=str.charCodeAt(i++);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v15>>8]+hl[v15&255]+hl[v14>>8]+hl[v14&255]+hl[v13>>8]+hl[v13&255]+hl[v12>>8]+hl[v12&255]+hl[v11>>8]+hl[v11&255]+hl[v10>>8]+hl[v10&255]+hl[v9>>8]+hl[v9&255]+hl[v8>>8]+hl[v8&255]+hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],256);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash256_1a_utf(str){
var c,i,l=str.length,s=fnvConstants[256].offset,t0=0,v0=s[15]|0,t1=0,v1=s[14]|0,t2=0,v2=s[13]|0,t3=0,v3=s[12]|0,t4=0,v4=s[11]|0,t5=0,v5=s[10]|0,t6=0,v6=s[9]|0,t7=0,v7=s[8]|0,t8=0,v8=s[7]|0,t9=0,v9=s[6]|0,t10=0,v10=s[5]|0,t11=0,v11=s[4]|0,t12=0,v12=s[3]|0,t13=0,v13=s[2]|0,t14=0,v14=s[1]|0,t15=0,v15=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=((c>>12)&63)|128;
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=((c>>6)&63)|128;
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=((c>>6)&63)|128;
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=(c&63)|128;
}
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v15>>8]+hl[v15&255]+hl[v14>>8]+hl[v14&255]+hl[v13>>8]+hl[v13&255]+hl[v12>>8]+hl[v12&255]+hl[v11>>8]+hl[v11&255]+hl[v10>>8]+hl[v10&255]+hl[v9>>8]+hl[v9&255]+hl[v8>>8]+hl[v8&255]+hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],256);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash256_1_utf(str){
var c,i,l=str.length,s=fnvConstants[256].offset,t0=0,v0=s[15]|0,t1=0,v1=s[14]|0,t2=0,v2=s[13]|0,t3=0,v3=s[12]|0,t4=0,v4=s[11]|0,t5=0,v5=s[10]|0,t6=0,v6=s[9]|0,t7=0,v7=s[8]|0,t8=0,v8=s[7]|0,t9=0,v9=s[6]|0,t10=0,v10=s[5]|0,t11=0,v11=s[4]|0,t12=0,v12=s[3]|0,t13=0,v13=s[2]|0,t14=0,v14=s[1]|0,t15=0,v15=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=((c>>12)&63)|128;
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=((c>>6)&63)|128;
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=((c>>6)&63)|128;
t0=v0*355;t1=v1*355;t2=v2*355;t3=v3*355;t4=v4*355;t5=v5*355;t6=v6*355;t7=v7*355;t8=v8*355;t9=v9*355;t10=v10*355;t11=v11*355;t12=v12*355;t13=v13*355;t14=v14*355;t15=v15*355;
t10+=v0<<8;t11+=v1<<8;t12+=v2<<8;t13+=v3<<8;t14+=v4<<8;t15+=v5<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;v15=(t15+(t14>>>16))&65535;v14=t14&65535;
v0^=(c&63)|128;
}
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v15>>8]+hl[v15&255]+hl[v14>>8]+hl[v14&255]+hl[v13>>8]+hl[v13&255]+hl[v12>>8]+hl[v12&255]+hl[v11>>8]+hl[v11&255]+hl[v10>>8]+hl[v10&255]+hl[v9>>8]+hl[v9&255]+hl[v8>>8]+hl[v8&255]+hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],256);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
_hash256 = _hash256_1a;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash512_1a(str){
var i,l=str.length-3,s=fnvConstants[512].offset,t0=0,v0=s[31]|0,t1=0,v1=s[30]|0,t2=0,v2=s[29]|0,t3=0,v3=s[28]|0,t4=0,v4=s[27]|0,t5=0,v5=s[26]|0,t6=0,v6=s[25]|0,t7=0,v7=s[24]|0,t8=0,v8=s[23]|0,t9=0,v9=s[22]|0,t10=0,v10=s[21]|0,t11=0,v11=s[20]|0,t12=0,v12=s[19]|0,t13=0,v13=s[18]|0,t14=0,v14=s[17]|0,t15=0,v15=s[16]|0,t16=0,v16=s[15]|0,t17=0,v17=s[14]|0,t18=0,v18=s[13]|0,t19=0,v19=s[12]|0,t20=0,v20=s[11]|0,t21=0,v21=s[10]|0,t22=0,v22=s[9]|0,t23=0,v23=s[8]|0,t24=0,v24=s[7]|0,t25=0,v25=s[6]|0,t26=0,v26=s[5]|0,t27=0,v27=s[4]|0,t28=0,v28=s[3]|0,t29=0,v29=s[2]|0,t30=0,v30=s[1]|0,t31=0,v31=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
v0^=str.charCodeAt(i++);
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=str.charCodeAt(i++);
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=str.charCodeAt(i++);
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=str.charCodeAt(i++);
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
v0^=str.charCodeAt(i++);
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v31>>8]+hl[v31&255]+hl[v30>>8]+hl[v30&255]+hl[v29>>8]+hl[v29&255]+hl[v28>>8]+hl[v28&255]+hl[v27>>8]+hl[v27&255]+hl[v26>>8]+hl[v26&255]+hl[v25>>8]+hl[v25&255]+hl[v24>>8]+hl[v24&255]+hl[v23>>8]+hl[v23&255]+hl[v22>>8]+hl[v22&255]+hl[v21>>8]+hl[v21&255]+hl[v20>>8]+hl[v20&255]+hl[v19>>8]+hl[v19&255]+hl[v18>>8]+hl[v18&255]+hl[v17>>8]+hl[v17&255]+hl[v16>>8]+hl[v16&255]+hl[v15>>8]+hl[v15&255]+hl[v14>>8]+hl[v14&255]+hl[v13>>8]+hl[v13&255]+hl[v12>>8]+hl[v12&255]+hl[v11>>8]+hl[v11&255]+hl[v10>>8]+hl[v10&255]+hl[v9>>8]+hl[v9&255]+hl[v8>>8]+hl[v8&255]+hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],512);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash512_1(str){
var i,l=str.length-3,s=fnvConstants[512].offset,t0=0,v0=s[31]|0,t1=0,v1=s[30]|0,t2=0,v2=s[29]|0,t3=0,v3=s[28]|0,t4=0,v4=s[27]|0,t5=0,v5=s[26]|0,t6=0,v6=s[25]|0,t7=0,v7=s[24]|0,t8=0,v8=s[23]|0,t9=0,v9=s[22]|0,t10=0,v10=s[21]|0,t11=0,v11=s[20]|0,t12=0,v12=s[19]|0,t13=0,v13=s[18]|0,t14=0,v14=s[17]|0,t15=0,v15=s[16]|0,t16=0,v16=s[15]|0,t17=0,v17=s[14]|0,t18=0,v18=s[13]|0,t19=0,v19=s[12]|0,t20=0,v20=s[11]|0,t21=0,v21=s[10]|0,t22=0,v22=s[9]|0,t23=0,v23=s[8]|0,t24=0,v24=s[7]|0,t25=0,v25=s[6]|0,t26=0,v26=s[5]|0,t27=0,v27=s[4]|0,t28=0,v28=s[3]|0,t29=0,v29=s[2]|0,t30=0,v30=s[1]|0,t31=0,v31=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=str.charCodeAt(i++);
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=str.charCodeAt(i++);
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=str.charCodeAt(i++);
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=str.charCodeAt(i++);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=str.charCodeAt(i++);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v31>>8]+hl[v31&255]+hl[v30>>8]+hl[v30&255]+hl[v29>>8]+hl[v29&255]+hl[v28>>8]+hl[v28&255]+hl[v27>>8]+hl[v27&255]+hl[v26>>8]+hl[v26&255]+hl[v25>>8]+hl[v25&255]+hl[v24>>8]+hl[v24&255]+hl[v23>>8]+hl[v23&255]+hl[v22>>8]+hl[v22&255]+hl[v21>>8]+hl[v21&255]+hl[v20>>8]+hl[v20&255]+hl[v19>>8]+hl[v19&255]+hl[v18>>8]+hl[v18&255]+hl[v17>>8]+hl[v17&255]+hl[v16>>8]+hl[v16&255]+hl[v15>>8]+hl[v15&255]+hl[v14>>8]+hl[v14&255]+hl[v13>>8]+hl[v13&255]+hl[v12>>8]+hl[v12&255]+hl[v11>>8]+hl[v11&255]+hl[v10>>8]+hl[v10&255]+hl[v9>>8]+hl[v9&255]+hl[v8>>8]+hl[v8&255]+hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],512);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash512_1a_utf(str){
var c,i,l=str.length,s=fnvConstants[512].offset,t0=0,v0=s[31]|0,t1=0,v1=s[30]|0,t2=0,v2=s[29]|0,t3=0,v3=s[28]|0,t4=0,v4=s[27]|0,t5=0,v5=s[26]|0,t6=0,v6=s[25]|0,t7=0,v7=s[24]|0,t8=0,v8=s[23]|0,t9=0,v9=s[22]|0,t10=0,v10=s[21]|0,t11=0,v11=s[20]|0,t12=0,v12=s[19]|0,t13=0,v13=s[18]|0,t14=0,v14=s[17]|0,t15=0,v15=s[16]|0,t16=0,v16=s[15]|0,t17=0,v17=s[14]|0,t18=0,v18=s[13]|0,t19=0,v19=s[12]|0,t20=0,v20=s[11]|0,t21=0,v21=s[10]|0,t22=0,v22=s[9]|0,t23=0,v23=s[8]|0,t24=0,v24=s[7]|0,t25=0,v25=s[6]|0,t26=0,v26=s[5]|0,t27=0,v27=s[4]|0,t28=0,v28=s[3]|0,t29=0,v29=s[2]|0,t30=0,v30=s[1]|0,t31=0,v31=s[0]|0;
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=((c>>12)&63)|128;
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=((c>>6)&63)|128;
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=((c>>6)&63)|128;
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=(c&63)|128;
}
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v31>>8]+hl[v31&255]+hl[v30>>8]+hl[v30&255]+hl[v29>>8]+hl[v29&255]+hl[v28>>8]+hl[v28&255]+hl[v27>>8]+hl[v27&255]+hl[v26>>8]+hl[v26&255]+hl[v25>>8]+hl[v25&255]+hl[v24>>8]+hl[v24&255]+hl[v23>>8]+hl[v23&255]+hl[v22>>8]+hl[v22&255]+hl[v21>>8]+hl[v21&255]+hl[v20>>8]+hl[v20&255]+hl[v19>>8]+hl[v19&255]+hl[v18>>8]+hl[v18&255]+hl[v17>>8]+hl[v17&255]+hl[v16>>8]+hl[v16&255]+hl[v15>>8]+hl[v15&255]+hl[v14>>8]+hl[v14&255]+hl[v13>>8]+hl[v13&255]+hl[v12>>8]+hl[v12&255]+hl[v11>>8]+hl[v11&255]+hl[v10>>8]+hl[v10&255]+hl[v9>>8]+hl[v9&255]+hl[v8>>8]+hl[v8&255]+hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],512);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash512_1_utf(str){
var c,i,l=str.length,s=fnvConstants[512].offset,t0=0,v0=s[31]|0,t1=0,v1=s[30]|0,t2=0,v2=s[29]|0,t3=0,v3=s[28]|0,t4=0,v4=s[27]|0,t5=0,v5=s[26]|0,t6=0,v6=s[25]|0,t7=0,v7=s[24]|0,t8=0,v8=s[23]|0,t9=0,v9=s[22]|0,t10=0,v10=s[21]|0,t11=0,v11=s[20]|0,t12=0,v12=s[19]|0,t13=0,v13=s[18]|0,t14=0,v14=s[17]|0,t15=0,v15=s[16]|0,t16=0,v16=s[15]|0,t17=0,v17=s[14]|0,t18=0,v18=s[13]|0,t19=0,v19=s[12]|0,t20=0,v20=s[11]|0,t21=0,v21=s[10]|0,t22=0,v22=s[9]|0,t23=0,v23=s[8]|0,t24=0,v24=s[7]|0,t25=0,v25=s[6]|0,t26=0,v26=s[5]|0,t27=0,v27=s[4]|0,t28=0,v28=s[3]|0,t29=0,v29=s[2]|0,t30=0,v30=s[1]|0,t31=0,v31=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=((c>>12)&63)|128;
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=((c>>6)&63)|128;
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=((c>>6)&63)|128;
t0=v0*343;t1=v1*343;t2=v2*343;t3=v3*343;t4=v4*343;t5=v5*343;t6=v6*343;t7=v7*343;t8=v8*343;t9=v9*343;t10=v10*343;t11=v11*343;t12=v12*343;t13=v13*343;t14=v14*343;t15=v15*343;t16=v16*343;t17=v17*343;t18=v18*343;t19=v19*343;t20=v20*343;t21=v21*343;t22=v22*343;t23=v23*343;t24=v24*343;t25=v25*343;t26=v26*343;t27=v27*343;t28=v28*343;t29=v29*343;t30=v30*343;t31=v31*343;
t21+=v0<<8;t22+=v1<<8;t23+=v2<<8;t24+=v3<<8;t25+=v4<<8;t26+=v5<<8;t27+=v6<<8;t28+=v7<<8;t29+=v8<<8;t30+=v9<<8;t31+=v10<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;v31=(t31+(t30>>>16))&65535;v30=t30&65535;
v0^=(c&63)|128;
}
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v31>>8]+hl[v31&255]+hl[v30>>8]+hl[v30&255]+hl[v29>>8]+hl[v29&255]+hl[v28>>8]+hl[v28&255]+hl[v27>>8]+hl[v27&255]+hl[v26>>8]+hl[v26&255]+hl[v25>>8]+hl[v25&255]+hl[v24>>8]+hl[v24&255]+hl[v23>>8]+hl[v23&255]+hl[v22>>8]+hl[v22&255]+hl[v21>>8]+hl[v21&255]+hl[v20>>8]+hl[v20&255]+hl[v19>>8]+hl[v19&255]+hl[v18>>8]+hl[v18&255]+hl[v17>>8]+hl[v17&255]+hl[v16>>8]+hl[v16&255]+hl[v15>>8]+hl[v15&255]+hl[v14>>8]+hl[v14&255]+hl[v13>>8]+hl[v13&255]+hl[v12>>8]+hl[v12&255]+hl[v11>>8]+hl[v11&255]+hl[v10>>8]+hl[v10&255]+hl[v9>>8]+hl[v9&255]+hl[v8>>8]+hl[v8&255]+hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],512);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
_hash512 = _hash512_1a;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash1024_1a(str){
var i,l=str.length-3,s=fnvConstants[1024].offset,t0=0,v0=s[63]|0,t1=0,v1=s[62]|0,t2=0,v2=s[61]|0,t3=0,v3=s[60]|0,t4=0,v4=s[59]|0,t5=0,v5=s[58]|0,t6=0,v6=s[57]|0,t7=0,v7=s[56]|0,t8=0,v8=s[55]|0,t9=0,v9=s[54]|0,t10=0,v10=s[53]|0,t11=0,v11=s[52]|0,t12=0,v12=s[51]|0,t13=0,v13=s[50]|0,t14=0,v14=s[49]|0,t15=0,v15=s[48]|0,t16=0,v16=s[47]|0,t17=0,v17=s[46]|0,t18=0,v18=s[45]|0,t19=0,v19=s[44]|0,t20=0,v20=s[43]|0,t21=0,v21=s[42]|0,t22=0,v22=s[41]|0,t23=0,v23=s[40]|0,t24=0,v24=s[39]|0,t25=0,v25=s[38]|0,t26=0,v26=s[37]|0,t27=0,v27=s[36]|0,t28=0,v28=s[35]|0,t29=0,v29=s[34]|0,t30=0,v30=s[33]|0,t31=0,v31=s[32]|0,t32=0,v32=s[31]|0,t33=0,v33=s[30]|0,t34=0,v34=s[29]|0,t35=0,v35=s[28]|0,t36=0,v36=s[27]|0,t37=0,v37=s[26]|0,t38=0,v38=s[25]|0,t39=0,v39=s[24]|0,t40=0,v40=s[23]|0,t41=0,v41=s[22]|0,t42=0,v42=s[21]|0,t43=0,v43=s[20]|0,t44=0,v44=s[19]|0,t45=0,v45=s[18]|0,t46=0,v46=s[17]|0,t47=0,v47=s[16]|0,t48=0,v48=s[15]|0,t49=0,v49=s[14]|0,t50=0,v50=s[13]|0,t51=0,v51=s[12]|0,t52=0,v52=s[11]|0,t53=0,v53=s[10]|0,t54=0,v54=s[9]|0,t55=0,v55=s[8]|0,t56=0,v56=s[7]|0,t57=0,v57=s[6]|0,t58=0,v58=s[5]|0,t59=0,v59=s[4]|0,t60=0,v60=s[3]|0,t61=0,v61=s[2]|0,t62=0,v62=s[1]|0,t63=0,v63=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
v0^=str.charCodeAt(i++);
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=str.charCodeAt(i++);
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=str.charCodeAt(i++);
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=str.charCodeAt(i++);
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
v0^=str.charCodeAt(i++);
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v63>>8]+hl[v63&255]+hl[v62>>8]+hl[v62&255]+hl[v61>>8]+hl[v61&255]+hl[v60>>8]+hl[v60&255]+hl[v59>>8]+hl[v59&255]+hl[v58>>8]+hl[v58&255]+hl[v57>>8]+hl[v57&255]+hl[v56>>8]+hl[v56&255]+hl[v55>>8]+hl[v55&255]+hl[v54>>8]+hl[v54&255]+hl[v53>>8]+hl[v53&255]+hl[v52>>8]+hl[v52&255]+hl[v51>>8]+hl[v51&255]+hl[v50>>8]+hl[v50&255]+hl[v49>>8]+hl[v49&255]+hl[v48>>8]+hl[v48&255]+hl[v47>>8]+hl[v47&255]+hl[v46>>8]+hl[v46&255]+hl[v45>>8]+hl[v45&255]+hl[v44>>8]+hl[v44&255]+hl[v43>>8]+hl[v43&255]+hl[v42>>8]+hl[v42&255]+hl[v41>>8]+hl[v41&255]+hl[v40>>8]+hl[v40&255]+hl[v39>>8]+hl[v39&255]+hl[v38>>8]+hl[v38&255]+hl[v37>>8]+hl[v37&255]+hl[v36>>8]+hl[v36&255]+hl[v35>>8]+hl[v35&255]+hl[v34>>8]+hl[v34&255]+hl[v33>>8]+hl[v33&255]+hl[v32>>8]+hl[v32&255]+hl[v31>>8]+hl[v31&255]+hl[v30>>8]+hl[v30&255]+hl[v29>>8]+hl[v29&255]+hl[v28>>8]+hl[v28&255]+hl[v27>>8]+hl[v27&255]+hl[v26>>8]+hl[v26&255]+hl[v25>>8]+hl[v25&255]+hl[v24>>8]+hl[v24&255]+hl[v23>>8]+hl[v23&255]+hl[v22>>8]+hl[v22&255]+hl[v21>>8]+hl[v21&255]+hl[v20>>8]+hl[v20&255]+hl[v19>>8]+hl[v19&255]+hl[v18>>8]+hl[v18&255]+hl[v17>>8]+hl[v17&255]+hl[v16>>8]+hl[v16&255]+hl[v15>>8]+hl[v15&255]+hl[v14>>8]+hl[v14&255]+hl[v13>>8]+hl[v13&255]+hl[v12>>8]+hl[v12&255]+hl[v11>>8]+hl[v11&255]+hl[v10>>8]+hl[v10&255]+hl[v9>>8]+hl[v9&255]+hl[v8>>8]+hl[v8&255]+hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],1024);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash1024_1(str){
var i,l=str.length-3,s=fnvConstants[1024].offset,t0=0,v0=s[63]|0,t1=0,v1=s[62]|0,t2=0,v2=s[61]|0,t3=0,v3=s[60]|0,t4=0,v4=s[59]|0,t5=0,v5=s[58]|0,t6=0,v6=s[57]|0,t7=0,v7=s[56]|0,t8=0,v8=s[55]|0,t9=0,v9=s[54]|0,t10=0,v10=s[53]|0,t11=0,v11=s[52]|0,t12=0,v12=s[51]|0,t13=0,v13=s[50]|0,t14=0,v14=s[49]|0,t15=0,v15=s[48]|0,t16=0,v16=s[47]|0,t17=0,v17=s[46]|0,t18=0,v18=s[45]|0,t19=0,v19=s[44]|0,t20=0,v20=s[43]|0,t21=0,v21=s[42]|0,t22=0,v22=s[41]|0,t23=0,v23=s[40]|0,t24=0,v24=s[39]|0,t25=0,v25=s[38]|0,t26=0,v26=s[37]|0,t27=0,v27=s[36]|0,t28=0,v28=s[35]|0,t29=0,v29=s[34]|0,t30=0,v30=s[33]|0,t31=0,v31=s[32]|0,t32=0,v32=s[31]|0,t33=0,v33=s[30]|0,t34=0,v34=s[29]|0,t35=0,v35=s[28]|0,t36=0,v36=s[27]|0,t37=0,v37=s[26]|0,t38=0,v38=s[25]|0,t39=0,v39=s[24]|0,t40=0,v40=s[23]|0,t41=0,v41=s[22]|0,t42=0,v42=s[21]|0,t43=0,v43=s[20]|0,t44=0,v44=s[19]|0,t45=0,v45=s[18]|0,t46=0,v46=s[17]|0,t47=0,v47=s[16]|0,t48=0,v48=s[15]|0,t49=0,v49=s[14]|0,t50=0,v50=s[13]|0,t51=0,v51=s[12]|0,t52=0,v52=s[11]|0,t53=0,v53=s[10]|0,t54=0,v54=s[9]|0,t55=0,v55=s[8]|0,t56=0,v56=s[7]|0,t57=0,v57=s[6]|0,t58=0,v58=s[5]|0,t59=0,v59=s[4]|0,t60=0,v60=s[3]|0,t61=0,v61=s[2]|0,t62=0,v62=s[1]|0,t63=0,v63=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l;) {
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=str.charCodeAt(i++);
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=str.charCodeAt(i++);
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=str.charCodeAt(i++);
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=str.charCodeAt(i++);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
while(i<l+3){
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=str.charCodeAt(i++);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v63>>8]+hl[v63&255]+hl[v62>>8]+hl[v62&255]+hl[v61>>8]+hl[v61&255]+hl[v60>>8]+hl[v60&255]+hl[v59>>8]+hl[v59&255]+hl[v58>>8]+hl[v58&255]+hl[v57>>8]+hl[v57&255]+hl[v56>>8]+hl[v56&255]+hl[v55>>8]+hl[v55&255]+hl[v54>>8]+hl[v54&255]+hl[v53>>8]+hl[v53&255]+hl[v52>>8]+hl[v52&255]+hl[v51>>8]+hl[v51&255]+hl[v50>>8]+hl[v50&255]+hl[v49>>8]+hl[v49&255]+hl[v48>>8]+hl[v48&255]+hl[v47>>8]+hl[v47&255]+hl[v46>>8]+hl[v46&255]+hl[v45>>8]+hl[v45&255]+hl[v44>>8]+hl[v44&255]+hl[v43>>8]+hl[v43&255]+hl[v42>>8]+hl[v42&255]+hl[v41>>8]+hl[v41&255]+hl[v40>>8]+hl[v40&255]+hl[v39>>8]+hl[v39&255]+hl[v38>>8]+hl[v38&255]+hl[v37>>8]+hl[v37&255]+hl[v36>>8]+hl[v36&255]+hl[v35>>8]+hl[v35&255]+hl[v34>>8]+hl[v34&255]+hl[v33>>8]+hl[v33&255]+hl[v32>>8]+hl[v32&255]+hl[v31>>8]+hl[v31&255]+hl[v30>>8]+hl[v30&255]+hl[v29>>8]+hl[v29&255]+hl[v28>>8]+hl[v28&255]+hl[v27>>8]+hl[v27&255]+hl[v26>>8]+hl[v26&255]+hl[v25>>8]+hl[v25&255]+hl[v24>>8]+hl[v24&255]+hl[v23>>8]+hl[v23&255]+hl[v22>>8]+hl[v22&255]+hl[v21>>8]+hl[v21&255]+hl[v20>>8]+hl[v20&255]+hl[v19>>8]+hl[v19&255]+hl[v18>>8]+hl[v18&255]+hl[v17>>8]+hl[v17&255]+hl[v16>>8]+hl[v16&255]+hl[v15>>8]+hl[v15&255]+hl[v14>>8]+hl[v14&255]+hl[v13>>8]+hl[v13&255]+hl[v12>>8]+hl[v12&255]+hl[v11>>8]+hl[v11&255]+hl[v10>>8]+hl[v10&255]+hl[v9>>8]+hl[v9&255]+hl[v8>>8]+hl[v8&255]+hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],1024);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash1024_1a_utf(str){
var c,i,l=str.length,s=fnvConstants[1024].offset,t0=0,v0=s[63]|0,t1=0,v1=s[62]|0,t2=0,v2=s[61]|0,t3=0,v3=s[60]|0,t4=0,v4=s[59]|0,t5=0,v5=s[58]|0,t6=0,v6=s[57]|0,t7=0,v7=s[56]|0,t8=0,v8=s[55]|0,t9=0,v9=s[54]|0,t10=0,v10=s[53]|0,t11=0,v11=s[52]|0,t12=0,v12=s[51]|0,t13=0,v13=s[50]|0,t14=0,v14=s[49]|0,t15=0,v15=s[48]|0,t16=0,v16=s[47]|0,t17=0,v17=s[46]|0,t18=0,v18=s[45]|0,t19=0,v19=s[44]|0,t20=0,v20=s[43]|0,t21=0,v21=s[42]|0,t22=0,v22=s[41]|0,t23=0,v23=s[40]|0,t24=0,v24=s[39]|0,t25=0,v25=s[38]|0,t26=0,v26=s[37]|0,t27=0,v27=s[36]|0,t28=0,v28=s[35]|0,t29=0,v29=s[34]|0,t30=0,v30=s[33]|0,t31=0,v31=s[32]|0,t32=0,v32=s[31]|0,t33=0,v33=s[30]|0,t34=0,v34=s[29]|0,t35=0,v35=s[28]|0,t36=0,v36=s[27]|0,t37=0,v37=s[26]|0,t38=0,v38=s[25]|0,t39=0,v39=s[24]|0,t40=0,v40=s[23]|0,t41=0,v41=s[22]|0,t42=0,v42=s[21]|0,t43=0,v43=s[20]|0,t44=0,v44=s[19]|0,t45=0,v45=s[18]|0,t46=0,v46=s[17]|0,t47=0,v47=s[16]|0,t48=0,v48=s[15]|0,t49=0,v49=s[14]|0,t50=0,v50=s[13]|0,t51=0,v51=s[12]|0,t52=0,v52=s[11]|0,t53=0,v53=s[10]|0,t54=0,v54=s[9]|0,t55=0,v55=s[8]|0,t56=0,v56=s[7]|0,t57=0,v57=s[6]|0,t58=0,v58=s[5]|0,t59=0,v59=s[4]|0,t60=0,v60=s[3]|0,t61=0,v61=s[2]|0,t62=0,v62=s[1]|0,t63=0,v63=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=((c>>12)&63)|128;
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=((c>>6)&63)|128;
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=((c>>6)&63)|128;
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=(c&63)|128;
}
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v63>>8]+hl[v63&255]+hl[v62>>8]+hl[v62&255]+hl[v61>>8]+hl[v61&255]+hl[v60>>8]+hl[v60&255]+hl[v59>>8]+hl[v59&255]+hl[v58>>8]+hl[v58&255]+hl[v57>>8]+hl[v57&255]+hl[v56>>8]+hl[v56&255]+hl[v55>>8]+hl[v55&255]+hl[v54>>8]+hl[v54&255]+hl[v53>>8]+hl[v53&255]+hl[v52>>8]+hl[v52&255]+hl[v51>>8]+hl[v51&255]+hl[v50>>8]+hl[v50&255]+hl[v49>>8]+hl[v49&255]+hl[v48>>8]+hl[v48&255]+hl[v47>>8]+hl[v47&255]+hl[v46>>8]+hl[v46&255]+hl[v45>>8]+hl[v45&255]+hl[v44>>8]+hl[v44&255]+hl[v43>>8]+hl[v43&255]+hl[v42>>8]+hl[v42&255]+hl[v41>>8]+hl[v41&255]+hl[v40>>8]+hl[v40&255]+hl[v39>>8]+hl[v39&255]+hl[v38>>8]+hl[v38&255]+hl[v37>>8]+hl[v37&255]+hl[v36>>8]+hl[v36&255]+hl[v35>>8]+hl[v35&255]+hl[v34>>8]+hl[v34&255]+hl[v33>>8]+hl[v33&255]+hl[v32>>8]+hl[v32&255]+hl[v31>>8]+hl[v31&255]+hl[v30>>8]+hl[v30&255]+hl[v29>>8]+hl[v29&255]+hl[v28>>8]+hl[v28&255]+hl[v27>>8]+hl[v27&255]+hl[v26>>8]+hl[v26&255]+hl[v25>>8]+hl[v25&255]+hl[v24>>8]+hl[v24&255]+hl[v23>>8]+hl[v23&255]+hl[v22>>8]+hl[v22&255]+hl[v21>>8]+hl[v21&255]+hl[v20>>8]+hl[v20&255]+hl[v19>>8]+hl[v19&255]+hl[v18>>8]+hl[v18&255]+hl[v17>>8]+hl[v17&255]+hl[v16>>8]+hl[v16&255]+hl[v15>>8]+hl[v15&255]+hl[v14>>8]+hl[v14&255]+hl[v13>>8]+hl[v13&255]+hl[v12>>8]+hl[v12&255]+hl[v11>>8]+hl[v11&255]+hl[v10>>8]+hl[v10&255]+hl[v9>>8]+hl[v9&255]+hl[v8>>8]+hl[v8&255]+hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],1024);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function _hash1024_1_utf(str){
var c,i,l=str.length,s=fnvConstants[1024].offset,t0=0,v0=s[63]|0,t1=0,v1=s[62]|0,t2=0,v2=s[61]|0,t3=0,v3=s[60]|0,t4=0,v4=s[59]|0,t5=0,v5=s[58]|0,t6=0,v6=s[57]|0,t7=0,v7=s[56]|0,t8=0,v8=s[55]|0,t9=0,v9=s[54]|0,t10=0,v10=s[53]|0,t11=0,v11=s[52]|0,t12=0,v12=s[51]|0,t13=0,v13=s[50]|0,t14=0,v14=s[49]|0,t15=0,v15=s[48]|0,t16=0,v16=s[47]|0,t17=0,v17=s[46]|0,t18=0,v18=s[45]|0,t19=0,v19=s[44]|0,t20=0,v20=s[43]|0,t21=0,v21=s[42]|0,t22=0,v22=s[41]|0,t23=0,v23=s[40]|0,t24=0,v24=s[39]|0,t25=0,v25=s[38]|0,t26=0,v26=s[37]|0,t27=0,v27=s[36]|0,t28=0,v28=s[35]|0,t29=0,v29=s[34]|0,t30=0,v30=s[33]|0,t31=0,v31=s[32]|0,t32=0,v32=s[31]|0,t33=0,v33=s[30]|0,t34=0,v34=s[29]|0,t35=0,v35=s[28]|0,t36=0,v36=s[27]|0,t37=0,v37=s[26]|0,t38=0,v38=s[25]|0,t39=0,v39=s[24]|0,t40=0,v40=s[23]|0,t41=0,v41=s[22]|0,t42=0,v42=s[21]|0,t43=0,v43=s[20]|0,t44=0,v44=s[19]|0,t45=0,v45=s[18]|0,t46=0,v46=s[17]|0,t47=0,v47=s[16]|0,t48=0,v48=s[15]|0,t49=0,v49=s[14]|0,t50=0,v50=s[13]|0,t51=0,v51=s[12]|0,t52=0,v52=s[11]|0,t53=0,v53=s[10]|0,t54=0,v54=s[9]|0,t55=0,v55=s[8]|0,t56=0,v56=s[7]|0,t57=0,v57=s[6]|0,t58=0,v58=s[5]|0,t59=0,v59=s[4]|0,t60=0,v60=s[3]|0,t61=0,v61=s[2]|0,t62=0,v62=s[1]|0,t63=0,v63=s[0]|0;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (i = 0; i < l; i++) {
c = str.charCodeAt(i);
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
if(c < 128){
v0^=c;
}else if(c < 2048){
v0^=(c>>6)|192;
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=(c&63)|128;
}else if(((c&64512)==55296)&&(i+1)<l&&((str.charCodeAt(i+1)&64512)==56320)){
c=65536+((c&1023)<<10)+(str.charCodeAt(++i)&1023);
v0^=(c>>18)|240;
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=((c>>12)&63)|128;
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=((c>>6)&63)|128;
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=(c&63)|128;
}else {
v0^=(c>>12)|224;
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=((c>>6)&63)|128;
t0=v0*397;t1=v1*397;t2=v2*397;t3=v3*397;t4=v4*397;t5=v5*397;t6=v6*397;t7=v7*397;t8=v8*397;t9=v9*397;t10=v10*397;t11=v11*397;t12=v12*397;t13=v13*397;t14=v14*397;t15=v15*397;t16=v16*397;t17=v17*397;t18=v18*397;t19=v19*397;t20=v20*397;t21=v21*397;t22=v22*397;t23=v23*397;t24=v24*397;t25=v25*397;t26=v26*397;t27=v27*397;t28=v28*397;t29=v29*397;t30=v30*397;t31=v31*397;t32=v32*397;t33=v33*397;t34=v34*397;t35=v35*397;t36=v36*397;t37=v37*397;t38=v38*397;t39=v39*397;t40=v40*397;t41=v41*397;t42=v42*397;t43=v43*397;t44=v44*397;t45=v45*397;t46=v46*397;t47=v47*397;t48=v48*397;t49=v49*397;t50=v50*397;t51=v51*397;t52=v52*397;t53=v53*397;t54=v54*397;t55=v55*397;t56=v56*397;t57=v57*397;t58=v58*397;t59=v59*397;t60=v60*397;t61=v61*397;t62=v62*397;t63=v63*397;
t42+=v0<<8;t43+=v1<<8;t44+=v2<<8;t45+=v3<<8;t46+=v4<<8;t47+=v5<<8;t48+=v6<<8;t49+=v7<<8;t50+=v8<<8;t51+=v9<<8;t52+=v10<<8;t53+=v11<<8;t54+=v12<<8;t55+=v13<<8;t56+=v14<<8;t57+=v15<<8;t58+=v16<<8;t59+=v17<<8;t60+=v18<<8;t61+=v19<<8;t62+=v20<<8;t63+=v21<<8;
t1+=t0>>>16;v0=t0&65535;t2+=t1>>>16;v1=t1&65535;t3+=t2>>>16;v2=t2&65535;t4+=t3>>>16;v3=t3&65535;t5+=t4>>>16;v4=t4&65535;t6+=t5>>>16;v5=t5&65535;t7+=t6>>>16;v6=t6&65535;t8+=t7>>>16;v7=t7&65535;t9+=t8>>>16;v8=t8&65535;t10+=t9>>>16;v9=t9&65535;t11+=t10>>>16;v10=t10&65535;t12+=t11>>>16;v11=t11&65535;t13+=t12>>>16;v12=t12&65535;t14+=t13>>>16;v13=t13&65535;t15+=t14>>>16;v14=t14&65535;t16+=t15>>>16;v15=t15&65535;t17+=t16>>>16;v16=t16&65535;t18+=t17>>>16;v17=t17&65535;t19+=t18>>>16;v18=t18&65535;t20+=t19>>>16;v19=t19&65535;t21+=t20>>>16;v20=t20&65535;t22+=t21>>>16;v21=t21&65535;t23+=t22>>>16;v22=t22&65535;t24+=t23>>>16;v23=t23&65535;t25+=t24>>>16;v24=t24&65535;t26+=t25>>>16;v25=t25&65535;t27+=t26>>>16;v26=t26&65535;t28+=t27>>>16;v27=t27&65535;t29+=t28>>>16;v28=t28&65535;t30+=t29>>>16;v29=t29&65535;t31+=t30>>>16;v30=t30&65535;t32+=t31>>>16;v31=t31&65535;t33+=t32>>>16;v32=t32&65535;t34+=t33>>>16;v33=t33&65535;t35+=t34>>>16;v34=t34&65535;t36+=t35>>>16;v35=t35&65535;t37+=t36>>>16;v36=t36&65535;t38+=t37>>>16;v37=t37&65535;t39+=t38>>>16;v38=t38&65535;t40+=t39>>>16;v39=t39&65535;t41+=t40>>>16;v40=t40&65535;t42+=t41>>>16;v41=t41&65535;t43+=t42>>>16;v42=t42&65535;t44+=t43>>>16;v43=t43&65535;t45+=t44>>>16;v44=t44&65535;t46+=t45>>>16;v45=t45&65535;t47+=t46>>>16;v46=t46&65535;t48+=t47>>>16;v47=t47&65535;t49+=t48>>>16;v48=t48&65535;t50+=t49>>>16;v49=t49&65535;t51+=t50>>>16;v50=t50&65535;t52+=t51>>>16;v51=t51&65535;t53+=t52>>>16;v52=t52&65535;t54+=t53>>>16;v53=t53&65535;t55+=t54>>>16;v54=t54&65535;t56+=t55>>>16;v55=t55&65535;t57+=t56>>>16;v56=t56&65535;t58+=t57>>>16;v57=t57&65535;t59+=t58>>>16;v58=t58&65535;t60+=t59>>>16;v59=t59&65535;t61+=t60>>>16;v60=t60&65535;t62+=t61>>>16;v61=t61&65535;v63=(t63+(t62>>>16))&65535;v62=t62&65535;
v0^=(c&63)|128;
}
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashValHex(hl[v63>>8]+hl[v63&255]+hl[v62>>8]+hl[v62&255]+hl[v61>>8]+hl[v61&255]+hl[v60>>8]+hl[v60&255]+hl[v59>>8]+hl[v59&255]+hl[v58>>8]+hl[v58&255]+hl[v57>>8]+hl[v57&255]+hl[v56>>8]+hl[v56&255]+hl[v55>>8]+hl[v55&255]+hl[v54>>8]+hl[v54&255]+hl[v53>>8]+hl[v53&255]+hl[v52>>8]+hl[v52&255]+hl[v51>>8]+hl[v51&255]+hl[v50>>8]+hl[v50&255]+hl[v49>>8]+hl[v49&255]+hl[v48>>8]+hl[v48&255]+hl[v47>>8]+hl[v47&255]+hl[v46>>8]+hl[v46&255]+hl[v45>>8]+hl[v45&255]+hl[v44>>8]+hl[v44&255]+hl[v43>>8]+hl[v43&255]+hl[v42>>8]+hl[v42&255]+hl[v41>>8]+hl[v41&255]+hl[v40>>8]+hl[v40&255]+hl[v39>>8]+hl[v39&255]+hl[v38>>8]+hl[v38&255]+hl[v37>>8]+hl[v37&255]+hl[v36>>8]+hl[v36&255]+hl[v35>>8]+hl[v35&255]+hl[v34>>8]+hl[v34&255]+hl[v33>>8]+hl[v33&255]+hl[v32>>8]+hl[v32&255]+hl[v31>>8]+hl[v31&255]+hl[v30>>8]+hl[v30&255]+hl[v29>>8]+hl[v29&255]+hl[v28>>8]+hl[v28&255]+hl[v27>>8]+hl[v27&255]+hl[v26>>8]+hl[v26&255]+hl[v25>>8]+hl[v25&255]+hl[v24>>8]+hl[v24&255]+hl[v23>>8]+hl[v23&255]+hl[v22>>8]+hl[v22&255]+hl[v21>>8]+hl[v21&255]+hl[v20>>8]+hl[v20&255]+hl[v19>>8]+hl[v19&255]+hl[v18>>8]+hl[v18&255]+hl[v17>>8]+hl[v17&255]+hl[v16>>8]+hl[v16&255]+hl[v15>>8]+hl[v15&255]+hl[v14>>8]+hl[v14&255]+hl[v13>>8]+hl[v13&255]+hl[v12>>8]+hl[v12&255]+hl[v11>>8]+hl[v11&255]+hl[v10>>8]+hl[v10&255]+hl[v9>>8]+hl[v9&255]+hl[v8>>8]+hl[v8&255]+hl[v7>>8]+hl[v7&255]+hl[v6>>8]+hl[v6&255]+hl[v5>>8]+hl[v5&255]+hl[v4>>8]+hl[v4&255]+hl[v3>>8]+hl[v3&255]+hl[v2>>8]+hl[v2&255]+hl[v1>>8]+hl[v1&255]+hl[v0>>8]+hl[v0&255],1024);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
_hash1024 = _hash1024_1a;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Init library.
setVersion('1a');
setUTF8(false);
seed();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return {
hash: hash,
setKeyspace: setKeyspace,
version: setVersion,
useUTF8: setUTF8,
seed: seed,
fast1a32: _hash32_1a_fast,
fast1a32hex:_hash32_1a_fast_hex,
fast1a52: _hash52_1a_fast,
fast1a52hex: _hash52_1a_fast_hex,
fast1a64: _hash64_1a_fast,
fast1a32utf: _hash32_1a_fast_utf,
fast1a32hexutf:_hash32_1a_fast_hex_utf,
fast1a52utf: _hash52_1a_fast_utf,
fast1a52hexutf: _hash52_1a_fast_hex_utf,
fast1a64utf: _hash64_1a_fast_utf
};
})();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
module.exports = fnvplus;
});
2020-10-23 09:02:04 +03:00
/*
2021-01-21 21:26:22 +03:00
Copyright 2020 0KIMS association.
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
2020-10-23 09:02:04 +03:00
*/
2021-01-21 21:26:22 +03:00
function flatArray(a) {
var res = [];
fillArray(res, a);
return res;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function fillArray(res, a) {
if (Array.isArray(a)) {
for (let i=0; i<a.length; i++) {
fillArray(res, a[i]);
}
} else {
res.push(a);
}
2020-10-23 09:02:04 +03:00
}
}
2021-01-21 21:26:22 +03:00
function fnvHash(str) {
return fnvPlus.hash(str, 64).hex();
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
/* globals WebAssembly */
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function builder(code, options) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
options = options || {};
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let memorySize = 32767;
let memory;
let memoryAllocated = false;
while (!memoryAllocated){
try{
memory = new WebAssembly.Memory({initial:memorySize});
memoryAllocated = true;
} catch(err){
if(memorySize === 1){
throw err;
}
console.warn("Could not allocate " + memorySize * 1024 * 64 + " bytes. This may cause severe instability. Trying with " + memorySize * 1024 * 64 / 2 + " bytes");
memorySize = Math.floor(memorySize/2);
}
}
const wasmModule = await WebAssembly.compile(code);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let wc;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const instance = await WebAssembly.instantiate(wasmModule, {
env: {
"memory": memory
},
runtime: {
error: function(code, pstr, a,b,c,d) {
let errStr;
if (code == 7) {
errStr=p2str(pstr) + " " + wc.getFr(b).toString() + " != " + wc.getFr(c).toString() + " " +p2str(d);
} else if (code == 9) {
errStr=p2str(pstr) + " " + wc.getFr(b).toString() + " " +p2str(c);
} else if ((code == 5)&&(options.sym)) {
errStr=p2str(pstr)+ " " + options.sym.labelIdx2Name[c];
} else {
errStr=p2str(pstr)+ " " + a + " " + b + " " + c + " " + d;
}
console.log("ERROR: ", code, errStr);
throw new Error(errStr);
},
log: function(a) {
console.log(wc.getFr(a).toString());
},
logGetSignal: function(signal, pVal) {
if (options.logGetSignal) {
options.logGetSignal(signal, wc.getFr(pVal) );
}
},
logSetSignal: function(signal, pVal) {
if (options.logSetSignal) {
options.logSetSignal(signal, wc.getFr(pVal) );
}
},
logStartComponent: function(cIdx) {
if (options.logStartComponent) {
options.logStartComponent(cIdx);
}
},
logFinishComponent: function(cIdx) {
if (options.logFinishComponent) {
options.logFinishComponent(cIdx);
}
}
}
});
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const sanityCheck =
options &&
(
options.sanityCheck ||
options.logGetSignal ||
options.logSetSignal ||
options.logStartComponent ||
options.logFinishComponent
);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
wc = new WitnessCalculator(memory, instance, sanityCheck);
return wc;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function p2str(p) {
const i8 = new Uint8Array(memory.buffer);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const bytes = [];
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (let i=0; i8[p+i]>0; i++) bytes.push(i8[p+i]);
return String.fromCharCode.apply(null, bytes);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
}
class WitnessCalculator {
constructor(memory, instance, sanityCheck) {
this.memory = memory;
this.i32 = new Uint32Array(memory.buffer);
this.instance = instance;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
this.n32 = (this.instance.exports.getFrLen() >> 2) - 2;
const pRawPrime = this.instance.exports.getPRawPrime();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const arr = new Array(this.n32);
for (let i=0; i<this.n32; i++) {
arr[this.n32-1-i] = this.i32[(pRawPrime >> 2) + i];
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
this.prime = Scalar$1.fromArray(arr, 0x100000000);
this.Fr = new F1Field(this.prime);
this.mask32 = Scalar$1.fromString("FFFFFFFF", 16);
this.NVars = this.instance.exports.getNVars();
this.n64 = Math.floor((this.Fr.bitLength - 1) / 64)+1;
this.R = this.Fr.e( Scalar$1.shiftLeft(1 , this.n64*64));
this.RInv = this.Fr.inv(this.R);
this.sanityCheck = sanityCheck;
}
async _doCalculateWitness(input, sanityCheck) {
this.instance.exports.init((this.sanityCheck || sanityCheck) ? 1 : 0);
const pSigOffset = this.allocInt();
const pFr = this.allocFr();
const keys = Object.keys(input);
keys.forEach( (k) => {
const h = fnvHash(k);
const hMSB = parseInt(h.slice(0,8), 16);
const hLSB = parseInt(h.slice(8,16), 16);
try {
this.instance.exports.getSignalOffset32(pSigOffset, 0, hMSB, hLSB);
} catch (err) {
throw new Error(`Signal ${k} is not an input of the circuit.`);
}
const sigOffset = this.getInt(pSigOffset);
const fArr = flatArray(input[k]);
for (let i=0; i<fArr.length; i++) {
this.setFr(pFr, fArr[i]);
this.instance.exports.setSignal(0, 0, sigOffset + i, pFr);
}
});
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async calculateWitness(input, sanityCheck) {
const self = this;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const old0 = self.i32[0];
const w = [];
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await self._doCalculateWitness(input, sanityCheck);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (let i=0; i<self.NVars; i++) {
const pWitness = self.instance.exports.getPWitness(i);
w.push(self.getFr(pWitness));
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
self.i32[0] = old0;
return w;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
async calculateBinWitness(input, sanityCheck) {
const self = this;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const old0 = self.i32[0];
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await self._doCalculateWitness(input, sanityCheck);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const pWitnessBuffer = self.instance.exports.getWitnessBuffer();
self.i32[0] = old0;
const buff = self.memory.buffer.slice(pWitnessBuffer, pWitnessBuffer + (self.NVars * self.n64 * 8));
return new Uint8Array(buff);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
allocInt() {
const p = this.i32[0];
this.i32[0] = p+8;
return p;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
allocFr() {
const p = this.i32[0];
this.i32[0] = p+this.n32*4 + 8;
return p;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
getInt(p) {
return this.i32[p>>2];
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
setInt(p, v) {
this.i32[p>>2] = v;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
getFr(p) {
const self = this;
const idx = (p>>2);
if (self.i32[idx + 1] & 0x80000000) {
const arr = new Array(self.n32);
for (let i=0; i<self.n32; i++) {
arr[self.n32-1-i] = self.i32[idx+2+i];
}
const res = self.Fr.e(Scalar$1.fromArray(arr, 0x100000000));
if (self.i32[idx + 1] & 0x40000000) {
return fromMontgomery(res);
} else {
return res;
}
2020-10-23 09:02:04 +03:00
} else {
2021-01-21 21:26:22 +03:00
if (self.i32[idx] & 0x80000000) {
return self.Fr.e( self.i32[idx] - 0x100000000);
} else {
return self.Fr.e(self.i32[idx]);
}
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
function fromMontgomery(n) {
return self.Fr.mul(self.RInv, n);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
setFr(p, v) {
const self = this;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
v = self.Fr.e(v);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const minShort = self.Fr.neg(self.Fr.e("80000000", 16));
const maxShort = self.Fr.e("7FFFFFFF", 16);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if ( (self.Fr.geq(v, minShort))
&&(self.Fr.leq(v, maxShort)))
{
let a;
if (self.Fr.geq(v, self.Fr.zero)) {
a = Scalar$1.toNumber(v);
} else {
a = Scalar$1.toNumber( self.Fr.sub(v, minShort));
a = a - 0x80000000;
a = 0x100000000 + a;
}
self.i32[(p >> 2)] = a;
self.i32[(p >> 2) + 1] = 0;
return;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
self.i32[(p >> 2)] = 0;
self.i32[(p >> 2) + 1] = 0x80000000;
const arr = Scalar$1.toArray(v, 0x100000000);
for (let i=0; i<self.n32; i++) {
const idx = arr.length-1-i;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if ( idx >=0) {
self.i32[(p >> 2) + 2 + i] = arr[idx];
} else {
self.i32[(p >> 2) + 2 + i] = 0;
}
}
2020-10-23 09:02:04 +03:00
}
}
2021-01-21 21:26:22 +03:00
async function wtnsCalculate(input, wasmFileName, wtnsFileName, options) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const fdWasm = await readExisting$2(wasmFileName);
const wasm = await fdWasm.read(fdWasm.totalSize);
await fdWasm.close();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const wc = await builder(wasm);
const w = await wc.calculateBinWitness(input);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const fdWtns = await createBinFile(wtnsFileName, "wtns", 2, 2);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await writeBin(fdWtns, w, wc.prime);
await fdWtns.close();
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
async function groth16FullProve(input, wasmFile, zkeyFileName, logger) {
const wtns= {
type: "mem"
};
await wtnsCalculate(input, wasmFile, wtns);
return await groth16Prove(zkeyFileName, wtns, logger);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
/*
Copyright 2018 0kims association.
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
This file is part of snarkjs.
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
snarkjs is a free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
snarkjs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
You should have received a copy of the GNU General Public License along with
snarkjs. If not, see <https://www.gnu.org/licenses/>.
*/
const {unstringifyBigInts: unstringifyBigInts$3} = utils$1;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function groth16Verify(vk_verifier, publicSignals, proof, logger) {
/*
let cpub = vk_verifier.IC[0];
for (let s= 0; s< vk_verifier.nPublic; s++) {
cpub = G1.add( cpub, G1.timesScalar( vk_verifier.IC[s+1], publicSignals[s]));
}
*/
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
vk_verifier = unstringifyBigInts$3(vk_verifier);
proof = unstringifyBigInts$3(proof);
publicSignals = unstringifyBigInts$3(publicSignals);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const curve = await getCurveFromName(vk_verifier.curve);
const IC0 = curve.G1.fromObject(vk_verifier.IC[0]);
const IC = new Uint8Array(curve.G1.F.n8*2 * publicSignals.length);
const w = new Uint8Array(curve.Fr.n8 * publicSignals.length);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (let i=0; i<publicSignals.length; i++) {
const buffP = curve.G1.fromObject(vk_verifier.IC[i+1]);
IC.set(buffP, i*curve.G1.F.n8*2);
Scalar$1.toRprLE(w, curve.Fr.n8*i, publicSignals[i], curve.Fr.n8);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
let cpub = await curve.G1.multiExpAffine(IC, w);
cpub = curve.G1.add(cpub, IC0);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const pi_a = curve.G1.fromObject(proof.pi_a);
const pi_b = curve.G2.fromObject(proof.pi_b);
const pi_c = curve.G1.fromObject(proof.pi_c);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const vk_gamma_2 = curve.G2.fromObject(vk_verifier.vk_gamma_2);
const vk_delta_2 = curve.G2.fromObject(vk_verifier.vk_delta_2);
const vk_alpha_1 = curve.G1.fromObject(vk_verifier.vk_alpha_1);
const vk_beta_2 = curve.G2.fromObject(vk_verifier.vk_beta_2);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const res = await curve.pairingEq(
curve.G1.neg(pi_a) , pi_b,
cpub , vk_gamma_2,
pi_c , vk_delta_2,
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
vk_alpha_1, vk_beta_2
);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (! res) {
if (logger) logger.error("Invalid proof");
return false;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
if (logger) logger.info("OK!");
return true;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
var groth16 = /*#__PURE__*/Object.freeze({
__proto__: null,
fullProve: groth16FullProve,
prove: groth16Prove,
verify: groth16Verify
});
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function hashToG2(curve, hash) {
const hashV = new DataView(hash.buffer, hash.byteOffset, hash.byteLength);
const seed = [];
for (let i=0; i<8; i++) {
seed[i] = hashV.getUint32(i*4);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const rng = new ChaCha(seed);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const g2_sp = curve.G2.fromRng(rng);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return g2_sp;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function getG2sp(curve, persinalization, challenge, g1s, g1sx) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const h = blake2bWasm(64);
const b1 = new Uint8Array([persinalization]);
h.update(b1);
h.update(challenge);
const b3 = curve.G1.toUncompressed(g1s);
h.update( b3);
const b4 = curve.G1.toUncompressed(g1sx);
h.update( b4);
const hash =h.digest();
return hashToG2(curve, hash);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
function calculatePubKey(k, curve, personalization, challengeHash, rng ) {
k.g1_s = curve.G1.toAffine(curve.G1.fromRng(rng));
k.g1_sx = curve.G1.toAffine(curve.G1.timesFr(k.g1_s, k.prvKey));
k.g2_sp = curve.G2.toAffine(getG2sp(curve, personalization, challengeHash, k.g1_s, k.g1_sx));
k.g2_spx = curve.G2.toAffine(curve.G2.timesFr(k.g2_sp, k.prvKey));
return k;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function createPTauKey(curve, challengeHash, rng) {
const key = {
tau: {},
alpha: {},
beta: {}
};
key.tau.prvKey = curve.Fr.fromRng(rng);
key.alpha.prvKey = curve.Fr.fromRng(rng);
key.beta.prvKey = curve.Fr.fromRng(rng);
calculatePubKey(key.tau, curve, 0, challengeHash, rng);
calculatePubKey(key.alpha, curve, 1, challengeHash, rng);
calculatePubKey(key.beta, curve, 2, challengeHash, rng);
return key;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function writePTauHeader(fd, curve, power, ceremonyPower) {
// Write the header
///////////
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (! ceremonyPower) ceremonyPower = power;
await fd.writeULE32(1); // Header type
const pHeaderSize = fd.pos;
await fd.writeULE64(0); // Temporally set to 0 length
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fd.writeULE32(curve.F1.n64*8);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buff = new Uint8Array(curve.F1.n8);
Scalar$1.toRprLE(buff, 0, curve.q, curve.F1.n8);
await fd.write(buff);
await fd.writeULE32(power); // power
await fd.writeULE32(ceremonyPower); // power
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const headerSize = fd.pos - pHeaderSize - 8;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const oldPos = fd.pos;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fd.writeULE64(headerSize, pHeaderSize);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
fd.pos = oldPos;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function readPTauHeader(fd, sections) {
if (!sections[1]) throw new Error(fd.fileName + ": File has no header");
if (sections[1].length>1) throw new Error(fd.fileName +": File has more than one header");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
fd.pos = sections[1][0].p;
const n8 = await fd.readULE32();
const buff = await fd.read(n8);
const q = Scalar$1.fromRprLE(buff);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const curve = await getCurveFromQ(q);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (curve.F1.n64*8 != n8) throw new Error(fd.fileName +": Invalid size");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const power = await fd.readULE32();
const ceremonyPower = await fd.readULE32();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (fd.pos-sections[1][0].p != sections[1][0].size) throw new Error("Invalid PTau header size");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return {curve, power, ceremonyPower};
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function readPtauPubKey(fd, curve, montgomery) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buff = await fd.read(curve.F1.n8*2*6 + curve.F2.n8*2*3);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return fromPtauPubKeyRpr(buff, 0, curve, montgomery);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function fromPtauPubKeyRpr(buff, pos, curve, montgomery) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const key = {
tau: {},
alpha: {},
beta: {}
};
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
key.tau.g1_s = readG1();
key.tau.g1_sx = readG1();
key.alpha.g1_s = readG1();
key.alpha.g1_sx = readG1();
key.beta.g1_s = readG1();
key.beta.g1_sx = readG1();
key.tau.g2_spx = readG2();
key.alpha.g2_spx = readG2();
key.beta.g2_spx = readG2();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return key;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function readG1() {
let p;
if (montgomery) {
p = curve.G1.fromRprLEM( buff, pos );
} else {
p = curve.G1.fromRprUncompressed( buff, pos );
}
pos += curve.G1.F.n8*2;
return p;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
function readG2() {
let p;
if (montgomery) {
p = curve.G2.fromRprLEM( buff, pos );
} else {
p = curve.G2.fromRprUncompressed( buff, pos );
}
pos += curve.G2.F.n8*2;
return p;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function toPtauPubKeyRpr(buff, pos, curve, key, montgomery) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
writeG1(key.tau.g1_s);
writeG1(key.tau.g1_sx);
writeG1(key.alpha.g1_s);
writeG1(key.alpha.g1_sx);
writeG1(key.beta.g1_s);
writeG1(key.beta.g1_sx);
writeG2(key.tau.g2_spx);
writeG2(key.alpha.g2_spx);
writeG2(key.beta.g2_spx);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function writeG1(p) {
if (montgomery) {
curve.G1.toRprLEM(buff, pos, p);
} else {
curve.G1.toRprUncompressed(buff, pos, p);
}
pos += curve.F1.n8*2;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
async function writeG2(p) {
if (montgomery) {
curve.G2.toRprLEM(buff, pos, p);
} else {
curve.G2.toRprUncompressed(buff, pos, p);
}
pos += curve.F2.n8*2;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
return buff;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function writePtauPubKey(fd, curve, key, montgomery) {
const buff = new Uint8Array(curve.F1.n8*2*6 + curve.F2.n8*2*3);
toPtauPubKeyRpr(buff, 0, curve, key, montgomery);
await fd.write(buff);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function readContribution$1(fd, curve) {
const c = {};
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
c.tauG1 = await readG1();
c.tauG2 = await readG2();
c.alphaG1 = await readG1();
c.betaG1 = await readG1();
c.betaG2 = await readG2();
c.key = await readPtauPubKey(fd, curve, true);
c.partialHash = await fd.read(216);
c.nextChallenge = await fd.read(64);
c.type = await fd.readULE32();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buffV = new Uint8Array(curve.G1.F.n8*2*6+curve.G2.F.n8*2*3);
toPtauPubKeyRpr(buffV, 0, curve, c.key, false);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const responseHasher = blake2bWasm(64);
responseHasher.setPartialHash(c.partialHash);
responseHasher.update(buffV);
c.responseHash = responseHasher.digest();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const paramLength = await fd.readULE32();
const curPos = fd.pos;
let lastType =0;
while (fd.pos-curPos < paramLength) {
const buffType = await readDV(1);
if (buffType[0]<= lastType) throw new Error("Parameters in the contribution must be sorted");
lastType = buffType[0];
if (buffType[0]==1) { // Name
const buffLen = await readDV(1);
const buffStr = await readDV(buffLen[0]);
c.name = new TextDecoder().decode(buffStr);
} else if (buffType[0]==2) {
const buffExp = await readDV(1);
c.numIterationsExp = buffExp[0];
} else if (buffType[0]==3) {
const buffLen = await readDV(1);
c.beaconHash = await readDV(buffLen[0]);
} else {
throw new Error("Parameter not recognized");
}
}
if (fd.pos != curPos + paramLength) {
throw new Error("Parametes do not match");
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
return c;
async function readG1() {
const pBuff = await fd.read(curve.G1.F.n8*2);
return curve.G1.fromRprLEM( pBuff );
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
async function readG2() {
const pBuff = await fd.read(curve.G2.F.n8*2);
return curve.G2.fromRprLEM( pBuff );
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
async function readDV(n) {
const b = await fd.read(n);
return new Uint8Array(b);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function readContributions(fd, curve, sections) {
if (!sections[7]) throw new Error(fd.fileName + ": File has no contributions");
if (sections[7][0].length>1) throw new Error(fd.fileName +": File has more than one contributions section");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
fd.pos = sections[7][0].p;
const nContributions = await fd.readULE32();
const contributions = [];
for (let i=0; i<nContributions; i++) {
const c = await readContribution$1(fd, curve);
c.id = i+1;
contributions.push(c);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
if (fd.pos-sections[7][0].p != sections[7][0].size) throw new Error("Invalid contribution section size");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return contributions;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function writeContribution$1(fd, curve, contribution) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buffG1 = new Uint8Array(curve.F1.n8*2);
const buffG2 = new Uint8Array(curve.F2.n8*2);
await writeG1(contribution.tauG1);
await writeG2(contribution.tauG2);
await writeG1(contribution.alphaG1);
await writeG1(contribution.betaG1);
await writeG2(contribution.betaG2);
await writePtauPubKey(fd, curve, contribution.key, true);
await fd.write(contribution.partialHash);
await fd.write(contribution.nextChallenge);
await fd.writeULE32(contribution.type || 0);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const params = [];
if (contribution.name) {
params.push(1); // Param Name
const nameData = new TextEncoder("utf-8").encode(contribution.name.substring(0,64));
params.push(nameData.byteLength);
for (let i=0; i<nameData.byteLength; i++) params.push(nameData[i]);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
if (contribution.type == 1) {
params.push(2); // Param numIterationsExp
params.push(contribution.numIterationsExp);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
params.push(3); // Beacon Hash
params.push(contribution.beaconHash.byteLength);
for (let i=0; i<contribution.beaconHash.byteLength; i++) params.push(contribution.beaconHash[i]);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
if (params.length>0) {
const paramsBuff = new Uint8Array(params);
await fd.writeULE32(paramsBuff.byteLength);
await fd.write(paramsBuff);
} else {
await fd.writeULE32(0);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
async function writeG1(p) {
curve.G1.toRprLEM(buffG1, 0, p);
await fd.write(buffG1);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
async function writeG2(p) {
curve.G2.toRprLEM(buffG2, 0, p);
await fd.write(buffG2);
}
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
async function writeContributions(fd, curve, contributions) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fd.writeULE32(7); // Header type
const pContributionsSize = fd.pos;
await fd.writeULE64(0); // Temporally set to 0 length
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fd.writeULE32(contributions.length);
for (let i=0; i< contributions.length; i++) {
await writeContribution$1(fd, curve, contributions[i]);
}
const contributionsSize = fd.pos - pContributionsSize - 8;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const oldPos = fd.pos;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fd.writeULE64(contributionsSize, pContributionsSize);
fd.pos = oldPos;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function calculateFirstChallengeHash(curve, power, logger) {
if (logger) logger.debug("Calculating First Challenge Hash");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const hasher = new blake2bWasm(64);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const vG1 = new Uint8Array(curve.G1.F.n8*2);
const vG2 = new Uint8Array(curve.G2.F.n8*2);
curve.G1.toRprUncompressed(vG1, 0, curve.G1.g);
curve.G2.toRprUncompressed(vG2, 0, curve.G2.g);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
hasher.update(blake2bWasm(64).digest());
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let n;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
n=(2 ** power)*2 -1;
if (logger) logger.debug("Calculate Initial Hash: tauG1");
hashBlock(vG1, n);
n= 2 ** power;
if (logger) logger.debug("Calculate Initial Hash: tauG2");
hashBlock(vG2, n);
if (logger) logger.debug("Calculate Initial Hash: alphaTauG1");
hashBlock(vG1, n);
if (logger) logger.debug("Calculate Initial Hash: betaTauG1");
hashBlock(vG1, n);
hasher.update(vG2);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hasher.digest();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function hashBlock(buff, n) {
const blockSize = 500000;
const nBlocks = Math.floor(n / blockSize);
const rem = n % blockSize;
const bigBuff = new Uint8Array(blockSize * buff.byteLength);
for (let i=0; i<blockSize; i++) {
bigBuff.set(buff, i*buff.byteLength);
}
for (let i=0; i<nBlocks; i++) {
hasher.update(bigBuff);
if (logger) logger.debug("Initial hash: " +i*blockSize);
}
for (let i=0; i<rem; i++) {
hasher.update(buff);
}
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function keyFromBeacon(curve, challengeHash, beaconHash, numIterationsExp) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const rng = rngFromBeaconParams(beaconHash, numIterationsExp);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const key = createPTauKey(curve, challengeHash, rng);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return key;
}
/*
Header(1)
n8
prime
power
tauG1(2)
{(2 ** power)*2-1} [
G1, tau*G1, tau^2 * G1, ....
]
tauG2(3)
{2 ** power}[
G2, tau*G2, tau^2 * G2, ...
]
alphaTauG1(4)
{2 ** power}[
alpha*G1, alpha*tau*G1, alpha*tau^2*G1,....
]
betaTauG1(5)
{2 ** power} []
beta*G1, beta*tau*G1, beta*tau^2*G1, ....
]
betaG2(6)
{1}[
beta*G2
]
contributions(7)
NContributions
{NContributions}[
tau*G1
tau*G2
alpha*G1
beta*G1
beta*G2
pubKey
tau_g1s
tau_g1sx
tau_g2spx
alpha_g1s
alpha_g1sx
alpha_g1spx
beta_g1s
beta_g1sx
beta_g1spx
partialHash (216 bytes) See https://github.com/mafintosh/blake2b-wasm/blob/23bee06945806309977af802bc374727542617c7/blake2b.wat#L9
hashNewChallenge
]
*/
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function newAccumulator(curve, power, fileName, logger) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await blake2bWasm.ready();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const fd = await createBinFile(fileName, "ptau", 1, 7);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await writePTauHeader(fd, curve, power, 0);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buffG1 = curve.G1.oneAffine;
const buffG2 = curve.G2.oneAffine;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Write tauG1
///////////
await startWriteSection(fd, 2);
const nTauG1 = (2 ** power) * 2 -1;
for (let i=0; i< nTauG1; i++) {
await fd.write(buffG1);
if ((logger)&&((i%100000) == 0)&&i) logger.log("tauG1: " + i);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
await endWriteSection(fd);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Write tauG2
///////////
await startWriteSection(fd, 3);
const nTauG2 = (2 ** power);
for (let i=0; i< nTauG2; i++) {
await fd.write(buffG2);
if ((logger)&&((i%100000) == 0)&&i) logger.log("tauG2: " + i);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
await endWriteSection(fd);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Write alphaTauG1
///////////
await startWriteSection(fd, 4);
const nAlfaTauG1 = (2 ** power);
for (let i=0; i< nAlfaTauG1; i++) {
await fd.write(buffG1);
if ((logger)&&((i%100000) == 0)&&i) logger.log("alphaTauG1: " + i);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
await endWriteSection(fd);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Write betaTauG1
///////////
await startWriteSection(fd, 5);
const nBetaTauG1 = (2 ** power);
for (let i=0; i< nBetaTauG1; i++) {
await fd.write(buffG1);
if ((logger)&&((i%100000) == 0)&&i) logger.log("betaTauG1: " + i);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
await endWriteSection(fd);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Write betaG2
///////////
await startWriteSection(fd, 6);
await fd.write(buffG2);
await endWriteSection(fd);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Contributions
///////////
await startWriteSection(fd, 7);
await fd.writeULE32(0); // 0 Contributions
await endWriteSection(fd);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fd.close();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const firstChallengeHash = calculateFirstChallengeHash(curve, power, logger);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.debug(formatHash(blake2bWasm(64).digest(), "Blank Contribution Hash:"));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.info(formatHash(firstChallengeHash, "First Contribution Hash:"));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return firstChallengeHash;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
// Format of the outpu
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function exportChallenge(pTauFilename, challengeFilename, logger) {
await blake2bWasm.ready();
const {fd: fdFrom, sections} = await readBinFile(pTauFilename, "ptau", 1);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const {curve, power} = await readPTauHeader(fdFrom, sections);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const contributions = await readContributions(fdFrom, curve, sections);
let lastResponseHash, curChallengeHash;
if (contributions.length == 0) {
lastResponseHash = blake2bWasm(64).digest();
curChallengeHash = calculateFirstChallengeHash(curve, power);
} else {
lastResponseHash = contributions[contributions.length-1].responseHash;
curChallengeHash = contributions[contributions.length-1].nextChallenge;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
if (logger) logger.info(formatHash(lastResponseHash, "Last Response Hash: "));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.info(formatHash(curChallengeHash, "New Challenge Hash: "));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const fdTo = await createOverride(challengeFilename);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const toHash = blake2bWasm(64);
await fdTo.write(lastResponseHash);
toHash.update(lastResponseHash);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await exportSection(2, "G1", (2 ** power) * 2 -1, "tauG1");
await exportSection(3, "G2", (2 ** power) , "tauG2");
await exportSection(4, "G1", (2 ** power) , "alphaTauG1");
await exportSection(5, "G1", (2 ** power) , "betaTauG1");
await exportSection(6, "G2", 1 , "betaG2");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fdFrom.close();
await fdTo.close();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const calcCurChallengeHash = toHash.digest();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (!hashIsEqual (curChallengeHash, calcCurChallengeHash)) {
if (logger) logger.info(formatHash(calcCurChallengeHash, "Calc Curret Challenge Hash: "));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.error("PTau file is corrupted. Calculated new challenge hash does not match with the eclared one");
throw new Error("PTau file is corrupted. Calculated new challenge hash does not match with the eclared one");
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
return curChallengeHash;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function exportSection(sectionId, groupName, nPoints, sectionName) {
const G = curve[groupName];
const sG = G.F.n8*2;
const nPointsChunk = Math.floor((1<<24)/sG);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await startReadUniqueSection(fdFrom, sections, sectionId);
for (let i=0; i< nPoints; i+= nPointsChunk) {
if (logger) logger.debug(`Exporting ${sectionName}: ${i}/${nPoints}`);
const n = Math.min(nPoints-i, nPointsChunk);
let buff;
buff = await fdFrom.read(n*sG);
buff = await G.batchLEMtoU(buff);
await fdTo.write(buff);
toHash.update(buff);
}
await endReadSection(fdFrom);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function importResponse(oldPtauFilename, contributionFilename, newPTauFilename, name, importPoints, logger) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await blake2bWasm.ready();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const noHash = new Uint8Array(64);
for (let i=0; i<64; i++) noHash[i] = 0xFF;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const {fd: fdOld, sections} = await readBinFile(oldPtauFilename, "ptau", 1);
const {curve, power} = await readPTauHeader(fdOld, sections);
const contributions = await readContributions(fdOld, curve, sections);
const currentContribution = {};
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (name) currentContribution.name = name;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const sG1 = curve.F1.n8*2;
const scG1 = curve.F1.n8; // Compresed size
const sG2 = curve.F2.n8*2;
const scG2 = curve.F2.n8; // Compresed size
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const fdResponse = await readExisting$2(contributionFilename);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (fdResponse.totalSize !=
64 + // Old Hash
((2 ** power)*2-1)*scG1 +
(2 ** power)*scG2 +
(2 ** power)*scG1 +
(2 ** power)*scG1 +
scG2 +
sG1*6 + sG2*3)
throw new Error("Size of the contribution is invalid");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let lastChallengeHash;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (contributions.length>0) {
lastChallengeHash = contributions[contributions.length-1].nextChallenge;
} else {
lastChallengeHash = calculateFirstChallengeHash(curve, power, logger);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
const fdNew = await createBinFile(newPTauFilename, "ptau", 1, importPoints ? 7: 2);
await writePTauHeader(fdNew, curve, power);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const contributionPreviousHash = await fdResponse.read(64);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (hashIsEqual(noHash,lastChallengeHash)) {
lastChallengeHash = contributionPreviousHash;
contributions[contributions.length-1].nextChallenge = lastChallengeHash;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
if(!hashIsEqual(contributionPreviousHash,lastChallengeHash))
throw new Error("Wrong contribution. this contribution is not based on the previus hash");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const hasherResponse = new blake2bWasm(64);
hasherResponse.update(contributionPreviousHash);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const startSections = [];
let res;
res = await processSection(fdResponse, fdNew, "G1", 2, (2 ** power) * 2 -1, [1], "tauG1");
currentContribution.tauG1 = res[0];
res = await processSection(fdResponse, fdNew, "G2", 3, (2 ** power) , [1], "tauG2");
currentContribution.tauG2 = res[0];
res = await processSection(fdResponse, fdNew, "G1", 4, (2 ** power) , [0], "alphaG1");
currentContribution.alphaG1 = res[0];
res = await processSection(fdResponse, fdNew, "G1", 5, (2 ** power) , [0], "betaG1");
currentContribution.betaG1 = res[0];
res = await processSection(fdResponse, fdNew, "G2", 6, 1 , [0], "betaG2");
currentContribution.betaG2 = res[0];
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
currentContribution.partialHash = hasherResponse.getPartialHash();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buffKey = await fdResponse.read(curve.F1.n8*2*6+curve.F2.n8*2*3);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
currentContribution.key = fromPtauPubKeyRpr(buffKey, 0, curve, false);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
hasherResponse.update(new Uint8Array(buffKey));
const hashResponse = hasherResponse.digest();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.info(formatHash(hashResponse, "Contribution Response Hash imported: "));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (importPoints) {
const nextChallengeHasher = new blake2bWasm(64);
nextChallengeHasher.update(hashResponse);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await hashSection(nextChallengeHasher, fdNew, "G1", 2, (2 ** power) * 2 -1, "tauG1", logger);
await hashSection(nextChallengeHasher, fdNew, "G2", 3, (2 ** power) , "tauG2", logger);
await hashSection(nextChallengeHasher, fdNew, "G1", 4, (2 ** power) , "alphaTauG1", logger);
await hashSection(nextChallengeHasher, fdNew, "G1", 5, (2 ** power) , "betaTauG1", logger);
await hashSection(nextChallengeHasher, fdNew, "G2", 6, 1 , "betaG2", logger);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
currentContribution.nextChallenge = nextChallengeHasher.digest();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.info(formatHash(currentContribution.nextChallenge, "Next Challenge Hash: "));
} else {
currentContribution.nextChallenge = noHash;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
contributions.push(currentContribution);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await writeContributions(fdNew, curve, contributions);
await fdResponse.close();
await fdNew.close();
await fdOld.close();
return currentContribution.nextChallenge;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function processSection(fdFrom, fdTo, groupName, sectionId, nPoints, singularPointIndexes, sectionName) {
if (importPoints) {
return await processSectionImportPoints(fdFrom, fdTo, groupName, sectionId, nPoints, singularPointIndexes, sectionName);
2020-10-23 09:02:04 +03:00
} else {
2021-01-21 21:26:22 +03:00
return await processSectionNoImportPoints(fdFrom, fdTo, groupName, sectionId, nPoints, singularPointIndexes, sectionName);
2020-10-23 09:02:04 +03:00
}
}
2021-01-21 21:26:22 +03:00
async function processSectionImportPoints(fdFrom, fdTo, groupName, sectionId, nPoints, singularPointIndexes, sectionName) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const G = curve[groupName];
const scG = G.F.n8;
const sG = G.F.n8*2;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const singularPoints = [];
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await startWriteSection(fdTo, sectionId);
const nPointsChunk = Math.floor((1<<24)/sG);
startSections[sectionId] = fdTo.pos;
for (let i=0; i< nPoints; i += nPointsChunk) {
if (logger) logger.debug(`Importing ${sectionName}: ${i}/${nPoints}`);
const n = Math.min(nPoints-i, nPointsChunk);
const buffC = await fdFrom.read(n * scG);
hasherResponse.update(buffC);
const buffLEM = await G.batchCtoLEM(buffC);
await fdTo.write(buffLEM);
for (let j=0; j<singularPointIndexes.length; j++) {
const sp = singularPointIndexes[j];
if ((sp >=i) && (sp < i+n)) {
const P = G.fromRprLEM(buffLEM, (sp-i)*sG);
singularPoints.push(P);
}
2020-10-23 09:02:04 +03:00
}
}
2021-01-21 21:26:22 +03:00
await endWriteSection(fdTo);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return singularPoints;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
async function processSectionNoImportPoints(fdFrom, fdTo, groupName, sectionId, nPoints, singularPointIndexes, sectionName) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const G = curve[groupName];
const scG = G.F.n8;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const singularPoints = [];
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const nPointsChunk = Math.floor((1<<24)/scG);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (let i=0; i< nPoints; i += nPointsChunk) {
if (logger) logger.debug(`Importing ${sectionName}: ${i}/${nPoints}`);
const n = Math.min(nPoints-i, nPointsChunk);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buffC = await fdFrom.read(n * scG);
hasherResponse.update(buffC);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (let j=0; j<singularPointIndexes.length; j++) {
const sp = singularPointIndexes[j];
if ((sp >=i) && (sp < i+n)) {
const P = G.fromRprCompressed(buffC, (sp-i)*scG);
singularPoints.push(P);
}
}
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
return singularPoints;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
async function hashSection(nextChallengeHasher, fdTo, groupName, sectionId, nPoints, sectionName, logger) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const G = curve[groupName];
const sG = G.F.n8*2;
const nPointsChunk = Math.floor((1<<24)/sG);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const oldPos = fdTo.pos;
fdTo.pos = startSections[sectionId];
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (let i=0; i< nPoints; i += nPointsChunk) {
if (logger) logger.debug(`Hashing ${sectionName}: ${i}/${nPoints}`);
const n = Math.min(nPoints-i, nPointsChunk);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buffLEM = await fdTo.read(n * sG);
const buffU = await G.batchLEMtoU(buffLEM);
nextChallengeHasher.update(buffU);
}
fdTo.pos = oldPos;
}
}
const sameRatio$1 = sameRatio;
async function verifyContribution(curve, cur, prev, logger) {
let sr;
if (cur.type == 1) { // Verify the beacon.
const beaconKey = keyFromBeacon(curve, prev.nextChallenge, cur.beaconHash, cur.numIterationsExp);
if (!curve.G1.eq(cur.key.tau.g1_s, beaconKey.tau.g1_s)) {
if (logger) logger.error(`BEACON key (tauG1_s) is not generated correctly in challenge #${cur.id} ${cur.name || ""}` );
return false;
}
if (!curve.G1.eq(cur.key.tau.g1_sx, beaconKey.tau.g1_sx)) {
if (logger) logger.error(`BEACON key (tauG1_sx) is not generated correctly in challenge #${cur.id} ${cur.name || ""}` );
return false;
}
if (!curve.G2.eq(cur.key.tau.g2_spx, beaconKey.tau.g2_spx)) {
if (logger) logger.error(`BEACON key (tauG2_spx) is not generated correctly in challenge #${cur.id} ${cur.name || ""}` );
return false;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
if (!curve.G1.eq(cur.key.alpha.g1_s, beaconKey.alpha.g1_s)) {
if (logger) logger.error(`BEACON key (alphaG1_s) is not generated correctly in challenge #${cur.id} ${cur.name || ""}` );
return false;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
if (!curve.G1.eq(cur.key.alpha.g1_sx, beaconKey.alpha.g1_sx)) {
if (logger) logger.error(`BEACON key (alphaG1_sx) is not generated correctly in challenge #${cur.id} ${cur.name || ""}` );
return false;
}
if (!curve.G2.eq(cur.key.alpha.g2_spx, beaconKey.alpha.g2_spx)) {
if (logger) logger.error(`BEACON key (alphaG2_spx) is not generated correctly in challenge #${cur.id} ${cur.name || ""}` );
return false;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
if (!curve.G1.eq(cur.key.beta.g1_s, beaconKey.beta.g1_s)) {
if (logger) logger.error(`BEACON key (betaG1_s) is not generated correctly in challenge #${cur.id} ${cur.name || ""}` );
return false;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
if (!curve.G1.eq(cur.key.beta.g1_sx, beaconKey.beta.g1_sx)) {
if (logger) logger.error(`BEACON key (betaG1_sx) is not generated correctly in challenge #${cur.id} ${cur.name || ""}` );
return false;
}
if (!curve.G2.eq(cur.key.beta.g2_spx, beaconKey.beta.g2_spx)) {
if (logger) logger.error(`BEACON key (betaG2_spx) is not generated correctly in challenge #${cur.id} ${cur.name || ""}` );
return false;
2020-10-23 09:02:04 +03:00
}
}
2021-01-21 21:26:22 +03:00
cur.key.tau.g2_sp = curve.G2.toAffine(getG2sp(curve, 0, prev.nextChallenge, cur.key.tau.g1_s, cur.key.tau.g1_sx));
cur.key.alpha.g2_sp = curve.G2.toAffine(getG2sp(curve, 1, prev.nextChallenge, cur.key.alpha.g1_s, cur.key.alpha.g1_sx));
cur.key.beta.g2_sp = curve.G2.toAffine(getG2sp(curve, 2, prev.nextChallenge, cur.key.beta.g1_s, cur.key.beta.g1_sx));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
sr = await sameRatio$1(curve, cur.key.tau.g1_s, cur.key.tau.g1_sx, cur.key.tau.g2_sp, cur.key.tau.g2_spx);
if (sr !== true) {
if (logger) logger.error("INVALID key (tau) in challenge #"+cur.id);
return false;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
sr = await sameRatio$1(curve, cur.key.alpha.g1_s, cur.key.alpha.g1_sx, cur.key.alpha.g2_sp, cur.key.alpha.g2_spx);
if (sr !== true) {
if (logger) logger.error("INVALID key (alpha) in challenge #"+cur.id);
return false;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
sr = await sameRatio$1(curve, cur.key.beta.g1_s, cur.key.beta.g1_sx, cur.key.beta.g2_sp, cur.key.beta.g2_spx);
if (sr !== true) {
if (logger) logger.error("INVALID key (beta) in challenge #"+cur.id);
return false;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
sr = await sameRatio$1(curve, prev.tauG1, cur.tauG1, cur.key.tau.g2_sp, cur.key.tau.g2_spx);
if (sr !== true) {
if (logger) logger.error("INVALID tau*G1. challenge #"+cur.id+" It does not follow the previous contribution");
return false;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
sr = await sameRatio$1(curve, cur.key.tau.g1_s, cur.key.tau.g1_sx, prev.tauG2, cur.tauG2);
if (sr !== true) {
if (logger) logger.error("INVALID tau*G2. challenge #"+cur.id+" It does not follow the previous contribution");
return false;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
sr = await sameRatio$1(curve, prev.alphaG1, cur.alphaG1, cur.key.alpha.g2_sp, cur.key.alpha.g2_spx);
if (sr !== true) {
if (logger) logger.error("INVALID alpha*G1. challenge #"+cur.id+" It does not follow the previous contribution");
return false;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
sr = await sameRatio$1(curve, prev.betaG1, cur.betaG1, cur.key.beta.g2_sp, cur.key.beta.g2_spx);
if (sr !== true) {
if (logger) logger.error("INVALID beta*G1. challenge #"+cur.id+" It does not follow the previous contribution");
return false;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
sr = await sameRatio$1(curve, cur.key.beta.g1_s, cur.key.beta.g1_sx, prev.betaG2, cur.betaG2);
if (sr !== true) {
if (logger) logger.error("INVALID beta*G2. challenge #"+cur.id+"It does not follow the previous contribution");
return false;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
if (logger) logger.info("Powers Of tau file OK!");
return true;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function verify(tauFilename, logger) {
let sr;
await blake2bWasm.ready();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const {fd, sections} = await readBinFile(tauFilename, "ptau", 1);
const {curve, power, ceremonyPower} = await readPTauHeader(fd, sections);
const contrs = await readContributions(fd, curve, sections);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.debug("power: 2**" + power);
// Verify Last contribution
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.debug("Computing initial contribution hash");
const initialContribution = {
tauG1: curve.G1.g,
tauG2: curve.G2.g,
alphaG1: curve.G1.g,
betaG1: curve.G1.g,
betaG2: curve.G2.g,
nextChallenge: calculateFirstChallengeHash(curve, ceremonyPower, logger),
responseHash: blake2bWasm(64).digest()
};
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (contrs.length == 0) {
if (logger) logger.error("This file has no contribution! It cannot be used in production");
return false;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
let prevContr;
if (contrs.length>1) {
prevContr = contrs[contrs.length-2];
} else {
prevContr = initialContribution;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
const curContr = contrs[contrs.length-1];
if (logger) logger.debug("Validating contribution #"+contrs[contrs.length-1].id);
const res = await verifyContribution(curve, curContr, prevContr, logger);
if (!res) return false;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const nextContributionHasher = blake2bWasm(64);
nextContributionHasher.update(curContr.responseHash);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Verify powers and compute nextChallengeHash
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// await test();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Verify Section tau*G1
if (logger) logger.debug("Verifying powers in tau*G1 section");
const rTau1 = await processSection(2, "G1", "tauG1", (2 ** power)*2-1, [0, 1], logger);
sr = await sameRatio$1(curve, rTau1.R1, rTau1.R2, curve.G2.g, curContr.tauG2);
if (sr !== true) {
if (logger) logger.error("tauG1 section. Powers do not match");
return false;
}
if (!curve.G1.eq(curve.G1.g, rTau1.singularPoints[0])) {
if (logger) logger.error("First element of tau*G1 section must be the generator");
return false;
}
if (!curve.G1.eq(curContr.tauG1, rTau1.singularPoints[1])) {
if (logger) logger.error("Second element of tau*G1 section does not match the one in the contribution section");
return false;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// await test();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Verify Section tau*G2
if (logger) logger.debug("Verifying powers in tau*G2 section");
const rTau2 = await processSection(3, "G2", "tauG2", 2 ** power, [0, 1], logger);
sr = await sameRatio$1(curve, curve.G1.g, curContr.tauG1, rTau2.R1, rTau2.R2);
if (sr !== true) {
if (logger) logger.error("tauG2 section. Powers do not match");
return false;
}
if (!curve.G2.eq(curve.G2.g, rTau2.singularPoints[0])) {
if (logger) logger.error("First element of tau*G2 section must be the generator");
return false;
}
if (!curve.G2.eq(curContr.tauG2, rTau2.singularPoints[1])) {
if (logger) logger.error("Second element of tau*G2 section does not match the one in the contribution section");
return false;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Verify Section alpha*tau*G1
if (logger) logger.debug("Verifying powers in alpha*tau*G1 section");
const rAlphaTauG1 = await processSection(4, "G1", "alphatauG1", 2 ** power, [0], logger);
sr = await sameRatio$1(curve, rAlphaTauG1.R1, rAlphaTauG1.R2, curve.G2.g, curContr.tauG2);
if (sr !== true) {
if (logger) logger.error("alphaTauG1 section. Powers do not match");
return false;
}
if (!curve.G1.eq(curContr.alphaG1, rAlphaTauG1.singularPoints[0])) {
if (logger) logger.error("First element of alpha*tau*G1 section (alpha*G1) does not match the one in the contribution section");
return false;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Verify Section beta*tau*G1
if (logger) logger.debug("Verifying powers in beta*tau*G1 section");
const rBetaTauG1 = await processSection(5, "G1", "betatauG1", 2 ** power, [0], logger);
sr = await sameRatio$1(curve, rBetaTauG1.R1, rBetaTauG1.R2, curve.G2.g, curContr.tauG2);
if (sr !== true) {
if (logger) logger.error("betaTauG1 section. Powers do not match");
return false;
}
if (!curve.G1.eq(curContr.betaG1, rBetaTauG1.singularPoints[0])) {
if (logger) logger.error("First element of beta*tau*G1 section (beta*G1) does not match the one in the contribution section");
return false;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
//Verify Beta G2
const betaG2 = await processSectionBetaG2(logger);
if (!curve.G2.eq(curContr.betaG2, betaG2)) {
if (logger) logger.error("betaG2 element in betaG2 section does not match the one in the contribution section");
return false;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const nextContributionHash = nextContributionHasher.digest();
// Check the nextChallengeHash
if (power == ceremonyPower) {
if (!hashIsEqual(nextContributionHash,curContr.nextChallenge)) {
if (logger) logger.error("Hash of the values does not match the next challenge of the last contributor in the contributions section");
return false;
2020-10-23 09:02:04 +03:00
}
}
2021-01-21 21:26:22 +03:00
if (logger) logger.info(formatHash(nextContributionHash, "Next challenge hash: "));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Verify Previous contributions
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
printContribution(curContr, prevContr);
for (let i = contrs.length-2; i>=0; i--) {
const curContr = contrs[i];
const prevContr = (i>0) ? contrs[i-1] : initialContribution;
const res = await verifyContribution(curve, curContr, prevContr, logger);
if (!res) return false;
printContribution(curContr, prevContr);
}
if (logger) logger.info("-----------------------------------------------------");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if ((!sections[12]) || (!sections[13]) || (!sections[14]) || (!sections[15])) {
if (logger) logger.warn(
"this file does not contain phase2 precalculated values. Please run: \n" +
" snarkjs \"powersoftau preparephase2\" to prepare this file to be used in the phase2 ceremony."
);
} else {
let res;
res = await verifyLagrangeEvaluations("G1", 2, 12, "tauG1", logger);
if (!res) return false;
res = await verifyLagrangeEvaluations("G2", 3, 13, "tauG2", logger);
if (!res) return false;
res = await verifyLagrangeEvaluations("G1", 4, 14, "alphaTauG1", logger);
if (!res) return false;
res = await verifyLagrangeEvaluations("G1", 5, 15, "betaTauG1", logger);
if (!res) return false;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fd.close();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.info("Powers of Tau Ok!");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return true;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function printContribution(curContr, prevContr) {
if (!logger) return;
logger.info("-----------------------------------------------------");
logger.info(`Contribution #${curContr.id}: ${curContr.name ||""}`);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
logger.info(formatHash(curContr.nextChallenge, "Next Challenge: "));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buffV = new Uint8Array(curve.G1.F.n8*2*6+curve.G2.F.n8*2*3);
toPtauPubKeyRpr(buffV, 0, curve, curContr.key, false);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const responseHasher = blake2bWasm(64);
responseHasher.setPartialHash(curContr.partialHash);
responseHasher.update(buffV);
const responseHash = responseHasher.digest();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
logger.info(formatHash(responseHash, "Response Hash:"));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
logger.info(formatHash(prevContr.nextChallenge, "Response Hash:"));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (curContr.type == 1) {
logger.info(`Beacon generator: ${byteArray2hex(curContr.beaconHash)}`);
logger.info(`Beacon iterations Exp: ${curContr.numIterationsExp}`);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function processSectionBetaG2(logger) {
const G = curve.G2;
const sG = G.F.n8*2;
const buffUv = new Uint8Array(sG);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (!sections[6]) {
logger.error("File has no BetaG2 section");
throw new Error("File has no BetaG2 section");
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
if (sections[6].length>1) {
logger.error("File has no BetaG2 section");
throw new Error("File has more than one GetaG2 section");
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
fd.pos = sections[6][0].p;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buff = await fd.read(sG);
const P = G.fromRprLEM(buff);
G.toRprUncompressed(buffUv, 0, P);
nextContributionHasher.update(buffUv);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return P;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
async function processSection(idSection, groupName, sectionName, nPoints, singularPointIndexes, logger) {
const MAX_CHUNK_SIZE = 1<<16;
const G = curve[groupName];
const sG = G.F.n8*2;
await startReadUniqueSection(fd, sections, idSection);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const singularPoints = [];
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let R1 = G.zero;
let R2 = G.zero;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let lastBase = G.zero;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (let i=0; i<nPoints; i += MAX_CHUNK_SIZE) {
if (logger) logger.debug(`points relations: ${sectionName}: ${i}/${nPoints} `);
const n = Math.min(nPoints - i, MAX_CHUNK_SIZE);
const bases = await fd.read(n*sG);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const basesU = await G.batchLEMtoU(bases);
nextContributionHasher.update(basesU);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const scalars = new Uint8Array(4*(n-1));
crypto.randomFillSync(scalars);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (i>0) {
const firstBase = G.fromRprLEM(bases, 0);
const r = crypto.randomBytes(4).readUInt32BE(0, true);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
R1 = G.add(R1, G.timesScalar(lastBase, r));
R2 = G.add(R2, G.timesScalar(firstBase, r));
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
const r1 = await G.multiExpAffine(bases.slice(0, (n-1)*sG), scalars);
const r2 = await G.multiExpAffine(bases.slice(sG), scalars);
R1 = G.add(R1, r1);
R2 = G.add(R2, r2);
lastBase = G.fromRprLEM( bases, (n-1)*sG);
for (let j=0; j<singularPointIndexes.length; j++) {
const sp = singularPointIndexes[j];
if ((sp >=i) && (sp < i+n)) {
const P = G.fromRprLEM(bases, (sp-i)*sG);
singularPoints.push(P);
}
}
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
await endReadSection(fd);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return {
R1: R1,
R2: R2,
singularPoints: singularPoints
};
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
async function verifyLagrangeEvaluations(gName, tauSection, lagrangeSection, sectionName, logger) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.debug(`Verifying phase2 calculated values ${sectionName}...`);
const G = curve[gName];
const sG = G.F.n8*2;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const seed= new Array(8);
for (let i=0; i<8; i++) {
seed[i] = crypto.randomBytes(4).readUInt32BE(0, true);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (let p=0; p<= power; p ++) {
const res = await verifyPower(p);
if (!res) return false;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
if (tauSection == 2) {
const res = await verifyPower(power+1);
if (!res) return false;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return true;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function verifyPower(p) {
if (logger) logger.debug(`Power ${p}...`);
const n8r = curve.Fr.n8;
const nPoints = 2 ** p;
let buff_r = new Uint32Array(nPoints);
let buffG;
let rng = new ChaCha(seed);
if (logger) logger.debug(`Creating random numbers Powers${p}...`);
for (let i=0; i<nPoints; i++) {
if ((p == power+1)&&(i == nPoints-1)) {
buff_r[i] = 0;
} else {
buff_r[i] = rng.nextU32();
}
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
buff_r = new Uint8Array(buff_r.buffer, buff_r.byteOffset, buff_r.byteLength);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.debug(`reading points Powers${p}...`);
await startReadUniqueSection(fd, sections, tauSection);
buffG = new BigBuffer(nPoints*sG);
if (p == power+1) {
await fd.readToBuffer(buffG, 0, (nPoints-1)*sG);
buffG.set(curve.G1.zeroAffine, (nPoints-1)*sG);
} else {
await fd.readToBuffer(buffG, 0, nPoints*sG);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
await endReadSection(fd, true);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const resTau = await G.multiExpAffine(buffG, buff_r, logger, sectionName + "_" + p);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
buff_r = new BigBuffer(nPoints * n8r);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
rng = new ChaCha(seed);
2021-01-17 21:06:51 +03:00
2021-01-21 21:26:22 +03:00
const buff4 = new Uint8Array(4);
const buff4V = new DataView(buff4.buffer);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.debug(`Creating random numbers Powers${p}...`);
for (let i=0; i<nPoints; i++) {
if ((i != nPoints-1) || (p != power+1)) {
buff4V.setUint32(0, rng.nextU32(), true);
buff_r.set(buff4, i*n8r);
}
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.debug(`batchToMontgomery ${p}...`);
buff_r = await curve.Fr.batchToMontgomery(buff_r);
if (logger) logger.debug(`fft ${p}...`);
buff_r = await curve.Fr.fft(buff_r);
if (logger) logger.debug(`batchFromMontgomery ${p}...`);
buff_r = await curve.Fr.batchFromMontgomery(buff_r);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.debug(`reading points Lagrange${p}...`);
await startReadUniqueSection(fd, sections, lagrangeSection);
fd.pos += sG*((2 ** p)-1);
await fd.readToBuffer(buffG, 0, nPoints*sG);
await endReadSection(fd, true);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const resLagrange = await G.multiExpAffine(buffG, buff_r, logger, sectionName + "_" + p + "_transformed");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (!G.eq(resTau, resLagrange)) {
if (logger) logger.error("Phase2 caclutation does not match with powers of tau");
return false;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return true;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
}
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
/*
This function creates a new section in the fdTo file with id idSection.
It multiplies the pooints in fdFrom by first, first*inc, first*inc^2, ....
nPoint Times.
It also updates the newChallengeHasher with the new points
*/
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function applyKeyToSection(fdOld, sections, fdNew, idSection, curve, groupName, first, inc, sectionName, logger) {
const MAX_CHUNK_SIZE = 1 << 16;
const G = curve[groupName];
const sG = G.F.n8*2;
const nPoints = sections[idSection][0].size / sG;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await startReadUniqueSection(fdOld, sections,idSection );
await startWriteSection(fdNew, idSection);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let t = first;
for (let i=0; i<nPoints; i += MAX_CHUNK_SIZE) {
if (logger) logger.debug(`Applying key: ${sectionName}: ${i}/${nPoints}`);
const n= Math.min(nPoints - i, MAX_CHUNK_SIZE);
let buff;
buff = await fdOld.read(n*sG);
buff = await G.batchApplyKey(buff, t, inc);
await fdNew.write(buff);
t = curve.Fr.mul(t, curve.Fr.exp(inc, n));
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await endWriteSection(fdNew);
await endReadSection(fdOld);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function applyKeyToChallengeSection(fdOld, fdNew, responseHasher, curve, groupName, nPoints, first, inc, formatOut, sectionName, logger) {
const G = curve[groupName];
const sG = G.F.n8*2;
const chunkSize = Math.floor((1<<20) / sG); // 128Mb chunks
let t = first;
for (let i=0 ; i<nPoints ; i+= chunkSize) {
if (logger) logger.debug(`Applying key ${sectionName}: ${i}/${nPoints}`);
const n= Math.min(nPoints-i, chunkSize );
const buffInU = await fdOld.read(n * sG);
const buffInLEM = await G.batchUtoLEM(buffInU);
const buffOutLEM = await G.batchApplyKey(buffInLEM, t, inc);
let buffOut;
if (formatOut == "COMPRESSED") {
buffOut = await G.batchLEMtoC(buffOutLEM);
} else {
buffOut = await G.batchLEMtoU(buffOutLEM);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (responseHasher) responseHasher.update(buffOut);
await fdNew.write(buffOut);
t = curve.Fr.mul(t, curve.Fr.exp(inc, n));
}
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Format of the output
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function challengeContribute(curve, challengeFilename, responesFileName, entropy, logger) {
await blake2bWasm.ready();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const fdFrom = await readExisting$2(challengeFilename);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const sG1 = curve.F1.n64*8*2;
const sG2 = curve.F2.n64*8*2;
const domainSize = (fdFrom.totalSize + sG1 - 64 - sG2) / (4*sG1 + sG2);
let e = domainSize;
let power = 0;
while (e>1) {
e = e /2;
power += 1;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (2 ** power != domainSize) throw new Error("Invalid file size");
if (logger) logger.debug("Power to tau size: "+power);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const rng = await getRandomRng(entropy);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const fdTo = await createOverride(responesFileName);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Calculate the hash
const challengeHasher = blake2bWasm(64);
for (let i=0; i<fdFrom.totalSize; i+= fdFrom.pageSize) {
if (logger) logger.debug(`Hashing challenge ${i}/${fdFrom.totalSize}`);
const s = Math.min(fdFrom.totalSize - i, fdFrom.pageSize);
const buff = await fdFrom.read(s);
challengeHasher.update(buff);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const claimedHash = await fdFrom.read(64, 0);
if (logger) logger.info(formatHash(claimedHash, "Claimed Previous Response Hash: "));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const challengeHash = challengeHasher.digest();
if (logger) logger.info(formatHash(challengeHash, "Current Challenge Hash: "));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const key = createPTauKey(curve, challengeHash, rng);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) {
["tau", "alpha", "beta"].forEach( (k) => {
logger.debug(k + ".g1_s: " + curve.G1.toString(key[k].g1_s, 16));
logger.debug(k + ".g1_sx: " + curve.G1.toString(key[k].g1_sx, 16));
logger.debug(k + ".g2_sp: " + curve.G2.toString(key[k].g2_sp, 16));
logger.debug(k + ".g2_spx: " + curve.G2.toString(key[k].g2_spx, 16));
logger.debug("");
});
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const responseHasher = blake2bWasm(64);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fdTo.write(challengeHash);
responseHasher.update(challengeHash);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await applyKeyToChallengeSection(fdFrom, fdTo, responseHasher, curve, "G1", (2 ** power)*2-1, curve.Fr.one , key.tau.prvKey, "COMPRESSED", "tauG1" , logger );
await applyKeyToChallengeSection(fdFrom, fdTo, responseHasher, curve, "G2", (2 ** power) , curve.Fr.one , key.tau.prvKey, "COMPRESSED", "tauG2" , logger );
await applyKeyToChallengeSection(fdFrom, fdTo, responseHasher, curve, "G1", (2 ** power) , key.alpha.prvKey, key.tau.prvKey, "COMPRESSED", "alphaTauG1", logger );
await applyKeyToChallengeSection(fdFrom, fdTo, responseHasher, curve, "G1", (2 ** power) , key.beta.prvKey , key.tau.prvKey, "COMPRESSED", "betaTauG1" , logger );
await applyKeyToChallengeSection(fdFrom, fdTo, responseHasher, curve, "G2", 1 , key.beta.prvKey , key.tau.prvKey, "COMPRESSED", "betaTauG2" , logger );
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Write and hash key
const buffKey = new Uint8Array(curve.F1.n8*2*6+curve.F2.n8*2*3);
toPtauPubKeyRpr(buffKey, 0, curve, key, false);
await fdTo.write(buffKey);
responseHasher.update(buffKey);
const responseHash = responseHasher.digest();
if (logger) logger.info(formatHash(responseHash, "Contribution Response Hash: "));
await fdTo.close();
await fdFrom.close();
}
async function beacon(oldPtauFilename, newPTauFilename, name, beaconHashStr,numIterationsExp, logger) {
const beaconHash = hex2ByteArray(beaconHashStr);
if ( (beaconHash.byteLength == 0)
|| (beaconHash.byteLength*2 !=beaconHashStr.length))
{
if (logger) logger.error("Invalid Beacon Hash. (It must be a valid hexadecimal sequence)");
return false;
}
if (beaconHash.length>=256) {
if (logger) logger.error("Maximum lenght of beacon hash is 255 bytes");
return false;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
numIterationsExp = parseInt(numIterationsExp);
if ((numIterationsExp<10)||(numIterationsExp>63)) {
if (logger) logger.error("Invalid numIterationsExp. (Must be between 10 and 63)");
return false;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await blake2bWasm.ready();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const {fd: fdOld, sections} = await readBinFile(oldPtauFilename, "ptau", 1);
const {curve, power, ceremonyPower} = await readPTauHeader(fdOld, sections);
if (power != ceremonyPower) {
if (logger) logger.error("This file has been reduced. You cannot contribute into a reduced file.");
return false;
}
if (sections[12]) {
if (logger) logger.warn("Contributing into a file that has phase2 calculated. You will have to prepare phase2 again.");
}
const contributions = await readContributions(fdOld, curve, sections);
const curContribution = {
name: name,
type: 1, // Beacon
numIterationsExp: numIterationsExp,
beaconHash: beaconHash
};
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let lastChallengeHash;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (contributions.length>0) {
lastChallengeHash = contributions[contributions.length-1].nextChallenge;
} else {
lastChallengeHash = calculateFirstChallengeHash(curve, power, logger);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
curContribution.key = keyFromBeacon(curve, lastChallengeHash, beaconHash, numIterationsExp);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const responseHasher = new blake2bWasm(64);
responseHasher.update(lastChallengeHash);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const fdNew = await createBinFile(newPTauFilename, "ptau", 1, 7);
await writePTauHeader(fdNew, curve, power);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const startSections = [];
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let firstPoints;
firstPoints = await processSection(2, "G1", (2 ** power) * 2 -1, curve.Fr.e(1), curContribution.key.tau.prvKey, "tauG1", logger );
curContribution.tauG1 = firstPoints[1];
firstPoints = await processSection(3, "G2", (2 ** power) , curve.Fr.e(1), curContribution.key.tau.prvKey, "tauG2", logger );
curContribution.tauG2 = firstPoints[1];
firstPoints = await processSection(4, "G1", (2 ** power) , curContribution.key.alpha.prvKey, curContribution.key.tau.prvKey, "alphaTauG1", logger );
curContribution.alphaG1 = firstPoints[0];
firstPoints = await processSection(5, "G1", (2 ** power) , curContribution.key.beta.prvKey, curContribution.key.tau.prvKey, "betaTauG1", logger );
curContribution.betaG1 = firstPoints[0];
firstPoints = await processSection(6, "G2", 1, curContribution.key.beta.prvKey, curContribution.key.tau.prvKey, "betaTauG2", logger );
curContribution.betaG2 = firstPoints[0];
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
curContribution.partialHash = responseHasher.getPartialHash();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buffKey = new Uint8Array(curve.F1.n8*2*6+curve.F2.n8*2*3);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
toPtauPubKeyRpr(buffKey, 0, curve, curContribution.key, false);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
responseHasher.update(new Uint8Array(buffKey));
const hashResponse = responseHasher.digest();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.info(formatHash(hashResponse, "Contribution Response Hash imported: "));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const nextChallengeHasher = new blake2bWasm(64);
nextChallengeHasher.update(hashResponse);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await hashSection(fdNew, "G1", 2, (2 ** power) * 2 -1, "tauG1", logger);
await hashSection(fdNew, "G2", 3, (2 ** power) , "tauG2", logger);
await hashSection(fdNew, "G1", 4, (2 ** power) , "alphaTauG1", logger);
await hashSection(fdNew, "G1", 5, (2 ** power) , "betaTauG1", logger);
await hashSection(fdNew, "G2", 6, 1 , "betaG2", logger);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
curContribution.nextChallenge = nextChallengeHasher.digest();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.info(formatHash(curContribution.nextChallenge, "Next Challenge Hash: "));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
contributions.push(curContribution);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await writeContributions(fdNew, curve, contributions);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fdOld.close();
await fdNew.close();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashResponse;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function processSection(sectionId, groupName, NPoints, first, inc, sectionName, logger) {
const res = [];
fdOld.pos = sections[sectionId][0].p;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await startWriteSection(fdNew, sectionId);
startSections[sectionId] = fdNew.pos;
const G = curve[groupName];
const sG = G.F.n8*2;
const chunkSize = Math.floor((1<<20) / sG); // 128Mb chunks
let t = first;
for (let i=0 ; i<NPoints ; i+= chunkSize) {
if (logger) logger.debug(`applying key${sectionName}: ${i}/${NPoints}`);
const n= Math.min(NPoints-i, chunkSize );
const buffIn = await fdOld.read(n * sG);
const buffOutLEM = await G.batchApplyKey(buffIn, t, inc);
/* Code to test the case where we don't have the 2^m-2 component
if (sectionName== "tauG1") {
const bz = new Uint8Array(64);
buffOutLEM.set(bz, 64*((2 ** power) - 1 ));
}
*/
const promiseWrite = fdNew.write(buffOutLEM);
const buffOutC = await G.batchLEMtoC(buffOutLEM);
responseHasher.update(buffOutC);
await promiseWrite;
if (i==0) // Return the 2 first points.
for (let j=0; j<Math.min(2, NPoints); j++)
res.push(G.fromRprLEM(buffOutLEM, j*sG));
t = curve.Fr.mul(t, curve.Fr.exp(inc, n));
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await endWriteSection(fdNew);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return res;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function hashSection(fdTo, groupName, sectionId, nPoints, sectionName, logger) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const G = curve[groupName];
const sG = G.F.n8*2;
const nPointsChunk = Math.floor((1<<24)/sG);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const oldPos = fdTo.pos;
fdTo.pos = startSections[sectionId];
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (let i=0; i< nPoints; i += nPointsChunk) {
if (logger) logger.debug(`Hashing ${sectionName}: ${i}/${nPoints}`);
const n = Math.min(nPoints-i, nPointsChunk);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buffLEM = await fdTo.read(n * sG);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buffU = await G.batchLEMtoU(buffLEM);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
nextChallengeHasher.update(buffU);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
fdTo.pos = oldPos;
}
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Format of the output
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function contribute(oldPtauFilename, newPTauFilename, name, entropy, logger) {
await blake2bWasm.ready();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const {fd: fdOld, sections} = await readBinFile(oldPtauFilename, "ptau", 1);
const {curve, power, ceremonyPower} = await readPTauHeader(fdOld, sections);
if (power != ceremonyPower) {
if (logger) logger.error("This file has been reduced. You cannot contribute into a reduced file.");
throw new Error("This file has been reduced. You cannot contribute into a reduced file.");
}
if (sections[12]) {
if (logger) logger.warn("WARNING: Contributing into a file that has phase2 calculated. You will have to prepare phase2 again.");
}
const contributions = await readContributions(fdOld, curve, sections);
const curContribution = {
name: name,
type: 0, // Beacon
};
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let lastChallengeHash;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const rng = await getRandomRng(entropy);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (contributions.length>0) {
lastChallengeHash = contributions[contributions.length-1].nextChallenge;
} else {
lastChallengeHash = calculateFirstChallengeHash(curve, power, logger);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// Generate a random key
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
curContribution.key = createPTauKey(curve, lastChallengeHash, rng);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const responseHasher = new blake2bWasm(64);
responseHasher.update(lastChallengeHash);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const fdNew = await createBinFile(newPTauFilename, "ptau", 1, 7);
await writePTauHeader(fdNew, curve, power);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const startSections = [];
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let firstPoints;
firstPoints = await processSection(2, "G1", (2 ** power) * 2 -1, curve.Fr.e(1), curContribution.key.tau.prvKey, "tauG1" );
curContribution.tauG1 = firstPoints[1];
firstPoints = await processSection(3, "G2", (2 ** power) , curve.Fr.e(1), curContribution.key.tau.prvKey, "tauG2" );
curContribution.tauG2 = firstPoints[1];
firstPoints = await processSection(4, "G1", (2 ** power) , curContribution.key.alpha.prvKey, curContribution.key.tau.prvKey, "alphaTauG1" );
curContribution.alphaG1 = firstPoints[0];
firstPoints = await processSection(5, "G1", (2 ** power) , curContribution.key.beta.prvKey, curContribution.key.tau.prvKey, "betaTauG1" );
curContribution.betaG1 = firstPoints[0];
firstPoints = await processSection(6, "G2", 1, curContribution.key.beta.prvKey, curContribution.key.tau.prvKey, "betaTauG2" );
curContribution.betaG2 = firstPoints[0];
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
curContribution.partialHash = responseHasher.getPartialHash();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buffKey = new Uint8Array(curve.F1.n8*2*6+curve.F2.n8*2*3);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
toPtauPubKeyRpr(buffKey, 0, curve, curContribution.key, false);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
responseHasher.update(new Uint8Array(buffKey));
const hashResponse = responseHasher.digest();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.info(formatHash(hashResponse, "Contribution Response Hash imported: "));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const nextChallengeHasher = new blake2bWasm(64);
nextChallengeHasher.update(hashResponse);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await hashSection(fdNew, "G1", 2, (2 ** power) * 2 -1, "tauG1");
await hashSection(fdNew, "G2", 3, (2 ** power) , "tauG2");
await hashSection(fdNew, "G1", 4, (2 ** power) , "alphaTauG1");
await hashSection(fdNew, "G1", 5, (2 ** power) , "betaTauG1");
await hashSection(fdNew, "G2", 6, 1 , "betaG2");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
curContribution.nextChallenge = nextChallengeHasher.digest();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.info(formatHash(curContribution.nextChallenge, "Next Challenge Hash: "));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
contributions.push(curContribution);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await writeContributions(fdNew, curve, contributions);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fdOld.close();
await fdNew.close();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return hashResponse;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function processSection(sectionId, groupName, NPoints, first, inc, sectionName) {
const res = [];
fdOld.pos = sections[sectionId][0].p;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await startWriteSection(fdNew, sectionId);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
startSections[sectionId] = fdNew.pos;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const G = curve[groupName];
const sG = G.F.n8*2;
const chunkSize = Math.floor((1<<20) / sG); // 128Mb chunks
let t = first;
for (let i=0 ; i<NPoints ; i+= chunkSize) {
if (logger) logger.debug(`processing: ${sectionName}: ${i}/${NPoints}`);
const n= Math.min(NPoints-i, chunkSize );
const buffIn = await fdOld.read(n * sG);
const buffOutLEM = await G.batchApplyKey(buffIn, t, inc);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
/* Code to test the case where we don't have the 2^m-2 component
if (sectionName== "tauG1") {
const bz = new Uint8Array(64);
buffOutLEM.set(bz, 64*((2 ** power) - 1 ));
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
*/
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const promiseWrite = fdNew.write(buffOutLEM);
const buffOutC = await G.batchLEMtoC(buffOutLEM);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
responseHasher.update(buffOutC);
await promiseWrite;
if (i==0) // Return the 2 first points.
for (let j=0; j<Math.min(2, NPoints); j++)
res.push(G.fromRprLEM(buffOutLEM, j*sG));
t = curve.Fr.mul(t, curve.Fr.exp(inc, n));
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
await endWriteSection(fdNew);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return res;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
async function hashSection(fdTo, groupName, sectionId, nPoints, sectionName) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const G = curve[groupName];
const sG = G.F.n8*2;
const nPointsChunk = Math.floor((1<<24)/sG);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const oldPos = fdTo.pos;
fdTo.pos = startSections[sectionId];
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (let i=0; i< nPoints; i += nPointsChunk) {
if ((logger)&&i) logger.debug(`Hashing ${sectionName}: ` + i);
const n = Math.min(nPoints-i, nPointsChunk);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buffLEM = await fdTo.read(n * sG);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const buffU = await G.batchLEMtoU(buffLEM);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
nextChallengeHasher.update(buffU);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
fdTo.pos = oldPos;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function preparePhase2(oldPtauFilename, newPTauFilename, logger) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const {fd: fdOld, sections} = await readBinFile(oldPtauFilename, "ptau", 1);
const {curve, power} = await readPTauHeader(fdOld, sections);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const fdNew = await createBinFile(newPTauFilename, "ptau", 1, 11);
await writePTauHeader(fdNew, curve, power);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await copySection(fdOld, sections, fdNew, 2);
await copySection(fdOld, sections, fdNew, 3);
await copySection(fdOld, sections, fdNew, 4);
await copySection(fdOld, sections, fdNew, 5);
await copySection(fdOld, sections, fdNew, 6);
await copySection(fdOld, sections, fdNew, 7);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await processSection(2, 12, "G1", "tauG1" );
await processSection(3, 13, "G2", "tauG2" );
await processSection(4, 14, "G1", "alphaTauG1" );
await processSection(5, 15, "G1", "betaTauG1" );
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fdOld.close();
await fdNew.close();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// await fs.promises.unlink(newPTauFilename+ ".tmp");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function processSection(oldSectionId, newSectionId, Gstr, sectionName) {
if (logger) logger.debug("Starting section: "+sectionName);
await startWriteSection(fdNew, newSectionId);
for (let p=0; p<=power; p++) {
await processSectionPower(p);
}
if (oldSectionId == 2) {
await processSectionPower(power+1);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
await endWriteSection(fdNew);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function processSectionPower(p) {
const nPoints = 2 ** p;
const G = curve[Gstr];
const Fr = curve.Fr;
const sGin = G.F.n8*2;
const sGmid = G.F.n8*3;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let buff;
buff = new BigBuffer(nPoints*sGin);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await startReadUniqueSection(fdOld, sections, oldSectionId);
if ((oldSectionId == 2)&&(p==power+1)) {
await fdOld.readToBuffer(buff, 0,(nPoints-1)*sGin );
buff.set(curve.G1.zeroAffine, (nPoints-1)*sGin );
} else {
await fdOld.readToBuffer(buff, 0,nPoints*sGin );
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
await endReadSection(fdOld, true);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
buff = await G.lagrangeEvaluations(buff, "affine", "affine", logger, sectionName);
await fdNew.write(buff);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
/*
if (p <= curve.Fr.s) {
buff = await G.ifft(buff, "affine", "affine", logger, sectionName);
await fdNew.write(buff);
} else if (p == curve.Fr.s+1) {
const smallM = 1<<curve.Fr.s;
let t0 = new BigBuffer( smallM * sGmid );
let t1 = new BigBuffer( smallM * sGmid );
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const shift_to_small_m = Fr.exp(Fr.shift, smallM);
const one_over_denom = Fr.inv(Fr.sub(shift_to_small_m, Fr.one));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let sInvAcc = Fr.one;
for (let i=0; i<smallM; i++) {
const ti = buff.slice(i*sGin, (i+1)*sGin);
const tmi = buff.slice((i+smallM)*sGin, (i+smallM+1)*sGin);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
t0.set(
G.timesFr(
G.sub(
G.timesFr(ti , shift_to_small_m),
tmi
),
one_over_denom
),
i*sGmid
);
t1.set(
G.timesFr(
G.sub( tmi, ti),
Fr.mul(sInvAcc, one_over_denom)
),
i*sGmid
);
sInvAcc = Fr.mul(sInvAcc, Fr.shiftInv);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
t0 = await G.ifft(t0, "jacobian", "affine", logger, sectionName + " t0");
await fdNew.write(t0);
t0 = null;
t1 = await G.ifft(t1, "jacobian", "affine", logger, sectionName + " t0");
await fdNew.write(t1);
} else {
if (logger) logger.error("Power too big");
throw new Error("Power to big");
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
*/
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
}
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function truncate(ptauFilename, template, logger) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const {fd: fdOld, sections} = await readBinFile(ptauFilename, "ptau", 1);
const {curve, power, ceremonyPower} = await readPTauHeader(fdOld, sections);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const sG1 = curve.G1.F.n8*2;
const sG2 = curve.G2.F.n8*2;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
for (let p=1; p<power; p++) {
await generateTruncate(p);
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fdOld.close();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return true;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function generateTruncate(p) {
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let sP = p.toString();
while (sP.length<2) sP = "0" + sP;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (logger) logger.debug("Writing Power: "+sP);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const fdNew = await createBinFile(template + sP + ".ptau", "ptau", 1, 11);
await writePTauHeader(fdNew, curve, p, ceremonyPower);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await copySection(fdOld, sections, fdNew, 2, ((2 ** p)*2-1) * sG1 ); // tagG1
await copySection(fdOld, sections, fdNew, 3, (2 ** p) * sG2); // tauG2
await copySection(fdOld, sections, fdNew, 4, (2 ** p) * sG1); // alfaTauG1
await copySection(fdOld, sections, fdNew, 5, (2 ** p) * sG1); // betaTauG1
await copySection(fdOld, sections, fdNew, 6, sG2); // betaTauG2
await copySection(fdOld, sections, fdNew, 7); // contributions
await copySection(fdOld, sections, fdNew, 12, ((2 ** (p+1))*2 -1) * sG1); // L_tauG1
await copySection(fdOld, sections, fdNew, 13, ((2 ** p)*2 -1) * sG2); // L_tauG2
await copySection(fdOld, sections, fdNew, 14, ((2 ** p)*2 -1) * sG1); // L_alfaTauG1
await copySection(fdOld, sections, fdNew, 15, ((2 ** p)*2 -1) * sG1); // L_betaTauG1
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fdNew.close();
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function convert(oldPtauFilename, newPTauFilename, logger) {
const {fd: fdOld, sections} = await readBinFile(oldPtauFilename, "ptau", 1);
const {curve, power} = await readPTauHeader(fdOld, sections);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const fdNew = await createBinFile(newPTauFilename, "ptau", 1, 11);
await writePTauHeader(fdNew, curve, power);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
// const fdTmp = await fastFile.createOverride(newPTauFilename+ ".tmp");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await copySection(fdOld, sections, fdNew, 2);
await copySection(fdOld, sections, fdNew, 3);
await copySection(fdOld, sections, fdNew, 4);
await copySection(fdOld, sections, fdNew, 5);
await copySection(fdOld, sections, fdNew, 6);
await copySection(fdOld, sections, fdNew, 7);
await processSection(2, 12, "G1", "tauG1" );
await copySection(fdOld, sections, fdNew, 13);
await copySection(fdOld, sections, fdNew, 14);
await copySection(fdOld, sections, fdNew, 15);
await fdOld.close();
await fdNew.close();
// await fs.promises.unlink(newPTauFilename+ ".tmp");
return;
async function processSection(oldSectionId, newSectionId, Gstr, sectionName) {
if (logger) logger.debug("Starting section: "+sectionName);
await startWriteSection(fdNew, newSectionId);
const size = sections[newSectionId][0].size;
const chunkSize = fdOld.pageSize;
await startReadUniqueSection(fdOld, sections, newSectionId);
for (let p=0; p<size; p+=chunkSize) {
const l = Math.min(size -p, chunkSize);
const buff = await fdOld.read(l);
await fdNew.write(buff);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
await endReadSection(fdOld);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
if (oldSectionId == 2) {
await processSectionPower(power+1);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
await endWriteSection(fdNew);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function processSectionPower(p) {
const nPoints = 2 ** p;
const G = curve[Gstr];
const sGin = G.F.n8*2;
let buff;
buff = new BigBuffer(nPoints*sGin);
await startReadUniqueSection(fdOld, sections, oldSectionId);
if ((oldSectionId == 2)&&(p==power+1)) {
await fdOld.readToBuffer(buff, 0,(nPoints-1)*sGin );
buff.set(curve.G1.zeroAffine, (nPoints-1)*sGin );
2020-10-23 09:02:04 +03:00
} else {
2021-01-21 21:26:22 +03:00
await fdOld.readToBuffer(buff, 0,nPoints*sGin );
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
await endReadSection(fdOld, true);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
buff = await G.lagrangeEvaluations(buff, "affine", "affine", logger, sectionName);
await fdNew.write(buff);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
/*
if (p <= curve.Fr.s) {
buff = await G.ifft(buff, "affine", "affine", logger, sectionName);
await fdNew.write(buff);
} else if (p == curve.Fr.s+1) {
const smallM = 1<<curve.Fr.s;
let t0 = new BigBuffer( smallM * sGmid );
let t1 = new BigBuffer( smallM * sGmid );
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const shift_to_small_m = Fr.exp(Fr.shift, smallM);
const one_over_denom = Fr.inv(Fr.sub(shift_to_small_m, Fr.one));
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let sInvAcc = Fr.one;
for (let i=0; i<smallM; i++) {
if (i%10000) logger.debug(`sectionName prepare L calc: ${sectionName}, ${i}/${smallM}`);
const ti = buff.slice(i*sGin, (i+1)*sGin);
const tmi = buff.slice((i+smallM)*sGin, (i+smallM+1)*sGin);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
t0.set(
G.timesFr(
G.sub(
G.timesFr(ti , shift_to_small_m),
tmi
),
one_over_denom
),
i*sGmid
);
t1.set(
G.timesFr(
G.sub( tmi, ti),
Fr.mul(sInvAcc, one_over_denom)
),
i*sGmid
);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
sInvAcc = Fr.mul(sInvAcc, Fr.shiftInv);
}
t0 = await G.ifft(t0, "jacobian", "affine", logger, sectionName + " t0");
await fdNew.write(t0);
t0 = null;
t1 = await G.ifft(t1, "jacobian", "affine", logger, sectionName + " t1");
await fdNew.write(t1);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
} else {
if (logger) logger.error("Power too big");
throw new Error("Power to big");
}
*/
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
}
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function exportJson(pTauFilename, verbose) {
const {fd, sections} = await readBinFile(pTauFilename, "ptau", 1);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const {curve, power} = await readPTauHeader(fd, sections);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const pTau = {};
pTau.q = curve.q;
pTau.power = power;
pTau.contributions = await readContributions(fd, curve, sections);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
pTau.tauG1 = await exportSection(2, "G1", (2 ** power)*2 -1, "tauG1");
pTau.tauG2 = await exportSection(3, "G2", (2 ** power), "tauG2");
pTau.alphaTauG1 = await exportSection(4, "G1", (2 ** power), "alphaTauG1");
pTau.betaTauG1 = await exportSection(5, "G1", (2 ** power), "betaTauG1");
pTau.betaG2 = await exportSection(6, "G2", 1, "betaG2");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
pTau.lTauG1 = await exportLagrange(12, "G1", "lTauG1");
pTau.lTauG2 = await exportLagrange(13, "G2", "lTauG2");
pTau.lAlphaTauG1 = await exportLagrange(14, "G1", "lAlphaTauG2");
pTau.lBetaTauG1 = await exportLagrange(15, "G1", "lBetaTauG2");
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
await fd.close();
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return pTau;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function exportSection(sectionId, groupName, nPoints, sectionName) {
const G = curve[groupName];
const sG = G.F.n8*2;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const res = [];
await startReadUniqueSection(fd, sections, sectionId);
for (let i=0; i< nPoints; i++) {
if ((verbose)&&i&&(i%10000 == 0)) console.log(`${sectionName}: ` + i);
const buff = await fd.read(sG);
res.push(G.fromRprLEM(buff, 0));
}
await endReadSection(fd);
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
return res;
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
async function exportLagrange(sectionId, groupName, sectionName) {
const G = curve[groupName];
const sG = G.F.n8*2;
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
const res = [];
await startReadUniqueSection(fd, sections, sectionId);
for (let p=0; p<=power; p++) {
if (verbose) console.log(`${sectionName}: Power: ${p}`);
res[p] = [];
const nPoints = (2 ** p);
for (let i=0; i<nPoints; i++) {
if ((verbose)&&i&&(i%10000 == 0)) console.log(`${sectionName}: ${i}/${nPoints}`);
const buff = await fd.read(sG);
res[p].push(G.fromRprLEM(buff, 0));
}
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
await endReadSection(fd);
return res;
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
}
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
var powersoftau = /*#__PURE__*/Object.freeze({
__proto__: null,
newAccumulator: newAccumulator,
exportChallenge: exportChallenge,
importResponse: importResponse,
verify: verify,
challengeContribute: challengeContribute,
beacon: beacon,
contribute: contribute,
preparePhase2: preparePhase2,
truncate: truncate,
convert: convert,
exportJson: exportJson
});
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
function r1csPrint(r1cs, syms, logger) {
for (let i=0; i<r1cs.constraints.length; i++) {
printCostraint(r1cs.constraints[i]);
}
function printCostraint(c) {
const lc2str = (lc) => {
let S = "";
const keys = Object.keys(lc);
keys.forEach( (k) => {
let name = syms.varIdx2Name[k];
if (name == "one") name = "";
2020-10-23 09:02:04 +03:00
2021-01-21 21:26:22 +03:00
let vs = r1cs.curve.Fr.toString(lc[k]);
if (vs == "1") vs = ""; // Do not show ones
if (vs == "-1") vs = "-"; // Do not show ones
if ((S!="")&&(vs[0]!="-")) vs = "+"+vs;
if (S!="") vs = " "+vs;
S= S + vs + name;
});
return S;
};
const S = `[ ${lc2str(c[0])} ] * [ ${lc2str(c[1])} ] - [ ${lc2str(c[2])} ] = 0`;
if (logger) logger.info(S);
2020-10-23 09:02:04 +03:00
}
2021-01-21 21:26:22 +03:00
2020-10-23 09:02:04 +03:00
}
const SUBARRAY_SIZE = 0x40000;
const BigArrayHandler = {
get: function(obj, prop) {
if (!isNaN(prop)) {
return obj.getElement(prop);
} else return obj[prop];
},
set: function(obj, prop, value) {
if (!isNaN(prop)) {
return obj.setElement(prop, value);
} else {
obj[prop] = value;
return true;
}
}
};
class _BigArray {
constructor (initSize) {
this.length = initSize || 0;
this.arr = new Array(SUBARRAY_SIZE);
for (let i=0; i<initSize; i+=SUBARRAY_SIZE) {
this.arr[i/SUBARRAY_SIZE] = new Array(Math.min(SUBARRAY_SIZE, initSize - i));
}
return this;
}
push () {
for (let i=0; i<arguments.length; i++) {
this.setElement (this.length, arguments[i]);
}
}
slice (f, t) {
const arr = new Array(t-f);
for (let i=f; i< t; i++) arr[i-f] = this.getElement(i);
return arr;
}
getElement(idx) {
idx = parseInt(idx);
const idx1 = Math.floor(idx / SUBARRAY_SIZE);
const idx2 = idx % SUBARRAY_SIZE;
return this.arr[idx1] ? this.arr[idx1][idx2] : undefined;
}
setElement(idx, value) {
idx = parseInt(idx);
const idx1 = Math.floor(idx / SUBARRAY_SIZE);
if (!this.arr[idx1]) {
this.arr[idx1] = new Array(SUBARRAY_SIZE);
}
const idx2 = idx % SUBARRAY_SIZE;
this.arr[idx1][idx2] = value;
if (idx >= this.length) this.length = idx+1;
return true;
}
getKeys() {
const newA = new BigArray();
for (let i=0; i<this.arr.length; i++) {
if (this.arr[i]) {
for (let j=0; j<this.arr[i].length; j++) {
if (typeof this.arr[i][j] !== "undefined") {
newA.push(i*SUBARRAY_SIZE+j);
}
}
}
}
return newA;
}
}
class BigArray {
constructor( initSize ) {
const obj = new _BigArray(initSize);
const extObj = new Proxy(obj, BigArrayHandler);
return extObj;
}
}
2020-10-20 20:24:45 +03:00
async function readR1csHeader(fd,sections,singleThread) {
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
const res = {};
2021-01-17 20:44:43 +03:00
await startReadUniqueSection(fd, sections, 1);
2020-10-20 20:24:45 +03:00
// Read Header
res.n8 = await fd.readULE32();
2021-01-17 20:44:43 +03:00
res.prime = await readBigInt(fd, res.n8);
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
res.curve = await getCurveFromR(res.prime, singleThread);
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
res.nVars = await fd.readULE32();
res.nOutputs = await fd.readULE32();
res.nPubInputs = await fd.readULE32();
res.nPrvInputs = await fd.readULE32();
res.nLabels = await fd.readULE64();
res.nConstraints = await fd.readULE32();
2021-01-17 20:44:43 +03:00
await endReadSection(fd);
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
return res;
}
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
async function readConstraints(fd,sections, r1cs, logger, loggerCtx) {
2021-01-17 20:44:43 +03:00
const bR1cs = await readSection(fd, sections, 2);
2020-10-20 20:24:45 +03:00
let bR1csPos = 0;
let constraints;
if (r1cs.nConstraints>1<<20) {
constraints = new BigArray();
} else {
constraints = [];
}
for (let i=0; i<r1cs.nConstraints; i++) {
if ((logger)&&(i%100000 == 0)) logger.info(`${loggerCtx}: Loading constraints: ${i}/${r1cs.nConstraints}`);
const c = readConstraint();
constraints.push(c);
2020-08-12 02:33:08 +03:00
}
2020-10-20 20:24:45 +03:00
return constraints;
2020-09-25 08:38:22 +03:00
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
function readConstraint() {
const c = [];
c[0] = readLC();
c[1] = readLC();
c[2] = readLC();
return c;
}
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
function readLC() {
const lc= {};
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
const buffUL32 = bR1cs.slice(bR1csPos, bR1csPos+4);
bR1csPos += 4;
const buffUL32V = new DataView(buffUL32.buffer);
const nIdx = buffUL32V.getUint32(0, true);
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
const buff = bR1cs.slice(bR1csPos, bR1csPos + (4+r1cs.n8)*nIdx );
bR1csPos += (4+r1cs.n8)*nIdx;
const buffV = new DataView(buff.buffer);
for (let i=0; i<nIdx; i++) {
const idx = buffV.getUint32(i*(4+r1cs.n8), true);
const val = r1cs.curve.Fr.fromRprLE(buff, i*(4+r1cs.n8)+4);
lc[idx] = val;
}
return lc;
2020-07-11 11:31:52 +03:00
}
}
2020-10-20 20:24:45 +03:00
async function readMap(fd, sections, r1cs, logger, loggerCtx) {
2021-01-17 20:44:43 +03:00
const bMap = await readSection(fd, sections, 3);
2020-10-20 20:24:45 +03:00
let bMapPos = 0;
let map;
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
if (r1cs.nVars>1<<20) {
map = new BigArray();
} else {
map = [];
}
for (let i=0; i<r1cs.nVars; i++) {
if ((logger)&&(i%10000 == 0)) logger.info(`${loggerCtx}: Loading map: ${i}/${r1cs.nVars}`);
const idx = readULE64();
map.push(idx);
}
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
return map;
2020-10-08 12:43:05 +03:00
2020-10-20 20:24:45 +03:00
function readULE64() {
const buffULE64 = bMap.slice(bMapPos, bMapPos+8);
bMapPos += 8;
const buffULE64V = new DataView(buffULE64.buffer);
const LSB = buffULE64V.getUint32(0, true);
const MSB = buffULE64V.getUint32(4, true);
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
return MSB * 0x100000000 + LSB;
}
2020-07-11 11:31:52 +03:00
}
2020-10-09 07:29:55 +03:00
async function readR1cs(fileName, loadConstraints, loadMap, singleThread, logger, loggerCtx) {
2020-07-11 11:31:52 +03:00
2021-01-17 20:44:43 +03:00
const {fd, sections} = await readBinFile(fileName, "r1cs", 1);
2020-10-20 20:24:45 +03:00
2020-10-09 07:29:55 +03:00
const res = await readR1csHeader(fd, sections, singleThread);
2020-07-11 11:31:52 +03:00
if (loadConstraints) {
2020-10-20 20:24:45 +03:00
res.constraints = await readConstraints(fd, sections, res, logger, loggerCtx);
2020-07-11 11:31:52 +03:00
}
// Read Labels
if (loadMap) {
2020-10-20 20:24:45 +03:00
res.map = await readMap(fd, sections, res, logger, loggerCtx);
2020-07-11 11:31:52 +03:00
}
await fd.close();
return res;
}
2021-01-21 21:26:22 +03:00
const bls12381r$2 = Scalar$1.e("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001", 16);
const bn128r$2 = Scalar$1.e("21888242871839275222246405745257275088548364400416034343698204186575808495617");
2020-07-11 11:31:52 +03:00
async function r1csInfo(r1csName, logger) {
2020-10-08 12:43:05 +03:00
const cir = await readR1cs(r1csName);
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
if (Scalar$1.eq(cir.prime, bn128r$2)) {
2020-07-11 11:31:52 +03:00
if (logger) logger.info("Curve: bn-128");
2021-01-21 21:26:22 +03:00
} else if (Scalar$1.eq(cir.prime, bls12381r$2)) {
2020-07-11 11:31:52 +03:00
if (logger) logger.info("Curve: bls12-381");
} else {
if (logger) logger.info(`Unknown Curve. Prime: ${Scalar$1.toString(cir.prime)}`);
}
if (logger) logger.info(`# of Wires: ${cir.nVars}`);
if (logger) logger.info(`# of Constraints: ${cir.nConstraints}`);
if (logger) logger.info(`# of Private Inputs: ${cir.nPrvInputs}`);
if (logger) logger.info(`# of Public Inputs: ${cir.nPubInputs}`);
2020-12-16 13:24:34 +03:00
if (logger) logger.info(`# of Labels: ${cir.nLabels}`);
2020-07-11 11:31:52 +03:00
if (logger) logger.info(`# of Outputs: ${cir.nOutputs}`);
return cir;
}
2021-01-21 21:26:22 +03:00
function stringifyBigInts$4(Fr, o) {
2020-10-08 17:06:48 +03:00
if (o instanceof Uint8Array) {
return Fr.toString(o);
} else if (Array.isArray(o)) {
2021-01-21 21:26:22 +03:00
return o.map(stringifyBigInts$4.bind(null, Fr));
2020-10-08 17:06:48 +03:00
} else if (typeof o == "object") {
const res = {};
const keys = Object.keys(o);
keys.forEach( (k) => {
2021-01-21 21:26:22 +03:00
res[k] = stringifyBigInts$4(Fr, o[k]);
2020-10-08 17:06:48 +03:00
});
return res;
} else if ((typeof(o) == "bigint") || o.eq !== undefined) {
return o.toString(10);
} else {
return o;
}
}
2020-07-11 11:31:52 +03:00
async function r1csExportJson(r1csFileName, logger) {
2020-10-09 07:29:55 +03:00
const cir = await readR1cs(r1csFileName, true, true, true, logger);
2020-10-08 17:06:48 +03:00
const Fr=cir.curve.Fr;
delete cir.curve;
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
return stringifyBigInts$4(Fr, cir);
2020-07-11 11:31:52 +03:00
}
var r1cs = /*#__PURE__*/Object.freeze({
__proto__: null,
print: r1csPrint,
info: r1csInfo,
exportJson: r1csExportJson
});
async function loadSymbols(symFileName) {
const sym = {
labelIdx2Name: [ "one" ],
varIdx2Name: [ "one" ],
componentIdx2Name: []
};
2021-01-21 21:26:22 +03:00
const fd = await readExisting$2(symFileName);
2020-07-11 11:31:52 +03:00
const buff = await fd.read(fd.totalSize);
const symsStr = new TextDecoder("utf-8").decode(buff);
const lines = symsStr.split("\n");
for (let i=0; i<lines.length; i++) {
const arr = lines[i].split(",");
if (arr.length!=4) continue;
if (sym.varIdx2Name[arr[1]]) {
sym.varIdx2Name[arr[1]] += "|" + arr[3];
} else {
sym.varIdx2Name[arr[1]] = arr[3];
}
sym.labelIdx2Name[arr[0]] = arr[3];
if (!sym.componentIdx2Name[arr[2]]) {
sym.componentIdx2Name[arr[2]] = extractComponent(arr[3]);
}
}
await fd.close();
return sym;
function extractComponent(name) {
const arr = name.split(".");
arr.pop(); // Remove the lasr element
return arr.join(".");
}
}
async function wtnsDebug(input, wasmFileName, wtnsFileName, symName, options, logger) {
2021-01-21 21:26:22 +03:00
const fdWasm = await readExisting$2(wasmFileName);
2020-07-11 11:31:52 +03:00
const wasm = await fdWasm.read(fdWasm.totalSize);
await fdWasm.close();
let wcOps = {
sanityCheck: true
};
let sym = await loadSymbols(symName);
if (options.set) {
if (!sym) sym = await loadSymbols(symName);
wcOps.logSetSignal= function(labelIdx, value) {
if (logger) logger.info("SET " + sym.labelIdx2Name[labelIdx] + " <-- " + value.toString());
};
}
if (options.get) {
if (!sym) sym = await loadSymbols(symName);
wcOps.logGetSignal= function(varIdx, value) {
if (logger) logger.info("GET " + sym.labelIdx2Name[varIdx] + " --> " + value.toString());
};
}
if (options.trigger) {
if (!sym) sym = await loadSymbols(symName);
wcOps.logStartComponent= function(cIdx) {
if (logger) logger.info("START: " + sym.componentIdx2Name[cIdx]);
};
wcOps.logFinishComponent= function(cIdx) {
if (logger) logger.info("FINISH: " + sym.componentIdx2Name[cIdx]);
};
}
2020-08-31 12:33:56 +03:00
wcOps.sym = sym;
2020-07-11 11:31:52 +03:00
2021-01-21 21:26:22 +03:00
const wc = await builder(wasm, wcOps);
2020-07-11 11:31:52 +03:00
const w = await wc.calculateWitness(input);
const fdWtns = await createBinFile(wtnsFileName, "wtns", 2, 2);
await write(fdWtns, w, wc.prime);
await fdWtns.close();
}
async function wtnsExportJson(wtnsFileName) {
const w = await read(wtnsFileName);
return w;
}
var wtns = /*#__PURE__*/Object.freeze({
__proto__: null,
calculate: wtnsCalculate,
debug: wtnsDebug,
exportJson: wtnsExportJson
});
2020-09-25 08:38:22 +03:00
const SUBARRAY_SIZE$1 = 0x40000;
2020-08-18 21:09:46 +03:00
2020-09-25 08:38:22 +03:00
const BigArrayHandler$1 = {
2020-08-18 21:09:46 +03:00
get: function(obj, prop) {
if (!isNaN(prop)) {
return obj.getElement(prop);
} else return obj[prop];
},
set: function(obj, prop, value) {
if (!isNaN(prop)) {
return obj.setElement(prop, value);
} else {
obj[prop] = value;
return true;
}
}
};
2020-09-25 08:38:22 +03:00
class _BigArray$1 {
2020-08-18 21:09:46 +03:00
constructor (initSize) {
this.length = initSize || 0;
2020-09-25 08:38:22 +03:00
this.arr = new Array(SUBARRAY_SIZE$1);
2020-08-18 21:09:46 +03:00
2020-09-25 08:38:22 +03:00
for (let i=0; i<initSize; i+=SUBARRAY_SIZE$1) {
this.arr[i/SUBARRAY_SIZE$1] = new Array(Math.min(SUBARRAY_SIZE$1, initSize - i));
2020-08-18 21:09:46 +03:00
}
return this;
}
push () {
for (let i=0; i<arguments.length; i++) {
this.setElement (this.length, arguments[i]);
}
}
slice (f, t) {
const arr = new Array(t-f);
for (let i=f; i< t; i++) arr[i-f] = this.getElement(i);
return arr;
}
getElement(idx) {
idx = parseInt(idx);
2020-09-25 08:38:22 +03:00
const idx1 = Math.floor(idx / SUBARRAY_SIZE$1);
const idx2 = idx % SUBARRAY_SIZE$1;
2020-08-18 21:09:46 +03:00
return this.arr[idx1] ? this.arr[idx1][idx2] : undefined;
}
setElement(idx, value) {
idx = parseInt(idx);
2020-09-25 08:38:22 +03:00
const idx1 = Math.floor(idx / SUBARRAY_SIZE$1);
2020-08-18 21:09:46 +03:00
if (!this.arr[idx1]) {
2020-09-25 08:38:22 +03:00
this.arr[idx1] = new Array(SUBARRAY_SIZE$1);
2020-08-18 21:09:46 +03:00
}
2020-09-25 08:38:22 +03:00
const idx2 = idx % SUBARRAY_SIZE$1;
2020-08-18 21:09:46 +03:00
this.arr[idx1][idx2] = value;
if (idx >= this.length) this.length = idx+1;
return true;
}
getKeys() {
2020-09-25 08:38:22 +03:00
const newA = new BigArray$1();
2020-08-18 21:09:46 +03:00
for (let i=0; i<this.arr.length; i++) {
if (this.arr[i]) {
for (let j=0; j<this.arr[i].length; j++) {
if (typeof this.arr[i][j] !== "undefined") {
2020-09-25 08:38:22 +03:00
newA.push(i*SUBARRAY_SIZE$1+j);
2020-08-18 21:09:46 +03:00
}
}
}
}
return newA;
}
}
2020-09-25 08:38:22 +03:00
class BigArray$1 {
2020-08-18 21:09:46 +03:00
constructor( initSize ) {
2020-09-25 08:38:22 +03:00
const obj = new _BigArray$1(initSize);
const extObj = new Proxy(obj, BigArrayHandler$1);
2020-08-18 21:09:46 +03:00
return extObj;
}
}
2020-07-11 11:31:52 +03:00
async function newZKey(r1csName, ptauName, zkeyName, logger) {
2020-10-20 20:24:45 +03:00
const TAU_G1 = 0;
const TAU_G2 = 1;
const ALPHATAU_G1 = 2;
const BETATAU_G1 = 3;
2020-07-11 11:31:52 +03:00
await blake2bWasm.ready();
const csHasher = blake2bWasm(64);
const {fd: fdPTau, sections: sectionsPTau} = await readBinFile(ptauName, "ptau", 1);
const {curve, power} = await readPTauHeader(fdPTau, sectionsPTau);
2020-10-20 20:24:45 +03:00
const {fd: fdR1cs, sections: sectionsR1cs} = await readBinFile(r1csName, "r1cs", 1);
const r1cs = await readR1csHeader(fdR1cs, sectionsR1cs, false);
2020-07-11 11:31:52 +03:00
2020-10-10 08:42:14 +03:00
const fdZKey = await createBinFile(zkeyName, "zkey", 1, 10, 1<<22, 1<<24);
2020-07-11 11:31:52 +03:00
const sG1 = curve.G1.F.n8*2;
const sG2 = curve.G2.F.n8*2;
if (r1cs.prime != curve.r) {
if (logger) logger.error("r1cs curve does not match powers of tau ceremony curve");
return -1;
}
2020-08-29 15:12:24 +03:00
const cirPower = log2$1(r1cs.nConstraints + r1cs.nPubInputs + r1cs.nOutputs +1 -1) +1;
2020-07-11 11:31:52 +03:00
2020-09-02 13:06:20 +03:00
if (cirPower > power) {
2020-08-12 02:33:08 +03:00
if (logger) logger.error(`circuit too big for this power of tau ceremony. ${r1cs.nConstraints}*2 > 2**${power}`);
2020-07-11 11:31:52 +03:00
return -1;
}
if (!sectionsPTau[12]) {
if (logger) logger.error("Powers of tau is not prepared.");
return -1;
}
const nPublic = r1cs.nOutputs + r1cs.nPubInputs;
2020-09-07 13:43:50 +03:00
const domainSize = 2 ** cirPower;
2020-07-11 11:31:52 +03:00
// Write the header
///////////
await startWriteSection(fdZKey, 1);
await fdZKey.writeULE32(1); // Groth
await endWriteSection(fdZKey);
// Write the Groth header section
///////////
await startWriteSection(fdZKey, 2);
const primeQ = curve.q;
const n8q = (Math.floor( (Scalar$1.bitLength(primeQ) - 1) / 64) +1)*8;
const primeR = curve.r;
const n8r = (Math.floor( (Scalar$1.bitLength(primeR) - 1) / 64) +1)*8;
const Rr = Scalar$1.mod(Scalar$1.shl(1, n8r*8), primeR);
const R2r = curve.Fr.e(Scalar$1.mod(Scalar$1.mul(Rr,Rr), primeR));
await fdZKey.writeULE32(n8q);
await writeBigInt(fdZKey, primeQ, n8q);
await fdZKey.writeULE32(n8r);
await writeBigInt(fdZKey, primeR, n8r);
await fdZKey.writeULE32(r1cs.nVars); // Total number of bars
await fdZKey.writeULE32(nPublic); // Total number of public vars (not including ONE)
await fdZKey.writeULE32(domainSize); // domainSize
let bAlpha1;
bAlpha1 = await fdPTau.read(sG1, sectionsPTau[4][0].p);
await fdZKey.write(bAlpha1);
bAlpha1 = await curve.G1.batchLEMtoU(bAlpha1);
csHasher.update(bAlpha1);
let bBeta1;
bBeta1 = await fdPTau.read(sG1, sectionsPTau[5][0].p);
await fdZKey.write(bBeta1);
bBeta1 = await curve.G1.batchLEMtoU(bBeta1);
csHasher.update(bBeta1);
let bBeta2;
bBeta2 = await fdPTau.read(sG2, sectionsPTau[6][0].p);
await fdZKey.write(bBeta2);
bBeta2 = await curve.G2.batchLEMtoU(bBeta2);
csHasher.update(bBeta2);
const bg1 = new Uint8Array(sG1);
curve.G1.toRprLEM(bg1, 0, curve.G1.g);
const bg2 = new Uint8Array(sG2);
curve.G2.toRprLEM(bg2, 0, curve.G2.g);
const bg1U = new Uint8Array(sG1);
curve.G1.toRprUncompressed(bg1U, 0, curve.G1.g);
const bg2U = new Uint8Array(sG2);
curve.G2.toRprUncompressed(bg2U, 0, curve.G2.g);
await fdZKey.write(bg2); // gamma2
await fdZKey.write(bg1); // delta1
await fdZKey.write(bg2); // delta2
csHasher.update(bg2U); // gamma2
csHasher.update(bg1U); // delta1
csHasher.update(bg2U); // delta2
await endWriteSection(fdZKey);
2020-10-20 20:24:45 +03:00
if (logger) logger.info("Reading r1cs");
let sR1cs = await readSection(fdR1cs, sectionsR1cs, 2);
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
const A = new BigArray$1(r1cs.nVars);
const B1 = new BigArray$1(r1cs.nVars);
const B2 = new BigArray$1(r1cs.nVars);
const C = new BigArray$1(r1cs.nVars- nPublic -1);
2020-07-11 11:31:52 +03:00
const IC = new Array(nPublic+1);
2020-10-20 20:24:45 +03:00
if (logger) logger.info("Reading tauG1");
let sTauG1 = await readSection(fdPTau, sectionsPTau, 12, (domainSize -1)*sG1, domainSize*sG1);
if (logger) logger.info("Reading tauG2");
let sTauG2 = await readSection(fdPTau, sectionsPTau, 13, (domainSize -1)*sG2, domainSize*sG2);
if (logger) logger.info("Reading alphatauG1");
let sAlphaTauG1 = await readSection(fdPTau, sectionsPTau, 14, (domainSize -1)*sG1, domainSize*sG1);
if (logger) logger.info("Reading betatauG1");
let sBetaTauG1 = await readSection(fdPTau, sectionsPTau, 15, (domainSize -1)*sG1, domainSize*sG1);
2020-10-12 13:18:45 +03:00
await processConstraints();
2020-08-18 21:09:46 +03:00
2020-10-12 13:18:45 +03:00
await composeAndWritePoints(3, "G1", IC, "IC");
2020-07-11 11:31:52 +03:00
2020-10-12 13:18:45 +03:00
await writeHs();
2020-10-11 13:42:04 +03:00
2020-10-12 13:18:45 +03:00
await hashHPoints();
2020-10-11 13:42:04 +03:00
2020-10-12 13:18:45 +03:00
await composeAndWritePoints(8, "G1", C, "C");
await composeAndWritePoints(5, "G1", A, "A");
await composeAndWritePoints(6, "G1", B1, "B1");
await composeAndWritePoints(7, "G2", B2, "B2");
2020-10-11 13:42:04 +03:00
2020-10-12 13:18:45 +03:00
const csHash = csHasher.digest();
// Contributions section
await startWriteSection(fdZKey, 10);
await fdZKey.write(csHash);
await fdZKey.writeULE32(0);
await endWriteSection(fdZKey);
2020-07-11 11:31:52 +03:00
2020-10-12 13:18:45 +03:00
if (logger) logger.info(formatHash(csHash, "Circuit hash: "));
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
2020-10-12 13:18:45 +03:00
await fdZKey.close();
await fdR1cs.close();
2020-10-20 20:24:45 +03:00
await fdPTau.close();
2020-07-11 11:31:52 +03:00
2020-10-12 13:18:45 +03:00
return csHash;
async function writeHs() {
await startWriteSection(fdZKey, 9);
const buffOut = new BigBuffer(domainSize*sG1);
if (cirPower < curve.Fr.s) {
let sTauG1 = await readSection(fdPTau, sectionsPTau, 12, (domainSize*2-1)*sG1, domainSize*2*sG1);
for (let i=0; i< domainSize; i++) {
if ((logger)&&(i%10000 == 0)) logger.debug(`spliting buffer: ${i}/${domainSize}`);
const buff = sTauG1.slice( (i*2+1)*sG1, (i*2+1)*sG1 + sG1 );
buffOut.set(buff, i*sG1);
2020-07-11 11:31:52 +03:00
}
2020-10-12 13:18:45 +03:00
} else if (cirPower == curve.Fr.s) {
const o = sectionsPTau[12][0].p + ((2 ** (cirPower+1)) -1)*sG1;
await fdPTau.readToBuffer(buffOut, 0, domainSize*sG1, o + domainSize*sG1);
} else {
if (logger) logger.error("Circuit too big");
throw new Error("Circuit too big for this curve");
2020-07-11 11:31:52 +03:00
}
2020-10-12 13:18:45 +03:00
await fdZKey.write(buffOut);
await endWriteSection(fdZKey);
}
2020-07-11 11:31:52 +03:00
2020-10-12 13:18:45 +03:00
async function processConstraints() {
const buffCoeff = new Uint8Array(12 + curve.Fr.n8);
const buffCoeffV = new DataView(buffCoeff.buffer);
2020-10-20 20:24:45 +03:00
const bOne = new Uint8Array(curve.Fr.n8);
curve.Fr.toRprLE(bOne, 0, curve.Fr.e(1));
2020-07-11 11:31:52 +03:00
2020-10-12 13:18:45 +03:00
let r1csPos = 0;
2020-07-11 11:31:52 +03:00
2020-10-12 13:18:45 +03:00
function r1cs_readULE32() {
const buff = sR1cs.slice(r1csPos, r1csPos+4);
r1csPos += 4;
const buffV = new DataView(buff.buffer);
return buffV.getUint32(0, true);
2020-07-11 11:31:52 +03:00
}
2020-10-20 20:24:45 +03:00
const coefs = new BigArray$1();
2020-10-12 13:18:45 +03:00
for (let c=0; c<r1cs.nConstraints; c++) {
if ((logger)&&(c%10000 == 0)) logger.debug(`processing constraints: ${c}/${r1cs.nConstraints}`);
const nA = r1cs_readULE32();
for (let i=0; i<nA; i++) {
const s = r1cs_readULE32();
2020-10-20 20:24:45 +03:00
const coefp = r1csPos;
r1csPos += curve.Fr.n8;
2020-10-12 13:18:45 +03:00
2020-10-20 20:24:45 +03:00
const l1t = TAU_G1;
const l1 = sG1*c;
const l2t = BETATAU_G1;
const l2 = sG1*c;
2020-10-12 13:18:45 +03:00
if (typeof A[s] === "undefined") A[s] = [];
2020-10-20 20:24:45 +03:00
A[s].push([l1t, l1, coefp]);
2020-10-12 13:18:45 +03:00
if (s <= nPublic) {
if (typeof IC[s] === "undefined") IC[s] = [];
2020-10-20 20:24:45 +03:00
IC[s].push([l2t, l2, coefp]);
2020-10-12 13:18:45 +03:00
} else {
if (typeof C[s- nPublic -1] === "undefined") C[s- nPublic -1] = [];
2020-10-20 20:24:45 +03:00
C[s - nPublic -1].push([l2t, l2, coefp]);
2020-10-12 13:18:45 +03:00
}
2020-10-20 20:24:45 +03:00
coefs.push([0, c, s, coefp]);
2020-10-12 13:18:45 +03:00
}
2020-07-11 11:31:52 +03:00
2020-10-12 13:18:45 +03:00
const nB = r1cs_readULE32();
for (let i=0; i<nB; i++) {
const s = r1cs_readULE32();
2020-10-20 20:24:45 +03:00
const coefp = r1csPos;
r1csPos += curve.Fr.n8;
const l1t = TAU_G1;
const l1 = sG1*c;
const l2t = TAU_G2;
const l2 = sG2*c;
const l3t = ALPHATAU_G1;
const l3 = sG1*c;
2020-10-12 13:18:45 +03:00
if (typeof B1[s] === "undefined") B1[s] = [];
2020-10-20 20:24:45 +03:00
B1[s].push([l1t, l1, coefp]);
2020-10-12 13:18:45 +03:00
if (typeof B2[s] === "undefined") B2[s] = [];
2020-10-20 20:24:45 +03:00
B2[s].push([l2t, l2, coefp]);
2020-10-12 13:18:45 +03:00
if (s <= nPublic) {
if (typeof IC[s] === "undefined") IC[s] = [];
2020-10-20 20:24:45 +03:00
IC[s].push([l3t, l3, coefp]);
2020-10-12 13:18:45 +03:00
} else {
if (typeof C[s- nPublic -1] === "undefined") C[s- nPublic -1] = [];
2020-10-20 20:24:45 +03:00
C[s- nPublic -1].push([l3t, l3, coefp]);
2020-10-12 13:18:45 +03:00
}
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
coefs.push([1, c, s, coefp]);
2020-10-12 13:18:45 +03:00
}
2020-09-07 13:43:50 +03:00
2020-10-12 13:18:45 +03:00
const nC = r1cs_readULE32();
for (let i=0; i<nC; i++) {
const s = r1cs_readULE32();
2020-10-20 20:24:45 +03:00
const coefp = r1csPos;
r1csPos += curve.Fr.n8;
2020-10-11 13:42:04 +03:00
2020-10-20 20:24:45 +03:00
const l1t = TAU_G1;
const l1 = sG1*c;
2020-10-12 13:18:45 +03:00
if (s <= nPublic) {
if (typeof IC[s] === "undefined") IC[s] = [];
2020-10-20 20:24:45 +03:00
IC[s].push([l1t, l1, coefp]);
2020-10-12 13:18:45 +03:00
} else {
if (typeof C[s- nPublic -1] === "undefined") C[s- nPublic -1] = [];
2020-10-20 20:24:45 +03:00
C[s- nPublic -1].push([l1t, l1, coefp]);
2020-10-12 13:18:45 +03:00
}
}
2020-09-07 13:43:50 +03:00
}
2020-07-11 11:31:52 +03:00
2020-10-12 13:18:45 +03:00
for (let s = 0; s <= nPublic ; s++) {
2020-10-20 20:24:45 +03:00
const l1t = TAU_G1;
const l1 = sG1*(r1cs.nConstraints + s);
const l2t = BETATAU_G1;
const l2 = sG1*(r1cs.nConstraints + s);
2020-10-12 13:18:45 +03:00
if (typeof A[s] === "undefined") A[s] = [];
2020-10-20 20:24:45 +03:00
A[s].push([l1t, l1, -1]);
2020-10-12 13:18:45 +03:00
if (typeof IC[s] === "undefined") IC[s] = [];
2020-10-20 20:24:45 +03:00
IC[s].push([l2t, l2, -1]);
coefs.push([0, r1cs.nConstraints + s, s, -1]);
2020-10-12 13:18:45 +03:00
}
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
await startWriteSection(fdZKey, 4);
const buffSection = new BigBuffer(coefs.length*(12+curve.Fr.n8) + 4);
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
const buff4 = new Uint8Array(4);
const buff4V = new DataView(buff4.buffer);
buff4V.setUint32(0, coefs.length, true);
buffSection.set(buff4);
let coefsPos = 4;
for (let i=0; i<coefs.length; i++) {
if ((logger)&&(i%100000 == 0)) logger.debug(`writing coeffs: ${i}/${coefs.length}`);
writeCoef(coefs[i]);
}
2020-07-11 11:31:52 +03:00
2020-10-20 20:24:45 +03:00
await fdZKey.write(buffSection);
await endWriteSection(fdZKey);
function writeCoef(c) {
buffCoeffV.setUint32(0, c[0], true);
buffCoeffV.setUint32(4, c[1], true);
buffCoeffV.setUint32(8, c[2], true);
let n;
if (c[3]>=0) {
n = curve.Fr.fromRprLE(sR1cs.slice(c[3], c[3] + curve.Fr.n8), 0);
} else {
n = curve.Fr.fromRprLE(bOne, 0);
}
2020-10-12 13:18:45 +03:00
const nR2 = curve.Fr.mul(n, R2r);
curve.Fr.toRprLE(buffCoeff, 12, nR2);
2020-10-20 20:24:45 +03:00
buffSection.set(buffCoeff, coefsPos);
coefsPos += buffCoeff.length;
2020-10-12 13:18:45 +03:00
}
2020-07-11 11:31:52 +03:00
}
async function composeAndWritePoints(idSection, groupName, arr, sectionName) {
2020-10-21 11:22:07 +03:00
const CHUNK_SIZE= 1<<15;
2020-09-25 08:38:22 +03:00
const G = curve[groupName];
2020-07-11 11:31:52 +03:00
hashU32(arr.length);
await startWriteSection(fdZKey, idSection);
2020-09-25 08:38:22 +03:00
let opPromises = [];
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
let i=0;
while (i<arr.length) {
let t=0;
while ((i<arr.length)&&(t<curve.tm.concurrency)) {
if (logger) logger.debug(`Writing points start ${sectionName}: ${i}/${arr.length}`);
let n = 1;
let nP = (arr[i] ? arr[i].length : 0);
2020-10-21 11:22:07 +03:00
while ((i + n < arr.length) && (nP + (arr[i+n] ? arr[i+n].length : 0) < CHUNK_SIZE) && (n<CHUNK_SIZE)) {
2020-09-25 08:38:22 +03:00
nP += (arr[i+n] ? arr[i+n].length : 0);
n ++;
}
const subArr = arr.slice(i, i + n);
const _i = i;
opPromises.push(composeAndWritePointsThread(groupName, subArr, logger, sectionName).then( (r) => {
if (logger) logger.debug(`Writing points end ${sectionName}: ${_i}/${arr.length}`);
return r;
}));
i += n;
t++;
2020-07-11 11:31:52 +03:00
}
2020-09-25 08:38:22 +03:00
const result = await Promise.all(opPromises);
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
for (let k=0; k<result.length; k++) {
await fdZKey.write(result[k][0]);
const buff = await G.batchLEMtoU(result[k][0]);
csHasher.update(buff);
}
opPromises = [];
2020-07-11 11:31:52 +03:00
}
2020-09-25 08:38:22 +03:00
await endWriteSection(fdZKey);
2020-07-11 11:31:52 +03:00
}
2020-09-25 08:38:22 +03:00
async function composeAndWritePointsThread(groupName, arr, logger, sectionName) {
2020-07-11 11:31:52 +03:00
const G = curve[groupName];
const sGin = G.F.n8*2;
const sGmid = G.F.n8*3;
const sGout = G.F.n8*2;
let fnExp, fnMultiExp, fnBatchToAffine, fnZero;
if (groupName == "G1") {
fnExp = "g1m_timesScalarAffine";
fnMultiExp = "g1m_multiexpAffine";
fnBatchToAffine = "g1m_batchToAffine";
fnZero = "g1m_zero";
} else if (groupName == "G2") {
fnExp = "g2m_timesScalarAffine";
fnMultiExp = "g2m_multiexpAffine";
fnBatchToAffine = "g2m_batchToAffine";
fnZero = "g2m_zero";
} else {
throw new Error("Invalid group");
}
let acc =0;
for (let i=0; i<arr.length; i++) acc += arr[i] ? arr[i].length : 0;
2020-09-25 08:38:22 +03:00
let bBases, bScalars;
if (acc> 2<<14) {
bBases = new BigBuffer(acc*sGin);
bScalars = new BigBuffer(acc*curve.Fr.n8);
} else {
bBases = new Uint8Array(acc*sGin);
bScalars = new Uint8Array(acc*curve.Fr.n8);
}
2020-07-11 11:31:52 +03:00
let pB =0;
let pS =0;
2020-09-25 08:38:22 +03:00
2020-10-20 20:24:45 +03:00
const sBuffs = [
sTauG1,
sTauG2,
sAlphaTauG1,
sBetaTauG1
];
const bOne = new Uint8Array(curve.Fr.n8);
curve.Fr.toRprLE(bOne, 0, curve.Fr.e(1));
2020-09-25 08:38:22 +03:00
let offset = 0;
2020-07-11 11:31:52 +03:00
for (let i=0; i<arr.length; i++) {
if (!arr[i]) continue;
for (let j=0; j<arr[i].length; j++) {
2020-10-21 11:22:07 +03:00
if ((logger)&&(j)&&(j%10000 == 0)) logger.debug(`Configuring big array ${sectionName}: ${j}/${arr[i].length}`);
2020-10-20 20:24:45 +03:00
bBases.set(
sBuffs[arr[i][j][0]].slice(
arr[i][j][1],
arr[i][j][1] + sGin
), offset*sGin
);
if (arr[i][j][2]>=0) {
bScalars.set(
sR1cs.slice(
arr[i][j][2],
arr[i][j][2] + curve.Fr.n8
),
offset*curve.Fr.n8
);
} else {
bScalars.set(bOne, offset*curve.Fr.n8);
}
2020-10-11 11:32:31 +03:00
offset ++;
2020-07-11 11:31:52 +03:00
}
}
2020-09-25 08:38:22 +03:00
if (arr.length>1) {
const task = [];
task.push({cmd: "ALLOCSET", var: 0, buff: bBases});
task.push({cmd: "ALLOCSET", var: 1, buff: bScalars});
task.push({cmd: "ALLOC", var: 2, len: arr.length*sGmid});
pB = 0;
pS = 0;
let pD =0;
for (let i=0; i<arr.length; i++) {
if (!arr[i]) {
task.push({cmd: "CALL", fnName: fnZero, params: [
{var: 2, offset: pD}
]});
pD += sGmid;
continue;
}
if (arr[i].length == 1) {
task.push({cmd: "CALL", fnName: fnExp, params: [
{var: 0, offset: pB},
{var: 1, offset: pS},
{val: curve.Fr.n8},
{var: 2, offset: pD}
]});
} else {
task.push({cmd: "CALL", fnName: fnMultiExp, params: [
{var: 0, offset: pB},
{var: 1, offset: pS},
{val: curve.Fr.n8},
{val: arr[i].length},
{var: 2, offset: pD}
]});
}
pB += sGin*arr[i].length;
pS += curve.Fr.n8*arr[i].length;
pD += sGmid;
}
task.push({cmd: "CALL", fnName: fnBatchToAffine, params: [
{var: 2},
{val: arr.length},
{var: 2},
]});
task.push({cmd: "GET", out: 0, var: 2, len: arr.length*sGout});
2020-07-11 11:31:52 +03:00
2020-09-25 08:38:22 +03:00
const res = await curve.tm.queueAction(task);
return res;
} else {
let res = await G.multiExpAffine(bBases, bScalars, logger, sectionName);
res = [ G.toAffine(res) ];
return res;
}
2020-07-11 11:31:52 +03:00
}
async function hashHPoints() {
const CHUNK_SIZE = 1<<14;
hashU32(domainSize-1);
for (let i=0; i<domainSize-1; i+= CHUNK_SIZE) {
if (logger) logger.debug(`HashingHPoints: ${i}/${domainSize}`);
const n = Math.min(domainSize-1, CHUNK_SIZE);
await hashHPointsChunk(i, n);
}
}
async function hashHPointsChunk(offset, nPoints) {
const buff1 = await fdPTau.read(nPoints *sG1, sectionsPTau[2][0].p + (offset + domainSize)*sG1);
const buff2 = await fdPTau.read(nPoints *sG1, sectionsPTau[2][0].p + offset*sG1);
const concurrency= curve.tm.concurrency;
const nPointsPerThread = Math.floor(nPoints / concurrency);
const opPromises = [];
for (let i=0; i<concurrency; i++) {
let n;
if (i< concurrency-1) {
n = nPointsPerThread;
} else {
n = nPoints - i*nPointsPerThread;
}
if (n==0) continue;
const subBuff1 = buff1.slice(i*nPointsPerThread*sG1, (i*nPointsPerThread+n)*sG1);
const subBuff2 = buff2.slice(i*nPointsPerThread*sG1, (i*nPointsPerThread+n)*sG1);
opPromises.push(hashHPointsThread(subBuff1, subBuff2));
}
const result = await Promise.all(opPromises);
for (let i=0; i<result.length; i++) {
csHasher.update(result[i][0]);
}
}
async function hashHPointsThread(buff1, buff2) {
const nPoints = buff1.byteLength/sG1;
const sGmid = curve.G1.F.n8*3;
const task = [];
task.push({cmd: "ALLOCSET", var: 0, buff: buff1});
task.push({cmd: "ALLOCSET", var: 1, buff: buff2});
task.push({cmd: "ALLOC", var: 2, len: nPoints*sGmid});
for (let i=0; i<nPoints; i++) {
task.push({
cmd: "CALL",
fnName: "g1m_subAffine",
params: [
{var: 0, offset: i*sG1},
{var: 1, offset: i*sG1},
{var: 2, offset: i*sGmid},
]
});
}
task.push({cmd: "CALL", fnName: "g1m_batchToAffine", params: [
{var: 2},
{val: nPoints},
{var: 2},
]});
task.push({cmd: "CALL", fnName: "g1m_batchLEMtoU", params: [
{var: 2},
{val: nPoints},
{var: 2},
]});
task.push({cmd: "GET", out: 0, var: 2, len: nPoints*sG1});
const res = await curve.tm.queueAction(task);
return res;
}
function hashU32(n) {
const buff = new Uint8Array(4);
const buffV = new DataView(buff.buffer, buff.byteOffset, buff.byteLength);
buffV.setUint32(0, n, false);
csHasher.update(buff);
}
}
async function phase2exportMPCParams(zkeyName, mpcparamsName, logger) {
const {fd: fdZKey, sections: sectionsZKey} = await readBinFile(zkeyName, "zkey", 2);
const zkey = await readHeader(fdZKey, sectionsZKey, "groth16");
const curve = await getCurveFromQ(zkey.q);
const sG1 = curve.G1.F.n8*2;
const sG2 = curve.G2.F.n8*2;
const mpcParams = await readMPCParams(fdZKey, curve, sectionsZKey);
2021-01-21 21:26:22 +03:00
const fdMPCParams = await createOverride(mpcparamsName);
2020-07-11 11:31:52 +03:00
/////////////////////
// Verification Key Section
/////////////////////
await writeG1(zkey.vk_alpha_1);
await writeG1(zkey.vk_beta_1);
await writeG2(zkey.vk_beta_2);
await writeG2(zkey.vk_gamma_2);
await writeG1(zkey.vk_delta_1);
await writeG2(zkey.vk_delta_2);
// IC
let buffBasesIC;
2020-10-22 15:52:18 +03:00
buffBasesIC = await readSection(fdZKey, sectionsZKey, 3);
2020-07-11 11:31:52 +03:00
buffBasesIC = await curve.G1.batchLEMtoU(buffBasesIC);
await writePointArray("G1", buffBasesIC);
/////////////////////
// h Section
/////////////////////
2020-10-22 15:52:18 +03:00
const buffBasesH_Lodd = await readSection(fdZKey, sectionsZKey, 9);
2020-07-11 11:31:52 +03:00
let buffBasesH_Tau;
buffBasesH_Tau = await curve.G1.fft(buffBasesH_Lodd, "affine", "jacobian", logger);
buffBasesH_Tau = await curve.G1.batchApplyKey(buffBasesH_Tau, curve.Fr.neg(curve.Fr.e(2)), curve.Fr.w[zkey.power+1], "jacobian", "affine", logger);
// Remove last element. (The degree of H will be allways m-2)
buffBasesH_Tau = buffBasesH_Tau.slice(0, buffBasesH_Tau.byteLength - sG1);
buffBasesH_Tau = await curve.G1.batchLEMtoU(buffBasesH_Tau);
await writePointArray("G1", buffBasesH_Tau);
/////////////////////
// L section
/////////////////////
let buffBasesC;
2020-10-22 15:52:18 +03:00
buffBasesC = await readSection(fdZKey, sectionsZKey, 8);
2020-07-11 11:31:52 +03:00
buffBasesC = await curve.G1.batchLEMtoU(buffBasesC);
await writePointArray("G1", buffBasesC);
/////////////////////
// A Section (C section)
/////////////////////
let buffBasesA;
2020-10-22 15:52:18 +03:00
buffBasesA = await readSection(fdZKey, sectionsZKey, 5);
2020-07-11 11:31:52 +03:00
buffBasesA = await curve.G1.batchLEMtoU(buffBasesA);
await writePointArray("G1", buffBasesA);
/////////////////////
// B1 Section
/////////////////////
let buffBasesB1;
2020-10-22 15:52:18 +03:00
buffBasesB1 = await readSection(fdZKey, sectionsZKey, 6);
2020-07-11 11:31:52 +03:00
buffBasesB1 = await curve.G1.batchLEMtoU(buffBasesB1);
await writePointArray("G1", buffBasesB1);
/////////////////////
// B2 Section
/////////////////////
let buffBasesB2;
2020-10-22 15:52:18 +03:00
buffBasesB2 = await readSection(fdZKey, sectionsZKey, 7);
2020-07-11 11:31:52 +03:00
buffBasesB2 = await curve.G2.batchLEMtoU(buffBasesB2);
await writePointArray("G2", buffBasesB2);
await fdMPCParams.write(mpcParams.csHash);
await writeU32(mpcParams.contributions.length);
for (let i=0; i<mpcParams.contributions.length; i++) {
const c = mpcParams.contributions[i];
await writeG1(c.deltaAfter);
await writeG1(c.delta.g1_s);
await writeG1(c.delta.g1_sx);
await writeG2(c.delta.g2_spx);
await fdMPCParams.write(c.transcript);
}
await fdZKey.close();
await fdMPCParams.close();
async function writeG1(P) {
const buff = new Uint8Array(sG1);
curve.G1.toRprUncompressed(buff, 0, P);
await fdMPCParams.write(buff);
}
async function writeG2(P) {
const buff = new Uint8Array(sG2);
curve.G2.toRprUncompressed(buff, 0, P);
await fdMPCParams.write(buff);
}
async function writePointArray(groupName, buff) {
let sG;
if (groupName == "G1") {
sG = sG1;
} else {
sG = sG2;
}
const buffSize = new Uint8Array(4);
const buffSizeV = new DataView(buffSize.buffer, buffSize.byteOffset, buffSize.byteLength);
buffSizeV.setUint32(0, buff.byteLength / sG, false);
await fdMPCParams.write(buffSize);
await fdMPCParams.write(buff);
}
async function writeU32(n) {
const buffSize = new Uint8Array(4);
const buffSizeV = new DataView(buffSize.buffer, buffSize.byteOffset, buffSize.byteLength);
buffSizeV.setUint32(0, n, false);
await fdMPCParams.write(buffSize);
}
}
async function phase2importMPCParams(zkeyNameOld, mpcparamsName, zkeyNameNew, name, logger) {
const {fd: fdZKeyOld, sections: sectionsZKeyOld} = await readBinFile(zkeyNameOld, "zkey", 2);
const zkeyHeader = await readHeader(fdZKeyOld, sectionsZKeyOld, "groth16");
const curve = await getCurveFromQ(zkeyHeader.q);
const sG1 = curve.G1.F.n8*2;
const sG2 = curve.G2.F.n8*2;
const oldMPCParams = await readMPCParams(fdZKeyOld, curve, sectionsZKeyOld);
const newMPCParams = {};
2021-01-21 21:26:22 +03:00
const fdMPCParams = await readExisting$2(mpcparamsName);
2020-07-11 11:31:52 +03:00
fdMPCParams.pos =
sG1*3 + sG2*3 + // vKey
8 + sG1*zkeyHeader.nVars + // IC + C
4 + sG1*(zkeyHeader.domainSize-1) + // H
4 + sG1*zkeyHeader.nVars + // A
4 + sG1*zkeyHeader.nVars + // B1
4 + sG2*zkeyHeader.nVars; // B2
// csHash
newMPCParams.csHash = await fdMPCParams.read(64);
const nConttributions = await fdMPCParams.readUBE32();
newMPCParams.contributions = [];
for (let i=0; i<nConttributions; i++) {
const c = { delta:{} };
c.deltaAfter = await readG1(fdMPCParams);
c.delta.g1_s = await readG1(fdMPCParams);
c.delta.g1_sx = await readG1(fdMPCParams);
c.delta.g2_spx = await readG2(fdMPCParams);
c.transcript = await fdMPCParams.read(64);
if (i<oldMPCParams.contributions.length) {
c.type = oldMPCParams.contributions[i].type;
if (c.type==1) {
c.beaconHash = oldMPCParams.contributions[i].beaconHash;
c.numIterationsExp = oldMPCParams.contributions[i].numIterationsExp;
}
if (oldMPCParams.contributions[i].name) {
c.name = oldMPCParams.contributions[i].name;
}
}
newMPCParams.contributions.push(c);
}
if (!hashIsEqual(newMPCParams.csHash, oldMPCParams.csHash)) {
if (logger) logger.error("Hash of the original circuit does not match with the MPC one");
return false;
}
if (oldMPCParams.contributions.length > newMPCParams.contributions.length) {
if (logger) logger.error("The impoerted file does not include new contributions");
return false;
}
for (let i=0; i<oldMPCParams.contributions.length; i++) {
if (!contributionIsEqual(oldMPCParams.contributions[i], newMPCParams.contributions[i])) {
if (logger) logger.error(`Previos contribution ${i} does not match`);
return false;
}
}
// Set the same name to all new controbutions
if (name) {
for (let i=oldMPCParams.contributions.length; i<newMPCParams.contributions.length; i++) {
newMPCParams.contributions[i].name = name;
}
}
const fdZKeyNew = await createBinFile(zkeyNameNew, "zkey", 1, 10);
fdMPCParams.pos = 0;
// Header
fdMPCParams.pos += sG1; // ignore alpha1 (keep original)
fdMPCParams.pos += sG1; // ignore beta1
fdMPCParams.pos += sG2; // ignore beta2
fdMPCParams.pos += sG2; // ignore gamma2
zkeyHeader.vk_delta_1 = await readG1(fdMPCParams);
zkeyHeader.vk_delta_2 = await readG2(fdMPCParams);
await writeHeader(fdZKeyNew, zkeyHeader);
// IC (Keep original)
const nIC = await fdMPCParams.readUBE32();
if (nIC != zkeyHeader.nPublic +1) {
if (logger) logger.error("Invalid number of points in IC");
await fdZKeyNew.discard();
return false;
}
fdMPCParams.pos += sG1*(zkeyHeader.nPublic+1);
await copySection(fdZKeyOld, sectionsZKeyOld, fdZKeyNew, 3);
// Coeffs (Keep original)
await copySection(fdZKeyOld, sectionsZKeyOld, fdZKeyNew, 4);
// H Section
const nH = await fdMPCParams.readUBE32();
if (nH != zkeyHeader.domainSize-1) {
if (logger) logger.error("Invalid number of points in H");
await fdZKeyNew.discard();
return false;
}
let buffH;
const buffTauU = await fdMPCParams.read(sG1*(zkeyHeader.domainSize-1));
const buffTauLEM = await curve.G1.batchUtoLEM(buffTauU);
buffH = new Uint8Array(zkeyHeader.domainSize*sG1);
buffH.set(buffTauLEM); // Let the last one to zero.
2020-08-12 02:33:08 +03:00
curve.G1.toRprLEM(buffH, sG1*(zkeyHeader.domainSize-1), curve.G1.zeroAffine);
2020-07-11 11:31:52 +03:00
const n2Inv = curve.Fr.neg(curve.Fr.inv(curve.Fr.e(2)));
const wInv = curve.Fr.inv(curve.Fr.w[zkeyHeader.power+1]);
buffH = await curve.G1.batchApplyKey(buffH, n2Inv, wInv, "affine", "jacobian", logger);
buffH = await curve.G1.ifft(buffH, "jacobian", "affine", logger);
await startWriteSection(fdZKeyNew, 9);
await fdZKeyNew.write(buffH);
await endWriteSection(fdZKeyNew);
// C Secion (L section)
const nL = await fdMPCParams.readUBE32();
if (nL != (zkeyHeader.nVars-zkeyHeader.nPublic-1)) {
if (logger) logger.error("Invalid number of points in L");
await fdZKeyNew.discard();
return false;
}
let buffL;
buffL = await fdMPCParams.read(sG1*(zkeyHeader.nVars-zkeyHeader.nPublic-1));
buffL = await curve.G1.batchUtoLEM(buffL);
await startWriteSection(fdZKeyNew, 8);
await fdZKeyNew.write(buffL);
await endWriteSection(fdZKeyNew);
// A Section
const nA = await fdMPCParams.readUBE32();
if (nA != zkeyHeader.nVars) {
if (logger) logger.error("Invalid number of points in A");
await fdZKeyNew.discard();
return false;
}
fdMPCParams.pos += sG1*(zkeyHeader.nVars);
await copySection(fdZKeyOld, sectionsZKeyOld, fdZKeyNew, 5);
// B1 Section
const nB1 = await fdMPCParams.readUBE32();
if (nB1 != zkeyHeader.nVars) {
if (logger) logger.error("Invalid number of points in B1");
await fdZKeyNew.discard();
return false;
}
fdMPCParams.pos += sG1*(zkeyHeader.nVars);
await copySection(fdZKeyOld, sectionsZKeyOld, fdZKeyNew, 6);
// B2 Section
const nB2 = await fdMPCParams.readUBE32();
if (nB2 != zkeyHeader.nVars) {
if (logger) logger.error("Invalid number of points in B2");
await fdZKeyNew.discard();
return false;
}
fdMPCParams.pos += sG2*(zkeyHeader.nVars);
await copySection(fdZKeyOld, sectionsZKeyOld, fdZKeyNew, 7);
await writeMPCParams(fdZKeyNew, curve, newMPCParams);
await fdMPCParams.close();
await fdZKeyNew.close();
await fdZKeyOld.close();
return true;
async function readG1(fd) {
const buff = await fd.read(curve.G1.F.n8*2);
return curve.G1.fromRprUncompressed(buff, 0);
}
async function readG2(fd) {
const buff = await fd.read(curve.G2.F.n8*2);
return curve.G2.fromRprUncompressed(buff, 0);
}
function contributionIsEqual(c1, c2) {
if (!curve.G1.eq(c1.deltaAfter , c2.deltaAfter)) return false;
if (!curve.G1.eq(c1.delta.g1_s , c2.delta.g1_s)) return false;
if (!curve.G1.eq(c1.delta.g1_sx , c2.delta.g1_sx)) return false;
if (!curve.G2.eq(c1.delta.g2_spx , c2.delta.g2_spx)) return false;
if (!hashIsEqual(c1.transcript, c2.transcript)) return false;
return true;
}
}
const sameRatio$2 = sameRatio;
2020-12-18 17:38:31 +03:00
async function phase2verifyFromInit(initFileName, pTauFileName, zkeyFileName, logger) {
2020-07-11 11:31:52 +03:00
let sr;
await blake2bWasm.ready();
const {fd, sections} = await readBinFile(zkeyFileName, "zkey", 2);
const zkey = await readHeader(fd, sections, "groth16");
const curve = await getCurveFromQ(zkey.q);
const sG1 = curve.G1.F.n8*2;
const sG2 = curve.G2.F.n8*2;
const mpcParams = await readMPCParams(fd, curve, sections);
const accumulatedHasher = blake2bWasm(64);
accumulatedHasher.update(mpcParams.csHash);
let curDelta = curve.G1.g;
for (let i=0; i<mpcParams.contributions.length; i++) {
const c = mpcParams.contributions[i];
const ourHasher = cloneHasher(accumulatedHasher);
hashG1(ourHasher, curve, c.delta.g1_s);
hashG1(ourHasher, curve, c.delta.g1_sx);
if (!hashIsEqual(ourHasher.digest(), c.transcript)) {
console.log(`INVALID(${i}): Inconsistent transcript `);
return false;
}
const delta_g2_sp = hashToG2(curve, c.transcript);
sr = await sameRatio$2(curve, c.delta.g1_s, c.delta.g1_sx, delta_g2_sp, c.delta.g2_spx);
if (sr !== true) {
console.log(`INVALID(${i}): public key G1 and G2 do not have the same ration `);
return false;
}
sr = await sameRatio$2(curve, curDelta, c.deltaAfter, delta_g2_sp, c.delta.g2_spx);
if (sr !== true) {
console.log(`INVALID(${i}): deltaAfter does not fillow the public key `);
return false;
}
if (c.type == 1) {
const rng = rngFromBeaconParams(c.beaconHash, c.numIterationsExp);
const expected_prvKey = curve.Fr.fromRng(rng);
const expected_g1_s = curve.G1.toAffine(curve.G1.fromRng(rng));
const expected_g1_sx = curve.G1.toAffine(curve.G1.timesFr(expected_g1_s, expected_prvKey));
if (curve.G1.eq(expected_g1_s, c.delta.g1_s) !== true) {
console.log(`INVALID(${i}): Key of the beacon does not match. g1_s `);
return false;
}
if (curve.G1.eq(expected_g1_sx, c.delta.g1_sx) !== true) {
console.log(`INVALID(${i}): Key of the beacon does not match. g1_sx `);
return false;
}
}
hashPubKey(accumulatedHasher, curve, c);
const contributionHasher = blake2bWasm(64);
hashPubKey(contributionHasher, curve, c);
c.contributionHash = contributionHasher.digest();
curDelta = c.deltaAfter;
}
const {fd: fdInit, sections: sectionsInit} = await readBinFile(initFileName, "zkey", 2);
const zkeyInit = await readHeader(fdInit, sectionsInit, "groth16");
if ( (!Scalar$1.eq(zkeyInit.q, zkey.q))
||(!Scalar$1.eq(zkeyInit.r, zkey.r))
||(zkeyInit.n8q != zkey.n8q)
||(zkeyInit.n8r != zkey.n8r))
{
if (logger) logger.error("INVALID: Different curves");
return false;
}
if ( (zkeyInit.nVars != zkey.nVars)
||(zkeyInit.nPublic != zkey.nPublic)
||(zkeyInit.domainSize != zkey.domainSize))
{
if (logger) logger.error("INVALID: Different circuit parameters");
return false;
}
if (!curve.G1.eq(zkey.vk_alpha_1, zkeyInit.vk_alpha_1)) {
if (logger) logger.error("INVALID: Invalid alpha1");
return false;
}
if (!curve.G1.eq(zkey.vk_beta_1, zkeyInit.vk_beta_1)) {
if (logger) logger.error("INVALID: Invalid beta1");
return false;
}
if (!curve.G2.eq(zkey.vk_beta_2, zkeyInit.vk_beta_2)) {
if (logger) logger.error("INVALID: Invalid beta2");
return false;
}
if (!curve.G2.eq(zkey.vk_gamma_2, zkeyInit.vk_gamma_2)) {
if (logger) logger.error("INVALID: Invalid gamma2");
return false;
}
if (!curve.G1.eq(zkey.vk_delta_1, curDelta)) {
2020-10-29 08:56:31 +03:00
if (logger) logger.error("INVALID: Invalid delta1");
2020-07-11 11:31:52 +03:00
return false;
}
sr = await sameRatio$2(curve, curve.G1.g, curDelta, curve.G2.g, zkey.vk_delta_2);
if (sr !== true) {
2020-10-29 08:56:31 +03:00
if (logger) logger.error("INVALID: Invalid delta2");
2020-07-11 11:31:52 +03:00
return false;
}
const mpcParamsInit = await readMPCParams(fdInit, curve, sectionsInit);
if (!hashIsEqual(mpcParams.csHash, mpcParamsInit.csHash)) {
if (logger) logger.error("INVALID: Circuit does not match");
return false;
}
// Check sizes of sections
if (sections[8][0].size != sG1*(zkey.nVars-zkey.nPublic-1)) {
if (logger) logger.error("INVALID: Invalid L section size");
return false;
}
if (sections[9][0].size != sG1*(zkey.domainSize)) {
if (logger) logger.error("INVALID: Invalid H section size");
return false;
}
let ss;
ss = await sectionIsEqual(fd, sections, fdInit, sectionsInit, 3);
if (!ss) {
if (logger) logger.error("INVALID: IC section is not identical");
return false;
}
ss = await sectionIsEqual(fd, sections, fdInit, sectionsInit, 4);
if (!ss) {
if (logger) logger.error("Coeffs section is not identical");
return false;
}
ss = await sectionIsEqual(fd, sections, fdInit, sectionsInit, 5);
if (!ss) {
if (logger) logger.error("A section is not identical");
return false;
}
ss = await sectionIsEqual(fd, sections, fdInit, sectionsInit, 6);
if (!ss) {
if (logger) logger.error("B1 section is not identical");
return false;
}
ss = await sectionIsEqual(fd, sections, fdInit, sectionsInit, 7);
if (!ss) {
if (logger) logger.error("B2 section is not identical");
return false;
}
// Check L
sr = await sectionHasSameRatio("G1", fdInit, sectionsInit, fd, sections, 8, zkey.vk_delta_2, zkeyInit.vk_delta_2, "L section");
if (sr!==true) {
if (logger) logger.error("L section does not match");
return false;
}
// Check H
sr = await sameRatioH();
if (sr!==true) {
if (logger) logger.error("H section does not match");
return false;
}
if (logger) logger.info(formatHash(mpcParams.csHash, "Circuit Hash: "));
await fd.close();
await fdInit.close();
for (let i=mpcParams.contributions.length-1; i>=0; i--) {
const c = mpcParams.contributions[i];
if (logger) logger.info("-------------------------");
if (logger) logger.info(formatHash(c.contributionHash, `contribution #${i+1} ${c.name ? c.name : ""}:`));
if (c.type == 1) {
if (logger) logger.info(`Beacon generator: ${byteArray2hex(c.beaconHash)}`);
if (logger) logger.info(`Beacon iterations Exp: ${c.numIterationsExp}`);
}
}
if (logger) logger.info("-------------------------");
if (logger) logger.info("ZKey Ok!");
return true;
async function sectionHasSameRatio(groupName, fd1, sections1, fd2, sections2, idSection, g2sp, g2spx, sectionName) {
const MAX_CHUNK_SIZE = 1<<20;
const G = curve[groupName];
const sG = G.F.n8*2;
await startReadUniqueSection(fd1, sections1, idSection);
await startReadUniqueSection(fd2, sections2, idSection);
let R1 = G.zero;
let R2 = G.zero;
const nPoints = sections1[idSection][0].size / sG;
for (let i=0; i<nPoints; i += MAX_CHUNK_SIZE) {
if (logger) logger.debug(`Same ratio check ${sectionName}: ${i}/${nPoints}`);
const n = Math.min(nPoints - i, MAX_CHUNK_SIZE);
const bases1 = await fd1.read(n*sG);
const bases2 = await fd2.read(n*sG);
const scalars = new Uint8Array(4*n);
crypto.randomFillSync(scalars);
const r1 = await G.multiExpAffine(bases1, scalars);
const r2 = await G.multiExpAffine(bases2, scalars);
R1 = G.add(R1, r1);
R2 = G.add(R2, r2);
}
await endReadSection(fd1);
await endReadSection(fd2);
if (nPoints == 0) return true;
2020-07-11 11:31:52 +03:00
sr = await sameRatio$2(curve, R1, R2, g2sp, g2spx);
if (sr !== true) return false;
return true;
}
async function sameRatioH() {
const MAX_CHUNK_SIZE = 1<<20;
const G = curve.G1;
2020-09-07 13:43:50 +03:00
const Fr = curve.Fr;
2020-07-11 11:31:52 +03:00
const sG = G.F.n8*2;
const {fd: fdPTau, sections: sectionsPTau} = await readBinFile(pTauFileName, "ptau", 1);
2020-10-27 09:39:05 +03:00
let buff_r = new BigBuffer(zkey.domainSize * zkey.n8r);
2020-07-11 11:31:52 +03:00
const seed= new Array(8);
for (let i=0; i<8; i++) {
seed[i] = crypto.randomBytes(4).readUInt32BE(0, true);
}
const rng = new ChaCha(seed);
for (let i=0; i<zkey.domainSize-1; i++) { // Note that last one is zero
2020-09-07 13:43:50 +03:00
const e = Fr.fromRng(rng);
Fr.toRprLE(buff_r, i*zkey.n8r, e);
2020-07-11 11:31:52 +03:00
}
2020-09-07 13:43:50 +03:00
Fr.toRprLE(buff_r, (zkey.domainSize-1)*zkey.n8r, Fr.zero);
2020-07-11 11:31:52 +03:00
let R1 = G.zero;
for (let i=0; i<zkey.domainSize; i += MAX_CHUNK_SIZE) {
if (logger) logger.debug(`H Verificaition(tau): ${i}/${zkey.domainSize}`);
const n = Math.min(zkey.domainSize - i, MAX_CHUNK_SIZE);
2020-12-16 13:18:14 +03:00
const buff1 = await fdPTau.read(sG*n, sectionsPTau[2][0].p + zkey.domainSize*sG + i*sG);
const buff2 = await fdPTau.read(sG*n, sectionsPTau[2][0].p + i*sG);
2020-07-11 11:31:52 +03:00
const buffB = await batchSubstract(buff1, buff2);
2020-10-28 09:28:26 +03:00
const buffS = buff_r.slice(i*zkey.n8r, (i+n)*zkey.n8r);
2020-07-11 11:31:52 +03:00
const r = await G.multiExpAffine(buffB, buffS);
R1 = G.add(R1, r);
}
// Caluclate odd coeficients in transformed domain
2020-09-07 13:43:50 +03:00
buff_r = await Fr.batchToMontgomery(buff_r);
2020-07-11 11:31:52 +03:00
// const first = curve.Fr.neg(curve.Fr.inv(curve.Fr.e(2)));
// Works*2 const first = curve.Fr.neg(curve.Fr.e(2));
2020-09-07 13:43:50 +03:00
let first;
if (zkey.power < Fr.s) {
first = Fr.neg(Fr.e(2));
} else {
const small_m = 2 ** Fr.s;
const shift_to_small_m = Fr.exp(Fr.shift, small_m);
first = Fr.sub( shift_to_small_m, Fr.one);
}
2020-07-11 11:31:52 +03:00
// const inc = curve.Fr.inv(curve.PFr.w[zkey.power+1]);
2020-09-07 13:43:50 +03:00
const inc = zkey.power < Fr.s ? Fr.w[zkey.power+1] : Fr.shift;
buff_r = await Fr.batchApplyKey(buff_r, first, inc);
buff_r = await Fr.fft(buff_r);
buff_r = await Fr.batchFromMontgomery(buff_r);
2020-07-11 11:31:52 +03:00
await startReadUniqueSection(fd, sections, 9);
let R2 = G.zero;
for (let i=0; i<zkey.domainSize; i += MAX_CHUNK_SIZE) {
if (logger) logger.debug(`H Verificaition(lagrange): ${i}/${zkey.domainSize}`);
const n = Math.min(zkey.domainSize - i, MAX_CHUNK_SIZE);
const buff = await fd.read(sG*n);
2020-10-29 08:56:31 +03:00
const buffS = buff_r.slice(i*zkey.n8r, (i+n)*zkey.n8r);
2020-07-11 11:31:52 +03:00
const r = await G.multiExpAffine(buff, buffS);
R2 = G.add(R2, r);
}
await endReadSection(fd);
sr = await sameRatio$2(curve, R1, R2, zkey.vk_delta_2, zkeyInit.vk_delta_2);
if (sr !== true) return false;
return true;
}
async function batchSubstract(buff1, buff2) {
const sG = curve.G1.F.n8*2;
const nPoints = buff1.byteLength / sG;
const concurrency= curve.tm.concurrency;
const nPointsPerThread = Math.floor(nPoints / concurrency);
const opPromises = [];
for (let i=0; i<concurrency; i++) {
let n;
if (i< concurrency-1) {
n = nPointsPerThread;
} else {
n = nPoints - i*nPointsPerThread;
}
if (n==0) continue;
const subBuff1 = buff1.slice(i*nPointsPerThread*sG1, (i*nPointsPerThread+n)*sG1);
const subBuff2 = buff2.slice(i*nPointsPerThread*sG1, (i*nPointsPerThread+n)*sG1);
opPromises.push(batchSubstractThread(subBuff1, subBuff2));
}
const result = await Promise.all(opPromises);
const fullBuffOut = new Uint8Array(nPoints*sG);
let p =0;
for (let i=0; i<result.length; i++) {
fullBuffOut.set(result[i][0], p);
p+=result[i][0].byteLength;
}
return fullBuffOut;
}
async function batchSubstractThread(buff1, buff2) {
const sG1 = curve.G1.F.n8*2;
const sGmid = curve.G1.F.n8*3;
const nPoints = buff1.byteLength/sG1;
const task = [];
task.push({cmd: "ALLOCSET", var: 0, buff: buff1});
task.push({cmd: "ALLOCSET", var: 1, buff: buff2});
task.push({cmd: "ALLOC", var: 2, len: nPoints*sGmid});
for (let i=0; i<nPoints; i++) {
task.push({
cmd: "CALL",
fnName: "g1m_subAffine",
params: [
{var: 0, offset: i*sG1},
{var: 1, offset: i*sG1},
{var: 2, offset: i*sGmid},
]
});
}
task.push({cmd: "CALL", fnName: "g1m_batchToAffine", params: [
{var: 2},
{val: nPoints},
{var: 2},
]});
task.push({cmd: "GET", out: 0, var: 2, len: nPoints*sG1});
const res = await curve.tm.queueAction(task);
return res;
}
}
2020-12-18 17:38:31 +03:00
async function phase2verifyFromR1cs(r1csFileName, pTauFileName, zkeyFileName, logger) {
// const initFileName = "~" + zkeyFileName + ".init";
const initFileName = {type: "bigMem"};
await newZKey(r1csFileName, pTauFileName, initFileName, logger);
2020-12-21 15:30:36 +03:00
return await phase2verifyFromInit(initFileName, pTauFileName, zkeyFileName, logger);
2020-12-18 17:38:31 +03:00
}
2020-07-11 11:31:52 +03:00
async function phase2contribute(zkeyNameOld, zkeyNameNew, name, entropy, logger) {
await blake2bWasm.ready();
const {fd: fdOld, sections: sections} = await readBinFile(zkeyNameOld, "zkey", 2);
const zkey = await readHeader(fdOld, sections, "groth16");
const curve = await getCurveFromQ(zkey.q);
const mpcParams = await readMPCParams(fdOld, curve, sections);
const fdNew = await createBinFile(zkeyNameNew, "zkey", 1, 10);
const rng = await getRandomRng(entropy);
const transcriptHasher = blake2bWasm(64);
transcriptHasher.update(mpcParams.csHash);
for (let i=0; i<mpcParams.contributions.length; i++) {
hashPubKey(transcriptHasher, curve, mpcParams.contributions[i]);
}
const curContribution = {};
curContribution.delta = {};
curContribution.delta.prvKey = curve.Fr.fromRng(rng);
curContribution.delta.g1_s = curve.G1.toAffine(curve.G1.fromRng(rng));
curContribution.delta.g1_sx = curve.G1.toAffine(curve.G1.timesFr(curContribution.delta.g1_s, curContribution.delta.prvKey));
hashG1(transcriptHasher, curve, curContribution.delta.g1_s);
hashG1(transcriptHasher, curve, curContribution.delta.g1_sx);
curContribution.transcript = transcriptHasher.digest();
curContribution.delta.g2_sp = hashToG2(curve, curContribution.transcript);
curContribution.delta.g2_spx = curve.G2.toAffine(curve.G2.timesFr(curContribution.delta.g2_sp, curContribution.delta.prvKey));
zkey.vk_delta_1 = curve.G1.timesFr(zkey.vk_delta_1, curContribution.delta.prvKey);
zkey.vk_delta_2 = curve.G2.timesFr(zkey.vk_delta_2, curContribution.delta.prvKey);
curContribution.deltaAfter = zkey.vk_delta_1;
curContribution.type = 0;
if (name) curContribution.name = name;
mpcParams.contributions.push(curContribution);
await writeHeader(fdNew, zkey);
// IC
await copySection(fdOld, sections, fdNew, 3);
// Coeffs (Keep original)
await copySection(fdOld, sections, fdNew, 4);
// A Section
await copySection(fdOld, sections, fdNew, 5);
// B1 Section
await copySection(fdOld, sections, fdNew, 6);
// B2 Section
await copySection(fdOld, sections, fdNew, 7);
const invDelta = curve.Fr.inv(curContribution.delta.prvKey);
await applyKeyToSection(fdOld, sections, fdNew, 8, curve, "G1", invDelta, curve.Fr.e(1), "L Section", logger);
await applyKeyToSection(fdOld, sections, fdNew, 9, curve, "G1", invDelta, curve.Fr.e(1), "H Section", logger);
await writeMPCParams(fdNew, curve, mpcParams);
await fdOld.close();
await fdNew.close();
const contributionHasher = blake2bWasm(64);
hashPubKey(contributionHasher, curve, curContribution);
const contribuionHash = contributionHasher.digest();
2020-12-17 22:42:58 +03:00
if (logger) logger.info(formatHash(mpcParams.csHash, "Circuit Hash: "));
2020-07-11 11:31:52 +03:00
if (logger) logger.info(formatHash(contribuionHash, "Contribution Hash: "));
return contribuionHash;
}
async function beacon$1(zkeyNameOld, zkeyNameNew, name, beaconHashStr, numIterationsExp, logger) {
await blake2bWasm.ready();
const beaconHash = hex2ByteArray(beaconHashStr);
if ( (beaconHash.byteLength == 0)
|| (beaconHash.byteLength*2 !=beaconHashStr.length))
{
if (logger) logger.error("Invalid Beacon Hash. (It must be a valid hexadecimal sequence)");
return false;
}
if (beaconHash.length>=256) {
if (logger) logger.error("Maximum lenght of beacon hash is 255 bytes");
return false;
}
numIterationsExp = parseInt(numIterationsExp);
if ((numIterationsExp<10)||(numIterationsExp>63)) {
if (logger) logger.error("Invalid numIterationsExp. (Must be between 10 and 63)");
return false;
}
const {fd: fdOld, sections: sections} = await readBinFile(zkeyNameOld, "zkey", 2);
const zkey = await readHeader(fdOld, sections, "groth16");
const curve = await getCurveFromQ(zkey.q);
const mpcParams = await readMPCParams(fdOld, curve, sections);
const fdNew = await createBinFile(zkeyNameNew, "zkey", 1, 10);
const rng = await rngFromBeaconParams(beaconHash, numIterationsExp);
const transcriptHasher = blake2bWasm(64);
transcriptHasher.update(mpcParams.csHash);
for (let i=0; i<mpcParams.contributions.length; i++) {
hashPubKey(transcriptHasher, curve, mpcParams.contributions[i]);
}
const curContribution = {};
curContribution.delta = {};
curContribution.delta.prvKey = curve.Fr.fromRng(rng);
curContribution.delta.g1_s = curve.G1.toAffine(curve.G1.fromRng(rng));
curContribution.delta.g1_sx = curve.G1.toAffine(curve.G1.timesFr(curContribution.delta.g1_s, curContribution.delta.prvKey));
hashG1(transcriptHasher, curve, curContribution.delta.g1_s);
hashG1(transcriptHasher, curve, curContribution.delta.g1_sx);
curContribution.transcript = transcriptHasher.digest();
curContribution.delta.g2_sp = hashToG2(curve, curContribution.transcript);
curContribution.delta.g2_spx = curve.G2.toAffine(curve.G2.timesFr(curContribution.delta.g2_sp, curContribution.delta.prvKey));
zkey.vk_delta_1 = curve.G1.timesFr(zkey.vk_delta_1, curContribution.delta.prvKey);
zkey.vk_delta_2 = curve.G2.timesFr(zkey.vk_delta_2, curContribution.delta.prvKey);
curContribution.deltaAfter = zkey.vk_delta_1;
curContribution.type = 1;
curContribution.numIterationsExp = numIterationsExp;
curContribution.beaconHash = beaconHash;
if (name) curContribution.name = name;
mpcParams.contributions.push(curContribution);
await writeHeader(fdNew, zkey);
// IC
await copySection(fdOld, sections, fdNew, 3);
// Coeffs (Keep original)
await copySection(fdOld, sections, fdNew, 4);
// A Section
await copySection(fdOld, sections, fdNew, 5);
// B1 Section
await copySection(fdOld, sections, fdNew, 6);
// B2 Section
await copySection(fdOld, sections, fdNew, 7);
const invDelta = curve.Fr.inv(curContribution.delta.prvKey);
await applyKeyToSection(fdOld, sections, fdNew, 8, curve, "G1", invDelta, curve.Fr.e(1), "L Section", logger);
await applyKeyToSection(fdOld, sections, fdNew, 9, curve, "G1", invDelta, curve.Fr.e(1), "H Section", logger);
await writeMPCParams(fdNew, curve, mpcParams);
await fdOld.close();
await fdNew.close();
const contributionHasher = blake2bWasm(64);
hashPubKey(contributionHasher, curve, curContribution);
const contribuionHash = contributionHasher.digest();
if (logger) logger.info(formatHash(contribuionHash, "Contribution Hash: "));
return contribuionHash;
}
async function zkeyExportJson(zkeyFileName, verbose) {
2021-01-28 23:55:41 +03:00
const zKey = await readZKey(zkeyFileName, true);
2020-07-11 11:31:52 +03:00
return zKey;
}
// Format of the output
2020-07-14 12:55:12 +03:00
async function bellmanContribute(curve, challengeFilename, responesFileName, entropy, logger) {
2020-07-11 11:31:52 +03:00
await blake2bWasm.ready();
const rng = await getRandomRng(entropy);
const delta = curve.Fr.fromRng(rng);
const invDelta = curve.Fr.inv(delta);
const sG1 = curve.G1.F.n8*2;
const sG2 = curve.G2.F.n8*2;
2021-01-21 21:26:22 +03:00
const fdFrom = await readExisting$2(challengeFilename);
const fdTo = await createOverride(responesFileName);
2020-07-11 11:31:52 +03:00
await copy(sG1); // alpha1
await copy(sG1); // beta1
await copy(sG2); // beta2
await copy(sG2); // gamma2
const oldDelta1 = await readG1();
const delta1 = curve.G1.timesFr(oldDelta1, delta);
await writeG1(delta1);
const oldDelta2 = await readG2();
const delta2 = curve.G2.timesFr(oldDelta2, delta);
await writeG2(delta2);
// IC
const nIC = await fdFrom.readUBE32();
await fdTo.writeUBE32(nIC);
await copy(nIC*sG1);
// H
const nH = await fdFrom.readUBE32();
await fdTo.writeUBE32(nH);
2020-07-14 12:55:12 +03:00
await applyKeyToChallengeSection(fdFrom, fdTo, null, curve, "G1", nH, invDelta, curve.Fr.e(1), "UNCOMPRESSED", "H", logger);
2020-07-11 11:31:52 +03:00
// L
const nL = await fdFrom.readUBE32();
await fdTo.writeUBE32(nL);
2020-07-14 12:55:12 +03:00
await applyKeyToChallengeSection(fdFrom, fdTo, null, curve, "G1", nL, invDelta, curve.Fr.e(1), "UNCOMPRESSED", "L", logger);
2020-07-11 11:31:52 +03:00
// A
const nA = await fdFrom.readUBE32();
await fdTo.writeUBE32(nA);
await copy(nA*sG1);
// B1
const nB1 = await fdFrom.readUBE32();
await fdTo.writeUBE32(nB1);
await copy(nB1*sG1);
// B2
const nB2 = await fdFrom.readUBE32();
await fdTo.writeUBE32(nB2);
await copy(nB2*sG2);
//////////
/// Read contributions
//////////
const transcriptHasher = blake2bWasm(64);
const mpcParams = {};
// csHash
mpcParams.csHash = await fdFrom.read(64);
transcriptHasher.update(mpcParams.csHash);
const nConttributions = await fdFrom.readUBE32();
mpcParams.contributions = [];
for (let i=0; i<nConttributions; i++) {
const c = { delta:{} };
c.deltaAfter = await readG1();
c.delta.g1_s = await readG1();
c.delta.g1_sx = await readG1();
c.delta.g2_spx = await readG2();
c.transcript = await fdFrom.read(64);
mpcParams.contributions.push(c);
hashPubKey(transcriptHasher, curve, c);
}
const curContribution = {};
curContribution.delta = {};
curContribution.delta.prvKey = delta;
curContribution.delta.g1_s = curve.G1.toAffine(curve.G1.fromRng(rng));
curContribution.delta.g1_sx = curve.G1.toAffine(curve.G1.timesFr(curContribution.delta.g1_s, delta));
hashG1(transcriptHasher, curve, curContribution.delta.g1_s);
hashG1(transcriptHasher, curve, curContribution.delta.g1_sx);
curContribution.transcript = transcriptHasher.digest();
curContribution.delta.g2_sp = hashToG2(curve, curContribution.transcript);
curContribution.delta.g2_spx = curve.G2.toAffine(curve.G2.timesFr(curContribution.delta.g2_sp, delta));
curContribution.deltaAfter = delta1;
curContribution.type = 0;
mpcParams.contributions.push(curContribution);
//////////
/// Write COntribution
//////////
await fdTo.write(mpcParams.csHash);
await fdTo.writeUBE32(mpcParams.contributions.length);
for (let i=0; i<mpcParams.contributions.length; i++) {
const c = mpcParams.contributions[i];
await writeG1(c.deltaAfter);
await writeG1(c.delta.g1_s);
await writeG1(c.delta.g1_sx);
await writeG2(c.delta.g2_spx);
await fdTo.write(c.transcript);
}
const contributionHasher = blake2bWasm(64);
hashPubKey(contributionHasher, curve, curContribution);
const contributionHash = contributionHasher.digest();
if (logger) logger.info(formatHash(contributionHash, "Contribution Hash: "));
await fdTo.close();
await fdFrom.close();
return contributionHash;
async function copy(nBytes) {
const CHUNK_SIZE = fdFrom.pageSize*2;
for (let i=0; i<nBytes; i+= CHUNK_SIZE) {
const n = Math.min(nBytes -i, CHUNK_SIZE);
const buff = await fdFrom.read(n);
await fdTo.write(buff);
}
}
async function readG1() {
const buff = await fdFrom.read(curve.G1.F.n8*2);
return curve.G1.fromRprUncompressed(buff, 0);
}
async function readG2() {
const buff = await fdFrom.read(curve.G2.F.n8*2);
return curve.G2.fromRprUncompressed(buff, 0);
}
async function writeG1(P) {
const buff = new Uint8Array(sG1);
curve.G1.toRprUncompressed(buff, 0, P);
await fdTo.write(buff);
}
async function writeG2(P) {
const buff = new Uint8Array(sG2);
curve.G2.toRprUncompressed(buff, 0, P);
await fdTo.write(buff);
}
}
2021-01-21 21:26:22 +03:00
const {stringifyBigInts: stringifyBigInts$5} = utils$1;
2020-07-11 11:31:52 +03:00
async function zkeyExportVerificationKey(zkeyName, logger) {
const {fd, sections} = await readBinFile(zkeyName, "zkey", 2);
const zkey = await readHeader(fd, sections, "groth16");
const curve = await getCurveFromQ(zkey.q);
const sG1 = curve.G1.F.n8*2;
const alphaBeta = await curve.pairing( zkey.vk_alpha_1 , zkey.vk_beta_2 );
let vKey = {
protocol: zkey.protocol,
curve: curve.name,
nPublic: zkey.nPublic,
vk_alpha_1: curve.G1.toObject(zkey.vk_alpha_1),
vk_beta_2: curve.G2.toObject(zkey.vk_beta_2),
vk_gamma_2: curve.G2.toObject(zkey.vk_gamma_2),
vk_delta_2: curve.G2.toObject(zkey.vk_delta_2),
vk_alphabeta_12: curve.Gt.toObject(alphaBeta)
};
// Read IC Section
///////////
await startReadUniqueSection(fd, sections, 3);
vKey.IC = [];
for (let i=0; i<= zkey.nPublic; i++) {
const buff = await fd.read(sG1);
const P = curve.G1.toObject(buff);
vKey.IC.push(P);
}
await endReadSection(fd);
2021-01-21 21:26:22 +03:00
vKey = stringifyBigInts$5(vKey);
2020-07-11 11:31:52 +03:00
await fd.close();
return vKey;
}
// Not ready yet
// module.exports.generateVerifier_kimleeoh = generateVerifier_kimleeoh;
async function exportSolidityVerifier(zKeyName, templateName, logger) {
const verificationKey = await zkeyExportVerificationKey(zKeyName);
2021-01-21 21:26:22 +03:00
const fd = await readExisting$2(templateName);
2020-07-11 11:31:52 +03:00
const buff = await fd.read(fd.totalSize);
let template = new TextDecoder("utf-8").decode(buff);
const vkalpha1_str = `${verificationKey.vk_alpha_1[0].toString()},`+
`${verificationKey.vk_alpha_1[1].toString()}`;
template = template.replace("<%vk_alpha1%>", vkalpha1_str);
const vkbeta2_str = `[${verificationKey.vk_beta_2[0][1].toString()},`+
`${verificationKey.vk_beta_2[0][0].toString()}], `+
`[${verificationKey.vk_beta_2[1][1].toString()},` +
`${verificationKey.vk_beta_2[1][0].toString()}]`;
template = template.replace("<%vk_beta2%>", vkbeta2_str);
const vkgamma2_str = `[${verificationKey.vk_gamma_2[0][1].toString()},`+
`${verificationKey.vk_gamma_2[0][0].toString()}], `+
`[${verificationKey.vk_gamma_2[1][1].toString()},` +
`${verificationKey.vk_gamma_2[1][0].toString()}]`;
template = template.replace("<%vk_gamma2%>", vkgamma2_str);
const vkdelta2_str = `[${verificationKey.vk_delta_2[0][1].toString()},`+
`${verificationKey.vk_delta_2[0][0].toString()}], `+
`[${verificationKey.vk_delta_2[1][1].toString()},` +
`${verificationKey.vk_delta_2[1][0].toString()}]`;
template = template.replace("<%vk_delta2%>", vkdelta2_str);
// The points
template = template.replace("<%vk_input_length%>", (verificationKey.IC.length-1).toString());
template = template.replace("<%vk_ic_length%>", verificationKey.IC.length.toString());
let vi = "";
for (let i=0; i<verificationKey.IC.length; i++) {
if (vi != "") vi = vi + " ";
vi = vi + `vk.IC[${i}] = Pairing.G1Point(${verificationKey.IC[i][0].toString()},`+
`${verificationKey.IC[i][1].toString()});\n`;
}
template = template.replace("<%vk_ic_pts%>", vi);
return template;
}
var zkey = /*#__PURE__*/Object.freeze({
__proto__: null,
newZKey: newZKey,
exportBellman: phase2exportMPCParams,
importBellman: phase2importMPCParams,
2020-12-18 17:38:31 +03:00
verifyFromR1cs: phase2verifyFromR1cs,
verifyFromInit: phase2verifyFromInit,
2020-07-11 11:31:52 +03:00
contribute: phase2contribute,
beacon: beacon$1,
exportJson: zkeyExportJson,
bellmanContribute: bellmanContribute,
exportVerificationKey: zkeyExportVerificationKey,
exportSolidityVerifier: exportSolidityVerifier
});
exports.groth16 = groth16;
exports.powersOfTau = powersoftau;
exports.r1cs = r1cs;
exports.wtns = wtns;
exports.zKey = zkey;
2021-01-17 11:56:08 +03:00
Object.defineProperty(exports, '__esModule', { value: true });
2020-07-11 11:31:52 +03:00
return exports;
}({}));
2021-02-11 10:07:33 +03:00
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic25hcmtqcy5qcyIsInNvdXJjZXMiOlsiLi4vbm9kZV9tb2R1bGVzL2ZmamF2YXNjcmlwdC9zcmMvc2NhbGFyX25hdGl2ZS5qcyIsIi4uL25vZGVfbW9kdWxlcy9iaWctaW50ZWdlci9CaWdJbnRlZ2VyLmpzIiwiLi4vbm9kZV9tb2R1bGVzL2ZmamF2YXNjcmlwdC9zcmMvc2NhbGFyX2JpZ2ludC5qcyIsIi4uL25vZGVfbW9kdWxlcy9mZmphdmFzY3JpcHQvc3JjL3NjYWxhci5qcyIsIi4uL25vZGVfbW9kdWxlcy9mZmphdmFzY3JpcHQvc3JjL3BvbGZpZWxkLmpzIiwiLi4vbm9kZV9tb2R1bGVzL2ZmamF2YXNjcmlwdC9zcmMvZnV0aWxzLmpzIiwiLi4vbm9kZV9tb2R1bGVzL2ZmamF2YXNjcmlwdC9zcmMvZnNxcnQuanMiLCIuLi9ub2RlX21vZHVsZXMvZmZqYXZhc2NyaXB0L3NyYy9jaGFjaGEuanMiLCIuLi9ub2RlX21vZHVsZXMvZmZqYXZhc2NyaXB0L3NyYy9yYW5kb20uanMiLCIuLi9ub2RlX21vZHVsZXMvZmZqYXZhc2NyaXB0L3NyYy9mMWZpZWxkX25hdGl2ZS5qcyIsIi4uL25vZGVfbW9kdWxlcy9mZmphdmFzY3JpcHQvc3JjL2YxZmllbGRfYmlnaW50LmpzIiwiLi4vbm9kZV9tb2R1bGVzL2ZmamF2YXNjcmlwdC9zcmMvZjFmaWVsZC5qcyIsIi4uL25vZGVfbW9kdWxlcy93YXNtY3VydmVzL2J1aWxkL2JuMTI4X3dhc20uanMiLCIuLi9ub2RlX21vZHVsZXMvd2FzbWN1cnZlcy9idWlsZC9ibHMxMjM4MV93YXNtLmpzIiwiLi4vbm9kZV9tb2R1bGVzL3dhc21jdXJ2ZXMvaW5kZXguanMiLCIuLi9ub2RlX21vZHVsZXMvZmZqYXZhc2NyaXB0L3NyYy91dGlsc19uYXRpdmUuanMiLCIuLi9ub2RlX21vZHVsZXMvZmZqYXZhc2NyaXB0L3NyYy91dGlsc19iaWdpbnQuanMiLCIuLi9ub2RlX21vZHVsZXMvZmZqYXZhc2NyaXB0L3NyYy91dGlscy5qcyIsIi4uL25vZGVfbW9kdWxlcy9mZmphdmFzY3JpcHQvc3JjL2JpZ2J1ZmZlci5qcyIsIi4uL25vZGVfbW9kdWxlcy9mZmphdmFzY3JpcHQvc3JjL2VuZ2luZV9iYXRjaGNvbnZlcnQuanMiLCIuLi9ub2RlX21vZHVsZXMvZmZqYXZhc2NyaXB0L3NyYy93YXNtX2ZpZWxkMS5qcyIsIi4uL25vZGVfbW9kdWxlcy9mZmphdmFzY3JpcHQvc3JjL3dhc21fZmllbGQyLmpzIiwiLi4vbm9kZV9tb2R1bGVzL2ZmamF2YXNjcmlwdC9zcmMvd2FzbV9maWVsZDMuanMiLCIuLi9ub2RlX21vZHVsZXMvZmZqYXZhc2NyaXB0L3NyYy93YXNtX2N1cnZlLmpzIiwiLi4vbm9kZV9tb2R1bGVzL2ZmamF2YXNjcmlwdC9zcmMvdGhyZWFkbWFuX3RocmVhZC5qcyIsIi4uL25vZGVfbW9kdWxlcy93ZWItd29ya2VyL2Nqcy9icm93c2VyLmpzIiwiLi4vbm9kZV9tb2R1bGVzL2ZmamF2YXNjcmlwdC9zcmMvdGhyZWFkbWFuLmpzIiwiLi4vbm9kZV9tb2R1bGVzL2ZmamF2YXNjcmlwdC9zcmMvZW5naW5lX2FwcGx5a2V5LmpzIiwiLi4vbm9kZV9tb2R1bGVzL2ZmamF2YXNjcmlwdC9zcmMvZW5naW5lX3BhaXJpbmcuanMiLCIuLi9ub2RlX21vZHVsZXMvZmZqYXZhc2NyaXB0L3NyYy9lbmdpbmVfbXVsdGlleHAuanMiLCIuLi9ub2RlX21vZHVsZXMvZmZqYXZhc2NyaXB0L3NyYy9lbmdpbmVfZmZ0LmpzIiwiLi4vbm9kZV9tb2R1bGVzL2ZmamF2YXNjcmlwdC9zcmMvZW5naW5lLmpzIiwiLi4vbm9kZV9tb2R1bGVzL2ZmamF2YXNjcmlwdC9zcmMvYm4xMjguanMiLCIuLi9ub2RlX21vZHVsZXMvZmZqYXZhc2NyaXB0L3NyYy9ibHMxMjM4MS5qcyIsIi4uL25vZGVfbW9kdWxlcy9mZmphdmFzY3JpcHQvc3JjL2N1cnZlcy5qcyIsIi4uL25vZGVfbW9kdWxlcy9mZmphdmFzY3JpcHQvbWFpbi5qcyIsIi4uL25vZGVfbW9kdWxlcy9mYXN0ZmlsZS9zcmMvb3NmaWxlLmpzIiwiLi4vbm9kZV9tb2R1bGVzL2Zhc3RmaWxlL3NyYy9tZW1maWxlLmpzIiwiLi4vbm9kZV9tb2R1bGVzL2Zhc3RmaWxlL3NyYy9iaWdtZW1maWxlLmpzIiwiLi4vbm9kZV9tb2R1bGVzL2Zhc3RmaWxlL3NyYy9mYXN0ZmlsZS5qcyIsIi4uL25vZGVfbW9kdWxlcy9AaWRlbjMvYmluZmlsZXV0aWxzL3NyYy9iaW5maWxldXRpbHMuanMiLCIuLi9zcmMvY3VydmVzLmpzIiwiLi4vbm9kZV9tb2R1bGVzL25hbm9hc3NlcnQvaW5kZXguanMiLCIuLi9ub2RlX21vZHVsZXMvYmxha2UyYi13YXNtL2JsYWtlMmIuanMiLCIuLi9ub2RlX21vZHVsZXMvYmxha2UyYi13YXNtL2luZGV4LmpzIiwiLi4vc3JjL21pc2MuanMiLCIuLi9zcmMvemtleV91dGlscy5qcyIsIi4uL3NyYy93dG5zX3V0aWxzLmpzIiwiLi4vc3JjL2dyb3RoMTZfcHJvdmUuanMiLCIuLi9ub2RlX21vZHVsZXMvZm52LXBsdXMvaW5kZXguanMiLCIuLi9ub2RlX21vZHVsZXMvY2lyY29tX3J1bnRpbWUvanMvdXRpbHMuanMiLCIuLi9ub2RlX21vZHVsZXMvY2lyY29tX3J1bnRpbWUvanMvd2l0bmVzc19jYWxjdWxhdG9yLmpzIiwiLi4vc3JjL3d0bnNfY2FsY3VsYXRlLmpzIiwiLi4vc3JjL2dyb3RoMTZfZnVsbHByb3ZlLmpzIiwiLi4vc3JjL2dyb3RoMTZfdmVyaWZ5LmpzIiwiLi4vc3JjL2tleXBhaXIuanMiLCIuLi9zcmMvcG93ZXJzb2Z0YXVfdXRpbHMuanMiLCIuLi9zcmMvcG93ZXJzb2Z0YXVfbmV3LmpzIiwiLi4vc3JjL3Bvd2Vyc29mdGF1X2V4cG9ydF9jaGFsbGVuZ2UuanMiLCIuLi9zcmMvcG93ZXJzb2Z0YXVfaW1wb3J0LmpzIiwiLi4vc3JjL3Bvd2Vyc29mdGF1X3ZlcmlmeS5qcyIsIi4uL3NyYy9tcGNfYXBwbHlrZXkuanMiLCIuLi9zcmMvcG93ZXJzb2Z0YXVfY2hhbGxlbmdlX2NvbnRyaWJ1dGUuanMiLCIuLi9zcmMvcG93ZXJzb2Z0YXVfYmVhY29uLmpzIiwiLi4vc3JjL3Bvd2Vyc29mdGF1X2NvbnRyaWJ1dGUuanMiLCIuLi9zcmMvcG93ZXJzb2Z0YXVfcHJlcGFyZXBoYXNlMi5qcyIsIi4uL3NyYy9wb3dlcnNvZnRhdV90cnVuY2F0ZS5qcyIsIi4uL3NyYy9wb3dlcnNvZnRhdV9jb252ZXJ0LmpzIiwiLi4vc3JjL3Bvd2Vyc29mdGF1X2V4cG9ydF9qc29uLmpzIiwiLi4vc3JjL3IxY3NfcHJpbnQuanMiLCIuLi9ub2RlX21vZHVsZXMvQGlkZW4zL2JpZ2FycmF5L3NyYy9iaWdhcnJheS5qcyIsIi4uL25vZGVfbW9kdWxlcy9yMWNzZmlsZS9zcmMvcjF