add binary_formats.ml

This commit is contained in:
swrup 2025-10-06 23:56:10 +02:00
parent b7b28af6fe
commit 45a1027542
4 changed files with 76 additions and 24 deletions

View file

@ -42,7 +42,9 @@ let () =
let l = parse Data.content in
assert (List.length l = 74);
let pp ppf { name; value } = Fmt.pf ppf "let %s = %d@." name value in
let pp ppf { name; value } =
Fmt.pf ppf "let %s : int32 = %d_l@." name value
in
Fmt.pr "%a" (Fmt.list pp) l;
()

72
src/binary_formats.ml Normal file
View file

@ -0,0 +1,72 @@
(* https://docs.taler.net/core/api-common.html#binary-formats
- numeric values are in network byte order (big endian) *)
module Time = struct
module Absolute = struct
type t = { timestamp_us: int64 }
type t_nbo = { abs_value_us__: int64 }
let bin =
let open Bin in
record (fun timestamp_us -> { timestamp_us })
|+ field neint64 (fun t -> t.timestamp_us) (* not BE here? *)
|> sealr
let nbo_bin =
let open Bin in
record (fun abs_value_us__ -> { abs_value_us__ })
|+ field beint64 (fun t -> t.abs_value_us__)
|> sealr
end
module Relative = struct
type t = { timestamp_us: int64 }
type t_nbo = { rel_value_us__: int64 }
let bin =
let open Bin in
record (fun timestamp_us -> { timestamp_us })
|+ field neint64 (fun t -> t.timestamp_us) (* not BE here? *)
|> sealr
let nbo_bin =
let open Bin in
record (fun rel_value_us__ -> { rel_value_us__ })
|+ field beint64 (fun t -> t.rel_value_us__)
|> sealr
end
end
module Hash_code = struct
(* usually SHA-512 *)
type t = { hash: int8 array (* = uint8_t hash[64] *) }
end
module Purpose = struct
(* defined in gnunet_crypto_lib.h *)
type t = {
(* This field equals the number of bytes being signed,
namely 'sizeof (struct Data)'. *)
size: int32; (* = uint32_t *)
(* This field is used to express the context in
which the signature is made, ensuring that a
signature cannot be lifted from one part of the protocol
to another. *)
purpose: int32; (* = uint32_t *)
}
let t_bin =
let open Bin in
record (fun size purpose -> { size; purpose })
|+ field beint32 (fun t -> t.size)
|+ field beint32 (fun t -> t.purpose)
|> sealr
end
module Data = struct
type t = {
purpose: Purpose.t;
payloads: string list;
}
end

View file

@ -13,6 +13,7 @@
;
include
;
bin
angstrom
zarith ;
mirage-crypto

View file

@ -1,23 +0,0 @@
(* https://docs.taler.net/core/api-common.html#id7 *)
module Purpose = struct
(* defined in gnunet_crypto_lib.h *)
type t = {
(* This field equals the number of bytes being signed,
namely 'sizeof (struct Data)'. *)
size: Int32.t; (* = uint32_t *)
(* This field is used to express the context in
which the signature is made, ensuring that a
signature cannot be lifted from one part of the protocol
to another. See `src/include/taler/taler_signatures.h` within the
exchange's codebase (git://taler.net/exchange). *)
purpose: Int32.t; (* = uint32_t *)
}
end
module Data = struct
type t = {
purpose: Purpose.t;
payloads: string list;
}
end