2 * transsip - the telephony toolkit
3 * By Daniel Borkmann <daniel@transsip.org>
4 * Copyright 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>
5 * Subject to the GPL, version 2.
6 * Based on Bhaskar Biswas and Nicolas Sendrier McEliece
7 * implementation, LGPL 2.1.
15 typedef uint16_t gf16_t
;
17 extern uint32_t gf_extension_degree
, gf_cardinality
, gf_multiplicative_order
;
18 extern gf16_t
*gf_log_t
, *gf_exp_t
;
20 #define gf_extd() gf_extension_degree
21 #define gf_card() gf_cardinality
22 #define gf_ord() gf_multiplicative_order
27 #define gf_add(x, y) ((x) ^ (y))
28 #define gf_sub(x, y) ((x) ^ (y))
29 #define gf_exp(i) gf_exp_t[i] /* alpha^i */
30 #define gf_log(x) gf_log_t[x] /* return i when x=alpha^i */
32 #define gf_zdo(t, func) (!!(t) ? (func) : 0)
36 * when -q < d < 0, we get (q-1+d)
37 * when 0 <= d < q, we get (d)
38 * when q <= d < 2q-1, we get (d-q+1)
39 * we obtain a value between 0 and (q-1) included, the class of 0 is
40 * represented by 0 or q-1 (this is why we write
41 * _K->exp[q-1] = _K->exp[0] = 1)
43 #define _gf_modq_1(d) (((d) & gf_ord()) + ((d) >> gf_extd()))
45 #define gf_mul_fast(x, y) gf_zdo(y, gf_exp(_gf_modq_1(gf_log(x) + gf_log(y))))
46 #define gf_mul(x, y) gf_zdo(x, gf_mul_fast(x, y))
47 #define gf_square(x) gf_zdo(x, gf_exp(_gf_modq_1(gf_log(x) << 1)))
48 #define gf_sqrt(x) gf_zdo(x, gf_exp(_gf_modq_1(gf_log(x) << (gf_extd() - 1))))
49 #define gf_div(x, y) gf_zdo(x, gf_exp(_gf_modq_1(gf_log(x) - gf_log(y))))
50 #define gf_inv(x) gf_exp(gf_ord() - gf_log(x))
52 extern void gf_init(int extdeg
);
53 extern gf16_t
gf_rand(int (*rnd_u8
)(void));
54 extern gf16_t
gf_pow(gf16_t x
, int i
);