1///|
2/// This package is based on the Go implementation found here:
3/// https://cs.opensource.google/go/go/+/refs/tags/go1.23.0:src/crypto/sha256/sha256.go
4/// which has the copyright notice:
5/// Copyright 2009 The Go Authors. All rights reserved.
6/// Use of this source code is governed by a BSD-style
7/// license that can be found in the LICENSE file.
8
9// The size of a sha256 checksum in bytes.
10let 
Int

This package is based on the Go implementation found here: https://cs.opensource.google/go/go/+/refs/tags/go1.23.0:src/crypto/sha256/sha256.go which has the copyright notice: Copyright 2009 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

size
= 32
11 12// The blocksize of sha256 in bytes. 13///| 14let
Int
block_size
= 64
15 16///| 17let
Int
chunk
= 64
18 19///| 20let
UInt
init0
= 0x6A09E667U
21 22///| 23let
UInt
init1
= 0xBB67AE85U
24 25///| 26let
UInt
init2
= 0x3C6EF372U
27 28///| 29let
UInt
init3
= 0xA54FF53AU
30 31///| 32let
UInt
init4
= 0x510E527FU
33 34///| 35let
UInt
init5
= 0x9B05688CU
36 37///| 38let
UInt
init6
= 0x1F83D9ABU
39 40///| 41let
UInt
init7
= 0x5BE0CD19U
42 43///| 44/// `Digest` represents the partial evaluation of a checksum. 45struct Digest { 46
FixedArray[UInt]
h
:
type FixedArray[A]
FixedArray
[
UInt
UInt
] // 8
47
FixedArray[Byte]
x
:
type FixedArray[A]
FixedArray
[
Byte
Byte
] // chunk
48 mut
Int
nx
:
Int
Int
49 mut
UInt64
len
:
UInt64
UInt64
50 // workaround to get rid of compiler warning: 51 // See: https://discord.com/channels/1135479745207869510/1272985768498958387/1275082247484997693 52
String
name
:
String
String
53} 54 55///| 56/// `Digest::new` returns a new, reset Digest, ready to sum. 57pub fn
struct Digest {
  h: FixedArray[UInt]
  x: FixedArray[Byte]
  mut nx: Int
  mut len: UInt64
  name: String
}

Digest represents the partial evaluation of a checksum.

Digest
::
() -> Digest

Digest::new returns a new, reset Digest, ready to sum.

new
() ->
struct Digest {
  h: FixedArray[UInt]
  x: FixedArray[Byte]
  mut nx: Int
  mut len: UInt64
  name: String
}

Digest represents the partial evaluation of a checksum.

Digest
{
58 { 59
FixedArray[UInt]
h
:
type FixedArray[A]
FixedArray
::
(array : Array[UInt]) -> FixedArray[UInt]

Creates a new fixed-size array from a dynamic array. The resulting fixed array will have the same length and elements as the input array.

Parameters:

  • array : A dynamic array containing elements of type T that will be converted to a fixed array.

Returns a new fixed array containing the same elements as the input array.

Example:

test "FixedArray::from_array" {
  let dynamic_array = [1, 2, 3, 4, 5]
  let fixed_array = FixedArray::from_array(dynamic_array)
  inspect(fixed_array, content="[1, 2, 3, 4, 5]")
}
from_array
([
60
UInt
init0
,
UInt
init1
,
UInt
init2
,
UInt
init3
,
UInt
init4
,
UInt
init5
,
UInt
init6
,
UInt
init7
,
61 ]), 62
FixedArray[Byte]
x
:
type FixedArray[A]
FixedArray
::
(len : Int, init : Byte) -> FixedArray[Byte]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

test "FixedArray::make" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")
}

test "panic FixedArray::make/negative_length" {
  ignore(FixedArray::make(-1, 0))
}

WARNING: A common pitfall is creating with the same initial value, for example:

test {
  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)
}

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(
Int
chunk
, b'\x00'),
63
Int
nx
: 0,
64
UInt64
len
: 0,
65 // workaround to get rid of compiler warning: 66
String
name
: "sha256",
67 } 68} 69 70///| 71let
&HashFunc
_trait
: &
trait HashFunc {
  name(Self) -> String
  write(Self, Byte) -> Unit
  check_sum(Self) -> String
}

HashFunc represents a hash algorithm like @crc32, @md5, or @sha256.

HashFunc
=
struct Digest {
  h: FixedArray[UInt]
  x: FixedArray[Byte]
  mut nx: Int
  mut len: UInt64
  name: String
}

Digest represents the partial evaluation of a checksum.

Digest
::
() -> Digest

Digest::new returns a new, reset Digest, ready to sum.

new
()
72 73///| 74pub impl
trait HashFunc {
  name(Self) -> String
  write(Self, Byte) -> Unit
  check_sum(Self) -> String
}

HashFunc represents a hash algorithm like @crc32, @md5, or @sha256.

HashFunc
for
struct Digest {
  h: FixedArray[UInt]
  x: FixedArray[Byte]
  mut nx: Int
  mut len: UInt64
  name: String
}

Digest represents the partial evaluation of a checksum.

Digest
with
(self : Digest) -> String
name
(
Digest
self
) {
75
Digest
self
.
String
name
76} 77 78///| 79/// `check_sum` returns the final sha256sum as a hex string. 80pub impl
trait HashFunc {
  name(Self) -> String
  write(Self, Byte) -> Unit
  check_sum(Self) -> String
}

HashFunc represents a hash algorithm like @crc32, @md5, or @sha256.

HashFunc
for
struct Digest {
  h: FixedArray[UInt]
  x: FixedArray[Byte]
  mut nx: Int
  mut len: UInt64
  name: String
}

Digest represents the partial evaluation of a checksum.

Digest
with
(self : Digest) -> String

check_sum returns the final sha256sum as a hex string.

