Constant-Time Cryptography

Clever enforces constant-time discipline and compile-time specialization in the type system.

Secret<T> and declassify

Wrap sensitive scalars (Bool, U8..U128, I8..I128) in Secret<T> (and fixed arrays like Array<Secret<U8>, 32>). The secrecy checker statically guarantees that a Secret value can never:

Instead, use wrapping/carry arithmetic (+%, -%, *%, addCarry, subBorrow), bitwise operations, and select(cond, whenTrue, whenFalse) — which emits an opaque barrier (__asm__ volatile) to discourage C compilers from turning selects back into branches.

To return a public verdict (such as MAC tag verification), mark the exact boundary with declassify(...):

Checked
fun ctEq32(a: &Array<Secret<U8>, 32>, b: &Array<Secret<U8>, 32>): Secret<Bool> {
  var diff: Secret<U8> = 0
  for (i in 0u32 until 32u32) {
    diff = diff | (a[i] ^ b[i])
  }
  diff == 0
}

export fun cswap4(
  swap: Secret<Bool>,
  x: &mut Array<Secret<U64>, 4>,
  y: &mut Array<Secret<U64>, 4>
) {
  for (i in 0u32 until 4u32) {
    val xi = x[i]
    val yi = y[i]
    x[i] = select(swap, yi, xi)
    y[i] = select(swap, xi, yi)
  }
}

export fun verifyTag(tag: &Array<Secret<U8>, 32>, expected: &Array<Secret<U8>, 32>): Bool =
  declassify(ctEq32(tag, expected))

What the Secrecy Checker Rejects

Branching on a secret condition is a compile error:

Rejected, as it should be
export fun leakyCompare(a: Secret<U64>, b: Secret<U64>): U64 {
  if (a < b) { 1u64 } else { 0u64 }   // ERROR: branch on secret condition
}
The compiler saysline 2:7: no secret control flow: secret condition of `if` / `when`; use `select(c, a, b)` (in `leakyCompare`)

Indexing a table with a secret byte (classic cache-timing attack) is also rejected:

Rejected, as it should be
const SBOX: Array<U8, 4> = [0x63u8, 0x7cu8, 0x77u8, 0x7bu8]

export fun leakyLookup(k: Secret<U8>): U8 = SBOX[(k & 3u8).toU32()]
The compiler saysline 3:45: no secret addresses: secret index (in `leakyLookup`)

Auditing Secrets (clever secrets)

Run clever secrets F.clv to print every function touching Secret<..>, every barrier and masked select inserted, and every declassify site with exact file, line, and column:

clever secrets F.clv

Remember the explicit boundary documented in SECURITY.md: Secret<T> checks the source semantics and emits compiler barriers; it does not formally verify clang's backend, microarchitectural speculation, or automatic memory zeroization.

Parametric Modules & Compile-Time const fun

Real-world cryptography needs one generic implementation specialized across many prime fields and curves with zero runtime overhead.

In Clever:

Checked
module ModRing(M: U64) {
  const fun computeMask(m: U64): U64 {
    var v = m -% 1u64
    v = v | (v >> 1)
    v = v | (v >> 2)
    v = v | (v >> 4)
    v = v | (v >> 8)
    v = v | (v >> 16)
    v = v | (v >> 32)
    v
  }

  const MASK: U64 = computeMask(M)

  fun addMod(a: U64, b: U64): U64 {
    val s = (a % M) +% (b % M)
    if (s >= M) { s -% M } else { s }
  }
}

instance KyberField = ModRing(3329u64)

export fun kyberAdd(a: U64, b: U64): U64 = KyberField.addMod(a, b)
export fun kyberMask(): U64 = KyberField.MASK
kyberAdd()

Inspect any compile-time constant directly from the CLI:

clever const ring.clv KyberField.MASK

Portable SIMD, @hw Intrinsics & @native Fast Paths

Portable SIMD (Vec<T, N>)

Clever includes fixed-width vectors (Vec<U8, 16>, Vec<U32, 4>, Vec<U64, 2>, etc.) that live in locals (loaded from and stored into byte buffers with .loadLe / .storeLe). They lower to NEON / SSE / SIMD instructions on C and automatically scalarize lane-by-lane on targets without hardware vectors (like JS w32 or MVP Wasm):

Checked
export fun xorBlock16(out: &mut Bytes, a: &Bytes, b: &Bytes)
  requires out.len >= 16u64 && a.len >= 16u64 && b.len >= 16u64
{
  val va = Vec<U32, 4>.loadLe(a, 0u64)
  val vb = Vec<U32, 4>.loadLe(b, 0u64)
  (va ^ vb).storeLe(out, 0u64)
}

@hw Hardware Intrinsics (Compiler Prelude)

Hardware instructions such as Arm SHA-256 (sha256Rounds4, sha256Sched4), Arm SHA-512 (sha512Rounds2, sha512Sched2), and Arm Keccak (keccakP1600) live in Clever/Prelude/hw.clv with @hw("..."):

@native(fastFn) Alternatives (Standard Library)

Similarly, @native(alt) (reserved for the compiler prelude and lib/std/) lets a portable scalar reference function (such as UTF-8 validation in Clever/Prelude/text.clv) dispatch to a vectorized SIMD implementation on optimized C builds while keeping the scalar reference for the interpreter, Lean model, Wasm, and JS. Every @native substitution used in a build is listed under assumed native alternatives in clever trust.