From 230d495dc6511c2ca1f3a97ad759c45ee75a8799 Mon Sep 17 00:00:00 2001 From: Vincent Bernardoff Date: Tue, 17 Apr 2018 10:37:49 +0200 Subject: [PATCH] Vendors/blake2b: use bigstring --- src/lib_crypto/blake2B.ml | 17 +++++------ src/lib_crypto/s.ml | 2 +- src/lib_protocol_compiler/compiler.ml | 19 ------------ vendors/ocaml-blake2/blake2.opam | 3 +- vendors/ocaml-blake2/src/blake2.ml | 40 ++++++++++++------------- vendors/ocaml-blake2/src/blake2.mli | 8 ++--- vendors/ocaml-blake2/src/jbuild | 5 ++-- vendors/ocaml-blake2/test/jbuild | 2 +- vendors/ocaml-blake2/test/test.ml | 42 ++++++++++++--------------- 9 files changed, 56 insertions(+), 82 deletions(-) diff --git a/src/lib_crypto/blake2B.ml b/src/lib_crypto/blake2B.ml index 2d763d33c..5c9aa05b5 100644 --- a/src/lib_crypto/blake2B.ml +++ b/src/lib_crypto/blake2B.ml @@ -38,7 +38,7 @@ module Make_minimal (K : Name) = struct if String.length s <> size then None else - Some (Blake2b.Hash (Cstruct.of_string s)) + Some (Blake2b.Hash (MBytes.of_string s)) let of_string s = match of_string_opt s with | None -> @@ -52,7 +52,7 @@ module Make_minimal (K : Name) = struct "%s.of_string: wrong string size (%d)" K.name (String.length s) | Some h -> h - let to_string (Blake2b.Hash h) = Cstruct.to_string h + let to_string (Blake2b.Hash h) = MBytes.to_string h let of_hex s = of_string (Hex.to_string s) let of_hex_opt s = of_string_opt (Hex.to_string s) @@ -70,7 +70,7 @@ module Make_minimal (K : Name) = struct if MBytes.length b <> size then None else - Some (Blake2b.Hash (Cstruct.of_bigarray b)) + Some (Blake2b.Hash b) let of_bytes_exn b = match of_bytes_opt b with | None -> @@ -84,21 +84,20 @@ module Make_minimal (K : Name) = struct | Some x -> Ok x | None -> generic_error "Failed to deserialize a hash (%s)" K.name - let to_bytes (Blake2b.Hash h) = Cstruct.to_bigarray h + let to_bytes (Blake2b.Hash h) = h (* let read src off = of_bytes_exn @@ MBytes.sub src off size *) (* let write dst off h = MBytes.blit (to_bytes h) 0 dst off size *) let hash_bytes ?key l = - let key = Option.map ~f:Cstruct.of_bigarray key in let state = Blake2b.init ?key size in - List.iter (fun b -> Blake2b.update state (Cstruct.of_bigarray b)) l ; + List.iter (fun b -> Blake2b.update state b) l ; Blake2b.final state let hash_string ?key l = - let key = Option.map ~f:Cstruct.of_string key in + let key = Option.map ~f:Bigstring.of_string key in let state = Blake2b.init ?key size in - List.iter (fun s -> Blake2b.update state (Cstruct.of_string s)) l ; + List.iter (fun s -> Blake2b.update state (MBytes.of_string s)) l ; Blake2b.final state let path_length = 6 @@ -129,7 +128,7 @@ module Make_minimal (K : Name) = struct include Compare.Make(struct type nonrec t = t - let compare (Blake2b.Hash h1) (Blake2b.Hash h2) = Cstruct.compare h1 h2 + let compare (Blake2b.Hash h1) (Blake2b.Hash h2) = MBytes.compare h1 h2 end) end diff --git a/src/lib_crypto/s.ml b/src/lib_crypto/s.ml index 62b2cca47..20982a5e2 100644 --- a/src/lib_crypto/s.ml +++ b/src/lib_crypto/s.ml @@ -29,7 +29,7 @@ module type MINIMAL_HASH = sig include Compare.S with type t := t - val hash_bytes: ?key:Cstruct.buffer -> Cstruct.buffer list -> t + val hash_bytes: ?key:MBytes.t -> MBytes.t list -> t val hash_string: ?key:string -> string list -> t val zero: t diff --git a/src/lib_protocol_compiler/compiler.ml b/src/lib_protocol_compiler/compiler.ml index 15736b0f8..b16e99e22 100644 --- a/src/lib_protocol_compiler/compiler.ml +++ b/src/lib_protocol_compiler/compiler.ml @@ -116,25 +116,6 @@ let debug fmt = if !debug_flag then Format.eprintf fmt else Format.ifprintf Format.err_formatter fmt -let hash_file file = - let open Blake2 in - let buflen = 8092 in - let buf = BytesLabels.create buflen in - let fd = Unix.openfile file [Unix.O_RDONLY] 0o600 in - let state = Blake2b.init 32 in - let loop () = - match Unix.read fd buf 0 buflen with - | 0 -> () - | nb_read -> - Blake2b.update state - (Cstruct.of_bytes - (if nb_read = buflen then buf else BytesLabels.sub buf ~pos:0 ~len:nb_read)) - in - loop () ; - Unix.close fd ; - let Blake2b.Hash h = Blake2b.final state in - Cstruct.to_string h - let mktemp_dir () = Filename.get_temp_dir_name () // Printf.sprintf "tezos-protocol-build-%06X" (Random.int 0xFFFFFF) diff --git a/vendors/ocaml-blake2/blake2.opam b/vendors/ocaml-blake2/blake2.opam index aba17f1f1..01a0b5c1b 100644 --- a/vendors/ocaml-blake2/blake2.opam +++ b/vendors/ocaml-blake2/blake2.opam @@ -16,6 +16,7 @@ build: [ "jbuilder" "build" "-j" jobs "-p" name "@install" ] build-test: [ "jbuilder" "runtest" "-p" name "-j" jobs ] depends: [ "jbuilder" {build & >= "1.0+beta16"} - "cstruct" {>= "3.2.1"} + "bigstring" {>= "0.1.1"} "alcotest" { test } + "hex" {test & >= "1.2.0"} ] diff --git a/vendors/ocaml-blake2/src/blake2.ml b/vendors/ocaml-blake2/src/blake2.ml index d3f9bcbe1..56dba1f21 100644 --- a/vendors/ocaml-blake2/src/blake2.ml +++ b/vendors/ocaml-blake2/src/blake2.ml @@ -4,32 +4,32 @@ ---------------------------------------------------------------------------*) module Blake2b = struct - type t = Cstruct.t + type t = Bigstring.t external sizeof_state : unit -> int = "sizeof_blake2b_state" [@@noalloc] let bytes = sizeof_state () - external init : Cstruct.buffer -> int -> int = + external init : Bigstring.t -> int -> int = "ml_blake2b_init" [@@noalloc] - external outlen : Cstruct.buffer -> int = + external outlen : Bigstring.t -> int = "blake2b_state_outlen" [@@noalloc] - let outlen t = outlen t.Cstruct.buffer + let outlen t = outlen t - external init_key : Cstruct.buffer -> int -> Cstruct.buffer -> int = + external init_key : Bigstring.t -> int -> Bigstring.t -> int = "ml_blake2b_init_key" [@@noalloc] - external update : Cstruct.buffer -> Cstruct.buffer -> int = + external update : Bigstring.t -> Bigstring.t -> int = "ml_blake2b_update" [@@noalloc] - external final : Cstruct.buffer -> Cstruct.buffer -> int = + external final : Bigstring.t -> Bigstring.t -> int = "ml_blake2b_final" [@@noalloc] external direct : - Cstruct.buffer -> Cstruct.buffer -> Cstruct.buffer -> int = + Bigstring.t -> Bigstring.t -> Bigstring.t -> int = "ml_blake2b" [@@noalloc] let or_fail ~msg f = @@ -40,37 +40,33 @@ module Blake2b = struct let init ?key size = if size < 1 || size > 64 then invalid_arg "Blake2b.init: size must be between 1 and 64" ; - let t = Cstruct.create_unsafe bytes in + let t = Bigstring.create bytes in begin match key with | Some key -> or_fail ~msg:"Blake2b.init" - (fun () -> init_key t.buffer size key.Cstruct.buffer) + (fun () -> init_key t size key) | None -> or_fail ~msg:"Blake2b.init" - (fun () -> init t.buffer size) + (fun () -> init t size) end ; t let update t buf = - or_fail ~msg:"Blake2b.update" - (fun () -> update t.Cstruct.buffer buf.Cstruct.buffer) + or_fail ~msg:"Blake2b.update" (fun () -> update t buf) - type hash = Hash of Cstruct.t + type hash = Hash of Bigstring.t let final t = let len = outlen t in - let buf = Cstruct.create_unsafe len in - or_fail ~msg:"Blake2b.final" - (fun () -> final t.Cstruct.buffer buf.Cstruct.buffer) ; + let buf = Bigstring.create len in + or_fail ~msg:"Blake2b.final" (fun () -> final t buf) ; Hash buf - let direct ?(key=Cstruct.create_unsafe 0) inbuf len = + let direct ?(key=Bigstring.create 0) inbuf len = if len < 1 || len > 64 then invalid_arg "Blake2b.direct: size must be between 1 and 64" ; - let outbuf = Cstruct.create len in - or_fail ~msg:"Blake2b.direct" - (fun () -> direct outbuf.Cstruct.buffer - inbuf.Cstruct.buffer key.buffer) ; + let outbuf = Bigstring.create len in + or_fail ~msg:"Blake2b.direct" (fun () -> direct outbuf inbuf key) ; Hash outbuf end diff --git a/vendors/ocaml-blake2/src/blake2.mli b/vendors/ocaml-blake2/src/blake2.mli index 9cda35b60..7c47e8b19 100644 --- a/vendors/ocaml-blake2/src/blake2.mli +++ b/vendors/ocaml-blake2/src/blake2.mli @@ -5,20 +5,20 @@ module Blake2b : sig type t - type hash = Hash of Cstruct.t + type hash = Hash of Bigstring.t - val init : ?key:Cstruct.t -> int -> t + val init : ?key:Bigstring.t -> int -> t (** [init ?key size] is a blake2b context for hashes of size [size], using [key] if present. *) - val update : t -> Cstruct.t -> unit + val update : t -> Bigstring.t -> unit (** [update t buf] updates [t] with the data in [buf]. *) val final : t -> hash (** [final t] is the blake2b hash of all data updated in [t] so far. *) - val direct : ?key:Cstruct.t -> Cstruct.t -> int -> hash + val direct : ?key:Bigstring.t -> Bigstring.t -> int -> hash (** [direct ?key inbuf len] is the blake2b hash of length [len], using [key] is present. *) end diff --git a/vendors/ocaml-blake2/src/jbuild b/vendors/ocaml-blake2/src/jbuild index 6ce592cfd..8d1a727f6 100644 --- a/vendors/ocaml-blake2/src/jbuild +++ b/vendors/ocaml-blake2/src/jbuild @@ -3,5 +3,6 @@ (library ((name blake2) (public_name blake2) - (libraries (cstruct)) - (c_names (blake2b-ref)))) + (libraries (bigstring)) + (c_names (blake2b-ref)) + (c_flags (-O3)))) diff --git a/vendors/ocaml-blake2/test/jbuild b/vendors/ocaml-blake2/test/jbuild index 42fe2a4a7..68230860f 100644 --- a/vendors/ocaml-blake2/test/jbuild +++ b/vendors/ocaml-blake2/test/jbuild @@ -2,7 +2,7 @@ (executable ((name test) - (libraries (blake2 alcotest)))) + (libraries (blake2 hex alcotest)))) (alias ((name runtest-blake2) diff --git a/vendors/ocaml-blake2/test/test.ml b/vendors/ocaml-blake2/test/test.ml index 2670bbe1a..6c715520e 100644 --- a/vendors/ocaml-blake2/test/test.ml +++ b/vendors/ocaml-blake2/test/test.ml @@ -2,44 +2,40 @@ open Blake2 type vector = { - data_in : string list ; - data_key : string option ; - data_out : string ; + data_in : Bigstring.t list ; + data_key : Bigstring.t option ; + data_out : Bigstring.t ; } +let hex s = + Cstruct.to_bigarray (Hex.to_cstruct (`Hex s)) + let vectors = [ - { data_in = [ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f" ] ; + { data_in = [ hex "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f" ] ; data_key = None ; - data_out = "1c077e279de6548523502b6df800ffdab5e2c3e9442eb838f58c295f3b147cef9d701c41c321283f00c71affa0619310399126295b78dd4d1a74572ef9ed5135" ; + data_out = hex "1c077e279de6548523502b6df800ffdab5e2c3e9442eb838f58c295f3b147cef9d701c41c321283f00c71affa0619310399126295b78dd4d1a74572ef9ed5135" ; } ; - { data_in = [ "000102030405060708090a0b0c0d0e0f101112131415" ; "161718"; "191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f" ] ; + { data_in = [ hex "000102030405060708090a0b0c0d0e0f101112131415" ; hex "161718"; hex "191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f" ] ; data_key = None ; - data_out = "1c077e279de6548523502b6df800ffdab5e2c3e9442eb838f58c295f3b147cef9d701c41c321283f00c71affa0619310399126295b78dd4d1a74572ef9ed5135" ; + data_out = hex "1c077e279de6548523502b6df800ffdab5e2c3e9442eb838f58c295f3b147cef9d701c41c321283f00c71affa0619310399126295b78dd4d1a74572ef9ed5135" ; } ; - { data_in = [ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3" ] ; - data_key = Some ("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f") ; - data_out = "b39614268fdd8781515e2cfebf89b4d5402bab10c226e6344e6b9ae000fb0d6c79cb2f3ec80e80eaeb1980d2f8698916bd2e9f747236655116649cd3ca23a837" ; + { data_in = [ hex "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3" ] ; + data_key = Some (hex "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f") ; + data_out = hex "b39614268fdd8781515e2cfebf89b4d5402bab10c226e6344e6b9ae000fb0d6c79cb2f3ec80e80eaeb1980d2f8698916bd2e9f747236655116649cd3ca23a837" ; } ; ] let test_update { data_in ; data_key ; data_out } = - let key = - match data_key with None -> None | Some s -> Some (Cstruct.of_hex s) in - let data_out = Cstruct.of_hex data_out in - let d = Blake2b.init ?key (Cstruct.len data_out) in - List.iter (fun s -> Blake2b.update d (Cstruct.of_hex s)) data_in ; + let d = Blake2b.init ?key:data_key (Bigstring.length data_out) in + List.iter (Blake2b.update d) data_in ; let Blake2b.Hash h = Blake2b.final d in - assert Cstruct.(equal data_out h) + assert Bigstring.(equal data_out h) let test_direct { data_in ; data_key ; data_out } = - let key = - match data_key with None -> None | Some s -> Some (Cstruct.of_hex s) in - let data_out = Cstruct.of_hex data_out in let Blake2b.Hash h = - Blake2b.direct ?key - (Cstruct.of_hex (String.concat "" data_in)) - (Cstruct.len data_out) in - assert Cstruct.(equal data_out h) + Blake2b.direct ?key:data_key + (Bigstring.concat "" data_in) (Bigstring.length data_out) in + assert Bigstring.(equal data_out h) let update_tests = List.mapi