forked from
gazagnaire.org/ocaml-crypto
upstream: https://github.com/mirage/mirage-crypto
1open OUnit2
2open Crypto.Uncommon
3open Crypto_pk
4open Test_common
5
6let n_encode_decode_selftest ~typ ~bound n =
7 typ ^ "selftest"
8 >:: times ~n @@ fun _ ->
9 let r = Z_extra.gen bound in
10 let s = Z_extra.(of_octets_be @@ to_octets_be r)
11 and t = Z_extra.(of_octets_be @@ to_octets_be ~size:24 r) in
12 assert_equal r s;
13 assert_equal r t
14
15let n_decode_reencode_selftest ~typ ~bytes n =
16 typ ^ " selftest"
17 >:: times ~n @@ fun _ ->
18 let cs = Crypto_rng.generate bytes in
19 let cs' = Z_extra.(to_octets_be ~size:bytes @@ of_octets_be cs) in
20 assert_oct_equal cs cs'
21
22let random_n_selftest ~typ n bounds =
23 typ ^ " selftest"
24 >::: (bounds
25 |> List.map @@ fun (lo, hi) ->
26 "selftest"
27 >:: times ~n @@ fun _ ->
28 let x = Z_extra.gen_r lo hi in
29 if x < lo || x >= hi then assert_failure "range error")
30
31let int_safe_bytes = (Sys.word_size // 8) - 1
32
33let suite =
34 [
35 "Numeric extraction 1"
36 >::: [
37 n_encode_decode_selftest ~typ:"z"
38 ~bound:Z.(of_int64 Int64.max_int)
39 2000;
40 ];
41 "Numeric extraction 2"
42 >::: [ n_decode_reencode_selftest ~typ:"z" ~bytes:37 2000 ];
43 "RNG extraction"
44 >::: [
45 random_n_selftest ~typ:"Z" 1000
46 [
47 Z.(of_int 7, of_int 135);
48 Z.(of_int 0, of_int 536870913);
49 Z.(of_int 0, of_int64 2305843009213693953L);
50 ];
51 ];
52 ]