check_sum
(
Digest
self
) {
81 let
FixedArray[Byte]
digest
=
Digest
self
.
(self : Digest) -> FixedArray[Byte]

sum returns the final sha256sum as a FixedArray[Byte].

sum
()
82 let
@buffer.T
result
=
(size_hint~ : Int) -> @buffer.T

Creates a new extensible buffer with specified initial capacity. If the initial capacity is less than 1, the buffer will be initialized with capacity 1.

Parameters:

  • size_hint : Initial capacity of the buffer in bytes. Defaults to 0.

Returns a new buffer of type T.

Example:

test "new" {
  let buf = @buffer.new(size_hint=10)
  inspect(buf.length(), content="0")
  buf.write_string("test")
  inspect(buf.length(), content="8")
}
@buffer.new
(
Int
size_hint
=2
(self : Int, other : Int) -> Int

Multiplies two 32-bit integers. This is the implementation of the * operator for Int.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns the product of the two integers. If the result overflows the range of Int, it wraps around according to two's complement arithmetic.

Example:

test "Int::op_mul" {
  inspect(42 * 2, content="84")
  inspect(-10 * 3, content="-30")
  let max = 2147483647 // Int.max_value
  inspect(max * 2, content="-2") // Overflow wraps around
}
*
Int

This package is based on the Go implementation found here: https://cs.opensource.google/go/go/+/refs/tags/go1.23.0:src/crypto/sha256/sha256.go which has the copyright notice: Copyright 2009 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

size
)
83 for
Byte
b
in
FixedArray[Byte]
digest
{
84
@buffer.T
result
.
(self : @buffer.T, value : Char) -> Unit

Writes a UTF-16LE encoded character into the buffer. Automatically grows the buffer if necessary.

Parameters:

  • buffer : The buffer to write to.
  • char : The character to be written.

Example:

test "write_char" {
  let buf = @buffer.new()
  buf.write_char('A')
  inspect(buf.contents(), content=
  #|b"\x41\x00"
)
}
write_char
(
(v : Int) -> Char
to_hex
((
Byte
b
.
(self : Byte) -> Int

Converts a byte value to a 32-bit signed integer. The resulting integer will have the same binary representation as the byte value, preserving the numerical value in the range [0, 255].

Parameters:

  • byte : The byte value to be converted to an integer.

Returns a 32-bit signed integer representing the same numerical value as the input byte.

Example:

test "Byte::to_int" {
  let b = b'\xFF' // byte with value 255
  inspect(b.to_int(), content="255")
  let zero = b'\x00'
  inspect(zero.to_int(), content="0")
}
to_int
()
(self : Int, other : Int) -> Int

Performs an arithmetic right shift operation on an integer value. Shifts the bits of the first operand to the right by the number of positions specified by the second operand. The sign bit is preserved and copied to the leftmost positions.

Parameters:

  • self : The integer value to be shifted.
  • shift : The number of positions to shift the bits to the right. Must be non-negative.

Returns an integer representing the result of the arithmetic right shift operation.

Example:

test "Int::op_shr" {
  let n = -16
  inspect(n >> 2, content="-4") // Sign bit is preserved during shift
  let p = 16
  inspect(p >> 2, content="4") // Regular right shift for positive numbers
}
>>
4)
(Int, Int) -> Int
&
0xf))
85
@buffer.T
result
.
(self : @buffer.T, value : Char) -> Unit

Writes a UTF-16LE encoded character into the buffer. Automatically grows the buffer if necessary.

Parameters:

  • buffer : The buffer to write to.
  • char : The character to be written.

Example:

test "write_char" {
  let buf = @buffer.new()
  buf.write_char('A')
  inspect(buf.contents(), content=
  #|b"\x41\x00"
)
}
write_char
(
(v : Int) -> Char
to_hex
(
Byte
b
.
(self : Byte) -> Int

Converts a byte value to a 32-bit signed integer. The resulting integer will have the same binary representation as the byte value, preserving the numerical value in the range [0, 255].

Parameters:

  • byte : The byte value to be converted to an integer.

Returns a 32-bit signed integer representing the same numerical value as the input byte.

Example:

test "Byte::to_int" {
  let b = b'\xFF' // byte with value 255
  inspect(b.to_int(), content="255")
  let zero = b'\x00'
  inspect(zero.to_int(), content="0")
}
to_int
()
(Int, Int) -> Int
&
0xf))
86 } 87
@buffer.T
result
.
(self : @buffer.T) -> Bytes

Returns a copy of the buffer's content as a sequence of bytes.

Parameters:

  • buffer : The buffer to read from.

Returns a Bytes object containing all bytes written to the buffer.

Example:

test "contents" {
  let buf = @buffer.new()
  buf.write_string("Test")
  inspect(
    buf.contents(),
    content=
      #|b"\x54\x00\x65\x00\x73\x00\x74\x00"
    ,
  )
}
contents
().
(self : Bytes, offset~ : Int = .., length~ : Int = ..) -> String

Return an unchecked string, containing the subsequence of self that starts at offset and has length length. Both offset and length are indexed by byte.

Note this function does not validate the encoding of the byte sequence, it simply copy the bytes into a new String.

to_unchecked_string
()
88} 89 90///| 91/// `sum` returns the final sha256sum as a `FixedArray[Byte]`. 92fn
(self : Digest) -> FixedArray[Byte]

sum returns the final sha256sum as a FixedArray[Byte].

sum
(
Digest
self
:
struct Digest {
  h: FixedArray[UInt]
  x: FixedArray[Byte]
  mut nx: Int
  mut len: UInt64
  name: String
}

Digest represents the partial evaluation of a checksum.

Digest
) ->
type FixedArray[A]
FixedArray
[
Byte
Byte
] {
93 // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64. 94 let
FixedArray[Byte]
tmp
=
type FixedArray[A]
FixedArray
::
(len : Int, init : Byte) -> FixedArray[Byte]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

test "FixedArray::make" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")
}

test "panic FixedArray::make/negative_length" {
  ignore(FixedArray::make(-1, 0))
}

WARNING: A common pitfall is creating with the same initial value, for example:

test {
  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)
}

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(64
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
8, b'\x00')
95
FixedArray[Byte]
tmp
[0] = b'\x80'
96 let
Int
t
= if
Digest
self
.
UInt64
len
(self : UInt64, other : UInt64) -> UInt64

Calculates the remainder of dividing one unsigned 64-bit integer by another.

Parameters:

  • self : The dividend unsigned 64-bit integer.
  • other : The divisor unsigned 64-bit integer.

Returns the remainder of the division operation.

Throws a panic if the divisor is zero.

Example:

test "UInt64::op_mod" {
  let a = 17UL
  let b = 5UL
  inspect(a % b, content="2") // 17 divided by 5 gives quotient 3 and remainder 2
  inspect(7UL % 4UL, content="3")
}

