From 0fef7fbd10b4efa704ffd78c6b4d3f936da060e3 Mon Sep 17 00:00:00 2001 From: swrup Date: Tue, 7 Oct 2025 12:02:16 +0200 Subject: [PATCH] wip purpose sizeof --- src/binary_formats.ml | 44 +++++++++++++++++++++++++++++++------------ test/test.ml | 11 +++++++++++ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/binary_formats.ml b/src/binary_formats.ml index fd9cafe2..d833311f 100644 --- a/src/binary_formats.ml +++ b/src/binary_formats.ml @@ -2,6 +2,8 @@ - numeric values are in network byte order (big endian) *) +open Include + (* TODO less boilerplate? - type t abstract @@ -55,27 +57,38 @@ module Hash_code = struct (* usually SHA-512 *) type t = { hash: string (* = uint8_t hash[64] *) } + let size = 64 + let bin = let open Bin in - record (fun hash -> { hash }) |+ field (bytes 64) (fun t -> t.hash) |> sealr + record (fun hash -> { hash }) + |+ field (bytes size) (fun t -> t.hash) + |> sealr end module Short_hash_code = struct type t = { hash: string } + let size = 32 + let bin = let open Bin in - record (fun hash -> { hash }) |+ field (bytes 32) (fun t -> t.hash) |> sealr + record (fun hash -> { hash }) + |+ field (bytes size) (fun t -> t.hash) + |> sealr end module MAKE_H (H : sig - type t + type t = { hash: string } + val size : int val bin : t Bin.t end) = struct type t = { hash: H.t } + let size = H.size + let bin = let open Bin in record (fun hash -> { hash }) |+ field H.bin (fun t -> t.hash) |> sealr @@ -261,7 +274,8 @@ module Token_public_key_p = MAKE_EDDSA_PUB () (* -- Signatures -- *) -module Ecc_signature_purpose = struct +(* EccSignaturePurpose *) +module Purpose = struct type t = { (* This field equals the number of bytes being signed, namely 'sizeof (struct Data)'. *) @@ -273,12 +287,16 @@ module Ecc_signature_purpose = struct purpose: int32; } + let size = 4 + 4 + let bin = let open Bin in record (fun size purpose -> { size; purpose }) |+ field beint32 (fun t -> t.size) |+ field beint32 (fun t -> t.purpose) |> sealr + + let make size purpose = { size= Int32.of_int size; purpose } end (* This is the running SHA512-hash over all @@ -405,7 +423,7 @@ module Withdraw_request_ps = struct type t = { (* TODO set it in encode/decode *) (* Purpose is #TALER_SIGNATURE_WALLET_RESERVE_WITHDRAW *) - purpose: Ecc_signature_purpose.t; + purpose: Purpose.t; (* Amount to withdraw, excluding fees, i.e. the total sum of the denominations of the coins. Note that the reserve must have a value of at least amount+fee. *) @@ -459,7 +477,7 @@ module Withdraw_request_ps = struct max_age_group; mask; }) - |+ field Ecc_signature_purpose.bin (fun t -> t.purpose) + |+ field Purpose.bin (fun t -> t.purpose) |+ field Amount.bin (fun t -> t.amount) |+ field Amount.bin (fun t -> t.fee) |+ field Hash_planchets_p.bin (fun t -> t.h_planchets) @@ -470,10 +488,9 @@ module Withdraw_request_ps = struct end module Withdraw_confirmation_ps = struct + (* Purpose is #TALER_SIGNATURE_EXCHANGE_CONFIRM_WITHDRAW. + Signed by a `struct TALER_ExchangePrivateKeyP` using EdDSA. *) type t = { - (* Purpose is #TALER_SIGNATURE_EXCHANGE_CONFIRM_WITHDRAW. - Signed by a `struct TALER_ExchangePrivateKeyP` using EdDSA. *) - purpose: Ecc_signature_purpose.t; (* Commitment made in the /withdraw request. Also needed for the /reveal-withdraw endpoint (in case of required proof of age restriction) and for /recoup *) @@ -489,11 +506,14 @@ module Withdraw_confirmation_ps = struct noreveal_index: int32; } + let size = Purpose.size + Hash_planchets_p.size + 4 + let purpose = Purpose.make size Taler_signatures.exchange_confirm_withdraw + let bin = let open Bin in - record (fun purpose h_planchets noreveal_index -> - { purpose; h_planchets; noreveal_index }) - |+ field Ecc_signature_purpose.bin (fun t -> t.purpose) + record (fun _purpose h_planchets noreveal_index -> + { h_planchets; noreveal_index }) + |+ field Purpose.bin (Fun.const purpose) |+ field Hash_planchets_p.bin (fun t -> t.h_planchets) |+ field beint32 (fun t -> t.noreveal_index) |> sealr diff --git a/test/test.ml b/test/test.ml index dac934bc..bc491f9b 100644 --- a/test/test.ml +++ b/test/test.ml @@ -71,3 +71,14 @@ let () = check_bad "\"foo\" ,"; check_bad ", \"foo\""; () + +let () = + let open Binary_formats.Withdraw_confirmation_ps in + let str64 = String.make 64 '0' in + let dummy_t = + { h_planchets= { hash= { hash= str64 } }; noreveal_index= 0_l } + in + let size' = Bin.size_of_value bin dummy_t |> Option.get in + assert (size = size'); + assert (size = 76); + ()