diff --git a/dune-project b/dune-project index 6c6d1452..9cb43285 100644 --- a/dune-project +++ b/dune-project @@ -32,7 +32,6 @@ (ocaml (>= "5.1")) (odoc (and :with-doc (>= "2.2.2"))) (ptime (>= "1.1.0")) - (randomconv (= "0.2.0")) (rio (>= "0.0.1")) (telemetry (>= "0.0.1")) (uri (>= "4.4.0")) diff --git a/riot.opam b/riot.opam index e9578891..72348321 100644 --- a/riot.opam +++ b/riot.opam @@ -21,7 +21,6 @@ depends: [ "ocaml" {>= "5.1"} "odoc" {with-doc & >= "2.2.2"} "ptime" {>= "1.1.0"} - "randomconv" {= "0.2.0"} "rio" {>= "0.0.1"} "telemetry" {>= "0.0.1"} "uri" {>= "4.4.0"} diff --git a/riot/lib/crypto.ml b/riot/lib/crypto.ml index a1a5253a..2920108a 100644 --- a/riot/lib/crypto.ml +++ b/riot/lib/crypto.ml @@ -1,16 +1,58 @@ let () = Mirage_crypto_rng_unix.initialize (module Mirage_crypto_rng.Fortuna) +(* Some of the code from below is based on the randomconv repository. + Source: https://github.com/hannesm/randomconv/ + The original code is licensed under the following license: + + Copyright (c) 2016 Hannes Mehnert hannes@mehnert.org + + Permission to use, copy, modify, and distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*) + module Random = struct let string n = Mirage_crypto_rng.generate n - let int8 () = Randomconv.int8 string - let int16 () = Randomconv.int16 string - let int32 () = Randomconv.int32 string - let int64 () = Randomconv.int64 string + let int8 () = Bytes.get_uint8 (Bytes.unsafe_of_string (string 1)) 0 + let int16 () = Bytes.get_uint16_le (Bytes.unsafe_of_string (string 2)) 0 + let int32 () = Bytes.get_int32_le (Bytes.unsafe_of_string (string 4)) 0 + let int64 () = Bytes.get_int64_le (Bytes.unsafe_of_string (string 8)) 0 + + let bitmask n = + let rec go c = function 0 -> c | n -> go (c lsl 1) (n lsr 1) in + go 1 n - 1 + + let rec int ?(max = max_int) () = + if max <= 0 then invalid_arg "bound smaller or equal 0 not supported"; + if max = 1 then 0 + else + let r = + if max <= 256 then int8 () + else if max <= 65536 then int16 () + else + match Sys.word_size with + | 32 -> Int32.to_int (int32 ()) + | 64 -> Int64.to_int (int64 ()) + | _ -> invalid_arg "unknown word size" + in + let r = r land bitmask (pred max) in + if r < max then r else int ~max () + + let float ?(max = 1.) () = + if max <= 0. then invalid_arg "bound smaller or equal 0 not supported"; + let scale = float_of_int max_int and r1 = int () and r2 = int () in + max *. (((float_of_int r1 /. scale) +. float_of_int r2) /. scale) + let char () = Char.chr (int8 ()) - let int ?max () = Randomconv.int ?bound:max string - let float ?max () = Randomconv.float ?bound:max string let bytes n = string n |> Bytes.unsafe_of_string let bytestring n = string n |> Bytestring.of_string - let alphanum () = Char.chr (48 + Randomconv.int ~bound:74 string) + let alphanum () = Char.chr (48 + int ~max:74 ()) let seq n gen = List.init n (fun _ -> gen ()) |> List.to_seq |> String.of_seq end diff --git a/riot/lib/dune b/riot/lib/dune index 181f9d86..e2143b76 100644 --- a/riot/lib/dune +++ b/riot/lib/dune @@ -7,7 +7,6 @@ riot_runtime bytestring telemetry - randomconv mirage-crypto mirage-crypto-rng mirage-crypto-rng.unix))