test "panic UInt64::op_mod/division_by_zero" {
  let a = 42UL
  ignore(a % 0UL) // Panics: division by zero
}
%
64
(self_ : UInt64, other : UInt64) -> Bool
<
56 {
97 (56UL
(self : UInt64, other : UInt64) -> UInt64

Performs subtraction between two unsigned 64-bit integers. When the result would be negative, the function wraps around using modular arithmetic (2^64).

Parameters:

  • self : The first unsigned 64-bit integer (minuend).
  • other : The second unsigned 64-bit integer to subtract from the first (subtrahend).

Returns the difference between the two numbers, wrapped around if necessary.

Example:

test "UInt64::op_sub" {
  let a = 5UL
  let b = 3UL
  inspect(a - b, content="2")
  let c = 3UL
  let d = 5UL
  inspect(c - d, content="18446744073709551614") // wraps around to 2^64 - 2
}
-
Digest
self
.
UInt64
len
(self : UInt64, other : UInt64) -> UInt64

Calculates the remainder of dividing one unsigned 64-bit integer by another.

Parameters:

  • self : The dividend unsigned 64-bit integer.
  • other : The divisor unsigned 64-bit integer.

Returns the remainder of the division operation.

Throws a panic if the divisor is zero.

Example:

test "UInt64::op_mod" {
  let a = 17UL
  let b = 5UL
  inspect(a % b, content="2") // 17 divided by 5 gives quotient 3 and remainder 2
  inspect(7UL % 4UL, content="3")
}

test "panic UInt64::op_mod/division_by_zero" {
  let a = 42UL
  ignore(a % 0UL) // Panics: division by zero
}
%
64).
(self : UInt64) -> Int

Converts an unsigned 64-bit integer to a 32-bit signed integer. The conversion truncates the value to fit within the 32-bit range, possibly losing information if the input value is too large.

Parameters:

  • self : The unsigned 64-bit integer to be converted.

Returns a 32-bit signed integer representing the lower 32 bits of the input value. If the input value is larger than the maximum value of a 32-bit signed integer (2147483647), the result will be the truncated value interpreted as a signed integer.

Example:

test "UInt64::to_int" {
  let a = 42UL
  inspect(a.to_int(), content="42")
  let b = 18446744073709551615UL // max value of UInt64
  inspect(b.to_int(), content="-1") // truncated to 32 bits
}
to_int
()
98 } else { 99 (64UL
(self : UInt64, other : UInt64) -> UInt64

Adds two 64-bit unsigned integers. This is the implementation of the + operator for UInt64.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the sum of the two unsigned 64-bit integers. If the result overflows, it wraps around modulo 2^64.

Example:

test "UInt64::op_add" {
  let a = 42UL
  let b = 100UL
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 18446744073709551615UL
  inspect(max + 1UL, content="0")
}
+
56UL
(self : UInt64, other : UInt64) -> UInt64

Performs subtraction between two unsigned 64-bit integers. When the result would be negative, the function wraps around using modular arithmetic (2^64).

Parameters:

  • self : The first unsigned 64-bit integer (minuend).
  • other : The second unsigned 64-bit integer to subtract from the first (subtrahend).

Returns the difference between the two numbers, wrapped around if necessary.

Example:

test "UInt64::op_sub" {
  let a = 5UL
  let b = 3UL
  inspect(a - b, content="2")
  let c = 3UL
  let d = 5UL
  inspect(c - d, content="18446744073709551614") // wraps around to 2^64 - 2
}
-
Digest
self
.
UInt64
len
(self : UInt64, other : UInt64) -> UInt64

Calculates the remainder of dividing one unsigned 64-bit integer by another.

Parameters:

  • self : The dividend unsigned 64-bit integer.
  • other : The divisor unsigned 64-bit integer.

Returns the remainder of the division operation.

Throws a panic if the divisor is zero.

Example:

test "UInt64::op_mod" {
  let a = 17UL
  let b = 5UL
  inspect(a % b, content="2") // 17 divided by 5 gives quotient 3 and remainder 2
  inspect(7UL % 4UL, content="3")
}

test "panic UInt64::op_mod/division_by_zero" {
  let a = 42UL
  ignore(a % 0UL) // Panics: division by zero
}
%
64).
(self : UInt64) -> Int

Converts an unsigned 64-bit integer to a 32-bit signed integer. The conversion truncates the value to fit within the 32-bit range, possibly losing information if the input value is too large.

Parameters:

  • self : The unsigned 64-bit integer to be converted.

Returns a 32-bit signed integer representing the lower 32 bits of the input value. If the input value is larger than the maximum value of a 32-bit signed integer (2147483647), the result will be the truncated value interpreted as a signed integer.

Example:

test "UInt64::to_int" {
  let a = 42UL
  inspect(a.to_int(), content="42")
  let b = 18446744073709551615UL // max value of UInt64
  inspect(b.to_int(), content="-1") // truncated to 32 bits
}
to_int
()
100 } 101 102 // Length in bits. 103 let
UInt64
len
=
Digest
self
.
UInt64
len
(self : UInt64, shift : Int) -> UInt64

Performs a bitwise left shift operation on an unsigned 64-bit integer.

Parameters:

  • self : The unsigned 64-bit integer to be shifted.
  • shift : The number of positions to shift the bits to the left. Must be between 0 and 63 inclusive, values outside this range will be masked with & 63.

Returns the result of shifting self left by shift positions. For each position shifted, the rightmost bit is filled with 0, and the leftmost bit is discarded.

Example:

test "UInt64::op_shl" {
  let x = 1UL
  inspect(x << 3, content="8") // 1 shifted left by 3 positions becomes 8
  inspect(x << 63, content="9223372036854775808") // 1 shifted left by 63 positions
}
<<
3
104
(b : FixedArray[Byte], offset : Int, value : UInt64) -> Unit
be_put_uint64
(
FixedArray[Byte]
tmp
,
Int
t
,
UInt64
len
) // append length in bits
105 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Int
t
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
8;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1 {
106
Digest
self
.
(self : Digest, b : Byte) -> Unit

write writes a byte to the digest.

write
(
FixedArray[Byte]
tmp
(FixedArray[Byte], Int) -> Byte

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

test "FixedArray::op_get" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
}

test "panic FixedArray::op_get/out_of_bounds" {
  let arr = FixedArray::make(3, 0)
  ignore(arr[3]) // Index out of bounds
}
[
i])
107 } 108 109 // The previous write ensures that a whole number of 110 // blocks (i.e. a multiple of 64 bytes) have been hashed. 111 if
Digest
self
.
Int
nx
(x : Int, y : Int) -> Bool
!=
0 {
112
() -> Unit
panic
()
113 } 114 115 // Generate digest 116 let
FixedArray[Byte]
digest
=
type FixedArray[A]
FixedArray
::
(len : Int, init : Byte) -> FixedArray[Byte]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

