2016-11-03 22:15:31 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2016-11-16 04:19:13 +04:00
|
|
|
open Utils
|
|
|
|
|
2016-11-03 22:15:31 +04:00
|
|
|
(** Tezos - X25519/XSalsa20-Poly1305 cryptography *)
|
|
|
|
|
|
|
|
type secret_key = Sodium.Box.secret_key
|
|
|
|
type public_key = Sodium.Box.public_key
|
|
|
|
type channel_key = Sodium.Box.channel_key
|
|
|
|
type nonce = Sodium.Box.nonce
|
2016-11-25 21:03:57 +04:00
|
|
|
type target = int64 list (* used as unsigned intergers... *)
|
2016-11-18 00:02:32 +04:00
|
|
|
exception TargetNot256Bit
|
2016-11-03 22:15:31 +04:00
|
|
|
|
|
|
|
let random_keypair = Sodium.Box.random_keypair
|
|
|
|
let random_nonce = Sodium.Box.random_nonce
|
|
|
|
let increment_nonce = Sodium.Box.increment_nonce
|
|
|
|
let box = Sodium.Box.Bigbytes.box
|
2016-11-08 02:38:02 +04:00
|
|
|
let box_open sk pk msg nonce =
|
|
|
|
try Some (Sodium.Box.Bigbytes.box_open sk pk msg nonce) with
|
|
|
|
| Sodium.Verification_failure -> None
|
2016-11-16 04:19:13 +04:00
|
|
|
|
2016-11-25 21:03:57 +04:00
|
|
|
let make_target target =
|
|
|
|
if List.length target > 8 then raise TargetNot256Bit ;
|
|
|
|
target
|
2016-11-18 00:02:32 +04:00
|
|
|
|
2016-11-25 21:03:57 +04:00
|
|
|
(* Compare a SHA256 hash to a 256bits-target prefix.
|
|
|
|
The prefix is a list of "unsigned" int64. *)
|
|
|
|
let compare_target hash target =
|
2016-11-25 22:46:50 +04:00
|
|
|
let hash = Hash.Generic_hash.to_string hash in
|
2016-11-25 21:03:57 +04:00
|
|
|
let rec check offset = function
|
|
|
|
| [] -> true
|
|
|
|
| x :: xs ->
|
|
|
|
Compare.Uint64.(EndianString.BigEndian.get_int64 hash offset < x)
|
|
|
|
&& check (offset + 8) xs in
|
|
|
|
check 0 target
|
2016-11-19 02:07:27 +04:00
|
|
|
|
|
|
|
let default_target =
|
2016-11-25 21:03:57 +04:00
|
|
|
(* FIXME we use an easy target until we allow custom configuration. *)
|
|
|
|
[ Int64.shift_left 1L 48 ]
|
2016-11-18 00:02:32 +04:00
|
|
|
|
|
|
|
let check_proof_of_work pk nonce target =
|
2016-11-25 21:03:57 +04:00
|
|
|
let hash =
|
2016-11-25 22:46:50 +04:00
|
|
|
Hash.Generic_hash.hash_bytes [
|
|
|
|
Sodium.Box.Bigbytes.of_public_key pk ;
|
|
|
|
Sodium.Box.Bigbytes.of_nonce nonce ;
|
|
|
|
] in
|
2016-11-25 21:03:57 +04:00
|
|
|
compare_target hash target
|
2016-11-18 00:02:32 +04:00
|
|
|
|
|
|
|
let generate_proof_of_work pk target =
|
2016-11-09 06:18:09 +04:00
|
|
|
let rec loop nonce =
|
2016-11-18 00:02:32 +04:00
|
|
|
if check_proof_of_work pk nonce target then nonce
|
2016-11-09 06:18:09 +04:00
|
|
|
else loop (increment_nonce nonce) in
|
|
|
|
loop (random_nonce ())
|
|
|
|
|
2016-11-16 04:19:13 +04:00
|
|
|
let public_key_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
|
|
|
Sodium.Box.Bigbytes.of_public_key
|
|
|
|
Sodium.Box.Bigbytes.to_public_key
|
|
|
|
(Fixed.bytes Sodium.Box.public_key_size)
|
|
|
|
|
|
|
|
let secret_key_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
|
|
|
Sodium.Box.Bigbytes.of_secret_key
|
|
|
|
Sodium.Box.Bigbytes.to_secret_key
|
|
|
|
(Fixed.bytes Sodium.Box.secret_key_size)
|
|
|
|
|
|
|
|
let nonce_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
|
|
|
Sodium.Box.Bigbytes.of_nonce
|
|
|
|
Sodium.Box.Bigbytes.to_nonce
|
|
|
|
(Fixed.bytes Sodium.Box.nonce_size)
|