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. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
open Error_monad
|
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
module Public_key_hash = Blake2B.Make(Base58)(struct
|
|
|
|
let name = "Ed25519.Public_key_hash"
|
2018-04-05 18:07:05 +04:00
|
|
|
let title = "An Ed25519 public key hash"
|
2017-11-27 09:13:12 +04:00
|
|
|
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
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
let name = "Ed25519.Public_key"
|
|
|
|
let title = "Ed25519 public key"
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-02-04 21:39:34 +04:00
|
|
|
let to_string s = Cstruct.to_string (Sign.to_cstruct s)
|
2018-04-05 18:07:05 +04:00
|
|
|
let of_string_opt s = Sign.pk_of_cstruct (Cstruct.of_string s)
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
let to_bytes pk = Cstruct.to_bigarray (Sign.to_cstruct pk)
|
|
|
|
let of_bytes_opt s = Sign.pk_of_cstruct (Cstruct.of_bigarray s)
|
2018-01-09 16:21:01 +04:00
|
|
|
|
2018-02-08 13:51:01 +04:00
|
|
|
let size = Sign.pkbytes
|
2018-01-09 16:21:01 +04:00
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
type Base58.data +=
|
|
|
|
| Data of t
|
|
|
|
|
|
|
|
let b58check_encoding =
|
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.ed25519_public_key
|
|
|
|
~length: size
|
|
|
|
~to_raw: to_string
|
|
|
|
~of_raw: of_string_opt
|
|
|
|
~wrap: (fun x -> Data x)
|
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-05 18:07:05 +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)
|
|
|
|
|
|
|
|
include Helpers.MakeRaw(struct
|
|
|
|
type nonrec t = t
|
|
|
|
let name = name
|
|
|
|
let of_bytes_opt = of_bytes_opt
|
|
|
|
let of_string_opt = of_string_opt
|
|
|
|
let to_string = to_string
|
|
|
|
end)
|
|
|
|
|
|
|
|
include Helpers.MakeB58(struct
|
|
|
|
type nonrec t = t
|
|
|
|
let title = title
|
|
|
|
let name = name
|
|
|
|
let b58check_encoding = b58check_encoding
|
|
|
|
end)
|
|
|
|
|
|
|
|
include Helpers.MakeEncoder(struct
|
|
|
|
type nonrec t = t
|
|
|
|
let name = name
|
|
|
|
let title = title
|
|
|
|
let raw_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv to_bytes of_bytes_exn (Fixed.bytes size)
|
|
|
|
let of_b58check = of_b58check
|
|
|
|
let of_b58check_opt = of_b58check_opt
|
|
|
|
let of_b58check_exn = of_b58check_exn
|
|
|
|
let to_b58check = to_b58check
|
|
|
|
let to_short_b58check = to_short_b58check
|
|
|
|
end)
|
|
|
|
|
|
|
|
let pp ppf t = Format.fprintf ppf "%s" (to_b58check t)
|
2018-04-03 13:44:11 +04:00
|
|
|
|
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-04-05 18:07:05 +04:00
|
|
|
let name = "Ed25519.Secret_key"
|
|
|
|
let title = "An Ed25519 secret key"
|
|
|
|
|
|
|
|
let size = Sign.seedbytes
|
|
|
|
|
|
|
|
let to_bytes x = Cstruct.to_bigarray (Sign.seed x)
|
|
|
|
let of_bytes_opt s =
|
|
|
|
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
|
|
|
|
|
|
|
|
let to_string s = MBytes.to_string (to_bytes s)
|
|
|
|
let of_string_opt s = of_bytes_opt (MBytes.of_string s)
|
|
|
|
|
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 +=
|
2018-04-05 18:07:05 +04:00
|
|
|
| Data of t
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
let b58check_encoding =
|
2018-01-08 20:21:29 +04:00
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.ed25519_seed
|
2018-04-05 18:07:05 +04:00
|
|
|
~length: size
|
|
|
|
~to_raw: (fun sk -> Cstruct.to_string (Sign.seed sk))
|
|
|
|
~of_raw: (fun buf ->
|
2018-02-04 21:39:34 +04:00
|
|
|
let seed = Cstruct.of_string buf in
|
|
|
|
match Sign.keypair ~seed () with
|
|
|
|
| exception _ -> None
|
|
|
|
| _pk, sk -> Some sk)
|
2018-04-05 18:07:05 +04:00
|
|
|
~wrap: (fun sk -> Data 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-04-05 18:07:05 +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))
|
|
|
|
~wrap: (fun x -> Data x)
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-01-08 20:21:29 +04:00
|
|
|
let of_b58check_opt s =
|
2018-04-05 18:07:05 +04:00
|
|
|
match Base58.simple_decode b58check_encoding s with
|
2018-01-08 20:21:29 +04:00
|
|
|
| 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-04-05 18:07:05 +04:00
|
|
|
| None -> Format.kasprintf Pervasives.failwith "Unexpected data (%s)" name
|
|
|
|
let of_b58check s =
|
|
|
|
match of_b58check_opt s with
|
|
|
|
| Some x -> Ok x
|
2018-01-09 16:21:01 +04:00
|
|
|
| None ->
|
2018-04-05 18:07:05 +04:00
|
|
|
generic_error
|
|
|
|
"Failed to read a b58check_encoding data (%s): %S"
|
|
|
|
name s
|
2018-01-09 16:21:01 +04:00
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
let to_b58check s = Base58.simple_encode b58check_encoding s
|
|
|
|
let to_short_b58check s =
|
|
|
|
String.sub
|
|
|
|
(to_b58check s) 0
|
|
|
|
(10 + String.length (Base58.prefix b58check_encoding))
|
2017-11-27 09:13:12 +04:00
|
|
|
|
|
|
|
let () =
|
2018-04-05 18:07:05 +04:00
|
|
|
Base58.check_encoded_prefix b58check_encoding "edsk" 54 ;
|
2018-01-08 20:21:29 +04:00
|
|
|
Base58.check_encoded_prefix secret_key_encoding "edsk" 98
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-04-05 18:07:05 +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)
|
|
|
|
|
|
|
|
include Helpers.MakeRaw(struct
|
|
|
|
type nonrec t = t
|
|
|
|
let name = name
|
|
|
|
let of_bytes_opt = of_bytes_opt
|
|
|
|
let of_string_opt = of_string_opt
|
|
|
|
let to_string = to_string
|
|
|
|
end)
|
|
|
|
|
|
|
|
include Helpers.MakeEncoder(struct
|
|
|
|
type nonrec t = t
|
|
|
|
let name = name
|
|
|
|
let title = title
|
|
|
|
let raw_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv to_bytes of_bytes_exn (Fixed.bytes size)
|
|
|
|
let of_b58check = of_b58check
|
|
|
|
let of_b58check_opt = of_b58check_opt
|
|
|
|
let of_b58check_exn = of_b58check_exn
|
|
|
|
let to_b58check = to_b58check
|
|
|
|
let to_short_b58check = to_short_b58check
|
|
|
|
end)
|
|
|
|
|
|
|
|
let pp ppf t = Format.fprintf ppf "%s" (to_b58check t)
|
2018-04-03 13:44:11 +04:00
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
end
|
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
type t = MBytes.t
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
let name = "Ed25519"
|
|
|
|
let title = "An Ed25519 signature"
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
let size = Sign.bytes
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
let of_bytes_opt s =
|
|
|
|
if MBytes.length s = size then Some s else None
|
|
|
|
let to_bytes x = x
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
let to_string s = MBytes.to_string (to_bytes s)
|
|
|
|
let of_string_opt s = of_bytes_opt (MBytes.of_string s)
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
type Base58.data +=
|
|
|
|
| Data of t
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
let b58check_encoding =
|
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.ed25519_signature
|
|
|
|
~length: size
|
|
|
|
~to_raw: MBytes.to_string
|
|
|
|
~of_raw: (fun s -> Some (MBytes.of_string s))
|
|
|
|
~wrap: (fun x -> Data x)
|
2018-01-09 16:21:01 +04:00
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix b58check_encoding "edsig" 99
|
2018-01-09 16:21:01 +04:00
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
include Compare.Make(struct
|
|
|
|
type nonrec t = t
|
|
|
|
let compare = MBytes.compare
|
|
|
|
end)
|
2017-11-27 09:13:12 +04:00
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
include Helpers.MakeRaw(struct
|
|
|
|
type nonrec t = t
|
|
|
|
let name = name
|
|
|
|
let of_bytes_opt = of_bytes_opt
|
|
|
|
let of_string_opt = of_string_opt
|
|
|
|
let to_string = to_string
|
|
|
|
end)
|
|
|
|
|
|
|
|
include Helpers.MakeB58(struct
|
|
|
|
type nonrec t = t
|
|
|
|
let title = title
|
|
|
|
let name = name
|
|
|
|
let b58check_encoding = b58check_encoding
|
|
|
|
end)
|
|
|
|
|
|
|
|
include Helpers.MakeEncoder(struct
|
|
|
|
type nonrec t = t
|
|
|
|
let name = name
|
|
|
|
let title = title
|
|
|
|
let raw_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv to_bytes of_bytes_exn (Fixed.bytes size)
|
|
|
|
let of_b58check = of_b58check
|
|
|
|
let of_b58check_opt = of_b58check_opt
|
|
|
|
let of_b58check_exn = of_b58check_exn
|
|
|
|
let to_b58check = to_b58check
|
|
|
|
let to_short_b58check = to_short_b58check
|
|
|
|
end)
|
|
|
|
|
|
|
|
let pp ppf t = Format.fprintf ppf "%s" (to_b58check t)
|
|
|
|
|
|
|
|
let zero = MBytes.init size '\000'
|
|
|
|
|
|
|
|
let sign key msg =
|
|
|
|
Cstruct.(to_bigarray (Sign.detached ~key (of_bigarray msg)))
|
|
|
|
|
|
|
|
let check public_key signature msg =
|
|
|
|
Sign.verify_detached ~key:public_key
|
|
|
|
~signature:(Cstruct.of_bigarray signature)
|
|
|
|
(Cstruct.of_bigarray msg)
|
|
|
|
|
|
|
|
let append key msg =
|
|
|
|
MBytes.concat msg (sign key msg)
|
|
|
|
|
|
|
|
let concat msg signature =
|
|
|
|
MBytes.concat msg signature
|
2017-11-27 09:13:12 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-04-05 18:07:05 +04:00
|
|
|
end
|
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)
|
2018-04-05 18:07:05 +04:00
|
|
|
|
|
|
|
let generate_key () =
|
|
|
|
let seed = Seed.generate () in
|
|
|
|
generate_seeded_key seed
|
|
|
|
|
|
|
|
include Compare.Make(struct
|
|
|
|
type nonrec t = t
|
|
|
|
let compare = MBytes.compare
|
|
|
|
end)
|
|
|
|
|