mte/src/eddsa.ml

106 lines
2.6 KiB
OCaml
Raw Normal View History

2026-03-20 22:47:58 +01:00
open Syntax
2026-03-21 09:50:04 +01:00
module Mirage_eddsa = Mirage_crypto_ec.Ed25519
2026-03-20 22:47:58 +01:00
let pp_mirage_error = Mirage_crypto_ec.pp_error
2026-03-20 22:47:58 +01:00
2026-03-21 09:50:04 +01:00
type priv = Mirage_eddsa.priv
type pub = Mirage_eddsa.pub
type sig_ = S of string [@@unboxed]
2026-03-20 22:47:58 +01:00
2026-03-21 09:50:04 +01:00
let generate = Mirage_eddsa.generate
let pub_of_priv = Mirage_eddsa.pub_of_priv
2026-03-20 22:47:58 +01:00
let sign ~key s =
let sig_ = Mirage_eddsa.sign ~key s in
S sig_
let verify ~key (S s) ~msg =
let b = Mirage_eddsa.verify ~key s ~msg in
if b then Ok () else Error `Invalid_signature_eddsa
2026-03-20 22:47:58 +01:00
module Priv = struct
let priv_to_octets t = Mirage_eddsa.priv_to_octets t
2026-03-20 22:47:58 +01:00
let priv_of_octets o =
Mirage_eddsa.priv_of_octets o
|> Result.map_error (Fmt.str "%a" pp_mirage_error)
let priv_to_b32 t = priv_to_octets t |> B32.encode
let priv_of_b32 s =
2026-03-20 22:47:58 +01:00
let* octets = B32.decode s in
priv_of_octets octets
let priv_jsont =
Jsont.of_of_string ~kind:"EddsaPrivateKey" priv_of_b32 ~enc:priv_to_b32
2026-03-20 22:47:58 +01:00
let priv_bin =
let priv_of_octets_exn t =
match priv_of_octets t with Error e -> invalid_arg e | Ok v -> v
in
Bin.map (Bin.bytes 32) priv_of_octets_exn priv_to_octets
end
2026-03-20 22:47:58 +01:00
module Pub = struct
let pub_to_octets t = Mirage_eddsa.pub_to_octets t
2026-03-20 22:47:58 +01:00
let pub_of_octets t =
Mirage_eddsa.pub_of_octets t
|> Result.map_error (Fmt.str "%a" pp_mirage_error)
2026-03-20 22:47:58 +01:00
let pub_to_b32 t = pub_to_octets t |> B32.encode
2026-03-20 22:47:58 +01:00
let pub_of_b32 s =
let* octets = B32.decode s in
pub_of_octets octets
2026-03-20 22:47:58 +01:00
let pub_jsont =
Jsont.of_of_string ~kind:"EddsaPublicKey" pub_of_b32 ~enc:pub_to_b32
2026-03-20 22:47:58 +01:00
let pub_bin =
let pub_of_octets_exn t =
match pub_of_octets t with Error e -> invalid_arg e | Ok v -> v
in
Bin.map (Bin.bytes 32) pub_of_octets_exn pub_to_octets
2026-03-20 22:47:58 +01:00
let pub_caqti =
Caqti_type.custom
~encode:(fun v -> Ok (pub_to_octets v))
~decode:pub_of_octets Caqti_type.octets
end
2026-03-20 22:47:58 +01:00
module Sig_ = struct
let sig_to_octets (S s) = s
2026-03-20 22:47:58 +01:00
let sig_of_octets s =
match String.length s = 64 with
| false -> Error "invalid eddsa signature length"
| true -> Ok (S s)
2026-03-20 22:47:58 +01:00
let sig_of_b32 s =
let* s = B32.decode s in
sig_of_octets s
let sig_to_b32 (S s) = B32.encode s
let sig_jsont =
Jsont.of_of_string ~kind:"EddsaSignature" sig_of_b32 ~enc:sig_to_b32
let sig_bin =
let sig_of_octets_exn t =
match sig_of_octets t with Error e -> invalid_arg e | Ok v -> v
in
Bin.map (Bin.bytes 64) sig_of_octets_exn sig_to_octets
let sig_caqti =
Caqti_type.custom
~encode:(fun v -> Ok (sig_to_octets v))
~decode:sig_of_octets Caqti_type.octets
end
include Priv
include Pub
include Sig_
let pp_pub ppf pub = Fmt.pf ppf "%s" (pub_to_b32 pub)