Skip to content

Commit

Permalink
Added Blake3 implementation using blake3 crate (#1613)
Browse files Browse the repository at this point in the history
* Added Blake3 implementation using blake3 crate

Signed-off-by: Prakhar Singhal <[email protected]>

Added tests for blake3 implementation

Signed-off-by: Prakhar Singhal <[email protected]>

added/integrated blake3 function to crypto lib

Signed-off-by: Prakhar Singhal <[email protected]>

Added tests for blake3 in grammer tests

Signed-off-by: Prakhar Singhal <[email protected]>

Addded external dependency in cargo.toml and fixed formatting

Signed-off-by: Prakhar Singhal <[email protected]>

* minor bug fix

Signed-off-by: Prakhar Singhal <[email protected]>

---------

Signed-off-by: Prakhar Singhal <[email protected]>
  • Loading branch information
prakhar479 authored Sep 3, 2024
1 parent fd9fe09 commit bf8505d
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 3 deletions.
3 changes: 2 additions & 1 deletion compiler_base/3rdparty/rustc_span/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ cfg-if = "0.1.2"
tracing = "0.1"
sha1 = { package = "sha-1", version = "0.10.0" }
sha2 = "0.10.1"
md5 = { package = "md-5", version = "0.10.0" }
md5 = { package = "md-5", version = "0.10.0" }
blake3 = "1.5.4"
10 changes: 10 additions & 0 deletions compiler_base/3rdparty/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use std::rc::Rc;
use std::str::FromStr;

use md5::Digest;
use blake3::Hash as Blake3;
use md5::Md5;
use sha1::Sha1;
use sha2::Sha256;
Expand Down Expand Up @@ -602,6 +603,7 @@ pub enum SourceFileHashAlgorithm {
Md5,
Sha1,
Sha256,
Blake3,
}

impl FromStr for SourceFileHashAlgorithm {
Expand All @@ -612,6 +614,7 @@ impl FromStr for SourceFileHashAlgorithm {
"md5" => Ok(SourceFileHashAlgorithm::Md5),
"sha1" => Ok(SourceFileHashAlgorithm::Sha1),
"sha256" => Ok(SourceFileHashAlgorithm::Sha256),
"blake3" => Ok(SourceFileHashAlgorithm::Blake3),
_ => Err(()),
}
}
Expand Down Expand Up @@ -643,6 +646,12 @@ impl SourceFileHash {
SourceFileHashAlgorithm::Sha256 => {
value.copy_from_slice(&Sha256::digest(data));
}
SourceFileHashAlgorithm::Blake3 => {
let mut hasher = Blake3::new();
hasher.update(data);
let result = hasher.finalize();
value.copy_from_slice(result.as_bytes());
}
}
hash
}
Expand All @@ -663,6 +672,7 @@ impl SourceFileHash {
SourceFileHashAlgorithm::Md5 => 16,
SourceFileHashAlgorithm::Sha1 => 20,
SourceFileHashAlgorithm::Sha256 => 32,
SourceFileHashAlgorithm::Blake3 => 32,
}
}
}
Expand Down
39 changes: 37 additions & 2 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kclvm/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ regex = "1.5.5"
md5 = "0.7.0"
sha2 = "0.9.8"
sha1 = "0.6.0"
blake3 = "1.5.4"
chrono = "0.4.19"
ahash = "0.7.2"
indexmap = "1.0"
Expand Down
2 changes: 2 additions & 0 deletions kclvm/runtime/src/_kclvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ kclvm_value_ref_t* kclvm_crypto_sha384(kclvm_context_t* ctx, kclvm_value_ref_t*

kclvm_value_ref_t* kclvm_crypto_sha512(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* _kwargs);

kclvm_value_ref_t* kclvm_crypto_blake3(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* _kwargs);

kclvm_value_ref_t* kclvm_crypto_uuid(kclvm_context_t* ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);

kclvm_value_ref_t* kclvm_datetime_date(kclvm_context_t* ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);
Expand Down
1 change: 1 addition & 0 deletions kclvm/runtime/src/_kclvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub enum ApiFunc {
kclvm_crypto_sha256,
kclvm_crypto_sha384,
kclvm_crypto_sha512,
kclvm_crypto_blake3,
kclvm_crypto_uuid,
kclvm_datetime_date,
kclvm_datetime_now,
Expand Down
1 change: 1 addition & 0 deletions kclvm/runtime/src/_kclvm_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub fn _kclvm_get_fn_ptr_by_name(name: &str) -> u64 {
"kclvm_crypto_sha256" => crate::kclvm_crypto_sha256 as *const () as u64,
"kclvm_crypto_sha384" => crate::kclvm_crypto_sha384 as *const () as u64,
"kclvm_crypto_sha512" => crate::kclvm_crypto_sha512 as *const () as u64,
"kclvm_crypto_blake3" => crate::kclvm_crypto_blake3 as *const () as u64,
"kclvm_crypto_uuid" => crate::kclvm_crypto_uuid as *const () as u64,
"kclvm_datetime_date" => crate::kclvm_datetime_date as *const () as u64,
"kclvm_datetime_now" => crate::kclvm_datetime_now as *const () as u64,
Expand Down
4 changes: 4 additions & 0 deletions kclvm/runtime/src/_kclvm_api_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,10 @@
// api-spec(c): kclvm_value_ref_t* kclvm_crypto_sha512(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* _kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_crypto_sha512(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %_kwargs);

/// api-spec: kclvm_crypto_blake3
/// api-spec(c): kclvm_value_ref_t* kclvm_crypto_blake3(kclvm_context_t* ctx, kclvm_value_ref_t* args, kclvm_value_ref_t* _kwargs);
/// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_crypto_blake3(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %args, %kclvm_value_ref_t* %_kwargs);

// api-spec: kclvm_crypto_uuid
// api-spec(c): kclvm_value_ref_t* kclvm_crypto_uuid(kclvm_context_t* ctx, kclvm_value_ref_t* _args, kclvm_value_ref_t* _kwargs);
// api-spec(llvm): declare %kclvm_value_ref_t* @kclvm_crypto_uuid(%kclvm_context_t* %ctx, %kclvm_value_ref_t* %_args, %kclvm_value_ref_t* %_kwargs);
Expand Down
29 changes: 29 additions & 0 deletions kclvm/runtime/src/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Copyright The KCL Authors. All rights reserved.

extern crate blake3;
extern crate md5;
extern crate sha1;
extern crate sha2;
Expand Down Expand Up @@ -167,6 +168,34 @@ pub extern "C" fn kclvm_crypto_sha512(
panic!("sha512() missing 1 required positional argument: 'value'");
}

// blake3(value: str, encoding: str = "utf-8") -> str

#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_crypto_blake3(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
_kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let ctx = mut_ptr_as_ref(ctx);
if let Some(s) = args.arg_i_str(0, None) {
let mut hasher = blake3::Hasher::new();
hasher.update(s.as_bytes());
let result = hasher.finalize();

let mut hex = String::with_capacity(2 * Sha256::output_size());
use std::fmt::Write;

for byte in result.as_bytes() {
let _ = write!(&mut hex, "{byte:02x}");
}

return ValueRef::str(hex.as_ref()).into_raw(ctx);
}
panic!("blake3() missing 1 required positional argument: 'value'");
}

#[no_mangle]
#[runtime_fn]
pub extern "C" fn kclvm_crypto_uuid(
Expand Down
21 changes: 21 additions & 0 deletions kclvm/sema/src/builtin/system_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,27 @@ register_crypto_member! {
false,
None,
)
blake3 => Type::function(
None,
Type::str_ref(),
&[
Parameter {
name: "value".to_string(),
ty: Type::str_ref(),
has_default: false,
range: dummy_range(),
},
Parameter {
name: "encoding".to_string(),
ty: Type::str_ref(),
has_default: true,
range: dummy_range(),
},
],
r#"Encrypt the string `value` using `BLAKE3` and the codec registered for encoding."#,
false,
None,
)
uuid => Type::function(
None,
Type::str_ref(),
Expand Down
9 changes: 9 additions & 0 deletions kclvm/tests/test_units/runtime/crypto/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def sha384(self, value: str) -> str:
def sha512(self, value: str) -> str:
return self.dylib.Invoke(f"crypto.sha512", value)

def blake3(self, value: str) -> str:
return self.dylib.Invoke(f"crypto.blake3", value)

def test_md5(self):
self.assertEqual(
self.md5("The quick brown fox jumps over the lazy dog"),
Expand Down Expand Up @@ -77,6 +80,12 @@ def test_sha512(self):
self.sha512(""),
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
)

def test_blake3(self):
self.assertEqual(
self.blake3(""),
"af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262",
)


if __name__ == "__main__":
Expand Down
3 changes: 3 additions & 0 deletions test/grammar/builtins/crypto/blake/blake3/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import crypto

blake3 = crypto.blake3("ABCDEF")
1 change: 1 addition & 0 deletions test/grammar/builtins/crypto/blake/blake3/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
blake3: 61c8c05f3e588c663cd9fbb1d7ad93604ed08cf335497095a020222cc9976cf1

0 comments on commit bf8505d

Please sign in to comment.