From 493b550653f47ef53b9ffaf50478e1e017633902 Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Fri, 1 Dec 2017 21:54:27 +0000 Subject: [PATCH 1/6] Reorganise the library This allows to have a core library that can be used to compile against and two implementation (in C and OCaml) to choose for linking with. --- .gitignore | 2 + _tags | 4 +- pkg/META | 28 +++-- pkg/pkg.ml | 7 +- src-c/digestif-c.mllib | 2 + src-c/digestif.ml | 103 +++--------------- src-c/digestif.mli | 16 ++- src-c/digestif.mllib | 5 - src-c/digestif_bigstring.ml | 1 - src-c/digestif_bytes.ml | 1 - src-c/digestif_pp.ml | 1 - .../{digestif.mllib => digestif-ocaml.mllib} | 3 - src-ocaml/digestif.mli | 16 ++- src-ocaml/digestif_bigstring.ml | 1 - src-ocaml/digestif_bytes.ml | 1 - src-ocaml/digestif_pp.ml | 1 - {common => src}/digestif.mllib | 1 - {common => src}/digestif_bigstring.ml | 0 src/digestif_bigstring.mli | 51 +++++++++ {common => src}/digestif_bytes.ml | 0 src/digestif_bytes.mli | 50 +++++++++ {common => src}/digestif_pp.ml | 4 +- src/digestif_pp.mli | 19 ++++ common/digestif.mli => src/digestif_sig.mli | 56 ++-------- 24 files changed, 200 insertions(+), 173 deletions(-) create mode 100644 src-c/digestif-c.mllib mode change 120000 => 100644 src-c/digestif.mli delete mode 100644 src-c/digestif.mllib delete mode 120000 src-c/digestif_bigstring.ml delete mode 120000 src-c/digestif_bytes.ml delete mode 120000 src-c/digestif_pp.ml rename src-ocaml/{digestif.mllib => digestif-ocaml.mllib} (77%) mode change 120000 => 100644 src-ocaml/digestif.mli delete mode 120000 src-ocaml/digestif_bigstring.ml delete mode 120000 src-ocaml/digestif_bytes.ml delete mode 120000 src-ocaml/digestif_pp.ml rename {common => src}/digestif.mllib (83%) rename {common => src}/digestif_bigstring.ml (100%) create mode 100644 src/digestif_bigstring.mli rename {common => src}/digestif_bytes.ml (100%) create mode 100644 src/digestif_bytes.mli rename {common => src}/digestif_pp.ml (98%) create mode 100644 src/digestif_pp.mli rename common/digestif.mli => src/digestif_sig.mli (55%) diff --git a/.gitignore b/.gitignore index 68b8919..cb71ec5 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ _tests lib_test/files zpipe c/dpipe +*.install +*~ diff --git a/_tags b/_tags index c0e16d2..17594ff 100644 --- a/_tags +++ b/_tags @@ -1,7 +1,7 @@ true: safe_string, bin_annot -# : include (not needed to the binary test) -: record_digestif_rakia_stubs +: include +: record_digestif_rakia_stubs : link_digestif_rakia_stubs, use_bigarray : use_bigarray : package(alcotest), package(cstruct) diff --git a/pkg/META b/pkg/META index c810e4a..5cacdac 100644 --- a/pkg/META +++ b/pkg/META @@ -1,27 +1,31 @@ version = "%%VERSION%%" requires = "bigarray" description = "Interface of the library" +archive(byte) = "digestif.cma" +archive(native) = "digestif.cmxa" +plugin(byte) = "digestif.cma" +plugin(native) = "digestif.cmxs" package "c" ( directory = "c" version = "%%VERSION%%" description = "Implementation of hash function in C" - requires = "bigarray" - archive(byte) = "digestif.cma" - archive(native) = "digestif.cmxa" - plugin(byte) = "digestif.cma" - plugin(native) = "digestif.cmxs" - exists_if = "digestif.cma" + requires = "bigarray digestif" + archive(byte) = "digestif-c.cma" + archive(native) = "digestif-c.cmxa" + plugin(byte) = "digestif-c.cma" + plugin(native) = "digestif-c.cmxs" + exists_if = "digestif-c.cma" ) package "ocaml" ( directory = "ocaml" version = "%%VERSION%%" description = "Implementation of hash function in OCaml" - requires = "bigarray" - archive(byte) = "digestif.cma" - archive(native) = "digestif.cmxa" - plugin(byte) = "digestif.cma" - plugin(native) = "digestif.cmxs" - exists_if = "digestif.cma" + requires = "bigarray digestif" + archive(byte) = "digestif-ocaml.cma" + archive(native) = "digestif-ocaml.cmxa" + plugin(byte) = "digestif-ocaml.cma" + plugin(native) = "digestif-ocaml.cmxs" + exists_if = "digestif-ocaml.cma" ) diff --git a/pkg/pkg.ml b/pkg/pkg.ml index 71c8ef2..60d68ae 100644 --- a/pkg/pkg.ml +++ b/pkg/pkg.ml @@ -16,6 +16,7 @@ let () = ; Pkg.doc "CHANGES.md" ; Pkg.clib "src-c/librakia_stubs.clib" ~lib_dst_dir:"c" - ; Pkg.lib "common/digestif.cmi" ~dst:"digestif.cmi" - ; Pkg.mllib ~api:["Digestif"] "src-c/digestif.mllib" ~dst_dir:"c" - ; Pkg.mllib ~api:["Digestif"] "src-ocaml/digestif.mllib" ~dst_dir:"ocaml" ] + ; Pkg.lib "src/digestif_sig.cmi" ~dst:"digestif_sig.cmi" + ; Pkg.mllib "src/digestif.mllib" + ; Pkg.mllib ~api:["Digestif"] "src-c/digestif-c.mllib" ~dst_dir:"c" + ; Pkg.mllib ~api:["Digestif"] "src-ocaml/digestif-ocaml.mllib" ~dst_dir:"ocaml" ] diff --git a/src-c/digestif-c.mllib b/src-c/digestif-c.mllib new file mode 100644 index 0000000..7e6a6f0 --- /dev/null +++ b/src-c/digestif-c.mllib @@ -0,0 +1,2 @@ +Digestif +Rakia_native \ No newline at end of file diff --git a/src-c/digestif.ml b/src-c/digestif.ml index b8f4bab..5a0d611 100644 --- a/src-c/digestif.ml +++ b/src-c/digestif.ml @@ -1,67 +1,11 @@ +module type S = Digestif_sig.S + module Bi = Digestif_bigstring module By = Digestif_bytes module Pp = Digestif_pp module Native = Rakia_native -module type S = -sig - val digest_size : int - - module Bigstring : - sig - type buffer = Native.ba - type ctx - type t = Native.ba - - val init : unit -> ctx - val feed : ctx -> buffer -> unit - val feed_bytes : ctx -> By.t -> unit - val feed_bigstring : ctx -> Bi.t -> unit - val get : ctx -> t - - val digest : buffer -> t - val digestv : buffer list -> t - val hmac : key:buffer -> buffer -> t - val hmacv : key:buffer -> buffer list -> t - - val compare : t -> t -> int - val eq : t -> t -> bool - val neq : t -> t -> bool - - val pp : Format.formatter -> t -> unit - val of_hex : buffer -> t - val to_hex : t -> buffer - end - - module Bytes : - sig - type buffer = Native.st - type ctx - type t = Native.st - - val init : unit -> ctx - val feed : ctx -> buffer -> unit - val feed_bytes : ctx -> By.t -> unit - val feed_bigstring : ctx -> Bi.t -> unit - val get : ctx -> t - - val digest : buffer -> t - val digestv : buffer list -> t - val hmac : key:buffer -> buffer -> t - val hmacv : key:buffer -> buffer list -> t - - val compare : t -> t -> int - val eq : t -> t -> bool - val neq : t -> t -> bool - - val pp : Format.formatter -> t -> unit - val of_hex : buffer -> t - val to_hex : t -> buffer - end -end - -module type Foreign = -sig +module type Foreign = sig open Native module Bigstring : @@ -87,8 +31,7 @@ sig val digest_size : int end -module type Convenience = -sig +module type Convenience = sig type t val compare : t -> t -> int @@ -96,8 +39,7 @@ sig val neq : t -> t -> bool end -module Core (F : Foreign) (D : Desc) = -struct +module Core (F : Foreign) (D : Desc) = struct let block_size = D.block_size and digest_size = D.digest_size and ctx_size = F.ctx_size () @@ -134,8 +76,7 @@ struct let t = init () in ( List.iter (feed t) bufs; get t ) end - module Bigstring = - struct + module Bigstring = struct type buffer = Native.ba type ctx = Native.ctx @@ -167,8 +108,7 @@ struct end end -module Make (F : Foreign) (D : Desc) = -struct +module Make (F : Foreign) (D : Desc) = struct type ctx = Native.ctx module C = Core (F) (D) @@ -199,8 +139,7 @@ struct let hmac ~key msg = hmacv ~key [ msg ] end - module Bigstring = - struct + module Bigstring = struct include C.Bigstring let opad = Bi.init C.block_size (fun _ -> '\x5c') @@ -222,8 +161,7 @@ struct end end -module type ForeignExt = -sig +module type ForeignExt = sig open Native module Bigstring : @@ -246,8 +184,7 @@ sig val key_size : unit -> int end -module Make_BLAKE2 (F : ForeignExt) (D : Desc) : S = -struct +module Make_BLAKE2 (F : ForeignExt) (D : Desc) : Digestif_sig.S = struct let block_size = D.block_size and digest_size = D.digest_size and ctx_size = F.ctx_size () @@ -296,8 +233,7 @@ struct hmacv ~key [ msg ] end - module Bigstring = - struct + module Bigstring = struct type buffer = Native.ba type ctx = Native.ctx @@ -353,16 +289,7 @@ module BLAKE2B = Make_BLAKE2(Native.BLAKE2B) (struct let (digest_size, block_siz module BLAKE2S = Make_BLAKE2(Native.BLAKE2S) (struct let (digest_size, block_size) = (32, 64) end) module RMD160 : S = Make (Native.RMD160) (struct let (digest_size, block_size) = (20, 64) end) -type hash = - [ `MD5 - | `SHA1 - | `SHA224 - | `SHA256 - | `SHA384 - | `SHA512 - | `BLAKE2B - | `BLAKE2S - | `RMD160 ] +type hash = Digestif_sig.hash let module_of = function | `MD5 -> (module MD5 : S) @@ -375,8 +302,7 @@ let module_of = function | `BLAKE2S -> (module BLAKE2S : S) | `RMD160 -> (module RMD160 : S) -module Bytes = -struct +module Bytes = struct let digest hash = let module H = (val (module_of hash)) in H.Bytes.digest @@ -406,8 +332,7 @@ struct H.Bytes.pp end -module Bigstring = -struct +module Bigstring = struct let digest hash = let module H = (val (module_of hash)) in H.Bigstring.digest diff --git a/src-c/digestif.mli b/src-c/digestif.mli deleted file mode 120000 index e5206f2..0000000 --- a/src-c/digestif.mli +++ /dev/null @@ -1 +0,0 @@ -../common/digestif.mli \ No newline at end of file diff --git a/src-c/digestif.mli b/src-c/digestif.mli new file mode 100644 index 0000000..8e31fc6 --- /dev/null +++ b/src-c/digestif.mli @@ -0,0 +1,15 @@ +module type S = Digestif_sig.S + +type hash = Digestif_sig.hash + +val digest_size : hash -> int + +module MD5 : S +module SHA1 : S +module SHA224 : S +module SHA256 : S +module SHA384 : S +module SHA512 : S +module BLAKE2B : S +module BLAKE2S : S +module RMD160 : S diff --git a/src-c/digestif.mllib b/src-c/digestif.mllib deleted file mode 100644 index 741b846..0000000 --- a/src-c/digestif.mllib +++ /dev/null @@ -1,5 +0,0 @@ -Digestif -Digestif_pp -Digestif_bytes -Digestif_bigstring -Rakia_native \ No newline at end of file diff --git a/src-c/digestif_bigstring.ml b/src-c/digestif_bigstring.ml deleted file mode 120000 index b235589..0000000 --- a/src-c/digestif_bigstring.ml +++ /dev/null @@ -1 +0,0 @@ -../common/digestif_bigstring.ml \ No newline at end of file diff --git a/src-c/digestif_bytes.ml b/src-c/digestif_bytes.ml deleted file mode 120000 index 8c3ae79..0000000 --- a/src-c/digestif_bytes.ml +++ /dev/null @@ -1 +0,0 @@ -../common/digestif_bytes.ml \ No newline at end of file diff --git a/src-c/digestif_pp.ml b/src-c/digestif_pp.ml deleted file mode 120000 index ab8959f..0000000 --- a/src-c/digestif_pp.ml +++ /dev/null @@ -1 +0,0 @@ -../common/digestif_pp.ml \ No newline at end of file diff --git a/src-ocaml/digestif.mllib b/src-ocaml/digestif-ocaml.mllib similarity index 77% rename from src-ocaml/digestif.mllib rename to src-ocaml/digestif-ocaml.mllib index 5510a28..e279451 100644 --- a/src-ocaml/digestif.mllib +++ b/src-ocaml/digestif-ocaml.mllib @@ -1,7 +1,4 @@ Digestif -Digestif_pp -Digestif_bytes -Digestif_bigstring Baijiu_xor Baijiu_buffer Baijiu_sha1 diff --git a/src-ocaml/digestif.mli b/src-ocaml/digestif.mli deleted file mode 120000 index e5206f2..0000000 --- a/src-ocaml/digestif.mli +++ /dev/null @@ -1 +0,0 @@ -../common/digestif.mli \ No newline at end of file diff --git a/src-ocaml/digestif.mli b/src-ocaml/digestif.mli new file mode 100644 index 0000000..8e31fc6 --- /dev/null +++ b/src-ocaml/digestif.mli @@ -0,0 +1,15 @@ +module type S = Digestif_sig.S + +type hash = Digestif_sig.hash + +val digest_size : hash -> int + +module MD5 : S +module SHA1 : S +module SHA224 : S +module SHA256 : S +module SHA384 : S +module SHA512 : S +module BLAKE2B : S +module BLAKE2S : S +module RMD160 : S diff --git a/src-ocaml/digestif_bigstring.ml b/src-ocaml/digestif_bigstring.ml deleted file mode 120000 index b235589..0000000 --- a/src-ocaml/digestif_bigstring.ml +++ /dev/null @@ -1 +0,0 @@ -../common/digestif_bigstring.ml \ No newline at end of file diff --git a/src-ocaml/digestif_bytes.ml b/src-ocaml/digestif_bytes.ml deleted file mode 120000 index 8c3ae79..0000000 --- a/src-ocaml/digestif_bytes.ml +++ /dev/null @@ -1 +0,0 @@ -../common/digestif_bytes.ml \ No newline at end of file diff --git a/src-ocaml/digestif_pp.ml b/src-ocaml/digestif_pp.ml deleted file mode 120000 index ab8959f..0000000 --- a/src-ocaml/digestif_pp.ml +++ /dev/null @@ -1 +0,0 @@ -../common/digestif_pp.ml \ No newline at end of file diff --git a/common/digestif.mllib b/src/digestif.mllib similarity index 83% rename from common/digestif.mllib rename to src/digestif.mllib index fa6e4d3..096528d 100644 --- a/common/digestif.mllib +++ b/src/digestif.mllib @@ -1,4 +1,3 @@ -Digestif Digestif_pp Digestif_bytes Digestif_bigstring diff --git a/common/digestif_bigstring.ml b/src/digestif_bigstring.ml similarity index 100% rename from common/digestif_bigstring.ml rename to src/digestif_bigstring.ml diff --git a/src/digestif_bigstring.mli b/src/digestif_bigstring.mli new file mode 100644 index 0000000..7be3cd8 --- /dev/null +++ b/src/digestif_bigstring.mli @@ -0,0 +1,51 @@ +open Bigarray +type t = (char, int8_unsigned_elt, c_layout) Array1.t +val length: t -> int +val create: int -> t +val get: t -> int -> char +val set: t -> int -> char -> unit +val sub: t -> int -> int -> t +val init: int -> (int -> char) -> t +val fill: t -> int -> int -> char -> unit +val copy: t -> t +val get_u8: t -> int -> int +external get_u16: t -> int -> int = "%caml_bigstring_get16u" +external get_u32: t -> int -> int32 = "%caml_bigstring_get32u" +external get_u64: t -> int -> int64 = "%caml_bigstring_get64u" +val get_nat: t -> int -> nativeint +val set_u8: t -> int -> int -> unit +external set_u16: t -> int -> int -> unit = "%caml_bigstring_set16u" +external set_u32: t -> int -> int32 -> unit = "%caml_bigstring_set32u" +external set_u64: t -> int -> int64 -> unit = "%caml_bigstring_set64u" +val set_nat: t -> int -> nativeint -> unit +val to_string: t -> string +val blit: t -> int -> t -> int -> int -> unit +val blit_from_bytes: bytes -> int -> t -> int -> int -> unit +val blit_from_bigstring: t -> int -> t -> int -> int -> unit +val rpad: t -> int -> char -> t +val compare: t -> t -> int +val eq: t -> t -> bool +val neq: t -> t -> bool +val iter: (char -> unit) -> t -> unit +val empty: t +external swap32: int32 -> int32 = "%bswap_int32" +external swap64: int64 -> int64 = "%bswap_int64" +external swapnat: nativeint -> nativeint = "%bswap_native" +val cpu_to_be32: t -> int -> int32 -> unit +val cpu_to_le32: t -> int -> int32 -> unit +val cpu_to_be64: t -> int -> int64 -> unit +val cpu_to_le64: t -> int -> int64 -> unit +val be32_to_cpu: t -> int -> int32 +val be32_from_bigstring_to_cpu: t -> int -> int32 +val be32_from_bytes_to_cpu: Bytes.t -> int -> int32 +val le32_to_cpu: t -> int -> int32 +val le32_from_bigstring_to_cpu: t -> int -> int32 +val le32_from_bytes_to_cpu: Bytes.t -> int -> int32 +val be64_to_cpu: t -> int -> int64 +val be64_from_bigstring_to_cpu: t -> int -> int64 +val be64_from_bytes_to_cpu: Bytes.t -> int -> int64 +val le64_to_cpu: t -> int -> int64 +val le64_from_bigstring_to_cpu: t -> int -> int64 +val le64_from_bytes_to_cpu: Bytes.t -> int -> int64 +val benat_to_cpu: t -> int -> nativeint +val cpu_to_benat: t -> int -> nativeint -> unit diff --git a/common/digestif_bytes.ml b/src/digestif_bytes.ml similarity index 100% rename from common/digestif_bytes.ml rename to src/digestif_bytes.ml diff --git a/src/digestif_bytes.mli b/src/digestif_bytes.mli new file mode 100644 index 0000000..c248c2a --- /dev/null +++ b/src/digestif_bytes.mli @@ -0,0 +1,50 @@ +type t = Bytes.t +val length: t -> int +val create: int -> t +val get: t -> int -> char +val set: t -> int -> char -> unit +val sub: t -> int -> int -> t +val init: int -> (int -> char) -> t +val fill: t -> int -> int -> char -> unit +val copy: t -> t +val get_u8: t -> int -> int +external get_u16: t -> int -> int = "%caml_string_get16u" +external get_u32: t -> int -> int32 = "%caml_string_get32u" +external get_u64: t -> int -> int64 = "%caml_string_get64u" +val get_nat: t -> int -> nativeint +val set_u8: t -> int -> int -> unit +external set_u16: t -> int -> int -> unit = "%caml_string_set16u" +external set_u32: t -> int -> int32 -> unit = "%caml_string_set32u" +external set_u64: t -> int -> int64 -> unit = "%caml_string_set64u" +val set_nat: t -> int -> nativeint -> unit +val to_string: t -> string +val blit: t -> int -> t -> int -> int -> unit +val blit_from_bytes: bytes -> int -> t -> int -> int -> unit +val blit_from_bigstring: Digestif_bigstring.t -> int -> t -> int -> int -> unit +val rpad: t -> int -> char -> t +val compare: t -> t -> int +val eq: t -> t -> bool +val neq: t -> t -> bool +val iter: (char -> unit) -> t -> unit +val empty: t +external swap32: int32 -> int32 = "%bswap_int32" +external swap64: int64 -> int64 = "%bswap_int64" +external swapnat: nativeint -> nativeint = "%bswap_native" +val cpu_to_be32: t -> int -> int32 -> unit +val cpu_to_le32: t -> int -> int32 -> unit +val cpu_to_be64: t -> int -> int64 -> unit +val cpu_to_le64: t -> int -> int64 -> unit +val be32_to_cpu: t -> int -> int32 +val be32_from_bigstring_to_cpu: Digestif_bigstring.t -> int -> int32 +val be32_from_bytes_to_cpu: Bytes.t -> int -> int32 +val le32_to_cpu: t -> int -> int32 +val le32_from_bigstring_to_cpu: Digestif_bigstring.t -> int -> int32 +val le32_from_bytes_to_cpu: Bytes.t -> int -> int32 +val be64_to_cpu: t -> int -> int64 +val be64_from_bigstring_to_cpu: Digestif_bigstring.t -> int -> int64 +val be64_from_bytes_to_cpu: Bytes.t -> int -> int64 +val le64_to_cpu: t -> int -> int64 +val le64_from_bigstring_to_cpu: Digestif_bigstring.t -> int -> int64 +val le64_from_bytes_to_cpu: Bytes.t -> int -> int64 +val benat_to_cpu: t -> int -> nativeint +val cpu_to_benat: t -> int -> nativeint -> unit diff --git a/common/digestif_pp.ml b/src/digestif_pp.ml similarity index 98% rename from common/digestif_pp.ml rename to src/digestif_pp.ml index 62a4581..df2e4c9 100644 --- a/common/digestif_pp.ml +++ b/src/digestif_pp.ml @@ -13,8 +13,7 @@ sig val digest_size : int end -module Make (S : B) (D : D) = -struct +module Make (S : B) (D : D) = struct let to_hex hash = let res = S.create (D.digest_size * 2) in @@ -69,4 +68,3 @@ struct for i = 0 to D.digest_size - 1 do Format.fprintf fmt "%02x" (Char.code (S.get hash i)) done end - diff --git a/src/digestif_pp.mli b/src/digestif_pp.mli new file mode 100644 index 0000000..01e24da --- /dev/null +++ b/src/digestif_pp.mli @@ -0,0 +1,19 @@ +module type B = sig + type t + + val create : int -> t + val iter : (char -> unit) -> t -> unit + val set : t -> int -> char -> unit + val get : t -> int -> char +end + +module type D = sig + val digest_size : int +end + +module Make (S : B) (D : D): sig + val to_hex : S.t -> S.t + val fold_s : ('a -> char -> 'a) -> 'a -> S.t -> 'a + val of_hex : S.t -> S.t + val pp : Format.formatter -> S.t -> unit +end diff --git a/common/digestif.mli b/src/digestif_sig.mli similarity index 55% rename from common/digestif.mli rename to src/digestif_sig.mli index c1eab89..35801d1 100644 --- a/common/digestif.mli +++ b/src/digestif_sig.mli @@ -1,19 +1,18 @@ -module Bi : module type of Digestif_bigstring +module Bigstring = Digestif_bigstring + +module type S = sig -module type S = -sig val digest_size : int - module Bigstring : - sig - type buffer = Bi.t + module Bigstring: sig + type buffer = Bigstring.t type ctx - type t = Bi.t + type t = Bigstring.t val init : unit -> ctx val feed : ctx -> buffer -> unit val feed_bytes : ctx -> Bytes.t -> unit - val feed_bigstring : ctx -> Bi.t -> unit + val feed_bigstring : ctx -> Bigstring.t -> unit val get : ctx -> t val digest : buffer -> t @@ -30,8 +29,7 @@ sig val to_hex : t -> buffer end - module Bytes : - sig + module Bytes: sig type buffer = Bytes.t type ctx type t = Bytes.t @@ -39,7 +37,7 @@ sig val init : unit -> ctx val feed : ctx -> buffer -> unit val feed_bytes : ctx -> Bytes.t -> unit - val feed_bigstring : ctx -> Bi.t -> unit + val feed_bigstring : ctx -> Bigstring.t -> unit val get : ctx -> t val digest : buffer -> t @@ -57,16 +55,6 @@ sig end end -module MD5 : S -module SHA1 : S -module SHA224 : S -module SHA256 : S -module SHA384 : S -module SHA512 : S -module BLAKE2B : S -module BLAKE2S : S -module RMD160 : S - type hash = [ `MD5 | `SHA1 @@ -77,29 +65,3 @@ type hash = | `BLAKE2B | `BLAKE2S | `RMD160 ] - -module Bytes : -sig - val digest : hash -> Bytes.t -> Bytes.t - val digestv : hash -> Bytes.t list -> Bytes.t - val mac : hash -> key:Bytes.t -> Bytes.t -> Bytes.t - val macv : hash -> key:Bytes.t -> Bytes.t list -> Bytes.t - - val pp : hash -> Format.formatter -> Bytes.t -> unit - val of_hex : hash -> Bytes.t -> Bytes.t - val to_hex : hash -> Bytes.t -> Bytes.t -end - -module Bigstring : -sig - val digest : hash -> Bi.t -> Bi.t - val digestv : hash -> Bi.t list -> Bi.t - val mac : hash -> key:Bi.t -> Bi.t -> Bi.t - val macv : hash -> key:Bi.t -> Bi.t list -> Bi.t - - val pp : hash -> Format.formatter -> Bi.t -> unit - val of_hex : hash -> Bi.t -> Bi.t - val to_hex : hash -> Bi.t -> Bi.t -end - -val digest_size : hash -> int From 027f9256e85ce4f7b2f4ac02c572851ce6b6ec7c Mon Sep 17 00:00:00 2001 From: Romain Calascibetta Date: Sun, 3 Dec 2017 01:00:19 -0800 Subject: [PATCH 2/6] Fixed signature and tests --- src-c/digestif.ml | 7 +++++ src-c/digestif.mli | 8 ++++++ src-ocaml/digestif.ml | 64 ++++++------------------------------------ src-ocaml/digestif.mli | 8 ++++++ src/digestif_sig.mli | 14 +++++++++ 5 files changed, 45 insertions(+), 56 deletions(-) diff --git a/src-c/digestif.ml b/src-c/digestif.ml index 5a0d611..7b63b1e 100644 --- a/src-c/digestif.ml +++ b/src-c/digestif.ml @@ -1,4 +1,5 @@ module type S = Digestif_sig.S +module type T = Digestif_sig.T module Bi = Digestif_bigstring module By = Digestif_bytes @@ -303,6 +304,9 @@ let module_of = function | `RMD160 -> (module RMD160 : S) module Bytes = struct + type t = Bytes.t + type buffer = Bytes.t + let digest hash = let module H = (val (module_of hash)) in H.Bytes.digest @@ -333,6 +337,9 @@ module Bytes = struct end module Bigstring = struct + type t = Bi.t + type buffer = Bi.t + let digest hash = let module H = (val (module_of hash)) in H.Bigstring.digest diff --git a/src-c/digestif.mli b/src-c/digestif.mli index 8e31fc6..318430d 100644 --- a/src-c/digestif.mli +++ b/src-c/digestif.mli @@ -1,4 +1,5 @@ module type S = Digestif_sig.S +module type T = Digestif_sig.T type hash = Digestif_sig.hash @@ -13,3 +14,10 @@ module SHA512 : S module BLAKE2B : S module BLAKE2S : S module RMD160 : S + +module Bytes : T + with type t = Bytes.t + and type buffer = Bytes.t +module Bigstring : T + with type t = Digestif_sig.Bigstring.t + and type buffer = Digestif_sig.Bigstring.t diff --git a/src-ocaml/digestif.ml b/src-ocaml/digestif.ml index 8aac790..9ade085 100644 --- a/src-ocaml/digestif.ml +++ b/src-ocaml/digestif.ml @@ -3,62 +3,8 @@ module By = Digestif_bytes module Pp = Digestif_pp module Xor = Baijiu_xor -module type S = -sig - val digest_size : int - - module Bigstring : - sig - type buffer = Bi.t - type ctx - type t = Bi.t - - val init : unit -> ctx - val feed : ctx -> buffer -> unit - val feed_bytes : ctx -> Bytes.t -> unit - val feed_bigstring : ctx -> Bi.t -> unit - val get : ctx -> t - - val digest : buffer -> t - val digestv : buffer list -> t - val hmac : key:buffer -> buffer -> t - val hmacv : key:buffer -> buffer list -> t - - val compare : t -> t -> int - val eq : t -> t -> bool - val neq : t -> t -> bool - - val pp : Format.formatter -> t -> unit - val of_hex : buffer -> t - val to_hex : t -> buffer - end - - module Bytes : - sig - type buffer = By.t - type ctx - type t = By.t - - val init : unit -> ctx - val feed : ctx -> buffer -> unit - val feed_bytes : ctx -> Bytes.t -> unit - val feed_bigstring : ctx -> Bi.t -> unit - val get : ctx -> t - - val digest : buffer -> t - val digestv : buffer list -> t - val hmac : key:buffer -> buffer -> t - val hmacv : key:buffer -> buffer list -> t - - val compare : t -> t -> int - val eq : t -> t -> bool - val neq : t -> t -> bool - - val pp : Format.formatter -> t -> unit - val of_hex : buffer -> t - val to_hex : t -> buffer - end -end +module type S = Digestif_sig.S +module type T = Digestif_sig.T module type Desc = sig @@ -321,6 +267,9 @@ let module_of = function module Bytes = struct + type t = Bytes.t + type buffer = Bytes.t + let digest hash = let module H = (val (module_of hash)) in H.Bytes.digest @@ -352,6 +301,9 @@ end module Bigstring = struct + type t = Bi.t + type buffer = Bi.t + let digest hash = let module H = (val (module_of hash)) in H.Bigstring.digest diff --git a/src-ocaml/digestif.mli b/src-ocaml/digestif.mli index 8e31fc6..318430d 100644 --- a/src-ocaml/digestif.mli +++ b/src-ocaml/digestif.mli @@ -1,4 +1,5 @@ module type S = Digestif_sig.S +module type T = Digestif_sig.T type hash = Digestif_sig.hash @@ -13,3 +14,10 @@ module SHA512 : S module BLAKE2B : S module BLAKE2S : S module RMD160 : S + +module Bytes : T + with type t = Bytes.t + and type buffer = Bytes.t +module Bigstring : T + with type t = Digestif_sig.Bigstring.t + and type buffer = Digestif_sig.Bigstring.t diff --git a/src/digestif_sig.mli b/src/digestif_sig.mli index 35801d1..91041a5 100644 --- a/src/digestif_sig.mli +++ b/src/digestif_sig.mli @@ -1,4 +1,5 @@ module Bigstring = Digestif_bigstring +module Bytes = Digestif_bytes module type S = sig @@ -65,3 +66,16 @@ type hash = | `BLAKE2B | `BLAKE2S | `RMD160 ] + +module type T = sig + type t + type buffer + + val pp : hash -> Format.formatter -> t -> unit + val digest : hash -> buffer -> t + val digestv : hash -> buffer list -> t + val mac : hash -> key:buffer -> buffer -> t + val macv : hash -> key:buffer -> buffer list -> t + val of_hex : hash -> buffer -> t + val to_hex : hash -> t -> buffer +end From fa20c2344cefb327e0186dec1b858a68d1deb620 Mon Sep 17 00:00:00 2001 From: Romain Calascibetta Date: Sun, 3 Dec 2017 01:05:06 -0800 Subject: [PATCH 3/6] Fixed the test with the new signature --- .travis.yml | 8 ++++---- src/digestif_bigstring.mli | 1 + test/_tags | 3 +++ test/digestif-test-c.opam | 25 +++++++++++++++++++++++++ test/digestif-test-ocaml.opam | 25 +++++++++++++++++++++++++ test/test.ml | 12 ++++++------ 6 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 test/_tags create mode 100644 test/digestif-test-c.opam create mode 100644 test/digestif-test-ocaml.opam diff --git a/.travis.yml b/.travis.yml index da079b5..e13bb85 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,8 @@ env: matrix: - PACKAGE="digestif" OCAML_VERSION=4.03 TESTS=false - - PACKAGE="digestif-test-c" OCAML_VERSION=4.03 INSTALL=false TESTS=true - PINS=$(if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then echo -n "digestif:https://github.com/$TRAVIS_PULL_REQUEST_SLUG.git"; if [ ! -z $TRAVIS_PULL_REQUEST_BRANCH ]; then echo -n "#$TRAVIS_PULL_REQUEST_BRANCH"; fi else echo -n "digestif:https://github.com/$TRAVIS_REPO_SLUG.git"; if [ ! -z $TRAVIS_BRANCH ]; then echo -n "#$TRAVIS_BRANCH"; fi fi) + - PACKAGE="digestif" OCAML_VERSION=4.03 INSTALL=false TESTS=true + PINS="digestif-test-c:test" - - PACKAGE="digestif-test-ocaml" OCAML_VERSION=4.03 INSTALL=false TESTS=true - PINS=$(if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then echo -n "digestif:https://github.com/$TRAVIS_PULL_REQUEST_SLUG.git"; if [ ! -z $TRAVIS_PULL_REQUEST_BRANCH ]; then echo -n "#$TRAVIS_PULL_REQUEST_BRANCH"; fi else echo -n "digestif:https://github.com/$TRAVIS_REPO_SLUG.git"; if [ ! -z $TRAVIS_BRANCH ]; then echo -n "#$TRAVIS_BRANCH"; fi fi) + - PACKAGE="test/digestif-test-ocaml" OCAML_VERSION=4.03 INSTALL=false TESTS=true + PINS="digestif-test-ocaml:test" diff --git a/src/digestif_bigstring.mli b/src/digestif_bigstring.mli index 7be3cd8..8ba4355 100644 --- a/src/digestif_bigstring.mli +++ b/src/digestif_bigstring.mli @@ -23,6 +23,7 @@ val blit: t -> int -> t -> int -> int -> unit val blit_from_bytes: bytes -> int -> t -> int -> int -> unit val blit_from_bigstring: t -> int -> t -> int -> int -> unit val rpad: t -> int -> char -> t +val pp: Format.formatter -> t -> unit val compare: t -> t -> int val eq: t -> t -> bool val neq: t -> t -> bool diff --git a/test/_tags b/test/_tags new file mode 100644 index 0000000..dc4d280 --- /dev/null +++ b/test/_tags @@ -0,0 +1,3 @@ +true: safe_string, bin_annot + +<*.{ml,mli,byte,native}>: package(fmt), package(alcotest), package(cstruct) diff --git a/test/digestif-test-c.opam b/test/digestif-test-c.opam new file mode 100644 index 0000000..e53a63a --- /dev/null +++ b/test/digestif-test-c.opam @@ -0,0 +1,25 @@ +opam-version: "1.2" +name: "digestif" +maintainer: [ "Eyyüb Sari " + "Romain Calascibetta " ] +authors: [ "Eyyüb Sari " + "Romain Calascibetta " ] +homepage: "https://github.com/mirage/digestif" +bug-reports: "https://github.com/mirage/digestif/issues" +dev-repo: "https://github.com/mirage/digestif.git" +doc: "https://mirage.github.io/digestif/" +license: "MIT" + +build: [ "ocamlbuild" "-use-ocamlfind" "-package" "digestif.c" "test.native" ] +build-test: [ "./test.native" ] + +depends: [ + "ocamlbuild" {build} + "ocamlfind" {build} + "cstruct" + "base-bytes" + "digestif" + "alcotest" +] + +available: [ ocaml-version >= "4.03.0" ] diff --git a/test/digestif-test-ocaml.opam b/test/digestif-test-ocaml.opam new file mode 100644 index 0000000..bca23f3 --- /dev/null +++ b/test/digestif-test-ocaml.opam @@ -0,0 +1,25 @@ +opam-version: "1.2" +name: "digestif" +maintainer: [ "Eyyüb Sari " + "Romain Calascibetta " ] +authors: [ "Eyyüb Sari " + "Romain Calascibetta " ] +homepage: "https://github.com/mirage/digestif" +bug-reports: "https://github.com/mirage/digestif/issues" +dev-repo: "https://github.com/mirage/digestif.git" +doc: "https://mirage.github.io/digestif/" +license: "MIT" + +build: [ "ocamlbuild" "-use-ocamlfind" "-package" "digestif.ocaml" "test.native" ] +build-test: [ "./test.native" ] + +depends: [ + "ocamlbuild" {build} + "ocamlfind" {build} + "cstruct" + "base-bytes" + "digestif" + "alcotest" +] + +available: [ ocaml-version >= "4.03.0" ] diff --git a/test/test.ml b/test/test.ml index c377a39..167c5ec 100644 --- a/test/test.ml +++ b/test/test.ml @@ -53,7 +53,7 @@ module Bi (Hash : HASH) = struct type t = bigstring - let eq = Digestif.Bi.eq + let eq = Digestif_bigstring.eq let pp = Digestif.Bigstring.pp Hash.v end @@ -104,9 +104,9 @@ let makes ~name buffer hash keys inputs expects = let to_bigstring s = let ln = Bytes.length s in - let bi = Digestif.Bi.create ln in + let bi = Digestif_bigstring.create ln in - Digestif.Bi.blit_from_bytes s 0 bi 0 ln; + Digestif_bigstring.blit_from_bytes s 0 bi 0 ln; bi let keys_by, keys_bi = @@ -226,8 +226,8 @@ let results_blake2s_by, results_blake2s_bi = module BLAKE2 = struct - let input_blake2b_file = "./test/blake2b.test" - let input_blake2s_file = "./test/blake2s.test" + let input_blake2b_file = "./blake2b.test" + let input_blake2s_file = "./blake2s.test" let fold_s f a s = let r = ref a in @@ -361,7 +361,7 @@ struct List.map (fun (input, expect) -> make_digest ~name:"rmd160" Bytes `RMD160 input expect) (List.combine (List.map Bytes.unsafe_of_string inputs) (List.map (BLAKE2.of_hex Digestif.RMD160.digest_size) expects)) @ [ million "(bytes)" (module Digestif.RMD160.Bytes : D with type t = Bytes.t) (module By) ~expect:expect_million_y - ; million "(bigstring)" (module Digestif.RMD160.Bigstring : D with type t = bigstring) (module Digestif.Bi) ~expect:expect_million_i ] + ; million "(bigstring)" (module Digestif.RMD160.Bigstring : D with type t = bigstring) (module Digestif_bigstring) ~expect:expect_million_i ] end let tests () = From aa0dfe4bcafacb02506460c6c3c8652ce65bda72 Mon Sep 17 00:00:00 2001 From: Romain Calascibetta Date: Sun, 3 Dec 2017 01:53:03 -0800 Subject: [PATCH 4/6] Move test on the build rules (trick) --- .travis.yml | 4 ++-- test/digestif-test-c.opam | 5 +++-- test/digestif-test-ocaml.opam | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index e13bb85..15263ee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,8 @@ env: matrix: - PACKAGE="digestif" OCAML_VERSION=4.03 TESTS=false - - PACKAGE="digestif" OCAML_VERSION=4.03 INSTALL=false TESTS=true + - PACKAGE="digestif" OCAML_VERSION=4.03 INSTALL=false PINS="digestif-test-c:test" - - PACKAGE="test/digestif-test-ocaml" OCAML_VERSION=4.03 INSTALL=false TESTS=true + - PACKAGE="test/digestif-test-ocaml" OCAML_VERSION=4.03 INSTALL=false PINS="digestif-test-ocaml:test" diff --git a/test/digestif-test-c.opam b/test/digestif-test-c.opam index e53a63a..0854c74 100644 --- a/test/digestif-test-c.opam +++ b/test/digestif-test-c.opam @@ -10,8 +10,9 @@ dev-repo: "https://github.com/mirage/digestif.git" doc: "https://mirage.github.io/digestif/" license: "MIT" -build: [ "ocamlbuild" "-use-ocamlfind" "-package" "digestif.c" "test.native" ] -build-test: [ "./test.native" ] +build: + [ [ "ocamlbuild" "-use-ocamlfind" "-package" "digestif.c" "test.native" ] + [ "./test.native" ] ] depends: [ "ocamlbuild" {build} diff --git a/test/digestif-test-ocaml.opam b/test/digestif-test-ocaml.opam index bca23f3..101f108 100644 --- a/test/digestif-test-ocaml.opam +++ b/test/digestif-test-ocaml.opam @@ -10,8 +10,9 @@ dev-repo: "https://github.com/mirage/digestif.git" doc: "https://mirage.github.io/digestif/" license: "MIT" -build: [ "ocamlbuild" "-use-ocamlfind" "-package" "digestif.ocaml" "test.native" ] -build-test: [ "./test.native" ] +build: + [ [ "ocamlbuild" "-use-ocamlfind" "-package" "digestif.ocaml" "test.native" ] + [ "./test.native" ] ] depends: [ "ocamlbuild" {build} From 62039e28893e95010656c62b3efc22c382212b79 Mon Sep 17 00:00:00 2001 From: Romain Calascibetta Date: Sun, 3 Dec 2017 02:09:15 -0800 Subject: [PATCH 5/6] Add EXTRA_DEPS in travis ci for force to install tests --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 15263ee..df9d9e0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,8 @@ env: - PACKAGE="digestif" OCAML_VERSION=4.03 INSTALL=false PINS="digestif-test-c:test" + EXTRA_DEPS="digestif-test-c" - - PACKAGE="test/digestif-test-ocaml" OCAML_VERSION=4.03 INSTALL=false + - PACKAGE="digestif" OCAML_VERSION=4.03 INSTALL=false PINS="digestif-test-ocaml:test" + EXTRA_DEPS="digestif-test-ocaml" From 439e231baf084137f4cff1671c690db293b3dc41 Mon Sep 17 00:00:00 2001 From: Romain Calascibetta Date: Sun, 3 Dec 2017 02:50:24 -0800 Subject: [PATCH 6/6] Remove useless opam files --- digestif-test-c.opam | 25 ------------------------- digestif-test-ocaml.opam | 25 ------------------------- 2 files changed, 50 deletions(-) delete mode 100644 digestif-test-c.opam delete mode 100644 digestif-test-ocaml.opam diff --git a/digestif-test-c.opam b/digestif-test-c.opam deleted file mode 100644 index cb4fc33..0000000 --- a/digestif-test-c.opam +++ /dev/null @@ -1,25 +0,0 @@ -opam-version: "1.2" -name: "digestif" -maintainer: [ "Eyyüb Sari " - "Romain Calascibetta " ] -authors: [ "Eyyüb Sari " - "Romain Calascibetta " ] -homepage: "https://github.com/mirage/digestif" -bug-reports: "https://github.com/mirage/digestif/issues" -dev-repo: "https://github.com/mirage/digestif.git" -doc: "https://mirage.github.io/digestif/" -license: "MIT" - -build: [ "ocamlbuild" "-use-ocamlfind" "-package" "digestif.c" "test/test.native" ] -build-test: [ "./test.native" ] - -depends: [ - "ocamlbuild" {build} - "ocamlfind" {build} - "cstruct" - "base-bytes" - "digestif" - "alcotest" -] - -available: [ ocaml-version >= "4.03.0" ] diff --git a/digestif-test-ocaml.opam b/digestif-test-ocaml.opam deleted file mode 100644 index 2e48585..0000000 --- a/digestif-test-ocaml.opam +++ /dev/null @@ -1,25 +0,0 @@ -opam-version: "1.2" -name: "digestif" -maintainer: [ "Eyyüb Sari " - "Romain Calascibetta " ] -authors: [ "Eyyüb Sari " - "Romain Calascibetta " ] -homepage: "https://github.com/mirage/digestif" -bug-reports: "https://github.com/mirage/digestif/issues" -dev-repo: "https://github.com/mirage/digestif.git" -doc: "https://mirage.github.io/digestif/" -license: "MIT" - -build: [ "ocamlbuild" "-use-ocamlfind" "-package" "digestif.ocaml" "test/test.native" ] -build-test: [ "./test.native" ] - -depends: [ - "ocamlbuild" {build} - "ocamlfind" {build} - "cstruct" - "base-bytes" - "digestif" - "alcotest" -] - -available: [ ocaml-version >= "4.03.0" ]