///|
#non_exhaustive
#derive(Debug, Eq)
pub(all) suberror CryptoError {
  InvalidKeyLength(expected~ : Int32, actual~ : Int32)
  InvalidNonceLength(expected~ : Int32, actual~ : Int32)
  InvalidOutputLength(Int32)
  InvalidEncoding(String)
}

///|
let sha256_k : FixedArray[UInt32] = [
  0x428a2f98U, 0x71374491U, 0xb5c0fbcfU, 0xe9b5dba5U, 0x3956c25bU, 0x59f111f1U, 0x923f82a4U,
  0xab1c5ed5U, 0xd807aa98U, 0x12835b01U, 0x243185beU, 0x550c7dc3U, 0x72be5d74U, 0x80deb1feU,
  0x9bdc06a7U, 0xc19bf174U, 0xe49b69c1U, 0xefbe4786U, 0x0fc19dc6U, 0x240ca1ccU, 0x2de92c6fU,
  0x4a7484aaU, 0x5cb0a9dcU, 0x76f988daU, 0x983e5152U, 0xa831c66dU, 0xb00327c8U, 0xbf597fc7U,
  0xc6e00bf3U, 0xd5a79147U, 0x06ca6351U, 0x14292967U, 0x27b70a85U, 0x2e1b2138U, 0x4d2c6dfcU,
  0x53380d13U, 0x650a7354U, 0x766a0abbU, 0x81c2c92eU, 0x92722c85U, 0xa2bfe8a1U, 0xa81a664bU,
  0xc24b8b70U, 0xc76c51a3U, 0xd192e819U, 0xd6990624U, 0xf40e3585U, 0x106aa070U, 0x19a4c116U,
  0x1e376c08U, 0x2748774cU, 0x34b0bcb5U, 0x391c0cb3U, 0x4ed8aa4aU, 0x5b9cca4fU, 0x682e6ff3U,
  0x748f82eeU, 0x78a5636fU, 0x84c87814U, 0x8cc70208U, 0x90befffaU, 0xa4506cebU, 0xbef9a3f7U,
  0xc67178f2U,
]

///|
fn rotr32(x : UInt32, n : Int32) -> UInt32 {
  (x >> n) | (x << (32 - n))
}

///|
fn rotl32(x : UInt32, n : Int32) -> UInt32 {
  (x << n) | (x >> (32 - n))
}

///|
fn nibble_to_hex(n : Int32) -> Char {
  match n & 0xF {
    0 => '0'
    1 => '1'
    2 => '2'
    3 => '3'
    4 => '4'
    5 => '5'
    6 => '6'
    7 => '7'
    8 => '8'
    9 => '9'
    10 => 'a'
    11 => 'b'
    12 => 'c'
    13 => 'd'
    14 => 'e'
    _ => 'f'
  }
}

///|
pub fn bytes_to_hex(data : BytesView) -> String {
  let sb = StringBuilder()
  for b in data {
    let v = b.to_int()
    sb.write_char(nibble_to_hex(v >> 4))
    sb.write_char(nibble_to_hex(v & 0xF))
  }
  sb.to_string()
}

