veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

bits

lib/bits.vk

lib/bits.vk Pure-Veko binary wire helpers (u24, signed i16/i32, IEEE-754 f64 bitcasts, LEB128/ZigZag varints), checksums (CRC32, Adler-32, FNV-1a), and constant-time byte primitives. Zero capabilities required.

VarintVal struct
signature
VarintVal = struct { value i64, next_off i64 }

Result of decoding a varint at a byte offset.

signature
get-u24-be = fn [b bytes, off i64] i64

Reads an unsigned 24-bit big-endian integer from b at off.

Executable Example
veko
use [bits]
mut b = bytes/with-cap 4
_ = bits/push-u24-be mut b 0x123456
if (bits/get-u24-be b 0) != 0x123456 [ err 'u24-be mismatch' ] else [ .ok ]
signature
get-u24-le = fn [b bytes, off i64] i64

Reads an unsigned 24-bit little-endian integer from b at off.

signature
set-u24-be = fn [mut b bytes, off i64, v i64] i64

Writes an unsigned 24-bit big-endian integer into b at off.

signature
set-u24-le = fn [mut b bytes, off i64, v i64] i64

Writes an unsigned 24-bit little-endian integer into b at off.

signature
push-u24-be = fn [mut b bytes, v i64] i64

Appends an unsigned 24-bit big-endian integer to b, returning the new length.

signature
push-u24-le = fn [mut b bytes, v i64] i64

Appends an unsigned 24-bit little-endian integer to b, returning the new length.

signature
get-i16-le = fn [b bytes, off i64] i64

Reads a signed 16-bit little-endian integer from b at off.

signature
get-i16-be = fn [b bytes, off i64] i64

Reads a signed 16-bit big-endian integer from b at off.

signature
get-i32-le = fn [b bytes, off i64] i64

Reads a signed 32-bit little-endian integer from b at off.

signature
get-i32-be = fn [b bytes, off i64] i64

Reads a signed 32-bit big-endian integer from b at off.

signature
push-i16-le = fn [mut b bytes, v i64] i64

Appends a signed 16-bit little-endian integer to b.

signature
push-i16-be = fn [mut b bytes, v i64] i64

Appends a signed 16-bit big-endian integer to b.

signature
push-i32-le = fn [mut b bytes, v i64] i64

Appends a signed 32-bit little-endian integer to b.

signature
push-i32-be = fn [mut b bytes, v i64] i64

Appends a signed 32-bit big-endian integer to b.

signature
f64-to-bits = fn [x f64] i64

Converts an IEEE-754 f64 value into its 64-bit integer bit pattern.

Executable Example
veko
use [bits]
b1 = bits/f64-to-bits 1.0
b2 = bits/f64-to-bits -2.5
rt = bits/bits-to-f64 b2
if b1 != 0x3ff0000000000000 or rt != -2.5 [ err 'f64 bitcast mismatch' ] else [ .ok ]
signature
bits-to-f64 = fn [raw i64] f64

Reconstructs an IEEE-754 f64 value from its 64-bit integer bit pattern.

signature
zigzag-encode = fn [n i64] i64

Encodes a signed i64 into an unsigned ZigZag i64 bit pattern.

Executable Example
veko
use [bits]
z = bits/zigzag-encode -42
if (bits/zigzag-decode z) != -42 [ err 'zigzag mismatch' ] else [ .ok ]
signature
zigzag-decode = fn [u i64] i64

Decodes an unsigned ZigZag i64 back into a signed i64.

signature
push-varint-u64 = fn [mut out bytes, v i64] i64

Appends an unsigned LEB128 varint (64-bit) to out, returning the new length.

Executable Example
veko
use [bits]
mut b = bytes/with-cap 16
_ = bits/push-varint-u64 mut b 300
r = bits/read-varint-u64 b 0
if r.value != 300 or r.next_off != 2 [ err 'varint mismatch' ] else [ .ok ]
signature
read-varint-u64 = fn [b bytes, off i64] VarintVal

Reads an unsigned LEB128 varint from b starting at off.

signature
crc32 = fn [data bytes] i64

Computes the IEEE 802.3 CRC-32 checksum (0xEDB88320 reflected polynomial).

Executable Example
veko
use [bits]
c = bits/crc32 (bytes/from-str '123456789')
if c != 0xcbf43926 [ err 'crc32 mismatch' ] else [ .ok ]
signature
adler32 = fn [data bytes] i64

Computes the RFC 1950 Adler-32 checksum of data.

Executable Example
veko
use [bits]
a = bits/adler32 (bytes/from-str 'Wikipedia')
if a != 0x11e60398 [ err 'adler32 mismatch' ] else [ .ok ]
signature
fnv1a-64 = fn [data bytes] i64

Computes the 64-bit FNV-1a non-cryptographic hash of data.

signature
index-of = fn [hay bytes, needle bytes, start i64] i64

Finds the first index of needle inside hay at or after start, or -1 if absent.

Executable Example
veko
use [bits]
h = bytes/from-str 'hello veko world'
n = bytes/from-str 'veko'
if (bits/index-of h n 0) != 6 [ err 'bits/index-of mismatch' ] else [ .ok ]
signature
starts-with? = fn [data bytes, prefix bytes] bool

Returns true if data starts with prefix.

signature
ends-with? = fn [data bytes, suffix bytes] bool

Returns true if data ends with suffix.

signature
xor-in-place! = fn [mut dst bytes, mask bytes] i64

XORs mask into dst in-place across min(len dst, len mask) bytes.

signature
ct-eq? = fn [a bytes, b bytes] bool

Constant-time equality comparison for equal-length byte buffers (returns false if lengths differ).

Executable Example
veko
use [bits]
a = bytes/from-str 'secret-tag'
b = bytes/from-str 'secret-tag'
c = bytes/from-str 'secret-tah'
if (not (bits/ct-eq? a b)) or (bits/ct-eq? a c) [ err 'ct-eq mismatch' ] else [ .ok ]