This commit is contained in:
swrup 2026-02-05 17:51:24 +01:00
parent 55ccc6f18a
commit 128c0ffc3d
11 changed files with 136 additions and 141 deletions

113
src/hash.ml Normal file
View file

@ -0,0 +1,113 @@
(* TODO hash:
json:
"A JSON object is canonicalized by converting it to an ASCII byte array
with the algorithm specified in RFC 8785. The resulting bytes are
terminated with a single 0-byte and then hashed with SHA512." *)
(* C-terminated strings
some strings need to be hashed with a '\0' termination char *)
module Hash_32 = struct
type t = Digestif.SHA256.t
let hash s = Digestif.SHA256.(digest_string s)
let of_octets s =
match String.length s = 32 with
| false -> Fmt.failwith "Hash.of_octets failure: data is not 32 bytes"
| true -> Digestif.SHA256.of_raw_string s
let to_octets = Digestif.SHA256.to_raw_string
let of_b32 s = Result.map of_octets (B32.decode s)
let bin =
let open Bin in
map (bytes 32) of_octets to_octets
(* hashes are not b32 encoded in the database *)
let caqti : t Caqti_type.t =
let open Caqti_type in
custom
~encode:(fun v -> Ok (to_octets v))
~decode:(fun v -> Ok (of_octets v))
octets
let jsont =
let enc v = B32.encode (to_octets v) in
Jsont.of_of_string ~kind:"Hash 32" of_b32 ~enc
end
module Hash_64 = struct
type t = Digestif.SHA512.t
let hash s = Digestif.SHA512.(digest_string s)
let of_octets s =
match String.length s = 64 with
| false -> Fmt.failwith "Hash.of_octets failure: data is not 64 bytes"
| true -> Digestif.SHA512.of_raw_string s
let to_octets v =
let s = Digestif.SHA512.to_raw_string v in
match String.length s = 64 with
| false -> Fmt.failwith "Hash.to_octets failure: data is not 64 bytes"
| true -> s
let of_b32 s = Result.map of_octets (B32.decode s)
let bin =
let open Bin in
map (bytes 64) of_octets to_octets
(* hashes are not b32 encoded in the database *)
let caqti : t Caqti_type.t =
let open Caqti_type in
custom
~encode:(fun v -> Ok (to_octets v))
~decode:(fun v -> Ok (of_octets v))
octets
let jsont =
let enc v = B32.encode (to_octets v) in
Jsont.of_of_string ~kind:"Hash 64" of_b32 ~enc
end
(* Hash over string + '\0' *)
module Hash_32_cstr = struct
include Hash_32
let hash s =
let s = s ^ "\x00" in
Digestif.SHA256.(digest_string s)
end
module Hash_64_cstr = struct
include Hash_64
let hash s =
let s = s ^ "\x00" in
Digestif.SHA512.(digest_string s)
end
module type Hash_S = sig
type t
val bin : t Bin.t
val caqti : t Caqti_type.t
val jsont : t Jsont.t
val hash : string -> t
val of_octets : string -> t
val to_octets : t -> string
val of_b32 : B32.t -> (t, string) result
end
module FullPaytoHash : Hash_S = Hash_32
module NormalizedPaytoHash : Hash_S = Hash_32
module DenominationHash : Hash_S = Hash_64
module PrivateContractHash : Hash_S = Hash_64
module ExtensionsPolicyHash : Hash_S = Hash_64
module MerchantWireHash : Hash_S = Hash_64
module AgeCommitmentHash : Hash_S = Hash_64
module BlindedCoinHash : Hash_S = Hash_64
module CoinPubHash : Hash_S = Hash_64
module OutputCommitmentHash : Hash_S = Hash_64
module HashPlanchetsP : Hash_S = Hash_64