///|
/// Constant-time equality check for secret MACs, digests, and tokens.
pub fn constant_time_equal(a : BytesView, b : BytesView) -> Bool {
  if a.length() != b.length() {
    return false
  }
  let mut diff : Int32 = 0
  for i in 0.. Unit {
  state[0] = 0x6a09e667U
  state[1] = 0xbb67ae85U
  state[2] = 0x3c6ef372U
  state[3] = 0xa54ff53aU
  state[4] = 0x510e527fU
  state[5] = 0x9b05688cU
  state[6] = 0x1f83d9abU
  state[7] = 0x5be0cd19U
}

///|
/// Compresses the 16 big-endian words already loaded in `w[0..16]` into `state[0..8]` in-place.
fn sha256_compress_words(
  state : FixedArray[UInt32],
  w : FixedArray[UInt32],
) -> Unit {
  for i in 16..<64 {
    let w15 = w[i - 15]
    let w2 = w[i - 2]
    let s0 = rotr32(w15, 7) ^ rotr32(w15, 18) ^ (w15 >> 3)
    let s1 = rotr32(w2, 17) ^ rotr32(w2, 19) ^ (w2 >> 10)
    w[i] = w[i - 16] + s0 + w[i - 7] + s1
  }
  let mut a = state[0]
  let mut b = state[1]
  let mut c = state[2]
  let mut d = state[3]
  let mut e = state[4]
  let mut f = state[5]
  let mut g = state[6]
  let mut h = state[7]
  for i in 0..<64 {
    let s1 = rotr32(e, 6) ^ rotr32(e, 11) ^ rotr32(e, 25)
    let ch = (e & f) ^ ((e ^ 0xFFFFFFFFU) & g)
    let temp1 = h + s1 + ch + sha256_k[i] + w[i]
    let s0 = rotr32(a, 2) ^ rotr32(a, 13) ^ rotr32(a, 22)
    let maj = (a & b) ^ (a & c) ^ (b & c)
    let temp2 = s0 + maj
    h = g
    g = f
    f = e
    e = d + temp1
    d = c
    c = b
    b = a
    a = temp1 + temp2
  }
  state[0] = state[0] + a
  state[1] = state[1] + b
  state[2] = state[2] + c
  state[3] = state[3] + d
  state[4] = state[4] + e
  state[5] = state[5] + f
  state[6] = state[6] + g
  state[7] = state[7] + h
}

///|
fn sha256_compress_fixed(
  state : FixedArray[UInt32],
  w : FixedArray[UInt32],
  block : FixedArray[Byte],
  offset : Int32,
) -> Unit {
  for i in 0..<16 {
    let base = offset + i * 4
    w[i] = (block[base].to_uint() << 24) |
      (block[base + 1].to_uint() << 16) |
      (block[base + 2].to_uint() << 8) |
      block[base + 3].to_uint()
  }
  sha256_compress_words(state, w)
}

///|
fn sha256_compress_view(
  state : FixedArray[UInt32],
  w : FixedArray[UInt32],
  data : BytesView,
  offset : Int32,
) -> Unit {
  for i in 0..<16 {
    let base = offset + i * 4
    w[i] = (data[base].to_uint() << 24) |
      (data[base + 1].to_uint() << 16) |
      (data[base + 2].to_uint() << 8) |
      data[base + 3].to_uint()
  }
  sha256_compress_words(state, w)
}

///|
/// Streams `data` (zero-copy) plus optional 4-byte suffix (`extra_u32`) into an initialized
/// SHA-256 `state` and finalizes padding with total byte count `prefix_len + data.length() (+ 4)`.
fn sha256_update_and_finalize(
  state : FixedArray[UInt32],
  w : FixedArray[UInt32],
  tail : FixedArray[Byte],
  prefix_bytes : UInt64,
  data : BytesView,
  extra_be32? : UInt32,
) -> Unit {
  let len = data.length()
  let mut offset = 0
  while offset + 64 <= len {
    sha256_compress_view(state, w, data, offset)
    offset = offset + 64
  }
  let mut rem = len - offset
  for i in 0.. 0UL
    Some(bi) => {
      let b0 = ((bi >> 24) & 0xFFU).to_byte()
      let b1 = ((bi >> 16) & 0xFFU).to_byte()
      let b2 = ((bi >> 8) & 0xFFU).to_byte()
      let b3 = (bi & 0xFFU).to_byte()
      let extra_bytes : FixedArray[Byte] = [b0, b1, b2, b3]
      for i in 0..<4 {
        tail[rem] = extra_bytes[i]
        rem = rem + 1
        if rem == 64 {
          sha256_compress_fixed(state, w, tail, 0)
          rem = 0
        }
      }
      4UL
    }
  }
  let total_bits = (prefix_bytes + len.to_uint64() + extra_len) * 8UL
  tail[rem] = b'\x80'
  rem = rem + 1
  if rem > 56 {
    for i in rem..<64 {
      tail[i] = b'\x00'
    }
    sha256_compress_fixed(state, w, tail, 0)
    rem = 0
  }
  for i in rem..<56 {
    tail[i] = b'\x00'
  }
  for i in 0..<8 {
    let shift = (7 - i) * 8
    tail[56 + i] = ((total_bits >> shift) & 0xFFUL).to_byte()
  }
  sha256_compress_fixed(state, w, tail, 0)
}

///|
fn sha256_state_to_bytes(state : FixedArray[UInt32]) -> Bytes {
  let out : FixedArray[Byte] = FixedArray::make(32, b'\x00')
  for i in 0..<8 {
    let v = state[i]
    out[i * 4] = ((v >> 24) & 0xFFU).to_byte()
    out[i * 4 + 1] = ((v >> 16) & 0xFFU).to_byte()
    out[i * 4 + 2] = ((v >> 8) & 0xFFU).to_byte()
    out[i * 4 + 3] = (v & 0xFFU).to_byte()
  }
  Bytes::from_array(out)
}