test "FixedArray::make" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")
}

test "panic FixedArray::make/negative_length" {
  ignore(FixedArray::make(-1, 0))
}

WARNING: A common pitfall is creating with the same initial value, for example:

test {
  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)
}

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(
Int

This package is based on the Go implementation found here: https://cs.opensource.google/go/go/+/refs/tags/go1.23.0:src/crypto/sha256/sha256.go which has the copyright notice: Copyright 2009 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

size
, b'\x00')
117
(b : FixedArray[Byte], offset : Int, value : UInt) -> Unit
be_put_uint32
(
FixedArray[Byte]
digest
, 0,
Digest
self
.
FixedArray[UInt]
h
[0])
118
(b : FixedArray[Byte], offset : Int, value : UInt) -> Unit
be_put_uint32
(
FixedArray[Byte]
digest
, 4,
Digest
self
.
FixedArray[UInt]
h
[1])
119
(b : FixedArray[Byte], offset : Int, value : UInt) -> Unit
be_put_uint32
(
FixedArray[Byte]
digest
, 8,
Digest
self
.
FixedArray[UInt]
h
[2])
120
(b : FixedArray[Byte], offset : Int, value : UInt) -> Unit
be_put_uint32
(
FixedArray[Byte]
digest
, 12,
Digest
self
.
FixedArray[UInt]
h
[3])
121
(b : FixedArray[Byte], offset : Int, value : UInt) -> Unit
be_put_uint32
(
FixedArray[Byte]
digest
, 16,
Digest
self
.
FixedArray[UInt]
h
[4])
122
(b : FixedArray[Byte], offset : Int, value : UInt) -> Unit
be_put_uint32
(
FixedArray[Byte]
digest
, 20,
Digest
self
.
FixedArray[UInt]
h
[5])
123
(b : FixedArray[Byte], offset : Int, value : UInt) -> Unit
be_put_uint32
(
FixedArray[Byte]
digest
, 24,
Digest
self
.
FixedArray[UInt]
h
[6])
124
(b : FixedArray[Byte], offset : Int, value : UInt) -> Unit
be_put_uint32
(
FixedArray[Byte]
digest
, 28,
Digest
self
.
FixedArray[UInt]
h
[7])
125
FixedArray[Byte]
digest
126} 127 128///| 129/// `reset` resets a digest for re-use. 130pub fn
(self : Digest) -> Unit

reset resets a digest for re-use.

reset
(
Digest
self
:
struct Digest {
  h: FixedArray[UInt]
  x: FixedArray[Byte]
  mut nx: Int
  mut len: UInt64
  name: String
}

Digest represents the partial evaluation of a checksum.

Digest
) ->
Unit
Unit
{
131
Digest
self
.
FixedArray[UInt]
h
[0] =
UInt
init0
132
Digest
self
.
FixedArray[UInt]
h
[1] =
UInt
init1
133
Digest
self
.
FixedArray[UInt]
h
[2] =
UInt
init2
134
Digest
self
.
FixedArray[UInt]
h
[3] =
UInt
init3
135
Digest
self
.
FixedArray[UInt]
h
[4] =
UInt
init4
136
Digest
self
.
FixedArray[UInt]
h
[5] =
UInt
init5
137
Digest
self
.
FixedArray[UInt]
h
[6] =
UInt
init6
138
Digest
self
.
FixedArray[UInt]
h
[7] =
UInt
init7
139
Digest
self
.
Int
nx
= 0
140
Digest
self
.
UInt64
len
= 0
141} 142 143///| 144/// `write` writes a byte to the digest. 145pub impl
trait HashFunc {
  name(Self) -> String
  write(Self, Byte) -> Unit
  check_sum(Self) -> String
}

HashFunc represents a hash algorithm like @crc32, @md5, or @sha256.

HashFunc
for
struct Digest {
  h: FixedArray[UInt]
  x: FixedArray[Byte]
  mut nx: Int
  mut len: UInt64
  name: String
}

Digest represents the partial evaluation of a checksum.

Digest
with
(self : Digest, b : Byte) -> Unit

write writes a byte to the digest.

write
(
Digest
self
,
Byte
b
) {
146
Digest
self
.
UInt64
len
(self : UInt64, other : UInt64) -> UInt64

Adds two 64-bit unsigned integers. This is the implementation of the + operator for UInt64.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the sum of the two unsigned 64-bit integers. If the result overflows, it wraps around modulo 2^64.

Example:

test "UInt64::op_add" {
  let a = 42UL
  let b = 100UL
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 18446744073709551615UL
  inspect(max + 1UL, content="0")
}
+=
1
147
Digest
self
.
FixedArray[Byte]
x
(FixedArray[Byte], Int, Byte) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
self.
Int
nx
] =
Byte
b
148
Digest
self
.
Int
nx
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+=
1
149 if
Digest
self
.
Int
nx
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

test "Int::op_equal" {
  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
}
==
Int
block_size
{
150
Digest
self
.
(self : Digest) -> Unit
block_generic
()
151
Digest
self
.
Int
nx
= 0
152 } 153} 154 155///| 156let
Array[UInt]
_K
= [
157 0x428a2f98U, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 158 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 159 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 160 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 161 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 162 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 163 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 164 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 165 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 166 0xc67178f2, 167] 168 169///| 170fn
(self : Digest) -> Unit
block_generic
(
Digest
self
:
struct Digest {
  h: FixedArray[UInt]
  x: FixedArray[Byte]
  mut nx: Int
  mut len: UInt64
  name: String
}

Digest represents the partial evaluation of a checksum.

Digest
) ->
Unit
Unit
{
171 let
FixedArray[UInt]
w
=
type FixedArray[A]
FixedArray
::
(len : Int, init : UInt) -> FixedArray[UInt]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

test "FixedArray::make" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")
}

test "panic FixedArray::make/negative_length" {
  ignore(FixedArray::make(-1, 0))
}

WARNING: A common pitfall is creating with the same initial value, for example:

