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d,CApJA,WAAW,UAAY,oBAAoB,KAC3C,WAAW,QAAU,0BAA0B,KAqJ/C,MAAM,MACF,QACA,WAAA,GACI,KAAK,QAAU,QAAQ,SAC3B,CACA,IAAA,GACI,IAAI,EACJ,MAAM,EAAI,IAAI,SAAQ,IAClB,EAAW,IAAM,GAAS,IAExB,EAAK,KAAK,QAAQ,MAAK,IAAM,IAEnC,OADA,KAAK,QAAU,EACR,CACX,EAGJ,MAAM,mBAAqB,IAAI,qBAChB","file":"/npm/@dashlane/pqc-kem-kyber1024-node@1.0.0/dist/kem.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@dashlane/pqc-kem-kyber1024-node@1.0.0/dist/kem.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import * as createWASMKEMNativeCaller from './kem.wasm.js';\nimport * as createJSKEMNativeCaller from './kem.asm.js';\n// https://github.com/emscripten-core/emscripten/issues/11792#issuecomment-877120580\n/* nodeblock:start */\nimport { dirname } from 'path';\nimport { createRequire } from 'module';\nglobalThis.__dirname = dirname(import.meta.url);\nglobalThis.require = createRequire(import.meta.url);\n/* nodeblock:end */\n{ } // So the comment above is not dropped during transpilation\nasync function kemBuilder(useFallback = false, wasmFilePath = undefined) {\n    let Module = {};\n    if (wasmFilePath) {\n        Module.locateFile = () => {\n            return wasmFilePath;\n        };\n    }\n    if (useFallback) {\n        Module = (await createJSKEMNativeCaller.default(Module));\n    }\n    else {\n        try {\n            Module = (await createWASMKEMNativeCaller.default(Module));\n        }\n        catch (err) {\n            console.error('Failed to initialize KEM WASM, using fallback instead', err);\n            Module = (await createJSKEMNativeCaller.default(Module));\n        }\n    }\n    // This block is only needed when running on Node.js to avoid usage of `require` in libsodium\n    // cf https://github.com/jedisct1/libsodium/issues/1180\n    /* nodeblock:start */\n    {\n        const crypto = await import('crypto');\n        const randomValueNodeJS = () => {\n            const buf = crypto.randomBytes(4);\n            return ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]) >>> 0;\n        };\n        randomValueNodeJS();\n        Module.getRandomValue = randomValueNodeJS;\n        // @ts-ignore\n        const { subtle } = crypto.webcrypto;\n        Module.subtleCrypto = subtle;\n    }\n    /* nodeblock:end */\n    if (!Module.subtleCrypto) {\n        Module.subtleCrypto = self.crypto.subtle;\n    }\n    function dataReturn(returnValue, result) {\n        if (returnValue === 0) {\n            return result;\n        }\n        else {\n            throw new Error(`KEM error: ${returnValue}`);\n        }\n    }\n    function dataResult(buffer, size) {\n        return new Uint8Array(new Uint8Array(Module.HEAPU8.buffer, buffer, size));\n    }\n    function dataFree(buffer) {\n        try {\n            Module._free(buffer);\n        }\n        catch (err) {\n            setTimeout(() => {\n                throw err;\n            }, 0);\n        }\n    }\n    let publicKeyBytes, privateKeyBytes, ciphertextBytes, sharedSecretBytes;\n    const initiated = Module.ready.then(() => {\n        Module._kem_init();\n        publicKeyBytes = Module._kem_public_key_bytes();\n        privateKeyBytes = Module._kem_private_key_bytes();\n        ciphertextBytes = Module._kem_ciphertext_bytes();\n        sharedSecretBytes = Module._kem_shared_secret_bytes();\n    });\n    return {\n        publicKeyBytes: initiated.then(() => {\n            return publicKeyBytes;\n        }),\n        privateKeyBytes: initiated.then(() => {\n            return privateKeyBytes;\n        }),\n        ciphertextBytes: initiated.then(() => {\n            return ciphertextBytes;\n        }),\n        sharedSecretBytes: initiated.then(() => {\n            return sharedSecretBytes;\n        }),\n        keypair: async () => {\n            const release = await bindingCallerMutex.lock();\n            return initiated.then(async () => {\n                const publicKeyBuffer = Module._malloc(publicKeyBytes);\n                const privateKeyBuffer = Module._malloc(privateKeyBytes);\n                try {\n                    const returnValue = await Module.ccall('kem_keypair', 'number', ['number', 'number'], [publicKeyBuffer, privateKeyBuffer], { async: true });\n                    return dataReturn(returnValue, {\n                        publicKey: dataResult(publicKeyBuffer, publicKeyBytes),\n                        privateKey: dataResult(privateKeyBuffer, privateKeyBytes),\n                    });\n                }\n                finally {\n                    release();\n                    dataFree(publicKeyBuffer);\n                    dataFree(privateKeyBuffer);\n                }\n            });\n        },\n        encapsulate: async (publicKey) => {\n            const release = await bindingCallerMutex.lock();\n            return initiated.then(async () => {\n                const ciphertextBuffer = Module._malloc(ciphertextBytes);\n                const sharedSecretBuffer = Module._malloc(sharedSecretBytes);\n                const publicKeyBuffer = Module._malloc(publicKeyBytes);\n                Module.writeArrayToMemory(publicKey, publicKeyBuffer);\n                try {\n                    const returnValue = await Module.ccall('kem_encapsulate', 'number', ['number', 'number', 'number'], [ciphertextBuffer, sharedSecretBuffer, publicKeyBuffer], { async: true });\n                    return dataReturn(returnValue, {\n                        ciphertext: dataResult(ciphertextBuffer, ciphertextBytes),\n                        sharedSecret: dataResult(sharedSecretBuffer, sharedSecretBytes),\n                    });\n                }\n                finally {\n                    release();\n                    dataFree(ciphertextBuffer);\n                    dataFree(sharedSecretBuffer);\n                    dataFree(publicKeyBuffer);\n                }\n            });\n        },\n        decapsulate: async (ciphertext, privateKey) => {\n            const release = await bindingCallerMutex.lock();\n            return initiated.then(async () => {\n                const sharedSecretBuffer = Module._malloc(sharedSecretBytes);\n                const ciphertextBuffer = Module._malloc(ciphertextBytes);\n                const privateKeyBuffer = Module._malloc(privateKeyBytes);\n                Module.writeArrayToMemory(ciphertext, ciphertextBuffer);\n                Module.writeArrayToMemory(privateKey, privateKeyBuffer);\n                try {\n                    const returnValue = await Module.ccall('kem_decapsulate', 'number', ['number', 'number', 'number'], [sharedSecretBuffer, ciphertextBuffer, privateKeyBuffer], { async: true });\n                    return dataReturn(returnValue, {\n                        sharedSecret: dataResult(sharedSecretBuffer, sharedSecretBytes),\n                    });\n                }\n                finally {\n                    release();\n                    dataFree(sharedSecretBuffer);\n                    dataFree(ciphertextBuffer);\n                    dataFree(privateKeyBuffer);\n                }\n            });\n        },\n    };\n}\n// Taken from https://stackoverflow.com/questions/51086688/mutex-in-javascript-does-this-look-like-a-correct-implementation\nclass Mutex {\n    current;\n    constructor() {\n        this.current = Promise.resolve();\n    }\n    lock() {\n        let _resolve;\n        const p = new Promise(resolve => {\n            _resolve = () => resolve();\n        });\n        const rv = this.current.then(() => _resolve);\n        this.current = p;\n        return rv;\n    }\n}\n// Only one call to the library can fly at once, the mutex below restricts the number of concurrent calls\nconst bindingCallerMutex = new Mutex();\nexport default kemBuilder;\n"]}