///|
/// FIPS 180-4 SHA-256 returning 32-byte digest (streaming zero-copy over input blocks).
pub fn sha256(data : BytesView) -> Bytes {
  let state : FixedArray[UInt32] = FixedArray::make(8, 0U)
  let w : FixedArray[UInt32] = FixedArray::make(64, 0U)
  let tail : FixedArray[Byte] = FixedArray::make(64, b'\x00')
  sha256_init_state(state)
  sha256_update_and_finalize(state, w, tail, 0UL, data)
  sha256_state_to_bytes(state)
}

///|
pub fn sha256_hex(data : BytesView) -> String {
  bytes_to_hex(sha256(data)[:])
}

///|
/// Precomputes HMAC-SHA256 ipad and opad 64-byte midstates once for a given key.
fn hmac_sha256_precompute_midstates(
  key : BytesView,
  ipad_state : FixedArray[UInt32],
  opad_state : FixedArray[UInt32],
  w : FixedArray[UInt32],
  key_block : FixedArray[Byte],
) -> Unit {
  for i in 0..<64 {
    key_block[i] = b'\x00'
  }
  if key.length() > 64 {
    let tmp_state : FixedArray[UInt32] = FixedArray::make(8, 0U)
    let tmp_tail : FixedArray[Byte] = FixedArray::make(64, b'\x00')
    sha256_init_state(tmp_state)
    sha256_update_and_finalize(tmp_state, w, tmp_tail, 0UL, key)
    for i in 0..<8 {
      let v = tmp_state[i]
      key_block[i * 4] = ((v >> 24) & 0xFFU).to_byte()
      key_block[i * 4 + 1] = ((v >> 16) & 0xFFU).to_byte()
      key_block[i * 4 + 2] = ((v >> 8) & 0xFFU).to_byte()
      key_block[i * 4 + 3] = (v & 0xFFU).to_byte()
    }
  } else {
    for i in 0.. opad in-place (0x36 ^ 0x5c == 0x6a) and compress
  for i in 0..<64 {
    key_block[i] = (key_block[i].to_int() ^ 0x6a).to_byte()
  }
  sha256_init_state(opad_state)
  sha256_compress_fixed(opad_state, w, key_block, 0)
}

///|
/// Finalizes the outer HMAC-SHA256 block from a 32-byte inner digest held in `inner_words[0..8]`
/// into `state[0..8]` using precomputed `opad_state` (zero heap allocations).
fn hmac_sha256_compress_32_from_midstate(
  midstate : FixedArray[UInt32],
  input_words : FixedArray[UInt32],
  out_state : FixedArray[UInt32],
  w : FixedArray[UInt32],
) -> Unit {
  for i in 0..<8 {
    out_state[i] = midstate[i]
    w[i] = input_words[i]
  }
  w[8] = 0x80000000U
  w[9] = 0U
  w[10] = 0U
  w[11] = 0U
  w[12] = 0U
  w[13] = 0U
  w[14] = 0U
  // Total message length: 64-byte pad + 32-byte payload = 96 bytes = 768 bits
  w[15] = 768U
  sha256_compress_words(out_state, w)
}

///|
/// RFC 2104 / RFC 4231 HMAC-SHA256 returning 32-byte MAC (zero-copy streaming over data).
pub fn hmac_sha256(key~ : BytesView, data~ : BytesView) -> Bytes {
  let ipad_state : FixedArray[UInt32] = FixedArray::make(8, 0U)
  let opad_state : FixedArray[UInt32] = FixedArray::make(8, 0U)
  let out_state : FixedArray[UInt32] = FixedArray::make(8, 0U)
  let w : FixedArray[UInt32] = FixedArray::make(64, 0U)
  let block : FixedArray[Byte] = FixedArray::make(64, b'\x00')
  hmac_sha256_precompute_midstates(key, ipad_state, opad_state, w, block)
  sha256_update_and_finalize(ipad_state, w, block, 64UL, data)
  hmac_sha256_compress_32_from_midstate(opad_state, ipad_state, out_state, w)
  sha256_state_to_bytes(out_state)
}

///|
/// RFC 2898 PBKDF2-HMAC-SHA256 key derivation with precomputed ipad/opad midstates
/// and zero heap allocations in the inner iteration loop.
pub fn pbkdf2_hmac_sha256(
  password~ : BytesView,
  salt~ : BytesView,
  iterations~ : Int32,
  dk_len~ : Int32,
) -> Bytes raise CryptoError {
  if iterations <= 0 || dk_len <= 0 {
    raise CryptoError::InvalidOutputLength(dk_len)
  }
  let ipad_state : FixedArray[UInt32] = FixedArray::make(8, 0U)
  let opad_state : FixedArray[UInt32] = FixedArray::make(8, 0U)
  let inner_state : FixedArray[UInt32] = FixedArray::make(8, 0U)
  let u_words : FixedArray[UInt32] = FixedArray::make(8, 0U)
  let acc_words : FixedArray[UInt32] = FixedArray::make(8, 0U)
  let w : FixedArray[UInt32] = FixedArray::make(64, 0U)
  let scratch_block : FixedArray[Byte] = FixedArray::make(64, b'\x00')
  hmac_sha256_precompute_midstates(
    password, ipad_state, opad_state, w, scratch_block,
  )

  let blocks = (dk_len + 31) / 32
  let out : FixedArray[Byte] = FixedArray::make(dk_len, b'\x00')
  let mut out_pos = 0
  for block_idx in 1..<=blocks {
    // U_1 = HMAC(password, salt || INT_32_BE(block_idx))
    for i in 0..<8 {
      inner_state[i] = ipad_state[i]
    }
    sha256_update_and_finalize(
      inner_state,
      w,
      scratch_block,
      64UL,
      salt,
      extra_be32=block_idx.reinterpret_as_uint(),
    )
    hmac_sha256_compress_32_from_midstate(opad_state, inner_state, u_words, w)
    for i in 0..<8 {
      acc_words[i] = u_words[i]
    }
    // U_2 .. U_iterations: zero-allocation in-place midstate iteration
    for _iter in 1..= dk_len {
        break
      }
      let word = acc_words[i]
      let b0 = ((word >> 24) & 0xFFU).to_byte()
      let b1 = ((word >> 16) & 0xFFU).to_byte()
      let b2 = ((word >> 8) & 0xFFU).to_byte()
      let b3 = (word & 0xFFU).to_byte()
      if out_pos < dk_len {
        out[out_pos] = b0
        out_pos = out_pos + 1
      }
      if out_pos < dk_len {
        out[out_pos] = b1
        out_pos = out_pos + 1
      }
      if out_pos < dk_len {
        out[out_pos] = b2
        out_pos = out_pos + 1
      }
      if out_pos < dk_len {
        out[out_pos] = b3
        out_pos = out_pos + 1
      }
    }
  }
  Bytes::from_array(out)
}

