2017-11-27 09:13:12 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2017-11-27 09:13:12 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
module Public_key_hash = Blake2B.Make(Base58)(struct
|
|
|
|
let name = "Ed25519.Public_key_hash"
|
|
|
|
let title = "An Ed25519 public key ID"
|
|
|
|
let b58check_prefix = Base58.Prefix.ed25519_public_key_hash
|
|
|
|
let size = Some 20
|
|
|
|
end)
|
|
|
|
|
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix Public_key_hash.b58check_encoding "tz1" 36
|
|
|
|
|
2018-02-04 21:39:34 +04:00
|
|
|
open Tweetnacl
|
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
module Public_key = struct
|
|
|
|
|
2018-02-04 21:39:34 +04:00
|
|
|
type t = Sign.public Sign.key
|
2018-02-13 20:30:25 +04:00
|
|
|
|
|
|
|
include Compare.Make(struct
|
|
|
|
type nonrec t = t
|
|
|
|
let compare a b =
|
|
|
|
Cstruct.compare (Sign.to_cstruct a) (Sign.to_cstruct b)
|
|
|
|
end)
|
2017-11-27 09:13:12 +04:00
|
|
|
|
|
|
|
type Base58.data +=
|
|
|
|
| Public_key of t
|
|
|
|
|
2018-02-04 21:39:34 +04:00
|
|
|
let to_string s = Cstruct.to_string (Sign.to_cstruct s)
|
|
|
|
let of_string s = Sign.pk_of_cstruct (Cstruct.of_string s)
|
2018-01-15 18:03:13 +04:00
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
let b58check_encoding =
|
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.ed25519_public_key
|
2018-02-04 21:39:34 +04:00
|
|
|
~length:Sign.pkbytes
|
2018-01-15 18:03:13 +04:00
|
|
|
~to_raw:to_string
|
|
|
|
~of_raw:of_string
|
2017-11-27 09:13:12 +04:00
|
|
|
~wrap:(fun x -> Public_key x)
|
|
|
|
|
|
|
|
let of_b58check_opt s = Base58.simple_decode b58check_encoding s
|
|
|
|
let of_b58check_exn s =
|
|
|
|
match Base58.simple_decode b58check_encoding s with
|
|
|
|
| Some x -> x
|
2018-02-01 20:31:08 +04:00
|
|
|
| None -> Pervasives.failwith
|
|
|
|
(Printf.sprintf "%s is not an ed25519 public key" s)
|
2017-11-27 09:13:12 +04:00
|
|
|
let to_b58check s = Base58.simple_encode b58check_encoding s
|
2018-02-01 20:31:08 +04:00
|
|
|
let pp ppf t = Format.fprintf ppf "%s" (to_b58check t)
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-01-15 18:03:13 +04:00
|
|
|
let of_hex s = of_string (Hex.to_string s)
|
2018-02-04 21:39:34 +04:00
|
|
|
let of_hex_exn s =
|
|
|
|
match of_string (Hex.to_string s) with
|
|
|
|
| Some x -> x
|
|
|
|
| None -> invalid_arg "Public_key.of_hex_exn"
|
2018-01-15 18:03:13 +04:00
|
|
|
let to_hex s = Hex.of_string (to_string s)
|
|
|
|
|
2018-01-09 16:21:01 +04:00
|
|
|
let of_bytes_opt s =
|
2018-02-04 21:39:34 +04:00
|
|
|
Sign.pk_of_cstruct (Cstruct.of_bigarray s)
|
2018-01-09 16:21:01 +04:00
|
|
|
|
|
|
|
let of_bytes_exn s =
|
|
|
|
match of_bytes_opt s with
|
|
|
|
| None ->
|
|
|
|
Pervasives.invalid_arg "Ed25519.Public_key.of_bytes_exn: argument is not a serialized public key"
|
|
|
|
| Some pk -> pk
|
2018-02-08 13:51:01 +04:00
|
|
|
let size = Sign.pkbytes
|
2018-01-09 16:21:01 +04:00
|
|
|
|
2018-02-04 21:39:34 +04:00
|
|
|
let to_bytes pk = Cstruct.to_bigarray (Sign.to_cstruct pk)
|
2017-11-27 09:13:12 +04:00
|
|
|
|
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix b58check_encoding "edpk" 54
|
|
|
|
|
|
|
|
let hash v =
|
|
|
|
Public_key_hash.hash_bytes
|
2018-02-08 13:51:01 +04:00
|
|
|
[ Cstruct.to_bigarray (Sign.to_cstruct v) ]
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-04-03 13:44:11 +04:00
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
splitted
|
|
|
|
~json:
|
|
|
|
(describe
|
|
|
|
~title: "An Ed25519 public key (Tezos_crypto.Base58Check encoded)" @@
|
|
|
|
conv
|
|
|
|
(fun s -> to_b58check s)
|
|
|
|
(fun s ->
|
|
|
|
match of_b58check_opt s with
|
|
|
|
| Some x -> x
|
|
|
|
| None -> Data_encoding.Json.cannot_destruct
|
|
|
|
"Ed25519 public key: unexpected prefix.")
|
|
|
|
string)
|
|
|
|
~binary:
|
|
|
|
(conv
|
|
|
|
to_bytes
|
|
|
|
of_bytes_exn
|
|
|
|
(Fixed.bytes size))
|
|
|
|
let of_b58check s =
|
|
|
|
match of_b58check_opt s with
|
|
|
|
| Some x -> Ok x
|
|
|
|
| None ->
|
|
|
|
Error_monad.generic_error
|
|
|
|
"Failed to read a base58-encoded Ed25519 public key"
|
|
|
|
let param
|
|
|
|
?(name="ed25519-public")
|
|
|
|
?(desc="Ed25519 public key (b58check-encoded)") t =
|
|
|
|
Clic.(param ~name ~desc
|
|
|
|
(parameter (fun _ str -> Lwt.return (of_b58check str))) t)
|
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
module Secret_key = struct
|
|
|
|
|
2018-02-04 21:39:34 +04:00
|
|
|
type t = Sign.secret Sign.key
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-02-04 21:39:34 +04:00
|
|
|
let to_public_key = Sign.public
|
2017-11-27 09:13:12 +04:00
|
|
|
|
|
|
|
type Base58.data +=
|
|
|
|
| Secret_key of t
|
|
|
|
|
2018-01-08 20:21:29 +04:00
|
|
|
let seed_encoding =
|
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.ed25519_seed
|
2018-02-04 21:39:34 +04:00
|
|
|
~length:Sign.seedbytes
|
|
|
|
~to_raw:(fun sk -> Cstruct.to_string (Sign.seed sk))
|
|
|
|
~of_raw:(fun buf ->
|
|
|
|
let seed = Cstruct.of_string buf in
|
|
|
|
match Sign.keypair ~seed () with
|
|
|
|
| exception _ -> None
|
|
|
|
| _pk, sk -> Some sk)
|
|
|
|
~wrap:(fun sk -> Secret_key sk)
|
2018-01-08 20:21:29 +04:00
|
|
|
|
|
|
|
let secret_key_encoding =
|
2017-11-27 09:13:12 +04:00
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.ed25519_secret_key
|
2018-02-04 21:39:34 +04:00
|
|
|
~length:Sign.skbytes
|
|
|
|
~to_raw:(fun sk -> Cstruct.to_string (Sign.to_cstruct sk))
|
|
|
|
~of_raw:(fun buf -> Sign.sk_of_cstruct (Cstruct.of_string buf))
|
2017-11-27 09:13:12 +04:00
|
|
|
~wrap:(fun x -> Secret_key x)
|
|
|
|
|
2018-01-08 20:21:29 +04:00
|
|
|
let of_b58check_opt s =
|
|
|
|
match Base58.simple_decode seed_encoding s with
|
|
|
|
| Some x -> Some x
|
|
|
|
| None -> Base58.simple_decode secret_key_encoding s
|
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
let of_b58check_exn s =
|
2018-01-08 20:21:29 +04:00
|
|
|
match of_b58check_opt s with
|
2017-11-27 09:13:12 +04:00
|
|
|
| Some x -> x
|
2018-02-01 20:31:08 +04:00
|
|
|
| None -> Pervasives.failwith
|
|
|
|
(Printf.sprintf "%s is not an ed25519 secret key" s)
|
2018-01-08 20:21:29 +04:00
|
|
|
let to_b58check s = Base58.simple_encode seed_encoding s
|
2018-02-01 20:31:08 +04:00
|
|
|
let pp ppf t = Format.fprintf ppf "%s" (to_b58check t)
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-01-09 16:21:01 +04:00
|
|
|
let of_bytes_opt s =
|
2018-02-04 21:39:34 +04:00
|
|
|
let s = Cstruct.of_bigarray s in
|
|
|
|
match Cstruct.len s with
|
|
|
|
| 32 -> let _pk, sk = Sign.keypair ~seed:s () in Some sk
|
|
|
|
| 64 -> Sign.sk_of_cstruct s
|
|
|
|
| _ -> None
|
2018-01-09 16:21:01 +04:00
|
|
|
|
|
|
|
let of_bytes_exn s =
|
|
|
|
match of_bytes_opt s with
|
|
|
|
| None ->
|
|
|
|
Pervasives.invalid_arg "Ed25519.Secret_key.of_bytes_exn: argument is not a serialized seed"
|
|
|
|
| Some sk -> sk
|
|
|
|
|
2018-02-08 13:51:01 +04:00
|
|
|
let to_bytes x = Cstruct.to_bigarray (Sign.seed x)
|
|
|
|
let size = Sign.seedbytes
|
2017-11-27 09:13:12 +04:00
|
|
|
|
|
|
|
let () =
|
2018-01-08 20:21:29 +04:00
|
|
|
Base58.check_encoded_prefix seed_encoding "edsk" 54 ;
|
|
|
|
Base58.check_encoded_prefix secret_key_encoding "edsk" 98
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-04-03 13:44:11 +04:00
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
splitted
|
|
|
|
~json:
|
|
|
|
(describe
|
|
|
|
~title: "An Ed25519 secret key (Tezos_crypto.Base58Check encoded)" @@
|
|
|
|
conv
|
|
|
|
(fun s -> to_b58check s)
|
|
|
|
(fun s ->
|
|
|
|
match of_b58check_opt s with
|
|
|
|
| Some x -> x
|
|
|
|
| None -> Data_encoding.Json.cannot_destruct
|
|
|
|
"Ed25519 secret key: unexpected prefix.")
|
|
|
|
string)
|
|
|
|
~binary:
|
|
|
|
(conv
|
|
|
|
to_bytes
|
|
|
|
of_bytes_exn
|
|
|
|
(Fixed.bytes size))
|
|
|
|
let of_b58check s =
|
|
|
|
match of_b58check_opt s with
|
|
|
|
| Some x -> Ok x
|
|
|
|
| None ->
|
|
|
|
Error_monad.generic_error
|
|
|
|
"Failed to read a base58-encoded Ed25519 secret key"
|
|
|
|
let param
|
|
|
|
?(name="ed25519-secret")
|
|
|
|
?(desc="Ed25519 secret key (b58check-encoded)") t =
|
|
|
|
Clic.(param ~name ~desc
|
|
|
|
(parameter (fun _ str -> Lwt.return (of_b58check str))) t)
|
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
let sign key msg =
|
2018-02-04 21:39:34 +04:00
|
|
|
Cstruct.(to_bigarray (Sign.detached ~key (of_bigarray msg)))
|
2017-11-27 09:13:12 +04:00
|
|
|
|
|
|
|
module Signature = struct
|
|
|
|
|
|
|
|
type t = MBytes.t
|
|
|
|
|
|
|
|
type Base58.data +=
|
|
|
|
| Signature of t
|
|
|
|
|
|
|
|
let b58check_encoding =
|
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.ed25519_signature
|
2018-02-04 21:39:34 +04:00
|
|
|
~length:Sign.bytes
|
2017-11-27 09:13:12 +04:00
|
|
|
~to_raw:MBytes.to_string
|
|
|
|
~of_raw:(fun s -> Some (MBytes.of_string s))
|
|
|
|
~wrap:(fun x -> Signature x)
|
|
|
|
|
|
|
|
let of_b58check_opt s = Base58.simple_decode b58check_encoding s
|
|
|
|
let of_b58check_exn s =
|
|
|
|
match Base58.simple_decode b58check_encoding s with
|
|
|
|
| Some x -> x
|
2018-02-01 20:31:08 +04:00
|
|
|
| None -> Pervasives.failwith
|
|
|
|
(Printf.sprintf "%s is not an ed25519 signature" s)
|
2017-11-27 09:13:12 +04:00
|
|
|
let to_b58check s = Base58.simple_encode b58check_encoding s
|
2018-02-01 20:31:08 +04:00
|
|
|
let pp ppf t = Format.fprintf ppf "%s" (to_b58check t)
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-01-09 16:21:01 +04:00
|
|
|
let of_bytes_opt s =
|
2018-02-04 21:39:34 +04:00
|
|
|
if MBytes.length s = Sign.bytes then Some s else None
|
2018-01-09 16:21:01 +04:00
|
|
|
|
|
|
|
let of_bytes_exn s =
|
|
|
|
match of_bytes_opt s with
|
|
|
|
| None ->
|
|
|
|
Pervasives.invalid_arg "Ed25519.Signature.of_bytes_exn: argument is not a serialized signature"
|
|
|
|
| Some signature -> signature
|
|
|
|
|
|
|
|
let to_bytes x = x
|
2018-02-08 13:51:01 +04:00
|
|
|
let size = Sign.bytes
|
2018-02-24 01:12:03 +04:00
|
|
|
let zero = MBytes.init size '\000'
|
2017-11-27 09:13:12 +04:00
|
|
|
|
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix b58check_encoding "edsig" 99
|
|
|
|
|
|
|
|
let check public_key signature msg =
|
2018-02-04 21:39:34 +04:00
|
|
|
Sign.verify_detached ~key:public_key
|
|
|
|
~signature:(Cstruct.of_bigarray signature)
|
|
|
|
(Cstruct.of_bigarray msg)
|
2017-11-27 09:13:12 +04:00
|
|
|
|
|
|
|
let append key msg =
|
|
|
|
MBytes.concat msg (sign key msg)
|
|
|
|
|
|
|
|
let concat msg signature =
|
|
|
|
MBytes.concat msg signature
|
|
|
|
|
2018-04-03 13:44:11 +04:00
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
splitted
|
|
|
|
~json:
|
|
|
|
(describe
|
|
|
|
~title: "An Ed25519 signature (Base58Check encoded)" @@
|
|
|
|
conv
|
|
|
|
(fun s -> to_b58check s)
|
|
|
|
(fun s ->
|
|
|
|
match of_b58check_opt s with
|
|
|
|
| Some x -> x
|
|
|
|
| None -> Data_encoding.Json.cannot_destruct
|
|
|
|
"Ed25519 signature: unexpected prefix.")
|
|
|
|
string)
|
|
|
|
~binary:
|
|
|
|
(conv
|
|
|
|
to_bytes
|
|
|
|
of_bytes_exn
|
|
|
|
(Fixed.bytes size))
|
|
|
|
let of_b58check s =
|
|
|
|
match of_b58check_opt s with
|
|
|
|
| Some x -> Ok x
|
|
|
|
| None ->
|
|
|
|
Error_monad.generic_error
|
|
|
|
"Failed to read a base58-encoded Ed25519 signature"
|
|
|
|
let param
|
|
|
|
?(name="ed25519-signature")
|
|
|
|
?(desc="Ed25519 signature (b58check-encoded)") t =
|
|
|
|
Clic.(param ~name ~desc
|
|
|
|
(parameter (fun _ str -> Lwt.return (of_b58check str))) t)
|
2017-11-27 09:13:12 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
module Seed = struct
|
|
|
|
|
2018-02-04 21:39:34 +04:00
|
|
|
type t = Cstruct.t
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-02-04 21:39:34 +04:00
|
|
|
let generate () = Rand.gen 32
|
|
|
|
let extract = Sign.seed
|
2017-11-27 09:13:12 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
let generate_key () =
|
2018-02-04 21:39:34 +04:00
|
|
|
let pk, sk = Sign.keypair () in
|
|
|
|
(Public_key.hash pk, pk, sk)
|
2017-11-27 09:13:12 +04:00
|
|
|
|
|
|
|
let generate_seeded_key seed =
|
2018-02-04 21:39:34 +04:00
|
|
|
let pk, sk = Sign.keypair ~seed () in
|
|
|
|
(Public_key.hash pk, pk, sk)
|