test {
  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)
}

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(64, 0U)
172 173 // 174 let
FixedArray[Byte]
p
=
Digest
self
.
FixedArray[Byte]
x
175 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
16;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1 {
176 let
Int
j
=
Int
i
(self : Int, other : Int) -> Int

Multiplies two 32-bit integers. This is the implementation of the * operator for Int.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns the product of the two integers. If the result overflows the range of Int, it wraps around according to two's complement arithmetic.

Example:

test "Int::op_mul" {
  inspect(42 * 2, content="84")
  inspect(-10 * 3, content="-30")
  let max = 2147483647 // Int.max_value
  inspect(max * 2, content="-2") // Overflow wraps around
}
*
4
177
FixedArray[UInt]
w
(FixedArray[UInt], Int, UInt) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
i] = (
FixedArray[Byte]
p
(FixedArray[Byte], Int) -> Byte

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

test "FixedArray::op_get" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
}

test "panic FixedArray::op_get/out_of_bounds" {
  let arr = FixedArray::make(3, 0)
  ignore(arr[3]) // Index out of bounds
}
[
j].
(self : Byte) -> UInt

Converts a Byte to a UInt.

Parameters:

  • byte : The Byte value to be converted.

Returns the UInt representation of the Byte.

to_uint
()
(self : UInt, shift : Int) -> UInt

Performs a left shift operation on an unsigned 32-bit integer. Each bit in the integer is shifted left by the specified number of positions, and zeros are filled in from the right.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift. Only the least significant 5 bits are used, effectively making the shift count always between 0 and 31.

Returns a new unsigned 32-bit integer that is the result of shifting self left by shift positions.

Example:

test "UInt::op_shl" {
  let x = 1U
  inspect(x << 3, content="8") // Binary: 1 -> 1000
  let y = 0xFFFFFFFFU
  inspect(y << 16, content="4294901760") // All bits after position 16 are discarded
}
<<
24)
(UInt, UInt) -> UInt
|
178 (
FixedArray[Byte]
p
(FixedArray[Byte], Int) -> Byte

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

test "FixedArray::op_get" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
}

test "panic FixedArray::op_get/out_of_bounds" {
  let arr = FixedArray::make(3, 0)
  ignore(arr[3]) // Index out of bounds
}
[
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1].
(self : Byte) -> UInt

Converts a Byte to a UInt.

Parameters:

  • byte : The Byte value to be converted.

Returns the UInt representation of the Byte.

to_uint
()
(self : UInt, shift : Int) -> UInt

Performs a left shift operation on an unsigned 32-bit integer. Each bit in the integer is shifted left by the specified number of positions, and zeros are filled in from the right.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift. Only the least significant 5 bits are used, effectively making the shift count always between 0 and 31.

Returns a new unsigned 32-bit integer that is the result of shifting self left by shift positions.

Example:

test "UInt::op_shl" {
  let x = 1U
  inspect(x << 3, content="8") // Binary: 1 -> 1000
  let y = 0xFFFFFFFFU
  inspect(y << 16, content="4294901760") // All bits after position 16 are discarded
}
<<
16)
(UInt, UInt) -> UInt
|
179 (
FixedArray[Byte]
p
(FixedArray[Byte], Int) -> Byte

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

test "FixedArray::op_get" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
}

test "panic FixedArray::op_get/out_of_bounds" {
  let arr = FixedArray::make(3, 0)
  ignore(arr[3]) // Index out of bounds
}
[
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
2].
(self : Byte) -> UInt

Converts a Byte to a UInt.

Parameters:

  • byte : The Byte value to be converted.

Returns the UInt representation of the Byte.

to_uint
()
(self : UInt, shift : Int) -> UInt

Performs a left shift operation on an unsigned 32-bit integer. Each bit in the integer is shifted left by the specified number of positions, and zeros are filled in from the right.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift. Only the least significant 5 bits are used, effectively making the shift count always between 0 and 31.

Returns a new unsigned 32-bit integer that is the result of shifting self left by shift positions.

Example:

test "UInt::op_shl" {
  let x = 1U
  inspect(x << 3, content="8") // Binary: 1 -> 1000
  let y = 0xFFFFFFFFU
  inspect(y << 16, content="4294901760") // All bits after position 16 are discarded
}
<<
8)
(UInt, UInt) -> UInt
|
180
FixedArray[Byte]
p
(FixedArray[Byte], Int) -> Byte

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

test "FixedArray::op_get" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
}

test "panic FixedArray::op_get/out_of_bounds" {
  let arr = FixedArray::make(3, 0)
  ignore(arr[3]) // Index out of bounds
}
[
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
3].
(self : Byte) -> UInt

Converts a Byte to a UInt.

Parameters:

  • byte : The Byte value to be converted.

Returns the UInt representation of the Byte.

to_uint
()
181 } 182 for
Int
i
= 16;
Int
i
(self_ : Int, other : Int) -> Bool
<
64;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1 {
183 let
UInt
v1
=
FixedArray[UInt]
w
(FixedArray[UInt], Int) -> UInt

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

test "FixedArray::op_get" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
}

test "panic FixedArray::op_get/out_of_bounds" {
  let arr = FixedArray::make(3, 0)
  ignore(arr[3]) // Index out of bounds
}
[
i
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
2]
184 let
UInt
t1
=
(x : UInt, r : Int) -> UInt
rotl
(
UInt
v1
, -17)
(UInt, UInt) -> UInt
^
(x : UInt, r : Int) -> UInt
rotl
(
UInt
v1
, -19)
(UInt, UInt) -> UInt
^
(
UInt
v1
(self : UInt, shift : Int) -> UInt

Performs a logical right shift operation on an unsigned 32-bit integer. The operation shifts all bits to the right by a specified number of positions, filling the leftmost positions with zeros.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift right. If this value is negative, the behavior is undefined. Values larger than 31 are masked with & 31.

Returns a new unsigned 32-bit integer containing the result of the right shift operation.

Example:

test "UInt::op_shr" {
  let x = 0xFF000000U
  inspect(x >> 8, content="16711680") // 0x00FF0000
  inspect(x >> 24, content="255") // 0x000000FF
}

test "UInt::op_shr/large_shift" {
  let x = 0xFF000000U
  inspect(x >> 32, content="4278190080") // Same as x >> 0 due to masking
}
>>
10)
185 let
UInt
v2
=
FixedArray[UInt]
w
(FixedArray[UInt], Int) -> UInt

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

test "FixedArray::op_get" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
}

