Change module exports syntax
This commit is contained in:
parent
cb620cfb0a
commit
a42fe34ba1
|
@ -202,13 +202,11 @@ local function blake3(iv, flags, msg, len)
|
|||
return table.concat(out):sub(1, len)
|
||||
end
|
||||
|
||||
local mod = {}
|
||||
|
||||
--- Hashes data using BLAKE3.
|
||||
--- @param message string The input message.
|
||||
--- @param len number? The desired hash length, in bytes. Defaults to 32.
|
||||
--- @return string hash The hash.
|
||||
function mod.digest(message, len)
|
||||
local function digest(message, len)
|
||||
expect(1, message, "string")
|
||||
len = expect(2, len, "number", "nil") or 32
|
||||
lassert(len % 1 == 0, "desired output length must be an integer", 2)
|
||||
|
@ -221,7 +219,7 @@ end
|
|||
--- @param message string The input message.
|
||||
--- @param len number? The desired hash length, in bytes. Defaults to 32.
|
||||
--- @return string hash The keyed hash.
|
||||
function mod.digestKeyed(key, message, len)
|
||||
local function digestKeyed(key, message, len)
|
||||
expect(1, key, "string")
|
||||
lassert(#key == 32, "key length must be 32", 2)
|
||||
expect(2, message, "string")
|
||||
|
@ -234,7 +232,7 @@ end
|
|||
--- Makes a context-based key derivation function (KDF).
|
||||
--- @param context string The context for the KDF.
|
||||
--- @return fun(material: string, len: number?): string kdf The KDF.
|
||||
function mod.deriveKey(context)
|
||||
local function deriveKey(context)
|
||||
expect(1, context, "string")
|
||||
local iv = {u8x4(fmt8x4, blake3(IV, DERIVE_KEY_CONTEXT, context, 32), 1)}
|
||||
|
||||
|
@ -250,4 +248,8 @@ function mod.deriveKey(context)
|
|||
end
|
||||
end
|
||||
|
||||
return mod
|
||||
return {
|
||||
digest = digest,
|
||||
digestKeyed = digestKeyed,
|
||||
deriveKey = deriveKey,
|
||||
}
|
||||
|
|
|
@ -11,8 +11,6 @@ local u3x4, fmt3x4 = packing.compileUnpack("<I4I4I4")
|
|||
local p16x4, fmt16x4 = packing.compilePack("<I4I4I4I4I4I4I4I4I4I4I4I4I4I4I4I4")
|
||||
local u16x4 = packing.compileUnpack(fmt16x4)
|
||||
|
||||
local mod = {}
|
||||
|
||||
--- Encrypts/Decrypts data using ChaCha20.
|
||||
--- @param key string A 32-byte random key.
|
||||
--- @param nonce string A 12-byte per-message unique nonce.
|
||||
|
@ -20,7 +18,7 @@ local mod = {}
|
|||
--- @param rounds number? The number of ChaCha20 rounds to use. Defaults to 20.
|
||||
--- @param offset number? The block offset to generate the keystream at. Defaults to 1.
|
||||
--- @return string out The resulting ciphertext or plaintext.
|
||||
function mod.crypt(key, nonce, message, rounds, offset)
|
||||
local function crypt(key, nonce, message, rounds, offset)
|
||||
expect(1, key, "string")
|
||||
lassert(#key == 32, "key length must be 32", 2)
|
||||
expect(2, nonce, "string")
|
||||
|
@ -123,4 +121,6 @@ function mod.crypt(key, nonce, message, rounds, offset)
|
|||
return table.concat(out):sub(1, #message)
|
||||
end
|
||||
|
||||
return mod
|
||||
return {
|
||||
crypt = crypt,
|
||||
}
|
||||
|
|
|
@ -7,12 +7,10 @@ local sha512 = require "ccryptolib.internal.sha512"
|
|||
local ed = require "ccryptolib.internal.edwards25519"
|
||||
local random = require "ccryptolib.random"
|
||||
|
||||
local mod = {}
|
||||
|
||||
--- Computes a public key from a secret key.
|
||||
--- @param sk string A random 32-byte secret key.
|
||||
--- @return string pk The matching 32-byte public key.
|
||||
function mod.publicKey(sk)
|
||||
local function publicKey(sk)
|
||||
expect(1, sk, "string")
|
||||
assert(#sk == 32, "secret key length must be 32")
|
||||
|
||||
|
@ -27,7 +25,7 @@ end
|
|||
--- @param pk string The signer's public key.
|
||||
--- @param msg string The message to be signed.
|
||||
--- @return string sig The 64-byte signature on the message.
|
||||
function mod.sign(sk, pk, msg)
|
||||
local function sign(sk, pk, msg)
|
||||
expect(1, sk, "string")
|
||||
lassert(#sk == 32, "secret key length must be 32", 2)
|
||||
expect(2, pk, "string")
|
||||
|
@ -59,7 +57,7 @@ end
|
|||
--- @param msg string The signed message.
|
||||
--- @param sig string The alleged signature.
|
||||
--- @return boolean valid Whether the signature is valid or not.
|
||||
function mod.verify(pk, msg, sig)
|
||||
local function verify(pk, msg, sig)
|
||||
expect(1, pk, "string")
|
||||
lassert(#pk == 32, "public key length must be 32", 2) --- @cast pk String32
|
||||
expect(2, msg, "string")
|
||||
|
@ -81,4 +79,8 @@ function mod.verify(pk, msg, sig)
|
|||
return ed.encode(rv) == rStr
|
||||
end
|
||||
|
||||
return mod
|
||||
return {
|
||||
publicKey = publicKey,
|
||||
sign = sign,
|
||||
verify = verify,
|
||||
}
|
||||
|
|
|
@ -90,8 +90,6 @@ local function mkUnpack(words, BE)
|
|||
return load(out)()
|
||||
end
|
||||
|
||||
local mod = {}
|
||||
|
||||
-- Check whether string.pack is implemented in a high-speed language.
|
||||
if not string.pack or pcall(string.dump, string.pack) then
|
||||
local function compile(fmt, fn)
|
||||
|
@ -119,7 +117,7 @@ if not string.pack or pcall(string.dump, string.pack) then
|
|||
--- @param fmt string A string matched by `^([><])I[I%d]+$`.
|
||||
--- @return fun(_ignored: any, ...: any): string pack A function that behaves like an unsafe version of `string.pack` for the given format string.
|
||||
--- @return string fmt
|
||||
function mod.compilePack(fmt)
|
||||
local function compilePack(fmt)
|
||||
if not packCache[fmt] then
|
||||
packCache[fmt] = compile(fmt, mkPack)
|
||||
end
|
||||
|
@ -134,26 +132,32 @@ if not string.pack or pcall(string.dump, string.pack) then
|
|||
--- @param fmt string A string matched by `^([><])I[I%d]+$`.
|
||||
--- @return fun(_ignored: any, str: string, pos: number) unpack A function that behaves like an unsafe version of `string.unpack` for the given format string. Note that the third argument isn't optional.
|
||||
--- @return string fmt
|
||||
function mod.compileUnpack(fmt)
|
||||
local function compileUnpack(fmt)
|
||||
if not unpackCache[fmt] then
|
||||
unpackCache[fmt] = compile(fmt, mkUnpack)
|
||||
end
|
||||
return unpackCache[fmt], fmt
|
||||
end
|
||||
|
||||
return mod
|
||||
return {
|
||||
compilePack = compilePack,
|
||||
compileUnpack = compileUnpack,
|
||||
}
|
||||
else
|
||||
--- (string.pack isn't nil) It's string.pack! It returns string.pack!
|
||||
--- @param fmt string
|
||||
--- @return fun(fmt: string, ...: any): string pack string.pack!
|
||||
--- @return string fmt
|
||||
mod.compilePack = function(fmt) return string.pack, fmt end
|
||||
local function compilePack(fmt) return string.pack, fmt end
|
||||
|
||||
--- (string.pack isn't nil) It's string.unpack! It returns string.unpack!
|
||||
--- @param fmt string
|
||||
--- @return fun(fmt: string, str: string, pos: number) unpack string.unpack!
|
||||
--- @return string fmt
|
||||
mod.compileUnpack = function(fmt) return string.unpack, fmt end
|
||||
end
|
||||
local function compileUnpack(fmt) return string.unpack, fmt end
|
||||
|
||||
return mod
|
||||
return {
|
||||
compilePack = compilePack,
|
||||
compileUnpack = compileUnpack,
|
||||
}
|
||||
end
|
||||
|
|
|
@ -7,13 +7,11 @@ local packing = require "ccryptolib.internal.packing"
|
|||
local u4x4, fmt4x4 = packing.compileUnpack("<I4I4I4I4")
|
||||
local p4x4 = packing.compilePack(fmt4x4)
|
||||
|
||||
local mod = {}
|
||||
|
||||
--- Computes a Poly1305 message authentication code.
|
||||
--- @param key string A 32-byte single-use random key.
|
||||
--- @param message string The message to authenticate.
|
||||
--- @return string tag The 16-byte authentication tag.
|
||||
function mod.mac(key, message)
|
||||
local function mac(key, message)
|
||||
expect(1, key, "string")
|
||||
lassert(#key == 32, "key length must be 32", 2)
|
||||
expect(2, message, "string")
|
||||
|
@ -130,4 +128,6 @@ function mod.mac(key, message)
|
|||
return p4x4(fmt4x4, u0, u1 / 2 ^ 32, u2 / 2 ^ 64, u3 / 2 ^ 96)
|
||||
end
|
||||
|
||||
return mod
|
||||
return {
|
||||
mac = mac,
|
||||
}
|
||||
|
|
|
@ -19,11 +19,9 @@ local ctx = {
|
|||
local state = blake3.digest(table.concat(ctx, "|"))
|
||||
local initialized = false
|
||||
|
||||
local mod = {}
|
||||
|
||||
--- Mixes entropy into the generator, and marks it as initialized.
|
||||
--- @param seed string The seed data.
|
||||
function mod.init(seed)
|
||||
local function init(seed)
|
||||
expect(1, seed, "string")
|
||||
state = blake3.digestKeyed(state, seed)
|
||||
initialized = true
|
||||
|
@ -31,14 +29,14 @@ end
|
|||
|
||||
--- Mixes extra entropy into the generator state.
|
||||
--- @param data string The additional entropy to mix.
|
||||
function mod.mix(data)
|
||||
local function mix(data)
|
||||
state = blake3.digestKeyed(state, data)
|
||||
end
|
||||
|
||||
--- Generates random bytes.
|
||||
--- @param len number The desired output length.
|
||||
--- @return string bytes
|
||||
function mod.random(len)
|
||||
local function random(len)
|
||||
expect(1, len, "number")
|
||||
lassert(initialized, "attempt to use an uninitialized random generator", 2)
|
||||
local msg = ("\0"):rep(len + 32)
|
||||
|
@ -48,4 +46,8 @@ function mod.random(len)
|
|||
return out:sub(33)
|
||||
end
|
||||
|
||||
return mod
|
||||
return {
|
||||
init = init,
|
||||
mix = mix,
|
||||
random = random,
|
||||
}
|
||||
|
|
|
@ -5,12 +5,10 @@ local lassert = require "ccryptolib.internal.util".lassert
|
|||
local util = require "ccryptolib.internal.util"
|
||||
local c25 = require "ccryptolib.internal.curve25519"
|
||||
|
||||
local mod = {}
|
||||
|
||||
--- Computes the public key from a secret key.
|
||||
--- @param sk string A random 32-byte secret key.
|
||||
--- @return string pk The matching public key.
|
||||
function mod.publicKey(sk)
|
||||
local function publicKey(sk)
|
||||
expect(1, sk, "string")
|
||||
assert(#sk == 32, "secret key length must be 32")
|
||||
return c25.encode(c25.scale(c25.mulG(util.bits(sk))))
|
||||
|
@ -20,7 +18,7 @@ end
|
|||
--- @param sk string A Curve25519 secret key.
|
||||
--- @param pk string A public key, usually derived from someone else's secret key.
|
||||
--- @return string ss The 32-byte shared secret between both keys.
|
||||
function mod.exchange(sk, pk)
|
||||
local function exchange(sk, pk)
|
||||
expect(1, sk, "string")
|
||||
lassert(#sk == 32, "secret key length must be 32", 2)
|
||||
expect(2, pk, "string")
|
||||
|
@ -33,7 +31,7 @@ end
|
|||
--- @param sk string A Curve25519 secret key
|
||||
--- @param pk string An Edwards25519 public key, usually derived from someone else's secret key.
|
||||
--- @return string ss The 32-byte shared secret between both keys.
|
||||
function mod.exchangeEd(sk, pk)
|
||||
local function exchangeEd(sk, pk)
|
||||
expect(1, sk, "string")
|
||||
lassert(#sk == 32, "secret key length must be 32", 2)
|
||||
expect(2, pk, "string")
|
||||
|
@ -41,4 +39,8 @@ function mod.exchangeEd(sk, pk)
|
|||
return c25.encode(c25.scale(c25.ladder8(c25.decodeEd(pk), util.bits8(sk))))
|
||||
end
|
||||
|
||||
return mod
|
||||
return {
|
||||
publicKey = publicKey,
|
||||
exchange = exchange,
|
||||
exchangeEd = exchangeEd,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue