Skip to main content

Module bigint

Module bigint 

Source
Available on crate feature alloc only.
Expand description

Encoding of unsigned big integers of size known ahead of time

fn encode_bigint(bigint, bits) {
  result = bigint.to_le_bytes()
  # Trim any trailing zero bytes
  while result.len() != 0 {
    if result[result.len() - 1] == 0 {
      result.pop()
    }
  }
  # Pad this to the bound, as necessary
  while result.len() < ((bits + 7) / 8) {
    result.push(0)
  }
  return result
}

fn decode_bigint(bytestream, bits) {
  result = []
  while result.len() != ((bits + 7) / 8) {
    result.push(bytestream.next_byte())
  }
  return BigInt::from_le_bytes(result)
}

This accepts bit bounds for the size of the integers, from which it applies a ceiling division to obtain a byte bound for the length of the encoding, but the bit bound is never strictly enforced. These methods assume the caller will ensure the encoded values were appropriate. The alignment to byte boundaries is to simplify decoding.

Functionsยง

decode_bigint ๐Ÿ”’ std
This function runs in time variable to the input.
encode_bigint ๐Ÿ”’
This function runs in time variable to the input.