Miasm2
 All Classes Namespaces Files Functions Variables Typedefs Properties Macros
modint.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 
4 class moduint(object):
5 
6  def __init__(self, arg):
7  self.arg = long(arg) % self.__class__.limit
8  assert(self.arg >= 0 and self.arg < self.__class__.limit)
9 
10  def __repr__(self):
11  return self.__class__.__name__ + '(' + hex(self.arg) + ')'
12 
13  def __hash__(self):
14  return hash(self.arg)
15 
16  @classmethod
17  def maxcast(cls, c2):
18  c2 = c2.__class__
19  if cls.size > c2.size:
20  return cls
21  else:
22  return c2
23 
24  def __cmp__(self, y):
25  if isinstance(y, moduint):
26  return cmp(self.arg, y.arg)
27  else:
28  return cmp(self.arg, y)
29 
30  def __add__(self, y):
31  if isinstance(y, moduint):
32  cls = self.maxcast(y)
33  return cls(self.arg + y.arg)
34  else:
35  return self.__class__(self.arg + y)
36 
37  def __and__(self, y):
38  if isinstance(y, moduint):
39  cls = self.maxcast(y)
40  return cls(self.arg & y.arg)
41  else:
42  return self.__class__(self.arg & y)
43 
44  def __div__(self, y):
45  if isinstance(y, moduint):
46  cls = self.maxcast(y)
47  return cls(self.arg / y.arg)
48  else:
49  return self.__class__(self.arg / y)
50 
51  def __int__(self):
52  return int(self.arg)
53 
54  def __long__(self):
55  return long(self.arg)
56 
57  def __invert__(self):
58  return self.__class__(~self.arg)
59 
60  def __lshift__(self, y):
61  if isinstance(y, moduint):
62  cls = self.maxcast(y)
63  return cls(self.arg << y.arg)
64  else:
65  return self.__class__(self.arg << y)
66 
67  def __mod__(self, y):
68  if isinstance(y, moduint):
69  cls = self.maxcast(y)
70  return cls(self.arg % y.arg)
71  else:
72  return self.__class__(self.arg % y)
73 
74  def __mul__(self, y):
75  if isinstance(y, moduint):
76  cls = self.maxcast(y)
77  return cls(self.arg * y.arg)
78  else:
79  return self.__class__(self.arg * y)
80 
81  def __neg__(self):
82  return self.__class__(-self.arg)
83 
84  def __or__(self, y):
85  if isinstance(y, moduint):
86  cls = self.maxcast(y)
87  return cls(self.arg | y.arg)
88  else:
89  return self.__class__(self.arg | y)
90 
91  def __radd__(self, y):
92  return self.__add__(y)
93 
94  def __rand__(self, y):
95  return self.__and__(y)
96 
97  def __rdiv__(self, y):
98  if isinstance(y, moduint):
99  cls = self.maxcast(y)
100  return cls(y.arg / self.arg)
101  else:
102  return self.__class__(y / self.arg)
103 
104  def __rlshift__(self, y):
105  if isinstance(y, moduint):
106  cls = self.maxcast(y)
107  return cls(y.arg << self.arg)
108  else:
109  return self.__class__(y << self.arg)
110 
111  def __rmod__(self, y):
112  if isinstance(y, moduint):
113  cls = self.maxcast(y)
114  return cls(y.arg % self.arg)
115  else:
116  return self.__class__(y % self.arg)
117 
118  def __rmul__(self, y):
119  return self.__mul__(y)
120 
121  def __ror__(self, y):
122  return self.__or__(y)
123 
124  def __rrshift__(self, y):
125  if isinstance(y, moduint):
126  cls = self.maxcast(y)
127  return cls(y.arg >> self.arg)
128  else:
129  return self.__class__(y >> self.arg)
130 
131  def __rshift__(self, y):
132  if isinstance(y, moduint):
133  cls = self.maxcast(y)
134  return cls(self.arg >> y.arg)
135  else:
136  return self.__class__(self.arg >> y)
137 
138  def __rsub__(self, y):
139  if isinstance(y, moduint):
140  cls = self.maxcast(y)
141  return cls(y.arg - self.arg)
142  else:
143  return self.__class__(y - self.arg)
144 
145  def __rxor__(self, y):
146  return self.__xor__(y)
147 
148  def __sub__(self, y):
149  if isinstance(y, moduint):
150  cls = self.maxcast(y)
151  return cls(self.arg - y.arg)
152  else:
153  return self.__class__(self.arg - y)
154 
155  def __xor__(self, y):
156  if isinstance(y, moduint):
157  cls = self.maxcast(y)
158  return cls(self.arg ^ y.arg)
159  else:
160  return self.__class__(self.arg ^ y)
161 
162  def __hex__(self):
163  return hex(self.arg)
164 
165  def __abs__(self):
166  return abs(self.arg)
167 
168  def __rpow__(self, v):
169  return v ** self.arg
170 
171  def __pow__(self, v):
172  return self.__class__(self.arg ** v)
173 
174 
176 
177  def __init__(self, arg):
178  if isinstance(arg, moduint):
179  arg = arg.arg
180  a = arg % self.__class__.limit
181  if a >= self.__class__.limit / 2:
182  a -= self.__class__.limit
183  self.arg = a
184  assert(self.arg >= -self.__class__.limit /
185  2 and self.arg < self.__class__.limit)
186 
187 
188 def is_modint(a):
189  return isinstance(a, moduint)
190 
191 
192 def size2mask(size):
193  return (1 << size) - 1
194 
195 mod_size2uint = {}
196 mod_size2int = {}
197 
198 mod_uint2size = {}
199 mod_int2size = {}
200 
201 
203  "Define common int: ExprInt1, ExprInt2, .."
204  global mod_size2int, mod_int2size, mod_size2uint, mod_uint2size
205 
206  common_int = xrange(1, 257)
207 
208  for i in common_int:
209  name = 'uint%d' % i
210  c = type(name, (moduint,), {"size": i, "limit": 1 << i})
211  globals()[name] = c
212  mod_size2uint[i] = c
213  mod_uint2size[c] = i
214 
215  for i in common_int:
216  name = 'int%d' % i
217  c = type(name, (modint,), {"size": i, "limit": 1 << i})
218  globals()[name] = c
219  mod_size2int[i] = c
220  mod_int2size[c] = i
221