Get Started

Installation

v install --git https://git.petal.cafe/mug/veil

Or add it to your v.mod dependencies:

Module {
    name: 'my_app'
    dependencies: ['veil']
}

Key exchange (ML-KEM)

Establish a shared secret with ML-KEM-768:

import veil.mlkem

// alice: generate a keypair
dk := mlkem.DecapsulationKey.generate(.ml_kem_768)!
ek := dk.encapsulation_key()

// send ek.bytes() to bob...

// bob: encapsulate a shared secret
shared_key, ciphertext := ek.encapsulate()!

// send ciphertext back to alice...

// alice: recover the shared secret
recovered := dk.decapsulate(ciphertext)!

assert shared_key == recovered

The result is a 32-byte shared key you can feed into whatever symmetric cipher you want (AES-256-GCM, ChaCha20-Poly1305, you pick).

Digital signatures (ML-DSA)

Sign and verify a message with ML-DSA-65:

import veil.mldsa

sk := mldsa.PrivateKey.generate(.ml_dsa_65)!
pk := sk.public_key()

msg := 'important document'.bytes()
sig := sk.sign(msg)!

assert pk.verify(msg, sig)! == true

Serialization

Keys and signatures serialize to and from []u8:

// serialize
ek_bytes := ek.bytes()
sk_bytes := sk.seed()?   // 32-byte seed for ML-DSA
dk_bytes := dk.bytes()?  // 64-byte seed (d || z) for ML-KEM

// deserialize
ek2 := mlkem.EncapsulationKey.from_bytes(ek_bytes, .ml_kem_768)!
sk2 := mldsa.PrivateKey.from_seed(sk_bytes, .ml_dsa_65)!
dk2 := mlkem.DecapsulationKey.from_seed(dk_bytes, .ml_kem_768)!

seed() and bytes() return an option: a key parsed from its expanded encoding (mldsa.PrivateKey.from_bytes, mlkem.DecapsulationKey.from_expanded_bytes) does not carry its seed, so they return none for it. Store the seed when you can; it is the smallest form and the only one that round-trips through from_seed.

SHAKE (extendable output)

SHAKE is an extendable output function (XOF) based on Keccak. Use it when you need arbitrary-length pseudorandom output from a seed.

V ships SHAKE in crypto.sha3, and veil uses it. veil.shake remains as a deprecated wrapper for one release.

import crypto.sha3

mut s := sha3.new_shake256()
s.write('seed'.bytes())
out := s.read(64) // squeeze 64 bytes

You can absorb data in multiple calls, then squeeze as many bytes as you need:

mut s := sha3.new_shake128()

// absorb in chunks
s.write(key)
s.write(label)
s.write(context)

// squeeze output
first_32 := s.read(32)
next_32 := s.read(32) // continues from where the last read left off

Once you call read, the sponge is finalized and further calls to write will panic. Call reset to start over with fresh input:

s.reset()
s.write('new seed'.bytes())
new_output := s.read(32)

Both SHAKE-128 (new_shake128) and SHAKE-256 (new_shake256) are available. SHAKE-256 provides 256-bit security; SHAKE-128 provides 128-bit security with a higher throughput rate (168 vs 136 bytes per permutation).

Which parameter set?

For most applications, go with ML-KEM-768 and ML-DSA-65 (Category 3, roughly AES-192 equivalent). Use Category 5 (ML-KEM-1024 / ML-DSA-87) if regulations demand it. Category 1 (ML-KEM-512 / ML-DSA-44) exists for constrained environments but offers less security margin.