ligo/src/lib_crypto/rand.ml
2018-04-19 10:31:56 +02:00

22 lines
1.0 KiB
OCaml

(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2017. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
let generate = Tweetnacl.Rand.gen
let generate_into ?(pos=0) ?len buf =
let buflen = MBytes.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
Tweetnacl.Rand.write buf