Skip to content

Commit

Permalink
remove hex from dependency cone
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesm committed Mar 11, 2024
1 parent fff44e5 commit 0d597bd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 0 additions & 1 deletion mirage-crypto-ec.opam
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ depends: [
"eqaf" {>= "0.7"}
"mirage-crypto-rng" {=version}
"digestif" {>= "1.1.4"}
"hex" {with-test}
"alcotest" {with-test & >= "0.8.1"}
"ppx_deriving_yojson" {with-test}
"ppx_deriving" {with-test}
Expand Down
2 changes: 1 addition & 1 deletion tests/wycheproof/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(library
(name wycheproof)
(libraries yojson ppx_deriving_yojson.runtime hex)
(libraries yojson ppx_deriving_yojson.runtime)
(preprocess
(pps ppx_deriving.std ppx_deriving_yojson))
(optional))
38 changes: 34 additions & 4 deletions tests/wycheproof/wycheproof.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,44 @@ let pp_json = Yojson.Safe.pretty_print

type hex = string [@@deriving eq]

let pp_hex fmt s =
let (`Hex h) = Hex.of_string s in
Format.pp_print_string fmt h
let pp_hex fmt buf =
let n = String.length buf in
let bbuf = Bytes.unsafe_of_string buf in
for i = n - 1 downto 0 do
let byte = Bytes.get_uint8 bbuf i in
Format.fprintf fmt "%02x" byte
done

let hex_of_string s =
let fold f acc str =
let st = ref acc in
String.iter (fun c -> st := f !st c) str;
!st
and digit c =
match c with
| '0'..'9' -> int_of_char c - 0x30
| 'A'..'F' -> int_of_char c - 0x41 + 10
| 'a'..'f' -> int_of_char c - 0x61 + 10
| _ -> invalid_arg "bad character"
in
let out = Bytes.create (String.length s / 2) in
let _idx, leftover =
fold (fun (idx, leftover) c ->
let c = digit c in
match leftover with
| None -> idx, Some (c lsl 4)
| Some c' ->
Bytes.set_uint8 out idx (c' lor c);
succ idx, None)
(0, None) s
in
assert (leftover = None);
Bytes.unsafe_to_string out

let hex_of_yojson json =
let padded s = if String.length s mod 2 = 0 then s else "0" ^ s in
match [%of_yojson: string] json with
| Ok s -> Ok (Hex.to_string (`Hex (padded s)))
| Ok s -> Ok (hex_of_string (padded s))
| Error _ as e -> e

type test_result = Valid | Acceptable | Invalid [@@deriving show]
Expand Down

0 comments on commit 0d597bd

Please sign in to comment.