///|
/// Constant-time equality comparison for two byte slices.
/// Returns `false` immediately only when lengths differ; otherwise inspects every byte without branching.
pub fn ct_eq(a : BytesView, b : BytesView) -> Bool {
  if a.length() != b.length() {
    false
  } else {
    let mut diff : UInt32 = 0
    for i in 0.. Bool {
  let mut acc : UInt32 = 0
  for i in 0.. UInt32 {
  data[offset].to_uint() |
  (data[offset + 1].to_uint() << 8) |
  (data[offset + 2].to_uint() << 16) |
  (data[offset + 3].to_uint() << 24)
}

///|
/// Read a 64-bit unsigned integer in little-endian order at `offset`.
pub fn read_u64_le(data : BytesView, offset : Int32) -> UInt64 {
  data[offset].to_uint64() |
  (data[offset + 1].to_uint64() << 8) |
  (data[offset + 2].to_uint64() << 16) |
  (data[offset + 3].to_uint64() << 24) |
  (data[offset + 4].to_uint64() << 32) |
  (data[offset + 5].to_uint64() << 40) |
  (data[offset + 6].to_uint64() << 48) |
  (data[offset + 7].to_uint64() << 56)
}

///|
/// Write a 32-bit unsigned integer in little-endian order into `out` starting at `offset`.
pub fn write_u32_le(out : FixedArray[Byte], offset : Int32, v : UInt32) -> Unit {
  out[offset] = (v & 0xffU).to_byte()
  out[offset + 1] = ((v >> 8) & 0xffU).to_byte()
  out[offset + 2] = ((v >> 16) & 0xffU).to_byte()
  out[offset + 3] = ((v >> 24) & 0xffU).to_byte()
}

///|
/// Write a 64-bit unsigned integer in little-endian order into `out` starting at `offset`.
pub fn write_u64_le(out : FixedArray[Byte], offset : Int32, v : UInt64) -> Unit {
  out[offset] = (v & 0xffUL).to_byte()
  out[offset + 1] = ((v >> 8) & 0xffUL).to_byte()
  out[offset + 2] = ((v >> 16) & 0xffUL).to_byte()
  out[offset + 3] = ((v >> 24) & 0xffUL).to_byte()
  out[offset + 4] = ((v >> 32) & 0xffUL).to_byte()
  out[offset + 5] = ((v >> 40) & 0xffUL).to_byte()
  out[offset + 6] = ((v >> 48) & 0xffUL).to_byte()
  out[offset + 7] = ((v >> 56) & 0xffUL).to_byte()
}

///|
/// Concatenate two byte views into a newly allocated `Bytes`.
pub fn concat2(a : BytesView, b : BytesView) -> Bytes {
  let total = a.length() + b.length()
  let out = FixedArray::make(total, b'\x00')
  for i in 0.. String {
  let digits = "0123456789abcdef"
  let sb = StringBuilder()
  for i in 0..> 4) & 0x0f
    let lo = v & 0x0f
    sb.write_char(digits[hi].to_int().unsafe_to_char())
    sb.write_char(digits[lo].to_int().unsafe_to_char())
  }
  sb.to_string()
}

///|
fn hex_nibble(c : UInt16) -> Int32 raise CryptoError {
  if c >= '0' && c <= '9' {
    c.to_int() - '0'.to_int()
  } else if c >= 'a' && c <= 'f' {
    c.to_int() - 'a'.to_int() + 10
  } else if c >= 'A' && c <= 'F' {
    c.to_int() - 'A'.to_int() + 10
  } else {
    raise CryptoError::InvalidHex("invalid hex character")
  }
}

///|
/// Decode hexadecimal string (whitespace ignored) into `Bytes`.
pub fn from_hex(hex : StringView) -> Bytes raise CryptoError {
  let clean : Array[UInt16] = []
  for i in 0.. String {
  let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  let sb = StringBuilder()
  let len = data.length()
  let mut i = 0
  while i + 3 <= len {
    let b0 = data[i].to_int()
    let b1 = data[i + 1].to_int()
    let b2 = data[i + 2].to_int()
    let n = (b0 << 16) | (b1 << 8) | b2
    sb.write_char(alphabet[(n >> 18) & 0x3f].to_int().unsafe_to_char())
    sb.write_char(alphabet[(n >> 12) & 0x3f].to_int().unsafe_to_char())
    sb.write_char(alphabet[(n >> 6) & 0x3f].to_int().unsafe_to_char())
    sb.write_char(alphabet[n & 0x3f].to_int().unsafe_to_char())
    i = i + 3
  }
  let rem = len - i
  if rem == 1 {
    let b0 = data[i].to_int()
    let n = b0 << 16
    sb.write_char(alphabet[(n >> 18) & 0x3f].to_int().unsafe_to_char())
    sb.write_char(alphabet[(n >> 12) & 0x3f].to_int().unsafe_to_char())
    sb.write_char('=')
    sb.write_char('=')
  } else if rem == 2 {
    let b0 = data[i].to_int()
    let b1 = data[i + 1].to_int()
    let n = (b0 << 16) | (b1 << 8)
    sb.write_char(alphabet[(n >> 18) & 0x3f].to_int().unsafe_to_char())
    sb.write_char(alphabet[(n >> 12) & 0x3f].to_int().unsafe_to_char())
    sb.write_char(alphabet[(n >> 6) & 0x3f].to_int().unsafe_to_char())
    sb.write_char('=')
  }
  sb.to_string()
}

///|
fn b64_val(c : UInt16) -> Int32 raise CryptoError {
  if c >= 'A' && c <= 'Z' {
    c.to_int() - 'A'.to_int()
  } else if c >= 'a' && c <= 'z' {
    c.to_int() - 'a'.to_int() + 26
  } else if c >= '0' && c <= '9' {
    c.to_int() - '0'.to_int() + 52
  } else if c == '+' {
    62
  } else if c == '/' {
    63
  } else {
    raise CryptoError::InvalidBase64("invalid base64 character")
  }
}

///|
/// Decode standard RFC 4648 Base64 string into `Bytes`.
pub fn from_base64(input : StringView) -> Bytes raise CryptoError {
  let chars : Array[UInt16] = []
  for i in 0..> 4)).to_byte())
      } else if c3 == '=' {
        if g != groups - 1 {
          raise CryptoError::InvalidBase64("malformed base64 padding")
        }
        let v2 = b64_val(c2)
        if (v2 & 0x03) != 0 {
          raise CryptoError::InvalidBase64("non-canonical base64 tail bits")
        }
        out.push(((v0 << 2) | (v1 >> 4)).to_byte())
        out.push((((v1 & 0x0f) << 4) | (v2 >> 2)).to_byte())
      } else {
        let v2 = b64_val(c2)
        let v3 = b64_val(c3)
        out.push(((v0 << 2) | (v1 >> 4)).to_byte())
        out.push((((v1 & 0x0f) << 4) | (v2 >> 2)).to_byte())
        out.push((((v2 & 0x03) << 6) | v3).to_byte())
      }
    }
    Bytes::from_array(out)
  }
}