test "panic FixedArray::op_get/out_of_bounds" {
  let arr = FixedArray::make(3, 0)
  ignore(arr[3]) // Index out of bounds
}
[
i
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
15]
186 let
UInt
t2
=
(x : UInt, r : Int) -> UInt
rotl
(
UInt
v2
, -7)
(UInt, UInt) -> UInt
^
(x : UInt, r : Int) -> UInt
rotl
(
UInt
v2
, -18)
(UInt, UInt) -> UInt
^
(
UInt
v2
(self : UInt, shift : Int) -> UInt

Performs a logical right shift operation on an unsigned 32-bit integer. The operation shifts all bits to the right by a specified number of positions, filling the leftmost positions with zeros.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift right. If this value is negative, the behavior is undefined. Values larger than 31 are masked with & 31.

Returns a new unsigned 32-bit integer containing the result of the right shift operation.

Example:

test "UInt::op_shr" {
  let x = 0xFF000000U
  inspect(x >> 8, content="16711680") // 0x00FF0000
  inspect(x >> 24, content="255") // 0x000000FF
}

test "UInt::op_shr/large_shift" {
  let x = 0xFF000000U
  inspect(x >> 32, content="4278190080") // Same as x >> 0 due to masking
}
>>
3)
187
FixedArray[UInt]
w
(FixedArray[UInt], Int, UInt) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
i] =
UInt
t1
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+
FixedArray[UInt]
w
(FixedArray[UInt], Int) -> UInt

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

test "FixedArray::op_get" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
}

test "panic FixedArray::op_get/out_of_bounds" {
  let arr = FixedArray::make(3, 0)
  ignore(arr[3]) // Index out of bounds
}
[
i
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
7]
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+
UInt
t2
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+
FixedArray[UInt]
w
(FixedArray[UInt], Int) -> UInt

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

test "FixedArray::op_get" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
}

test "panic FixedArray::op_get/out_of_bounds" {
  let arr = FixedArray::make(3, 0)
  ignore(arr[3]) // Index out of bounds
}
[
i
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
16]
188 } 189 // 190 let mut
UInt
a
=
Digest
self
.
FixedArray[UInt]
h
[0]
191 let mut
UInt
b
=
Digest
self
.
FixedArray[UInt]
h
[1]
192 let mut
UInt
c
=
Digest
self
.
FixedArray[UInt]
h
[2]
193 let mut
UInt
d
=
Digest
self
.
FixedArray[UInt]
h
[3]
194 let mut
UInt
e
=
Digest
self
.
FixedArray[UInt]
h
[4]
195 let mut
UInt
f
=
Digest
self
.
FixedArray[UInt]
h
[5]
196 let mut
UInt
g
=
Digest
self
.
FixedArray[UInt]
h
[6]
197 let mut
UInt
h
=
Digest
self
.
FixedArray[UInt]
h
[7]
198 // 199 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
64;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1 {
200 let
UInt
t1
=
UInt
h
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+
201 (
(x : UInt, r : Int) -> UInt
rotl
(
UInt
e
, -6)
(UInt, UInt) -> UInt
^
(x : UInt, r : Int) -> UInt
rotl
(
UInt
e
, -11)
(UInt, UInt) -> UInt
^
(x : UInt, r : Int) -> UInt
rotl
(
UInt
e
, -25))
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+
202 ((
UInt
e
(UInt, UInt) -> UInt
&
UInt
f
)
(UInt, UInt) -> UInt
^
(
UInt
e
.
(self : UInt) -> UInt

Performs a bitwise NOT operation on an unsigned 32-bit integer. Flips all bits in the number (changes each 0 to 1 and each 1 to 0).

Parameters:

  • self : The unsigned 32-bit integer value on which to perform the bitwise NOT operation.

Returns a new unsigned 32-bit integer where each bit is inverted from the input value.

Example:

test "UInt::lnot" {
  let x = 0xFF00U // Binary: 1111_1111_0000_0000
  inspect(x.lnot(), content="4294902015") // Binary: ...0000_0000_1111_1111
}
lnot
()
(UInt, UInt) -> UInt
&
UInt
g
))
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+
203
Array[UInt]
_K
(Array[UInt], Int) -> UInt

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
i]
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+
204
FixedArray[UInt]
w
(FixedArray[UInt], Int) -> UInt

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

test "FixedArray::op_get" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
}

test "panic FixedArray::op_get/out_of_bounds" {
  let arr = FixedArray::make(3, 0)
  ignore(arr[3]) // Index out of bounds
}
[
i]
205 let
UInt
t2
= (
(x : UInt, r : Int) -> UInt
rotl
(
UInt
a
, -2)
(UInt, UInt) -> UInt
^
(x : UInt, r : Int) -> UInt
rotl
(
UInt
a
, -13)
(UInt, UInt) -> UInt
^
(x : UInt, r : Int) -> UInt
rotl
(
UInt
a
, -22))
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+
206 ((
UInt
a
(UInt, UInt) -> UInt
&
UInt
b
)
(UInt, UInt) -> UInt
^
(
UInt
a
(UInt, UInt) -> UInt
&
UInt
c
)
(UInt, UInt) -> UInt
^
(
UInt
b
(UInt, UInt) -> UInt
&
UInt
c
))
207
UInt
h
=
UInt
g
208
UInt
g
=
UInt
f
209
UInt
f
=
UInt
e
210
UInt
e
=
UInt
d
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+
UInt
t1
211
UInt
d
=
UInt
c
212
UInt
c
=
UInt
b
213
UInt
b
=
UInt
a
214
UInt
a
=
UInt
t1
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+
UInt
t2
215 } 216 217 // save state 218
Digest
self
.
FixedArray[UInt]
h
(FixedArray[UInt], Int, UInt) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
0]
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+=
UInt
a
219
Digest
self
.
FixedArray[UInt]
h
(FixedArray[UInt], Int, UInt) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
1]
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+=
UInt
b
220
Digest
self
.
FixedArray[UInt]
h
(FixedArray[UInt], Int, UInt) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
2]
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+=
UInt
c
221
Digest
self
.
FixedArray[UInt]
h
(FixedArray[UInt], Int, UInt) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
3]
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+=
UInt
d
222
Digest
self
.
FixedArray[UInt]
h
(FixedArray[UInt], Int, UInt) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
4]
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+=
UInt
e
223
Digest
self
.
FixedArray[UInt]
h
(FixedArray[UInt], Int, UInt) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
5]
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+=
UInt
f
224
Digest
self
.
FixedArray[UInt]
h
(FixedArray[UInt], Int, UInt) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
6]
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+=
UInt
g
225
Digest
self
.
FixedArray[UInt]
h
(FixedArray[UInt], Int, UInt) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
7]
(self : UInt, other : UInt) -> UInt

