Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: adapt to latest compiler #92

Merged
merged 6 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/benchmark.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn add(self : Criterion, task : Task) -> Unit {
}

///|
pub fn run(self : Criterion) -> Map[String, TaskResult] {
pub fn Criterion::run(self : Criterion) -> Map[String, TaskResult] {
let map = {}
for task in self.tasks {
map[task.name] = task.run()
Expand Down
45 changes: 24 additions & 21 deletions crypto/chacha.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ test "chachaBlock" {
}

///|
fn stateToBytes(state : FixedArray[UInt]) -> Bytes {
fn from_array(arr : Array[UInt]) -> Bytes {
let rv = Bytes::new(arr.length())
fn stateToBytes(state : FixedArray[UInt]) -> FixedArray[Byte] {
fn from_array(arr : Array[UInt]) -> FixedArray[Byte] {
let rv = FixedArray::make(arr.length(), Byte::default())
for i = 0; i < arr.length(); i = i + 1 {
rv[i] = arr[i].to_byte()
}
Expand Down Expand Up @@ -301,9 +301,9 @@ test "stateToBytes" {
pub fn chacha8(
key : FixedArray[UInt],
counter : UInt,
block : Bytes,
block : FixedArray[Byte],
nonce~ : UInt = 0
) -> Bytes!Error {
) -> FixedArray[Byte]!Error {
if key.length() != 8 {
fail!("Invalid key length -- key must be 8 32-bit unsigned integers")
}
Expand All @@ -319,9 +319,9 @@ pub fn chacha8(
pub fn chacha12(
key : FixedArray[UInt],
counter : UInt,
block : Bytes,
block : FixedArray[Byte],
nonce~ : UInt = 0
) -> Bytes!Error {
) -> FixedArray[Byte]!Error {
if key.length() != 8 {
fail!("Invalid key length -- key must be 8 32-bit unsigned integers")
}
Expand All @@ -337,9 +337,9 @@ pub fn chacha12(
pub fn chacha20(
key : FixedArray[UInt],
counter : UInt,
block : Bytes,
block : FixedArray[Byte],
nonce~ : UInt = 0
) -> Bytes!Error {
) -> FixedArray[Byte]!Error {
if key.length() != 8 {
fail!("Invalid key length -- key must be 8 32-bit unsigned integers")
}
Expand All @@ -350,15 +350,15 @@ pub fn chacha20(
fn chacha(
key : FixedArray[UInt],
counter : UInt,
block : Bytes,
block : FixedArray[Byte],
round : UInt,
nonce : UInt
) -> Bytes {
) -> FixedArray[Byte] {
if block.length() == 0 {
return Bytes::new(0)
return FixedArray::make(0, Byte::default())
}
fn takeToFixedArray(b : Bytes, len : Int) -> FixedArray[Byte] {
let result = FixedArray::make(len, b'0')
fn takeToFixedArray(b : FixedArray[Byte], len : Int) -> FixedArray[Byte] {
let result = FixedArray::make(len, Byte::default())
for i = 0; i < len; i = i + 1 {
result[i] = b[i]
}
Expand All @@ -369,24 +369,27 @@ fn chacha(
op : (Byte, Byte) -> Byte,
state1 : FixedArray[Byte],
state2 : FixedArray[Byte]
) -> Bytes {
let result = Array::make(state1.length(), b'0')
) -> FixedArray[Byte] {
let result = FixedArray::make(state1.length(), Byte::default())
for i = 0; i < state1.length(); i = i + 1 {
result[i] = op(state1[i], state2[i])
}
Bytes::from_array(result)
result
}

fn dropBytes(b : Bytes, len : Int) -> Bytes {
let result = Bytes::new(b.length() - len)
fn dropBytes(b : FixedArray[Byte], len : Int) -> FixedArray[Byte] {
let result = FixedArray::make(b.length() - len, Byte::default())
for i = 0; i < b.length() - len; i = i + 1 {
result[i] = b[len + i]
}
result
}

fn concatBytes(b1 : Bytes, b2 : Bytes) -> Bytes {
let result = Bytes::new(b1.length() + b2.length())
fn concatBytes(
b1 : FixedArray[Byte],
b2 : FixedArray[Byte]
) -> FixedArray[Byte] {
let result = FixedArray::make(b1.length() + b2.length(), Byte::default())
for i = 0; i < b1.length(); i = i + 1 {
result[i] = b1[i]
}
Expand Down
2 changes: 1 addition & 1 deletion crypto/chacha_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test "chachaEncrypt" {
key[6] = 0
key[7] = 0
let plaintext = "abc"
let block = plaintext.to_bytes()
let block = plaintext.to_bytes().to_fixedarray()
inspect!(bytes_to_hex_string(block), content="610062006300")
let encrypted = @crypto.chacha8!(key, 0, block)
let expected = "5f008d2fea5f"
Expand Down
36 changes: 18 additions & 18 deletions crypto/crypto.mbti
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
package moonbitlang/x/crypto

// Values
fn bytes_to_hex_string(Bytes) -> String
fn bytes_to_hex_string(FixedArray[Byte]) -> String

fn chacha12(FixedArray[UInt], UInt, Bytes, nonce~ : UInt = ..) -> Bytes!
fn chacha12(FixedArray[UInt], UInt, FixedArray[Byte], nonce~ : UInt = ..) -> FixedArray[Byte]!

fn chacha20(FixedArray[UInt], UInt, Bytes, nonce~ : UInt = ..) -> Bytes!
fn chacha20(FixedArray[UInt], UInt, FixedArray[Byte], nonce~ : UInt = ..) -> FixedArray[Byte]!

fn chacha8(FixedArray[UInt], UInt, Bytes, nonce~ : UInt = ..) -> Bytes!
fn chacha8(FixedArray[UInt], UInt, FixedArray[Byte], nonce~ : UInt = ..) -> FixedArray[Byte]!

fn md5(Bytes) -> Bytes
fn md5(FixedArray[Byte]) -> FixedArray[Byte]

fn sha1(Bytes) -> Bytes
fn sha1(FixedArray[Byte]) -> FixedArray[Byte]

fn sha224(Bytes) -> Bytes
fn sha224(FixedArray[Byte]) -> FixedArray[Byte]

fn sha224_from_iter(Iter[Byte]) -> Bytes
fn sha224_from_iter(Iter[Byte]) -> FixedArray[Byte]

fn sha256(Bytes) -> Bytes
fn sha256(FixedArray[Byte]) -> FixedArray[Byte]

fn sha256_from_iter(Iter[Byte]) -> Bytes
fn sha256_from_iter(Iter[Byte]) -> FixedArray[Byte]

fn sm3(Bytes) -> Bytes
fn sm3(FixedArray[Byte]) -> FixedArray[Byte]

fn sm3_from_iter(Iter[Byte]) -> Bytes
fn sm3_from_iter(Iter[Byte]) -> FixedArray[Byte]

fn uints_to_hex_string(Iter[UInt]) -> String

// Types and methods
type MD5Context
impl MD5Context {
finalize(Self) -> Bytes
finalize(Self) -> FixedArray[Byte]
new() -> Self
update(Self, Bytes) -> Unit
update(Self, FixedArray[Byte]) -> Unit
}

type SM3Context
impl SM3Context {
finalize(Self) -> Bytes
finalize(Self) -> FixedArray[Byte]
new() -> Self
update(Self, Bytes) -> Unit
update(Self, FixedArray[Byte]) -> Unit
update_from_iter(Self, Iter[Byte]) -> Unit
}

type Sha256Context
impl Sha256Context {
finalize(Self) -> Bytes
finalize(Self) -> FixedArray[Byte]
new(reg~ : FixedArray[UInt] = ..) -> Self
update(Self, Bytes) -> Unit
update(Self, FixedArray[Byte]) -> Unit
update_from_iter(Self, Iter[Byte]) -> Unit
}

Expand Down
Loading
Loading