use correct denomination hash (with age-mask and cipher)

This commit is contained in:
swrup 2026-02-27 23:36:05 +01:00
parent 9c87aa63ea
commit 3cef594e1f
14 changed files with 142 additions and 61 deletions

View file

@ -209,13 +209,12 @@ module RsaPublicKey = struct
let of_octets = Binary_format_rsa.pub_of_octets
let to_b32 t = B32.encode (to_octets t)
let jsont =
let of_b32 s =
let* s = B32.decode s in
let+ v = of_octets s in
v
in
Jsont.of_of_string ~kind:"RsaPublicKey" of_b32 ~enc:to_b32
let of_b32 s =
let* s = B32.decode s in
let+ v = of_octets s in
v
let jsont = Jsont.of_of_string ~kind:"RsaPublicKey" of_b32 ~enc:to_b32
let caqti : t Caqti_type.t =
Caqti_type.custom
@ -257,6 +256,63 @@ module RsaSignature = struct
Jsont.of_of_string ~kind:"RsaSignature" of_b32 ~enc:to_b32
end
module DenominationHash : sig
type t
val bin : t Bin.t
val caqti : t Caqti_type.t
val jsont : t Jsont.t
val hash_of_rsa : RsaPublicKey.t -> t
val of_octets : string -> t
val to_octets : t -> string
val of_b32 : B32.t -> (t, string) result
val to_b32 : t -> B32.t
end = struct
open Digestif
type t = SHA512.t
type cipher =
| RSA
| CS [@ocaml.warning "-37"]
(* = GNUNET_CRYPTO_BSA_(RSA|CS) *)
let cipher_to_int32 = function RSA -> 1_l | CS -> 2_l
let hash_of_rsa pub =
let age_mask = 0_l in
let cipher = cipher_to_int32 RSA in
let pub = RsaPublicKey.to_octets pub in
let len = String.length pub in
let buf = Bytes.create (4 + 4 + len) in
Bytes.set_int32_be buf 0 age_mask;
Bytes.set_int32_be buf 4 cipher;
Bytes.blit_string pub 0 buf 8 len;
SHA512.digest_bytes buf
let of_octets s =
match SHA512.of_raw_string_opt s with
| None -> Fmt.failwith "H64.of_octets failure"
| Some t -> t
let to_octets = SHA512.to_raw_string
let of_b32 s = Result.map of_octets (B32.decode s)
let to_b32 t = B32.encode (to_octets t)
let bin =
let open Bin in
map (bytes 64) of_octets to_octets
let caqti =
let open Caqti_type in
custom
~encode:(fun v -> Ok (to_octets v))
~decode:(fun v -> Ok (of_octets v))
octets
let jsont = Jsont.of_of_string ~kind:"DenominationHash" of_b32 ~enc:to_b32
end
(* some type aliases, just for prettier .mli *)
type eddsa_priv = EddsaPrivateKey.t
type eddsa_pub = EddsaPublicKey.t
@ -264,4 +320,4 @@ type eddsa_sig = EddsaSignature.t
type rsa_priv = RsaPrivateKey.t
type rsa_pub = RsaPublicKey.t
type rsa_sig = RsaSignature.t
type denom_hash = Hash.DenominationHash.t
type denom_hash = DenominationHash.t