return r\r
}\r
\r
- // Pluggable, initialized in high-level API below.\r
- var randombytes = function (x, n) { throw new Error('no PRNG') }\r
-\r
var gf0 = gf(),\r
gf1 = gf([1]),\r
D = gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203]),\r
scalarmult(p, q, s)\r
}\r
\r
- function crypto_sign_keypair (pk, sk, seeded?: boolean) {\r
+ function crypto_sign_keypair (pk: Uint8Array, sk: Uint8Array, seeded?: boolean) {\r
var d = new Uint8Array(64)\r
var p = [gf(), gf(), gf(), gf()]\r
var i\r
\r
- if (!seeded) randombytes(sk, 32)\r
+ if (!seeded) {\r
+ var QUOTA = 65536 // https://w3c.github.io/webcrypto/#Crypto-method-getRandomValues\r
+ var i, n = 32, v = new Uint8Array(n)\r
+ for (i = 0; i < n; i += QUOTA) {\r
+ crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)))\r
+ }\r
+ for (i = 0; i < n; i++) sk[i] = v[i]\r
+ cleanup(v)\r
+ }\r
+\r
crypto_hash(d, sk, 32)\r
d[0] &= 248\r
d[31] &= 127\r
const seedLength = crypto_sign_SEEDBYTES\r
const signatureLength = crypto_sign_BYTES\r
\r
- const setPRNG = function (fn) {\r
- randombytes = fn\r
- };\r
-\r
- (function () {\r
- // Initialize PRNG if environment provides CSPRNG.\r
- // If not, methods calling randombytes will throw.\r
- var crypto = self?.crypto ?? null\r
- if (crypto && crypto.getRandomValues) {\r
- // Browsers.\r
- var QUOTA = 65536\r
- setPRNG(function (x, n) {\r
- var i, v = new Uint8Array(n)\r
- for (i = 0; i < n; i += QUOTA) {\r
- crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)))\r
- }\r
- for (i = 0; i < n; i++) x[i] = v[i]\r
- cleanup(v)\r
- })\r
- } else if (typeof require !== 'undefined') {\r
- // Node.js.\r
- try {\r
- crypto = require('node:crypto')\r
- if (crypto && crypto.randomBytes) {\r
- setPRNG(function (x, n) {\r
- var i, v = crypto.randomBytes(n)\r
- for (i = 0; i < n; i++) x[i] = v[i]\r
- cleanup(v)\r
- })\r
- }\r
- } catch (err) {\r
- throw new Error('failed to load node:crypto')\r
- }\r
- }\r
- })()\r
}\r
\r
export const nacl = {\r