]> zoso.dev Git - libnemo.git/commitdiff
Start rewriting tweetnacl as module with exports.
authorChris Duncan <chris@zoso.dev>
Sat, 23 Nov 2024 09:47:23 +0000 (01:47 -0800)
committerChris Duncan <chris@zoso.dev>
Sat, 23 Nov 2024 09:47:23 +0000 (01:47 -0800)
src/lib/workers/nano25519.ts

index fe633b7f5f91047741a635ab0b39c022504c5496..de2536c093d28692921bebb67b8cf4b88ced5852 100644 (file)
@@ -3,7 +3,6 @@
 //@ts-nocheck\r
 import blake2b from 'blake2b'\r
 \r
-(function(nacl) {\r
        'use strict';\r
        \r
        // Ported in 2014 by Dmitry Chestnykh and Devi Mandiri.\r
@@ -2071,7 +2070,7 @@ import blake2b from 'blake2b'
                        crypto_sign_SEEDBYTES = 32,\r
                        crypto_hash_BYTES = 64;\r
        \r
-       nacl.lowlevel = {\r
+       export const lowlevel = {\r
                crypto_core_hsalsa20: crypto_core_hsalsa20,\r
                crypto_stream_xor: crypto_stream_xor,\r
                crypto_stream: crypto_stream,\r
@@ -2153,38 +2152,13 @@ import blake2b from 'blake2b'
                for (var i = 0; i < arr.length; i++) arr[i] = 0;\r
        }\r
        \r
-       nacl.randomBytes = function(n) {\r
+       const randomBytes = function(n) {\r
                var b = new Uint8Array(n);\r
                randombytes(b, n);\r
                return b;\r
        };\r
        \r
-       nacl.secretbox = function(msg, nonce, key) {\r
-               checkArrayTypes(msg, nonce, key);\r
-               checkLengths(key, nonce);\r
-               var m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length);\r
-               var c = new Uint8Array(m.length);\r
-               for (var i = 0; i < msg.length; i++) m[i+crypto_secretbox_ZEROBYTES] = msg[i];\r
-               crypto_secretbox(c, m, m.length, nonce, key);\r
-               return c.subarray(crypto_secretbox_BOXZEROBYTES);\r
-       };\r
-       \r
-       nacl.secretbox.open = function(box, nonce, key) {\r
-               checkArrayTypes(box, nonce, key);\r
-               checkLengths(key, nonce);\r
-               var c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length);\r
-               var m = new Uint8Array(c.length);\r
-               for (var i = 0; i < box.length; i++) c[i+crypto_secretbox_BOXZEROBYTES] = box[i];\r
-               if (c.length < 32) return null;\r
-               if (crypto_secretbox_open(m, c, c.length, nonce, key) !== 0) return null;\r
-               return m.subarray(crypto_secretbox_ZEROBYTES);\r
-       };\r
-       \r
-       nacl.secretbox.keyLength = crypto_secretbox_KEYBYTES;\r
-       nacl.secretbox.nonceLength = crypto_secretbox_NONCEBYTES;\r
-       nacl.secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES;\r
-       \r
-       nacl.scalarMult = function(n, p) {\r
+       const scalarMult = function(n, p) {\r
                checkArrayTypes(n, p);\r
                if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size');\r
                if (p.length !== crypto_scalarmult_BYTES) throw new Error('bad p size');\r
@@ -2193,7 +2167,7 @@ import blake2b from 'blake2b'
                return q;\r
        };\r
        \r
-       nacl.scalarMult.base = function(n) {\r
+       scalarMult.base = function(n) {\r
                checkArrayTypes(n);\r
                if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size');\r
                var q = new Uint8Array(crypto_scalarmult_BYTES);\r
@@ -2201,137 +2175,97 @@ import blake2b from 'blake2b'
                return q;\r
        };\r
        \r
-       nacl.scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES;\r
-       nacl.scalarMult.groupElementLength = crypto_scalarmult_BYTES;\r
-       \r
-       nacl.box = function(msg, nonce, publicKey, secretKey) {\r
-               var k = nacl.box.before(publicKey, secretKey);\r
-               return nacl.secretbox(msg, nonce, k);\r
-       };\r
-       \r
-       nacl.box.before = function(publicKey, secretKey) {\r
-               checkArrayTypes(publicKey, secretKey);\r
-               checkBoxLengths(publicKey, secretKey);\r
-               var k = new Uint8Array(crypto_box_BEFORENMBYTES);\r
-               crypto_box_beforenm(k, publicKey, secretKey);\r
-               return k;\r
-       };\r
-       \r
-       nacl.box.after = nacl.secretbox;\r
-       \r
-       nacl.box.open = function(msg, nonce, publicKey, secretKey) {\r
-               var k = nacl.box.before(publicKey, secretKey);\r
-               return nacl.secretbox.open(msg, nonce, k);\r
-       };\r
-       \r
-       nacl.box.open.after = nacl.secretbox.open;\r
-       \r
-       nacl.box.keyPair = function() {\r
-               var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);\r
-               var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);\r
-               crypto_box_keypair(pk, sk);\r
-               return {publicKey: pk, secretKey: sk};\r
-       };\r
-       \r
-       nacl.box.keyPair.fromSecretKey = function(secretKey) {\r
-               checkArrayTypes(secretKey);\r
-               if (secretKey.length !== crypto_box_SECRETKEYBYTES)\r
-                       throw new Error('bad secret key size');\r
-               var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);\r
-               crypto_scalarmult_base(pk, secretKey);\r
-               return {publicKey: pk, secretKey: new Uint8Array(secretKey)};\r
-       };\r
-       \r
-       nacl.box.publicKeyLength = crypto_box_PUBLICKEYBYTES;\r
-       nacl.box.secretKeyLength = crypto_box_SECRETKEYBYTES;\r
-       nacl.box.sharedKeyLength = crypto_box_BEFORENMBYTES;\r
-       nacl.box.nonceLength = crypto_box_NONCEBYTES;\r
-       nacl.box.overheadLength = nacl.secretbox.overheadLength;\r
-       \r
-       nacl.sign = function(msg, secretKey) {\r
-               checkArrayTypes(msg, secretKey);\r
-               if (secretKey.length !== crypto_sign_SECRETKEYBYTES)\r
-                       throw new Error('bad secret key size');\r
-               var signedMsg = new Uint8Array(crypto_sign_BYTES+msg.length);\r
-               crypto_sign(signedMsg, msg, msg.length, secretKey);\r
-               return signedMsg;\r
-       };\r
-       \r
-       nacl.sign.open = function(signedMsg, publicKey) {\r
-               checkArrayTypes(signedMsg, publicKey);\r
-               if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)\r
-                       throw new Error('bad public key size');\r
-               var tmp = new Uint8Array(signedMsg.length);\r
-               var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey);\r
-               if (mlen < 0) return null;\r
-               var m = new Uint8Array(mlen);\r
-               for (var i = 0; i < m.length; i++) m[i] = tmp[i];\r
-               return m;\r
-       };\r
-       \r
-       nacl.sign.detached = function(msg, secretKey) {\r
-               var signedMsg = nacl.sign(msg, secretKey);\r
-               var sig = new Uint8Array(crypto_sign_BYTES);\r
-               for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i];\r
-               return sig;\r
-       };\r
-       \r
-       nacl.sign.detached.verify = function(msg, sig, publicKey) {\r
-               checkArrayTypes(msg, sig, publicKey);\r
-               if (sig.length !== crypto_sign_BYTES)\r
-                       throw new Error('bad signature size');\r
-               if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)\r
-                       throw new Error('bad public key size');\r
-               var sm = new Uint8Array(crypto_sign_BYTES + msg.length);\r
-               var m = new Uint8Array(crypto_sign_BYTES + msg.length);\r
-               var i;\r
-               for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i];\r
-               for (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
-       };\r
-       \r
-       nacl.sign.keyPair = function() {\r
-               var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);\r
-               var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);\r
-               crypto_sign_keypair(pk, sk);\r
-               return {publicKey: pk, secretKey: sk};\r
-       };\r
-       \r
-       nacl.sign.keyPair.fromSecretKey = function(secretKey) {\r
-               checkArrayTypes(secretKey);\r
-               if (secretKey.length !== crypto_sign_SECRETKEYBYTES)\r
-                       throw new Error('bad secret key size');\r
-               var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);\r
-               for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32+i];\r
-               return {publicKey: pk, secretKey: new Uint8Array(secretKey)};\r
-       };\r
+       scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES;\r
+       scalarMult.groupElementLength = crypto_scalarmult_BYTES;\r
+       \r
+       const message = {\r
+               sign: (msg, secretKey) => {\r
+                       checkArrayTypes(msg, secretKey);\r
+                       if (secretKey.length !== crypto_sign_SECRETKEYBYTES)\r
+                               throw new Error('bad secret key size');\r
+                       var signedMsg = new Uint8Array(crypto_sign_BYTES+msg.length);\r
+                       crypto_sign(signedMsg, msg, msg.length, secretKey);\r
+                       return signedMsg;\r
+               },\r
+       \r
+               open: (signedMsg, publicKey) => {\r
+                       checkArrayTypes(signedMsg, publicKey);\r
+                       if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)\r
+                               throw new Error('bad public key size');\r
+                       var tmp = new Uint8Array(signedMsg.length);\r
+                       var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey);\r
+                       if (mlen < 0) return null;\r
+                       var m = new Uint8Array(mlen);\r
+                       for (var i = 0; i < m.length; i++) m[i] = tmp[i];\r
+                       return m;\r
+               },\r
+       \r
+               detach: (msg, secretKey) => {\r
+                       var signedMsg = nacl.sign(msg, secretKey);\r
+                       var sig = new Uint8Array(crypto_sign_BYTES);\r
+                       for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i];\r
+                       return sig;\r
+               },\r
+       \r
+               verify:(msg, sig, publicKey) => {\r
+                       checkArrayTypes(msg, sig, publicKey);\r
+                       if (sig.length !== crypto_sign_BYTES)\r
+                               throw new Error('bad signature size');\r
+                       if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)\r
+                               throw new Error('bad public key size');\r
+                       var sm = new Uint8Array(crypto_sign_BYTES + msg.length);\r
+                       var m = new Uint8Array(crypto_sign_BYTES + msg.length);\r
+                       var i;\r
+                       for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i];\r
+                       for (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
+               }\r
+       }\r
        \r
