Skip to content
This repository has been archived by the owner on Apr 23, 2021. It is now read-only.

Commit

Permalink
Add a Testable submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
emillon committed Jul 23, 2019
1 parent 6fb66f1 commit ee41157
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions test/unit/test_p256.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
module Testable = struct
let ok_or_error =
let pp ppf = function
| Ok () -> Format.fprintf ppf "ok"
| Error e -> Format.fprintf ppf "%a" Fiat_p256.pp_error e
in
let eq ra rb =
match (ra, rb) with
| Ok (), Ok () -> true
| Error (ea : Fiat_p256.error), Error eb -> ea = eb
| Ok _, Error _ -> false
| Error _, Ok _ -> false
in
Alcotest.testable pp eq
end

let key_pair_of_hex h = Fiat_p256.gen_key ~rng:(fun _ -> Hex.to_cstruct h)

let scalar_of_hex h = fst (key_pair_of_hex h)
Expand Down Expand Up @@ -109,23 +125,11 @@ let scalar_mult =
~expected:
"48e82c9b82c88cb9fc2a5cff9e7c41bc4255ff6bd3814538c9b130877c07e4cf" ]

let to_ok_or_error = function
| Ok _ -> Ok ()
| Error _ as e -> e

let point_validation =
let pp ppf = function
| Ok () -> Format.fprintf ppf "ok"
| Error e -> Format.fprintf ppf "%a" Fiat_p256.pp_error e
in
let eq ra rb =
match (ra, rb) with
| Ok (), Ok () -> true
| Error (ea : Fiat_p256.error), Error eb -> ea = eb
| Ok _, Error _ -> false
| Error _, Ok _ -> false
in
let to_result = function
| Ok _ -> Ok ()
| Error _ as e -> e
in
let testable = Alcotest.testable pp eq in
let test ~name ~x ~y ~expected =
let scalar =
scalar_of_hex
Expand All @@ -139,8 +143,8 @@ let point_validation =
, `Quick
, fun () ->
Fiat_p256.key_exchange scalar point
|> to_result
|> Alcotest.check testable __LOC__ expected )
|> to_ok_or_error
|> Alcotest.check Testable.ok_or_error __LOC__ expected )
in
let zero = `Hex (String.make 64 '0') in
let sb =
Expand Down

0 comments on commit ee41157

Please sign in to comment.