DoxigAlpha

encode

Encode a length according to NIST SP 800-185.

Function parameters

Parameters

#
len:usize

The NIST SP 800-185 encoded length format.

Types

#
NistLengthEncoding
The NIST SP 800-185 encoded length format.

TurboSHAKE128 is a XOF (a secure hash function with a variable output length), with a 128 bit security level.

Functions

#
TurboShake128
TurboSHAKE128 is a XOF (a secure hash function with a variable output length), with a 128 bit security level.
TurboShake256
TurboSHAKE256 is a XOF (a secure hash function with a variable output length), with a 256 bit security level.
Keccak
A generic Keccak hash function.
Shake
The SHAKE extendable output hash function.
TurboShake
The TurboSHAKE extendable output hash function.
CShake
The cSHAKE extendable output hash function.
KMac
The KMAC extendable output authentication function.
TupleHash
The TupleHash extendable output hash function, with domain-separated inputs.

Source

Implementation

#
pub fn encode(comptime encoding: NistLengthEncoding, len: usize) Length {
    const len_bits = @bitSizeOf(@TypeOf(len)) - @clz(len) + 3;
    const len_bytes = std.math.divCeil(usize, len_bits, 8) catch unreachable;

    var res = Length{ .len = len_bytes + 1 };
    if (encoding == .right) {
        res.buf[len_bytes] = @intCast(len_bytes);
    }
    const end = if (encoding == .right) len_bytes - 1 else len_bytes;
    res.buf[end] = @truncate(len << 3);
    var len_ = len >> 5;
    for (1..len_bytes) |i| {
        res.buf[end - i] = @truncate(len_);
        len_ >>= 8;
    }
    if (encoding == .left) {
        res.buf[0] = @intCast(len_bytes);
    }
    return res;
}