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

Commit

Permalink
Remove ppx_expect dependency (#43)
Browse files Browse the repository at this point in the history
ppx_expect is in theory usable in Mirage since it strips the code in the released binaries; but for now it causes some cross-compilation issues because of C stubs that rely on POSIX APIs. So for now let's do without it!

See janestreet/ppx_expect#15
  • Loading branch information
emillon authored Jul 23, 2019
1 parent b38f39b commit a203800
Show file tree
Hide file tree
Showing 25 changed files with 247 additions and 364 deletions.
2 changes: 0 additions & 2 deletions fiat-p256.opam
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ depends: [
"cstruct" {>= "3.5.0"}
"dune" {build & >= "1.6.0"}
"hex"
"hex" {with-test}
"ppx_deriving_yojson" {with-test}
"rresult" {with-test}
"ppx_expect"
"yojson" {with-test & >= "1.6.0"}
]
synopsis: "Primitives for Elliptic Curve Cryptography taken from Fiat"
Expand Down
19 changes: 0 additions & 19 deletions p256/cstruct_util.ml
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
let pp_hex_le fmt cs =
let n = Cstruct.len cs in
for i = n - 1 downto 0 do
let byte = Cstruct.get_uint8 cs i in
Format.fprintf fmt "%02x" byte
done

let compare_be a b =
let first_diff = ref None in
let a_len = Cstruct.len a in
Expand All @@ -24,15 +17,3 @@ let compare_be a b =
0
| Some d ->
d

let%expect_test "compare_be" =
let test a b = print_int (compare_be a b) in
test (Cstruct.of_string "aa") (Cstruct.of_string "ab");
[%expect {| -1 |}];
test (Cstruct.of_string "ab") (Cstruct.of_string "aa");
[%expect {| 1 |}];
test (Cstruct.of_string "aa") (Cstruct.of_string "aa");
[%expect {| 0 |}];
test (Cstruct.of_string "abx") (Cstruct.of_string "aaz");
[%expect {| 1 |}];
()
4 changes: 0 additions & 4 deletions p256/cstruct_util.mli
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
val pp_hex_le : Format.formatter -> Cstruct.t -> unit
(** Display the contents of a cstruct as hex data, seen as a little endian
number. *)

val compare_be : Cstruct.t -> Cstruct.t -> int
(** Compare two cstructs, interpreting them as big endian numbers.
Raises [Invalid_argument _] if they have a different length. *)
2 changes: 0 additions & 2 deletions p256/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
(libraries bigarray-compat cstruct hex)
(c_names p256_stubs)
(c_flags (:include discover/cflags.sexp))
(preprocess (pps ppx_expect))
(inline_tests)
)