-       nacl.sign.keyPair.fromSeed = function(seed) {\r
-               checkArrayTypes(seed);\r
-               if (seed.length !== crypto_sign_SEEDBYTES)\r
-                       throw new Error('bad seed size');\r
-               var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);\r
-               var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);\r
-               for (var i = 0; i < 32; i++) sk[i] = seed[i];\r
-               crypto_sign_keypair(pk, sk, true);\r
-               return {publicKey: pk, secretKey: sk};\r
-       };\r
+       const keyPair = {\r
+               create: () => {\r
+                       var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);\r
+                       var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);\r
+                       crypto_sign_keypair(pk, sk);\r
+                       return {publicKey: pk, secretKey: sk};\r
+               },\r
+       \r
+               fromSecretKey: (secretKey) => {\r
+                       checkArrayTypes(secretKey);\r
+                       if (secretKey.length !== crypto_sign_SECRETKEYBYTES)\r
+                               throw new Error('bad secret key size');\r
+                       var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);\r
+                       for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32+i];\r
+                       return {publicKey: pk, secretKey: new Uint8Array(secretKey)};\r
+               },\r
+       \r
+               fromSeed: (seed) => {\r
+                       checkArrayTypes(seed);\r
+                       if (seed.length !== crypto_sign_SEEDBYTES)\r
+                               throw new Error('bad seed size');\r
+                       var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);\r
+                       var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);\r
+                       for (var i = 0; i < 32; i++) sk[i] = seed[i];\r
+                       crypto_sign_keypair(pk, sk, true);\r
+                       return {publicKey: pk, secretKey: sk};\r
+               }\r
+       }\r
        \r
