'use strict'\r
\r
import { Blake2b } from '../blake2b.js'\r
+import { WorkerInterface } from '../pool.js'\r
\r
// Ported in 2014 by Dmitry Chestnykh and Devi Mandiri.\r
// Public domain.\r
// See for details: https://docs.nano.org/integration-guides/the-basics/\r
// Original source commit: https://github.com/dchest/tweetnacl-js/blob/71df1d6a1d78236ca3e9f6c788786e21f5a651a6/nacl-fast.js\r
\r
-const n = () => {\r
- const gf = function (init?: number[]): Float64Array {\r
+export class NanoNaCl extends WorkerInterface {\r
+ static gf = function (init?: number[]): Float64Array {\r
const r = new Float64Array(16)\r
if (init) for (let i = 0; i < init.length; i++) r[i] = init[i]\r
return r\r
}\r
\r
- const gf0: Float64Array = gf()\r
- const gf1: Float64Array = gf([1])\r
- const D: Float64Array = gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203])\r
- const D2: Float64Array = gf([0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406])\r
- const X: Float64Array = gf([0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169])\r
- const Y: Float64Array = gf([0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666])\r
- const I: Float64Array = gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83])\r
+ static gf0: Float64Array = this.gf()\r
+ static gf1: Float64Array = this.gf([1])\r
+ static D: Float64Array = this.gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203])\r
+ static D2: Float64Array = this.gf([0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406])\r
+ static X: Float64Array = this.gf([0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169])\r
+ static Y: Float64Array = this.gf([0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666])\r
+ static I: Float64Array = this.gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83])\r
\r
- function vn (x: Uint8Array, xi: number, y: Uint8Array, yi: number, n: number): number {\r
+ static vn (x: Uint8Array, xi: number, y: Uint8Array, yi: number, n: number): number {\r
let d = 0\r
for (let i = 0; i < n; i++) d |= x[xi + i] ^ y[yi + i]\r
return (1 & ((d - 1) >>> 8)) - 1\r
}\r
\r
- function crypto_verify_32 (x: Uint8Array, xi: number, y: Uint8Array, yi: number): number {\r
- return vn(x, xi, y, yi, 32)\r
+ static crypto_verify_32 (x: Uint8Array, xi: number, y: Uint8Array, yi: number): number {\r
+ return this.vn(x, xi, y, yi, 32)\r
}\r
\r
- function set25519 (r: Float64Array, a: Float64Array): void {\r
+ static set25519 (r: Float64Array, a: Float64Array): void {\r
for (let i = 0; i < 16; i++) r[i] = a[i] | 0\r
}\r
\r
- function car25519 (o: Float64Array): void {\r
+ static car25519 (o: Float64Array): void {\r
let v, c = 1\r
for (let i = 0; i < 16; i++) {\r
v = o[i] + c + 65535\r
o[0] += c - 1 + 37 * (c - 1)\r
}\r
\r
- function sel25519 (p: Float64Array, q: Float64Array, b: number): void {\r
+ static sel25519 (p: Float64Array, q: Float64Array, b: number): void {\r
let t\r
const c = ~(b - 1)\r
for (let i = 0; i < 16; i++) {\r
}\r
}\r
\r
- function pack25519 (o: Uint8Array, n: Float64Array): void {\r
+ static pack25519 (o: Uint8Array, n: Float64Array): void {\r
let b: number\r
- const m: Float64Array = gf()\r
- const t: Float64Array = gf()\r
+ const m: Float64Array = this.gf()\r
+ const t: Float64Array = this.gf()\r
for (let i = 0; i < 16; i++) t[i] = n[i]\r
- car25519(t)\r
- car25519(t)\r
- car25519(t)\r
+ this.car25519(t)\r
+ this.car25519(t)\r
+ this.car25519(t)\r
for (let j = 0; j < 2; j++) {\r
m[0] = t[0] - 0xffed\r
for (let i = 1; i < 15; i++) {\r
m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1)\r
b = (m[15] >> 16) & 1\r
m[14] &= 0xffff\r
- sel25519(t, m, 1 - b)\r
+ this.sel25519(t, m, 1 - b)\r
}\r
for (let i = 0; i < 16; i++) {\r
o[2 * i] = t[i] & 0xff\r
}\r
}\r
\r
- function neq25519 (a: Float64Array, b: Float64Array): number {\r
+ static neq25519 (a: Float64Array, b: Float64Array): number {\r
const c = new Uint8Array(32)\r
const d = new Uint8Array(32)\r
- pack25519(c, a)\r
- pack25519(d, b)\r
- return crypto_verify_32(c, 0, d, 0)\r
+ this.pack25519(c, a)\r
+ this.pack25519(d, b)\r
+ return this.crypto_verify_32(c, 0, d, 0)\r
}\r
\r
- function par25519 (a: Float64Array): number {\r
+ static par25519 (a: Float64Array): number {\r
var d = new Uint8Array(32)\r
- pack25519(d, a)\r
+ this.pack25519(d, a)\r
return d[0] & 1\r
}\r
\r
- function unpack25519 (o: Float64Array, n: Uint8Array): void {\r
+ static unpack25519 (o: Float64Array, n: Uint8Array): void {\r
for (let i = 0; i < 16; i++) o[i] = n[2 * i] + (n[2 * i + 1] << 8)\r
o[15] &= 0x7fff\r
}\r
\r
- function A (o: Float64Array, a: Float64Array, b: Float64Array): void {\r
+ static A (o: Float64Array, a: Float64Array, b: Float64Array): void {\r
for (let i = 0; i < 16; i++) o[i] = a[i] + b[i]\r
}\r
\r
- function Z (o: Float64Array, a: Float64Array, b: Float64Array): void {\r
+ static Z (o: Float64Array, a: Float64Array, b: Float64Array): void {\r
for (let i = 0; i < 16; i++) o[i] = a[i] - b[i]\r
}\r
\r
- function M (o: Float64Array, a: Float64Array, b: Float64Array): void {\r
+ static M (o: Float64Array, a: Float64Array, b: Float64Array): void {\r
let v, c,\r
t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0,\r
t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0,\r
o[15] = t15\r
}\r
\r
- function S (o: Float64Array, a: Float64Array): void {\r
- M(o, a, a)\r
+ static S (o: Float64Array, a: Float64Array): void {\r
+ this.M(o, a, a)\r
}\r
\r
- function inv25519 (o: Float64Array, i: Float64Array): void {\r
- const c: Float64Array = gf()\r
+ static inv25519 (o: Float64Array, i: Float64Array): void {\r
+ const c: Float64Array = this.gf()\r
for (let a = 0; a < 16; a++) c[a] = i[a]\r
for (let a = 253; a >= 0; a--) {\r
- S(c, c)\r
- if (a !== 2 && a !== 4) M(c, c, i)\r
+ this.S(c, c)\r
+ if (a !== 2 && a !== 4) this.M(c, c, i)\r
}\r
for (let a = 0; a < 16; a++) o[a] = c[a]\r
}\r
\r
- function pow2523 (o: Float64Array, i: Float64Array): void {\r
- const c: Float64Array = gf()\r
+ static pow2523 (o: Float64Array, i: Float64Array): void {\r
+ const c: Float64Array = this.gf()\r
for (let a = 0; a < 16; a++) c[a] = i[a]\r
for (let a = 250; a >= 0; a--) {\r
- S(c, c)\r
- if (a !== 1) M(c, c, i)\r
+ this.S(c, c)\r
+ if (a !== 1) this.M(c, c, i)\r
}\r
for (let a = 0; a < 16; a++) o[a] = c[a]\r
}\r
\r
// Note: difference from TweetNaCl - BLAKE2b used to hash instead of SHA-512.\r
- function crypto_hash (out: Uint8Array, m: Uint8Array, n: number): number {\r
+ static crypto_hash (out: Uint8Array, m: Uint8Array, n: number): number {\r
const input = new Uint8Array(n)\r
for (let i = 0; i < n; ++i) input[i] = m[i]\r
const hash = new Blake2b(64).update(m).digest()\r
return 0\r
}\r
\r
- function add (p: Float64Array[], q: Float64Array[]): void {\r
- const a: Float64Array = gf()\r
- const b: Float64Array = gf()\r
- const c: Float64Array = gf()\r
- const d: Float64Array = gf()\r
- const e: Float64Array = gf()\r
- const f: Float64Array = gf()\r
- const g: Float64Array = gf()\r
- const h: Float64Array = gf()\r
- const t: Float64Array = gf()\r
-\r
- Z(a, p[1], p[0])\r
- Z(t, q[1], q[0])\r
- M(a, a, t)\r
- A(b, p[0], p[1])\r
- A(t, q[0], q[1])\r
- M(b, b, t)\r
- M(c, p[3], q[3])\r
- M(c, c, D2)\r
- M(d, p[2], q[2])\r
- A(d, d, d)\r
- Z(e, b, a)\r
- Z(f, d, c)\r
- A(g, d, c)\r
- A(h, b, a)\r
-\r
- M(p[0], e, f)\r
- M(p[1], h, g)\r
- M(p[2], g, f)\r
- M(p[3], e, h)\r
- }\r
-\r
- function cswap (p: Float64Array[], q: Float64Array[], b: number): void {\r
+ static add (p: Float64Array[], q: Float64Array[]): void {\r
+ const a: Float64Array = this.gf()\r
+ const b: Float64Array = this.gf()\r
+ const c: Float64Array = this.gf()\r
+ const d: Float64Array = this.gf()\r
+ const e: Float64Array = this.gf()\r
+ const f: Float64Array = this.gf()\r
+ const g: Float64Array = this.gf()\r
+ const h: Float64Array = this.gf()\r
+ const t: Float64Array = this.gf()\r
+\r
+ this.Z(a, p[1], p[0])\r
+ this.Z(t, q[1], q[0])\r
+ this.M(a, a, t)\r
+ this.A(b, p[0], p[1])\r
+ this.A(t, q[0], q[1])\r
+ this.M(b, b, t)\r
+ this.M(c, p[3], q[3])\r
+ this.M(c, c, this.D2)\r
+ this.M(d, p[2], q[2])\r
+ this.A(d, d, d)\r
+ this.Z(e, b, a)\r
+ this.Z(f, d, c)\r
+ this.A(g, d, c)\r
+ this.A(h, b, a)\r
+\r
+ this.M(p[0], e, f)\r
+ this.M(p[1], h, g)\r
+ this.M(p[2], g, f)\r
+ this.M(p[3], e, h)\r
+ }\r
+\r
+ static cswap (p: Float64Array[], q: Float64Array[], b: number): void {\r
for (let i = 0; i < 4; i++) {\r
- sel25519(p[i], q[i], b)\r
+ this.sel25519(p[i], q[i], b)\r
}\r
}\r
\r
- function pack (r: Uint8Array, p: Float64Array[]): void {\r
- const tx: Float64Array = gf()\r
- const ty: Float64Array = gf()\r
- const zi: Float64Array = gf()\r
- inv25519(zi, p[2])\r
- M(tx, p[0], zi)\r
- M(ty, p[1], zi)\r
- pack25519(r, ty)\r
- r[31] ^= par25519(tx) << 7\r
+ static pack (r: Uint8Array, p: Float64Array[]): void {\r
+ const tx: Float64Array = this.gf()\r
+ const ty: Float64Array = this.gf()\r
+ const zi: Float64Array = this.gf()\r
+ this.inv25519(zi, p[2])\r
+ this.M(tx, p[0], zi)\r
+ this.M(ty, p[1], zi)\r
+ this.pack25519(r, ty)\r
+ r[31] ^= this.par25519(tx) << 7\r
}\r
\r
- function scalarmult (p: Float64Array[], q: Float64Array[], s: Uint8Array): void {\r
- set25519(p[0], gf0)\r
- set25519(p[1], gf1)\r
- set25519(p[2], gf1)\r
- set25519(p[3], gf0)\r
+ static scalarmult (p: Float64Array[], q: Float64Array[], s: Uint8Array): void {\r
+ this.set25519(p[0], this.gf0)\r
+ this.set25519(p[1], this.gf1)\r
+ this.set25519(p[2], this.gf1)\r
+ this.set25519(p[3], this.gf0)\r
for (let i = 255; i >= 0; --i) {\r
const b = (s[(i / 8) | 0] >> (i & 7)) & 1\r
- cswap(p, q, b)\r
- add(q, p)\r
- add(p, p)\r
- cswap(p, q, b)\r
+ this.cswap(p, q, b)\r
+ this.add(q, p)\r
+ this.add(p, p)\r
+ this.cswap(p, q, b)\r
}\r
}\r
\r
- function scalarbase (p: Float64Array[], s: Uint8Array): void {\r
- const q: Float64Array[] = [gf(), gf(), gf(), gf()]\r
- set25519(q[0], X)\r
- set25519(q[1], Y)\r
- set25519(q[2], gf1)\r
- M(q[3], X, Y)\r
- scalarmult(p, q, s)\r
+ static scalarbase (p: Float64Array[], s: Uint8Array): void {\r
+ const q: Float64Array[] = [this.gf(), this.gf(), this.gf(), this.gf()]\r
+ this.set25519(q[0], this.X)\r
+ this.set25519(q[1], this.Y)\r
+ this.set25519(q[2], this.gf1)\r
+ this.M(q[3], this.X, this.Y)\r
+ this.scalarmult(p, q, s)\r
}\r
\r
- const L = new Float64Array([0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10])\r
+ static L = new Float64Array([\r
+ 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,\r
+ 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,\r
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\r
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10\r
+ ])\r
\r
- function modL (r: Uint8Array, x: Float64Array): void {\r
+ static modL (r: Uint8Array, x: Float64Array): void {\r
let carry, i, j, k\r
for (i = 63; i >= 32; --i) {\r
carry = 0\r
for (j = i - 32, k = i - 12; j < k; ++j) {\r
- x[j] += carry - 16 * x[i] * L[j - (i - 32)]\r
+ x[j] += carry - 16 * x[i] * this.L[j - (i - 32)]\r
carry = Math.floor((x[j] + 128) / 256)\r
x[j] -= carry * 256\r
}\r
}\r
carry = 0\r
for (j = 0; j < 32; j++) {\r
- x[j] += carry - (x[31] >> 4) * L[j]\r
+ x[j] += carry - (x[31] >> 4) * this.L[j]\r
carry = x[j] >> 8\r
x[j] &= 255\r
}\r
- for (j = 0; j < 32; j++) x[j] -= carry * L[j]\r
+ for (j = 0; j < 32; j++) x[j] -= carry * this.L[j]\r
for (i = 0; i < 32; i++) {\r
x[i + 1] += x[i] >> 8\r
r[i] = x[i] & 255\r
}\r
}\r
\r
- function reduce (r: Uint8Array): void {\r
+ static reduce (r: Uint8Array): void {\r
let x = new Float64Array(64)\r
for (let i = 0; i < 64; i++) x[i] = r[i]\r
for (let i = 0; i < 64; i++) r[i] = 0\r
- modL(r, x)\r
+ this.modL(r, x)\r
}\r
\r
// Note: difference from C - smlen returned, not passed as argument.\r
- function crypto_sign (sm: Uint8Array, m: Uint8Array, n: number, sk: Uint8Array, pk: Uint8Array): number {\r
+ static crypto_sign (sm: Uint8Array, m: Uint8Array, n: number, sk: Uint8Array, pk: Uint8Array): number {\r
const d = new Uint8Array(64)\r
const h = new Uint8Array(64)\r
const r = new Uint8Array(64)\r
const x = new Float64Array(64)\r
- const p: Float64Array[] = [gf(), gf(), gf(), gf()]\r
+ const p: Float64Array[] = [this.gf(), this.gf(), this.gf(), this.gf()]\r
\r
- crypto_hash(d, sk, 32)\r
+ this.crypto_hash(d, sk, 32)\r
d[0] &= 248\r
d[31] &= 127\r
d[31] |= 64\r
for (let i = 0; i < n; i++) sm[64 + i] = m[i]\r
for (let i = 0; i < 32; i++) sm[32 + i] = d[32 + i]\r
\r
- crypto_hash(r, sm.subarray(32), n + 32)\r
- reduce(r)\r
- scalarbase(p, r)\r
- pack(sm, p)\r
+ this.crypto_hash(r, sm.subarray(32), n + 32)\r
+ this.reduce(r)\r
+ this.scalarbase(p, r)\r
+ this.pack(sm, p)\r
\r
for (let i = 0; i < 32; i++) sm[i + 32] = pk[i]\r
- crypto_hash(h, sm, n + 64)\r
- reduce(h)\r
+ this.crypto_hash(h, sm, n + 64)\r
+ this.reduce(h)\r
\r
for (let i = 0; i < 64; i++) x[i] = 0\r
for (let i = 0; i < 32; i++) x[i] = r[i]\r
}\r
}\r
\r
- modL(sm.subarray(32), x)\r
+ this.modL(sm.subarray(32), x)\r
return smlen\r
}\r
\r
- function unpackneg (r: Float64Array[], p: Uint8Array): -1 | 0 {\r
- const t: Float64Array = gf()\r
- const chk: Float64Array = gf()\r
- const num: Float64Array = gf()\r
- const den: Float64Array = gf()\r
- const den2: Float64Array = gf()\r
- const den4: Float64Array = gf()\r
- const den6: Float64Array = gf()\r
-\r
- set25519(r[2], gf1)\r
- unpack25519(r[1], p)\r
- S(num, r[1])\r
- M(den, num, D)\r
- Z(num, num, r[2])\r
- A(den, r[2], den)\r
-\r
- S(den2, den)\r
- S(den4, den2)\r
- M(den6, den4, den2)\r
- M(t, den6, num)\r
- M(t, t, den)\r
-\r
- pow2523(t, t)\r
- M(t, t, num)\r
- M(t, t, den)\r
- M(t, t, den)\r
- M(r[0], t, den)\r
-\r
- S(chk, r[0])\r
- M(chk, chk, den)\r
- if (neq25519(chk, num)) M(r[0], r[0], I)\r
-\r
- S(chk, r[0])\r
- M(chk, chk, den)\r
-\r
- if (neq25519(chk, num)) return -1\r
-\r
- if (par25519(r[0]) === (p[31] >> 7)) Z(r[0], gf0, r[0])\r
- M(r[3], r[0], r[1])\r
+ static unpackneg (r: Float64Array[], p: Uint8Array): -1 | 0 {\r
+ const t: Float64Array = this.gf()\r
+ const chk: Float64Array = this.gf()\r
+ const num: Float64Array = this.gf()\r
+ const den: Float64Array = this.gf()\r
+ const den2: Float64Array = this.gf()\r
+ const den4: Float64Array = this.gf()\r
+ const den6: Float64Array = this.gf()\r
+\r
+ this.set25519(r[2], this.gf1)\r
+ this.unpack25519(r[1], p)\r
+ this.S(num, r[1])\r
+ this.M(den, num, this.D)\r
+ this.Z(num, num, r[2])\r
+ this.A(den, r[2], den)\r
+\r
+ this.S(den2, den)\r
+ this.S(den4, den2)\r
+ this.M(den6, den4, den2)\r
+ this.M(t, den6, num)\r
+ this.M(t, t, den)\r
+\r
+ this.pow2523(t, t)\r
+ this.M(t, t, num)\r
+ this.M(t, t, den)\r
+ this.M(t, t, den)\r
+ this.M(r[0], t, den)\r
+\r
+ this.S(chk, r[0])\r
+ this.M(chk, chk, den)\r
+ if (this.neq25519(chk, num)) this.M(r[0], r[0], this.I)\r
+\r
+ this.S(chk, r[0])\r
+ this.M(chk, chk, den)\r
+\r
+ if (this.neq25519(chk, num)) return -1\r
+\r
+ if (this.par25519(r[0]) === (p[31] >> 7)) this.Z(r[0], this.gf0, r[0])\r
+ this.M(r[3], r[0], r[1])\r
return 0\r
}\r
\r
- function crypto_sign_open (m: Uint8Array, sm: Uint8Array, n: number, pk: Uint8Array): number {\r
+ static crypto_sign_open (m: Uint8Array, sm: Uint8Array, n: number, pk: Uint8Array): number {\r
const t = new Uint8Array(32)\r
const h = new Uint8Array(64)\r
- const p: Float64Array[] = [gf(), gf(), gf(), gf()]\r
- const q: Float64Array[] = [gf(), gf(), gf(), gf()]\r
+ const p: Float64Array[] = [this.gf(), this.gf(), this.gf(), this.gf()]\r
+ const q: Float64Array[] = [this.gf(), this.gf(), this.gf(), this.gf()]\r
\r
if (n < 64) return -1\r
\r
- if (unpackneg(q, pk)) return -1\r
+ if (this.unpackneg(q, pk)) return -1\r
\r
for (let i = 0; i < n; i++) m[i] = sm[i]\r
for (let i = 0; i < 32; i++) m[i + 32] = pk[i]\r
- crypto_hash(h, m, n)\r
- reduce(h)\r
- scalarmult(p, q, h)\r
+ this.crypto_hash(h, m, n)\r
+ this.reduce(h)\r
+ this.scalarmult(p, q, h)\r
\r
- scalarbase(q, sm.subarray(32))\r
- add(p, q)\r
- pack(t, p)\r
+ this.scalarbase(q, sm.subarray(32))\r
+ this.add(p, q)\r
+ this.pack(t, p)\r
\r
n -= 64\r
- if (crypto_verify_32(sm, 0, t, 0)) {\r
+ if (this.crypto_verify_32(sm, 0, t, 0)) {\r
for (let i = 0; i < n; i++) m[i] = 0\r
return -1\r
}\r
return n\r
}\r
\r
- const crypto_sign_BYTES = 64\r
- const crypto_sign_PUBLICKEYBYTES = 32\r
- const crypto_sign_SECRETKEYBYTES = 32\r
- const crypto_sign_SEEDBYTES = 32\r
+ static crypto_sign_BYTES = 64\r
+ static crypto_sign_PUBLICKEYBYTES = 32\r
+ static crypto_sign_SECRETKEYBYTES = 32\r
+ static crypto_sign_SEEDBYTES = 32\r
\r
/* High-level API */\r
\r
- function checkArrayTypes (...args: Uint8Array[]): void {\r
+ static checkArrayTypes (...args: Uint8Array[]): void {\r
for (let i = 0; i < args.length; i++) {\r
if (!(args[i] instanceof Uint8Array))\r
throw new TypeError(`expected Uint8Array; received ${args[i].constructor?.name ?? typeof args[i]}`)\r
}\r
}\r
\r
- function parseHex (hex: string): Uint8Array {\r
+ static parseHex (hex: string): Uint8Array {\r
if (hex.length % 2 === 1) hex = `0${hex}`\r
const arr = hex.match(/.{1,2}/g)?.map(byte => parseInt(byte, 16))\r
return Uint8Array.from(arr ?? [])\r
}\r
\r
- function hexify (buf: Uint8Array): string {\r
+ static hexify (buf: Uint8Array): string {\r
let str = ''\r
for (let i = 0; i < buf.length; i++) {\r
if (typeof buf[i] !== 'number')\r
return str\r
}\r
\r
- const sign = function (msg: Uint8Array, secretKey: Uint8Array): Uint8Array {\r
- checkArrayTypes(msg, secretKey)\r
- if (secretKey.length !== crypto_sign_SECRETKEYBYTES)\r
+ static sign (msg: Uint8Array, secretKey: Uint8Array): Uint8Array {\r
+ this.checkArrayTypes(msg, secretKey)\r
+ if (secretKey.length !== this.crypto_sign_SECRETKEYBYTES)\r
throw new Error('bad secret key size')\r
- var signedMsg = new Uint8Array(crypto_sign_BYTES + msg.length)\r
- const publicKey = parseHex(convert(secretKey))\r
- crypto_sign(signedMsg, msg, msg.length, secretKey, publicKey)\r
+ var signedMsg = new Uint8Array(this.crypto_sign_BYTES + msg.length)\r
+ const publicKey = this.parseHex(this.convert(secretKey))\r
+ this.crypto_sign(signedMsg, msg, msg.length, secretKey, publicKey)\r
return signedMsg\r
}\r
\r
- const open = function (signedMsg: Uint8Array, publicKey: Uint8Array): Uint8Array {\r
- checkArrayTypes(signedMsg, publicKey)\r
- if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)\r
+ static open (signedMsg: Uint8Array, publicKey: Uint8Array): Uint8Array {\r
+ this.checkArrayTypes(signedMsg, publicKey)\r
+ if (publicKey.length !== this.crypto_sign_PUBLICKEYBYTES)\r
throw new Error('bad public key size')\r
const tmp = new Uint8Array(signedMsg.length)\r
- var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey)\r
+ var mlen = this.crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey)\r
\r
if (mlen < 0) return new Uint8Array(0)\r
\r
return m\r
}\r
\r
- const detached = function (msg: Uint8Array, secretKey: Uint8Array): string {\r
- var signedMsg = sign(msg, secretKey)\r
- var sig = new Uint8Array(crypto_sign_BYTES)\r
+ static detached (msg: Uint8Array, secretKey: Uint8Array): string {\r
+ var signedMsg = this.sign(msg, secretKey)\r
+ var sig = new Uint8Array(this.crypto_sign_BYTES)\r
for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i]\r
- return hexify(sig).toUpperCase()\r
+ return this.hexify(sig).toUpperCase()\r
}\r
\r
- const verify = function (msg: Uint8Array, sig: Uint8Array, publicKey: Uint8Array): boolean {\r
- checkArrayTypes(msg, sig, publicKey)\r
- if (sig.length !== crypto_sign_BYTES)\r
+ static verify (msg: Uint8Array, sig: Uint8Array, publicKey: Uint8Array): boolean {\r
+ this.checkArrayTypes(msg, sig, publicKey)\r
+ if (sig.length !== this.crypto_sign_BYTES)\r
throw new Error('bad signature size')\r
- if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)\r
+ if (publicKey.length !== this.crypto_sign_PUBLICKEYBYTES)\r
throw new Error('bad public key size')\r
- const sm = new Uint8Array(crypto_sign_BYTES + msg.length)\r
- const m = new Uint8Array(crypto_sign_BYTES + msg.length)\r
- for (let i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i]\r
- for (let i = 0; i < msg.length; i++) sm[i + crypto_sign_BYTES] = msg[i]\r
- return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0)\r
+ const sm = new Uint8Array(this.crypto_sign_BYTES + msg.length)\r
+ const m = new Uint8Array(this.crypto_sign_BYTES + msg.length)\r
+ for (let i = 0; i < this.crypto_sign_BYTES; i++) sm[i] = sig[i]\r
+ for (let i = 0; i < msg.length; i++) sm[i + this.crypto_sign_BYTES] = msg[i]\r
+ return (this.crypto_sign_open(m, sm, sm.length, publicKey) >= 0)\r
}\r
\r
- const convert = function (seed: string | Uint8Array): string {\r
- if (typeof seed === 'string') seed = parseHex(seed)\r
- checkArrayTypes(seed)\r
- if (seed.length !== crypto_sign_SEEDBYTES)\r
+ static convert (seed: string | Uint8Array): string {\r
+ if (typeof seed === 'string') seed = this.parseHex(seed)\r
+ this.checkArrayTypes(seed)\r
+ if (seed.length !== this.crypto_sign_SEEDBYTES)\r
throw new Error('bad seed size')\r
\r
- const pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES)\r
- const p: Float64Array[] = [gf(), gf(), gf(), gf()]\r
+ const pk = new Uint8Array(this.crypto_sign_PUBLICKEYBYTES)\r
+ const p: Float64Array[] = [this.gf(), this.gf(), this.gf(), this.gf()]\r
\r
const hash = new Blake2b(64).update(seed).digest()\r
hash[0] &= 248\r
hash[31] &= 127\r
hash[31] |= 64\r
\r
- scalarbase(p, hash)\r
- pack(pk, p)\r
+ this.scalarbase(p, hash)\r
+ this.pack(pk, p)\r
\r
- return hexify(pk).toUpperCase()\r
+ return this.hexify(pk).toUpperCase()\r
}\r
\r
- return { sign, open, detached, verify, convert }\r
+ static async work (data: any[]): Promise<any[]> {\r
+ return new Promise(async (resolve, reject): Promise<void> => {\r
+ for (let d of data) {\r
+ try {\r
+ d.publicKey = await this.convert(d.privateKey)\r
+ } catch (err) {\r
+ reject(err)\r
+ }\r
+ }\r
+ resolve(data)\r
+ })\r
+ }\r
}\r
\r
-export const NanoNaCl = n()\r
-\r
-const start = n.toString().indexOf('{') + 1\r
-const end = n.toString().lastIndexOf('}')\r
-const blake2b = `const Blake2b = ${Blake2b}\n`\r
-export const worker = `${blake2b}${n.toString().substring(start, end)}`\r
+export default `\r
+ const Blake2b = ${Blake2b}\r
+ const WorkerInterface = ${WorkerInterface}\r
+ const NanoNaCl = ${NanoNaCl}\r
+`\r