(rule
Expand Down
36 changes: 2 additions & 34 deletions p256/fiat_p256.ml
Original file line number Diff line number Diff line change
@@ -1,38 +1,7 @@
let dh ~scalar ~point =
Point.x_of_finite_point (Montgomery_ladder.scalar_mult scalar point)

let base_point = Point.of_hex_exn Parameters.g

let public scalar = Montgomery_ladder.scalar_mult scalar base_point

let%expect_test "dh" =
let test d p =
Format.printf "%a\n" Cstruct_util.pp_hex_le (dh ~scalar:d ~point:p)
in
let d_a =
Scalar.of_hex_exn
(`Hex
"200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
in
let d_b =
Scalar.of_hex_exn
(`Hex
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
in
let p_a = public d_a in
let p_b = public d_b in
test d_b p_a;
[%expect
{| 2e3e4065a62a7f425aaf8aae3d158f367c733300b5002e0b62f4bc6260789e1b |}];
test d_a p_b;
[%expect
{| 2e3e4065a62a7f425aaf8aae3d158f367c733300b5002e0b62f4bc6260789e1b |}];
test d_a p_a;
[%expect
{| 2ea4e810837da217a5bfd05f01d12459eeda830b6e0dec7f8afa425c5b55c507 |}];
test d_b p_b;
[%expect
{| a7666bcc3818472194460f7df22d80a5886da0e1679eac930175ce1ff733c7ca |}]
let public scalar = Montgomery_ladder.scalar_mult scalar Point.params_g

type error = Error.point_error

Expand Down Expand Up @@ -73,7 +42,6 @@ let gen_key ~rng =

let key_exchange secret received =
match point_of_cs received with
| Error _ as err ->
err
| Error _ as err -> err
| Ok other_party_public_key ->
Ok (dh ~scalar:secret ~point:other_party_public_key)
60 changes: 0 additions & 60 deletions p256/montgomery_ladder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,3 @@ let scalar_mult d p =
r0 := Point.double !r0 )
done;
!r0

let%expect_test "scalar mult" =
let test ~scalar ~point =
let scalar = Scalar.of_hex_exn scalar in
let point = Point.of_hex_exn point in
let res = scalar_mult scalar point in
Format.printf "%a\n" Point.pp res
in
let point =
`Hex
"046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
in
test
~scalar:
(`Hex
"0000000000000000000000000000000000000000000000000000000000000001")
~point;
[%expect
{| 046b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c2964fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5 |}];
test
~scalar:
(`Hex
"0000000000000000000000000000000000000000000000000000000000000002")
~point;
[%expect
{| 047cf27b188d034f7e8a52380304b51ac3c08969e277f21b35a60b48fc4766997807775510db8ed040293d9ac69f7430dbba7dade63ce982299e04b79d227873d1 |}];
test
~scalar:
(`Hex
"0000000000000000000000000000000000000000000000000000000000000004")
~point;
[%expect
{| 04e2534a3532d08fbba02dde659ee62bd0031fe2db785596ef509302446b030852e0f1575a4c633cc719dfee5fda862d764efc96c3f30ee0055c42c23f184ed8c6 |}];
test
~scalar:
(`Hex
"0612465c89a023ab17855b0a6bcebfd3febb53aef84138647b5352e02c10c346")
~point:
(`Hex
"0462d5bd3372af75fe85a040715d0f502428e07046868b0bfdfa61d731afe44f26ac333a93a9e70a81cd5a95b5bf8d13990eb741c8c38872b4a07d275a014e30cf");
[%expect
{| 0453020d908b0219328b658b525f26780e3ae12bcd952bb25a93bc0895e1714285b2ba871dd1652c3f467df15c6b70647efbcbbab5cbf7f55e6ff336f843d628a1 |}];
test
~scalar:
(`Hex
"0a0d622a47e48f6bc1038ace438c6f528aa00ad2bd1da5f13ee46bf5f633d71a")
~point:
(`Hex
"043cbc1b31b43f17dc200dd70c2944c04c6cb1b082820c234a300b05b7763844c74fde0a4ef93887469793270eb2ff148287da9265b0334f9e2609aac16e8ad503");
[%expect
{| 047fffffffffffffffffffffffeecf2230ffffffffffffffffffffffffffffffff00000001c7c30643abed0af0a49fe352cb483ff9b97dccdf427c658e8793240d |}];
test
~scalar:
(`Hex
"55d55f11bb8da1ea318bca7266f0376662441ea87270aa2077f1b770c4854a48")
~point:
(`Hex
"04000000000000000000000000000000000000000000000000000000000000000066485c780e2f83d72433bd5d84a06bb6541c2af31dae871728bf856a174f93f4");
[%expect
{| 04cfe4077c8730b1c9384581d36bff5542bc417c9eff5c2afcb98cc8829b2ce8487764c65671a66a3ecf1ec63cf49b5c36119162ace73f8d8be270e27cdaf4677c |}]
8 changes: 5 additions & 3 deletions p256/parameters.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ let a =
let b =
`Hex "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"

let g =
`Hex
"046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
let g_x =
`Hex "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"

let g_y =
`Hex "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"

let p =
`Hex "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
Expand Down
7 changes: 5 additions & 2 deletions p256/parameters.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ val a : Hex.t

val b : Hex.t

val g : Hex.t
(** The base point, in uncompressed form. *)
val g_x : Hex.t
(** The base point's x coordinate. *)

val g_y : Hex.t
(** The base point's y coordinate. *)

val p : Hex.t
(** The prime number corresponding to [Fe]. *)
Expand Down
Loading

0 comments on commit a203800

Please sign in to comment.