00001 /* 00002 * MCE, the real life implementation of McEliece encryption scheme. 00003 * Copyright Projet SECRET, INRIA, Rocquencourt and Bhaskar Biswas and 00004 * Nicolas Sendrier (Bhaskar.Biswas@inria.fr, Nicolas.Sendrier@inria.fr). 00005 * 00006 * This is free software; you can redistribute it and/or modify it 00007 * under the terms of the GNU Lesser General Public License as 00008 * published by the Free Software Foundation; either version 2.1 of 00009 * the License, or (at your option) any later version. 00010 * 00011 * This software is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this software; if not, write to the Free 00018 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 00019 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 00020 */ 00021 #ifndef GF_H 00022 #define GF_H 00023 00024 typedef unsigned short gf_t; 00025 00026 int gf_extension_degree, gf_cardinality, gf_multiplicative_order; 00027 gf_t * gf_log; 00028 gf_t * gf_exp; 00029 00030 /* MACROs for certain operations */ 00031 00032 #define gf_extd() gf_extension_degree 00033 #define gf_card() gf_cardinality 00034 #define gf_ord() gf_multiplicative_order 00035 00036 #define gf_unit() 1 00037 #define gf_zero() 0 00038 #define gf_add(x, y) ((x) ^ (y)) 00039 #define gf_exp(i) gf_exp[i] /* alpha^i */ 00040 #define gf_log(x) gf_log[x] /* return i when x=alpha^i */ 00041 00042 // residual modulo q-1 00043 // when -q < d < 0, we get (q-1+d) 00044 // when 0 <= d < q, we get (d) 00045 // when q <= d < 2q-1, we get (d-q+1) 00046 #define _gf_modq_1(d) (((d) & gf_ord()) + ((d) >> gf_extd())) 00047 /* we obtain a value between 0 and (q-1) included, the class of 0 is 00048 represented by 0 or q-1 (this is why we write _K->exp[q-1]=_K->exp[0]=1)*/ 00049 00050 #define gf_mul_fast(x, y) ((y) ? gf_exp[_gf_modq_1(gf_log[x] + gf_log[y])] : 0) 00051 #define gf_mul(x, y) ((x) ? gf_mul_fast(x, y) : 0) 00052 #define gf_square(x) ((x) ? gf_exp[_gf_modq_1(gf_log[x] << 1)] : 0) 00053 #define gf_sqrt(x) ((x) ? gf_exp[_gf_modq_1(gf_log[x] << (gf_extd()-1))] : 0) 00054 // Try to devide by zero and get what you deserve! 00055 #define gf_div(x, y) ((x) ? gf_exp[_gf_modq_1(gf_log[x] - gf_log[y])] : 0) 00056 #define gf_inv(x) gf_exp[gf_ord() - gf_log[x]] 00057 00058 /****** gf.c ******/ 00059 00060 int gf_init(int extdeg); 00061 gf_t gf_rand(int (*u8rnd)()); 00062 gf_t gf_pow(gf_t x, int i); 00063 00064 #endif /* GF_H */