Skip to content

Commit

Permalink
dune'd
Browse files Browse the repository at this point in the history
  • Loading branch information
pqwy committed Apr 3, 2019
1 parent 9d46d4c commit 9413cee
Show file tree
Hide file tree
Showing 33 changed files with 148 additions and 376 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ _build
*.install
*.native
*.byte
.merlin

*~
\.\#*
\#*#

gmon.out
.gdb_history
*.prof
perf.data*
*.json
rondom
8 changes: 0 additions & 8 deletions .merlin

This file was deleted.

17 changes: 9 additions & 8 deletions .ocamlinit
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#require "cstruct, zarith, sexplib"
#directory "_build/src"
#load "nocrypto.cma"

(* #require "oUnit" *)
(* #directory "_build/tests" *)
(* #load "testlib.cma" *)
#directory "_build/default/src"
#directory "_build/default/src/.nocrypto.objs/byte"
#directory "_build/default/unix"
#directory "_build/default/unix/.nocrypto_entropy_unix.objs/byte"
#directory "_build/default/lwt"
#directory "_build/default/lwt/.nocrypto_entropy_lwt.objs/byte"

#directory "_build/unix"
#require "cstruct, ocplib-endian, zarith"
#load "nocrypto.cma"
#load "nocrypto_entropy_unix.cmo"
#load "nocrypto_entropy_lwt.cmo"

open Nocrypto
let _ = Nocrypto_entropy_unix.initialize ()
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ env:
- UPDATE_GCC_BINUTILS=1
- TESTS=true
matrix:
- OCAML_VERSION=4.02
- OCAML_VERSION=4.03
- OCAML_VERSION=4.04
- OCAML_VERSION=4.05
Expand All @@ -17,5 +16,6 @@ env:
- OCAML_VERSION=4.06 DEPOPTS="lwt cstruct-lwt"
- OCAML_VERSION=4.04 DEPOPTS="mirage-xen"
- OCAML_VERSION=4.06 DEPOPTS="mirage-solo5 mirage-entropy"
- OCAML_VERSION=4.07
notifications:
email: false
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@ delegating to AES-NI.
## Build

```bash
./pkg/pkg.ml build
--with-unix BOOL
--with-lwt BOOL
--xen BOOL
--freestanding BOOL

./pkg/pkg.ml test
dune build
dune runtest
```

## FAQ
Expand Down Expand Up @@ -59,11 +54,12 @@ Program terminated with signal SIGILL, Illegal instruction.
autodetection yet. You compiled the library with acceleration, but you are using
it on a machine that does not support it.

`pkg/pkg.ml build --accelerate false` force-disables non-portable code.
The environment variable `NOCRYPTO_ACCELERATE` can be used to override
detection:

`pkg/pkg.ml build --accelerate true` force-enables non-portable code.
- `NOCRYPTO_ACCELERATE=false dune build` force-disables non-portable code.
- `NOCRYPTO_ACCELERATE=true dune build` force-enables non-portable code.
- Otherwise, it matches the capabilities of the build machine.

The flag can also be set via the `NOCRYPTO_ACCELERATE` environment variable.
When unset, it maches the capabilities of the build machine.

[![Build Status](https://travis-ci.org/mirleft/ocaml-nocrypto.svg?branch=master)](https://travis-ci.org/mirleft/ocaml-nocrypto)
26 changes: 0 additions & 26 deletions _tags

This file was deleted.

4 changes: 4 additions & 0 deletions bench/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(executables
(names speed)
(modules speed)
(libraries nocrypto))
2 changes: 1 addition & 1 deletion bench/speed.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ let throughput title f =
sizes |> List.iter @@ fun size ->
Gc.full_major () ;
let (iters, time, bw) = burn f size in
Printf.printf " % 5d: %04.f MB/s (%d iters in %.03f s)\n%!"
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)
Expand Down
9 changes: 0 additions & 9 deletions build

This file was deleted.

26 changes: 26 additions & 0 deletions config/cfg.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

let flags = [
"--std=c99"; "-Wall"; "-Wextra"; "-O3";
"-D_DEFAULT_SOURCE";
"-Wno-implicit-fallthrough";
]

let accelenv = "NOCRYPTO_ACCELERATE"
let accelflags = ["-DACCELERATE"; "-mssse3"; "-maes"; "-mpclmul"]
let accelneeds = [`SSSE3; `AES; `PCLMULQDQ]

let accel =
let auto = match Cpuid.supports accelneeds with
Ok true -> accelflags | _ -> [] in
match Sys.getenv accelenv with
"true" -> Format.printf "Forcing acceleration ON.\n"; accelflags
| "false" -> Format.printf "Forcing acceleration OFF.\n"; []
| _ -> auto
| exception Not_found -> auto

module C = Configurator.V1

let output = ref ""
let args = ["-file", Arg.Set_string output, " FILE to dump flags"]
let () = C.main ~name:"name" ~args @@ fun _ ->
C.Flags.write_sexp !output (flags @ accel)
3 changes: 3 additions & 0 deletions config/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(executables
(names cfg)
(libraries dune.configurator result cpuid))
4 changes: 0 additions & 4 deletions doc/api.odocl

This file was deleted.

3 changes: 3 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(lang dune 1.7)
(name nocrypto)
(version %%VERSION_NUM%%)
7 changes: 7 additions & 0 deletions lwt/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(library
(name nocrypto_entropy_lwt)
(public_name nocrypto.lwt)
(synopsis "Unix+Lwt entropy seeding")
(libraries nocrypto nocrypto.unix lwt.unix)
(optional))

30 changes: 16 additions & 14 deletions lwt/nocrypto_entropy_lwt.ml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
open Lwt
open Lwt.Infix
open Nocrypto

let chunk = 32
and period = 30
and device = Nocrypto_entropy_unix.sys_rng


let mvar_map v f =
Lwt_mvar.take v >>= fun x ->
catch (fun () -> f x >>= Lwt_mvar.put v)
(fun exn -> Lwt_mvar.put v x >>= fun () -> fail exn)
Lwt.catch (fun () -> f x >>= Lwt_mvar.put v)
(fun exn -> Lwt_mvar.put v x >>= fun () -> Lwt.fail exn)

let some x = Some x

[@@@ocaml.warning "-3"]

type t = {
fd : Lwt_unix.file_descr ;
Expand All @@ -25,34 +25,36 @@ let background ~period f =
and live = ref false
and period = float period in
fun () ->
let t1 = !last
and t2 = Unix.gettimeofday () in
if (not !live) && (t2 -. t1 >= period) then begin
last := t2 ;
let t = Unix.gettimeofday () in
if (not !live) && (t -. !last >= period) then begin
last := t ;
live := true ;
async @@ fun () -> f () >|= fun () -> live := false
Lwt.async @@ fun () -> f () >|= fun () -> live := false
end

let rec read_cs fd cs =
Lwt_bytes.read fd cs.Cstruct.buffer cs.Cstruct.off cs.Cstruct.len >>=
function 0 -> Lwt.return_unit | n -> read_cs fd (Cstruct.shift cs n)

let attach ~period ?(device = device) g =
Lwt_unix.(openfile device [O_RDONLY] 0) >|= fun fd ->
let buf = Cstruct.create chunk in
let seed () =
Lwt_cstruct.(complete (read fd) buf) >|= fun () -> Rng.reseed ~g buf in
let seed () = read_cs fd buf >|= fun () -> Rng.reseed ~g buf in
let remove =
Lwt_sequence.add_r (background ~period seed) Lwt_main.enter_iter_hooks in
{ g ; fd ; remove }

let stop t =
Lwt_sequence.remove t.remove ;
catch (fun () -> Lwt_unix.close t.fd)
Unix.(function Unix_error (EBADF, _, _) -> return_unit | exn -> fail exn)
Lwt.(catch (fun () -> Lwt_unix.close t.fd)
Unix.(function Unix_error (EBADF, _, _) -> return_unit | exn -> fail exn))

let active = Lwt_mvar.create None

let initialize () =
Nocrypto_entropy_unix.initialize () ;
let g = !Rng.generator in
mvar_map active @@ function
| Some t when t.g == g -> return (Some t)
| Some t when t.g == g -> Lwt.return_some t
| Some t -> stop t >>= fun () -> attach ~period g >|= some
| None -> attach ~period g >|= some
1 change: 0 additions & 1 deletion lwt/nocrypto_lwt.mllib

This file was deleted.

6 changes: 6 additions & 0 deletions mirage/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(library
(name nocrypto_entropy_mirage)
(public_name nocrypto.mirage)
(synopsis "Mirage entropy seeding")
(libraries nocrypto lwt mirage-entropy)
(optional))
1 change: 0 additions & 1 deletion mirage/nocrypto_mirage.mllib

This file was deleted.

5 changes: 0 additions & 5 deletions myocamlbuild.ml

This file was deleted.

29 changes: 29 additions & 0 deletions nocrypto.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
opam-version: "2.0"
homepage: "https://github.com/mirleft/ocaml-nocrypto"
dev-repo: "git+https://github.com/mirleft/ocaml-nocrypto.git"
bug-reports: "https://github.com/mirleft/ocaml-nocrypto/issues"
doc: "https://mirleft.github.io/ocaml-nocrypto/doc"
authors: ["David Kaloper <[email protected]>"]
maintainer: "David Kaloper <[email protected]>"
license: "ISC"
synopsis: "Simple crypto for the modern age"

build: [ ["dune" "subst"] {pinned}
["dune" "build" "-p" name "-j" jobs ]
["dune" "runtest"] {with-test} ]

depends: [
"ocaml" {>= "4.03.0"}
"dune" {build & >= "1.7"}
"cpuid" {build}
"ounit" {with-test}
"cstruct" {>="3.0.0"}
"ocplib-endian"
"zarith"
("mirage-no-xen" | ("mirage-xen" & "zarith-xen"))
("mirage-no-solo5" | ("mirage-solo5" & "zarith-freestanding"))
]

depopts: [ "unix" "lwt" "mirage-entropy" ]

conflicts: [ "mirage-xen" {< "2.2.0"} ]
46 changes: 0 additions & 46 deletions opam

This file was deleted.

Loading

0 comments on commit 9413cee

Please sign in to comment.