@hazae41/berith
Version:
WebAssembly port of Ed25519 signatures and X25519 key exchange
785 lines (698 loc) • 22.3 kB
JavaScript
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 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));
}
const heap = new Array(128).fill(undefined);
heap.push(undefined, null, true, false);
let heap_next = heap.length;
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 getObject(idx) { return heap[idx]; }
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;
}
function _assertClass(instance, klass) {
if (!(instance instanceof klass)) {
throw new Error(`expected instance of ${klass.name}`);
}
return instance.ptr;
}
let cachedInt32Memory0 = null;
function getInt32Memory0() {
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
}
let WASM_VECTOR_LEN = 0;
function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1, 1) >>> 0;
getUint8Memory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
function handleError(f, args) {
try {
return f.apply(this, args);
} catch (e) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
}
/**
*/
class Ed25519Signature {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(Ed25519Signature.prototype);
obj.__wbg_ptr = ptr;
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
return ptr;
}
[Symbol.dispose]() {
this.free();
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_ed25519signature_free(ptr);
}
/**
* @param {Memory} bytes
*/
constructor(bytes) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
_assertClass(bytes, Memory);
wasm.ed25519signature_from_bytes(retptr, bytes.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return Ed25519Signature.__wrap(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @param {Memory} bytes
* @returns {Ed25519Signature}
*/
static from_bytes(bytes) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
_assertClass(bytes, Memory);
wasm.ed25519signature_from_bytes(retptr, bytes.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return Ed25519Signature.__wrap(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @returns {Memory}
*/
to_bytes() {
const ret = wasm.ed25519signature_to_bytes(this.__wbg_ptr);
return Memory.__wrap(ret);
}
}
/**
*/
class Ed25519SigningKey {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(Ed25519SigningKey.prototype);
obj.__wbg_ptr = ptr;
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
return ptr;
}
[Symbol.dispose]() {
this.free();
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_ed25519signingkey_free(ptr);
}
/**
*/
constructor() {
const ret = wasm.ed25519signingkey_new();
return Ed25519SigningKey.__wrap(ret);
}
/**
* @returns {Ed25519SigningKey}
*/
static random() {
const ret = wasm.ed25519signingkey_new();
return Ed25519SigningKey.__wrap(ret);
}
/**
* @param {Memory} bytes
* @returns {Ed25519SigningKey}
*/
static from_bytes(bytes) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
_assertClass(bytes, Memory);
wasm.ed25519signingkey_from_bytes(retptr, bytes.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return Ed25519SigningKey.__wrap(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @returns {Memory}
*/
to_bytes() {
const ret = wasm.ed25519signingkey_to_bytes(this.__wbg_ptr);
return Memory.__wrap(ret);
}
/**
* @returns {Ed25519VerifyingKey}
*/
public() {
const ret = wasm.ed25519signingkey_public(this.__wbg_ptr);
return Ed25519VerifyingKey.__wrap(ret);
}
/**
* @param {Memory} bytes
* @returns {Ed25519Signature}
*/
sign(bytes) {
_assertClass(bytes, Memory);
const ret = wasm.ed25519signingkey_sign(this.__wbg_ptr, bytes.__wbg_ptr);
return Ed25519Signature.__wrap(ret);
}
}
/**
*/
class Ed25519VerifyingKey {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(Ed25519VerifyingKey.prototype);
obj.__wbg_ptr = ptr;
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
return ptr;
}
[Symbol.dispose]() {
this.free();
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_ed25519verifyingkey_free(ptr);
}
/**
* @param {Memory} bytes
*/
constructor(bytes) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
_assertClass(bytes, Memory);
wasm.ed25519verifyingkey_from_bytes(retptr, bytes.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return Ed25519VerifyingKey.__wrap(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @param {Memory} bytes
* @returns {Ed25519VerifyingKey}
*/
static from_bytes(bytes) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
_assertClass(bytes, Memory);
wasm.ed25519verifyingkey_from_bytes(retptr, bytes.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return Ed25519VerifyingKey.__wrap(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @returns {Memory}
*/
to_bytes() {
const ret = wasm.ed25519verifyingkey_to_bytes(this.__wbg_ptr);
return Memory.__wrap(ret);
}
/**
* @param {Memory} bytes
* @param {Ed25519Signature} signature
* @returns {boolean}
*/
verify(bytes, signature) {
_assertClass(bytes, Memory);
_assertClass(signature, Ed25519Signature);
const ret = wasm.ed25519verifyingkey_verify(this.__wbg_ptr, bytes.__wbg_ptr, signature.__wbg_ptr);
return ret !== 0;
}
}
/**
*/
class Memory {
static __wrap(ptr, ptr0, len0) {
ptr = ptr >>> 0;
const obj = Object.create(Memory.prototype);
obj.__wbg_ptr = ptr;
obj.__wbg_ptr0 = ptr0;
obj.__wbg_len0 = len0;
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
return ptr;
}
[Symbol.dispose]() {
this.free();
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_memory_free(ptr);
}
/**
* @param {Uint8Array} inner
*/
constructor(inner) {
const ptr0 = passArray8ToWasm0(inner, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.memory_new(ptr0, len0);
return Memory.__wrap(ret, ptr0, len0);
}
/**
* @returns {number}
*/
ptr() {
return this.__wbg_ptr0 ??= wasm.memory_ptr(this.__wbg_ptr);
}
/**
* @returns {number}
*/
len() {
return this.__wbg_len0 ??= wasm.memory_len(this.__wbg_ptr);
}
freeNextTick() {
setTimeout(() => this.free(), 0);
return this;
}
get bytes() {
return getUint8Memory0().subarray(this.ptr(), this.ptr() + this.len());
}
copyAndDispose() {
const bytes = this.bytes.slice();
this.free();
return bytes;
}
}
/**
*/
class X25519PublicKey {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(X25519PublicKey.prototype);
obj.__wbg_ptr = ptr;
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
return ptr;
}
[Symbol.dispose]() {
this.free();
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_x25519publickey_free(ptr);
}
/**
* @param {Memory} bytes
*/
constructor(bytes) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
_assertClass(bytes, Memory);
wasm.x25519publickey_from_bytes(retptr, bytes.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return X25519PublicKey.__wrap(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @param {Memory} bytes
* @returns {X25519PublicKey}
*/
static from_bytes(bytes) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
_assertClass(bytes, Memory);
wasm.x25519publickey_from_bytes(retptr, bytes.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return X25519PublicKey.__wrap(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @returns {Memory}
*/
to_bytes() {
const ret = wasm.ed25519verifyingkey_to_bytes(this.__wbg_ptr);
return Memory.__wrap(ret);
}
}
/**
*/
class X25519SharedSecret {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(X25519SharedSecret.prototype);
obj.__wbg_ptr = ptr;
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
return ptr;
}
[Symbol.dispose]() {
this.free();
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_x25519sharedsecret_free(ptr);
}
/**
* @returns {Memory}
*/
to_bytes() {
const ret = wasm.ed25519verifyingkey_to_bytes(this.__wbg_ptr);
return Memory.__wrap(ret);
}
/**
* @returns {boolean}
*/
was_contributory() {
const ret = wasm.x25519sharedsecret_was_contributory(this.__wbg_ptr);
return ret !== 0;
}
}
/**
*/
class X25519StaticSecret {
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(X25519StaticSecret.prototype);
obj.__wbg_ptr = ptr;
return obj;
}
__destroy_into_raw() {
const ptr = this.__wbg_ptr;
this.__wbg_ptr = 0;
return ptr;
}
[Symbol.dispose]() {
this.free();
}
free() {
const ptr = this.__destroy_into_raw();
wasm.__wbg_x25519staticsecret_free(ptr);
}
/**
*/
constructor() {
const ret = wasm.x25519staticsecret_random();
return X25519StaticSecret.__wrap(ret);
}
/**
* @param {Memory} bytes
* @returns {X25519StaticSecret}
*/
static from_bytes(bytes) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
_assertClass(bytes, Memory);
wasm.x25519staticsecret_from_bytes(retptr, bytes.__wbg_ptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return X25519StaticSecret.__wrap(r0);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
/**
* @returns {Memory}
*/
to_bytes() {
const ret = wasm.x25519staticsecret_to_bytes(this.__wbg_ptr);
return Memory.__wrap(ret);
}
/**
* @param {X25519PublicKey} other
* @returns {X25519SharedSecret}
*/
diffie_hellman(other) {
_assertClass(other, X25519PublicKey);
const ret = wasm.x25519staticsecret_diffie_hellman(this.__wbg_ptr, other.__wbg_ptr);
return X25519SharedSecret.__wrap(ret);
}
/**
* @returns {X25519PublicKey}
*/
to_public() {
const ret = wasm.x25519staticsecret_to_public(this.__wbg_ptr);
return X25519PublicKey.__wrap(ret);
}
}
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.__wbindgen_error_new = function(arg0, arg1) {
const ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
};
imports.wbg.__wbindgen_object_clone_ref = function(arg0) {
const ret = getObject(arg0);
return addHeapObject(ret);
};
imports.wbg.__wbg_crypto_c48a774b022d20ac = function(arg0) {
const ret = getObject(arg0).crypto;
return addHeapObject(ret);
};
imports.wbg.__wbindgen_is_object = function(arg0) {
const val = getObject(arg0);
const ret = typeof(val) === 'object' && val !== null;
return ret;
};
imports.wbg.__wbg_process_298734cf255a885d = function(arg0) {
const ret = getObject(arg0).process;
return addHeapObject(ret);
};
imports.wbg.__wbg_versions_e2e78e134e3e5d01 = function(arg0) {
const ret = getObject(arg0).versions;
return addHeapObject(ret);
};
imports.wbg.__wbg_node_1cd7a5d853dbea79 = function(arg0) {
const ret = getObject(arg0).node;
return addHeapObject(ret);
};
imports.wbg.__wbindgen_is_string = function(arg0) {
const ret = typeof(getObject(arg0)) === 'string';
return ret;
};
imports.wbg.__wbindgen_object_drop_ref = function(arg0) {
takeObject(arg0);
};
imports.wbg.__wbg_msCrypto_bcb970640f50a1e8 = function(arg0) {
const ret = getObject(arg0).msCrypto;
return addHeapObject(ret);
};
imports.wbg.__wbg_newwithlength_f5933855e4f48a19 = function(arg0) {
const ret = new Uint8Array(arg0 >>> 0);
return addHeapObject(ret);
};
imports.wbg.__wbg_require_8f08ceecec0f4fee = function() { return handleError(function () {
const ret = module.require;
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbindgen_is_function = function(arg0) {
const ret = typeof(getObject(arg0)) === 'function';
return ret;
};
imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
const ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
};
imports.wbg.__wbg_call_168da88779e35f61 = function() { return handleError(function (arg0, arg1, arg2) {
const ret = getObject(arg0).call(getObject(arg1), getObject(arg2));
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbg_self_6d479506f72c6a71 = function() { return handleError(function () {
const ret = self.self;
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbg_window_f2557cc78490aceb = function() { return handleError(function () {
const ret = window.window;
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbg_globalThis_7f206bda628d5286 = function() { return handleError(function () {
const ret = globalThis.globalThis;
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbg_global_ba75c50d1cf384f4 = function() { return handleError(function () {
const ret = global.global;
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbindgen_is_undefined = function(arg0) {
const ret = getObject(arg0) === undefined;
return ret;
};
imports.wbg.__wbg_newnoargs_b5b063fc6c2f0376 = function(arg0, arg1) {
const ret = new Function(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
};
imports.wbg.__wbg_call_97ae9d8645dc388b = function() { return handleError(function (arg0, arg1) {
const ret = getObject(arg0).call(getObject(arg1));
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbg_subarray_58ad4efbb5bcb886 = function(arg0, arg1, arg2) {
const ret = getObject(arg0).subarray(arg1 >>> 0, arg2 >>> 0);
return addHeapObject(ret);
};
imports.wbg.__wbg_getRandomValues_37fa2ca9e4e07fab = function() { return handleError(function (arg0, arg1) {
getObject(arg0).getRandomValues(getObject(arg1));
}, arguments) };
imports.wbg.__wbindgen_memory = function() {
const ret = wasm.memory;
return addHeapObject(ret);
};
imports.wbg.__wbg_buffer_3f3d764d4747d564 = function(arg0) {
const ret = getObject(arg0).buffer;
return addHeapObject(ret);
};
imports.wbg.__wbg_new_8c3f0052272a457a = function(arg0) {
const ret = new Uint8Array(getObject(arg0));
return addHeapObject(ret);
};
imports.wbg.__wbg_set_83db9690f9353e79 = function(arg0, arg1, arg2) {
getObject(arg0).set(getObject(arg1), arg2 >>> 0);
};
imports.wbg.__wbg_newwithbyteoffsetandlength_d9aa266703cb98be = function(arg0, arg1, arg2) {
const ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0);
return addHeapObject(ret);
};
imports.wbg.__wbg_randomFillSync_dc1e9a60c158336d = function() { return handleError(function (arg0, arg1) {
getObject(arg0).randomFillSync(takeObject(arg1));
}, arguments) };
imports.wbg.__wbindgen_throw = function(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};
return imports;
}
function __wbg_finalize_init(instance, module) {
wasm = instance.exports;
__wbg_init.__wbindgen_wasm_module = module;
cachedInt32Memory0 = null;
cachedUint8Memory0 = null;
return wasm;
}
function initSync(module) {
if (wasm !== undefined) return wasm;
const imports = __wbg_get_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(input) {
if (wasm !== undefined) return wasm;
if (typeof input === 'undefined') {
throw new Error();
}
const imports = __wbg_get_imports();
if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
input = fetch(input);
}
const { instance, module } = await __wbg_load(await input, imports);
return __wbg_finalize_init(instance, module);
}
export { Ed25519Signature, Ed25519SigningKey, Ed25519VerifyingKey, Memory, X25519PublicKey, X25519SharedSecret, X25519StaticSecret, __wbg_init, __wbg_init as default, initSync };
//# sourceMappingURL=berith.mjs.map