pqc-kyber
Version:
The post-quantum Kyber key exchange algorithm, written in rust and compiled to wasm
658 lines (577 loc) • 18.1 kB
JavaScript
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}
const heap = new Array(128).fill(undefined);
heap.push(undefined, null, true, false);
function getObject(idx) { return heap[idx]; }
let heap_next = heap.length;
function dropObject(idx) {
if (idx < 132) return;
heap[idx] = heap_next;
heap_next = idx;
}
function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}
const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
cachedTextDecoder.decode();
let cachedUint8Memory0 = null;
function getUint8Memory0() {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
}
function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}
function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];
heap[idx] = obj;
return idx;
}
function debugString(val) {
// primitive types
const type = typeof val;
if (type == 'number' || type == 'boolean' || val == null) {
return `${val}`;
}
if (type == 'string') {
return `"${val}"`;
}
if (type == 'symbol') {
const description = val.description;
if (description == null) {
return 'Symbol';
} else {
return `Symbol(${description})`;
}
}
if (type == 'function') {
const name = val.name;
if (typeof name == 'string' && name.length > 0) {
return `Function(${name})`;
} else {
return 'Function';
}
}
// objects
if (Array.isArray(val)) {
const length = val.length;
let debug = '[';
if (length > 0) {
debug += debugString(val[0]);
}
for(let i = 1; i < length; i++) {
debug += ', ' + debugString(val[i]);
}
debug += ']';
return debug;
}
// Test for built-in
const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
let className;
if (builtInMatches.length > 1) {
className = builtInMatches[1];
} else {
// Failed to match the standard '[object ClassName]'
return toString.call(val);
}
if (className == 'Object') {
// we're a user defined class or Object
// JSON.stringify avoids problems with cycles, and is generally much
// easier than looping through ownProperties of `val`.
try {
return 'Object(' + JSON.stringify(val) + ')';
} catch (_) {
return 'Object';
}
}
// errors
if (val instanceof Error) {
return `${val.name}: ${val.message}\n${val.stack}`;
}
// TODO we could test for more things here, like `Set`s and `Map`s.
return className;
}
let WASM_VECTOR_LEN = 0;
const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder;
let cachedTextEncoder = new lTextEncoder('utf-8');
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;
getUint8Memory0().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 = getUint8Memory0();
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 = getUint8Memory0().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);
offset += ret.written;
}
WASM_VECTOR_LEN = offset;
return ptr;
}
let cachedInt32Memory0 = null;
function getInt32Memory0() {
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
}
/**
* @returns {Keys}
*/
export function keypair() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.keypair(retptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return Keys.__wrap(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1, 1) >>> 0;
getUint8Memory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
/**
* @param {Uint8Array} pk
* @returns {Kex}
*/
export function encapsulate(pk) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(pk, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.encapsulate(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return Kex.__wrap(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
function getArrayU8FromWasm0(ptr, len) {
ptr = ptr >>> 0;
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
}
/**
* @param {Uint8Array} ct
* @param {Uint8Array} sk
* @returns {Uint8Array}
*/
export function decapsulate(ct, sk) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(ct, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm0(sk, wasm.__wbindgen_malloc);
const len1 = WASM_VECTOR_LEN;
wasm.decapsulate(retptr, ptr0, len0, ptr1, len1);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
var r3 = getInt32Memory0()[retptr / 4 + 3];
if (r3) {
throw takeObject(r2);
}
var v3 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return v3;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
function handleError(f, args) {
try {
return f.apply(this, args);
} catch (e) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
}
/**
*/
export class Kex {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(Kex.prototype);
obj.__wbg_ptr = ptr;
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_kex_free(ptr);
}
/**
* @param {Uint8Array} public_key
*/
constructor(public_key) {
const ptr0 = passArray8ToWasm0(public_key, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.kex_new(ptr0, len0);
return Kex.__wrap(ret);
}
/**
* @returns {Uint8Array}
*/
get ciphertext() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.kex_ciphertext(retptr, this.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v1 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return v1;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @returns {Uint8Array}
*/
get sharedSecret() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.kex_sharedSecret(retptr, this.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v1 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return v1;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @param {Uint8Array} ciphertext
*/
set ciphertext(ciphertext) {
const ptr0 = passArray8ToWasm0(ciphertext, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.kex_set_ciphertext(this.__wbg_ptr, ptr0, len0);
}
/**
* @param {Uint8Array} sharedSecret
*/
set sharedSecret(sharedSecret) {
const ptr0 = passArray8ToWasm0(sharedSecret, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.kex_set_sharedSecret(this.__wbg_ptr, ptr0, len0);
}
}
/**
*/
export class Keys {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(Keys.prototype);
obj.__wbg_ptr = ptr;
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_keys_free(ptr);
}
/**
*/
constructor() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.keypair(retptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return Keys.__wrap(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @returns {Uint8Array}
*/
get pubkey() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.kex_ciphertext(retptr, this.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v1 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return v1;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @returns {Uint8Array}
*/
get secret() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.kex_sharedSecret(retptr, this.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v1 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return v1;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
}
/**
*/
export class Params {
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
return ptr;
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_params_free(ptr);
}
/**
* @returns {number}
*/
get publicKeyBytes() {
const ret = wasm.__wbg_get_params_publicKeyBytes(this.__wbg_ptr);
return ret >>> 0;
}
/**
* @returns {number}
*/
get secretKeyBytes() {
const ret = wasm.__wbg_get_params_secretKeyBytes(this.__wbg_ptr);
return ret >>> 0;
}
/**
* @returns {number}
*/
get ciphertextBytes() {
const ret = wasm.__wbg_get_params_ciphertextBytes(this.__wbg_ptr);
return ret >>> 0;
}
/**
* @returns {number}
*/
get sharedSecretBytes() {
const ret = wasm.__wbg_get_params_sharedSecretBytes(this.__wbg_ptr);
return ret >>> 0;
}
/**
* @returns {number}
*/
static get publicKeyBytes() {
const ret = wasm.params_publicKeyBytes();
return ret >>> 0;
}
/**
* @returns {number}
*/
static get secretKeyBytes() {
const ret = wasm.params_secretKeyBytes();
return ret >>> 0;
}
/**
* @returns {number}
*/
static get ciphertextBytes() {
const ret = wasm.params_ciphertextBytes();
return ret >>> 0;
}
/**
* @returns {number}
*/
static get sharedSecretBytes() {
const ret = wasm.params_sharedSecretBytes();
return ret >>> 0;
}
}
export function __wbindgen_object_drop_ref(arg0) {
takeObject(arg0);
};
export function __wbindgen_error_new(arg0, arg1) {
const ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
};
export function __wbg_crypto_c48a774b022d20ac(arg0) {
const ret = getObject(arg0).crypto;
return addHeapObject(ret);
};
export function __wbindgen_is_object(arg0) {
const val = getObject(arg0);
const ret = typeof(val) === 'object' && val !== null;
return ret;
};
export function __wbg_process_298734cf255a885d(arg0) {
const ret = getObject(arg0).process;
return addHeapObject(ret);
};
export function __wbg_versions_e2e78e134e3e5d01(arg0) {
const ret = getObject(arg0).versions;
return addHeapObject(ret);
};
export function __wbg_node_1cd7a5d853dbea79(arg0) {
const ret = getObject(arg0).node;
return addHeapObject(ret);
};
export function __wbindgen_is_string(arg0) {
const ret = typeof(getObject(arg0)) === 'string';
return ret;
};
export function __wbg_msCrypto_bcb970640f50a1e8(arg0) {
const ret = getObject(arg0).msCrypto;
return addHeapObject(ret);
};
export function __wbg_require_8f08ceecec0f4fee() { return handleError(function () {
const ret = module.require;
return addHeapObject(ret);
}, arguments) };
export function __wbindgen_is_function(arg0) {
const ret = typeof(getObject(arg0)) === 'function';
return ret;
};
export function __wbindgen_string_new(arg0, arg1) {
const ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
};
export function __wbg_getRandomValues_37fa2ca9e4e07fab() { return handleError(function (arg0, arg1) {
getObject(arg0).getRandomValues(getObject(arg1));
}, arguments) };
export function __wbg_randomFillSync_dc1e9a60c158336d() { return handleError(function (arg0, arg1) {
getObject(arg0).randomFillSync(takeObject(arg1));
}, arguments) };
export function __wbg_newnoargs_2b8b6bd7753c76ba(arg0, arg1) {
const ret = new Function(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
};
export function __wbg_call_95d1ea488d03e4e8() { return handleError(function (arg0, arg1) {
const ret = getObject(arg0).call(getObject(arg1));
return addHeapObject(ret);
}, arguments) };
export function __wbindgen_object_clone_ref(arg0) {
const ret = getObject(arg0);
return addHeapObject(ret);
};
export function __wbg_self_e7c1f827057f6584() { return handleError(function () {
const ret = self.self;
return addHeapObject(ret);
}, arguments) };
export function __wbg_window_a09ec664e14b1b81() { return handleError(function () {
const ret = window.window;
return addHeapObject(ret);
}, arguments) };
export function __wbg_globalThis_87cbb8506fecf3a9() { return handleError(function () {
const ret = globalThis.globalThis;
return addHeapObject(ret);
}, arguments) };
export function __wbg_global_c85a9259e621f3db() { return handleError(function () {
const ret = global.global;
return addHeapObject(ret);
}, arguments) };
export function __wbindgen_is_undefined(arg0) {
const ret = getObject(arg0) === undefined;
return ret;
};
export function __wbg_call_9495de66fdbe016b() { return handleError(function (arg0, arg1, arg2) {
const ret = getObject(arg0).call(getObject(arg1), getObject(arg2));
return addHeapObject(ret);
}, arguments) };
export function __wbg_buffer_cf65c07de34b9a08(arg0) {
const ret = getObject(arg0).buffer;
return addHeapObject(ret);
};
export function __wbg_newwithbyteoffsetandlength_9fb2f11355ecadf5(arg0, arg1, arg2) {
const ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0);
return addHeapObject(ret);
};
export function __wbg_new_537b7341ce90bb31(arg0) {
const ret = new Uint8Array(getObject(arg0));
return addHeapObject(ret);
};
export function __wbg_set_17499e8aa4003ebd(arg0, arg1, arg2) {
getObject(arg0).set(getObject(arg1), arg2 >>> 0);
};
export function __wbg_newwithlength_b56c882b57805732(arg0) {
const ret = new Uint8Array(arg0 >>> 0);
return addHeapObject(ret);
};
export function __wbg_subarray_7526649b91a252a6(arg0, arg1, arg2) {
const ret = getObject(arg0).subarray(arg1 >>> 0, arg2 >>> 0);
return addHeapObject(ret);
};
export function __wbindgen_debug_string(arg0, arg1) {
const ret = debugString(getObject(arg1));
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len1 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len1;
getInt32Memory0()[arg0 / 4 + 0] = ptr1;
};
export function __wbindgen_throw(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};
export function __wbindgen_memory() {
const ret = wasm.memory;
return addHeapObject(ret);
};