package octez-libs
A package that contains multiple base libraries used by the Octez suite
Install
Dune Dependency
Authors
Maintainers
Sources
tezos-octez-v20.1.tag.bz2
sha256=ddfb5076eeb0b32ac21c1eed44e8fc86a6743ef18ab23fff02d36e365bb73d61
sha512=d22a827df5146e0aa274df48bc2150b098177ff7e5eab52c6109e867eb0a1f0ec63e6bfbb0e3645a6c2112de3877c91a17df32ccbff301891ce4ba630c997a65
doc/src/octez-libs.crypto/crypto_box.ml.html
Source file crypto_box.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
(*****************************************************************************) (* *) (* Open Source License *) (* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) (* to deal in the Software without restriction, including without limitation *) (* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) (* and/or sell copies of the Software, and to permit persons to whom the *) (* Software is furnished to do so, subject to the following conditions: *) (* *) (* The above copyright notice and this permission notice shall be included *) (* in all copies or substantial portions of the Software. *) (* *) (* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) (* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) (* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) (* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) (* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) (* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) (* DEALINGS IN THE SOFTWARE. *) (* *) (*****************************************************************************) (** Tezos - X25519/XSalsa20-Poly1305 cryptography *) open Hacl type secret_key = secret Box.key type public_key = public Box.key type channel_key = Box.combined Box.key type nonce = Bytes.t type pow_target = Z.t module Secretbox = struct include Hacl.Secretbox let secretbox key msg nonce = let msglen = Bytes.length msg in let cmsg = Bytes.create (msglen + tagbytes) in secretbox ~key ~nonce ~msg ~cmsg ; cmsg let secretbox_open key cmsg nonce = let cmsglen = Bytes.length cmsg in let msg = Bytes.create (cmsglen - tagbytes) in match secretbox_open ~key ~nonce ~cmsg ~msg with | false -> None | true -> Some msg end module Public_key_hash = Blake2B.Make (Base58) (struct let name = "Crypto_box.Public_key_hash" let title = "A Cryptobox public key ID" let b58check_prefix = Base58.Prefix.cryptobox_public_key_hash let size = Some 16 end) 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] let tag_length = Box.tagbytes let random_keypair () = let pk, sk = Box.keypair () in (sk, pk, hash pk) let zero_nonce = Bytes.make Nonce.size '\x00' let random_nonce = Nonce.gen let increment_nonce = Nonce.increment let generate_nonce bytes_list = let hash = Blake2B.hash_bytes bytes_list in let s = Blake2B.to_bytes hash in Nonce.of_bytes_exn @@ Bytes.sub s 0 Nonce.size let init_to_resp_seed = Bytes.of_string "Init -> Resp" let resp_to_init_seed = Bytes.of_string "Resp -> Init" let generate_nonces ~incoming ~sent_msg ~recv_msg = let (init_msg, resp_msg, false | resp_msg, init_msg, true) = (sent_msg, recv_msg, incoming) in let nonce_init_to_resp = generate_nonce [init_msg; resp_msg; init_to_resp_seed] in let nonce_resp_to_init = generate_nonce [init_msg; resp_msg; resp_to_init_seed] in if incoming then (nonce_init_to_resp, nonce_resp_to_init) else (nonce_resp_to_init, nonce_init_to_resp) let precompute sk pk = Box.dh pk sk let fast_box_noalloc k nonce tag buf = Box.box_noalloc ~k ~nonce ~buf ~tag let fast_box_open_noalloc k nonce tag buf = Box.box_open_noalloc ~k ~nonce ~buf ~tag let fast_box k nonce msg = let cmsg = Bytes.create (Bytes.length msg + tag_length) in Box.box ~k ~nonce ~msg ~cmsg ; cmsg let fast_box_open k nonce cmsg = let cmsglen = Bytes.length cmsg in assert (cmsglen >= tag_length) ; let msg = Bytes.create (cmsglen - tag_length) in if Box.box_open ~k ~nonce ~cmsg ~msg then Some msg else None let compare_pow_target hash pow_target = let hash = Z.of_bits (Blake2B.to_string hash) in Z.compare hash pow_target <= 0 let make_pow_target f = if f < 0. || 256. < f then invalid_arg "Cryptobox.target_of_float" ; let frac, shift = modf f in let shift = int_of_float shift in let m = Z.of_int64 @@ if frac = 0. then Int64.(pred (shift_left 1L 54)) else Int64.of_float (2. ** (54. -. frac)) in if shift < 202 then Z.logor (Z.shift_left m (202 - shift)) (Z.pred @@ Z.shift_left Z.one (202 - shift)) else Z.shift_right m (shift - 202) let default_pow_target = make_pow_target 24. let target_0 = make_pow_target 0. let check_proof_of_work pk nonce pow_target = let hash = Blake2B.hash_bytes [Box.unsafe_to_bytes pk; nonce] in compare_pow_target hash pow_target (* This is the non-yielding function to generate an identity. It performs a bounded number of attempts ([n]). This function is not exported. Instead, the wrapper below, [generate_proof_of_work], uses this function repeatedly but it intersperses calls to [Lwt.pause] to yield explicitly. *) let generate_proof_of_work_n_attempts ~max:n pk pow_target = let rec loop nonce attempts = if attempts > n then raise Not_found else if check_proof_of_work pk nonce pow_target then nonce else loop (Nonce.increment nonce) (attempts + 1) in loop (random_nonce ()) 0 let generate_proof_of_work_with_target_0 pk = generate_proof_of_work_n_attempts ~max:1 pk target_0 let rec generate_proof_of_work ?(yield_every = 10000) ?max pk pow_target = let open Lwt.Syntax in match max with | None -> ( try let pow = generate_proof_of_work_n_attempts ~max:yield_every pk pow_target in Lwt.return pow with Not_found -> let* () = Lwt.pause () in generate_proof_of_work ~yield_every pk pow_target) | Some max -> ( if max <= 0 then Lwt.apply raise Not_found else let attempts = min max yield_every in try let pow = generate_proof_of_work_n_attempts ~max:attempts pk pow_target in Lwt.return pow with Not_found -> let* () = Lwt.pause () in let max = max - attempts in generate_proof_of_work ~yield_every ~max pk pow_target) let public_key_to_bytes pk = Bytes.copy (Box.unsafe_to_bytes pk) let public_key_of_bytes buf = Box.unsafe_pk_of_bytes (Bytes.copy buf) let public_key_size = Box.pkbytes let secret_key_to_bytes sk = Bytes.copy (Box.unsafe_to_bytes sk) let secret_key_of_bytes buf = Box.unsafe_sk_of_bytes (Bytes.copy buf) let secret_key_size = Box.skbytes let nonce_size = Nonce.size let public_key_encoding = let open Data_encoding in conv 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_bytes secret_key_of_bytes (Fixed.bytes secret_key_size) let nonce_encoding = 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 = Hex.pp ppf (Hex.of_bytes (public_key_to_bytes pk)) module For_testing_only = struct let generate_proof_of_work_n_attempts = generate_proof_of_work_n_attempts end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>