1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
(***********************************************************************)
(* rMisc.ml - Miscellaneous utilities associated with reconciliation, *)
(* and in particular those that require access to the size *)
(* of the prime modulus. *)
(* *)
(* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, *)
(* 2011, 2012, 2013 Yaron Minsky and Contributors *)
(* *)
(* This file is part of SKS. SKS is free software; you can *)
(* redistribute it and/or modify it under the terms of the GNU General *)
(* Public License as published by the Free Software Foundation; either *)
(* version 2 of the License, or (at your option) any later version. *)
(* *)
(* This program is distributed in the hope that it will be useful, but *)
(* WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *)
(* General Public License for more details. *)
(* *)
(* You should have received a copy of the GNU General Public License *)
(* along with this program; if not, write to the Free Software *)
(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *)
(* USA or see <http://www.gnu.org/licenses/>. *)
(***********************************************************************)
open Bytes
open MoreLabels
module Unix=UnixLabels
module List=ListLabels
(** deterministic RNG *)
let det_rng = Random.State.make [|104|]
module Set = PSet.Set (* was: Polyset.Set *)
module Map = PMap.Map
let stringset_to_string stringset =
let list = List.sort ~cmp:String.compare (Set.elements stringset) in
let cout = Channel.new_buffer_outc 1024 in
List.iter ~f:(fun string ->
cout#write_int (String.length string);
cout#write_string string)
list;
cout#contents
let digest_stringset strings =
let string = stringset_to_string strings in
Digest.string string
let print_lengths list =
let list = List.sort ~cmp:String.compare list in
MList.print ~f:(fun s -> Printf.printf "%d" (String.length s))
list
let rec fill_random_string rfunc string ~pos ~len =
if pos < len then
let steps =
if len - pos > 3 then 3 else len - pos in
(* CR yminsky: I think this has the same bug as the function with the same name in Utils *)
let _bits = rfunc () in
for i = 0 to steps - 1 do
Bytes.set string (pos + i) (
char_of_int (0xFF land ((rfunc ()) lsr (8 * i))))
done;
fill_random_string rfunc string ~pos:(pos + steps) ~len
else
()
let random_string rfunc len =
let buf = Bytes.create len in
fill_random_string rfunc buf ~pos:0 ~len;
Bytes.unsafe_to_string buf
let conv_chans (cin, cout) =
(new MeteredChannel.metered_in_channel (new Channel.sys_in_channel cin),
new MeteredChannel.metered_out_channel (new Channel.sys_out_channel cout))
(* new Bufchan.buf_out_channel cout (1024 * 100)) *)
(************************************************************)
(* String Sets ********************************************)
(************************************************************)
let add_random rfunc bytelength set =
Set.add (random_string rfunc bytelength) set
let add_n_random rfunc bytelength ~n set =
Utils.apply n (add_random rfunc bytelength) set
let det_string_set ~bytes ~size =
add_n_random
(fun () -> Random.State.bits det_rng)
bytes ~n:size Set.empty
let rand_string_set ~bytes ~size =
add_n_random Random.bits bytes ~n:size Set.empty
let localize_string_set ~bytes ~diff set =
add_n_random Random.bits bytes ~n:diff set
(*
let local_string_set ~bytes ~base_size ~diff =
let base_set = det_string_set ~bytes ~size:base_size in
let local_set = add_n_random Random.bits bytes ~n:diff base_set in
local_set
*)
(*
let string_sets ~bytes ~base_size ~diff =
let base_set = det_string_set ~bytes ~size:base_size in
let diff_set = add_n_random Random.bits bytes ~n:diff Set.empty in
(base_set,diff_set)
*)
(*
let print_string_set set =
let list = Set.elements set in
let list= List.sort ~cmp:compare list in
List.iter ~f:(fun string -> print_string string; print_newline ())
*)
(*****************************************************************)
(*****************************************************************)
let pad string bytes =
let len = String.length string in
if bytes > len then
let nstr = Bytes.create bytes in
BytesLabels.fill nstr ~pos:len ~len:(bytes - len) '\000';
Bytes.blit_string string 0 nstr 0 len;
Bytes.unsafe_to_string nstr
else
string
let padset stringset bytes =
Set.fold ~f:(fun el set -> Set.add (pad el bytes) set)
~init:Set.empty stringset
let truncate string bytes =
let len = String.length string in
if bytes < len then
let nstr = Bytes.create bytes in
Bytes.blit_string string 0 nstr 0 bytes;
Bytes.unsafe_to_string nstr
else
string
let truncset stringset bytes =
Set.fold ~f:(fun el set -> Set.add (truncate el bytes) set)
~init:Set.empty stringset
(*****************************************************************)
(* PRIMENESS-RELATED THINGS ***********************************)
(*****************************************************************)
let order_string = "530512889551602322505127520352579437339"
(************* Initialization code ****************************)
let _ =
Settings.setup_RNG ();
ZZp.set_order (ZZp.of_string order_string)
|