///|
let b64url_alphabet : FixedArray[Char] = [
  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_',
]

///|
/// RFC 4648 URL-safe Base64 encoding without '=' padding (JWT / PASETO / WebCrypto).
pub fn base64url_encode(data : BytesView) -> String {
  let sb = StringBuilder()
  let len = data.length()
  let mut i = 0
  while i + 3 <= len {
    let n = (data[i].to_int() << 16) |
      (data[i + 1].to_int() << 8) |
      data[i + 2].to_int()
    sb.write_char(b64url_alphabet[(n >> 18) & 0x3F])
    sb.write_char(b64url_alphabet[(n >> 12) & 0x3F])
    sb.write_char(b64url_alphabet[(n >> 6) & 0x3F])
    sb.write_char(b64url_alphabet[n & 0x3F])
    i = i + 3
  }
  let rem = len - i
  if rem == 1 {
    let n = data[i].to_int() << 16
    sb.write_char(b64url_alphabet[(n >> 18) & 0x3F])
    sb.write_char(b64url_alphabet[(n >> 12) & 0x3F])
  } else if rem == 2 {
    let n = (data[i].to_int() << 16) | (data[i + 1].to_int() << 8)
    sb.write_char(b64url_alphabet[(n >> 18) & 0x3F])
    sb.write_char(b64url_alphabet[(n >> 12) & 0x3F])
    sb.write_char(b64url_alphabet[(n >> 6) & 0x3F])
  }
  sb.to_string()
}

