Skip to content

Commit

Permalink
feat: remove randomconv as dep (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdevries authored Oct 14, 2024
1 parent e20e08e commit cbef123
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
1 change: 0 additions & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
1 change: 0 additions & 1 deletion riot.opam
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
56 changes: 49 additions & 7 deletions riot/lib/crypto.ml
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
1 change: 0 additions & 1 deletion riot/lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
riot_runtime
bytestring
telemetry
randomconv
mirage-crypto
mirage-crypto-rng
mirage-crypto-rng.unix))

0 comments on commit cbef123

Please sign in to comment.