• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

moonbitlang / x / 574

30 Jul 2025 05:30AM UTC coverage: 90.229% (+0.05%) from 90.184%
574

push

github

peter-jerry-ye
chore: bump info

1967 of 2180 relevant lines covered (90.23%)

400.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.14
/crypto/utils.mbt
1
// Copyright 2025 International Digital Economy Academy
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
///| Converts a byte array to a hex string, without any prefix like "0x".
16
let hex_digits : FixedArray[String] = [
17
  "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f",
18
]
19

20
///| print a sequence of byte in hex representation
21
pub fn bytes_to_hex_string(input : FixedArray[Byte]) -> String {
22
  let mut ret = ""
72✔
23
  for i = input.length() - 1; i >= 0; i = i - 1 {
72✔
24
    let byte = input[i]
1,872✔
25
    let high = (byte.to_uint() >> 4) & 0xf
1,872✔
26
    let low = byte.to_int() & 0xf
1,872✔
27
    let high_char = hex_digits[high.reinterpret_as_int()]
1,872✔
28
    let low_char = hex_digits[low]
29
    ret = high_char + low_char + ret
30
  }
31
  ret
32
}
33

34
///|
35
fn uint_to_hex_string(input : UInt) -> String {
36
  let ret = FixedArray::make(8, "0")
×
37
  let mut mut_input = input
38
  for cnt = 7; cnt >= 0; cnt = cnt - 1 {
×
39
    ret[cnt] = hex_digits[(mut_input & 0xf).reinterpret_as_int()]
×
40
    mut_input = mut_input >> 4
41
  }
42
  ret.fold(String::op_add, init="")
×
43
}
44

45
///| print a sequence of uint in hex representation
46
pub fn uints_to_hex_string(input : Iter[UInt]) -> String {
47
  input.map(uint_to_hex_string).fold(String::op_add, init="")
×
48
}
49

50
///|
51
fn uint32(x : Byte) -> UInt {
52
  x.to_int().reinterpret_as_uint()
11,072✔
53
}
54

55
///| convert 4 bytes a byte sequence to a UInt in little endian
56
/// - `x` : A byte sequence consisting of 4 or more bytes
57
/// - `i` : An offset. e.g. i = 4 will convert `x[4~7]` to a UInt.
58
fn u8_to_u32le(x : FixedArray[Byte], i~ : Int = 0) -> UInt {
59
  uint32(x[i]) |
816✔
60
  (uint32(x[i + 1]) << 8) |
816✔
61
  (uint32(x[i + 2]) << 16) |
816✔
62
  (uint32(x[i + 3]) << 24)
816✔
63
}
64

65
/// convert 4 bytes of a byte sequence to a UInt in big endian 
66
/// - `x` : A byte sequence consisting of 4 or more bytes
67
/// - `i` : An offset. e.g. i = 4 will convert `x[4~7]` to a UInt.
68

69
// fn u8_to_u32be(x : Bytes, ~i : Int = 0) -> UInt {
70
//   uint32(x[i]).lsl(24) | uint32(x[i + 1]).lsl(16) | uint32(x[i + 2]).lsl(8) | uint32(
71
//     x[i + 3],
72
//   )
73
// }
74

75
// fn arr_u8_to_u32be(x : Array[Byte], ~i : Int = 0) -> UInt {
76
//   uint32(x[i]).lsl(24) | uint32(x[i + 1]).lsl(16) | uint32(x[i + 2]).lsl(8) | uint32(
77
//     x[i + 3],
78
//   )
79
// }
80

81
///| convert 4 bytes of a byte array to a UInt in big endian 
82
/// - `x` : A byte sequence consisting of 4 or more bytes
83
/// - `i` : An offset. e.g. i = 4 will convert `x[4~7]` to a UInt.
84
fn bytes_u8_to_u32be(x : FixedArray[Byte], i~ : Int = 0) -> UInt {
85
  (uint32(x[i]) << 24) |
1,952✔
86
  (uint32(x[i + 1]) << 16) |
1,952✔
87
  (uint32(x[i + 2]) << 8) |
1,952✔
88
  uint32(x[i + 3])
1,952✔
89
}
90

91
///|
92
fn arr_u32_to_u8be_into(x : Iter[UInt], buffer : FixedArray[Byte]) -> Unit {
93
  x.eachi(fn(i, d) {
50✔
94
    buffer[i * 4 + 0] = (d >> 24).to_byte()
391✔
95
    buffer[i * 4 + 1] = (d >> 16).to_byte()
391✔
96
    buffer[i * 4 + 2] = (d >> 8).to_byte()
391✔
97
    buffer[i * 4 + 3] = d.to_byte()
391✔
98
  })
99
}
100

101
///| rotate a Int `x` left by `n` bit(s)
102
fn rotate_left(x : Int, n : Int) -> Int {
103
  (x << n) | (x.reinterpret_as_uint() >> (32 - n)).reinterpret_as_int()
119,616✔
104
}
105

106
///| rotate a UInt `x` left by `n` bit(s)
107
fn rotate_left_u(x : UInt, n : Int) -> UInt {
108
  (x << n) | (x >> (32 - n))
32,532✔
109
}
110

111
///|
112
fn rotate_right_u(x : UInt, n : Int) -> UInt {
113
  (x >> n) | (x << (32 - n))
45,504✔
114
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc