~ubuntu-branches/debian/experimental/sks/experimental

« back to all changes in this revision

Viewing changes to number2.ml

  • Committer: Package Import Robot
  • Author(s): Daniel Kahn Gillmor
  • Date: 2013-06-27 16:39:02 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130627163902-qqic4va2187boeji
Tags: 1.1.4-1
* New Upstream Release (Closes: #690135)
* added myself to Uploaders.
* convert to dh 9
* Standards-Version: bump to 3.9.4 (no changes needed)
* debian/rules: clean up
* refresh and clean up debian/patches
* switch packaging vcs to git
* avoid trying to upgrade DB_CONFIG (Closes: #709322)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
(************************************************************************)
2
 
(* This file is part of SKS.  SKS is free software; you can
3
 
   redistribute it and/or modify it under the terms of the GNU General
4
 
   Public License as published by the Free Software Foundation; either
5
 
   version 2 of the License, or (at your option) any later version.
6
 
 
7
 
   This program is distributed in the hope that it will be useful, but
8
 
   WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10
 
   General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
15
 
   USA *)
16
 
(***********************************************************************)
17
 
 
18
 
(** Basic operations and definitions for multi-precistion integers. *)
19
 
 
20
 
open Big_int
21
 
 
22
 
let two = big_int_of_int 2
23
 
let one = unit_big_int
24
 
let zero = zero_big_int
25
 
let neg_one = big_int_of_int (-1)
26
 
 
27
 
let ( *! ) = mult_big_int
28
 
let ( +! ) = add_big_int
29
 
let ( -! ) = sub_big_int
30
 
let ( %! ) = quomod_big_int
31
 
let ( /! ) = div_big_int
32
 
let ( **! ) = power_big_int_positive_big_int
33
 
let ( <>! ) x y = not (eq_big_int x y)
34
 
let ( =! ) = eq_big_int
35
 
let ( <! ) = lt_big_int
36
 
let ( >! ) = gt_big_int
37
 
let ( <=! ) = le_big_int
38
 
let ( >=! ) = ge_big_int
39
 
 
40
 
 
41
 
let width = 8
42
 
let width_pow = power_int_positive_int 2 width
43
 
 
44
 
let revstring s = 
45
 
  let len = String.length s in
46
 
  let copy = String.create len in
47
 
  for i = 0 to len - 1 do 
48
 
    copy.[i] <- s.[len - 1 - i]
49
 
  done;
50
 
  copy
51
 
 
52
 
let revstring_inplace s = 
53
 
  let len = String.length s in
54
 
  for i = 0 to (len - 2)/2 do
55
 
    let j = len - 1 - i in
56
 
    let tmp = s.[i] in
57
 
    s.[i] <- s.[j];
58
 
    s.[j] <- tmp
59
 
  done
60
 
 
61
 
let bigint_to_bytes ~nbytes n = 
62
 
  if sign_big_int n = -1 
63
 
  then raise (Invalid_argument "N.to_bytes: negative argument");
64
 
  let string = String.create nbytes in
65
 
  let rec loop n i = 
66
 
    if i < 0 then string
67
 
    else  
68
 
      let (a,b) = n %! width_pow in
69
 
      string.[i] <- char_of_int (int_of_big_int b);
70
 
      loop a (i - 1)
71
 
  in
72
 
  let str = loop n (nbytes - 1) in
73
 
  revstring_inplace str;
74
 
  str
75
 
 
76
 
let bigint_of_bytes str = 
77
 
  let str = revstring str in
78
 
  let nbytes = String.length str in
79
 
  let rec loop n i = 
80
 
    if i >= nbytes then n
81
 
    else
82
 
      let m = big_int_of_int (int_of_char str.[i]) in
83
 
      loop (n *! width_pow +! m) (i+1)
84
 
  in
85
 
  loop zero 0 
86
 
 
87
 
module type ZZpType = 
88
 
sig
89
 
  type t 
90
 
  type tref
91
 
  type zzarray 
92
 
  val nbits : int
93
 
  val nbytes : int
94
 
  val of_bytes : string -> t
95
 
  val to_bytes : t -> string
96
 
  val of_int : int -> t
97
 
  val to_N : t -> big_int
98
 
  val of_N : big_int -> t
99
 
 
100
 
  val one : t
101
 
  val zero : t
102
 
 
103
 
  val add : t -> t -> t
104
 
  val div : t -> t -> t
105
 
  val mul : t -> t -> t
106
 
  val mult : t -> t -> t
107
 
  val inv : t -> t
108
 
  val neg : t -> t
109
 
  val shl : t -> int -> t
110
 
 
111
 
  val imult : t -> int -> t
112
 
 
113
 
  val add_fast : t -> t -> t
114
 
  val mul_fast : t -> t -> t
115
 
  val mult_fast : t -> t -> t
116
 
  val square : t -> t
117
 
  val square_fast : t -> t
118
 
  val canonicalize : t -> t
119
 
 
120
 
  val sub : t -> t -> t
121
 
  val print : t -> unit
122
 
 
123
 
  val imul : t -> int -> t
124
 
 
125
 
  val lt : t -> t -> bool
126
 
  val gt : t -> t -> bool
127
 
  val eq : t -> t -> bool
128
 
  val neq : t -> t -> bool
129
 
 
130
 
  val look : tref -> t
131
 
  val mult_in : tref -> t -> t -> unit
132
 
  val mult_fast_in : tref -> t -> t -> unit
133
 
  val add_in : tref -> t -> t -> unit
134
 
  val add_fast_in : tref -> t -> t -> unit
135
 
  val sub_in : tref -> t -> t -> unit
136
 
  val sub_fast_in : tref -> t -> t -> unit
137
 
  val copy_in : tref -> t -> unit
138
 
  val copy_out : tref -> t
139
 
  val make_ref : t -> tref
140
 
  val canonicalize_in : tref -> unit
141
 
 
142
 
  val points : int -> t array
143
 
  val svalues : int -> zzarray
144
 
  val to_string : t -> string
145
 
 
146
 
  val add_el_array : points: t array -> t -> t array
147
 
  val del_el_array : points: t array -> t -> t array
148
 
  val mult_array : svalues: zzarray -> t array -> unit
149
 
 
150
 
 
151
 
  val add_el : svalues:zzarray -> points:t array -> 
152
 
    t -> unit (* modifies svalues *)
153
 
  val del_el : svalues:zzarray -> points:t array -> 
154
 
    t -> unit (* modifies svalues *)
155
 
 
156
 
  val length : zzarray -> int
157
 
  val zzarray_to_array : zzarray -> t array
158
 
  val zzarray_of_array : t array -> zzarray 
159
 
  val zzarray_div : zzarray -> zzarray -> zzarray
160
 
  val zzarray_copy : zzarray -> zzarray
161
 
 
162
 
  val cmp : t -> t -> int
163
 
 
164
 
  val order : big_int
165
 
end
166