From 1805a1d8167450dda41212f645f853edc960490f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Mon, 14 Nov 2016 15:55:24 +0100 Subject: [PATCH] Shell: inline `Ed25519` into `Environment` This interface is not used in the shell, only in the protocol. It is just a (documented) wrapper over a fragment of sodium. --- src/Makefile | 4 +- src/client/client_keys.ml | 2 + src/client/client_keys.mli | 1 + .../bootstrap/client_proto_context.ml | 1 + .../bootstrap/client_proto_contracts.ml | 2 + .../bootstrap/client_proto_programs.ml | 1 + .../mining/client_mining_endorsement.ml | 2 + .../bootstrap/mining/client_mining_forge.ml | 1 + .../mining/client_mining_operations.ml | 2 + src/node/updater/environment.ml | 116 ++++++++++++++- src/utils/ed25519.ml | 134 ------------------ src/utils/ed25519.mli | 49 ------- test/test_basic.ml | 2 +- 13 files changed, 129 insertions(+), 188 deletions(-) delete mode 100644 src/utils/ed25519.ml delete mode 100644 src/utils/ed25519.mli diff --git a/src/Makefile b/src/Makefile index 748caf687..78b6630db 100644 --- a/src/Makefile +++ b/src/Makefile @@ -110,7 +110,6 @@ UTILS_LIB_INTFS := \ utils/data_encoding.mli \ utils/time.mli \ utils/hash.mli \ - utils/ed25519.mli \ utils/error_monad.mli \ utils/logging.mli \ utils/lwt_utils.mli \ @@ -126,7 +125,6 @@ UTILS_LIB_IMPLS := \ utils/data_encoding.ml \ utils/time.ml \ utils/hash.ml \ - utils/ed25519.ml \ utils/error_monad_sig.ml \ utils/error_monad.ml \ utils/logging.ml \ @@ -318,7 +316,7 @@ proto/embedded_proto_%.cmxa: \ CLIENT_PROTO_INCLUDES := \ utils node/updater node/db node/net node/shell client \ - $(shell ocamlfind query lwt ocplib-json-typed) + $(shell ocamlfind query lwt ocplib-json-typed sodium) proto/client_embedded_proto_%.cmxa: \ ${TZCOMPILER} \ diff --git a/src/client/client_keys.ml b/src/client/client_keys.ml index 535f50b89..158434405 100644 --- a/src/client/client_keys.ml +++ b/src/client/client_keys.ml @@ -7,6 +7,8 @@ (* *) (**************************************************************************) +module Ed25519 = Environment.Ed25519 + module Public_key_hash = Client_aliases.Alias (struct type t = Ed25519.Public_key_hash.t let encoding = Ed25519.Public_key_hash.encoding diff --git a/src/client/client_keys.mli b/src/client/client_keys.mli index 989458f21..757df170c 100644 --- a/src/client/client_keys.mli +++ b/src/client/client_keys.mli @@ -7,6 +7,7 @@ (* *) (**************************************************************************) +module Ed25519 = Environment.Ed25519 module Public_key_hash : Client_aliases.Alias with type t = Ed25519.Public_key_hash.t diff --git a/src/client/embedded/bootstrap/client_proto_context.ml b/src/client/embedded/bootstrap/client_proto_context.ml index 893fd2534..ecffdecfb 100644 --- a/src/client/embedded/bootstrap/client_proto_context.ml +++ b/src/client/embedded/bootstrap/client_proto_context.ml @@ -11,6 +11,7 @@ open Client_proto_args open Client_proto_contracts open Client_proto_programs open Client_keys +module Ed25519 = Environment.Ed25519 let handle_error f () = f () >>= Client_proto_rpcs.handle_error diff --git a/src/client/embedded/bootstrap/client_proto_contracts.ml b/src/client/embedded/bootstrap/client_proto_contracts.ml index 2ad6b6fb1..9a921f594 100644 --- a/src/client/embedded/bootstrap/client_proto_contracts.ml +++ b/src/client/embedded/bootstrap/client_proto_contracts.ml @@ -7,6 +7,8 @@ (* *) (**************************************************************************) +module Ed25519 = Environment.Ed25519 + module RawContractAlias = Client_aliases.Alias (struct type t = Contract.t let encoding = Contract.encoding diff --git a/src/client/embedded/bootstrap/client_proto_programs.ml b/src/client/embedded/bootstrap/client_proto_programs.ml index 341cb5b8c..1ff02fa84 100644 --- a/src/client/embedded/bootstrap/client_proto_programs.ml +++ b/src/client/embedded/bootstrap/client_proto_programs.ml @@ -7,6 +7,7 @@ (* *) (**************************************************************************) +module Ed25519 = Environment.Ed25519 open Client_proto_args let report_parse_error _prefix exn _lexbuf = diff --git a/src/client/embedded/bootstrap/mining/client_mining_endorsement.ml b/src/client/embedded/bootstrap/mining/client_mining_endorsement.ml index b6478a31d..ca31a6219 100644 --- a/src/client/embedded/bootstrap/mining/client_mining_endorsement.ml +++ b/src/client/embedded/bootstrap/mining/client_mining_endorsement.ml @@ -10,6 +10,8 @@ open Logging.Client.Endorsement open Cli_entries +module Ed25519 = Environment.Ed25519 + module State : sig val get_endorsement: diff --git a/src/client/embedded/bootstrap/mining/client_mining_forge.ml b/src/client/embedded/bootstrap/mining/client_mining_forge.ml index 15f73d4c4..fb7a274dd 100644 --- a/src/client/embedded/bootstrap/mining/client_mining_forge.ml +++ b/src/client/embedded/bootstrap/mining/client_mining_forge.ml @@ -8,6 +8,7 @@ (**************************************************************************) open Logging.Client.Mining +module Ed25519 = Environment.Ed25519 let generate_proof_of_work_nonce () = Sodium.Random.Bigbytes.generate Constants.proof_of_work_nonce_size diff --git a/src/client/embedded/bootstrap/mining/client_mining_operations.ml b/src/client/embedded/bootstrap/mining/client_mining_operations.ml index 6df2bde39..bd95edab5 100644 --- a/src/client/embedded/bootstrap/mining/client_mining_operations.ml +++ b/src/client/embedded/bootstrap/mining/client_mining_operations.ml @@ -7,6 +7,8 @@ (* *) (**************************************************************************) +module Ed25519 = Environment.Ed25519 + open Logging.Client.Mining open Operation diff --git a/src/node/updater/environment.ml b/src/node/updater/environment.ml index 351a7bec3..a1bc9d8ab 100644 --- a/src/node/updater/environment.ml +++ b/src/node/updater/environment.ml @@ -31,7 +31,121 @@ module Data_encoding = Data_encoding module Time = Time module Base48 = Base48 module Hash = Hash -module Ed25519 = Ed25519 +module Ed25519 = struct + + type secret_key = Sodium.Sign.secret_key + type public_key = Sodium.Sign.public_key + type signature = MBytes.t + + let sign key msg = + Sodium.Sign.Bigbytes.(of_signature @@ sign_detached key msg) + + let check_signature public_key signature msg = + try + Sodium.Sign.Bigbytes.(verify public_key (to_signature signature) msg) ; + true + with _ -> false + + let append_signature key msg = + MBytes.concat msg (sign key msg) + + module Public_key_hash = Hash.Make_SHA256(Base48)(struct + let name = "Ed25519.Public_key_hash" + let title = "An Ed25519 public key ID" + let b48check_prefix = Base48.Prefix.ed25519_public_key_hash + end) + + let hash v = + Public_key_hash.hash_bytes + [ Sodium.Sign.Bigbytes.of_public_key v ] + + let generate_key () = + let secret, pub = Sodium.Sign.random_keypair () in + (hash pub, pub, secret) + + type Base48.data += + | Public_key of public_key + | Secret_key of secret_key + | Signature of signature + + let b48check_public_key_encoding = + Base48.register_encoding + ~prefix: Base48.Prefix.ed25519_public_key + ~to_raw:(fun x -> Bytes.to_string (Sodium.Sign.Bytes.of_public_key x)) + ~of_raw:(fun x -> Sodium.Sign.Bytes.to_public_key (Bytes.of_string x)) + ~wrap:(fun x -> Public_key x) + + let b48check_secret_key_encoding = + Base48.register_encoding + ~prefix: Base48.Prefix.ed25519_secret_key + ~to_raw:(fun x -> Bytes.to_string (Sodium.Sign.Bytes.of_secret_key x)) + ~of_raw:(fun x -> Sodium.Sign.Bytes.to_secret_key (Bytes.of_string x)) + ~wrap:(fun x -> Secret_key x) + + let b48check_signature_encoding = + Base48.register_encoding + ~prefix: Base48.Prefix.ed25519_signature + ~to_raw:MBytes.to_string + ~of_raw:MBytes.of_string + ~wrap:(fun x -> Signature x) + + let public_key_encoding = + let open Data_encoding in + splitted + ~json: + (describe + ~title: "An Ed25519 public key (Base48Check encoded)" @@ + conv + (fun s -> Base48.simple_encode b48check_public_key_encoding s) + (fun s -> + match Base48.simple_decode b48check_public_key_encoding s with + | Some x -> x + | None -> Data_encoding.Json.cannot_destruct + "Ed25519 public key: unexpected prefix.") + string) + ~binary: + (conv + Sodium.Sign.Bigbytes.of_public_key + Sodium.Sign.Bigbytes.to_public_key + bytes) + + let secret_key_encoding = + let open Data_encoding in + splitted + ~json: + (describe + ~title: "An Ed25519 secret key (Base48Check encoded)" @@ + conv + (fun s -> Base48.simple_encode b48check_secret_key_encoding s) + (fun s -> + match Base48.simple_decode b48check_secret_key_encoding s with + | Some x -> x + | None -> Data_encoding.Json.cannot_destruct + "Ed25519 secret key: unexpected prefix.") + string) + ~binary: + (conv + Sodium.Sign.Bigbytes.of_secret_key + Sodium.Sign.Bigbytes.to_secret_key + bytes) + + let signature_encoding = + let open Data_encoding in + splitted + ~json: + (describe + ~title: "An Ed25519 signature (Base48Check encoded)" @@ + conv + (fun s -> Base48.simple_encode b48check_signature_encoding s) + (fun s -> + match Base48.simple_decode b48check_signature_encoding s with + | Some x -> x + | None -> Data_encoding.Json.cannot_destruct + "Ed25519 signature: unexpected prefix.") + string) + ~binary: (Fixed.bytes 64) + +end module Persist = Persist module Context = Context module RPC = RPC diff --git a/src/utils/ed25519.ml b/src/utils/ed25519.ml deleted file mode 100644 index 2b04095ed..000000000 --- a/src/utils/ed25519.ml +++ /dev/null @@ -1,134 +0,0 @@ -(**************************************************************************) -(* *) -(* Copyright (c) 2014 - 2016. *) -(* Dynamic Ledger Solutions, Inc. *) -(* *) -(* All rights reserved. No warranty, explicit or implicit, provided. *) -(* *) -(**************************************************************************) - -(* Tezos - Ed25519 cryptography (simple interface to Sodium) *) - -(*-- Signature ---------------------------------------------------------------*) - -type secret_key = Sodium.Sign.secret_key -type public_key = Sodium.Sign.public_key -type signature = MBytes.t - -let sign key msg = - Sodium.Sign.Bigbytes.(of_signature @@ sign_detached key msg) - -let check_signature public_key signature msg = - try Sodium.Sign.Bigbytes.(verify public_key (to_signature signature) msg) ; true - with _ -> false - -let append_signature key msg = - MBytes.concat msg (sign key msg) - -(*-- Hashed public keys for user ID ------------------------------------------*) - -module Public_key_hash = Hash.Make_SHA256(struct - let name = "Ed25519.Public_key_hash" - let title = "An Ed25519 public key ID" - let prefix = Some Base48.Prefix.public_key_hash - end) - -type public_key_hash = Public_key_hash.t - -let hash v = - Public_key_hash.hash_bytes - [ Sodium.Sign.Bigbytes.of_public_key v ] - -let hash_path = Public_key_hash.to_path -let hash_hex = Public_key_hash.to_hex -let equal_hash = Public_key_hash.equal -let compare_hash = Public_key_hash.compare - -let generate_key () = - let secret, pub = Sodium.Sign.random_keypair () in - (hash pub, pub, secret) - -(*-- JSON Serializers --------------------------------------------------------*) - -type Base48.data += - | Public_key of public_key - | Secret_key of secret_key - | Signature of signature - -let _ = - Base48.register - ~prefix:Base48.Prefix.public_key - ~read:(function Public_key x -> Some (Bytes.to_string (Sodium.Sign.Bytes.of_public_key x)) | _ -> None) - ~build:(fun x -> Public_key (Sodium.Sign.Bytes.to_public_key (Bytes.of_string x))) - -let _ = - Base48.register - ~prefix:Base48.Prefix.secret_key - ~read:(function Secret_key x -> Some (Bytes.to_string (Sodium.Sign.Bytes.of_secret_key x)) | _ -> None) - ~build:(fun x -> Secret_key (Sodium.Sign.Bytes.to_secret_key (Bytes.of_string x))) - -let _ = - Base48.register - ~prefix:Base48.Prefix.signature - ~read:(function Signature x -> Some (MBytes.to_string x) | _ -> None) - ~build:(fun x -> Signature (MBytes.of_string x)) - -let public_key_hash_encoding = - Public_key_hash.encoding - -let public_key_encoding = - let open Data_encoding in - splitted - ~json: - (describe - ~title: "An Ed25519 public key (Base48Check encoded)" @@ - conv - (fun s -> Base48.encode (Public_key s)) - (fun s -> - match Base48.decode s with - | Public_key x -> x - | _ -> Data_encoding.Json.cannot_destruct - "Ed25519 public key: unexpected prefix.") - string) - ~binary: - (conv - Sodium.Sign.Bigbytes.of_public_key - Sodium.Sign.Bigbytes.to_public_key - bytes) - -let secret_key_encoding = - let open Data_encoding in - splitted - ~json: - (describe - ~title: "An Ed25519 secret key (Base48Check encoded)" @@ - conv - (fun s -> Base48.encode (Secret_key s)) - (fun s -> - match Base48.decode s with - | Secret_key x -> x - | _ -> Data_encoding.Json.cannot_destruct - "Ed25519 secret key: unexpected prefix.") - string) - ~binary: - (conv - Sodium.Sign.Bigbytes.of_secret_key - Sodium.Sign.Bigbytes.to_secret_key - bytes) - -let signature_encoding = - let open Data_encoding in - splitted - ~json: - (describe - ~title: "An Ed25519 signature (Base48Check encoded)" @@ - conv - (fun s -> Base48.encode (Signature s)) - (fun s -> - match Base48.decode s with - | Signature x -> x - | _ -> - Data_encoding.Json.cannot_destruct - "Ed25519 signature: unexpected prefix.") - string) - ~binary: (Fixed.bytes 64) diff --git a/src/utils/ed25519.mli b/src/utils/ed25519.mli deleted file mode 100644 index 5a9c367b5..000000000 --- a/src/utils/ed25519.mli +++ /dev/null @@ -1,49 +0,0 @@ -(**************************************************************************) -(* *) -(* Copyright (c) 2014 - 2016. *) -(* Dynamic Ledger Solutions, Inc. *) -(* *) -(* All rights reserved. No warranty, explicit or implicit, provided. *) -(* *) -(**************************************************************************) - -(** Tezos - Ed25519 cryptography *) - - -(** {2 Signature} ************************************************************) - -(** An Ed25519 public key *) -type public_key = Sodium.Sign.public_key - -(** An Ed25519 secret key *) -type secret_key = Sodium.Sign.secret_key - -(** The result of signing a sequence of bytes with a secret key *) -type signature - -(** Signs a sequence of bytes with a secret key *) -val sign : secret_key -> MBytes.t -> signature - -val append_signature : secret_key -> MBytes.t -> MBytes.t - -(** Checks a signature *) -val check_signature : public_key -> signature -> MBytes.t -> bool - -(** {2 Hashed public keys for user ID} ***************************************) - -module Public_key_hash : Hash.HASH - -(** Hashes an Ed25519 public key *) -val hash : public_key -> Public_key_hash.t - -(** {2 Serializers} **********************************************************) - -val public_key_encoding : public_key Data_encoding.t - -val secret_key_encoding : secret_key Data_encoding.t - -val signature_encoding : signature Data_encoding.t - -(** {2 Key pairs generation} *************************************************) - -val generate_key : unit -> Public_key_hash.t * public_key * secret_key diff --git a/test/test_basic.ml b/test/test_basic.ml index e55e99508..d3c80d69e 100644 --- a/test/test_basic.ml +++ b/test/test_basic.ml @@ -78,7 +78,7 @@ let bootstrap_accounts () = let create_account name = let secret_key, public_key = Sodium.Sign.random_keypair () in - let public_key_hash = Ed25519.hash public_key in + let public_key_hash = Environment.Ed25519.hash public_key in let contract = Contract.default_contract public_key_hash in Lwt.return { name ; contract ; public_key_hash ; public_key ; secret_key }