///|
fn b64url_val(c : Int32) -> Int32? {
  if c >= 'A'.to_int() && c <= 'Z'.to_int() {
    Some(c - 'A'.to_int())
  } else if c >= 'a'.to_int() && c <= 'z'.to_int() {
    Some(c - 'a'.to_int() + 26)
  } else if c >= '0'.to_int() && c <= '9'.to_int() {
    Some(c - '0'.to_int() + 52)
  } else if c == '-'.to_int() {
    Some(62)
  } else if c == '_'.to_int() {
    Some(63)
  } else {
    None
  }
}

///|
/// RFC 4648 URL-safe Base64 decoding (tolerates optional trailing '=' padding).
pub fn base64url_decode(input : StringView) -> Bytes raise CryptoError {
  let bytes = @utf8.encode(input)
  let mut len = bytes.length()
  while len > 0 && bytes[len - 1] == b'=' {
    len = len - 1
  }
  if len % 4 == 1 {
    raise CryptoError::InvalidEncoding("invalid base64url length")
  }
  let out = Buffer(capacity=len * 3 / 4)
  let mut i = 0
  while i + 4 <= len {
    let v0 = match b64url_val(bytes[i].to_int()) {
      Some(v) => v
      None => raise CryptoError::InvalidEncoding("invalid base64url character")
    }
    let v1 = match b64url_val(bytes[i + 1].to_int()) {
      Some(v) => v
      None => raise CryptoError::InvalidEncoding("invalid base64url character")
    }
    let v2 = match b64url_val(bytes[i + 2].to_int()) {
      Some(v) => v
      None => raise CryptoError::InvalidEncoding("invalid base64url character")
    }
    let v3 = match b64url_val(bytes[i + 3].to_int()) {
      Some(v) => v
      None => raise CryptoError::InvalidEncoding("invalid base64url character")
    }
    let n = (v0 << 18) | (v1 << 12) | (v2 << 6) | v3
    out.write_byte(((n >> 16) & 0xFF).to_byte())
    out.write_byte(((n >> 8) & 0xFF).to_byte())
    out.write_byte((n & 0xFF).to_byte())
    i = i + 4
  }
  let rem = len - i
  if rem == 2 {
    let v0 = match b64url_val(bytes[i].to_int()) {
      Some(v) => v
      None => raise CryptoError::InvalidEncoding("invalid base64url character")
    }
    let v1 = match b64url_val(bytes[i + 1].to_int()) {
      Some(v) => v
      None => raise CryptoError::InvalidEncoding("invalid base64url character")
    }
    let n = (v0 << 18) | (v1 << 12)
    out.write_byte(((n >> 16) & 0xFF).to_byte())
  } else if rem == 3 {
    let v0 = match b64url_val(bytes[i].to_int()) {
      Some(v) => v
      None => raise CryptoError::InvalidEncoding("invalid base64url character")
    }
    let v1 = match b64url_val(bytes[i + 1].to_int()) {
      Some(v) => v
      None => raise CryptoError::InvalidEncoding("invalid base64url character")
    }
    let v2 = match b64url_val(bytes[i + 2].to_int()) {
      Some(v) => v
      None => raise CryptoError::InvalidEncoding("invalid base64url character")
    }
    let n = (v0 << 18) | (v1 << 12) | (v2 << 6)
    out.write_byte(((n >> 16) & 0xFF).to_byte())
    out.write_byte(((n >> 8) & 0xFF).to_byte())
  }
  out.to_bytes()
}

///|
fn load_u32_le(data : BytesView, off : Int32) -> UInt32 {
  data[off].to_uint() |
  (data[off + 1].to_uint() << 8) |
  (data[off + 2].to_uint() << 16) |
  (data[off + 3].to_uint() << 24)
}

///|
fn store_u32_le(out : FixedArray[Byte], off : Int32, v : UInt32) -> Unit {
  out[off] = (v & 0xFFU).to_byte()
  out[off + 1] = ((v >> 8) & 0xFFU).to_byte()
  out[off + 2] = ((v >> 16) & 0xFFU).to_byte()
  out[off + 3] = ((v >> 24) & 0xFFU).to_byte()
}

///|
fn chacha_qr(
  s : FixedArray[UInt32],
  a : Int32,
  b : Int32,
  c : Int32,
  d : Int32,
) -> Unit {
  s[a] = s[a] + s[b]
  s[d] = rotl32(s[d] ^ s[a], 16)
  s[c] = s[c] + s[d]
  s[b] = rotl32(s[b] ^ s[c], 12)
  s[a] = s[a] + s[b]
  s[d] = rotl32(s[d] ^ s[a], 8)
  s[c] = s[c] + s[d]
  s[b] = rotl32(s[b] ^ s[c], 7)
}

