upstream: https://github.com/mirage/mirage-crypto
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

wycheproof: let open Json.Codec in cleanup

68 Json.Codec. prefixes removed across 10 codec defs in the test
data parser. None of the wycheproof records have fields that clash
with Json.Codec.mem_map, so no annotations needed.

+78 -68
+78 -68
test/ec/wycheproof/wycheproof.ml
··· 17 17 let hex_of_string s = Ohex.decode s 18 18 19 19 let hex_jsont = 20 + let open Json.Codec in 20 21 let padded s = if String.length s mod 2 = 0 then s else "0" ^ s in 21 - Json.Codec.map ~kind:"hex" 22 + map ~kind:"hex" 22 23 ~dec:(fun s -> hex_of_string (padded s)) 23 24 ~enc:(fun h -> Ohex.encode h) 24 - Json.Codec.string 25 + string 25 26 26 27 type test_result = Valid | Acceptable | Invalid 27 28 ··· 36 37 | Invalid -> "Invalid" 37 38 38 39 let test_result_jsont = 39 - Json.Codec.map ~kind:"test_result" 40 + let open Json.Codec in 41 + map ~kind:"test_result" 40 42 ~dec:(function 41 43 | "valid" -> Valid 42 44 | "acceptable" -> Acceptable ··· 44 46 | _ -> failwith "test_result: expected valid, acceptable, or invalid") 45 47 ~enc:(function 46 48 | Valid -> "valid" | Acceptable -> "acceptable" | Invalid -> "invalid") 47 - Json.Codec.string 49 + string 48 50 49 51 type ecdh_test = { 50 52 tc_id : int; ··· 70 72 let show_ecdh_test t = Fmt.str "%a" pp_ecdh_test t 71 73 72 74 let ecdh_test_jsont = 73 - Json.Codec.Object.map ~kind:"ecdh_test" 75 + let open Json.Codec in 76 + Object.map ~kind:"ecdh_test" 74 77 (fun tc_id comment curve public private_ shared result flags -> 75 78 { 76 79 tc_id; ··· 82 85 result; 83 86 flags = Option.value ~default:[] flags; 84 87 }) 85 - |> Json.Codec.Object.mem "tcId" Json.Codec.int ~enc:(fun t -> t.tc_id) 86 - |> Json.Codec.Object.mem "comment" Json.Codec.string ~enc:(fun t -> t.comment) 87 - |> Json.Codec.Object.opt_mem "curve" Json.Codec.Value.t ~enc:(fun t -> 88 + |> Object.mem "tcId" int ~enc:(fun t -> t.tc_id) 89 + |> Object.mem "comment" string ~enc:(fun t -> t.comment) 90 + |> Object.opt_mem "curve" Value.t ~enc:(fun t -> 88 91 t.curve) 89 - |> Json.Codec.Object.mem "public" hex_jsont ~enc:(fun t -> t.public) 90 - |> Json.Codec.Object.mem "private" hex_jsont ~enc:(fun t -> t.private_) 91 - |> Json.Codec.Object.mem "shared" hex_jsont ~enc:(fun t -> t.shared) 92 - |> Json.Codec.Object.mem "result" test_result_jsont ~enc:(fun t -> t.result) 93 - |> Json.Codec.Object.opt_mem "flags" (Json.Codec.list Json.Codec.string) 92 + |> Object.mem "public" hex_jsont ~enc:(fun t -> t.public) 93 + |> Object.mem "private" hex_jsont ~enc:(fun t -> t.private_) 94 + |> Object.mem "shared" hex_jsont ~enc:(fun t -> t.shared) 95 + |> Object.mem "result" test_result_jsont ~enc:(fun t -> t.result) 96 + |> Object.opt_mem "flags" (list string) 94 97 ~enc:(fun t -> if t.flags = [] then None else Some t.flags) 95 - |> Json.Codec.Object.skip_unknown |> Json.Codec.Object.finish 98 + |> Object.skip_unknown |> Object.finish 96 99 97 100 let has_ignored_flag test ~ignored_flags = 98 101 List.exists ··· 117 120 let show_ecdh_test_group t = Fmt.str "%a" pp_ecdh_test_group t 118 121 119 122 let ecdh_test_group_jsont = 120 - Json.Codec.Object.map ~kind:"ecdh_test_group" 123 + let open Json.Codec in 124 + Object.map ~kind:"ecdh_test_group" 121 125 (fun curve tests encoding type_ -> { curve; tests; encoding; type_ }) 122 - |> Json.Codec.Object.mem "curve" Json.Codec.string ~enc:(fun t -> t.curve) 123 - |> Json.Codec.Object.mem "tests" (Json.Codec.list ecdh_test_jsont) 126 + |> Object.mem "curve" string ~enc:(fun t -> t.curve) 127 + |> Object.mem "tests" (list ecdh_test_jsont) 124 128 ~enc:(fun t -> t.tests) 125 - |> Json.Codec.Object.opt_mem "encoding" Json.Codec.Value.t ~enc:(fun t -> 129 + |> Object.opt_mem "encoding" Value.t ~enc:(fun t -> 126 130 t.encoding) 127 - |> Json.Codec.Object.opt_mem "type" Json.Codec.Value.t ~enc:(fun t -> t.type_) 128 - |> Json.Codec.Object.skip_unknown |> Json.Codec.Object.finish 131 + |> Object.opt_mem "type" Value.t ~enc:(fun t -> t.type_) 132 + |> Object.skip_unknown |> Object.finish 129 133 130 134 type ecdsa_key = { 131 135 curve : string; ··· 146 150 let show_ecdsa_key t = Fmt.str "%a" pp_ecdsa_key t 147 151 148 152 let ecdsa_key_jsont = 149 - Json.Codec.Object.map ~kind:"ecdsa_key" 153 + let open Json.Codec in 154 + Object.map ~kind:"ecdsa_key" 150 155 (fun curve key_size type_ uncompressed wx wy -> 151 156 { curve; key_size; type_; uncompressed; wx; wy }) 152 - |> Json.Codec.Object.mem "curve" Json.Codec.string ~enc:(fun t -> t.curve) 153 - |> Json.Codec.Object.mem "keySize" Json.Codec.int ~enc:(fun t -> t.key_size) 154 - |> Json.Codec.Object.mem "type" Json.Codec.Value.t ~enc:(fun t -> t.type_) 155 - |> Json.Codec.Object.mem "uncompressed" hex_jsont ~enc:(fun t -> 157 + |> Object.mem "curve" string ~enc:(fun t -> t.curve) 158 + |> Object.mem "keySize" int ~enc:(fun t -> t.key_size) 159 + |> Object.mem "type" Value.t ~enc:(fun t -> t.type_) 160 + |> Object.mem "uncompressed" hex_jsont ~enc:(fun t -> 156 161 t.uncompressed) 157 - |> Json.Codec.Object.mem "wx" hex_jsont ~enc:(fun t -> t.wx) 158 - |> Json.Codec.Object.mem "wy" hex_jsont ~enc:(fun t -> t.wy) 159 - |> Json.Codec.Object.skip_unknown |> Json.Codec.Object.finish 162 + |> Object.mem "wx" hex_jsont ~enc:(fun t -> t.wx) 163 + |> Object.mem "wy" hex_jsont ~enc:(fun t -> t.wy) 164 + |> Object.skip_unknown |> Object.finish 160 165 161 166 type dsa_test = { 162 167 tc_id : int; ··· 177 182 let show_dsa_test t = Fmt.str "%a" pp_dsa_test t 178 183 179 184 let dsa_test_jsont = 180 - Json.Codec.Object.map ~kind:"dsa_test" 185 + let open Json.Codec in 186 + Object.map ~kind:"dsa_test" 181 187 (fun tc_id comment msg sig_ result flags -> 182 188 { 183 189 tc_id; ··· 187 193 result; 188 194 flags = Option.value ~default:[] flags; 189 195 }) 190 - |> Json.Codec.Object.mem "tcId" Json.Codec.int ~enc:(fun t -> t.tc_id) 191 - |> Json.Codec.Object.mem "comment" Json.Codec.string ~enc:(fun t -> t.comment) 192 - |> Json.Codec.Object.mem "msg" hex_jsont ~enc:(fun t -> t.msg) 193 - |> Json.Codec.Object.mem "sig" hex_jsont ~enc:(fun t -> t.sig_) 194 - |> Json.Codec.Object.mem "result" test_result_jsont ~enc:(fun t -> t.result) 195 - |> Json.Codec.Object.opt_mem "flags" (Json.Codec.list Json.Codec.string) 196 + |> Object.mem "tcId" int ~enc:(fun t -> t.tc_id) 197 + |> Object.mem "comment" string ~enc:(fun t -> t.comment) 198 + |> Object.mem "msg" hex_jsont ~enc:(fun t -> t.msg) 199 + |> Object.mem "sig" hex_jsont ~enc:(fun t -> t.sig_) 200 + |> Object.mem "result" test_result_jsont ~enc:(fun t -> t.result) 201 + |> Object.opt_mem "flags" (list string) 196 202 ~enc:(fun t -> if t.flags = [] then None else Some t.flags) 197 - |> Json.Codec.Object.skip_unknown |> Json.Codec.Object.finish 203 + |> Object.skip_unknown |> Object.finish 198 204 199 205 type ecdsa_test_group = { 200 206 key : ecdsa_key; ··· 216 222 let show_ecdsa_test_group t = Fmt.str "%a" pp_ecdsa_test_group t 217 223 218 224 let ecdsa_test_group_jsont = 219 - Json.Codec.Object.map ~kind:"ecdsa_test_group" 225 + let open Json.Codec in 226 + Object.map ~kind:"ecdsa_test_group" 220 227 (fun key key_der key_pem sha tests type_ -> 221 228 { key; key_der; key_pem; sha; tests; type_ }) 222 - |> Json.Codec.Object.mem "key" ecdsa_key_jsont ~enc:(fun t -> t.key) 223 - |> Json.Codec.Object.mem "keyDer" Json.Codec.string ~enc:(fun t -> t.key_der) 224 - |> Json.Codec.Object.mem "keyPem" Json.Codec.string ~enc:(fun t -> t.key_pem) 225 - |> Json.Codec.Object.mem "sha" Json.Codec.string ~enc:(fun t -> t.sha) 226 - |> Json.Codec.Object.mem "tests" (Json.Codec.list dsa_test_jsont) 229 + |> Object.mem "key" ecdsa_key_jsont ~enc:(fun t -> t.key) 230 + |> Object.mem "keyDer" string ~enc:(fun t -> t.key_der) 231 + |> Object.mem "keyPem" string ~enc:(fun t -> t.key_pem) 232 + |> Object.mem "sha" string ~enc:(fun t -> t.sha) 233 + |> Object.mem "tests" (list dsa_test_jsont) 227 234 ~enc:(fun t -> t.tests) 228 - |> Json.Codec.Object.opt_mem "type" Json.Codec.Value.t ~enc:(fun t -> t.type_) 229 - |> Json.Codec.Object.skip_unknown |> Json.Codec.Object.finish 235 + |> Object.opt_mem "type" Value.t ~enc:(fun t -> t.type_) 236 + |> Object.skip_unknown |> Object.finish 230 237 231 238 type eddsa_key = { 232 239 curve : string; ··· 243 250 let show_eddsa_key t = Fmt.str "%a" pp_eddsa_key t 244 251 245 252 let eddsa_key_jsont = 246 - Json.Codec.Object.map ~kind:"eddsa_key" (fun curve key_size pk sk type_ -> 253 + let open Json.Codec in 254 + Object.map ~kind:"eddsa_key" (fun curve key_size pk sk type_ -> 247 255 { curve; key_size; pk; sk; type_ }) 248 - |> Json.Codec.Object.mem "curve" Json.Codec.string ~enc:(fun t -> t.curve) 249 - |> Json.Codec.Object.mem "keySize" Json.Codec.int ~enc:(fun t -> t.key_size) 250 - |> Json.Codec.Object.mem "pk" hex_jsont ~enc:(fun t -> t.pk) 251 - |> Json.Codec.Object.mem "sk" hex_jsont ~enc:(fun t -> t.sk) 252 - |> Json.Codec.Object.mem "type" Json.Codec.Value.t ~enc:(fun t -> t.type_) 253 - |> Json.Codec.Object.skip_unknown |> Json.Codec.Object.finish 256 + |> Object.mem "curve" string ~enc:(fun t -> t.curve) 257 + |> Object.mem "keySize" int ~enc:(fun t -> t.key_size) 258 + |> Object.mem "pk" hex_jsont ~enc:(fun t -> t.pk) 259 + |> Object.mem "sk" hex_jsont ~enc:(fun t -> t.sk) 260 + |> Object.mem "type" Value.t ~enc:(fun t -> t.type_) 261 + |> Object.skip_unknown |> Object.finish 254 262 255 263 type eddsa_test_group = { 256 264 jwk : json; ··· 271 279 let show_eddsa_test_group t = Fmt.str "%a" pp_eddsa_test_group t 272 280 273 281 let eddsa_test_group_jsont = 274 - Json.Codec.Object.map ~kind:"eddsa_test_group" 282 + let open Json.Codec in 283 + Object.map ~kind:"eddsa_test_group" 275 284 (fun jwk key key_der key_pem type_ tests -> 276 285 { jwk; key; key_der; key_pem; type_; tests }) 277 - |> Json.Codec.Object.mem "jwk" Json.Codec.Value.t ~enc:(fun t -> t.jwk) 278 - |> Json.Codec.Object.mem "key" eddsa_key_jsont ~enc:(fun t -> t.key) 279 - |> Json.Codec.Object.mem "keyDer" Json.Codec.string ~enc:(fun t -> t.key_der) 280 - |> Json.Codec.Object.mem "keyPem" Json.Codec.string ~enc:(fun t -> t.key_pem) 281 - |> Json.Codec.Object.mem "type" Json.Codec.Value.t ~enc:(fun t -> t.type_) 282 - |> Json.Codec.Object.mem "tests" (Json.Codec.list dsa_test_jsont) 286 + |> Object.mem "jwk" Value.t ~enc:(fun t -> t.jwk) 287 + |> Object.mem "key" eddsa_key_jsont ~enc:(fun t -> t.key) 288 + |> Object.mem "keyDer" string ~enc:(fun t -> t.key_der) 289 + |> Object.mem "keyPem" string ~enc:(fun t -> t.key_pem) 290 + |> Object.mem "type" Value.t ~enc:(fun t -> t.type_) 291 + |> Object.mem "tests" (list dsa_test_jsont) 283 292 ~enc:(fun t -> t.tests) 284 - |> Json.Codec.Object.skip_unknown |> Json.Codec.Object.finish 293 + |> Object.skip_unknown |> Object.finish 285 294 286 295 type test_file = { 287 296 algorithm : json; ··· 304 313 let show_test_file t = Fmt.str "%a" pp_test_file t 305 314 306 315 let test_file_jsont = 307 - Json.Codec.Object.map ~kind:"test_file" 316 + let open Json.Codec in 317 + Object.map ~kind:"test_file" 308 318 (fun 309 319 algorithm 310 320 generator_version ··· 323 333 schema; 324 334 test_groups; 325 335 }) 326 - |> Json.Codec.Object.mem "algorithm" Json.Codec.Value.t ~enc:(fun t -> 336 + |> Object.mem "algorithm" Value.t ~enc:(fun t -> 327 337 t.algorithm) 328 - |> Json.Codec.Object.mem "generatorVersion" Json.Codec.Value.t ~enc:(fun t -> 338 + |> Object.mem "generatorVersion" Value.t ~enc:(fun t -> 329 339 t.generator_version) 330 - |> Json.Codec.Object.mem "header" Json.Codec.Value.t ~enc:(fun t -> t.header) 331 - |> Json.Codec.Object.mem "notes" Json.Codec.Value.t ~enc:(fun t -> t.notes) 332 - |> Json.Codec.Object.mem "numberOfTests" Json.Codec.Value.t ~enc:(fun t -> 340 + |> Object.mem "header" Value.t ~enc:(fun t -> t.header) 341 + |> Object.mem "notes" Value.t ~enc:(fun t -> t.notes) 342 + |> Object.mem "numberOfTests" Value.t ~enc:(fun t -> 333 343 t.number_of_tests) 334 - |> Json.Codec.Object.mem "schema" Json.Codec.Value.t ~enc:(fun t -> t.schema) 335 - |> Json.Codec.Object.mem "testGroups" (Json.Codec.list Json.Codec.Value.t) 344 + |> Object.mem "schema" Value.t ~enc:(fun t -> t.schema) 345 + |> Object.mem "testGroups" (list Value.t) 336 346 ~enc:(fun t -> t.test_groups) 337 - |> Json.Codec.Object.skip_unknown |> Json.Codec.Object.finish 347 + |> Object.skip_unknown |> Object.finish 338 348 339 349 let json_result = function 340 350 | Ok x -> x