You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

466 lines
15 KiB

25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
  1. // Modular integer operations.
  2. #ifndef _CL_MODINTEGER_H
  3. #define _CL_MODINTEGER_H
  4. #include "cln/object.h"
  5. #include "cln/ring.h"
  6. #include "cln/integer.h"
  7. #include "cln/random.h"
  8. #include "cln/malloc.h"
  9. #include "cln/io.h"
  10. #include "cln/proplist.h"
  11. #include "cln/condition.h"
  12. #include "cln/abort.h"
  13. #undef random // Linux defines random() as a macro!
  14. namespace cln {
  15. // Representation of an element of a ring Z/mZ.
  16. // To protect against mixing elements of different modular rings, such as
  17. // (3 mod 4) + (2 mod 5), every modular integer carries its ring in itself.
  18. // Representation of a ring Z/mZ.
  19. class cl_heap_modint_ring;
  20. class cl_modint_ring : public cl_ring {
  21. public:
  22. // Default constructor.
  23. cl_modint_ring ();
  24. // Constructor. Takes a cl_heap_modint_ring*, increments its refcount.
  25. cl_modint_ring (cl_heap_modint_ring* r);
  26. // Copy constructor.
  27. cl_modint_ring (const cl_modint_ring&);
  28. // Assignment operator.
  29. cl_modint_ring& operator= (const cl_modint_ring&);
  30. // Automatic dereferencing.
  31. cl_heap_modint_ring* operator-> () const
  32. { return (cl_heap_modint_ring*)heappointer; }
  33. };
  34. // Z/0Z
  35. extern const cl_modint_ring cl_modint0_ring;
  36. // Default constructor. This avoids dealing with NULL pointers.
  37. inline cl_modint_ring::cl_modint_ring ()
  38. : cl_ring (as_cl_private_thing(cl_modint0_ring)) {}
  39. CL_REQUIRE(cl_MI)
  40. // Copy constructor and assignment operator.
  41. CL_DEFINE_COPY_CONSTRUCTOR2(cl_modint_ring,cl_ring)
  42. CL_DEFINE_ASSIGNMENT_OPERATOR(cl_modint_ring,cl_modint_ring)
  43. // Normal constructor for `cl_modint_ring'.
  44. inline cl_modint_ring::cl_modint_ring (cl_heap_modint_ring* r)
  45. : cl_ring ((cl_private_thing) (cl_inc_pointer_refcount((cl_heap*)r), r)) {}
  46. // Operations on modular integer rings.
  47. inline bool operator== (const cl_modint_ring& R1, const cl_modint_ring& R2)
  48. { return (R1.pointer == R2.pointer); }
  49. inline bool operator!= (const cl_modint_ring& R1, const cl_modint_ring& R2)
  50. { return (R1.pointer != R2.pointer); }
  51. inline bool operator== (const cl_modint_ring& R1, cl_heap_modint_ring* R2)
  52. { return (R1.pointer == R2); }
  53. inline bool operator!= (const cl_modint_ring& R1, cl_heap_modint_ring* R2)
  54. { return (R1.pointer != R2); }
  55. // Condition raised when a probable prime is discovered to be composite.
  56. struct cl_composite_condition : public cl_condition {
  57. SUBCLASS_cl_condition()
  58. cl_I p; // the non-prime
  59. cl_I factor; // a nontrivial factor, or 0
  60. // Constructors.
  61. cl_composite_condition (const cl_I& _p)
  62. : p (_p), factor (0)
  63. { print(std::cerr); }
  64. cl_composite_condition (const cl_I& _p, const cl_I& _f)
  65. : p (_p), factor (_f)
  66. { print(std::cerr); }
  67. // Implement general condition methods.
  68. const char * name () const;
  69. void print (std::ostream&) const;
  70. ~cl_composite_condition () {}
  71. };
  72. // Representation of an element of a ring Z/mZ.
  73. class _cl_MI /* cf. _cl_ring_element */ {
  74. public:
  75. cl_I rep; // representative, integer >=0, <m
  76. // (maybe the Montgomery representative!)
  77. // Default constructor.
  78. _cl_MI () : rep () {}
  79. public: /* ugh */
  80. // Constructor.
  81. _cl_MI (const cl_heap_modint_ring* R, const cl_I& r) : rep (r) { (void)R; }
  82. _cl_MI (const cl_modint_ring& R, const cl_I& r) : rep (r) { (void)R; }
  83. public:
  84. // Conversion.
  85. CL_DEFINE_CONVERTER(_cl_ring_element)
  86. public: // Ability to place an object at a given address.
  87. void* operator new (size_t size) { return malloc_hook(size); }
  88. void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
  89. void operator delete (void* ptr) { free_hook(ptr); }
  90. };
  91. class cl_MI /* cf. cl_ring_element */ : public _cl_MI {
  92. protected:
  93. cl_modint_ring _ring; // ring Z/mZ
  94. public:
  95. const cl_modint_ring& ring () const { return _ring; }
  96. // Default constructor.
  97. cl_MI () : _cl_MI (), _ring () {}
  98. public: /* ugh */
  99. // Constructor.
  100. cl_MI (const cl_modint_ring& R, const cl_I& r) : _cl_MI (R,r), _ring (R) {}
  101. cl_MI (const cl_modint_ring& R, const _cl_MI& r) : _cl_MI (r), _ring (R) {}
  102. public:
  103. // Conversion.
  104. CL_DEFINE_CONVERTER(cl_ring_element)
  105. // Debugging output.
  106. void debug_print () const;
  107. public: // Ability to place an object at a given address.
  108. void* operator new (size_t size) { return malloc_hook(size); }
  109. void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
  110. void operator delete (void* ptr) { free_hook(ptr); }
  111. };
  112. // Representation of an element of a ring Z/mZ or an exception.
  113. class cl_MI_x {
  114. private:
  115. cl_MI value;
  116. public:
  117. cl_composite_condition* condition;
  118. // Constructors.
  119. cl_MI_x (cl_composite_condition* c) : value (), condition (c) {}
  120. cl_MI_x (const cl_MI& x) : value (x), condition (NULL) {}
  121. // Cast operators.
  122. //operator cl_MI& () { if (condition) cl_abort(); return value; }
  123. //operator const cl_MI& () const { if (condition) cl_abort(); return value; }
  124. operator cl_MI () const { if (condition) cl_abort(); return value; }
  125. };
  126. // Ring operations.
  127. struct _cl_modint_setops /* cf. _cl_ring_setops */ {
  128. // print
  129. void (* fprint) (cl_heap_modint_ring* R, std::ostream& stream, const _cl_MI& x);
  130. // equality
  131. cl_boolean (* equal) (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y);
  132. // random number
  133. const _cl_MI (* random) (cl_heap_modint_ring* R, random_state& randomstate);
  134. };
  135. struct _cl_modint_addops /* cf. _cl_ring_addops */ {
  136. // 0
  137. const _cl_MI (* zero) (cl_heap_modint_ring* R);
  138. cl_boolean (* zerop) (cl_heap_modint_ring* R, const _cl_MI& x);
  139. // x+y
  140. const _cl_MI (* plus) (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y);
  141. // x-y
  142. const _cl_MI (* minus) (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y);
  143. // -x
  144. const _cl_MI (* uminus) (cl_heap_modint_ring* R, const _cl_MI& x);
  145. };
  146. struct _cl_modint_mulops /* cf. _cl_ring_mulops */ {
  147. // 1
  148. const _cl_MI (* one) (cl_heap_modint_ring* R);
  149. // canonical homomorphism
  150. const _cl_MI (* canonhom) (cl_heap_modint_ring* R, const cl_I& x);
  151. // x*y
  152. const _cl_MI (* mul) (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y);
  153. // x^2
  154. const _cl_MI (* square) (cl_heap_modint_ring* R, const _cl_MI& x);
  155. // x^y, y Integer >0
  156. const _cl_MI (* expt_pos) (cl_heap_modint_ring* R, const _cl_MI& x, const cl_I& y);
  157. // x^-1
  158. const cl_MI_x (* recip) (cl_heap_modint_ring* R, const _cl_MI& x);
  159. // x*y^-1
  160. const cl_MI_x (* div) (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y);
  161. // x^y, y Integer
  162. const cl_MI_x (* expt) (cl_heap_modint_ring* R, const _cl_MI& x, const cl_I& y);
  163. // x -> x mod m for x>=0
  164. const cl_I (* reduce_modulo) (cl_heap_modint_ring* R, const cl_I& x);
  165. // some inverse of canonical homomorphism
  166. const cl_I (* retract) (cl_heap_modint_ring* R, const _cl_MI& x);
  167. };
  168. typedef const _cl_modint_setops cl_modint_setops;
  169. typedef const _cl_modint_addops cl_modint_addops;
  170. typedef const _cl_modint_mulops cl_modint_mulops;
  171. // Representation of the ring Z/mZ.
  172. // Currently rings are garbage collected only when they are not referenced
  173. // any more and when the ring table gets full.
  174. // Modular integer rings are kept unique in memory. This way, ring equality
  175. // can be checked very efficiently by a simple pointer comparison.
  176. class cl_heap_modint_ring /* cf. cl_heap_ring */ : public cl_heap {
  177. SUBCLASS_cl_heap_ring()
  178. private:
  179. cl_property_list properties;
  180. protected:
  181. cl_modint_setops* setops;
  182. cl_modint_addops* addops;
  183. cl_modint_mulops* mulops;
  184. public:
  185. cl_I modulus; // m, normalized to be >= 0
  186. public:
  187. // Low-level operations.
  188. void _fprint (std::ostream& stream, const _cl_MI& x)
  189. { setops->fprint(this,stream,x); }
  190. cl_boolean _equal (const _cl_MI& x, const _cl_MI& y)
  191. { return setops->equal(this,x,y); }
  192. const _cl_MI _random (random_state& randomstate)
  193. { return setops->random(this,randomstate); }
  194. const _cl_MI _zero ()
  195. { return addops->zero(this); }
  196. cl_boolean _zerop (const _cl_MI& x)
  197. { return addops->zerop(this,x); }
  198. const _cl_MI _plus (const _cl_MI& x, const _cl_MI& y)
  199. { return addops->plus(this,x,y); }
  200. const _cl_MI _minus (const _cl_MI& x, const _cl_MI& y)
  201. { return addops->minus(this,x,y); }
  202. const _cl_MI _uminus (const _cl_MI& x)
  203. { return addops->uminus(this,x); }
  204. const _cl_MI _one ()
  205. { return mulops->one(this); }
  206. const _cl_MI _canonhom (const cl_I& x)
  207. { return mulops->canonhom(this,x); }
  208. const _cl_MI _mul (const _cl_MI& x, const _cl_MI& y)
  209. { return mulops->mul(this,x,y); }
  210. const _cl_MI _square (const _cl_MI& x)
  211. { return mulops->square(this,x); }
  212. const _cl_MI _expt_pos (const _cl_MI& x, const cl_I& y)
  213. { return mulops->expt_pos(this,x,y); }
  214. const cl_MI_x _recip (const _cl_MI& x)
  215. { return mulops->recip(this,x); }
  216. const cl_MI_x _div (const _cl_MI& x, const _cl_MI& y)
  217. { return mulops->div(this,x,y); }
  218. const cl_MI_x _expt (const _cl_MI& x, const cl_I& y)
  219. { return mulops->expt(this,x,y); }
  220. const cl_I _reduce_modulo (const cl_I& x)
  221. { return mulops->reduce_modulo(this,x); }
  222. const cl_I _retract (const _cl_MI& x)
  223. { return mulops->retract(this,x); }
  224. // High-level operations.
  225. void fprint (std::ostream& stream, const cl_MI& x)
  226. {
  227. if (!(x.ring() == this)) cl_abort();
  228. _fprint(stream,x);
  229. }
  230. cl_boolean equal (const cl_MI& x, const cl_MI& y)
  231. {
  232. if (!(x.ring() == this)) cl_abort();
  233. if (!(y.ring() == this)) cl_abort();
  234. return _equal(x,y);
  235. }
  236. const cl_MI random (random_state& randomstate = default_random_state)
  237. {
  238. return cl_MI(this,_random(randomstate));
  239. }
  240. const cl_MI zero ()
  241. {
  242. return cl_MI(this,_zero());
  243. }
  244. cl_boolean zerop (const cl_MI& x)
  245. {
  246. if (!(x.ring() == this)) cl_abort();
  247. return _zerop(x);
  248. }
  249. const cl_MI plus (const cl_MI& x, const cl_MI& y)
  250. {
  251. if (!(x.ring() == this)) cl_abort();
  252. if (!(y.ring() == this)) cl_abort();
  253. return cl_MI(this,_plus(x,y));
  254. }
  255. const cl_MI minus (const cl_MI& x, const cl_MI& y)
  256. {
  257. if (!(x.ring() == this)) cl_abort();
  258. if (!(y.ring() == this)) cl_abort();
  259. return cl_MI(this,_minus(x,y));
  260. }
  261. const cl_MI uminus (const cl_MI& x)
  262. {
  263. if (!(x.ring() == this)) cl_abort();
  264. return cl_MI(this,_uminus(x));
  265. }
  266. const cl_MI one ()
  267. {
  268. return cl_MI(this,_one());
  269. }
  270. const cl_MI canonhom (const cl_I& x)
  271. {
  272. return cl_MI(this,_canonhom(x));
  273. }
  274. const cl_MI mul (const cl_MI& x, const cl_MI& y)
  275. {
  276. if (!(x.ring() == this)) cl_abort();
  277. if (!(y.ring() == this)) cl_abort();
  278. return cl_MI(this,_mul(x,y));
  279. }
  280. const cl_MI square (const cl_MI& x)
  281. {
  282. if (!(x.ring() == this)) cl_abort();
  283. return cl_MI(this,_square(x));
  284. }
  285. const cl_MI expt_pos (const cl_MI& x, const cl_I& y)
  286. {
  287. if (!(x.ring() == this)) cl_abort();
  288. return cl_MI(this,_expt_pos(x,y));
  289. }
  290. const cl_MI_x recip (const cl_MI& x)
  291. {
  292. if (!(x.ring() == this)) cl_abort();
  293. return _recip(x);
  294. }
  295. const cl_MI_x div (const cl_MI& x, const cl_MI& y)
  296. {
  297. if (!(x.ring() == this)) cl_abort();
  298. if (!(y.ring() == this)) cl_abort();
  299. return _div(x,y);
  300. }
  301. const cl_MI_x expt (const cl_MI& x, const cl_I& y)
  302. {
  303. if (!(x.ring() == this)) cl_abort();
  304. return _expt(x,y);
  305. }
  306. const cl_I reduce_modulo (const cl_I& x)
  307. {
  308. return _reduce_modulo(x);
  309. }
  310. const cl_I retract (const cl_MI& x)
  311. {
  312. if (!(x.ring() == this)) cl_abort();
  313. return _retract(x);
  314. }
  315. // Miscellaneous.
  316. sintL bits; // number of bits needed to represent a representative, or -1
  317. int log2_bits; // log_2(bits), or -1
  318. // Property operations.
  319. cl_property* get_property (const cl_symbol& key)
  320. { return properties.get_property(key); }
  321. void add_property (cl_property* new_property)
  322. { properties.add_property(new_property); }
  323. // Constructor.
  324. cl_heap_modint_ring (cl_I m, cl_modint_setops*, cl_modint_addops*, cl_modint_mulops*);
  325. // This class is intented to be subclassable, hence needs a virtual destructor.
  326. virtual ~cl_heap_modint_ring () {}
  327. private:
  328. virtual void dummy ();
  329. };
  330. #define SUBCLASS_cl_heap_modint_ring() \
  331. SUBCLASS_cl_heap_ring()
  332. // Lookup or create a modular integer ring Z/mZ
  333. extern const cl_modint_ring find_modint_ring (const cl_I& m);
  334. CL_REQUIRE(cl_MI)
  335. // Runtime typing support.
  336. extern cl_class cl_class_modint_ring;
  337. // Operations on modular integers.
  338. // Output.
  339. inline void fprint (std::ostream& stream, const cl_MI& x)
  340. { x.ring()->fprint(stream,x); }
  341. CL_DEFINE_PRINT_OPERATOR(cl_MI)
  342. // Add.
  343. inline const cl_MI operator+ (const cl_MI& x, const cl_MI& y)
  344. { return x.ring()->plus(x,y); }
  345. inline const cl_MI operator+ (const cl_MI& x, const cl_I& y)
  346. { return x.ring()->plus(x,x.ring()->canonhom(y)); }
  347. inline const cl_MI operator+ (const cl_I& x, const cl_MI& y)
  348. { return y.ring()->plus(y.ring()->canonhom(x),y); }
  349. // Negate.
  350. inline const cl_MI operator- (const cl_MI& x)
  351. { return x.ring()->uminus(x); }
  352. // Subtract.
  353. inline const cl_MI operator- (const cl_MI& x, const cl_MI& y)
  354. { return x.ring()->minus(x,y); }
  355. inline const cl_MI operator- (const cl_MI& x, const cl_I& y)
  356. { return x.ring()->minus(x,x.ring()->canonhom(y)); }
  357. inline const cl_MI operator- (const cl_I& x, const cl_MI& y)
  358. { return y.ring()->minus(y.ring()->canonhom(x),y); }
  359. // Shifts.
  360. extern const cl_MI operator<< (const cl_MI& x, sintL y); // assume 0 <= y < 2^31
  361. extern const cl_MI operator>> (const cl_MI& x, sintL y); // assume m odd, 0 <= y < 2^31
  362. // Equality.
  363. inline bool operator== (const cl_MI& x, const cl_MI& y)
  364. { return x.ring()->equal(x,y); }
  365. inline bool operator!= (const cl_MI& x, const cl_MI& y)
  366. { return !x.ring()->equal(x,y); }
  367. inline bool operator== (const cl_MI& x, const cl_I& y)
  368. { return x.ring()->equal(x,x.ring()->canonhom(y)); }
  369. inline bool operator!= (const cl_MI& x, const cl_I& y)
  370. { return !x.ring()->equal(x,x.ring()->canonhom(y)); }
  371. inline bool operator== (const cl_I& x, const cl_MI& y)
  372. { return y.ring()->equal(y.ring()->canonhom(x),y); }
  373. inline bool operator!= (const cl_I& x, const cl_MI& y)
  374. { return !y.ring()->equal(y.ring()->canonhom(x),y); }
  375. // Compare against 0.
  376. inline cl_boolean zerop (const cl_MI& x)
  377. { return x.ring()->zerop(x); }
  378. // Multiply.
  379. inline const cl_MI operator* (const cl_MI& x, const cl_MI& y)
  380. { return x.ring()->mul(x,y); }
  381. // Squaring.
  382. inline const cl_MI square (const cl_MI& x)
  383. { return x.ring()->square(x); }
  384. // Exponentiation x^y, where y > 0.
  385. inline const cl_MI expt_pos (const cl_MI& x, const cl_I& y)
  386. { return x.ring()->expt_pos(x,y); }
  387. // Reciprocal.
  388. inline const cl_MI recip (const cl_MI& x)
  389. { return x.ring()->recip(x); }
  390. // Division.
  391. inline const cl_MI div (const cl_MI& x, const cl_MI& y)
  392. { return x.ring()->div(x,y); }
  393. inline const cl_MI div (const cl_MI& x, const cl_I& y)
  394. { return x.ring()->div(x,x.ring()->canonhom(y)); }
  395. inline const cl_MI div (const cl_I& x, const cl_MI& y)
  396. { return y.ring()->div(y.ring()->canonhom(x),y); }
  397. // Exponentiation x^y.
  398. inline const cl_MI expt (const cl_MI& x, const cl_I& y)
  399. { return x.ring()->expt(x,y); }
  400. // Scalar multiplication.
  401. inline const cl_MI operator* (const cl_I& x, const cl_MI& y)
  402. { return y.ring()->mul(y.ring()->canonhom(x),y); }
  403. inline const cl_MI operator* (const cl_MI& x, const cl_I& y)
  404. { return x.ring()->mul(x.ring()->canonhom(y),x); }
  405. // TODO: implement gcd, index (= gcd), unitp, sqrtp
  406. // Debugging support.
  407. #ifdef CL_DEBUG
  408. extern int cl_MI_debug_module;
  409. static void* const cl_MI_debug_dummy[] = { &cl_MI_debug_dummy,
  410. &cl_MI_debug_module
  411. };
  412. #endif
  413. } // namespace cln
  414. #endif /* _CL_MODINTEGER_H */