From 85e70db051dd3c51fac518692616ff9a72521db6 Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Tue, 2 Jul 2019 09:33:49 +0200 Subject: [PATCH 01/14] stdlib: MBytes.t = bytes --- src/lib_stdlib/dune | 2 +- src/lib_stdlib/mBytes.ml | 25 ++++++++++++------------- src/lib_stdlib/mBytes.mli | 10 +++++++--- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/lib_stdlib/dune b/src/lib_stdlib/dune index 29ac588f8fd9..f3efed7648d0 100644 --- a/src/lib_stdlib/dune +++ b/src/lib_stdlib/dune @@ -1,7 +1,7 @@ (library (name tezos_stdlib) (public_name tezos-stdlib) - (libraries ocplib-endian.bigstring + (libraries ocplib-endian bigstring cstruct hex diff --git a/src/lib_stdlib/mBytes.ml b/src/lib_stdlib/mBytes.ml index 70a8a382a0d0..5f648024ad91 100644 --- a/src/lib_stdlib/mBytes.ml +++ b/src/lib_stdlib/mBytes.ml @@ -23,21 +23,16 @@ (* *) (*****************************************************************************) -include Bigstring +include Bytes -include EndianBigstring.BigEndian -module LE = EndianBigstring.LittleEndian +include EndianBytes.BigEndian +module LE = EndianBytes.LittleEndian -let make sz c = - let buf = create sz in - fill buf c ; - buf +let make = Bytes.make -let to_hex t = - Hex.of_cstruct (Cstruct.of_bigarray t) +let to_hex t = Hex.of_bytes t -let of_hex hex = - Cstruct.to_bigarray (Hex.to_cstruct hex) +let of_hex hex = Hex.to_bytes hex let pp_hex ppf s = let `Hex hex = to_hex s in @@ -48,7 +43,7 @@ let cut ?(copy=false) sz bytes = if length <= sz then [bytes] (* if the result fits in the given sz *) else - let may_copy = if copy then Bigstring.copy else fun t -> t in + let may_copy = if copy then Bytes.copy else fun t -> t in let nb_full = length / sz in (* nb of blocks of size sz *) let sz_full = nb_full * sz in (* size of the full part *) let acc = (* eventually init acc with a non-full block *) @@ -66,5 +61,9 @@ let cut ?(copy=false) sz bytes = include Compare.Make(struct type nonrec t = t - let compare = Bigstring.compare + let compare = Bytes.compare end) + +let blit_of_string = Bytes.blit_string + +let concat sep l = Bytes.concat (Bytes.unsafe_of_string sep) l diff --git a/src/lib_stdlib/mBytes.mli b/src/lib_stdlib/mBytes.mli index a96c7af134f2..5518b783889d 100644 --- a/src/lib_stdlib/mBytes.mli +++ b/src/lib_stdlib/mBytes.mli @@ -28,11 +28,11 @@ Default layout for numeric operations is big-endian. Little-endian operations in the LE submodule. **) -include module type of Bigstring +include module type of Bytes include Compare.S with type t := t -include EndianBigstring.EndianBigstringSig -module LE : EndianBigstring.EndianBigstringSig +include EndianBytes.EndianBytesSig +module LE : EndianBytes.EndianBytesSig val make : int -> char -> t val of_hex : Hex.t -> t @@ -47,3 +47,7 @@ val pp_hex : Format.formatter -> t -> unit unreachable (because of the 'optimized' implementation of [sub] used internally. *) val cut: ?copy:bool -> int -> t -> t list + +val blit_of_string: string -> int -> t -> int -> int -> unit + +val concat: string -> t list -> t -- GitLab From 8a3bc1feb43d4040bcb6360e45aa56644748ee4a Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Thu, 18 Jul 2019 09:17:19 +0200 Subject: [PATCH 02/14] ++stdlib --- src/lib_stdlib/mBytes.ml | 2 ++ src/lib_stdlib/mBytes.mli | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib_stdlib/mBytes.ml b/src/lib_stdlib/mBytes.ml index 5f648024ad91..2b95bcf474e1 100644 --- a/src/lib_stdlib/mBytes.ml +++ b/src/lib_stdlib/mBytes.ml @@ -67,3 +67,5 @@ include Compare.Make(struct let blit_of_string = Bytes.blit_string let concat sep l = Bytes.concat (Bytes.unsafe_of_string sep) l + +let blit_to_bytes = Bytes.blit diff --git a/src/lib_stdlib/mBytes.mli b/src/lib_stdlib/mBytes.mli index 5518b783889d..3b32294705cd 100644 --- a/src/lib_stdlib/mBytes.mli +++ b/src/lib_stdlib/mBytes.mli @@ -49,5 +49,5 @@ val pp_hex : Format.formatter -> t -> unit val cut: ?copy:bool -> int -> t -> t list val blit_of_string: string -> int -> t -> int -> int -> unit - +val blit_to_bytes: t -> int -> t -> int -> int -> unit val concat: string -> t list -> t -- GitLab From 4617447704dd04cce9b684b5fa0aa073ed3fc88b Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Tue, 2 Jul 2019 09:34:07 +0200 Subject: [PATCH 03/14] crypto: MBytes.t = bytes --- src/lib_crypto/blake2B.ml | 15 ++-- src/lib_crypto/chain_id.ml | 4 +- src/lib_crypto/crypto_box.ml | 83 ++++++++++--------- src/lib_crypto/crypto_box.mli | 22 ++--- src/lib_crypto/ed25519.ml | 76 +++++++++-------- src/lib_crypto/p256.ml | 38 +++++---- src/lib_crypto/rand.ml | 8 +- src/lib_crypto/rand.mli | 6 +- src/lib_crypto/s.ml | 4 +- src/lib_crypto/secp256k1.ml | 34 ++++---- src/lib_crypto/signature.mli | 2 +- .../test/test_deterministic_nonce.ml | 2 +- 12 files changed, 152 insertions(+), 142 deletions(-) diff --git a/src/lib_crypto/blake2B.ml b/src/lib_crypto/blake2B.ml index 4cf9697cca31..9abeff49b93e 100644 --- a/src/lib_crypto/blake2B.ml +++ b/src/lib_crypto/blake2B.ml @@ -54,7 +54,7 @@ module Make_minimal (K : Name) = struct if String.length s <> size then None else - Some (Blake2b.Hash (MBytes.of_string s)) + Some (Blake2b.Hash (Bigstring.of_string s)) let of_string s = match of_string_opt s with | None -> @@ -68,7 +68,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) = MBytes.to_string h + let to_string (Blake2b.Hash h) = Bigstring.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) @@ -86,7 +86,7 @@ module Make_minimal (K : Name) = struct if MBytes.length b <> size then None else - Some (Blake2b.Hash b) + Some (Blake2b.Hash (Bigstring.of_bytes b)) let of_bytes_exn b = match of_bytes_opt b with | None -> @@ -100,20 +100,21 @@ 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) = h + let to_bytes (Blake2b.Hash h) = Bigstring.to_bytes 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:Bigstring.of_bytes key in let state = Blake2b.init ?key size in - List.iter (fun b -> Blake2b.update state b) l ; + List.iter (fun b -> Blake2b.update state (Bigstring.of_bytes b)) l ; Blake2b.final state let hash_string ?key l = let key = Option.map ~f:Bigstring.of_string key in let state = Blake2b.init ?key size in - List.iter (fun s -> Blake2b.update state (MBytes.of_string s)) l ; + List.iter (fun s -> Blake2b.update state (Bigstring.of_string s)) l ; Blake2b.final state let path_length = 6 @@ -148,7 +149,7 @@ module Make_minimal (K : Name) = struct include Compare.Make(struct type nonrec t = t - let compare (Blake2b.Hash h1) (Blake2b.Hash h2) = MBytes.compare h1 h2 + let compare (Blake2b.Hash h1) (Blake2b.Hash h2) = Bigstring.compare h1 h2 end) end diff --git a/src/lib_crypto/chain_id.ml b/src/lib_crypto/chain_id.ml index f5e1a5a0e630..d07852789e10 100644 --- a/src/lib_crypto/chain_id.ml +++ b/src/lib_crypto/chain_id.ml @@ -81,8 +81,8 @@ let of_bytes s = generic_error "Failed to deserialize a hash (%s)" name let to_bytes = MBytes.of_string -(* 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 read src off = of_bytes_exn @@ Bigstring.sub src off size *) +(* let write dst off h = Bigstring.blit (to_bytes h) 0 dst off size *) let path_length = 1 let to_path key l = diff --git a/src/lib_crypto/crypto_box.ml b/src/lib_crypto/crypto_box.ml index 79caac4218fa..979a0cfe72a6 100644 --- a/src/lib_crypto/crypto_box.ml +++ b/src/lib_crypto/crypto_box.ml @@ -43,20 +43,20 @@ module Secretbox = struct let box key msg nonce = let msglen = MBytes.length msg in - let cmsg = MBytes.create (msglen + zerobytes) in - MBytes.fill cmsg '\x00' ; - MBytes.blit msg 0 cmsg zerobytes msglen ; + let cmsg = Bigstring.create (msglen + zerobytes) in + Bigstring.fill cmsg '\x00' ; + Bigstring.blit_of_bytes msg 0 cmsg zerobytes msglen ; box ~key ~nonce ~msg:cmsg ~cmsg ; - MBytes.sub cmsg boxzerobytes (msglen + zerobytes - boxzerobytes) + Bigstring.sub cmsg boxzerobytes (msglen + zerobytes - boxzerobytes) let box_open key cmsg nonce = - let cmsglen = MBytes.length cmsg in - let msg = MBytes.create (cmsglen + boxzerobytes) in - MBytes.fill msg '\x00' ; - MBytes.blit cmsg 0 msg boxzerobytes cmsglen ; + let cmsglen = Bigstring.length cmsg in + let msg = Bigstring.create (cmsglen + boxzerobytes) in + Bigstring.fill msg '\x00' ; + Bigstring.blit cmsg 0 msg boxzerobytes cmsglen ; match box_open ~key ~nonce ~cmsg:msg ~msg with | false -> None - | true -> Some (MBytes.sub msg zerobytes (cmsglen - boxzerobytes)) + | true -> Some (Bigstring.sub_bytes msg zerobytes (cmsglen - boxzerobytes)) end module Public_key_hash = Blake2B.Make (Base58) (struct @@ -70,7 +70,7 @@ let () = Base58.check_encoded_prefix Public_key_hash.b58check_encoding "id" 30 let hash pk = - Public_key_hash.hash_bytes [Box.unsafe_to_bytes pk] + Public_key_hash.hash_bytes [Bigstring.to_bytes (Box.unsafe_to_bytes pk)] let zerobytes = Box.zerobytes let boxzerobytes = Box.boxzerobytes @@ -79,12 +79,12 @@ let random_keypair () = let pk, sk = Box.keypair () in sk, pk, hash pk -let zero_nonce = MBytes.make Nonce.bytes '\x00' +let zero_nonce = Bigstring.make Nonce.bytes '\x00' let random_nonce = Nonce.gen let increment_nonce = Nonce.increment let generate_nonce mbytes = let hash = Blake2B.hash_bytes mbytes in - Nonce.of_bytes_exn @@ (Bigstring.sub (Blake2B.to_bytes hash) 0 Nonce.bytes) + Nonce.of_bytes_exn @@ (Bigstring.of_bytes_slice (Blake2B.to_bytes hash) 0 Nonce.bytes) let init_to_resp_seed = MBytes.of_string "Init -> Resp" let resp_to_init_seed = MBytes.of_string "Resp -> Init" @@ -103,25 +103,27 @@ let generate_nonces ~incoming ~sent_msg ~recv_msg = let precompute sk pk = Box.dh pk sk let fast_box_noalloc k nonce msg = + let msg = Bigstring.of_bytes msg in Box.box ~k ~nonce ~msg ~cmsg:msg let fast_box_open_noalloc k nonce cmsg = + let cmsg = Bigstring.of_bytes cmsg in Box.box_open ~k ~nonce ~cmsg ~msg:cmsg let fast_box k msg nonce = - let msglen = MBytes.length msg in - let cmsg = MBytes.create (msglen + zerobytes) in - MBytes.fill cmsg '\x00' ; - MBytes.blit msg 0 cmsg zerobytes msglen ; + let msglen = Bigstring.length msg in + let cmsg = Bigstring.create (msglen + zerobytes) in + Bigstring.fill cmsg '\x00' ; + Bigstring.blit msg 0 cmsg zerobytes msglen ; Box.box ~k ~nonce ~msg:cmsg ~cmsg ; cmsg let fast_box_open k cmsg nonce = - let cmsglen = MBytes.length cmsg in - let msg = MBytes.create cmsglen in + let cmsglen = Bigstring.length cmsg in + let msg = Bigstring.create cmsglen in match Box.box_open ~k ~nonce ~cmsg ~msg with | false -> None - | true -> Some (MBytes.sub msg zerobytes (cmsglen - zerobytes)) + | true -> Some (Bigstring.sub msg zerobytes (cmsglen - zerobytes)) let compare_target hash target = let hash = Z.of_bits (Blake2B.to_string hash) in @@ -150,8 +152,8 @@ let default_target = make_target 24. let check_proof_of_work pk nonce target = let hash = Blake2B.hash_bytes [ - Box.unsafe_to_bytes pk ; - nonce ; + Bigstring.to_bytes (Box.unsafe_to_bytes pk) ; + Bigstring.to_bytes nonce ; ] in compare_target hash target @@ -168,25 +170,19 @@ let generate_proof_of_work ?max pk target = loop (Nonce.increment nonce) (cpt + 1) in loop (random_nonce ()) 0 -let public_key_to_bigarray pk = - let buf = MBytes.create Box.pkbytes in - Box.blit_to_bytes pk buf ; - buf +let public_key_to_bytes pk = + Bigstring.to_bytes (Box.unsafe_to_bytes pk) -let public_key_of_bigarray buf = - let pk = MBytes.copy buf in - Box.unsafe_pk_of_bytes pk +let public_key_of_bytes buf = + Box.unsafe_pk_of_bytes (Bigstring.of_bytes buf) let public_key_size = Box.pkbytes -let secret_key_to_bigarray sk = - let buf = MBytes.create Box.skbytes in - Box.blit_to_bytes sk buf ; - buf +let secret_key_to_bytes sk = + Bigstring.to_bytes (Box.unsafe_to_bytes sk) -let secret_key_of_bigarray buf = - let sk = MBytes.copy buf in - Box.unsafe_sk_of_bytes sk +let secret_key_of_bytes buf = + Box.unsafe_sk_of_bytes (Bigstring.of_bytes buf) let secret_key_size = Box.skbytes @@ -195,21 +191,26 @@ let nonce_size = Nonce.bytes let public_key_encoding = let open Data_encoding in conv - public_key_to_bigarray - public_key_of_bigarray + public_key_to_bytes + public_key_of_bytes (Fixed.bytes public_key_size) let secret_key_encoding = let open Data_encoding in conv - secret_key_to_bigarray - secret_key_of_bigarray + secret_key_to_bytes + secret_key_of_bytes (Fixed.bytes secret_key_size) let nonce_encoding = - Data_encoding.Fixed.bytes nonce_size + let open Data_encoding in + conv + Bigstring.to_bytes + Bigstring.of_bytes + (Fixed.bytes nonce_size) + let neuterize : secret_key -> public_key = Box.neuterize let equal : public_key -> public_key -> bool = Box.equal let pp_pk ppf pk = - MBytes.pp_hex ppf (public_key_to_bigarray pk) + MBytes.pp_hex ppf (public_key_to_bytes pk) diff --git a/src/lib_crypto/crypto_box.mli b/src/lib_crypto/crypto_box.mli index a6cf448a8c40..eb5f276fb580 100644 --- a/src/lib_crypto/crypto_box.mli +++ b/src/lib_crypto/crypto_box.mli @@ -45,13 +45,13 @@ val generate_nonces : module Secretbox : sig type key - val unsafe_of_bytes : MBytes.t -> key + val unsafe_of_bytes : Bigstring.t -> key - val box_noalloc : key -> nonce -> MBytes.t -> unit - val box_open_noalloc : key -> nonce -> MBytes.t -> bool + val box_noalloc : key -> nonce -> Bigstring.t -> unit + val box_open_noalloc : key -> nonce -> Bigstring.t -> bool - val box : key -> MBytes.t -> nonce -> MBytes.t - val box_open : key -> MBytes.t -> nonce -> MBytes.t option + val box : key -> MBytes.t -> nonce -> Bigstring.t + val box_open : key -> Bigstring.t -> nonce -> MBytes.t option end type target @@ -72,8 +72,8 @@ val random_keypair : unit -> secret_key * public_key * Public_key_hash.t val precompute : secret_key -> public_key -> channel_key -val fast_box : channel_key -> MBytes.t -> nonce -> MBytes.t -val fast_box_open : channel_key -> MBytes.t -> nonce -> MBytes.t option +val fast_box : channel_key -> Bigstring.t -> nonce -> Bigstring.t +val fast_box_open : channel_key -> Bigstring.t -> nonce -> Bigstring.t option val fast_box_noalloc : channel_key -> nonce -> MBytes.t -> unit val fast_box_open_noalloc : channel_key -> nonce -> MBytes.t -> bool @@ -81,12 +81,12 @@ val fast_box_open_noalloc : channel_key -> nonce -> MBytes.t -> bool val check_proof_of_work : public_key -> nonce -> target -> bool val generate_proof_of_work : ?max:int -> public_key -> target -> nonce -val public_key_to_bigarray : public_key -> Cstruct.buffer -val public_key_of_bigarray : Cstruct.buffer -> public_key +val public_key_to_bytes : public_key -> MBytes.t +val public_key_of_bytes : MBytes.t -> public_key val public_key_size : int -val secret_key_to_bigarray : secret_key -> Cstruct.buffer -val secret_key_of_bigarray : Cstruct.buffer -> secret_key +val secret_key_to_bytes : secret_key -> MBytes.t +val secret_key_of_bytes : MBytes.t -> secret_key val secret_key_size : int val public_key_encoding : public_key Data_encoding.t diff --git a/src/lib_crypto/ed25519.ml b/src/lib_crypto/ed25519.ml index 28639a552ded..c5ad9a2a02b2 100644 --- a/src/lib_crypto/ed25519.ml +++ b/src/lib_crypto/ed25519.ml @@ -48,25 +48,23 @@ module Public_key = struct let name = "Ed25519.Public_key" let title = "Ed25519 public key" - let to_string s = MBytes.to_string (Sign.unsafe_to_bytes s) + let to_string s = Bigstring.to_string (Sign.unsafe_to_bytes s) let of_string_opt s = if String.length s < Sign.pkbytes then None else - let pk = MBytes.create Sign.pkbytes in - MBytes.blit_of_string s 0 pk 0 Sign.pkbytes ; + let pk = Bigstring.create Sign.pkbytes in + Bigstring.blit_of_string s 0 pk 0 Sign.pkbytes ; Some (Sign.unsafe_pk_of_bytes pk) let to_bytes pk = - let buf = MBytes.create Sign.pkbytes in - Sign.blit_to_bytes pk buf ; - buf + Bigstring.to_bytes (Sign.unsafe_to_bytes pk) let of_bytes_opt buf = let buflen = MBytes.length buf in if buflen < Sign.pkbytes then None else - let pk = MBytes.create Sign.pkbytes in - MBytes.blit buf 0 pk 0 Sign.pkbytes ; + let pk = Bigstring.create Sign.pkbytes in + Bigstring.blit_of_bytes buf 0 pk 0 Sign.pkbytes ; Some (Sign.unsafe_pk_of_bytes pk) let size = Sign.pkbytes @@ -86,12 +84,12 @@ module Public_key = struct Base58.check_encoded_prefix b58check_encoding "edpk" 54 let hash v = - Public_key_hash.hash_bytes [ Sign.unsafe_to_bytes v ] + Public_key_hash.hash_bytes [ Bigstring.to_bytes (Sign.unsafe_to_bytes v) ] include Compare.Make(struct type nonrec t = t let compare a b = - MBytes.compare (Sign.unsafe_to_bytes a) (Sign.unsafe_to_bytes b) + Bigstring.compare (Sign.unsafe_to_bytes a) (Sign.unsafe_to_bytes b) end) include Helpers.MakeRaw(struct @@ -135,16 +133,14 @@ module Secret_key = struct let size = Sign.skbytes - let to_bytes sk = - let buf = MBytes.create Sign.skbytes in - Sign.blit_to_bytes sk buf ; - buf + let to_bigstring sk = Sign.unsafe_to_bytes sk + let to_bytes sk = Bigstring.to_bytes (to_bigstring sk) let of_bytes_opt s = if MBytes.length s > 64 then None else - let sk = MBytes.create Sign.skbytes in - MBytes.blit s 0 sk 0 Sign.skbytes ; + let sk = Bigstring.create Sign.skbytes in + Bigstring.blit_of_bytes s 0 sk 0 Sign.skbytes ; Some (Sign.unsafe_sk_of_bytes sk) let to_string s = MBytes.to_string (to_bytes s) @@ -159,10 +155,10 @@ module Secret_key = struct Base58.register_encoding ~prefix: Base58.Prefix.ed25519_seed ~length: size - ~to_raw: (fun sk -> MBytes.to_string (Sign.unsafe_to_bytes sk)) + ~to_raw: (fun sk -> Bigstring.to_string (Sign.unsafe_to_bytes sk)) ~of_raw: (fun buf -> if String.length buf <> Sign.skbytes then None - else Some (Sign.unsafe_sk_of_bytes (MBytes.of_string buf))) + else Some (Sign.unsafe_sk_of_bytes (Bigstring.of_string buf))) ~wrap: (fun sk -> Data sk) (* Legacy NaCl secret key encoding. Used to store both sk and pk. *) @@ -172,15 +168,15 @@ module Secret_key = struct ~length: Sign.(skbytes + pkbytes) ~to_raw: (fun sk -> let pk = Sign.neuterize sk in - let buf = MBytes.create Sign.(skbytes + pkbytes) in + let buf = Bigstring.create Sign.(skbytes + pkbytes) in Sign.blit_to_bytes sk buf ; Sign.blit_to_bytes pk ~pos:Sign.skbytes buf ; - MBytes.to_string buf) + Bigstring.to_string buf) ~of_raw: (fun buf -> if String.length buf <> Sign.(skbytes + pkbytes) then None else - let sk = MBytes.create Sign.skbytes in - MBytes.blit_of_string buf 0 sk 0 Sign.skbytes ; + let sk = Bigstring.create Sign.skbytes in + Bigstring.blit_of_string buf 0 sk 0 Sign.skbytes ; Some (Sign.unsafe_sk_of_bytes sk)) ~wrap: (fun x -> Data x) @@ -213,7 +209,7 @@ module Secret_key = struct include Compare.Make(struct type nonrec t = t let compare a b = - MBytes.compare (Sign.unsafe_to_bytes a) (Sign.unsafe_to_bytes b) + Bigstring.compare (Sign.unsafe_to_bytes a) (Sign.unsafe_to_bytes b) end) include Helpers.MakeRaw(struct @@ -242,7 +238,7 @@ module Secret_key = struct end -type t = MBytes.t +type t = Bigstring.t type watermark = MBytes.t @@ -252,8 +248,8 @@ let title = "An Ed25519 signature" let size = Sign.bytes let of_bytes_opt s = - if MBytes.length s = size then Some s else None -let to_bytes x = x + if MBytes.length s = size then Some (Bigstring.of_bytes s) else None +let to_bytes x = Bigstring.to_bytes x let to_string s = MBytes.to_string (to_bytes s) let of_string_opt s = of_bytes_opt (MBytes.of_string s) @@ -265,8 +261,8 @@ let b58check_encoding = Base58.register_encoding ~prefix: Base58.Prefix.ed25519_signature ~length: size - ~to_raw: MBytes.to_string - ~of_raw: (fun s -> Some (MBytes.of_string s)) + ~to_raw: Bigstring.to_string + ~of_raw: (fun s -> Some (Bigstring.of_string s)) ~wrap: (fun x -> Data x) let () = @@ -302,7 +298,7 @@ include Helpers.MakeEncoder(struct let pp ppf t = Format.fprintf ppf "%s" (to_b58check t) -let zero = MBytes.make size '\000' +let zero = Bigstring.make size '\000' let sign ?watermark sk msg = let msg = @@ -311,8 +307,8 @@ let sign ?watermark sk msg = match watermark with | None -> [msg] | Some prefix -> [ prefix ; msg ] in - let signature = MBytes.create Sign.bytes in - Sign.sign ~sk ~msg ~signature ; + let signature = Bigstring.create Sign.bytes in + Sign.sign ~sk ~msg:(Bigstring.of_bytes msg) ~signature ; signature let check ?watermark pk signature msg = @@ -322,7 +318,7 @@ let check ?watermark pk signature msg = match watermark with | None -> [msg] | Some prefix -> [ prefix ; msg ] in - Sign.verify ~pk ~signature ~msg + Sign.verify ~pk ~signature ~msg:(Bigstring.of_bytes msg) let generate_key ?seed () = match seed with @@ -330,26 +326,28 @@ let generate_key ?seed () = let pk, sk = Sign.keypair () in Public_key.hash pk, pk, sk | Some seed -> - let seedlen = MBytes.length seed in + let seedlen = Bigstring.length seed in if seedlen < Sign.skbytes then invalid_arg (Printf.sprintf "Ed25519.generate_key: seed must \ be at least %d bytes long (got %d)" Sign.skbytes seedlen) ; - let sk = MBytes.create Sign.skbytes in - MBytes.blit seed 0 sk 0 Sign.skbytes ; + let sk = Bigstring.create Sign.skbytes in + Bigstring.blit seed 0 sk 0 Sign.skbytes ; let sk = Sign.unsafe_sk_of_bytes sk in let pk = Sign.neuterize sk in Public_key.hash pk, pk, sk let deterministic_nonce sk msg = - Hash.SHA256.HMAC.digest ~key: (Secret_key.to_bytes sk) ~msg + let msg = Bigstring.of_bytes msg in + let key = Secret_key.to_bigstring sk in + Hash.SHA256.HMAC.digest ~key ~msg let deterministic_nonce_hash sk msg = - Blake2B.to_bytes (Blake2B.hash_bytes [deterministic_nonce sk msg]) - + Blake2B.to_bytes + (Blake2B.hash_bytes [Bigstring.to_bytes (deterministic_nonce sk msg)]) include Compare.Make(struct type nonrec t = t - let compare = MBytes.compare + let compare = Bigstring.compare end) diff --git a/src/lib_crypto/p256.ml b/src/lib_crypto/p256.ml index dd94cfa19eae..793402f52ea1 100644 --- a/src/lib_crypto/p256.ml +++ b/src/lib_crypto/p256.ml @@ -48,10 +48,11 @@ module Public_key = struct let name = "P256.Public_key" let title = "A P256 public key" - let to_bytes = to_bytes ~compress:true - let of_bytes_opt = pk_of_bytes secp256r1 - + let to_bigstring = to_bytes ~compress:true + let to_bytes b = Bigstring.to_bytes (to_bigstring b) let to_string s = MBytes.to_string (to_bytes s) + + let of_bytes_opt b = pk_of_bytes secp256r1 (Bigstring.of_bytes b) let of_string_opt s = of_bytes_opt (MBytes.of_string s) let size = compressed_size secp256r1 @@ -121,11 +122,12 @@ module Secret_key = struct let size = sk_size secp256r1 let of_bytes_opt buf = - Option.map ~f:fst (sk_of_bytes secp256r1 buf) - - let to_bytes = to_bytes ~compress:true + Option.map ~f:fst (sk_of_bytes secp256r1 (Bigstring.of_bytes buf)) + let to_bigstring = to_bytes ~compress:true + let to_bytes t = Bigstring.to_bytes (to_bigstring t) let to_string s = MBytes.to_string (to_bytes s) + let of_string_opt s = of_bytes_opt (MBytes.of_string s) let to_public_key = neuterize @@ -182,7 +184,7 @@ module Secret_key = struct end -type t = MBytes.t +type t = Bigstring.t type watermark = MBytes.t @@ -192,9 +194,9 @@ let title = "A P256 signature" let size = pk_size secp256r1 let of_bytes_opt s = - if MBytes.length s = size then Some s else None + if MBytes.length s = size then Some (Bigstring.of_bytes s) else None -let to_bytes s = s +let to_bytes s = Bigstring.to_bytes s let to_string s = MBytes.to_string (to_bytes s) let of_string_opt s = of_bytes_opt (MBytes.of_string s) @@ -252,7 +254,7 @@ let sign ?watermark sk msg = match watermark with | None -> [msg] | Some prefix -> [ prefix ; msg ] in - match sign sk msg with + match sign sk (Bigstring.of_bytes msg) with | None -> (* Will never happen in practice. This can only happen in case of RNG error. *) @@ -266,10 +268,10 @@ let check ?watermark public_key signature msg = match watermark with | None -> [msg] | Some prefix -> [ prefix ; msg ] in - verify public_key ~msg ~signature + verify public_key ~msg:(Bigstring.of_bytes msg) ~signature -let generate_key ?(seed=Rand.generate 32) () = - let seedlen = MBytes.length seed in +let generate_key ?(seed=Hacl.Rand.gen 32) () = + let seedlen = Bigstring.length seed in if seedlen < 32 then invalid_arg (Printf.sprintf "P256.generate_key: seed must be at \ least 32 bytes long (was %d)" seedlen) ; @@ -280,13 +282,15 @@ let generate_key ?(seed=Rand.generate 32) () = pkh, pk, sk let deterministic_nonce sk msg = - Hacl.Hash.SHA256.HMAC.digest ~key: (Secret_key.to_bytes sk) ~msg + let msg = Bigstring.of_bytes msg in + let key = Secret_key.to_bigstring sk in + Hacl.Hash.SHA256.HMAC.digest ~key ~msg let deterministic_nonce_hash sk msg = - Blake2B.to_bytes (Blake2B.hash_bytes [deterministic_nonce sk msg]) - + let nonce = deterministic_nonce sk msg in + Blake2B.to_bytes (Blake2B.hash_bytes [Bigstring.to_bytes nonce]) include Compare.Make(struct type nonrec t = t - let compare = MBytes.compare + let compare = Bigstring.compare end) diff --git a/src/lib_crypto/rand.ml b/src/lib_crypto/rand.ml index ba6c1f5dd6f5..7405ec6c0625 100644 --- a/src/lib_crypto/rand.ml +++ b/src/lib_crypto/rand.ml @@ -23,15 +23,15 @@ (* *) (*****************************************************************************) -let generate = Hacl.Rand.gen +let generate len = Bigstring.to_bytes (Hacl.Rand.gen len) let generate_into ?(pos=0) ?len buf = - let buflen = MBytes.length buf in + let buflen = Bytes.length buf in let len = match len with | Some len -> len | None -> buflen - pos in if pos < 0 || len < 0 || pos + len > buflen then invalid_arg (Printf.sprintf "Rand.generate_into: \ invalid slice (pos=%d len=%d)" pos len) ; - let buf = MBytes.sub buf pos len in - Hacl.Rand.write buf + let rand = Hacl.Rand.gen len in + Bigstring.blit_to_bytes rand 0 buf pos len diff --git a/src/lib_crypto/rand.mli b/src/lib_crypto/rand.mli index f78f0dac6c08..d9206d615b49 100644 --- a/src/lib_crypto/rand.mli +++ b/src/lib_crypto/rand.mli @@ -23,10 +23,10 @@ (* *) (*****************************************************************************) -val generate : int -> Cstruct.buffer +val generate : int -> MBytes.t (** [generate len] is [len] random bytes. *) -val generate_into : ?pos:int -> ?len:int -> Cstruct.buffer -> unit +val generate_into : ?pos:int -> ?len:int -> MBytes.t -> unit (** [generate_into ?pos ?len buf] writes [len] (default: - [MBytes.length buf]) bytes in [buf] starting at [pos] (default: + [Bigstring.length buf]) bytes in [buf] starting at [pos] (default: [0]). *) diff --git a/src/lib_crypto/s.ml b/src/lib_crypto/s.ml index 802e9031a1dd..698a017955b8 100644 --- a/src/lib_crypto/s.ml +++ b/src/lib_crypto/s.ml @@ -254,9 +254,9 @@ module type SIGNATURE = sig val sign: ?watermark:watermark -> Secret_key.t -> MBytes.t -> t val check: ?watermark:watermark -> Public_key.t -> t -> MBytes.t -> bool - val generate_key: ?seed:MBytes.t -> unit -> (Public_key_hash.t * Public_key.t * Secret_key.t) + val generate_key: ?seed:Bigstring.t -> unit -> (Public_key_hash.t * Public_key.t * Secret_key.t) - val deterministic_nonce: Secret_key.t -> MBytes.t -> MBytes.t + val deterministic_nonce: Secret_key.t -> MBytes.t -> Bigstring.t val deterministic_nonce_hash: Secret_key.t -> MBytes.t -> MBytes.t diff --git a/src/lib_crypto/secp256k1.ml b/src/lib_crypto/secp256k1.ml index 733642b9dadd..fd75aba81783 100644 --- a/src/lib_crypto/secp256k1.ml +++ b/src/lib_crypto/secp256k1.ml @@ -42,7 +42,7 @@ open Libsecp256k1.External let context = let ctx = Context.create () in - match Context.randomize ctx (Rand.generate 32) with + match Context.randomize ctx (Hacl.Rand.gen 32) with | false -> failwith "Secp256k1 context randomization failed. Aborting." | true -> ctx @@ -53,9 +53,9 @@ module Public_key = struct let name = "Secp256k1.Public_key" let title = "A Secp256k1 public key" - let to_bytes pk = Key.to_bytes context pk + let to_bytes pk = Bigstring.to_bytes (Key.to_bytes context pk) let of_bytes_opt s = - try Some (Key.read_pk_exn context s) + try Some (Key.read_pk_exn context (Bigstring.of_bytes s)) with _ -> None let to_string s = MBytes.to_string (to_bytes s) @@ -128,10 +128,12 @@ module Secret_key = struct let size = Key.secret_bytes let of_bytes_opt s = - match Key.read_sk context s with + match Key.read_sk context (Bigstring.of_bytes s) with | Ok x -> Some x | _ -> None - let to_bytes x = Key.to_bytes context x + + let to_bigstring = Key.to_bytes context + let to_bytes x = Bigstring.to_bytes (to_bigstring x) let to_string s = MBytes.to_string (to_bytes s) let of_string_opt s = of_bytes_opt (MBytes.of_string s) @@ -155,7 +157,7 @@ module Secret_key = struct include Compare.Make(struct type nonrec t = t let compare a b = - MBytes.compare (Key.buffer a) (Key.buffer b) + Bigstring.compare (Key.buffer a) (Key.buffer b) end) include Helpers.MakeRaw(struct @@ -200,9 +202,10 @@ let title = "A Secp256k1 signature" let size = Sign.plain_bytes let of_bytes_opt s = - match Sign.read context s with Ok s -> Some s | Error _ -> None + match Sign.read context (Bigstring.of_bytes s) with + | Ok s -> Some s | Error _ -> None -let to_bytes = Sign.to_bytes ~der:false context +let to_bytes t = Bigstring.to_bytes (Sign.to_bytes ~der:false context t) let to_string s = MBytes.to_string (to_bytes s) let of_string_opt s = of_bytes_opt (MBytes.of_string s) @@ -224,7 +227,7 @@ let () = include Compare.Make(struct type nonrec t = t let compare a b = - MBytes.compare (Sign.buffer a) (Sign.buffer b) + Bigstring.compare (Sign.buffer a) (Sign.buffer b) end) include Helpers.MakeRaw(struct @@ -266,7 +269,7 @@ let sign ?watermark sk msg = match watermark with | None -> [msg] | Some prefix -> [ prefix ; msg ] in - Sign.sign_exn context ~sk msg + Sign.sign_exn context ~sk (Bigstring.of_bytes msg) let check ?watermark public_key signature msg = let msg = @@ -275,16 +278,19 @@ let check ?watermark public_key signature msg = match watermark with | None -> [msg] | Some prefix -> [ prefix ; msg ] in - Sign.verify_exn context ~pk:public_key ~msg ~signature + Sign.verify_exn context ~pk:public_key ~msg:(Bigstring.of_bytes msg) ~signature -let generate_key ?(seed=Rand.generate 32) () = +let generate_key ?(seed=Hacl.Rand.gen 32) () = let sk = Key.read_sk_exn context seed in let pk = Key.neuterize_exn context sk in let pkh = Public_key.hash pk in pkh, pk, sk let deterministic_nonce sk msg = - Hacl.Hash.SHA256.HMAC.digest ~key: (Secret_key.to_bytes sk) ~msg + let msg = Bigstring.of_bytes msg in + let key = Secret_key.to_bigstring sk in + Hacl.Hash.SHA256.HMAC.digest ~key ~msg let deterministic_nonce_hash sk msg = - Blake2B.to_bytes (Blake2B.hash_bytes [deterministic_nonce sk msg]) + let nonce = deterministic_nonce sk msg in + Blake2B.to_bytes (Blake2B.hash_bytes [Bigstring.to_bytes nonce]) diff --git a/src/lib_crypto/signature.mli b/src/lib_crypto/signature.mli index 675c6ed89ca2..4095a8c599d7 100644 --- a/src/lib_crypto/signature.mli +++ b/src/lib_crypto/signature.mli @@ -76,5 +76,5 @@ val algo_param: unit -> (algo, 'a) Clic.parameter val generate_key: ?algo:algo -> - ?seed:MBytes.t -> + ?seed:Bigstring.t -> unit -> public_key_hash * public_key * secret_key diff --git a/src/lib_crypto/test/test_deterministic_nonce.ml b/src/lib_crypto/test/test_deterministic_nonce.ml index c430eb0b54e8..42da2102ebf1 100644 --- a/src/lib_crypto/test/test_deterministic_nonce.ml +++ b/src/lib_crypto/test/test_deterministic_nonce.ml @@ -28,7 +28,7 @@ let test_hash_matches (module X : S.SIGNATURE) () = let data = MBytes.of_string "ce input sa pun eu aici oare?" in let nonce = X.deterministic_nonce sk data in let nonce_hash = X.deterministic_nonce_hash sk data in - let hashed_nonce = Blake2B.hash_bytes [nonce] in + let hashed_nonce = Blake2B.hash_bytes [Bigstring.to_bytes nonce] in if nonce_hash <> Blake2B.to_bytes hashed_nonce then Alcotest.failf "the hash of deterministic_nonce is NOT deterministic_nonce_hash" -- GitLab From 3559c222bc8bd8482da9ff79552a390383ae4235 Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Tue, 2 Jul 2019 09:38:27 +0200 Subject: [PATCH 04/14] base: MBytes = bytes --- src/lib_base/block_locator.ml | 8 +++++--- src/lib_base/dune | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib_base/block_locator.ml b/src/lib_base/block_locator.ml index 0fa8326a87ca..88bc68843e5f 100644 --- a/src/lib_base/block_locator.ml +++ b/src/lib_base/block_locator.ml @@ -91,19 +91,21 @@ module Step : sig end = struct - type state = Int32.t * int * MBytes.t + type state = Int32.t * int * Bigstring.t + + let update st b = Hacl.Hash.SHA256.update st (Bigstring.of_bytes b) let init seed head = let open Hacl.Hash in let st = SHA256.init () in - List.iter (SHA256.update st) [ + List.iter (update st) [ P2p_peer.Id.to_bytes seed.sender_id ; P2p_peer.Id.to_bytes seed.receiver_id ; Block_hash.to_bytes head ] ; (1l, 9, SHA256.finish st) let draw seed n = - Int32.rem (MBytes.get_int32 seed 0) n, + Int32.rem (EndianBigstring.BigEndian.get_int32 seed 0) n, Hacl.Hash.SHA256.digest seed let next (step, counter, seed) = diff --git a/src/lib_base/dune b/src/lib_base/dune index 6d06270f5a24..858eda938b9c 100644 --- a/src/lib_base/dune +++ b/src/lib_base/dune @@ -20,6 +20,7 @@ tezos-rpc tezos-clic tezos-micheline + ocplib-endian.bigstring re.str ptime ezjsonm -- GitLab From 30718059ec4c6ec18f30bc17bd186bab26fcb743 Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Tue, 2 Jul 2019 09:42:46 +0200 Subject: [PATCH 05/14] p2p: MBytes.t = bytes --- src/lib_p2p/p2p_discovery.ml | 4 ++-- src/lib_p2p/p2p_fd.ml | 3 +-- src/lib_p2p/p2p_fd.mli | 2 +- src/lib_stdlib_unix/lwt_utils_unix.ml | 4 ++-- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/lib_p2p/p2p_discovery.ml b/src/lib_p2p/p2p_discovery.ml index 6cc408c56ee0..583b1a3745f5 100644 --- a/src/lib_p2p/p2p_discovery.ml +++ b/src/lib_p2p/p2p_discovery.ml @@ -82,7 +82,7 @@ module Answer = struct let rec aux () = let buf = MBytes.create Message.length in protect ~canceler:st.canceler begin fun () -> - Lwt_bytes.recvfrom socket buf 0 Message.length [] >>= fun content -> + Lwt_unix.recvfrom socket buf 0 Message.length [] >>= fun content -> lwt_debug "Received discovery message..." >>= fun () -> return content end >>=? function @@ -188,7 +188,7 @@ module Sender = struct let addr = Lwt_unix.ADDR_INET (broadcast_ipv4, st.discovery_port) in Lwt_unix.connect socket addr >>= fun () -> lwt_debug "Broadcasting discovery message..." >>= fun () -> - Lwt_bytes.sendto socket msg 0 Message.length [] addr >>= fun _len -> + Lwt_unix.sendto socket msg 0 Message.length [] addr >>= fun _len -> Lwt_utils_unix.safe_close socket end begin fun _exn -> diff --git a/src/lib_p2p/p2p_fd.ml b/src/lib_p2p/p2p_fd.ml index a1714ae5899f..12fdbdcd1b83 100644 --- a/src/lib_p2p/p2p_fd.ml +++ b/src/lib_p2p/p2p_fd.ml @@ -75,7 +75,7 @@ let close t = let read t buf pos len = log t "try-read: %d" len; - Lwt_bytes.read t.fd buf pos len >>= fun nread -> + Lwt_unix.read t.fd buf pos len >>= fun nread -> t.nread <- t.nread + nread ; log t "read: %d (%d)" nread t.nread ; Lwt.return nread @@ -104,4 +104,3 @@ module Table = let equal { id = x ; _ } { id = y ; _ } = x = y let hash { id ; _ } = Hashtbl.hash id end) - diff --git a/src/lib_p2p/p2p_fd.mli b/src/lib_p2p/p2p_fd.mli index a8a3adb1c656..2e6ae82750d2 100644 --- a/src/lib_p2p/p2p_fd.mli +++ b/src/lib_p2p/p2p_fd.mli @@ -29,7 +29,7 @@ type t val id : t -> int -val read : t -> Lwt_bytes.t -> int -> int -> int Lwt.t +val read : t -> MBytes.t -> int -> int -> int Lwt.t val close : t -> unit Lwt.t val write : t -> MBytes.t -> unit Lwt.t val socket : Lwt_unix.socket_domain -> Lwt_unix.socket_type -> int -> t diff --git a/src/lib_stdlib_unix/lwt_utils_unix.ml b/src/lib_stdlib_unix/lwt_utils_unix.ml index 42cd48126d06..bdea153d0558 100644 --- a/src/lib_stdlib_unix/lwt_utils_unix.ml +++ b/src/lib_stdlib_unix/lwt_utils_unix.ml @@ -60,7 +60,7 @@ let read_mbytes ?(pos=0) ?len fd buf = if len = 0 then Lwt.return_unit else - Lwt_bytes.read fd buf pos len >>= function + Lwt_unix.read fd buf pos len >>= function | 0 -> Lwt.fail End_of_file (* other endpoint cleanly closed its connection *) | nb_read -> inner (pos + nb_read) (len - nb_read) in @@ -72,7 +72,7 @@ let write_mbytes ?(pos=0) ?len descr buf = if len = 0 then Lwt.return_unit else - Lwt_bytes.write descr buf pos len >>= function + Lwt_unix.write descr buf pos len >>= function | 0 -> Lwt.fail End_of_file (* other endpoint cleanly closed its connection *) | nb_written -> inner (pos + nb_written) (len - nb_written) in inner pos len -- GitLab From 0472db898fd8c3c1ba09dbb1ab224f34a140a4bb Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Thu, 18 Jul 2019 09:17:25 +0200 Subject: [PATCH 06/14] protocol-environment: MBytes.t = bytes --- src/lib_protocol_environment/tezos_protocol_environment.ml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib_protocol_environment/tezos_protocol_environment.ml b/src/lib_protocol_environment/tezos_protocol_environment.ml index 14acf631d31c..1d8b512f6336 100644 --- a/src/lib_protocol_environment/tezos_protocol_environment.ml +++ b/src/lib_protocol_environment/tezos_protocol_environment.ml @@ -291,8 +291,9 @@ module MakeV1 (Param : sig val name: string end) () = struct module Option = Option module MBytes = MBytes module Raw_hashes = struct - let sha256 msg = Hacl.Hash.SHA256.digest msg - let sha512 msg = Hacl.Hash.SHA512.digest msg + let conv f x = Bigstring.to_bytes (f (Bigstring.of_bytes x)) + let sha256 msg = conv Hacl.Hash.SHA256.digest msg + let sha512 msg = conv Hacl.Hash.SHA512.digest msg let blake2b msg = Blake2B.to_bytes (Blake2B.hash_bytes [ msg ]) end module Z = struct -- GitLab From 3be60f21541e738d3725c2e0f810fb63ef01d7f6 Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Thu, 18 Jul 2019 09:17:45 +0200 Subject: [PATCH 07/14] client-base: MBytes = bytes --- src/lib_client_base/client_keys.ml | 2 +- src/lib_client_base/client_keys.mli | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib_client_base/client_keys.ml b/src/lib_client_base/client_keys.ml index ee841ef98cf1..0a0009788b02 100644 --- a/src/lib_client_base/client_keys.ml +++ b/src/lib_client_base/client_keys.ml @@ -150,7 +150,7 @@ module type SIGNER = sig val sign : ?watermark: Signature.watermark -> sk_uri -> MBytes.t -> Signature.t tzresult Lwt.t - val deterministic_nonce : sk_uri -> MBytes.t -> MBytes.t tzresult Lwt.t + val deterministic_nonce : sk_uri -> MBytes.t -> Bigstring.t tzresult Lwt.t val deterministic_nonce_hash : sk_uri -> MBytes.t -> MBytes.t tzresult Lwt.t val supports_deterministic_nonces : sk_uri -> bool tzresult Lwt.t end diff --git a/src/lib_client_base/client_keys.mli b/src/lib_client_base/client_keys.mli index 48698b9271aa..683f4b3e2150 100644 --- a/src/lib_client_base/client_keys.mli +++ b/src/lib_client_base/client_keys.mli @@ -96,7 +96,7 @@ module type SIGNER = sig [sk]. *) val deterministic_nonce : - sk_uri -> MBytes.t -> MBytes.t tzresult Lwt.t + sk_uri -> MBytes.t -> Bigstring.t tzresult Lwt.t (** [deterministic_nonce sk data] is a nonce obtained deterministically from [data] and [sk]. *) @@ -142,7 +142,7 @@ val check : pk_uri -> Signature.t -> MBytes.t -> bool tzresult Lwt.t val deterministic_nonce : - sk_uri -> MBytes.t -> MBytes.t tzresult Lwt.t + sk_uri -> MBytes.t -> Bigstring.t tzresult Lwt.t val deterministic_nonce_hash : sk_uri -> MBytes.t -> MBytes.t tzresult Lwt.t -- GitLab From 16429b8cd44a204d1dffbe92722e7f94110ffe27 Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Thu, 18 Jul 2019 09:18:02 +0200 Subject: [PATCH 08/14] signer-backends: Mbytes = bytes --- src/lib_signer_backends/encrypted.ml | 15 ++++++----- src/lib_signer_backends/ledger.ml | 30 +++++++++++---------- src/lib_signer_services/signer_messages.ml | 8 ++++-- src/lib_signer_services/signer_messages.mli | 2 +- src/lib_signer_services/signer_services.ml | 6 ++++- src/lib_signer_services/signer_services.mli | 2 +- 6 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/lib_signer_backends/encrypted.ml b/src/lib_signer_backends/encrypted.ml index 6cade24e63cd..ce89386815c1 100644 --- a/src/lib_signer_backends/encrypted.ml +++ b/src/lib_signer_backends/encrypted.ml @@ -47,7 +47,7 @@ module Raw = struct Pbkdf.SHA512.pbkdf2 ~count:32768 ~dk_len:32l ~salt ~password let encrypt ~password sk = - let salt = Rand.generate salt_len in + let salt = Hacl.Rand.gen salt_len in let key = Crypto_box.Secretbox.unsafe_of_bytes (pbkdf ~salt ~password) in let msg = match (sk : Signature.secret_key) with @@ -57,13 +57,12 @@ module Raw = struct Data_encoding.Binary.to_bytes_exn Secp256k1.Secret_key.encoding sk | P256 sk -> Data_encoding.Binary.to_bytes_exn P256.Secret_key.encoding sk in - MBytes.concat "" [ salt ; - Crypto_box.Secretbox.box key msg nonce ] + Bigstring.concat "" [ salt ; Crypto_box.Secretbox.box key msg nonce ] let decrypt algo ~password ~encrypted_sk = - let salt = MBytes.sub encrypted_sk 0 salt_len in + let salt = Bigstring.sub encrypted_sk 0 salt_len in let encrypted_sk = - MBytes.sub encrypted_sk salt_len encrypted_size in + Bigstring.sub encrypted_sk salt_len encrypted_size in let key = Crypto_box.Secretbox.unsafe_of_bytes (pbkdf ~salt ~password) in match Crypto_box.Secretbox.box_open key encrypted_sk nonce, algo with | None, _ -> return_none @@ -149,6 +148,7 @@ let rec interactive_decrypt_loop cctxt#prompt_password "Enter password for encrypted key \"%s\": " name end >>=? fun password -> + let password = Bigstring.of_bytes password in Raw.decrypt algo ~password ~encrypted_sk >>=? function | Some sk -> passwords := password :: !passwords ; @@ -161,7 +161,7 @@ let password_file_load ctxt = match ctxt#load_passwords with | Some stream -> Lwt_stream.iter (fun p -> - passwords := MBytes.of_string p :: !passwords) + passwords := Bigstring.of_string p :: !passwords) stream >>= fun () -> return_unit | None -> return_unit @@ -183,6 +183,7 @@ let decrypt_payload cctxt ?name encrypted_sk = return (Signature.P256, encrypted_sk) | _ -> failwith "Not a Base58Check-encoded encrypted key" end >>=? fun (algo, encrypted_sk) -> + let encrypted_sk = Bigstring.of_bytes encrypted_sk in noninteractive_decrypt_loop algo ~encrypted_sk !passwords >>=? function | Some sk -> return sk | None -> interactive_decrypt_loop cctxt ?name ~encrypted_sk algo @@ -229,11 +230,13 @@ let rec read_password (cctxt : #Client_context.io) = let encrypt cctxt sk = read_password cctxt >>=? fun password -> + let password = Bigstring.of_bytes password in let payload = Raw.encrypt ~password sk in let encoding = match sk with | Ed25519 _ -> Encodings.ed25519 | Secp256k1 _ -> Encodings.secp256k1 | P256 _ -> Encodings.p256 in + let payload = Bigstring.to_bytes payload in let path = Base58.simple_encode encoding payload in let sk_uri = Client_keys.make_sk_uri (Uri.make ~scheme ~path ()) in Hashtbl.replace decrypted sk_uri sk ; diff --git a/src/lib_signer_backends/ledger.ml b/src/lib_signer_backends/ledger.ml index ec09f9432f84..247c1f8f4d59 100644 --- a/src/lib_signer_backends/ledger.ml +++ b/src/lib_signer_backends/ledger.ml @@ -178,29 +178,31 @@ module Ledger_commands = struct ~main_chain_id ~main_hwm ~test_hwm end end >>|? fun pk -> - let pk = Cstruct.to_bigarray pk in match curve with | Ledgerwallet_tezos.Ed25519 -> + let pk = Cstruct.to_bytes pk in MBytes.set_int8 pk 0 0 ; (* hackish, but works. *) Data_encoding.Binary.of_bytes_exn Signature.Public_key.encoding pk | Secp256k1 -> let open Libsecp256k1.External in - let buf = MBytes.create (Key.compressed_pk_bytes + 1) in - let pk = Key.read_pk_exn secp256k1_ctx pk in - MBytes.set_int8 buf 0 1 ; + let buf = Bigstring.create (Key.compressed_pk_bytes + 1) in + let pk = Key.read_pk_exn secp256k1_ctx (Cstruct.to_bigarray pk) in + EndianBigstring.BigEndian.set_int8 buf 0 1 ; let _nb_written = Key.write secp256k1_ctx ~pos:1 buf pk in - Data_encoding.Binary.of_bytes_exn Signature.Public_key.encoding buf + Data_encoding.Binary.of_bytes_exn Signature.Public_key.encoding + (Bigstring.to_bytes buf) | Secp256r1 -> let open Uecc in let pklen = compressed_size secp256r1 in - let buf = MBytes.create (pklen + 1) in - match pk_of_bytes secp256r1 pk with + let buf = Bigstring.create (pklen + 1) in + match pk_of_bytes secp256r1 (Cstruct.to_bigarray pk) with | None -> Pervasives.failwith "Impossible to read P256 public key from Ledger" | Some pk -> - MBytes.set_int8 buf 0 2 ; - let _nb_written = write_key ~compress:true (MBytes.sub buf 1 pklen) pk in - Data_encoding.Binary.of_bytes_exn Signature.Public_key.encoding buf + EndianBigstring.BigEndian.set_int8 buf 0 2 ; + let _nb_written = write_key ~compress:true (Bigstring.sub buf 1 pklen) pk in + Data_encoding.Binary.of_bytes_exn Signature.Public_key.encoding + (Bigstring.to_bytes buf) let get_public_key = public_key_returning_instruction `Get_public_key @@ -255,11 +257,11 @@ module Ledger_commands = struct * (Cstruct.of_bigarray (Blake2B.(to_bytes (hash_bytes [ msg ])))) * else *) Ledgerwallet_tezos.sign - ~pp hid curve path (Cstruct.of_bigarray msg) + ~pp hid curve path (Cstruct.of_bytes msg) end >>=? fun signature -> match curve with | Ed25519 -> - let signature = Cstruct.to_bigarray signature in + let signature = Cstruct.to_bytes signature in let signature = Ed25519.of_bytes_exn signature in return (Signature.of_ed25519 signature) | Secp256k1 -> @@ -269,7 +271,7 @@ module Ledger_commands = struct let open Libsecp256k1.External in let signature = Sign.read_der_exn secp256k1_ctx signature in let bytes = Sign.to_bytes secp256k1_ctx signature in - let signature = Secp256k1.of_bytes_exn bytes in + let signature = Secp256k1.of_bytes_exn (Bigstring.to_bytes bytes) in return (Signature.of_secp256k1 signature) | Secp256r1 -> (* Remove parity info *) @@ -279,7 +281,7 @@ module Ledger_commands = struct (* We use secp256r1 library to extract P256 DER signature. *) let signature = Sign.read_der_exn secp256k1_ctx signature in let buf = Sign.to_bytes secp256k1_ctx signature in - let signature = P256.of_bytes_exn buf in + let signature = P256.of_bytes_exn (Bigstring.to_bytes buf) in return (Signature.of_p256 signature) end diff --git a/src/lib_signer_services/signer_messages.ml b/src/lib_signer_services/signer_messages.ml index 6b0c52898e15..e3b873bcaf5f 100644 --- a/src/lib_signer_services/signer_messages.ml +++ b/src/lib_signer_services/signer_messages.ml @@ -92,10 +92,14 @@ module Deterministic_nonce = struct module Response = struct - type t = MBytes.t + type t = Bigstring.t + + let bigstring = + let open Data_encoding in + conv Bigstring.to_bytes Bigstring.of_bytes bytes let encoding = - Data_encoding.(obj1 (req "deterministic_nonce" bytes)) + Data_encoding.(obj1 (req "deterministic_nonce" bigstring)) end diff --git a/src/lib_signer_services/signer_messages.mli b/src/lib_signer_services/signer_messages.mli index 2a0f0794c795..02e9510f33f5 100644 --- a/src/lib_signer_services/signer_messages.mli +++ b/src/lib_signer_services/signer_messages.mli @@ -52,7 +52,7 @@ module Deterministic_nonce : sig module Request : Authenticated_request module Response : sig - type t = MBytes.t + type t = Bigstring.t val encoding : t Data_encoding.t end diff --git a/src/lib_signer_services/signer_services.ml b/src/lib_signer_services/signer_services.ml index 694125b08642..76ff35ffaa11 100644 --- a/src/lib_signer_services/signer_services.ml +++ b/src/lib_signer_services/signer_services.ml @@ -43,12 +43,16 @@ let sign = ~output: Data_encoding.(obj1 (req "signature" Signature.encoding)) RPC_path.(root / "keys" /: Signature.Public_key_hash.rpc_arg) +let bigstring = + let open Data_encoding in + conv Bigstring.to_bytes Bigstring.of_bytes bytes + let deterministic_nonce = RPC_service.post_service ~description: "Obtain some random data generated deterministically from some piece of data with a given remote key" ~query ~input: Data_encoding.bytes - ~output: Data_encoding.(obj1 (req "deterministic_nonce" bytes)) + ~output: Data_encoding.(obj1 (req "deterministic_nonce" bigstring)) RPC_path.(root / "keys" /: Signature.Public_key_hash.rpc_arg) let deterministic_nonce_hash = diff --git a/src/lib_signer_services/signer_services.mli b/src/lib_signer_services/signer_services.mli index 2785dec0eab7..8551ef7f51e6 100644 --- a/src/lib_signer_services/signer_services.mli +++ b/src/lib_signer_services/signer_services.mli @@ -29,7 +29,7 @@ val sign : val deterministic_nonce : ([ `POST ], unit, unit * Signature.Public_key_hash.t, - Signature.t option, MBytes.t, MBytes.t) RPC_service.t + Signature.t option, MBytes.t, Bigstring.t) RPC_service.t val deterministic_nonce_hash : ([ `POST ], unit, unit * Signature.Public_key_hash.t, -- GitLab From 30c189b719aa575d5f67ed1c28015750d1f967e6 Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Tue, 2 Jul 2019 10:29:11 +0200 Subject: [PATCH 09/14] client-commands: MBytes.t = bytes --- src/lib_client_commands/client_keys_commands.ml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lib_client_commands/client_keys_commands.ml b/src/lib_client_commands/client_keys_commands.ml index c9128cca58ad..2626fad3b74f 100644 --- a/src/lib_client_commands/client_keys_commands.ml +++ b/src/lib_client_commands/client_keys_commands.ml @@ -146,9 +146,10 @@ let rec input_fundraiser_params (cctxt : #Client_context.io_wallet) = cctxt#prompt_password "Enter the password used for the paper wallet: " >>=? fun password -> (* TODO: unicode normalization (NFKD)... *) - let passphrase = MBytes.(concat "" [of_string email ; password]) in + let passphrase = Bigstring.(concat "" [of_string email ; + Bigstring.of_bytes password]) in let sk = Bip39.to_seed ~passphrase t in - let sk = MBytes.sub sk 0 32 in + let sk = Bigstring.sub_bytes sk 0 32 in let sk : Signature.Secret_key.t = Ed25519 (Data_encoding.Binary.of_bytes_exn Ed25519.Secret_key.encoding sk) in @@ -459,7 +460,8 @@ let commands version : Client_context.full Clic.command list = (failure "secret key not present for %s" name) >>=? fun () -> Secret_key.find cctxt name >>=? fun sk_uri -> Client_keys.deterministic_nonce sk_uri data >>=? fun nonce -> - cctxt#message "%a" MBytes.pp_hex nonce >>= fun () -> return_unit) ; + cctxt#message "%a" MBytes.pp_hex (Bigstring.to_bytes nonce) + >>= fun () -> return_unit) ; command ~group ~desc: "Compute deterministic nonce hash." no_options -- GitLab From 4064ab9b19a4d7265b9e689fb2975c8cceb75f00 Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Tue, 2 Jul 2019 10:30:16 +0200 Subject: [PATCH 10/14] bin-signer: MBytes.t = bytes --- src/bin_signer/handler.mli | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin_signer/handler.mli b/src/bin_signer/handler.mli index 69ef13d747f5..d5ef9d52966e 100644 --- a/src/bin_signer/handler.mli +++ b/src/bin_signer/handler.mli @@ -44,7 +44,7 @@ val sign : val deterministic_nonce : #Client_context.wallet -> Signer_messages.Deterministic_nonce.Request.t -> - require_auth:bool -> MBytes.t tzresult Lwt.t + require_auth:bool -> Bigstring.t tzresult Lwt.t (** [deterministic_nonce cctxt req ~require_auth] generates deterministically a nonce from [req.data]. *) -- GitLab From 52b15b9294c230274fc0212fac62b1e3c5fdb443 Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Tue, 2 Jul 2019 14:32:06 +0200 Subject: [PATCH 11/14] network-sandbox: MBytes.t = bytes --- src/lib_network_sandbox/tezos_protocol.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib_network_sandbox/tezos_protocol.ml b/src/lib_network_sandbox/tezos_protocol.ml index 4af00cc56278..39f872bd47cf 100644 --- a/src/lib_network_sandbox/tezos_protocol.ml +++ b/src/lib_network_sandbox/tezos_protocol.ml @@ -11,7 +11,7 @@ module Key = struct let make name = let seed = - Tezos_stdlib.MBytes.of_string + Bigstring.of_string (String.concat ~sep:"" (List.init 42 ~f:(fun _ -> name))) in let pkh, pk, sk = Tezos_crypto.Ed25519.generate_key ~seed () in -- GitLab From f5560298d401133a86d400c8bd1a42ca4d9bb6e1 Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Tue, 2 Jul 2019 14:32:13 +0200 Subject: [PATCH 12/14] storage: MBytes.t = bytes --- src/lib_storage/context.ml | 12 ++++++------ src/lib_storage/context_dump.ml | 2 +- src/lib_storage/raw_store.ml | 17 +++++++++-------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/lib_storage/context.ml b/src/lib_storage/context.ml index cfdbc0ecd7c2..584406ad167d 100644 --- a/src/lib_storage/context.ml +++ b/src/lib_storage/context.ml @@ -30,9 +30,9 @@ module IrminPath = Irmin.Path.String_list module MBytesContent = struct type t = MBytes.t let t = - Irmin.Type.(like cstruct) - (fun x -> Cstruct.to_bigarray x) - (fun x -> Cstruct.of_bigarray x) + Irmin.Type.(like string) + (fun x -> MBytes.of_string x) + (fun x -> MBytes.to_string x) let merge = Irmin.Merge.default Irmin.Type.(option t) let pp ppf b = Format.pp_print_string ppf (MBytes.to_string b) let of_string s = Ok (MBytes.of_string s) @@ -51,9 +51,9 @@ module IrminBlake2B : Irmin.Hash.S with type t = Context_hash.t = struct let digest_size = Context_hash.size - let to_raw t = Cstruct.of_bigarray (Context_hash.to_bytes t) + let to_raw t = Cstruct.of_bytes (Context_hash.to_bytes t) let of_raw t = - match Context_hash.of_bytes_opt (Cstruct.to_bigarray t) with + match Context_hash.of_bytes_opt (Cstruct.to_bytes t) with | Some t -> t | None -> let str = Cstruct.to_string t in @@ -63,7 +63,7 @@ module IrminBlake2B : Irmin.Hash.S with type t = Context_hash.t = struct let digest t x = Context_hash.hash_bytes - [Cstruct.to_bigarray (Irmin.Type.encode_cstruct t x)] + [Cstruct.to_bytes (Irmin.Type.encode_cstruct t x)] let pp = Context_hash.pp diff --git a/src/lib_storage/context_dump.ml b/src/lib_storage/context_dump.ml index 135cb052f918..dbdeebb1fa44 100644 --- a/src/lib_storage/context_dump.ml +++ b/src/lib_storage/context_dump.ml @@ -391,7 +391,7 @@ module Make (I:Dump_interface) = struct let set_mbytes buf b = set_int64 buf (Int64.of_int (MBytes.length b)) ; - Buffer.add_bytes buf (MBytes.to_bytes b) + Buffer.add_bytes buf b let get_mbytes rbuf = get_int64 rbuf >>|? Int64.to_int >>=? fun l -> diff --git a/src/lib_storage/raw_store.ml b/src/lib_storage/raw_store.ml index 2b18af3b7c8d..b69046bcbfdb 100644 --- a/src/lib_storage/raw_store.ml +++ b/src/lib_storage/raw_store.ml @@ -99,10 +99,10 @@ let known { dir ; parent } key = let read_opt { dir ; parent } key = begin match Lwt.get parent with - | Some (txn, db, _cursor) -> Lmdb.get txn db (concat key) >>| MBytes.copy + | Some (txn, db, _cursor) -> Lmdb.get txn db (concat key) >>| Bigstring.to_bytes | None -> Lmdb.with_ro_db dir ~f:begin fun txn db -> - Lmdb.get txn db (concat key) >>| MBytes.copy + Lmdb.get txn db (concat key) >>| Bigstring.to_bytes end end |> function | Ok v -> Lwt.return_some v @@ -111,16 +111,17 @@ let read_opt { dir ; parent } key = let read { dir ; parent } key = begin match Lwt.get parent with - | Some (txn, db, _cursor) -> Lmdb.get txn db (concat key) >>| MBytes.copy + | Some (txn, db, _cursor) -> Lmdb.get txn db (concat key) >>| Bigstring.to_bytes | None -> Lmdb.with_ro_db dir ~f:begin fun txn db -> - Lmdb.get txn db (concat key) >>| MBytes.copy + Lmdb.get txn db (concat key) >>| Bigstring.to_bytes end end |> function | Ok v -> return v | Error _err -> fail (Unknown key) let store { dir ; parent } k v = + let v = Bigstring.of_bytes v in begin match Lwt.get parent with | Some (txn, db, _cursor) -> Lmdb.put txn db (concat k) v | None -> @@ -148,7 +149,7 @@ let known_dir { dir ; parent } k = let cursor_fun cursor = Lmdb.cursor_at cursor k >>= fun () -> Lmdb.cursor_get cursor >>| fun (first_k, _v) -> - (is_prefix k (MBytes.to_string first_k)) + (is_prefix k (Bigstring.to_string first_k)) in begin match Lwt.get parent with | Some (txn, db, _cursor) -> @@ -164,7 +165,7 @@ let remove_dir { dir ; parent } k = let cursor_fun cursor = Lmdb.cursor_at cursor k >>= fun () -> Lmdb.cursor_iter cursor ~f:begin fun (kk, _v) -> - let kk_string = MBytes.to_string kk in + let kk_string = Bigstring.to_string kk in if is_prefix k kk_string then begin Lmdb.cursor_del cursor end @@ -259,7 +260,7 @@ let fold t k ~init ~f = let base_len = List.length k in let rec inner ht cursor acc = Lmdb.cursor_get cursor >>=? fun (kk, _v) -> - let kk = MBytes.to_string kk in + let kk = Bigstring.to_string kk in let kk_split = split kk in match is_child ~child:kk_split ~parent:k with | false -> Lwt.return acc @@ -292,7 +293,7 @@ let fold_keys t k ~init ~f = (Lwt.return init) (let rec inner acc = Lmdb.cursor_get cursor >>=? fun (kk, _v) -> - let kk = MBytes.to_string kk in + let kk = Bigstring.to_string kk in let kk_split = split kk in match is_child ~child:kk_split ~parent:k with | false -> Lwt.return acc -- GitLab From 0308209b0181d7b48b96a6323a4a75175b991c72 Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Thu, 11 Jul 2019 14:29:39 +0200 Subject: [PATCH 13/14] shell: MBytes.t = bytes --- src/lib_shell/bench/bench_tool.ml | 4 ++-- src/lib_shell/test/test_store.ml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib_shell/bench/bench_tool.ml b/src/lib_shell/bench/bench_tool.ml index 1bf02cdb2078..8163a61da046 100644 --- a/src/lib_shell/bench/bench_tool.ml +++ b/src/lib_shell/bench/bench_tool.ml @@ -291,9 +291,9 @@ let init () = amount /? (Int64.of_int args.accounts) ) >>=? fun initial_amount -> (* Ensure a deterministic run *) - let new_seed () : MBytes.t = + let new_seed () : Bigstring.t = String.(make 32 '\000' |> map (fun _ -> Random.int 0x100 |> char_of_int)) |> - MBytes.of_string + Bigstring.of_string in map_s diff --git a/src/lib_shell/test/test_store.ml b/src/lib_shell/test/test_store.ml index bf43a74806d6..dcaf4c126e3f 100644 --- a/src/lib_shell/test/test_store.ml +++ b/src/lib_shell/test/test_store.ml @@ -198,7 +198,7 @@ let check (type t) | Some d' -> Assert.fail_msg ~expected:(MBytes.to_string d) ~given:(MBytes.to_string d') "Error while reading key %d %S\n%!" - Cstruct.(compare (of_bigarray d) (of_bigarray d')) (String.concat Filename.dir_sep k) + Bytes.(compare d d') (String.concat Filename.dir_sep k) | None -> Assert.fail_msg ~expected:(MBytes.to_string d) ~given:"" "Error while reading key %S\n%!" (String.concat Filename.dir_sep k) -- GitLab From 4f8d083fdbe58349d0d61e799dc55df8feef609f Mon Sep 17 00:00:00 2001 From: Thomas Gazagnaire Date: Thu, 11 Jul 2019 14:30:03 +0200 Subject: [PATCH 14/14] proto*: MBytes.t = bytes --- src/proto_alpha/lib_client/client_proto_context.ml | 4 ++-- src/proto_alpha/lib_delegate/client_baking_pow.mli | 4 ++-- src/proto_alpha/lib_protocol/test/activation.ml | 4 ++-- src/proto_alpha/lib_protocol/test/helpers/account.ml | 2 ++ 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/proto_alpha/lib_client/client_proto_context.ml b/src/proto_alpha/lib_client/client_proto_context.ml index 3bb25ffcf763..0d210fad32a5 100644 --- a/src/proto_alpha/lib_client/client_proto_context.ml +++ b/src/proto_alpha/lib_client/client_proto_context.ml @@ -339,9 +339,9 @@ let read_key key = failwith "" | Some t -> (* TODO: unicode normalization (NFKD)... *) - let passphrase = MBytes.(concat "" [of_string key.email ; of_string key.password]) in + let passphrase = Bigstring.(concat "" [of_string key.email ; of_string key.password]) in let sk = Bip39.to_seed ~passphrase t in - let sk = MBytes.sub sk 0 32 in + let sk = Bigstring.sub_bytes sk 0 32 in let sk : Signature.Secret_key.t = Ed25519 (Data_encoding.Binary.of_bytes_exn Ed25519.Secret_key.encoding sk) in let pk = Signature.Secret_key.to_public_key sk in diff --git a/src/proto_alpha/lib_delegate/client_baking_pow.mli b/src/proto_alpha/lib_delegate/client_baking_pow.mli index 89a67c9301dc..18d58dd246c9 100644 --- a/src/proto_alpha/lib_delegate/client_baking_pow.mli +++ b/src/proto_alpha/lib_delegate/client_baking_pow.mli @@ -27,7 +27,7 @@ open Protocol (** A null proof-of-work nonce. This should only be used to non-sensical blocks of the correct size and shape. *) -val empty_proof_of_work_nonce: Cstruct.buffer +val empty_proof_of_work_nonce: MBytes.t (** [mine cctxt chain block header builder] returns a block with a valid proof-of-work nonce. The function [builder], provided by the caller, is used @@ -38,5 +38,5 @@ val mine: Shell_services.chain -> Block_services.block -> Block_header.shell_header -> - (Cstruct.buffer -> Alpha_context.Block_header.contents) -> + (MBytes.t -> Alpha_context.Block_header.contents) -> Alpha_context.Block_header.contents tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/test/activation.ml b/src/proto_alpha/lib_protocol/test/activation.ml index 2078e96027d6..15e15a34733b 100644 --- a/src/proto_alpha/lib_protocol/test/activation.ml +++ b/src/proto_alpha/lib_protocol/test/activation.ml @@ -75,12 +75,12 @@ let secrets () = | None -> assert false | Some t -> (* TODO: unicode normalization (NFKD)... *) - let passphrase = MBytes.(concat "" [ + let passphrase = Bigstring.(concat "" [ of_string email ; of_string password ; ]) in let sk = Bip39.to_seed ~passphrase t in - let sk = MBytes.sub sk 0 32 in + let sk = Bigstring.sub_bytes sk 0 32 in let sk : Signature.Secret_key.t = Ed25519 (Data_encoding.Binary.of_bytes_exn Ed25519.Secret_key.encoding sk) in let pk = Signature.Secret_key.to_public_key sk in diff --git a/src/proto_alpha/lib_protocol/test/helpers/account.ml b/src/proto_alpha/lib_protocol/test/helpers/account.ml index 985e2b0ec0db..c62a6e5a8d98 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/account.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/account.ml @@ -35,6 +35,7 @@ type account = t let known_accounts = Signature.Public_key_hash.Table.create 17 let new_account ?seed () = + let seed = Option.map ~f:Bigstring.of_bytes seed in let (pkh, pk, sk) = Signature.generate_key ?seed () in let account = { pkh ; pk ; sk } in Signature.Public_key_hash.Table.add known_accounts pkh account ; @@ -82,6 +83,7 @@ let commitment_secret = "aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbb" let new_commitment ?seed () = + let seed = Option.map ~f:Bigstring.of_bytes seed in let (pkh, pk, sk) = Signature.generate_key ?seed ~algo:Ed25519 () in let unactivated_account = { pkh; pk; sk } in let open Commitment_repr in -- GitLab