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.
This commit is contained in:
Grégoire Henry 2016-11-14 15:55:24 +01:00
parent 9062c405ad
commit 1805a1d816
13 changed files with 129 additions and 188 deletions

View File

@ -110,7 +110,6 @@ UTILS_LIB_INTFS := \
utils/data_encoding.mli \ utils/data_encoding.mli \
utils/time.mli \ utils/time.mli \
utils/hash.mli \ utils/hash.mli \
utils/ed25519.mli \
utils/error_monad.mli \ utils/error_monad.mli \
utils/logging.mli \ utils/logging.mli \
utils/lwt_utils.mli \ utils/lwt_utils.mli \
@ -126,7 +125,6 @@ UTILS_LIB_IMPLS := \
utils/data_encoding.ml \ utils/data_encoding.ml \
utils/time.ml \ utils/time.ml \
utils/hash.ml \ utils/hash.ml \
utils/ed25519.ml \
utils/error_monad_sig.ml \ utils/error_monad_sig.ml \
utils/error_monad.ml \ utils/error_monad.ml \
utils/logging.ml \ utils/logging.ml \
@ -318,7 +316,7 @@ proto/embedded_proto_%.cmxa: \
CLIENT_PROTO_INCLUDES := \ CLIENT_PROTO_INCLUDES := \
utils node/updater node/db node/net node/shell client \ 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: \ proto/client_embedded_proto_%.cmxa: \
${TZCOMPILER} \ ${TZCOMPILER} \

View File

@ -7,6 +7,8 @@
(* *) (* *)
(**************************************************************************) (**************************************************************************)
module Ed25519 = Environment.Ed25519
module Public_key_hash = Client_aliases.Alias (struct module Public_key_hash = Client_aliases.Alias (struct
type t = Ed25519.Public_key_hash.t type t = Ed25519.Public_key_hash.t
let encoding = Ed25519.Public_key_hash.encoding let encoding = Ed25519.Public_key_hash.encoding

View File

@ -7,6 +7,7 @@
(* *) (* *)
(**************************************************************************) (**************************************************************************)
module Ed25519 = Environment.Ed25519
module Public_key_hash : module Public_key_hash :
Client_aliases.Alias with type t = Ed25519.Public_key_hash.t Client_aliases.Alias with type t = Ed25519.Public_key_hash.t

View File

@ -11,6 +11,7 @@ open Client_proto_args
open Client_proto_contracts open Client_proto_contracts
open Client_proto_programs open Client_proto_programs
open Client_keys open Client_keys
module Ed25519 = Environment.Ed25519
let handle_error f () = let handle_error f () =
f () >>= Client_proto_rpcs.handle_error f () >>= Client_proto_rpcs.handle_error

View File

@ -7,6 +7,8 @@
(* *) (* *)
(**************************************************************************) (**************************************************************************)
module Ed25519 = Environment.Ed25519
module RawContractAlias = Client_aliases.Alias (struct module RawContractAlias = Client_aliases.Alias (struct
type t = Contract.t type t = Contract.t
let encoding = Contract.encoding let encoding = Contract.encoding

View File

@ -7,6 +7,7 @@
(* *) (* *)
(**************************************************************************) (**************************************************************************)
module Ed25519 = Environment.Ed25519
open Client_proto_args open Client_proto_args
let report_parse_error _prefix exn _lexbuf = let report_parse_error _prefix exn _lexbuf =

View File

@ -10,6 +10,8 @@
open Logging.Client.Endorsement open Logging.Client.Endorsement
open Cli_entries open Cli_entries
module Ed25519 = Environment.Ed25519
module State : sig module State : sig
val get_endorsement: val get_endorsement:

View File

@ -8,6 +8,7 @@
(**************************************************************************) (**************************************************************************)
open Logging.Client.Mining open Logging.Client.Mining
module Ed25519 = Environment.Ed25519
let generate_proof_of_work_nonce () = let generate_proof_of_work_nonce () =
Sodium.Random.Bigbytes.generate Constants.proof_of_work_nonce_size Sodium.Random.Bigbytes.generate Constants.proof_of_work_nonce_size

View File

@ -7,6 +7,8 @@
(* *) (* *)
(**************************************************************************) (**************************************************************************)
module Ed25519 = Environment.Ed25519
open Logging.Client.Mining open Logging.Client.Mining
open Operation open Operation

View File

@ -31,7 +31,121 @@ module Data_encoding = Data_encoding
module Time = Time module Time = Time
module Base48 = Base48 module Base48 = Base48
module Hash = Hash 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 Persist = Persist
module Context = Context module Context = Context
module RPC = RPC module RPC = RPC

View File

@ -1,134 +0,0 @@
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2016. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* 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)

View File

@ -1,49 +0,0 @@
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2016. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* 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

View File

@ -78,7 +78,7 @@ let bootstrap_accounts () =
let create_account name = let create_account name =
let secret_key, public_key = Sodium.Sign.random_keypair () in 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 let contract = Contract.default_contract public_key_hash in
Lwt.return { name ; contract ; public_key_hash ; public_key ; secret_key } Lwt.return { name ; contract ; public_key_hash ; public_key ; secret_key }