UNPKG

epir

Version:

EllipticPIR client library (Node.js / TypeScript bindings).

118 lines 4.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRandomScalarsConcat = exports.getRandomScalars = exports.getRandomScalar = exports.isZero = exports.isCanonical = exports.getRandomBytes = exports.checkIsHex = exports.hexToArrayBuffer = exports.arrayBufferToHex = exports.arrayBufferCompare = exports.arrayBufferConcat = exports.printMeasurement = exports.runMeasurement = exports.time = void 0; const types_1 = require("./types"); const time = () => Date.now(); exports.time = time; const runMeasurement = async (func) => { const begin = exports.time(); const ret = func(); if (ret instanceof Promise) { const ret2 = await ret; const end = exports.time(); return { execTime: end - begin, ret: ret2 }; } else { const end = exports.time(); return { execTime: end - begin, ret: ret }; } }; exports.runMeasurement = runMeasurement; const printMeasurement = async (func, prefix, postfix = 'ms.') => { const { execTime, ret } = await exports.runMeasurement(func); console.log(`\u001b[32m${prefix} ${execTime.toLocaleString()} ${postfix}\u001b[0m`); return ret; }; exports.printMeasurement = printMeasurement; const arrayBufferConcat = (arr) => { const len = arr.reduce((acc, v) => acc + v.byteLength, 0); const ret = new Uint8Array(len); for (let i = 0, offset = 0; i < arr.length; i++) { ret.set(new Uint8Array(arr[i]), offset); offset += arr[i].byteLength; } return ret.buffer; }; exports.arrayBufferConcat = arrayBufferConcat; const arrayBufferCompare = (a, aOffset, b, bOffset, len) => { const aa = new Uint8Array(a, aOffset, len); const bb = new Uint8Array(b, bOffset, len); for (let i = 0; i < len; i++) { if (aa[i] == bb[i]) continue; return aa[i] - bb[i]; } return 0; }; exports.arrayBufferCompare = arrayBufferCompare; const arrayBufferToHex = (buf) => { const arr = new Uint8Array(buf); let ret = ''; for (const n of arr) { ret += Number(n).toString(16).padStart(2, '0'); } return ret; }; exports.arrayBufferToHex = arrayBufferToHex; const hexToArrayBuffer = (hex) => { const split = hex.match(/.{2}/g); if (!split) return new ArrayBuffer(0); return new Uint8Array(split.map((h) => parseInt(h, 16))).buffer; }; exports.hexToArrayBuffer = hexToArrayBuffer; const checkIsHex = (hex, expectedSize = -1) => { const pattern = /^[0-9a-fA-F]+$/; if (expectedSize >= 0) { return ((hex.length === 2 * expectedSize) && (hex.match(pattern) !== null)); } else { return ((hex.length % 2 === 0) && (hex.match(pattern) !== null)); } }; exports.checkIsHex = checkIsHex; const getRandomBytes = (len) => { const MAX_ENTROPY = 65536; const ret = new Uint8Array(len); for (let offset = 0; offset < len; offset += MAX_ENTROPY) { window.crypto.getRandomValues(ret.subarray(offset, Math.min(len, offset + MAX_ENTROPY))); } return ret.buffer; }; exports.getRandomBytes = getRandomBytes; const isCanonical = (buf) => { const bufView = new Uint8Array(buf); let c = (bufView[31] & 0x7f) ^ 0x7f; for (let i = 30; i > 0; i--) { c |= bufView[i] ^ 0xff; } const d = (0xed - 1 - bufView[0]) >> 8; return !((c == 0) && d); }; exports.isCanonical = isCanonical; const isZero = (buf) => { return new Uint8Array(buf).reduce((acc, v) => acc && (v == 0), true); }; exports.isZero = isZero; const getRandomScalar = () => { for (;;) { const scalar = exports.getRandomBytes(types_1.SCALAR_SIZE); new Uint8Array(scalar)[31] &= 0x1f; if (!exports.isCanonical(scalar) || exports.isZero(scalar)) continue; return scalar; } }; exports.getRandomScalar = getRandomScalar; const getRandomScalars = (cnt) => { const ret = []; for (let i = 0; i < cnt; i++) ret.push(exports.getRandomScalar()); return ret; }; exports.getRandomScalars = getRandomScalars; const getRandomScalarsConcat = (cnt) => { return exports.arrayBufferConcat(exports.getRandomScalars(cnt)); }; exports.getRandomScalarsConcat = getRandomScalarsConcat; //# sourceMappingURL=util.js.map