From 3abb84a51bf7ead515debcdf889544b2ada688fd Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Fri, 6 Dec 2024 11:24:26 +0100 Subject: [PATCH] bench/speed: use Urandom and Getentropy generators since the pfortuna benchmark has been moved to bench/speed, remove bench/miou Mirage_crypto_rng_unix: remove getrandom_into --- bench/dune | 9 +-- bench/miou.ml | 91 ----------------------------- bench/speed.ml | 20 +++---- rng/unix/mirage_crypto_rng_unix.mli | 4 -- 4 files changed, 9 insertions(+), 115 deletions(-) delete mode 100644 bench/miou.ml diff --git a/bench/dune b/bench/dune index 9b2b4828..71ad5387 100644 --- a/bench/dune +++ b/bench/dune @@ -2,11 +2,4 @@ (names speed) (modules speed) (libraries mirage-crypto mirage-crypto-rng mirage-crypto-rng.unix - mirage-crypto-pk mirage-crypto-ec mirage-crypto-rng-miou-unix threads.posix)) - -; marking as "(optional)" leads to OCaml-CI failures -; marking with "(package mirage-crypto-rng-miou-unix)" only has an effect with a "public_name" -;(executables -; (names miou) -; (modules miou) -; (libraries mirage-crypto-rng-miou-unix)) + mirage-crypto-pk mirage-crypto-ec mirage-crypto-rng-miou-unix)) diff --git a/bench/miou.ml b/bench/miou.ml deleted file mode 100644 index 27120c32..00000000 --- a/bench/miou.ml +++ /dev/null @@ -1,91 +0,0 @@ -open Mirage_crypto - -module Time = struct - - let time ~n f a = - let t1 = Sys.time () in - for _ = 1 to n do ignore (f a) done ; - let t2 = Sys.time () in - (t2 -. t1) - - let warmup () = - let x = ref 0 in - let rec go start = - if Sys.time () -. start < 1. then begin - for i = 0 to 10000 do x := !x + i done ; - go start - end in - go (Sys.time ()) - -end - -let burn_period = 2.0 - -let sizes = [16; 64; 256; 1024; 8192] -(* let sizes = [16] *) - -let burn f n = - let buf = Mirage_crypto_rng.generate n in - let (t1, i1) = - let rec loop it = - let t = Time.time ~n:it f buf in - if t > 0.2 then (t, it) else loop (it * 10) in - loop 10 in - let iters = int_of_float (float i1 *. burn_period /. t1) in - let time = Time.time ~n:iters f buf in - (iters, time, float (n * iters) /. time) - -let mb = 1024. *. 1024. - -let throughput title f = - Printf.printf "\n* [%s]\n%!" title ; - sizes |> List.iter @@ fun size -> - Gc.full_major () ; - let (iters, time, bw) = burn f size in - Printf.printf " % 5d: %04f MB/s (%d iters in %.03f s)\n%!" - size (bw /. mb) iters time - -let bm name f = (name, fun () -> f name) - -let benchmarks = [ - bm "pfortuna" (fun name -> - let open Mirage_crypto_rng_miou_unix.Pfortuna in - Miou_unix.run ~domains:2 @@ fun () -> - let rng = Mirage_crypto_rng_miou_unix.(initialize (module Pfortuna)) in - let g = create () in - reseed ~g "abcd" ; - throughput name (fun buf -> - let buf = Bytes.unsafe_of_string buf in - generate_into ~g buf ~off:0 (Bytes.length buf)); - Mirage_crypto_rng_miou_unix.kill rng) ; -] - -let help () = - Printf.printf "available benchmarks:\n "; - List.iter (fun (n, _) -> Printf.printf "%s " n) benchmarks ; - Printf.printf "\n%!" - -let runv fs = - Format.printf "accel: %a\n%!" - (fun ppf -> List.iter @@ fun x -> - Format.fprintf ppf "%s " @@ - match x with `XOR -> "XOR" | `AES -> "AES" | `GHASH -> "GHASH") - accelerated; - Time.warmup () ; - List.iter (fun f -> f ()) fs - - -let () = - let seed = "abcd" in - let g = Mirage_crypto_rng.(create ~seed (module Fortuna)) in - Mirage_crypto_rng.set_default_generator g; - match Array.to_list Sys.argv with - | _::(_::_ as args) -> begin - try - let fs = - args |> List.map @@ fun n -> - snd (benchmarks |> List.find @@ fun (n1, _) -> n = n1) in - runv fs - with Not_found -> help () - end - | _ -> help () diff --git a/bench/speed.ml b/bench/speed.ml index 5efef307..1726a0c4 100644 --- a/bench/speed.ml +++ b/bench/speed.ml @@ -480,12 +480,10 @@ let benchmarks = [ throughput_into name (fun dst cs -> DES.ECB.unsafe_encrypt_into ~key cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ; bm "fortuna" (fun name -> - let open Mirage_crypto_rng.Fortuna in - let g = create () in - reseed ~g "abcd" ; + Mirage_crypto_rng_unix.initialize (module Mirage_crypto_rng.Fortuna); throughput name (fun buf -> let buf = Bytes.unsafe_of_string buf in - generate_into ~g buf ~off:0 (Bytes.length buf))) ; + Mirage_crypto_rng.generate_into buf ~off:0 (Bytes.length buf))) ; bm "pfortuna" (fun name -> let open Mirage_crypto_rng_miou_unix.Pfortuna in @@ -498,19 +496,17 @@ let benchmarks = [ generate_into ~g buf ~off:0 (Bytes.length buf)); Mirage_crypto_rng_miou_unix.kill rng) ; - bm "getrandom" (fun name -> + bm "getentropy" (fun name -> + Mirage_crypto_rng_unix.use_getentropy (); throughput name (fun buf -> let buf = Bytes.unsafe_of_string buf in - Mirage_crypto_rng_unix.getrandom_into buf ~off:0 ~len:(Bytes.length buf))) ; + Mirage_crypto_rng.generate_into buf ~off:0 (Bytes.length buf))) ; - bm "urandom-channel" (fun name -> - In_channel.with_open_bin "/dev/urandom" @@ fun ic -> - let m = Mutex.create () in - let finally () = Mutex.unlock m in + bm "urandom" (fun name -> + Mirage_crypto_rng_unix.use_dev_urandom (); throughput name (fun buf -> let buf = Bytes.unsafe_of_string buf in - Mutex.lock m; - Fun.protect ~finally (fun () -> really_input ic buf 0 (Bytes.length buf)))); + Mirage_crypto_rng.generate_into buf ~off:0 (Bytes.length buf))) ; ] let help () = diff --git a/rng/unix/mirage_crypto_rng_unix.mli b/rng/unix/mirage_crypto_rng_unix.mli index 70cf72de..8a358030 100644 --- a/rng/unix/mirage_crypto_rng_unix.mli +++ b/rng/unix/mirage_crypto_rng_unix.mli @@ -12,10 +12,6 @@ val initialize : ?g:'a -> 'a Mirage_crypto_rng.generator -> unit (** [getrandom size] returns a buffer of [size] filled with random bytes. *) val getrandom : int -> string -(** [getrandom_into buf ~off ~len] fills [buf] with random data ([len] octets), - starting at [off]. *) -val getrandom_into : bytes -> off:int -> len:int -> unit - module Urandom : Mirage_crypto_rng.Generator module Getentropy : Mirage_crypto_rng.Generator