@reclaimprotocol/zk-symmetric-crypto
Version:
JS Wrappers for Various ZK Snark Circuits
414 lines (413 loc) • 14.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SymmetricCryptoAlgorithm = void 0;
exports.load_circuit = load_circuit;
exports.load_solver = load_solver;
exports.is_circuit_loaded = is_circuit_loaded;
exports.is_solver_loaded = is_solver_loaded;
exports.prove = prove;
exports.verify = verify;
exports.initSync = initSync;
let wasm;
const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available'); } });
if (typeof TextDecoder !== 'undefined') {
cachedTextDecoder.decode();
}
;
let cachedUint8ArrayMemory0 = null;
function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8ArrayMemory0;
}
function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}
let WASM_VECTOR_LEN = 0;
function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1, 1) >>> 0;
getUint8ArrayMemory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
/**
* @param {SymmetricCryptoAlgorithm} alg
* @param {Uint8Array} bytes
*/
function load_circuit(alg, bytes) {
const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.load_circuit(alg, ptr0, len0);
}
/**
* @param {SymmetricCryptoAlgorithm} alg
* @param {Uint8Array} bytes
*/
function load_solver(alg, bytes) {
const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.load_solver(alg, ptr0, len0);
}
/**
* @param {SymmetricCryptoAlgorithm} alg
* @returns {boolean}
*/
function is_circuit_loaded(alg) {
const ret = wasm.is_circuit_loaded(alg);
return ret !== 0;
}
/**
* @param {SymmetricCryptoAlgorithm} alg
* @returns {boolean}
*/
function is_solver_loaded(alg) {
const ret = wasm.is_solver_loaded(alg);
return ret !== 0;
}
function getArrayU8FromWasm0(ptr, len) {
ptr = ptr >>> 0;
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
}
/**
* @param {SymmetricCryptoAlgorithm} alg
* @param {Uint8Array} priv_input_bits
* @param {Uint8Array} pub_input_bits
* @returns {Uint8Array}
*/
function prove(alg, priv_input_bits, pub_input_bits) {
const ptr0 = passArray8ToWasm0(priv_input_bits, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm0(pub_input_bits, wasm.__wbindgen_malloc);
const len1 = WASM_VECTOR_LEN;
const ret = wasm.prove(alg, ptr0, len0, ptr1, len1);
var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
return v3;
}
/**
* @param {SymmetricCryptoAlgorithm} alg
* @param {Uint8Array} pub_input_bits
* @param {Uint8Array} proof_data
* @returns {boolean}
*/
function verify(alg, pub_input_bits, proof_data) {
const ptr0 = passArray8ToWasm0(pub_input_bits, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm0(proof_data, wasm.__wbindgen_malloc);
const len1 = WASM_VECTOR_LEN;
const ret = wasm.verify(alg, ptr0, len0, ptr1, len1);
return ret !== 0;
}
function addToExternrefTable0(obj) {
const idx = wasm.__externref_table_alloc();
wasm.__wbindgen_export_0.set(idx, obj);
return idx;
}
function handleError(f, args) {
try {
return f.apply(this, args);
}
catch (e) {
const idx = addToExternrefTable0(e);
wasm.__wbindgen_exn_store(idx);
}
}
const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available'); } });
const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
? function (arg, view) {
return cachedTextEncoder.encodeInto(arg, view);
}
: function (arg, view) {
const buf = cachedTextEncoder.encode(arg);
view.set(buf);
return {
read: arg.length,
written: buf.length
};
});
function passStringToWasm0(arg, malloc, realloc) {
if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 1) >>> 0;
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}
let len = arg.length;
let ptr = malloc(len, 1) >>> 0;
const mem = getUint8ArrayMemory0();
let offset = 0;
for (; offset < len; offset++) {
const code = arg.charCodeAt(offset);
if (code > 0x7F)
break;
mem[ptr + offset] = code;
}
if (offset !== len) {
if (offset !== 0) {
arg = arg.slice(offset);
}
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);
offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0;
}
WASM_VECTOR_LEN = offset;
return ptr;
}
let cachedDataViewMemory0 = null;
function getDataViewMemory0() {
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
}
return cachedDataViewMemory0;
}
exports.SymmetricCryptoAlgorithm = Object.freeze({ ChaCha20: 0, "0": "ChaCha20", });
async function __wbg_load(module, imports) {
if (typeof Response === 'function' && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === 'function') {
try {
return await WebAssembly.instantiateStreaming(module, imports);
}
catch (e) {
if (module.headers.get('Content-Type') != 'application/wasm') {
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
}
else {
throw e;
}
}
}
const bytes = await module.arrayBuffer();
return await WebAssembly.instantiate(bytes, imports);
}
else {
const instance = await WebAssembly.instantiate(module, imports);
if (instance instanceof WebAssembly.Instance) {
return { instance, module };
}
else {
return instance;
}
}
}
function __wbg_get_imports() {
const imports = {};
imports.wbg = {};
imports.wbg.__wbg_crypto_1d1f22824a6a080c = function (arg0) {
const ret = arg0.crypto;
return ret;
};
imports.wbg.__wbindgen_is_object = function (arg0) {
const val = arg0;
const ret = typeof (val) === 'object' && val !== null;
return ret;
};
imports.wbg.__wbg_process_4a72847cc503995b = function (arg0) {
const ret = arg0.process;
return ret;
};
imports.wbg.__wbg_versions_f686565e586dd935 = function (arg0) {
const ret = arg0.versions;
return ret;
};
imports.wbg.__wbg_node_104a2ff8d6ea03a2 = function (arg0) {
const ret = arg0.node;
return ret;
};
imports.wbg.__wbindgen_is_string = function (arg0) {
const ret = typeof (arg0) === 'string';
return ret;
};
imports.wbg.__wbg_require_cca90b1a94a0255b = function () {
return handleError(function () {
const ret = module.require;
return ret;
}, arguments);
};
imports.wbg.__wbindgen_is_function = function (arg0) {
const ret = typeof (arg0) === 'function';
return ret;
};
imports.wbg.__wbindgen_string_new = function (arg0, arg1) {
const ret = getStringFromWasm0(arg0, arg1);
return ret;
};
imports.wbg.__wbg_msCrypto_eb05e62b530a1508 = function (arg0) {
const ret = arg0.msCrypto;
return ret;
};
imports.wbg.__wbg_randomFillSync_5c9c955aa56b6049 = function () {
return handleError(function (arg0, arg1) {
arg0.randomFillSync(arg1);
}, arguments);
};
imports.wbg.__wbg_getRandomValues_3aa56aa6edec874c = function () {
return handleError(function (arg0, arg1) {
arg0.getRandomValues(arg1);
}, arguments);
};
imports.wbg.__wbg_new_abda76e883ba8a5f = function () {
const ret = new Error();
return ret;
};
imports.wbg.__wbg_stack_658279fe44541cf6 = function (arg0, arg1) {
const ret = arg1.stack;
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len1 = WASM_VECTOR_LEN;
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
};
imports.wbg.__wbg_error_f851667af71bcfc6 = function (arg0, arg1) {
let deferred0_0;
let deferred0_1;
try {
deferred0_0 = arg0;
deferred0_1 = arg1;
console.error(getStringFromWasm0(arg0, arg1));
}
finally {
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
}
};
imports.wbg.__wbg_newnoargs_1ede4bf2ebbaaf43 = function (arg0, arg1) {
const ret = new Function(getStringFromWasm0(arg0, arg1));
return ret;
};
imports.wbg.__wbg_call_a9ef466721e824f2 = function () {
return handleError(function (arg0, arg1) {
const ret = arg0.call(arg1);
return ret;
}, arguments);
};
imports.wbg.__wbg_self_bf91bf94d9e04084 = function () {
return handleError(function () {
const ret = self.self;
return ret;
}, arguments);
};
imports.wbg.__wbg_window_52dd9f07d03fd5f8 = function () {
return handleError(function () {
const ret = window.window;
return ret;
}, arguments);
};
imports.wbg.__wbg_globalThis_05c129bf37fcf1be = function () {
return handleError(function () {
const ret = globalThis.globalThis;
return ret;
}, arguments);
};
imports.wbg.__wbg_global_3eca19bb09e9c484 = function () {
return handleError(function () {
const ret = global.global;
return ret;
}, arguments);
};
imports.wbg.__wbindgen_is_undefined = function (arg0) {
const ret = arg0 === undefined;
return ret;
};
imports.wbg.__wbg_call_3bfa248576352471 = function () {
return handleError(function (arg0, arg1, arg2) {
const ret = arg0.call(arg1, arg2);
return ret;
}, arguments);
};
imports.wbg.__wbg_buffer_ccaed51a635d8a2d = function (arg0) {
const ret = arg0.buffer;
return ret;
};
imports.wbg.__wbg_newwithbyteoffsetandlength_7e3eb787208af730 = function (arg0, arg1, arg2) {
const ret = new Uint8Array(arg0, arg1 >>> 0, arg2 >>> 0);
return ret;
};
imports.wbg.__wbg_new_fec2611eb9180f95 = function (arg0) {
const ret = new Uint8Array(arg0);
return ret;
};
imports.wbg.__wbg_set_ec2fcf81bc573fd9 = function (arg0, arg1, arg2) {
arg0.set(arg1, arg2 >>> 0);
};
imports.wbg.__wbg_newwithlength_76462a666eca145f = function (arg0) {
const ret = new Uint8Array(arg0 >>> 0);
return ret;
};
imports.wbg.__wbg_subarray_975a06f9dbd16995 = function (arg0, arg1, arg2) {
const ret = arg0.subarray(arg1 >>> 0, arg2 >>> 0);
return ret;
};
imports.wbg.__wbindgen_throw = function (arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};
imports.wbg.__wbindgen_memory = function () {
const ret = wasm.memory;
return ret;
};
imports.wbg.__wbindgen_init_externref_table = function () {
const table = wasm.__wbindgen_export_0;
const offset = table.grow(4);
table.set(0, undefined);
table.set(offset + 0, undefined);
table.set(offset + 1, null);
table.set(offset + 2, true);
table.set(offset + 3, false);
;
};
return imports;
}
function __wbg_init_memory(imports, memory) {
}
function __wbg_finalize_init(instance, module) {
wasm = instance.exports;
__wbg_init.__wbindgen_wasm_module = module;
cachedDataViewMemory0 = null;
cachedUint8ArrayMemory0 = null;
wasm.__wbindgen_start();
return wasm;
}
function initSync(module) {
if (wasm !== undefined)
return wasm;
if (typeof module !== 'undefined') {
if (Object.getPrototypeOf(module) === Object.prototype) {
({ module } = module);
}
else {
console.warn('using deprecated parameters for `initSync()`; pass a single object instead');
}
}
const imports = __wbg_get_imports();
__wbg_init_memory(imports);
if (!(module instanceof WebAssembly.Module)) {
module = new WebAssembly.Module(module);
}
const instance = new WebAssembly.Instance(module, imports);
return __wbg_finalize_init(instance, module);
}
async function __wbg_init(module_or_path) {
if (wasm !== undefined)
return wasm;
if (typeof module_or_path !== 'undefined') {
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
({ module_or_path } = module_or_path);
}
else {
console.warn('using deprecated parameters for the initialization function; pass a single object instead');
}
}
if (typeof module_or_path === 'undefined') {
module_or_path = new URL('expander_symmetric_crypto_bg.wasm', ''); // NOTE: have manually changed
}
const imports = __wbg_get_imports();
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
module_or_path = fetch(module_or_path);
}
__wbg_init_memory(imports);
const { instance, module } = await __wbg_load(await module_or_path, imports);
return __wbg_finalize_init(instance, module);
}
exports.default = __wbg_init;