diff --git a/build/main.cjs b/build/main.cjs index d4aa5bb..712a3b0 100644 --- a/build/main.cjs +++ b/build/main.cjs @@ -34,7 +34,7 @@ function fromString(s, radix) { const e = fromString; function fromArray(a, radix) { - let acc =0n; + let acc =BigInt(0); radix = BigInt(radix); for (let i=0; i> 1n; + E = E >> BigInt(1); } return res; } @@ -92,12 +92,12 @@ function bits(n) { let E = BigInt(n); const res = []; while (E) { - if (E & 1n) { + if (E & BigInt(1)) { res.push(1); } else { res.push( 0 ); } - E = E >> 1n; + E = E >> BigInt(1); } return res; } @@ -1636,27 +1636,27 @@ function getThreadRng() { class ZqField { constructor(p) { this.type="F1"; - this.one = 1n; - this.zero = 0n; + this.one = BigInt(1); + this.zero = BigInt(0); this.p = BigInt(p); this.m = 1; - this.negone = this.p-1n; - this.two = 2n; - this.half = this.p >> 1n; + this.negone = this.p-this.one; + this.two = BigInt(2); + this.half = this.p >> this.one; this.bitLength = bitLength$2(this.p); - this.mask = (1n << BigInt(this.bitLength)) - 1n; + this.mask = (this.one << BigInt(this.bitLength)) - this.one; 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.R = this.e(this.one << BigInt(this.n64*64)); this.Ri = this.inv(this.R); - const e = this.negone >> 1n; + const e = this.negone >> this.one; this.nqr = this.two; let r = this.pow(this.nqr, e); while (!this.eq(r, this.negone)) { - this.nqr = this.nqr + 1n; + this.nqr = this.nqr + this.one; r = this.pow(this.nqr, e); } @@ -1664,9 +1664,9 @@ class ZqField { this.s = 0; this.t = this.negone; - while ((this.t & 1n) == 0n) { + while ((this.t & this.one) == this.zero) { this.s = this.s + 1; - this.t = this.t >> 1n; + this.t = this.t >> this.one; } this.nqr_to_t = this.pow(this.nqr, this.t); @@ -1760,16 +1760,16 @@ class ZqField { inv(a) { if (!a) throw new Error("Division by zero"); - let t = 0n; + let t = this.zero; let r = this.p; - let newt = 1n; + let newt = this.one; let newr = a % this.p; while (newr) { let q = r/newr; [t, newt] = [newt, t-q*newt]; [r, newr] = [newr, r-q*newr]; } - if (t<0n) t += this.p; + if (t> nb; } else { - return 0n; + return this.zero; } } } @@ -1834,34 +1834,34 @@ class ZqField { } land(a, b) { - return (a && b) ? 1n : 0n; + return (a && b) ? this.one : this.zero; } lor(a, b) { - return (a || b) ? 1n : 0n; + return (a || b) ? this.one : this.zero; } lnot(a) { - return (a) ? 0n : 1n; + return (a) ? this.zero : this.one; } sqrt_old(n) { - if (n == 0n) return this.zero; + if (n == this.zero) return this.zero; // Test that have solution const res = this.pow(n, this.negone >> this.one); - if ( res != 1n ) return null; + if ( res != this.one ) return null; 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 ); + let r = this.pow(n, this.add(this.t, this.one) >> this.one ); - while ( t != 1n ) { + while ( t != this.one ) { let sq = this.square(t); let i = 1; - while (sq != 1n ) { + while (sq != this.one ) { i++; sq = this.square(sq); } @@ -1876,7 +1876,7 @@ class ZqField { r = this.mul(r, b); } - if (r > (this.p >> 1n)) { + if (r > (this.p >> this.one)) { r = this.neg(r); } @@ -1896,9 +1896,9 @@ class ZqField { random() { const nBytes = (this.bitLength*2 / 8); - let res =0n; + let res =this.zero; for (let i=0; i 0) { if (o-4 >= 0) { o -= 4; - buffV.setUint32(o, Number(r & 0xFFFFFFFFn)); - r = r >> 32n; + buffV.setUint32(o, Number(r & BigInt(0xFFFFFFFF))); + r = r >> BigInt(32); } else if (o-2 >= 0) { o -= 2; - buffV.setUint16(o, Number(r & 0xFFFFn)); - r = r >> 16n; + buffV.setUint16(o, Number(r & BigInt(0xFFFF))); + r = r >> BigInt(16); } else { o -= 1; - buffV.setUint8(o, Number(r & 0xFFn)); - r = r >> 8n; + buffV.setUint8(o, Number(r & BigInt(0xFF))); + r = r >> BigInt(8); } } if (r) { @@ -3300,7 +3300,7 @@ function beInt2Buff(n, len) { function leBuff2int(buff) { - let res = 0n; + let res = BigInt(0); let i = 0; const buffV = new DataView(buff.buffer, buff.byteOffset, buff.byteLength); while (i> 32n; + r = r >> BigInt(32); } else if (o+2 <= len) { - buffV.setUint16(Number(o, r & 0xFFFFn), true ); + buffV.setUint16(Number(o, r & BigInt(0xFFFF)), true ); o += 2; - r = r >> 16n; + r = r >> BigInt(16); } else { - buffV.setUint8(Number(o, r & 0xFFn), true ); + buffV.setUint8(Number(o, r & BigInt(0xFF)), true ); o += 1; - r = r >> 8n; + r = r >> BigInt(8); } } if (r) { diff --git a/src/f1field_native.js b/src/f1field_native.js index 1cc4513..bc54598 100644 --- a/src/f1field_native.js +++ b/src/f1field_native.js @@ -7,27 +7,27 @@ import {getRandomBytes} from "./random.js"; export default class ZqField { constructor(p) { this.type="F1"; - this.one = 1n; - this.zero = 0n; + this.one = BigInt(1); + this.zero = BigInt(0); this.p = BigInt(p); this.m = 1; - this.negone = this.p-1n; - this.two = 2n; - this.half = this.p >> 1n; + this.negone = this.p-this.one; + this.two = BigInt(2); + this.half = this.p >> this.one; this.bitLength = Scalar.bitLength(this.p); - this.mask = (1n << BigInt(this.bitLength)) - 1n; + this.mask = (this.one << BigInt(this.bitLength)) - this.one; 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.R = this.e(this.one << BigInt(this.n64*64)); this.Ri = this.inv(this.R); - const e = this.negone >> 1n; + const e = this.negone >> this.one; this.nqr = this.two; let r = this.pow(this.nqr, e); while (!this.eq(r, this.negone)) { - this.nqr = this.nqr + 1n; + this.nqr = this.nqr + this.one; r = this.pow(this.nqr, e); } @@ -35,9 +35,9 @@ export default class ZqField { this.s = 0; this.t = this.negone; - while ((this.t & 1n) == 0n) { + while ((this.t & this.one) == this.zero) { this.s = this.s + 1; - this.t = this.t >> 1n; + this.t = this.t >> this.one; } this.nqr_to_t = this.pow(this.nqr, this.t); @@ -131,16 +131,16 @@ export default class ZqField { inv(a) { if (!a) throw new Error("Division by zero"); - let t = 0n; + let t = this.zero; let r = this.p; - let newt = 1n; + let newt = this.one; let newr = a % this.p; while (newr) { let q = r/newr; [t, newt] = [newt, t-q*newt]; [r, newr] = [newr, r-q*newr]; } - if (t<0n) t += this.p; + if (t> nb; } else { - return 0n; + return this.zero; } } } @@ -205,34 +205,34 @@ export default class ZqField { } land(a, b) { - return (a && b) ? 1n : 0n; + return (a && b) ? this.one : this.zero; } lor(a, b) { - return (a || b) ? 1n : 0n; + return (a || b) ? this.one : this.zero; } lnot(a) { - return (a) ? 0n : 1n; + return (a) ? this.zero : this.one; } sqrt_old(n) { - if (n == 0n) return this.zero; + if (n == this.zero) return this.zero; // Test that have solution const res = this.pow(n, this.negone >> this.one); - if ( res != 1n ) return null; + if ( res != this.one ) return null; 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 ); + let r = this.pow(n, this.add(this.t, this.one) >> this.one ); - while ( t != 1n ) { + while ( t != this.one ) { let sq = this.square(t); let i = 1; - while (sq != 1n ) { + while (sq != this.one ) { i++; sq = this.square(sq); } @@ -247,7 +247,7 @@ export default class ZqField { r = this.mul(r, b); } - if (r > (this.p >> 1n)) { + if (r > (this.p >> this.one)) { r = this.neg(r); } @@ -267,9 +267,9 @@ export default class ZqField { random() { const nBytes = (this.bitLength*2 / 8); - let res =0n; + let res =this.zero; for (let i=0; i> 1n; + E = E >> BigInt(1); } return res; } @@ -74,12 +74,12 @@ export function bits(n) { let E = BigInt(n); const res = []; while (E) { - if (E & 1n) { + if (E & BigInt(1)) { res.push(1); } else { res.push( 0 ); } - E = E >> 1n; + E = E >> BigInt(1); } return res; } diff --git a/src/utils_native.js b/src/utils_native.js index bd76352..78ba02a 100644 --- a/src/utils_native.js +++ b/src/utils_native.js @@ -39,7 +39,7 @@ export function unstringifyBigInts(o) { } export function beBuff2int(buff) { - let res = 0n; + let res = BigInt(0); let i = buff.length; let offset = 0; const buffV = new DataView(buff.buffer, buff.byteOffset, buff.byteLength); @@ -69,16 +69,16 @@ export function beInt2Buff(n, len) { while (o > 0) { if (o-4 >= 0) { o -= 4; - buffV.setUint32(o, Number(r & 0xFFFFFFFFn)); - r = r >> 32n; + buffV.setUint32(o, Number(r & BigInt(0xFFFFFFFF))); + r = r >> BigInt(32); } else if (o-2 >= 0) { o -= 2; - buffV.setUint16(o, Number(r & 0xFFFFn)); - r = r >> 16n; + buffV.setUint16(o, Number(r & BigInt(0xFFFF))); + r = r >> BigInt(16); } else { o -= 1; - buffV.setUint8(o, Number(r & 0xFFn)); - r = r >> 8n; + buffV.setUint8(o, Number(r & BigInt(0xFF))); + r = r >> BigInt(8); } } if (r) { @@ -89,7 +89,7 @@ export function beInt2Buff(n, len) { export function leBuff2int(buff) { - let res = 0n; + let res = BigInt(0); let i = 0; const buffV = new DataView(buff.buffer, buff.byteOffset, buff.byteLength); while (i> 32n; + r = r >> BigInt(32); } else if (o+2 <= len) { - buffV.setUint16(Number(o, r & 0xFFFFn), true ); + buffV.setUint16(Number(o, r & BigInt(0xFFFF)), true ); o += 2; - r = r >> 16n; + r = r >> BigInt(16); } else { - buffV.setUint8(Number(o, r & 0xFFn), true ); + buffV.setUint8(Number(o, r & BigInt(0xFF)), true ); o += 1; - r = r >> 8n; + r = r >> BigInt(8); } } if (r) {