Performs addition between two unsigned 32-bit integers. If the result overflows, it wraps around according to the rules of modular arithmetic (2^32).

Parameters:

  • self : The first unsigned 32-bit integer operand.
  • other : The second unsigned 32-bit integer operand to be added.

Returns the sum of the two unsigned integers, wrapped around if necessary.

Example:

test "UInt::op_add" {
  let a = 42U
  let b = 100U
  inspect(a + b, content="142")

  // Demonstrate overflow behavior
  let max = 4294967295U // UInt::max_value
  inspect(max + 1U, content="0")
}
+=
UInt
h
226} 227 228///| 229fn
(b : FixedArray[Byte], offset : Int, value : UInt) -> Unit
be_put_uint32
(
FixedArray[Byte]
b
:
type FixedArray[A]
FixedArray
[
Byte
Byte
],
Int
offset
:
Int
Int
,
UInt
value
:
UInt
UInt
) ->
Unit
Unit
{
230
FixedArray[Byte]
b
(FixedArray[Byte], Int, Byte) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
offset
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
3] = (
UInt
value
(UInt, UInt) -> UInt
&
0xff).
(self : UInt) -> Byte

Converts an unsigned 32-bit integer to a byte by taking its least significant 8 bits. Any bits beyond the first 8 bits are truncated.

Parameters:

  • self : The unsigned 32-bit integer to be converted. Only the least significant 8 bits will be used.

Returns a byte containing the least significant 8 bits of the input integer.

Example:

test "UInt::to_byte" {
  let n = 258U // In binary: 100000010
  inspect(n.to_byte(), content="b'\\x02'") // Only keeps 00000010
  let big = 4294967295U // Maximum value of UInt
  inspect(big.to_byte(), content="b'\\xFF'") // Only keeps 11111111
}
to_byte
()
231
FixedArray[Byte]
b
(FixedArray[Byte], Int, Byte) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
offset
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
2] = ((
UInt
value
(self : UInt, shift : Int) -> UInt

Performs a logical right shift operation on an unsigned 32-bit integer. The operation shifts all bits to the right by a specified number of positions, filling the leftmost positions with zeros.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift right. If this value is negative, the behavior is undefined. Values larger than 31 are masked with & 31.

Returns a new unsigned 32-bit integer containing the result of the right shift operation.

Example:

test "UInt::op_shr" {
  let x = 0xFF000000U
  inspect(x >> 8, content="16711680") // 0x00FF0000
  inspect(x >> 24, content="255") // 0x000000FF
}

test "UInt::op_shr/large_shift" {
  let x = 0xFF000000U
  inspect(x >> 32, content="4278190080") // Same as x >> 0 due to masking
}
>>
8)
(UInt, UInt) -> UInt
&
0xff).
(self : UInt) -> Byte

Converts an unsigned 32-bit integer to a byte by taking its least significant 8 bits. Any bits beyond the first 8 bits are truncated.

Parameters:

  • self : The unsigned 32-bit integer to be converted. Only the least significant 8 bits will be used.

Returns a byte containing the least significant 8 bits of the input integer.

Example:

test "UInt::to_byte" {
  let n = 258U // In binary: 100000010
  inspect(n.to_byte(), content="b'\\x02'") // Only keeps 00000010
  let big = 4294967295U // Maximum value of UInt
  inspect(big.to_byte(), content="b'\\xFF'") // Only keeps 11111111
}
to_byte
()
232
FixedArray[Byte]
b
(FixedArray[Byte], Int, Byte) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
offset
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1] = ((
UInt
value
(self : UInt, shift : Int) -> UInt

Performs a logical right shift operation on an unsigned 32-bit integer. The operation shifts all bits to the right by a specified number of positions, filling the leftmost positions with zeros.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift right. If this value is negative, the behavior is undefined. Values larger than 31 are masked with & 31.

Returns a new unsigned 32-bit integer containing the result of the right shift operation.

Example:

test "UInt::op_shr" {
  let x = 0xFF000000U
  inspect(x >> 8, content="16711680") // 0x00FF0000
  inspect(x >> 24, content="255") // 0x000000FF
}

test "UInt::op_shr/large_shift" {
  let x = 0xFF000000U
  inspect(x >> 32, content="4278190080") // Same as x >> 0 due to masking
}
>>
16)
(UInt, UInt) -> UInt
&
0xff).
(self : UInt) -> Byte

Converts an unsigned 32-bit integer to a byte by taking its least significant 8 bits. Any bits beyond the first 8 bits are truncated.

Parameters:

  • self : The unsigned 32-bit integer to be converted. Only the least significant 8 bits will be used.

Returns a byte containing the least significant 8 bits of the input integer.

Example:

test "UInt::to_byte" {
  let n = 258U // In binary: 100000010
  inspect(n.to_byte(), content="b'\\x02'") // Only keeps 00000010
  let big = 4294967295U // Maximum value of UInt
  inspect(big.to_byte(), content="b'\\xFF'") // Only keeps 11111111
}
to_byte
()
233
FixedArray[Byte]
b
(FixedArray[Byte], Int, Byte) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
offset] = ((
UInt
value
(self : UInt, shift : Int) -> UInt

Performs a logical right shift operation on an unsigned 32-bit integer. The operation shifts all bits to the right by a specified number of positions, filling the leftmost positions with zeros.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift right. If this value is negative, the behavior is undefined. Values larger than 31 are masked with & 31.

Returns a new unsigned 32-bit integer containing the result of the right shift operation.

Example:

test "UInt::op_shr" {
  let x = 0xFF000000U
  inspect(x >> 8, content="16711680") // 0x00FF0000
  inspect(x >> 24, content="255") // 0x000000FF
}

test "UInt::op_shr/large_shift" {
  let x = 0xFF000000U
  inspect(x >> 32, content="4278190080") // Same as x >> 0 due to masking
}
>>
24)
(UInt, UInt) -> UInt
&
0xff).
(self : UInt) -> Byte

Converts an unsigned 32-bit integer to a byte by taking its least significant 8 bits. Any bits beyond the first 8 bits are truncated.

Parameters:

  • self : The unsigned 32-bit integer to be converted. Only the least significant 8 bits will be used.

Returns a byte containing the least significant 8 bits of the input integer.

Example:

