Index Mathematics Conjectures nbv lettuce Widgets

typed_lex_chacha

stdlib/typed/typed_lex_chacha.nbv

typed_lex_chacha(static rounds, bytes, key, static nonce, static types, static lens, static reveal, static a_absorb)

ChaCha encryption (rounds 20 for ChaCha20, 8 for ChaCha8) of a typed byte string in anondb's lexicographic encoding (see typed_lex_check) with selected fields revealed, a programmatic commitment to the plaintext:

Costs about 22k rows per 64 byte block with ChaCha20 and 9k with ChaCha8.

Calls
bits/bytes_to_bits, bits/bytes_to_bits_static, chacha/chacha_block, bits/from_bits, slice, typed/typed_bytes_lattice, typed/typed_lex_check, typed/typed_lex_width, bits/u32_bits_static, bits/xor_bits_vec_wtns
Precompiles
assert_eq!, assert_lt!, clone!, concat!, div_ceil!, if_eq!, len!, loop!, loop_flat_map!, wtns!
Used by
typed/typed_lex_chacha20

Source

# ChaCha encryption (`rounds` 20 for ChaCha20, 8 for ChaCha8) of a typed byte
# string in anondb's lexicographic encoding (see typed_lex_check) with
# selected fields revealed, a programmatic commitment to the plaintext:
#   - `key_commitment`: the lattice sponge hash of the 32 key bytes as a
#     [u8; 32] document (typed_bytes_lattice), which makes the ciphertext
#     binding (ChaCha alone is not key committing)
#   - `ciphertext`: `bytes` xor the RFC 8439 style keystream of `key` and the
#     public 12 byte `nonce`, block counter starting at 0 (as the `chacha20`
#     crate's apply_keystream)
#   - `revealed`: the plaintext bytes of every field whose static `reveal`
#     entry is 1, in field order
# Costs about 22k rows per 64 byte block with ChaCha20 and 9k with ChaCha8.
(static rounds, bytes, key, static nonce, static types, static lens, static reveal, static a_absorb)

static n = len! bytes
static fields = len! types
assert_lt! 0, n
assert_eq! fields, len! reveal
assert_eq! 32, len! key
assert_eq! 12, len! nonce

let bits = typed_lex_check((clone! bytes), (clone! types), (clone! lens))
let key_bits = bytes_to_bits(clone! key)
let key_commitment = typed_bytes_lattice(key, [8], [32], a_absorb)

# the counter and nonce words are witnesses, chacha_block can't add statics
static nonce_bits = bytes_to_bits_static(nonce)
static blocks = div_ceil! n, 64
let keystream = loop_flat_map! blocks, b {
  chacha_block(rounds, (clone! key_bits), (wtns! concat! u32_bits_static(b), (clone! nonce_bits)))
}
let ct_bits = xor_bits_vec_wtns(bits, slice(keystream, 0, 8 * n))
let ciphertext = wtns! loop_flat_map! n, i {
  from_bits(slice((clone! ct_bits), 8 * i, 8))
}

# byte positions of the revealed fields
static mut r = 0
loop! fields, i {
  assert_lt! reveal[i], 2
  if_eq! reveal[i], 1 {
    r = r + typed_lex_width(types[i], lens[i])
  }
}
static mut positions[r]
static mut offset = 0
static mut j = 0
loop! fields, i {
  static w = typed_lex_width(types[i], lens[i])
  if_eq! reveal[i], 1 {
    loop! w, k {
      positions[j] = offset + k
      j = j + 1
    }
  }
  offset = offset + w
}
let revealed = loop_flat_map! r, k {
  slice((clone! bytes), positions[k], 1)
}

(key_commitment, ciphertext, revealed)