This commit is contained in:
swrup 2025-10-18 00:11:07 +02:00
parent fa25608428
commit 8e08e2d69b

View file

@ -85,12 +85,12 @@ module UTIL = struct
module MK_HASH_32 () : sig module MK_HASH_32 () : sig
type t = private { hash: string } type t = private { hash: string }
val of_string : string -> t val of_octets : string -> t
val bin : t Bin.t val bin : t Bin.t
end = struct end = struct
type t = { hash: string } type t = { hash: string }
let of_string s = let of_octets s =
match String.length s = 32 with match String.length s = 32 with
| false -> Fmt.failwith "SHA256 failure: data is not 32 bytes" | false -> Fmt.failwith "SHA256 failure: data is not 32 bytes"
| true -> | true ->
@ -125,6 +125,58 @@ module UTIL = struct
|+ field (bytes 64) (fun t -> t.hash) |+ field (bytes 64) (fun t -> t.hash)
|> sealr |> sealr
end end
module HASH_32_WITH_NULL : sig
type t
val hash_with_null : string -> t
val bin : t Bin.t
end = struct
type t = { hash: string }
let hash_with_null s =
let s =
let len = String.length s in
let b = Bytes.create (len + 1) in
Bytes.blit_string s 0 b 0 len;
Bytes.set b len '\x00';
Bytes.unsafe_to_string b
in
let hash = Digestif.SHA256.(to_raw_string (digest_string s)) in
{ hash }
let bin =
let open Bin in
record (fun hash -> { hash })
|+ field (bytes 32) (fun t -> t.hash)
|> sealr
end
module HASH_64_WITH_NULL : sig
type t
val hash_with_null : string -> t
val bin : t Bin.t
end = struct
type t = { hash: string }
let hash_with_null s =
let s =
let len = String.length s in
let b = Bytes.create (len + 1) in
Bytes.blit_string s 0 b 0 len;
Bytes.set b len '\x00';
Bytes.unsafe_to_string b
in
let hash = Digestif.SHA512.(to_raw_string (digest_string s)) in
{ hash }
let bin =
let open Bin in
record (fun hash -> { hash })
|+ field (bytes 64) (fun t -> t.hash)
|> sealr
end
end end
open UTIL open UTIL
@ -141,6 +193,76 @@ module TimestampNBO = MK_TIME_NBO ()
(* -- Cryptographic primitives -- *) (* -- Cryptographic primitives -- *)
(* GNUNET_CRYPTO format *)
module GNUNET_RsaPublicKey = struct
(* libgnuutil format:
https://docs.gnunet.org/doxygen/d9/dbe/structGNUNET__CRYPTO__RsaPublicKeyHeaderP.html
https://docs.gnunet.org/doxygen/d6/d70/group__libgnunetutil.html#ga9c99a81e8cd649c1c925d23211a0738f
https://www.gnupg.org/documentation/manuals/gcrypt/MPI-formats.html
format:
- rsa header
- modulus
- public_exponent
integer in big-endian format (MSB first).
Leading zeroes are stripped unless they are required to keep a value positive.
*)
type header = {
n_len: int;
e_len: int;
}
type t = {
header: header;
n: Z.t;
e: Z.t;
}
(* need to strip leading zeros or something? *)
let z_to_bigendian_bits v =
let s = Z.to_bits v in
let len = String.length s in
let s = String.init len (fun i -> s.[len - 1 - i]) in
s
let header_bin =
let open Bin in
record (fun n_len e_len -> { n_len; e_len })
|+ field beint16 (fun t -> t.n_len)
|+ field beint16 (fun t -> t.e_len)
|> sealr
(* note: this one has a dynamic sizeof *)
let bin =
let open Bin in
record (fun header n e ->
let n = Z.of_bits n in
let e = Z.of_bits e in
{ header; n; e })
|+ field header_bin (fun t -> t.header)
(* TODO
Z.to_bits is in little endian but we need it in big endian *)
|+ field cstring (fun t -> z_to_bigendian_bits t.n)
|+ field cstring (fun t -> z_to_bigendian_bits t.e)
|> sealr
let of_pub ({ n; e } : Mirage_crypto_pk.Rsa.pub) =
let header = { n_len= Z.size n; e_len= Z.size e } in
let t = { header; n; e } in
t
end
module DenominationHash = struct
include HASH_64_WITH_NULL
let make (pub : Mirage_crypto_pk.Rsa.pub) =
pub
|> GNUNET_RsaPublicKey.of_pub
|> Bin.to_string GNUNET_RsaPublicKey.bin
|> hash_with_null
end
(* --- Hashes --- *) (* --- Hashes --- *)
module ShortHashCode = MK_HASH_32 () module ShortHashCode = MK_HASH_32 ()
@ -153,7 +275,6 @@ module FullPaytoHash = MK_HASH_32 ()
(* Hash over a normalized payto://-URI, including all optional (* Hash over a normalized payto://-URI, including all optional
fields and also with account-part canonicalized (so no BIC). *) fields and also with account-part canonicalized (so no BIC). *)
module NormalizedPaytoHash = MK_HASH_32 () module NormalizedPaytoHash = MK_HASH_32 ()
module DenominationHash = MK_HASH_64 ()
module PrivateContractHash = MK_HASH_64 () module PrivateContractHash = MK_HASH_64 ()
module ExtensionsPolicyHash = MK_HASH_64 () module ExtensionsPolicyHash = MK_HASH_64 ()
module MerchantWireHash = MK_HASH_64 () module MerchantWireHash = MK_HASH_64 ()
@ -217,61 +338,6 @@ module EncryptedLinkSecretP = MK_64 ()
type DenominationBlindingKeyP = string; *) type DenominationBlindingKeyP = string; *)
module DenominationBlindingKeyP = MK_32 () module DenominationBlindingKeyP = MK_32 ()
(* GNUNET_CRYPTO format *)
module GNUNET_RsaPublicKey = struct
(* libgnuutil format:
https://docs.gnunet.org/doxygen/d9/dbe/structGNUNET__CRYPTO__RsaPublicKeyHeaderP.html
https://docs.gnunet.org/doxygen/d6/d70/group__libgnunetutil.html#ga9c99a81e8cd649c1c925d23211a0738f
https://www.gnupg.org/documentation/manuals/gcrypt/MPI-formats.html
format:
- rsa header
- modulus
- public_exponent
integer in big-endian format (MSB first).
Leading zeroes are stripped unless they are required to keep a value positive.
*)
type header = {
n_len: int;
e_len: int;
}
type t = {
header: header;
n: Z.t;
e: Z.t;
}
(* need to strip leading zeros or something? *)
let z_to_bigendian_bits v =
let s = Z.to_bits v in
let len = String.length s in
let s = String.init len (fun i -> s.[len - 1 - i]) in
s
let header_bin =
let open Bin in
record (fun n_len e_len -> { n_len; e_len })
|+ field beint16 (fun t -> t.n_len)
|+ field beint16 (fun t -> t.e_len)
|> sealr
(* note: this one has a dynamic sizeof *)
let bin =
let open Bin in
record (fun header n e ->
let n = Z.of_bits n in
let e = Z.of_bits e in
{ header; n; e })
|+ field header_bin (fun t -> t.header)
(* TODO
Z.to_bits is in little endian but we need it in big endian *)
|+ field cstring (fun t -> z_to_bigendian_bits t.n)
|+ field cstring (fun t -> z_to_bigendian_bits t.e)
|> sealr
end
(* --- Various --- *) (* --- Various --- *)
module RefreshCommitmentP = MK_64 () module RefreshCommitmentP = MK_64 ()