00001 
00002 #include <basix/int.hpp>
00003 #include <basix/vector.hpp>
00004 #include <basix/port.hpp>
00005 #include <basix/literal.hpp>
00006 #include <numerix/integer.hpp>
00007 #include <numerix/modular.hpp>
00008 #include <numerix/modular_integer.hpp>
00009 #include <basix/glue.hpp>
00010 
00011 #define int_literal(x) as_int (as_string (x))
00012 #define is_generic_literal is<literal>
00013 #define gen_literal_apply(f,v) gen (as<generic> (f), v)
00014 #define gen_literal_access(f,v) access (as<generic> (f), v)
00015 
00016 namespace mmx {
00017   static mmx_modular(int)
00018   GLUE_1 (const mmx_modular(int) &arg_1, const integer &arg_2) {
00019     return binpow (arg_1, arg_2);
00020   }
00021   
00022   static modulus<integer>
00023   GLUE_2 (const integer &arg_1) {
00024     return modulus<integer > (arg_1);
00025   }
00026   
00027   static modulus<integer>
00028   GLUE_3 (const integer &arg_1) {
00029     return modulus<integer > (arg_1);
00030   }
00031   
00032   static integer
00033   GLUE_4 (const modulus<integer> &arg_1) {
00034     return *arg_1;
00035   }
00036   
00037   static mmx_modular(integer)
00038   GLUE_5 (const integer &arg_1, const integer &arg_2) {
00039     return (mmx_modular(integer ) (arg_1, arg_2));
00040   }
00041   
00042   static mmx_modular(integer)
00043   GLUE_6 (const integer &arg_1, const modulus<integer> &arg_2) {
00044     return (mmx_modular(integer ) (arg_1, arg_2));
00045   }
00046   
00047   static mmx_modular(integer)
00048   GLUE_7 (const integer &arg_1, const modulus<integer> &arg_2) {
00049     return (mmx_modular(integer ) (arg_1, arg_2));
00050   }
00051   
00052   static modulus<integer>
00053   GLUE_8 (const mmx_modular(integer) &arg_1) {
00054     return get_modulus (arg_1);
00055   }
00056   
00057   static integer
00058   GLUE_9 (const mmx_modular(integer) &arg_1) {
00059     return *arg_1;
00060   }
00061   
00062   static mmx_modular(integer)
00063   GLUE_10 (const mmx_modular(integer) &arg_1) {
00064     return -arg_1;
00065   }
00066   
00067   static mmx_modular(integer)
00068   GLUE_11 (const mmx_modular(integer) &arg_1) {
00069     return square (arg_1);
00070   }
00071   
00072   static mmx_modular(integer)
00073   GLUE_12 (const mmx_modular(integer) &arg_1, const mmx_modular(integer) &arg_2) {
00074     return arg_1 + arg_2;
00075   }
00076   
00077   static mmx_modular(integer)
00078   GLUE_13 (const mmx_modular(integer) &arg_1, const mmx_modular(integer) &arg_2) {
00079     return arg_1 - arg_2;
00080   }
00081   
00082   static mmx_modular(integer)
00083   GLUE_14 (const mmx_modular(integer) &arg_1, const mmx_modular(integer) &arg_2) {
00084     return arg_1 * arg_2;
00085   }
00086   
00087   static mmx_modular(integer)
00088   GLUE_15 (const integer &arg_1, const mmx_modular(integer) &arg_2) {
00089     return arg_1 + arg_2;
00090   }
00091   
00092   static mmx_modular(integer)
00093   GLUE_16 (const mmx_modular(integer) &arg_1, const integer &arg_2) {
00094     return arg_1 + arg_2;
00095   }
00096   
00097   static mmx_modular(integer)
00098   GLUE_17 (const integer &arg_1, const mmx_modular(integer) &arg_2) {
00099     return arg_1 - arg_2;
00100   }
00101   
00102   static mmx_modular(integer)
00103   GLUE_18 (const mmx_modular(integer) &arg_1, const integer &arg_2) {
00104     return arg_1 - arg_2;
00105   }
00106   
00107   static mmx_modular(integer)
00108   GLUE_19 (const integer &arg_1, const mmx_modular(integer) &arg_2) {
00109     return arg_1 * arg_2;
00110   }
00111   
00112   static mmx_modular(integer)
00113   GLUE_20 (const mmx_modular(integer) &arg_1, const integer &arg_2) {
00114     return arg_1 * arg_2;
00115   }
00116   
00117   static mmx_modular(integer)
00118   GLUE_21 (const mmx_modular(integer) &arg_1, const mmx_modular(integer) &arg_2) {
00119     return arg_1 / arg_2;
00120   }
00121   
00122   static mmx_modular(integer)
00123   GLUE_22 (const integer &arg_1, const mmx_modular(integer) &arg_2) {
00124     return arg_1 / arg_2;
00125   }
00126   
00127   static mmx_modular(integer)
00128   GLUE_23 (const mmx_modular(integer) &arg_1, const integer &arg_2) {
00129     return arg_1 / arg_2;
00130   }
00131   
00132   static mmx_modular(integer)
00133   GLUE_24 (const mmx_modular(integer) &arg_1, const int &arg_2) {
00134     return binpow (arg_1, arg_2);
00135   }
00136   
00137   static mmx_modular(integer)
00138   GLUE_25 (const mmx_modular(integer) &arg_1, const integer &arg_2) {
00139     return binpow (arg_1, arg_2);
00140   }
00141   
00142   static bool
00143   GLUE_26 (const mmx_modular(integer) &arg_1, const mmx_modular(integer) &arg_2) {
00144     return arg_1 == arg_2;
00145   }
00146   
00147   static bool
00148   GLUE_27 (const mmx_modular(integer) &arg_1, const mmx_modular(integer) &arg_2) {
00149     return arg_1 != arg_2;
00150   }
00151   
00152   static bool
00153   GLUE_28 (const mmx_modular(integer) &arg_1, const integer &arg_2) {
00154     return arg_1 == arg_2;
00155   }
00156   
00157   static bool
00158   GLUE_29 (const mmx_modular(integer) &arg_1, const integer &arg_2) {
00159     return arg_1 != arg_2;
00160   }
00161   
00162   static bool
00163   GLUE_30 (const integer &arg_1, const mmx_modular(integer) &arg_2) {
00164     return arg_1 == arg_2;
00165   }
00166   
00167   static bool
00168   GLUE_31 (const integer &arg_1, const mmx_modular(integer) &arg_2) {
00169     return arg_1 != arg_2;
00170   }
00171   
00172   void
00173   glue_modular_integer () {
00174     static bool done = false;
00175     if (done) return;
00176     done = true;
00177     call_glue (string ("glue_int"));
00178     call_glue (string ("glue_integer"));
00179     call_glue (string ("glue_modular_int"));
00180     define_type<modulus<integer> > (gen (lit ("Modulus"), lit ("Integer")));
00181     define_type<mmx_modular(integer) > (gen (lit ("Modular"), lit ("Integer")));
00182     define ("^", GLUE_1);
00183     define ("modulus", GLUE_2);
00184     define_converter ("upgrade", GLUE_3, PENALTY_HOMOMORPHISM);
00185     define_converter (":>", GLUE_4, PENALTY_CAST);
00186     define ("modular", GLUE_5);
00187     define ("modular", GLUE_6);
00188     define ("mod", GLUE_7);
00189     define ("get_modulus", GLUE_8);
00190     define ("preimage", GLUE_9);
00191     define ("-", GLUE_10);
00192     define ("square", GLUE_11);
00193     define ("+", GLUE_12);
00194     define ("-", GLUE_13);
00195     define ("*", GLUE_14);
00196     define ("+", GLUE_15);
00197     define ("+", GLUE_16);
00198     define ("-", GLUE_17);
00199     define ("-", GLUE_18);
00200     define ("*", GLUE_19);
00201     define ("*", GLUE_20);
00202     define ("/", GLUE_21);
00203     define ("/", GLUE_22);
00204     define ("/", GLUE_23);
00205     define ("^", GLUE_24);
00206     define ("^", GLUE_25);
00207     define ("=", GLUE_26);
00208     define ("!=", GLUE_27);
00209     define ("=", GLUE_28);
00210     define ("!=", GLUE_29);
00211     define ("=", GLUE_30);
00212     define ("!=", GLUE_31);
00213   }
00214 }