Skip to content

Commit

Permalink
Runtime: support for blake2b
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Dec 20, 2023
1 parent 67f110b commit 00f6734
Show file tree
Hide file tree
Showing 8 changed files with 440 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .ocamlformat
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ break-separators=before
dock-collection-brackets=false
margin=90
module-item-spacing=sparse
version=0.26.0
version=0.26.1
ocaml-version=4.08.0
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ let runtime =
; effect
; zstd
; runtime_events
; blake2
]

include Files
2 changes: 2 additions & 0 deletions compiler/lib-runtime-files/tests/all.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let%expect_test _ =
+backtrace.js
+bigarray.js
+bigstring.js
+blake2.js
+compare.js
+domain.js
+dynlink.js
Expand Down Expand Up @@ -59,6 +60,7 @@ let%expect_test _ =
+backtrace.js
+bigarray.js
+bigstring.js
+blake2.js
+compare.js
+domain.js
+effect.js
Expand Down
15 changes: 9 additions & 6 deletions compiler/tests-full/stdlib.cma.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -24020,6 +24020,9 @@
cst_Digest_of_hex$1 = "Digest.of_hex",
cst_Digest_substring$1 = "Digest.substring",
cst_Digest_to_hex$1 = "Digest.to_hex",
caml_blake2_final = runtime.caml_blake2_final,
caml_blake2_string = runtime.caml_blake2_string,
caml_blake2_update = runtime.caml_blake2_update,
caml_bytes_unsafe_set = runtime.caml_bytes_unsafe_set,
caml_create_bytes = runtime.caml_create_bytes,
caml_maybe_attach_backtrace = runtime.caml_maybe_attach_backtrace,
Expand Down Expand Up @@ -24122,7 +24125,7 @@
compare = Stdlib_String[10],
equal = Stdlib_String[9];
function string(str){
/*<<digest.ml:85:4>>*/ return /*<<digest.ml:85:4>>*/ runtime.caml_blake2_string
/*<<digest.ml:85:4>>*/ return /*<<digest.ml:85:4>>*/ caml_blake2_string
(hash_length, cst, str, 0, caml_ml_string_length(str));
/*<<digest.ml:85:58>>*/ }
function bytes(b){
Expand All @@ -24140,7 +24143,7 @@
if(_g_)
/*<<digest.ml:92:9>>*/ /*<<digest.ml:92:9>>*/ caml_call1
(Stdlib[1], cst_Digest_substring);
/*<<digest.ml:93:4>>*/ return /*<<digest.ml:93:4>>*/ runtime.caml_blake2_string
/*<<digest.ml:93:4>>*/ return /*<<digest.ml:93:4>>*/ caml_blake2_string
(hash_length, cst, str, ofs, len);
/*<<digest.ml:93:44>>*/ }
function subbytes(b, ofs, len){
Expand All @@ -24160,7 +24163,7 @@
var toread$0 = toread;
/*<<digest.ml:111:8>>*/ for(;;){
if(0 === toread$0)
/*<<digest.ml:111:27>>*/ return /*<<digest.ml:111:27>>*/ runtime.caml_blake2_final
/*<<digest.ml:111:27>>*/ return /*<<digest.ml:111:27>>*/ caml_blake2_final
(ctx, hash_length);
/*<<digest.ml:112:44>>*/ var
/*<<digest.ml:112:44>>*/ _e_ =
Expand All @@ -24172,7 +24175,7 @@
/*<<digest.ml:113:10>>*/ if(0 === n)
/*<<digest.ml:114:15>>*/ throw /*<<digest.ml:114:15>>*/ caml_maybe_attach_backtrace
(Stdlib[12], 1);
/*<<digest.ml:116:12>>*/ /*<<digest.ml:116:12>>*/ runtime.caml_blake2_update
/*<<digest.ml:116:12>>*/ /*<<digest.ml:116:12>>*/ caml_blake2_update
(ctx,
/*<<digest.ml:116:23>>*/ caml_call1(Stdlib_Bytes[44], buf),
0,
Expand All @@ -24187,9 +24190,9 @@
/*<<digest.ml:104:16>>*/ caml_call4
(Stdlib_In_channel[16], ic, buf, 0, buf_size);
/*<<digest.ml:105:8>>*/ if(0 === n$0)
/*<<digest.ml:106:13>>*/ return /*<<digest.ml:106:13>>*/ runtime.caml_blake2_final
/*<<digest.ml:106:13>>*/ return /*<<digest.ml:106:13>>*/ caml_blake2_final
(ctx, hash_length);
/*<<digest.ml:107:14>>*/ /*<<digest.ml:107:14>>*/ runtime.caml_blake2_update
/*<<digest.ml:107:14>>*/ /*<<digest.ml:107:14>>*/ caml_blake2_update
(ctx,
/*<<digest.ml:107:25>>*/ caml_call1(Stdlib_Bytes[44], buf),
0,
Expand Down
76 changes: 76 additions & 0 deletions compiler/tests-ocaml/lib-digest/digests.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
(* TEST
*)

module Test(H: Digest.S) = struct

let string (msg, hh) =
if not ( (H.(equal (string msg) (of_hex hh))))
then (
Printf.printf "Expecting %S\
\nGot %S\n" hh (H.to_hex (H.string msg)); assert false)

let file wlen rlen =
let data = String.init wlen Char.unsafe_chr in
Out_channel.with_open_bin "data.tmp"
(fun oc -> Out_channel.output_string oc data);
let h1 = H.file "data.tmp" in
assert (H.equal h1 (H.string data));
let h2 =
In_channel.with_open_bin "data.tmp"
(fun ic -> H.channel ic rlen) in
assert (H.equal h2 (H.substring data 0 rlen));
Sys.remove "data.tmp"

let run_tests tests =
List.iter string tests;
file 100 99;
file 100_000 10_000
end

(* Test inputs *)

let in1 = ""
let in2 = "a"
let in3 = "abc"
let in4 = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno\
ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
let in5 = String.make 100_000 'a'

(* Test vectors *)

module TestMD5 = Test(Digest.MD5)
let _ = TestMD5.run_tests
[in1, "d41d8cd98f00b204e9800998ecf8427e";
in2, "0cc175b9c0f1b6a831c399e269772661";
in3, "900150983cd24fb0d6963f7d28e17f72";
in4, "03dd8807a93175fb062dfb55dc7d359c";
in5, "1af6d6f2f682f76f80e606aeaaee1680"]

module TestBLAKE512 = Test(Digest.BLAKE512)
let _ = TestBLAKE512.run_tests
[in1, "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419\
d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce";
in2, "333fcb4ee1aa7c115355ec66ceac917c8bfd815bf7587d325aec1864edd24e34\
d5abe2c6b1b5ee3face62fed78dbef802f2a85cb91d455a8f5249d330853cb3c";
in3, "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1\
7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923";
in4, "ce741ac5930fe346811175c5227bb7bfcd47f42612fae46c0809514f9e0e3a11\
ee1773287147cdeaeedff50709aa716341fe65240f4ad6777d6bfaf9726e5e52";
in5, "fe89a110a412012e7cc5c0e05b03b48a6b9d0ba108187826c5ac82ce7aa45e7e\
31b054979ec8ca5acd0bcc85f379d848f90f9d1593358cba8d88c7cd94ea8eee"]

module TestBLAKE256 = Test(Digest.BLAKE256)
let _ = TestBLAKE256.run_tests
[in1, "0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8";
in2, "8928aae63c84d87ea098564d1e03ad813f107add474e56aedd286349c0c03ea4";
in3, "bddd813c634239723171ef3fee98579b94964e3bb1cb3e427262c8c068d52319";
in4, "90a0bcf5e5a67ac1578c2754617994cfc248109275a809a0721feebd1e918738";
in5, "b717c86cf745507ec5373f12f21350eb8550039b4263f7ba6e8df9030b5673c6"]

module TestBLAKE128 = Test(Digest.BLAKE128)
let _ = TestBLAKE128.run_tests
[in1, "cae66941d9efbd404e4d88758ea67670";
in2, "27c35e6e9373877f29e562464e46497e";
in3, "cf4ab791c62b8d2b2109c90275287816";
in4, "8fa81cd08c10a6e4dd94583e6fb48c2f";
in5, "5c4b4b762807b3290e7eee0aa9b18655"]
8 changes: 7 additions & 1 deletion compiler/tests-ocaml/lib-digest/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(executables
(names md5)
(names md5 digests)
(libraries)
(modes js))

Expand All @@ -16,3 +16,9 @@
(deps md5.reference md5.referencejs)
(action
(diff md5.reference md5.referencejs)))

(rule
(alias runtest)
(deps digests.bc.js)
(action
(run node ./digests.bc.js)))
Loading

0 comments on commit 00f6734

Please sign in to comment.