test "UInt::to_byte" {
  let n = 258U // In binary: 100000010
  inspect(n.to_byte(), content="b'\\x02'") // Only keeps 00000010
  let big = 4294967295U // Maximum value of UInt
  inspect(big.to_byte(), content="b'\\xFF'") // Only keeps 11111111
}
to_byte
()
234} 235 236///| 237fn
(b : FixedArray[Byte], offset : Int, value : UInt64) -> Unit
be_put_uint64
(
FixedArray[Byte]
b
:
type FixedArray[A]
FixedArray
[
Byte
Byte
],
Int
offset
:
Int
Int
,
UInt64
value
:
UInt64
UInt64
) ->
Unit
Unit
{
238
(b : FixedArray[Byte], offset : Int, value : UInt) -> Unit
be_put_uint32
(
FixedArray[Byte]
b
,
Int
offset
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
4, (
UInt64
value
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

test "UInt64::land" {
  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
}
&
0xffffffff).
(self : UInt64) -> UInt

Converts a 64-bit unsigned integer to a 32-bit unsigned integer by truncating the higher 32 bits.

Parameters:

  • self : The 64-bit unsigned integer to be converted.

Returns a 32-bit unsigned integer containing the lower 32 bits of the input value.

Example:

test "UInt64::to_uint" {
  let big = 0xFFFFFFFFFFFFFFFFUL // max value of UInt64
  inspect(big.to_uint(), content="4294967295") // 0xFFFFFFFF, max value of UInt
  let small = 42UL
  inspect(small.to_uint(), content="42")
}
to_uint
())
239
(b : FixedArray[Byte], offset : Int, value : UInt) -> Unit
be_put_uint32
(
FixedArray[Byte]
b
,
Int
offset
, ((
UInt64
value
(self : UInt64, shift : Int) -> UInt64

Performs a logical right shift operation on a 64-bit unsigned integer by a specified number of bits. All bits shifted beyond the least significant bit are discarded, and zeros are shifted in from the most significant bit.

Parameters:

  • self : The 64-bit unsigned integer to be shifted.
  • shift : The number of positions to shift right. Only the least significant 6 bits of this value are used, effectively making the shift count always between 0 and 63.

Returns a new UInt64 value representing the result of the right shift operation.

Example:

test "UInt64::op_shr" {
  let x = 0xFF00FF00FF00FF00UL
  inspect(x >> 8, content="71777214294589695") // Shifted right by 8 bits
  inspect(x >> 64, content="18374966859414961920") // Equivalent to x >> 0 due to masking
}
>>
32)
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

test "UInt64::land" {
  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
}
&
0xffffffff).
(self : UInt64) -> UInt

Converts a 64-bit unsigned integer to a 32-bit unsigned integer by truncating the higher 32 bits.

Parameters:

  • self : The 64-bit unsigned integer to be converted.

Returns a 32-bit unsigned integer containing the lower 32 bits of the input value.

Example:

test "UInt64::to_uint" {
  let big = 0xFFFFFFFFFFFFFFFFUL // max value of UInt64
  inspect(big.to_uint(), content="4294967295") // 0xFFFFFFFF, max value of UInt
  let small = 42UL
  inspect(small.to_uint(), content="42")
}
to_uint
())
240} 241 242///| 243fn
(x : UInt, r : Int) -> UInt
rotl
(
UInt
x
:
UInt
UInt
,
Int
r
:
Int
Int
) ->
UInt
UInt
{
244 (
UInt
x
(self : UInt, shift : Int) -> UInt

Performs a left shift operation on an unsigned 32-bit integer. Each bit in the integer is shifted left by the specified number of positions, and zeros are filled in from the right.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift. Only the least significant 5 bits are used, effectively making the shift count always between 0 and 31.

Returns a new unsigned 32-bit integer that is the result of shifting self left by shift positions.

Example:

test "UInt::op_shl" {
  let x = 1U
  inspect(x << 3, content="8") // Binary: 1 -> 1000
  let y = 0xFFFFFFFFU
  inspect(y << 16, content="4294901760") // All bits after position 16 are discarded
}
<<
Int
r
)
(UInt, UInt) -> UInt
|
(
UInt
x
(self : UInt, shift : Int) -> UInt

Performs a logical right shift operation on an unsigned 32-bit integer. The operation shifts all bits to the right by a specified number of positions, filling the leftmost positions with zeros.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift right. If this value is negative, the behavior is undefined. Values larger than 31 are masked with & 31.

Returns a new unsigned 32-bit integer containing the result of the right shift operation.

Example:

test "UInt::op_shr" {
  let x = 0xFF000000U
  inspect(x >> 8, content="16711680") // 0x00FF0000
  inspect(x >> 24, content="255") // 0x000000FF
}

test "UInt::op_shr/large_shift" {
  let x = 0xFF000000U
  inspect(x >> 32, content="4278190080") // Same as x >> 0 due to masking
}
>>
(32
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
Int
r
))
245} 246 247///| 248fn
(v : Int) -> Char
to_hex
(
Int
v
:
Int
Int
) ->
Char
Char
{
249 if
Int
v
(self_ : Int, other : Int) -> Bool
<
10 {
250
(Int) -> Char
Int::
(self : Int) -> Char
unsafe_to_char
(
Int
v
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
b'0'.
(self : Byte) -> Int

Converts a byte value to a 32-bit signed integer. The resulting integer will have the same binary representation as the byte value, preserving the numerical value in the range [0, 255].

Parameters:

  • byte : The byte value to be converted to an integer.

Returns a 32-bit signed integer representing the same numerical value as the input byte.

Example:

test "Byte::to_int" {
  let b = b'\xFF' // byte with value 255
  inspect(b.to_int(), content="255")
  let zero = b'\x00'
  inspect(zero.to_int(), content="0")
}
to_int
())
251 } else { 252
(Int) -> Char
Int::
(self : Int) -> Char
unsafe_to_char
(
Int
v
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

test "Int::op_sub" {
  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
}
-
10
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
b'a'.
(self : Byte) -> Int

Converts a byte value to a 32-bit signed integer. The resulting integer will have the same binary representation as the byte value, preserving the numerical value in the range [0, 255].

Parameters:

  • byte : The byte value to be converted to an integer.

Returns a 32-bit signed integer representing the same numerical value as the input byte.

Example:

test "Byte::to_int" {
  let b = b'\xFF' // byte with value 255
  inspect(b.to_int(), content="255")
  let zero = b'\x00'
  inspect(zero.to_int(), content="0")
}
to_int
())
253 } 254} 255