Skip to content

Commit

Permalink
Merge branch 'main' into fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoorkin authored Dec 5, 2024
2 parents 3f35e4b + f228079 commit 84b49e0
Show file tree
Hide file tree
Showing 59 changed files with 613 additions and 292 deletions.
7 changes: 7 additions & 0 deletions benchmark/benchmark.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
pub fn Task::new(name : String, f : () -> Unit, count~ : Int = 10) -> Task {
{ name, f, count }
}

///|
pub fn run(self : Task) -> TaskResult {
let now = @ffi.instant_now()
for i = 1; i <= self.count; i = i + 1 {
Expand All @@ -34,6 +36,7 @@ pub fn run(self : Task) -> TaskResult {
{ task: self, average: time / self.count.to_double(), max, min }
}

///|
pub fn to_string(self : TaskResult) -> String {
$|Benchmark Task [\{self.task.name}] Count = \{self.task.count}
$|----------------------------------------
Expand All @@ -43,18 +46,22 @@ pub fn to_string(self : TaskResult) -> String {
$|----------------------------------------
}

///|
pub fn output(self : TaskResult, logger : Logger) -> Unit {
logger.write_string(self.to_string())
}

///|
pub fn Criterion::new() -> Criterion {
{ tasks: [] }
}

///|
pub fn add(self : Criterion, task : Task) -> Unit {
self.tasks.push(task)
}

///|
pub fn run(self : Criterion) -> Map[String, TaskResult] {
let map = {}
for task in self.tasks {
Expand Down
3 changes: 3 additions & 0 deletions benchmark/internal/ffi/ffi_js.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
type Date

///|
pub extern "js" fn instant_now() -> Date =
#| () => new Date()

///|
pub extern "js" fn instant_elapsed_as_secs_f64(x : Date) -> Double =
#|function cost(x){
#| const now = new Date();
Expand Down
3 changes: 3 additions & 0 deletions benchmark/internal/ffi/ffi_native.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
type Instant

///|
pub fn instant_now() -> Instant {
// no implement now
panic()
}

///|
pub fn instant_elapsed_as_secs_f64(_x : Instant) -> Double {
// no implement now
panic()
Expand Down
3 changes: 3 additions & 0 deletions benchmark/internal/ffi/ffi_wasm.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
type Instant

///|
pub fn instant_now() -> Instant = "__moonbit_time_unstable" "instant_now"

///|
pub fn instant_elapsed_as_secs_f64(x : Instant) -> Double = "__moonbit_time_unstable" "instant_elapsed_as_secs_f64"
3 changes: 3 additions & 0 deletions benchmark/types.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
struct Task {
name : String
f : () -> Unit
count : Int
}

///|
pub(readonly) struct TaskResult {
task : Task
average : Double
max : Double
min : Double
}

///|
struct Criterion {
tasks : Array[Task]
}
20 changes: 16 additions & 4 deletions crypto/chacha.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
fn flipWord(word : UInt) -> UInt {
let b0 = (word & 0xff) << 24
let b1 = (word & 0xff00) << 8
Expand All @@ -26,6 +27,7 @@ test "flipWord" {
inspect!(flipped, content="2018915346")
}

///|
fn quarterRound(
state : FixedArray[UInt],
w : Int,
Expand Down Expand Up @@ -75,6 +77,7 @@ test "quarterRound" {
)
}

///|
fn chachaBlockRound(state : FixedArray[UInt]) -> FixedArray[UInt] {
let state1 = quarterRound(state, 0, 4, 8, 12)
let state2 = quarterRound(state1, 1, 5, 9, 13)
Expand Down Expand Up @@ -112,6 +115,7 @@ test "chachaBlockRound" {
)
}

///|
fn chachaBlockLoop(state : FixedArray[UInt], n : UInt) -> FixedArray[UInt] {
match n {
0 => state.copy()
Expand Down Expand Up @@ -158,10 +162,12 @@ test "chachaBlockLoop" {
)
}

///|
fn flipState(state : FixedArray[UInt]) -> FixedArray[UInt] {
state.map(flipWord)
}

///|
fn zipWith(
op : (UInt, UInt) -> UInt,
state1 : FixedArray[UInt],
Expand Down Expand Up @@ -189,7 +195,7 @@ test "zipWith" {
inspect!(result, content="[6, 8, 10, 12]")
}

/// - chacha8: round = 4
///| - chacha8: round = 4
/// - chacha12: round = 6
/// - chacha20: round = 10
fn chachaBlock(
Expand Down Expand Up @@ -240,6 +246,7 @@ test "chachaBlock" {
)
}

///|
fn stateToBytes(state : FixedArray[UInt]) -> Bytes {
fn from_array(arr : Array[UInt]) -> Bytes {
let rv = Bytes::new(arr.length())
Expand Down Expand Up @@ -285,7 +292,7 @@ test "stateToBytes" {
)
}

/// Encrypts a block of data using the ChaCha8 algorithm.
///| Encrypts a block of data using the ChaCha8 algorithm.
/// - [key] must be 8 32-bit unsigned integers.
/// - [counter] is the counter value.
/// - [block] is the block of data to be encrypted.
Expand All @@ -303,7 +310,7 @@ pub fn chacha8(
chacha(key, counter, block, 4, nonce)
}

/// Encrypts a block of data using the ChaCha12 algorithm.
///| Encrypts a block of data using the ChaCha12 algorithm.
/// - [key] must be 8 32-bit unsigned integers.
/// - [counter] is the counter value.
/// - [block] is the block of data to be encrypted.
Expand All @@ -321,7 +328,7 @@ pub fn chacha12(
chacha(key, counter, block, 6, nonce)
}

/// Encrypts a block of data using the ChaCha20 algorithm.
///| Encrypts a block of data using the ChaCha20 algorithm.
/// - [key] must be 8 32-bit unsigned integers.
/// - [counter] is the counter value.
/// - [block] is the block of data to be encrypted.
Expand All @@ -339,6 +346,7 @@ pub fn chacha20(
chacha(key, counter, block, 10, nonce)
}

///|
fn chacha(
key : FixedArray[UInt],
counter : UInt,
Expand Down Expand Up @@ -403,27 +411,31 @@ fn chacha(
res
}

///|
fn quarterRound1(t : (UInt, UInt, UInt, UInt)) -> (UInt, UInt, UInt, UInt) {
let aprime = t.0 + t.1
let dprime = t.3 ^ aprime
let dout = rotate_left_u(dprime, 16)
(aprime, t.1, t.2, dout)
}

///|
fn quarterRound2(t : (UInt, UInt, UInt, UInt)) -> (UInt, UInt, UInt, UInt) {
let cprime = t.2 + t.3
let bprime = t.1 ^ cprime
let bout = rotate_left_u(bprime, 12)
(t.0, bout, cprime, t.3)
}

///|
fn quarterRound3(t : (UInt, UInt, UInt, UInt)) -> (UInt, UInt, UInt, UInt) {
let aprime = t.0 + t.1
let dprime = t.3 ^ aprime
let dout = rotate_left_u(dprime, 8)
(aprime, t.1, t.2, dout)
}

///|
fn quarterRound4(t : (UInt, UInt, UInt, UInt)) -> (UInt, UInt, UInt, UInt) {
let cprime = t.2 + t.3
let bprime = t.1 ^ cprime
Expand Down
18 changes: 13 additions & 5 deletions crypto/md5.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@
// [RFC1321] https://www.ietf.org/rfc/rfc1321.txt
// [Ron Rivest] https://people.csail.mit.edu/rivest/Md5.c
// [md5-0.7.0] https://docs.rs/md5/0.7.0/src/md5/lib.rs.html
///|
struct MD5Context {
state : FixedArray[UInt] // state 'a' 'b' 'c' 'd'
count : FixedArray[UInt]
buffer : Bytes
}

///|
let padding : Bytes = Bytes::make(64, b'\x00')

/// update the state of given context from new `data`
///| update the state of given context from new `data`
pub fn MD5Context::update(self : MD5Context, data : Bytes) -> Unit {
md5_update(self, data)
}

/// an alias of `MD5Context::compute()`
///| an alias of `MD5Context::compute()`
pub fn MD5Context::finalize(self : MD5Context) -> Bytes {
self.md5_compute()
}

/// Instantiate a MD5 context
///| Instantiate a MD5 context
pub fn MD5Context::new() -> MD5Context {
padding[0] = b'\x80'
{
Expand All @@ -44,7 +46,7 @@ pub fn MD5Context::new() -> MD5Context {
}
}

/// compute MD5 digest from given context
///| compute MD5 digest from given context
fn MD5Context::md5_compute(self : MD5Context) -> Bytes {
let input = FixedArray::make(16, 0U)
let idx = ((self.count[0] >> 3) & 0x3f).reinterpret_as_int()
Expand Down Expand Up @@ -89,22 +91,27 @@ fn MD5Context::md5_compute(self : MD5Context) -> Bytes {
// G(X,Y,Z) = XZ v Y not(Z)
// H(X,Y,Z) = X xor Y xor Z
// I(X,Y,Z) = Y xor (X v not(Z))
///|
fn MD5Context::f(x : UInt, y : UInt, z : UInt) -> UInt {
(x & y) | (x.lnot() & z)
}

///|
fn MD5Context::g(x : UInt, y : UInt, z : UInt) -> UInt {
((x ^ y) & z) ^ y
}

///|
fn MD5Context::h(x : UInt, y : UInt, z : UInt) -> UInt {
x ^ y ^ z
}

///|
fn MD5Context::i(x : UInt, y : UInt, z : UInt) -> UInt {
y ^ (x | z.lnot())
}

///|
fn md5_update(ctx : MD5Context, data : Bytes) -> Unit {
let input = FixedArray::make(16, 0U)
let mut idx = ((ctx.count[0] >> 3) & 0x3f).reinterpret_as_int()
Expand All @@ -130,6 +137,7 @@ fn md5_update(ctx : MD5Context, data : Bytes) -> Unit {
}
}

///|
fn md5_transform(state : FixedArray[UInt], input : FixedArray[UInt]) -> Unit {
let mut a = state[0]
let mut b = state[1]
Expand Down Expand Up @@ -217,7 +225,7 @@ fn md5_transform(state : FixedArray[UInt], input : FixedArray[UInt]) -> Unit {
state[3] += d
}

/// Compute the MD5 digest of some `data` based on [RFC1321](https://www.ietf.org/rfc/rfc1321.txt).
///| Compute the MD5 digest of some `data` based on [RFC1321](https://www.ietf.org/rfc/rfc1321.txt).
/// - Note that MD5 is considered _cryptographically broken_.
/// Unless mandated, more secure alternatives should be preferred.
pub fn md5(data : Bytes) -> Bytes {
Expand Down
1 change: 1 addition & 0 deletions crypto/md5_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
fn md5test(s : String) -> String {
bytes_to_hex_string(@crypto.md5(s.to_bytes()))
}
Expand Down
1 change: 1 addition & 0 deletions crypto/sha1.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
pub fn sha1(input : Bytes) -> Bytes {
// Padding
let old_length = input.length()
Expand Down
1 change: 1 addition & 0 deletions crypto/sha1_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
let bytes_to_hex_string = @crypto.bytes_to_hex_string

test "sha1" {
Expand Down
2 changes: 2 additions & 0 deletions crypto/sha224.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
pub fn sha224(data : Bytes) -> Bytes {
let ctx = Sha256Context::new(
reg=[
Expand All @@ -23,6 +24,7 @@ pub fn sha224(data : Bytes) -> Bytes {
arr_u32_to_u8be(ctx.sha256_compute().iter().take(7), 224)
}

///|
pub fn sha224_from_iter(data : Iter[Byte]) -> Bytes {
let ctx = Sha256Context::new(
reg=[
Expand Down
Loading

0 comments on commit 84b49e0

Please sign in to comment.