///|
fn hchacha20(key : BytesView, nonce16 : BytesView) -> Bytes {
  let s : FixedArray[UInt32] = FixedArray::make(16, 0U)
  s[0] = 0x61707865U
  s[1] = 0x3320646eU
  s[2] = 0x79622d32U
  s[3] = 0x6b206574U
  for i in 0..<8 {
    s[4 + i] = load_u32_le(key, i * 4)
  }
  for i in 0..<4 {
    s[12 + i] = load_u32_le(nonce16, i * 4)
  }
  for _r in 0..<10 {
    chacha_qr(s, 0, 4, 8, 12)
    chacha_qr(s, 1, 5, 9, 13)
    chacha_qr(s, 2, 6, 10, 14)
    chacha_qr(s, 3, 7, 11, 15)
    chacha_qr(s, 0, 5, 10, 15)
    chacha_qr(s, 1, 6, 11, 12)
    chacha_qr(s, 2, 7, 8, 13)
    chacha_qr(s, 3, 4, 9, 14)
  }
  let out : FixedArray[Byte] = FixedArray::make(32, b'\x00')
  for i in 0..<4 {
    store_u32_le(out, i * 4, s[i])
  }
  for i in 0..<4 {
    store_u32_le(out, 16 + i * 4, s[12 + i])
  }
  Bytes::from_array(out)
}

///|
/// RFC 8439 ChaCha20 XOR stream cipher (32-byte key, 12-byte nonce).
pub fn chacha20_xor(
  key~ : BytesView,
  nonce~ : BytesView,
  counter~ : UInt32 = 0U,
  data~ : BytesView,
) -> Bytes raise CryptoError {
  if key.length() != 32 {
    raise CryptoError::InvalidKeyLength(expected=32, actual=key.length())
  }
  if nonce.length() != 12 {
    raise CryptoError::InvalidNonceLength(expected=12, actual=nonce.length())
  }
  let out : FixedArray[Byte] = FixedArray::make(data.length(), b'\x00')
  let block : FixedArray[Byte] = FixedArray::make(64, b'\x00')
  let init_state : FixedArray[UInt32] = FixedArray::make(16, 0U)
  init_state[0] = 0x61707865U
  init_state[1] = 0x3320646eU
  init_state[2] = 0x79622d32U
  init_state[3] = 0x6b206574U
  for i in 0..<8 {
    init_state[4 + i] = load_u32_le(key, i * 4)
  }
  init_state[13] = load_u32_le(nonce, 0)
  init_state[14] = load_u32_le(nonce, 4)
  init_state[15] = load_u32_le(nonce, 8)
  let work : FixedArray[UInt32] = FixedArray::make(16, 0U)
  let mut ctr = counter
  let mut pos = 0
  while pos < data.length() {
    init_state[12] = ctr
    for i in 0..<16 {
      work[i] = init_state[i]
    }
    for _r in 0..<10 {
      chacha_qr(work, 0, 4, 8, 12)
      chacha_qr(work, 1, 5, 9, 13)
      chacha_qr(work, 2, 6, 10, 14)
      chacha_qr(work, 3, 7, 11, 15)
      chacha_qr(work, 0, 5, 10, 15)
      chacha_qr(work, 1, 6, 11, 12)
      chacha_qr(work, 2, 7, 8, 13)
      chacha_qr(work, 3, 4, 9, 14)
    }
    for i in 0..<16 {
      store_u32_le(block, i * 4, work[i] + init_state[i])
    }
    let chunk = if data.length() - pos < 64 { data.length() - pos } else { 64 }
    for j in 0.. Bytes raise CryptoError {
  if key.length() != 32 {
    raise CryptoError::InvalidKeyLength(expected=32, actual=key.length())
  }
  if nonce.length() != 24 {
    raise CryptoError::InvalidNonceLength(expected=24, actual=nonce.length())
  }
  let subkey = hchacha20(key, nonce[:16])
  let subnonce : FixedArray[Byte] = FixedArray::make(12, b'\x00')
  for i in 0..<8 {
    subnonce[4 + i] = nonce[16 + i]
  }
  chacha20_xor(
    key=subkey[:],
    nonce=Bytes::from_array(subnonce)[:],
    counter~,
    data~,
  )
}