2018-04-05 19:35:35 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
|
|
|
(* 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 = "Secp256k1.Public_key_hash"
|
|
|
|
let title = "A Secp256k1 public key hash"
|
|
|
|
let b58check_prefix = Base58.Prefix.secp256k1_public_key_hash
|
|
|
|
let size = Some 20
|
|
|
|
end)
|
|
|
|
|
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix Public_key_hash.b58check_encoding "tz2" 36
|
|
|
|
|
2018-04-19 16:29:27 +04:00
|
|
|
open Libsecp256k1.External
|
2018-04-05 19:35:35 +04:00
|
|
|
|
2018-04-19 19:10:45 +04:00
|
|
|
let context =
|
|
|
|
let ctx = Context.create () in
|
|
|
|
match Context.randomize ctx (Rand.generate 32) with
|
|
|
|
| false -> failwith "Secp256k1 context randomization failed. Aborting."
|
|
|
|
| true -> ctx
|
2018-04-05 19:35:35 +04:00
|
|
|
|
|
|
|
module Public_key = struct
|
|
|
|
|
|
|
|
type t = Key.public Key.t
|
|
|
|
|
|
|
|
let name = "Secp256k1.Public_key"
|
|
|
|
let title = "A Secp256k1 public key"
|
|
|
|
|
|
|
|
let to_bytes pk = Key.to_bytes context pk
|
|
|
|
let of_bytes_opt s =
|
|
|
|
try Some (Key.read_pk_exn context s)
|
|
|
|
with _ -> None
|
|
|
|
|
|
|
|
let to_string s = MBytes.to_string (to_bytes s)
|
|
|
|
let of_string_opt s = of_bytes_opt (MBytes.of_string s)
|
|
|
|
|
2018-04-19 19:10:45 +04:00
|
|
|
let size = Key.compressed_pk_bytes
|
2018-04-05 19:35:35 +04:00
|
|
|
|
|
|
|
type Base58.data +=
|
|
|
|
| Data of t
|
|
|
|
|
|
|
|
let b58check_encoding =
|
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.secp256k1_public_key
|
|
|
|
~length: size
|
|
|
|
~to_raw: to_string
|
|
|
|
~of_raw: of_string_opt
|
|
|
|
~wrap: (fun x -> Data x)
|
|
|
|
|
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix b58check_encoding "sppk" 55
|
|
|
|
|
|
|
|
let hash v =
|
|
|
|
Public_key_hash.hash_bytes [to_bytes v]
|
|
|
|
|
|
|
|
include Compare.Make(struct
|
|
|
|
type nonrec t = t
|
|
|
|
let compare a b =
|
2018-04-19 19:10:45 +04:00
|
|
|
MBytes.compare (Key.buffer a) (Key.buffer b)
|
2018-04-05 19:35:35 +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)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
module Secret_key = struct
|
|
|
|
|
|
|
|
type t = Key.secret Key.t
|
|
|
|
|
|
|
|
let name = "Secp256k1.Secret_key"
|
|
|
|
let title = "A Secp256k1 secret key"
|
|
|
|
|
2018-04-19 19:10:45 +04:00
|
|
|
let size = Key.secret_bytes
|
2018-04-05 19:35:35 +04:00
|
|
|
|
|
|
|
let of_bytes_opt s =
|
|
|
|
match Key.read_sk context s with
|
|
|
|
| Ok x -> Some x
|
|
|
|
| _ -> None
|
|
|
|
let to_bytes x = Key.to_bytes context x
|
|
|
|
|
|
|
|
let to_string s = MBytes.to_string (to_bytes s)
|
|
|
|
let of_string_opt s = of_bytes_opt (MBytes.of_string s)
|
|
|
|
|
|
|
|
let to_public_key key = Key.neuterize_exn context key
|
|
|
|
|
|
|
|
type Base58.data +=
|
|
|
|
| Data of t
|
|
|
|
|
|
|
|
let b58check_encoding =
|
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.secp256k1_secret_key
|
|
|
|
~length: size
|
|
|
|
~to_raw: to_string
|
|
|
|
~of_raw: of_string_opt
|
|
|
|
~wrap: (fun x -> Data x)
|
|
|
|
|
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix b58check_encoding "spsk" 54
|
|
|
|
|
|
|
|
include Compare.Make(struct
|
|
|
|
type nonrec t = t
|
|
|
|
let compare a b =
|
2018-04-19 19:10:45 +04:00
|
|
|
MBytes.compare (Key.buffer a) (Key.buffer b)
|
2018-04-05 19:35:35 +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)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-04-19 19:10:45 +04:00
|
|
|
type t = Sign.plain Sign.t
|
2018-04-05 19:35:35 +04:00
|
|
|
|
|
|
|
let name = "Secp256k1"
|
|
|
|
let title = "A Secp256k1 signature"
|
|
|
|
|
2018-04-19 19:10:45 +04:00
|
|
|
let size = Sign.plain_bytes
|
2018-04-05 19:35:35 +04:00
|
|
|
|
|
|
|
let of_bytes_opt s =
|
2018-04-19 19:10:45 +04:00
|
|
|
match Sign.read context s with Ok s -> Some s | Error _ -> None
|
|
|
|
|
|
|
|
let to_bytes = Sign.to_bytes ~der:false context
|
2018-04-05 19:35:35 +04:00
|
|
|
|
|
|
|
let to_string s = MBytes.to_string (to_bytes s)
|
|
|
|
let of_string_opt s = of_bytes_opt (MBytes.of_string s)
|
|
|
|
|
|
|
|
type Base58.data +=
|
|
|
|
| Data of t
|
|
|
|
|
|
|
|
let b58check_encoding =
|
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.secp256k1_signature
|
|
|
|
~length: size
|
|
|
|
~to_raw: to_string
|
|
|
|
~of_raw: of_string_opt
|
|
|
|
~wrap: (fun x -> Data x)
|
|
|
|
|
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix b58check_encoding "spsig1" 99
|
|
|
|
|
|
|
|
include Compare.Make(struct
|
|
|
|
type nonrec t = t
|
2018-04-19 19:10:45 +04:00
|
|
|
let compare a b =
|
|
|
|
MBytes.compare (Sign.buffer a) (Sign.buffer b)
|
2018-04-05 19:35:35 +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-05-10 13:12:19 +04:00
|
|
|
let zero = of_bytes_exn (MBytes.make size '\000')
|
2018-04-05 19:35:35 +04:00
|
|
|
|
2018-04-19 19:10:45 +04:00
|
|
|
let sign sk msg =
|
|
|
|
Sign.sign_exn context ~sk msg
|
2018-04-05 19:35:35 +04:00
|
|
|
|
|
|
|
let check public_key signature msg =
|
2018-04-19 19:10:45 +04:00
|
|
|
Sign.verify_exn context ~pk:public_key ~msg ~signature
|
2018-04-05 19:35:35 +04:00
|
|
|
|
2018-04-06 01:22:30 +04:00
|
|
|
let generate_key ?(seed=Rand.generate 32) () =
|
|
|
|
let sk = Key.read_sk_exn context seed in
|
2018-04-05 19:35:35 +04:00
|
|
|
let pk = Key.neuterize_exn context sk in
|
|
|
|
let pkh = Public_key.hash pk in
|
2018-04-06 01:22:30 +04:00
|
|
|
pkh, pk, sk
|
2018-04-05 19:35:35 +04:00
|
|
|
|