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
|
|
|
|
|
2018-06-12 23:07:50 +04:00
|
|
|
module Public_key_hash = struct
|
|
|
|
include Blake2B.Make(Base58)(struct
|
|
|
|
let name = "Ed25519.Public_key_hash"
|
|
|
|
let title = "An Ed25519 public key hash"
|
|
|
|
let b58check_prefix = Base58.Prefix.ed25519_public_key_hash
|
|
|
|
let size = Some 20
|
|
|
|
end)
|
|
|
|
module Logging = struct
|
|
|
|
let tag = Tag.def ~doc:title name pp
|
|
|
|
end
|
|
|
|
end
|
2017-11-27 09:13:12 +04:00
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix Public_key_hash.b58check_encoding "tz1" 36
|
|
|
|
|
2018-04-06 01:22:30 +04:00
|
|
|
open Hacl
|
2018-02-04 21:39:34 +04:00
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
module Public_key = struct
|
|
|
|
|
2018-04-06 01:22:30 +04:00
|
|
|
type t = 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-04-06 01:22:30 +04:00
|
|
|
let to_string s = MBytes.to_string (Sign.unsafe_to_bytes s)
|
|
|
|
let of_string_opt s =
|
|
|
|
if String.length s < Sign.pkbytes then None
|
|
|
|
else
|
|
|
|
let pk = MBytes.create Sign.pkbytes in
|
|
|
|
MBytes.blit_of_string s 0 pk 0 Sign.pkbytes ;
|
|
|
|
Some (Sign.unsafe_pk_of_bytes pk)
|
|
|
|
|
|
|
|
let to_bytes pk =
|
|
|
|
let buf = MBytes.create Sign.pkbytes in
|
|
|
|
Sign.blit_to_bytes pk buf ;
|
|
|
|
buf
|
|
|
|
|
|
|
|
let of_bytes_opt buf =
|
|
|
|
let buflen = MBytes.length buf in
|
|
|
|
if buflen < Sign.pkbytes then None
|
|
|
|
else
|
|
|
|
let pk = MBytes.create Sign.pkbytes in
|
|
|
|
MBytes.blit buf 0 pk 0 Sign.pkbytes ;
|
|
|
|
Some (Sign.unsafe_pk_of_bytes pk)
|
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 =
|
2018-04-06 01:22:30 +04:00
|
|
|
Public_key_hash.hash_bytes [ Sign.unsafe_to_bytes 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 =
|
2018-04-06 01:22:30 +04:00
|
|
|
MBytes.compare (Sign.unsafe_to_bytes a) (Sign.unsafe_to_bytes b)
|
2018-04-05 18:07:05 +04:00
|
|
|
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-04-06 01:22:30 +04:00
|
|
|
type t = 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"
|
|
|
|
|
2018-04-06 01:22:30 +04:00
|
|
|
let size = Sign.skbytes
|
|
|
|
|
|
|
|
let to_bytes sk =
|
|
|
|
let buf = MBytes.create Sign.skbytes in
|
|
|
|
Sign.blit_to_bytes sk buf ;
|
|
|
|
buf
|
2018-04-05 18:07:05 +04:00
|
|
|
|
|
|
|
let of_bytes_opt s =
|
2018-04-06 01:22:30 +04:00
|
|
|
if MBytes.length s > 64 then None
|
|
|
|
else
|
|
|
|
let sk = MBytes.create Sign.skbytes in
|
|
|
|
MBytes.blit s 0 sk 0 Sign.skbytes ;
|
|
|
|
Some (Sign.unsafe_sk_of_bytes sk)
|
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)
|
|
|
|
|
2018-04-06 01:22:30 +04:00
|
|
|
let to_public_key = Sign.neuterize
|
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
|
2018-04-06 01:22:30 +04:00
|
|
|
~to_raw: (fun sk -> MBytes.to_string (Sign.unsafe_to_bytes sk))
|
2018-04-05 18:07:05 +04:00
|
|
|
~of_raw: (fun buf ->
|
2018-04-06 01:22:30 +04:00
|
|
|
if String.length buf <> Sign.skbytes then None
|
|
|
|
else Some (Sign.unsafe_sk_of_bytes (MBytes.of_string buf)))
|
2018-04-05 18:07:05 +04:00
|
|
|
~wrap: (fun sk -> Data sk)
|
2018-01-08 20:21:29 +04:00
|
|
|
|
2018-04-06 01:22:30 +04:00
|
|
|
(* Legacy NaCl secret key encoding. Used to store both sk and pk. *)
|
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-06 01:22:30 +04:00
|
|
|
~length: Sign.(skbytes + pkbytes)
|
|
|
|
~to_raw: (fun sk ->
|
|
|
|
let pk = Sign.neuterize sk in
|
|
|
|
let buf = MBytes.create Sign.(skbytes + pkbytes) in
|
|
|
|
Sign.blit_to_bytes sk buf ;
|
|
|
|
Sign.blit_to_bytes pk ~pos:Sign.skbytes buf ;
|
|
|
|
MBytes.to_string buf)
|
|
|
|
~of_raw: (fun buf ->
|
|
|
|
if String.length buf <> Sign.(skbytes + pkbytes) then None
|
|
|
|
else
|
|
|
|
let sk = MBytes.create Sign.skbytes in
|
|
|
|
MBytes.blit_of_string buf 0 sk 0 Sign.skbytes ;
|
|
|
|
Some (Sign.unsafe_sk_of_bytes sk))
|
2018-04-05 18:07:05 +04:00
|
|
|
~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 =
|
2018-04-06 01:22:30 +04:00
|
|
|
MBytes.compare (Sign.unsafe_to_bytes a) (Sign.unsafe_to_bytes b)
|
2018-04-05 18:07:05 +04:00
|
|
|
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-05-25 17:50:31 +04:00
|
|
|
type watermark = MBytes.t
|
|
|
|
|
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 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-05-10 13:12:19 +04:00
|
|
|
let zero = MBytes.make size '\000'
|
2018-04-05 18:07:05 +04:00
|
|
|
|
2018-05-25 17:50:31 +04:00
|
|
|
let sign ?watermark sk msg =
|
|
|
|
let msg =
|
|
|
|
Blake2B.to_bytes @@
|
|
|
|
Blake2B.hash_bytes @@
|
|
|
|
match watermark with
|
|
|
|
| None -> [msg]
|
|
|
|
| Some prefix -> [ prefix ; msg ] in
|
2018-04-06 01:22:30 +04:00
|
|
|
let signature = MBytes.create Sign.bytes in
|
|
|
|
Sign.sign ~sk ~msg ~signature ;
|
|
|
|
signature
|
|
|
|
|
2018-05-25 17:50:31 +04:00
|
|
|
let check ?watermark pk signature msg =
|
|
|
|
let msg =
|
|
|
|
Blake2B.to_bytes @@
|
|
|
|
Blake2B.hash_bytes @@
|
|
|
|
match watermark with
|
|
|
|
| None -> [msg]
|
|
|
|
| Some prefix -> [ prefix ; msg ] in
|
2018-04-06 01:22:30 +04:00
|
|
|
Sign.verify ~pk ~signature ~msg
|
|
|
|
|
|
|
|
let generate_key ?seed () =
|
|
|
|
match seed with
|
|
|
|
| None ->
|
|
|
|
let pk, sk = Sign.keypair () in
|
|
|
|
Public_key.hash pk, pk, sk
|
|
|
|
| Some seed ->
|
|
|
|
let seedlen = MBytes.length seed in
|
|
|
|
if seedlen < Sign.skbytes then
|
|
|
|
invalid_arg (Printf.sprintf "Ed25519.generate_key: seed must \
|
|
|
|
be at least %d bytes long (got %d)"
|
|
|
|
Sign.skbytes seedlen) ;
|
|
|
|
let sk = MBytes.create Sign.skbytes in
|
|
|
|
MBytes.blit seed 0 sk 0 Sign.skbytes ;
|
|
|
|
let sk = Sign.unsafe_sk_of_bytes sk in
|
|
|
|
let pk = Sign.neuterize sk in
|
|
|
|
Public_key.hash pk, pk, sk
|
2018-04-05 18:07:05 +04:00
|
|
|
|
|
|
|
include Compare.Make(struct
|
|
|
|
type nonrec t = t
|
|
|
|
let compare = MBytes.compare
|
|
|
|
end)
|