Commit ca7b9b18 by Stephane Glondu

### Switch to exception-less style when Not_found is involved

parent 14d08b4c
 ... @@ -183,7 +183,11 @@ let b64_order = "+/0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYz ... @@ -183,7 +183,11 @@ let b64_order = "+/0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYz let compare_b64 a b = let compare_b64 a b = let na = String.length a and nb = String.length b in let na = String.length a and nb = String.length b in let value_of c = try String.index b64_order c with Not_found -> -1 in let value_of c = match String.index_opt b64_order c with | Some i -> i | None -> -1 in let rec loop i = let rec loop i = match (i < na), (i < nb) with match (i < na), (i < nb) with | true, true -> | true, true -> ... ...
 ... @@ -56,16 +56,15 @@ end ... @@ -56,16 +56,15 @@ end let check x = let check x = String.length x = token_length + 1 && String.length x = token_length + 1 && let rec loop i accu = let rec loop i accu = if i < token_length then ( if i < token_length then let digit = String.index digits x.[i] in match String.index_opt digits x.[i] with loop (i+1) Z.(n58 * accu + of_int digit) | Some digit -> loop (i+1) Z.(n58 * accu + of_int digit) ) else accu | None -> None else Some accu in in try match loop 0 Z.zero, String.index_opt digits x.[token_length] with let n = loop 0 Z.zero in | Some n, Some checksum -> Z.((n + of_int checksum) mod n53 =% zero) let checksum = String.index digits x.[token_length] in | _, _ -> false Z.((n + of_int checksum) mod n53 =% zero) with Not_found -> false let remove_dashes x = let remove_dashes x = let n = String.length x in let n = String.length x in ... ...
 ... @@ -582,10 +582,9 @@ module Make (W : ELECTION_DATA) (M : RANDOM) = struct ... @@ -582,10 +582,9 @@ module Make (W : ELECTION_DATA) (M : RANDOM) = struct in in let map = loop 0 G.one GMap.empty in let map = loop 0 G.one GMap.empty in fun x -> fun x -> try match GMap.find_opt x map with GMap.find x map | Some x -> x with Not_found -> | None -> invalid_arg "Cannot compute result" invalid_arg "Cannot compute result" in in let result = Array.mmap log results in let result = Array.mmap log results in {num_tallied; encrypted_tally; partial_decryptions; result} {num_tallied; encrypted_tally; partial_decryptions; result} ... ...
 ... @@ -33,8 +33,9 @@ let check token = ... @@ -33,8 +33,9 @@ let check token = n >= min_uuid_length && n >= min_uuid_length && let rec loop i = let rec loop i = if i >= 0 then if i >= 0 then let digit = try String.index digits token.[i] with Not_found -> -1 in match String.index_opt digits token.[i] with if digit >= 0 then loop (i-1) else false | Some _ -> loop (i-1) | None -> false else true else true in loop (n-1) in loop (n-1) ... ...
 ... @@ -49,7 +49,7 @@ let sha256_b64 x = ... @@ -49,7 +49,7 @@ let sha256_b64 x = Js.Unsafe.meth_call sjcl "codec.base64.fromBits" Js.Unsafe.meth_call sjcl "codec.base64.fromBits" [| sha256 x |] |> Js.to_string [| sha256 x |] |> Js.to_string in in match (try Some (String.index raw '=') with Not_found -> None) with match String.index_opt raw '=' with | Some i -> String.sub raw 0 i | Some i -> String.sub raw 0 i | None -> raw | None -> raw ... ...
 ... @@ -194,8 +194,7 @@ let decrypt ~key ~iv ~ciphertext = ... @@ -194,8 +194,7 @@ let decrypt ~key ~iv ~ciphertext = type rng = Cryptokit.Random.rng type rng = Cryptokit.Random.rng let secure_rng = let secure_rng = if Belenios_version.debug && if Belenios_version.debug && Sys.getenv_opt "BELENIOS_USE_URANDOM" <> None (try Sys.getenv "BELENIOS_USE_URANDOM" with Not_found -> "") <> "" then Cryptokit.Random.device_rng "/dev/urandom" then Cryptokit.Random.device_rng "/dev/urandom" else Cryptokit.Random.secure_rng else Cryptokit.Random.secure_rng ... ...
 ... @@ -125,10 +125,9 @@ module Make (P : PARSED_PARAMS) : S = struct ... @@ -125,10 +125,9 @@ module Make (P : PARSED_PARAMS) : S = struct | Some creds -> (fun b -> | Some creds -> (fun b -> match b.signature with match b.signature with | Some s -> | Some s -> (try (match GSet.find_opt s.s_public_key !creds with if GSet.find s.s_public_key !creds then false | Some false -> creds := GSet.add s.s_public_key true !creds; true else (creds := GSet.add s.s_public_key true !creds; true) | _ -> false) with Not_found -> false) | None -> false | None -> false ) ) | None -> (fun _ -> true) | None -> (fun _ -> true) ... ...
 ... @@ -25,7 +25,7 @@ let alert s : unit = ... @@ -25,7 +25,7 @@ let alert s : unit = let open Js.Unsafe in let open Js.Unsafe in fun_call (variable "alert") [| s |> Js.string |> inject |] fun_call (variable "alert") [| s |> Js.string |> inject |] let get_textarea id = let get_textarea_opt id = let res = ref None in let res = ref None in Js.Opt.iter Js.Opt.iter (document##getElementById (Js.string id)) (document##getElementById (Js.string id)) ... @@ -34,9 +34,12 @@ let get_textarea id = ... @@ -34,9 +34,12 @@ let get_textarea id = (Dom_html.CoerceTo.textarea e) (Dom_html.CoerceTo.textarea e) (fun x -> res := Some (Js.to_string (x##.value))) (fun x -> res := Some (Js.to_string (x##.value))) ); ); match !res with !res | None -> raise Not_found let get_textarea id = match get_textarea_opt id with | Some x -> x | Some x -> x | None -> Printf.ksprintf failwith "
 ... @@ -27,7 +27,11 @@ let generate _ = ... @@ -27,7 +27,11 @@ let generate _ = let raw = get_textarea "voters" in let raw = get_textarea "voters" in let rec loop i accu = let rec loop i accu = if i >= 0 then if i >= 0 then let j = try String.rindex_from raw i '\n' with Not_found -> -1 in let j = match String.rindex_from_opt raw i '\n' with | Some x -> x | None -> -1 in loop (j-1) (String.sub raw (j+1) (i-j) :: accu) loop (j-1) (String.sub raw (j+1) (i-j) :: accu) else else accu accu ... ...
 ... @@ -75,20 +75,20 @@ let compute_partial_decryption _ = ... @@ -75,20 +75,20 @@ let compute_partial_decryption _ = Dom_html.CoerceTo.input e >>= fun e -> Dom_html.CoerceTo.input e >>= fun e -> let pk_str = Js.to_string e##.value in let pk_str = Js.to_string e##.value in let private_key = let private_key = try match get_textarea_opt "encrypted_private_key" with let epk = get_textarea "encrypted_private_key" in | Some epk -> let module PKI = Trustees.MakePKI (P.G) (DirectRandom) in let module PKI = Trustees.MakePKI (P.G) (DirectRandom) in let module C = Trustees.MakeChannels (P.G) (DirectRandom) (PKI) in let module C = Trustees.MakeChannels (P.G) (DirectRandom) (PKI) in let sk = PKI.derive_sk pk_str and dk = PKI.derive_dk pk_str in let sk = PKI.derive_sk pk_str and dk = PKI.derive_dk pk_str in let vk = P.G.(g **~ sk) in let vk = P.G.(g **~ sk) in let epk = C.recv dk vk epk in let epk = C.recv dk vk epk in (partial_decryption_key_of_string epk).pdk_decryption_key (partial_decryption_key_of_string epk).pdk_decryption_key with Not_found -> | None -> basic_check_private_key pk_str; basic_check_private_key pk_str; try number_of_string pk_str try number_of_string pk_str with e -> with e -> Printf.ksprintf Printf.ksprintf failwith "Error in format of private key: %s" (Printexc.to_string e) failwith "Error in format of private key: %s" (Printexc.to_string e) in in let factor = E.compute_factor encrypted_tally private_key in let factor = E.compute_factor encrypted_tally private_key in set_textarea "pd" (string_of_partial_decryption P.G.write factor); set_textarea "pd" (string_of_partial_decryption P.G.write factor); ... @@ -129,8 +129,7 @@ let get_uuid x = ... @@ -129,8 +129,7 @@ let get_uuid x = None None else else let args = Url.decode_arguments (String.sub x 1 (n-1)) in let args = Url.decode_arguments (String.sub x 1 (n-1)) in try Some (List.assoc "uuid" args) List.assoc_opt "uuid" args with Not_found -> None let main _ = let main _ = let _ = let _ = ... ...