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
|
|
|
|
let compare a b = Cstruct.compare (Sign.to_cstruct a) (Sign.to_cstruct b)
|
2017-11-27 09:13:12 +04:00
|
|
|
let (=) xs ys = compare xs ys = 0
|
|
|
|
let (<>) xs ys = compare xs ys <> 0
|
|
|
|
let (<) xs ys = compare xs ys < 0
|
|
|
|
let (<=) xs ys = compare xs ys <= 0
|
|
|
|
let (>=) xs ys = compare xs ys >= 0
|
|
|
|
let (>) xs ys = compare xs ys > 0
|
|
|
|
let max x y = if x >= y then x else y
|
|
|
|
let min x y = if x <= y then x else y
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
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
|
|
|
|
|
|
|
|
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)
|