Stdlib: use bigstring
to implement MBytes
This commit is contained in:
parent
1cd82f5d82
commit
b48ba96106
@ -114,7 +114,7 @@ module Encrypted_signer : SIGNER = struct
|
||||
let key = Crypto_box.Secretbox.of_bytes_exn (pbkdf ~password ~salt) in
|
||||
let msg = Data_encoding.Binary.to_bytes Signature.Secret_key.encoding sk in
|
||||
let encrypted_passwd = Crypto_box.Secretbox.box key msg nonce in
|
||||
let payload = MBytes.(to_string (concat salt encrypted_passwd)) in
|
||||
let payload = MBytes.(to_string (concat "" [salt; encrypted_passwd])) in
|
||||
let location = Base58.safe_encode payload in
|
||||
Hashtbl.replace decrypted_sks location sk ;
|
||||
return (Secret_key_locator.create ~scheme ~location)
|
||||
|
@ -15,7 +15,7 @@ let name = "Chain_id"
|
||||
let title = "Network identifier"
|
||||
|
||||
let extract bh =
|
||||
MBytes.substring (Block_hash.to_bytes bh) 0 4
|
||||
MBytes.sub_string (Block_hash.to_bytes bh) 0 4
|
||||
let hash_bytes ?key l = extract (Block_hash.hash_bytes ?key l)
|
||||
let hash_string ?key l = extract (Block_hash.hash_string ?key l)
|
||||
|
||||
|
@ -51,7 +51,7 @@ let random_keypair () =
|
||||
let pk, sk = Box.keypair () in
|
||||
sk, pk, hash pk
|
||||
|
||||
let zero_nonce = Tweetnacl.Nonce.(of_bytes_exn (MBytes.init bytes '\x00'))
|
||||
let zero_nonce = Tweetnacl.Nonce.(of_bytes_exn (MBytes.make bytes '\x00'))
|
||||
let random_nonce = Nonce.gen
|
||||
let increment_nonce = Nonce.increment
|
||||
|
||||
|
@ -258,7 +258,7 @@ include Helpers.MakeEncoder(struct
|
||||
|
||||
let pp ppf t = Format.fprintf ppf "%s" (to_b58check t)
|
||||
|
||||
let zero = MBytes.init size '\000'
|
||||
let zero = MBytes.make size '\000'
|
||||
|
||||
let sign key msg = Sign.detached ~key msg
|
||||
|
||||
|
@ -237,7 +237,7 @@ include Helpers.MakeEncoder(struct
|
||||
|
||||
let pp ppf t = Format.fprintf ppf "%s" (to_b58check t)
|
||||
|
||||
let zero = of_bytes_exn (MBytes.init size '\000')
|
||||
let zero = of_bytes_exn (MBytes.make size '\000')
|
||||
|
||||
let sign sk msg =
|
||||
Sign.sign_exn context ~sk msg
|
||||
|
@ -462,10 +462,10 @@ let check public_key signature message =
|
||||
| Public_key.Secp256k1 _, Ed25519 _ -> false
|
||||
|
||||
let append sk msg =
|
||||
MBytes.concat msg (to_bytes (sign sk msg))
|
||||
MBytes.concat "" [msg; (to_bytes (sign sk msg))]
|
||||
|
||||
let concat msg signature =
|
||||
MBytes.concat msg (to_bytes signature)
|
||||
MBytes.concat "" [msg; (to_bytes signature)]
|
||||
|
||||
type algo =
|
||||
| Ed25519
|
||||
|
@ -229,12 +229,12 @@ module Writer = struct
|
||||
|
||||
let fixed_kind_string length s buf ofs =
|
||||
if String.length s <> length then invalid_arg "fixed_kind_string";
|
||||
MBytes.blit_from_string s 0 buf ofs length;
|
||||
MBytes.blit_of_string s 0 buf ofs length;
|
||||
ofs + length
|
||||
|
||||
let variable_length_string s buf ofs =
|
||||
let length = String.length s in
|
||||
MBytes.blit_from_string s 0 buf ofs length ;
|
||||
MBytes.blit_of_string s 0 buf ofs length ;
|
||||
ofs + length
|
||||
|
||||
let objs w1 w2 (v1,v2) buf ofs =
|
||||
@ -651,7 +651,7 @@ module Reader = struct
|
||||
ofs + length, s
|
||||
|
||||
let fixed_length_string length buf ofs _len =
|
||||
let s = MBytes.substring buf ofs length in
|
||||
let s = MBytes.sub_string buf ofs length in
|
||||
ofs + length, s
|
||||
|
||||
let seq r1 r2 buf ofs len =
|
||||
|
@ -139,7 +139,7 @@ let fixed_length_bytes length buf =
|
||||
generic_read_data length MBytes.sub buf
|
||||
|
||||
let fixed_length_string length buf =
|
||||
generic_read_data length MBytes.substring buf
|
||||
generic_read_data length MBytes.sub_string buf
|
||||
|
||||
let read_tag = function
|
||||
| `Uint8 -> uint8
|
||||
|
@ -35,7 +35,7 @@ module Crypto = struct
|
||||
let msglen = MBytes.length msg in
|
||||
fail_unless
|
||||
(msglen <= max_content_length) P2p_errors.Invalid_message_size >>=? fun () ->
|
||||
let buf = MBytes.init (msglen + Crypto_box.zerobytes) '\x00' in
|
||||
let buf = MBytes.make (msglen + Crypto_box.zerobytes) '\x00' in
|
||||
MBytes.blit msg 0 buf Crypto_box.zerobytes msglen ;
|
||||
let local_nonce = cryptobox_data.local_nonce in
|
||||
cryptobox_data.local_nonce <- Crypto_box.increment_nonce local_nonce ;
|
||||
@ -52,7 +52,7 @@ module Crypto = struct
|
||||
let header_buf = MBytes.create header_length in
|
||||
P2p_io_scheduler.read_full ~len:header_length fd header_buf >>=? fun () ->
|
||||
let encrypted_length = MBytes.get_uint16 header_buf 0 in
|
||||
let buf = MBytes.init (encrypted_length + Crypto_box.boxzerobytes) '\x00' in
|
||||
let buf = MBytes.make (encrypted_length + Crypto_box.boxzerobytes) '\x00' in
|
||||
P2p_io_scheduler.read_full
|
||||
~pos:Crypto_box.boxzerobytes ~len:encrypted_length fd buf >>=? fun () ->
|
||||
let remote_nonce = cryptobox_data.remote_nonce in
|
||||
|
@ -20,14 +20,11 @@ val sub: t -> int -> int -> t
|
||||
and of length [len]. No copying of elements is involved: the
|
||||
sub-array and the original array share the same storage space. *)
|
||||
|
||||
val shift: t -> int -> t
|
||||
(** [shift src ofs] is equivalent to [sub src ofs (length src - ofs)] *)
|
||||
|
||||
val blit: t -> int -> t -> int -> int -> unit
|
||||
(** [blit src ofs_src dst ofs_dst len] copy [len] bytes from [src]
|
||||
starting at [ofs_src] into [dst] starting at [ofs_dst]. *)
|
||||
|
||||
val blit_from_string: string -> int -> t -> int -> int -> unit
|
||||
val blit_of_string: string -> int -> t -> int -> int -> unit
|
||||
(** See [blit] *)
|
||||
|
||||
val blit_to_bytes: t -> int -> bytes -> int -> int -> unit
|
||||
@ -39,8 +36,8 @@ val of_string: string -> t
|
||||
val to_string: t -> string
|
||||
(** [to_string b] dump the array content in a [string]. *)
|
||||
|
||||
val substring: t -> int -> int -> string
|
||||
(** [substring b ofs len] is equivalent to [to_string (sub b ofs len)]. *)
|
||||
val sub_string: t -> int -> int -> string
|
||||
(** [sub_string b ofs len] is equivalent to [to_string (sub b ofs len)]. *)
|
||||
|
||||
|
||||
|
||||
@ -129,7 +126,7 @@ val (>=) : t -> t -> bool
|
||||
val (>) : t -> t -> bool
|
||||
val compare : t -> t -> int
|
||||
|
||||
val concat: t -> t -> t
|
||||
val concat: string -> t list -> t
|
||||
|
||||
val to_hex: t -> [ `Hex of string ]
|
||||
val of_hex: [ `Hex of string ] -> t
|
||||
|
@ -4,6 +4,8 @@
|
||||
((name tezos_stdlib)
|
||||
(public_name tezos-stdlib)
|
||||
(libraries (ocplib-endian.bigstring
|
||||
bigstring
|
||||
cstruct
|
||||
hex
|
||||
re
|
||||
zarith
|
||||
|
@ -7,97 +7,23 @@
|
||||
(* *)
|
||||
(**************************************************************************)
|
||||
|
||||
open Bigarray
|
||||
|
||||
type t = (char, int8_unsigned_elt, c_layout) Array1.t
|
||||
|
||||
let create sz = Array1.create char c_layout sz
|
||||
let length = Array1.dim
|
||||
let sub = Array1.sub
|
||||
let shift ba off = sub ba off (length ba - off)
|
||||
let blit src srcoff dst dstoff len =
|
||||
Array1.blit (sub src srcoff len) (sub dst dstoff len)
|
||||
let copy ba =
|
||||
let ba' = create (Array1.dim ba) in
|
||||
Array1.blit ba ba';
|
||||
ba'
|
||||
let fill = Array1.fill
|
||||
|
||||
let init sz v =
|
||||
let b = create sz in
|
||||
fill b v ;
|
||||
b
|
||||
|
||||
(** Adapted from ocaml-cstruct. *)
|
||||
|
||||
external unsafe_blit_string_to_bigstring
|
||||
: string -> int -> t -> int -> int -> unit
|
||||
= "caml_blit_string_to_bigstring" [@@noalloc]
|
||||
|
||||
external unsafe_blit_bigstring_to_bytes
|
||||
: t -> int -> bytes -> int -> int -> unit
|
||||
= "caml_blit_bigstring_to_string" [@@noalloc]
|
||||
|
||||
(** HACK: force Cstruct at link which provides the previous primitives. *)
|
||||
let _dummy = Cstruct.byte_to_int
|
||||
|
||||
let invalid_bounds j l =
|
||||
Printf.sprintf "invalid bounds (index %d, length %d)" j l
|
||||
|
||||
let blit_from_string src srcoff dst dstoff len =
|
||||
if len < 0 || srcoff < 0 || dstoff < 0 || String.length src - srcoff < len then
|
||||
raise (Invalid_argument (invalid_bounds srcoff len));
|
||||
if length dst - dstoff < len then
|
||||
raise (Invalid_argument (invalid_bounds dstoff len));
|
||||
unsafe_blit_string_to_bigstring src srcoff dst dstoff len
|
||||
|
||||
let blit_to_bytes src srcoff dst dstoff len =
|
||||
if len < 0 || srcoff < 0 || dstoff < 0 || length src - srcoff < len then
|
||||
raise (Invalid_argument (invalid_bounds srcoff len));
|
||||
if Bytes.length dst - dstoff < len then
|
||||
raise (Invalid_argument (invalid_bounds dstoff len));
|
||||
unsafe_blit_bigstring_to_bytes src srcoff dst dstoff len
|
||||
|
||||
let to_string buf =
|
||||
let sz = length buf in
|
||||
let s = Bytes.create sz in
|
||||
unsafe_blit_bigstring_to_bytes buf 0 s 0 sz;
|
||||
Bytes.unsafe_to_string s
|
||||
|
||||
let of_string buf =
|
||||
let buflen = String.length buf in
|
||||
let c = create buflen in
|
||||
unsafe_blit_string_to_bigstring buf 0 c 0 buflen;
|
||||
c
|
||||
|
||||
let to_hex s = Hex.of_cstruct (Cstruct.of_bigarray s)
|
||||
let of_hex s = Cstruct.to_bigarray (Hex.to_cstruct s)
|
||||
|
||||
let substring src srcoff len =
|
||||
if len < 0 || srcoff < 0 || length src - srcoff < len then
|
||||
raise (Invalid_argument (invalid_bounds srcoff len));
|
||||
let s = Bytes.create len in
|
||||
unsafe_blit_bigstring_to_bytes src srcoff s 0 len;
|
||||
Bytes.unsafe_to_string s
|
||||
include Bigstring
|
||||
|
||||
include EndianBigstring.BigEndian
|
||||
module LE = EndianBigstring.LittleEndian
|
||||
|
||||
include Compare.Make(struct
|
||||
type nonrec t = t
|
||||
let compare = Pervasives.compare
|
||||
end)
|
||||
|
||||
module LE = struct
|
||||
include EndianBigstring.LittleEndian
|
||||
end
|
||||
let make sz c =
|
||||
let buf = create sz in
|
||||
fill buf c ;
|
||||
buf
|
||||
|
||||
let concat b1 b2 =
|
||||
let l1 = length b1 in
|
||||
let l2 = length b2 in
|
||||
let b = create (l1 + l2) in
|
||||
blit b1 0 b 0 l1 ;
|
||||
blit b2 0 b l1 l2 ;
|
||||
b
|
||||
let to_hex t =
|
||||
Hex.of_cstruct (Cstruct.of_bigarray t)
|
||||
|
||||
let pp_hex ppf t =
|
||||
let `Hex s = to_hex t in
|
||||
Format.pp_print_string ppf s
|
||||
let of_hex hex =
|
||||
Cstruct.to_bigarray (Hex.to_cstruct hex)
|
||||
|
@ -12,143 +12,12 @@
|
||||
Default layout for numeric operations is big-endian.
|
||||
Little-endian operations in the LE submodule. **)
|
||||
|
||||
open Bigarray
|
||||
|
||||
(** Arrays are of characters, represented as uint8's, in row-major layout. *)
|
||||
type t = (char, int8_unsigned_elt, c_layout) Array1.t
|
||||
include module type of Bigstring
|
||||
include Compare.S with type t := t
|
||||
|
||||
val create: int -> t
|
||||
(** [create n] allocates and returns an array of size [n] **)
|
||||
include EndianBigstring.EndianBigstringSig
|
||||
module LE : EndianBigstring.EndianBigstringSig
|
||||
|
||||
val init: int -> char -> t
|
||||
(** [init n c] allocates and returns an array of size [n] initialized
|
||||
with [c]. *)
|
||||
|
||||
val length: t -> int
|
||||
|
||||
val copy: t -> t
|
||||
val fill: t -> char -> unit
|
||||
|
||||
val sub: t -> int -> int -> t
|
||||
(** [sub src ofs len] extract a sub-array of [src] starting at [ofs]
|
||||
and of length [len]. No copying of elements is involved: the
|
||||
sub-array and the original array share the same storage space. *)
|
||||
|
||||
val shift: t -> int -> t
|
||||
(** [shift src ofs] is equivalent to [sub src ofs (length src - ofs)] *)
|
||||
|
||||
val blit: t -> int -> t -> int -> int -> unit
|
||||
(** [blit src ofs_src dst ofs_dst len] copy [len] bytes from [src]
|
||||
starting at [ofs_src] into [dst] starting at [ofs_dst]. *)
|
||||
|
||||
val blit_from_string: string -> int -> t -> int -> int -> unit
|
||||
(** See [blit] *)
|
||||
|
||||
val blit_to_bytes: t -> int -> bytes -> int -> int -> unit
|
||||
(** See [blit] *)
|
||||
|
||||
val of_string: string -> t
|
||||
(** [of_string s] create an byte array filled with the same content than [s]. *)
|
||||
|
||||
val to_string: t -> string
|
||||
(** [to_string b] dump the array content in a [string]. *)
|
||||
|
||||
val substring: t -> int -> int -> string
|
||||
(** [substring b ofs len] is equivalent to [to_string (sub b ofs len)]. *)
|
||||
|
||||
(** Functions reading and writing bytes *)
|
||||
|
||||
val get_char: t -> int -> char
|
||||
(** [get_char buff i] reads 1 byte at offset i as a char *)
|
||||
|
||||
val get_uint8: t -> int -> int
|
||||
(** [get_uint8 buff i] reads 1 byte at offset i as an unsigned int of 8
|
||||
bits. i.e. It returns a value between 0 and 2^8-1 *)
|
||||
|
||||
val get_int8: t -> int -> int
|
||||
(** [get_int8 buff i] reads 1 byte at offset i as a signed int of 8
|
||||
bits. i.e. It returns a value between -2^7 and 2^7-1 *)
|
||||
|
||||
val set_char: t -> int -> char -> unit
|
||||
(** [set_char buff i v] writes [v] to [buff] at offset [i] *)
|
||||
|
||||
val set_int8: t -> int -> int -> unit
|
||||
(** [set_int8 buff i v] writes the least significant 8 bits of [v]
|
||||
to [buff] at offset [i] *)
|
||||
|
||||
(** Functions reading according to Big Endian byte order *)
|
||||
|
||||
val get_uint16: t -> int -> int
|
||||
(** [get_uint16 buff i] reads 2 bytes at offset i as an unsigned int
|
||||
of 16 bits. i.e. It returns a value between 0 and 2^16-1 *)
|
||||
|
||||
val get_int16: t -> int -> int
|
||||
(** [get_int16 buff i] reads 2 byte at offset i as a signed int of
|
||||
16 bits. i.e. It returns a value between -2^15 and 2^15-1 *)
|
||||
|
||||
val get_int32: t -> int -> int32
|
||||
(** [get_int32 buff i] reads 4 bytes at offset i as an int32. *)
|
||||
|
||||
val get_int64: t -> int -> int64
|
||||
(** [get_int64 buff i] reads 8 bytes at offset i as an int64. *)
|
||||
|
||||
val get_float: t -> int -> float
|
||||
(** [get_float buff i] reads 4 bytes at offset i as an IEEE754 float. *)
|
||||
|
||||
val get_double: t -> int -> float
|
||||
(** [get_float buff i] reads 8 bytes at offset i as an IEEE754 double. *)
|
||||
|
||||
val set_int16: t -> int -> int -> unit
|
||||
(** [set_int16 buff i v] writes the least significant 16 bits of [v]
|
||||
to [buff] at offset [i] *)
|
||||
|
||||
val set_int32: t -> int -> int32 -> unit
|
||||
(** [set_int32 buff i v] writes [v] to [buff] at offset [i] *)
|
||||
|
||||
val set_int64: t -> int -> int64 -> unit
|
||||
(** [set_int64 buff i v] writes [v] to [buff] at offset [i] *)
|
||||
|
||||
val set_float: t -> int -> float -> unit
|
||||
(** [set_float buff i v] writes [v] to [buff] at offset [i] *)
|
||||
|
||||
val set_double: t -> int -> float -> unit
|
||||
(** [set_double buff i v] writes [v] to [buff] at offset [i] *)
|
||||
|
||||
module LE: sig
|
||||
|
||||
(** Functions reading according to Little Endian byte order *)
|
||||
|
||||
val get_uint16: t -> int -> int
|
||||
(** [get_uint16 buff i] reads 2 bytes at offset i as an unsigned int
|
||||
of 16 bits. i.e. It returns a value between 0 and 2^16-1 *)
|
||||
|
||||
val get_int16: t -> int -> int
|
||||
(** [get_int16 buff i] reads 2 byte at offset i as a signed int of
|
||||
16 bits. i.e. It returns a value between -2^15 and 2^15-1 *)
|
||||
|
||||
val get_int32: t -> int -> int32
|
||||
(** [get_int32 buff i] reads 4 bytes at offset i as an int32. *)
|
||||
|
||||
val get_int64: t -> int -> int64
|
||||
(** [get_int64 buff i] reads 8 bytes at offset i as an int64. *)
|
||||
|
||||
val set_int16: t -> int -> int -> unit
|
||||
(** [set_int16 buff i v] writes the least significant 16 bits of [v]
|
||||
to [buff] at offset [i] *)
|
||||
|
||||
val set_int32: t -> int -> int32 -> unit
|
||||
(** [set_int32 buff i v] writes [v] to [buff] at offset [i] *)
|
||||
|
||||
val set_int64: t -> int -> int64 -> unit
|
||||
(** [set_int64 buff i v] writes [v] to [buff] at offset [i] *)
|
||||
|
||||
end
|
||||
|
||||
val concat: t -> t -> t
|
||||
(** Returns a new array with adjacent copies of the two input arrays **)
|
||||
|
||||
val to_hex: t -> Hex.t
|
||||
val of_hex: Hex.t -> t
|
||||
|
||||
val pp_hex: Format.formatter -> t -> unit
|
||||
val make : int -> char -> t
|
||||
val of_hex : Hex.t -> t
|
||||
val to_hex : t -> Hex.t
|
||||
|
@ -71,7 +71,7 @@ let write_double buf float =
|
||||
let write_string_data buf str =
|
||||
let len = String.length str in
|
||||
resize_if_necessary buf len ;
|
||||
MBytes.blit_from_string str 0 buf.buffer buf.offset len ;
|
||||
MBytes.blit_of_string str 0 buf.buffer buf.offset len ;
|
||||
buf.offset <- buf.offset + len
|
||||
|
||||
let write_sized buffer (writer : unit -> unit) =
|
||||
|
@ -19,7 +19,7 @@ let zero = MBytes.of_string (String.make size '\000')
|
||||
let to_b58check s =
|
||||
Ed25519.Public_key_hash.to_b58check
|
||||
(Ed25519.Public_key_hash.of_bytes_exn
|
||||
(MBytes.concat s zero))
|
||||
(MBytes.concat "" [s; zero]))
|
||||
|
||||
let of_b58check_exn s =
|
||||
let pkh = Ed25519.Public_key_hash.of_b58check_exn s in
|
||||
|
Loading…
Reference in New Issue
Block a user