Function encode [src]

Encode a length according to NIST SP 800-185.

Prototype

pub fn encode(comptime encoding: NistLengthEncoding, len: usize) Length

Parameters

encoding: NistLengthEncodinglen: usize

Source

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; }