-       nacl.sign.publicKeyLength = crypto_sign_PUBLICKEYBYTES;\r
-       nacl.sign.secretKeyLength = crypto_sign_SECRETKEYBYTES;\r
-       nacl.sign.seedLength = crypto_sign_SEEDBYTES;\r
-       nacl.sign.signatureLength = crypto_sign_BYTES;\r
+       export let publicKeyLength = crypto_sign_PUBLICKEYBYTES;\r
+       export let secretKeyLength = crypto_sign_SECRETKEYBYTES;\r
+       export let seedLength = crypto_sign_SEEDBYTES;\r
+       export let signatureLength = crypto_sign_BYTES;\r
        \r
-       nacl.hash = function(msg) {\r
+       const hash = function(msg) {\r
                checkArrayTypes(msg);\r
                var h = new Uint8Array(crypto_hash_BYTES);\r
                crypto_hash(h, msg, msg.length);\r
                return h;\r
        };\r
        \r
-       nacl.hash.hashLength = crypto_hash_BYTES;\r
+       hash.hashLength = crypto_hash_BYTES;\r
        \r
-       nacl.verify = function(x, y) {\r
+       const verify = function(x, y) {\r
                checkArrayTypes(x, y);\r
                // Zero length arguments are considered not equal.\r
                if (x.length === 0 || y.length === 0) return false;\r
@@ -2339,7 +2273,7 @@ import blake2b from 'blake2b'
                return (vn(x, 0, y, 0, x.length) === 0) ? true : false;\r
        };\r
        \r
-       nacl.setPRNG = function(fn) {\r
+       const setPRNG = function(fn) {\r
                randombytes = fn;\r
        };\r
        \r
@@ -2371,5 +2305,4 @@ import blake2b from 'blake2b'
                }\r
        })();\r
        \r
-       })(typeof module !== 'undefined' && module.exports ? module.exports : (self.nacl = self.nacl || {}));\r
-       
\ No newline at end of file
+       export { keyPair, hash, message, randomBytes, scalarMult  }
\ No newline at end of file