-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove randomconv as dep (#98)
- Loading branch information
Showing
4 changed files
with
49 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
riot_runtime | ||
bytestring | ||
telemetry | ||
randomconv | ||
mirage-crypto | ||
mirage-crypto-rng | ||
mirage-crypto-rng.unix)) |