3 void draw_poly(pb_poly
*a
)
11 for (x
= a
->used
- 1; x
>= 0; x
--) {
12 if (mp_iszero(&(a
->terms
[x
])) == MP_YES
) continue;
13 mp_toradix(&(a
->terms
[x
]), buf
, 10);
14 if ((x
!= a
->used
- 1) && a
->terms
[x
].sign
== MP_ZPOS
) {
17 printf(" %sx^%d ", buf
, x
);
20 if (mp_iszero(&(a
->characteristic
)) == MP_NO
) {
21 mp_toradix(&(a
->characteristic
), buf
, 10);
22 printf(" (mod %s)", buf
);
31 mp_int aa
,bb
,cc
,dd
,ee
;
35 mp_init_multi(&aa
,&bb
,&cc
,&dd
,&ee
,NULL
);
36 pb_init_size(&a
, &chara
, 32);
37 pb_init_size(&b
, &chara
, 32);
38 pb_init_size(&c
, &chara
, 32);
39 pb_init_size(&d
, &chara
, 32);
40 pb_init_size(&e
, &chara
, 32);
43 mp_set(&(a
.terms
[1]), 3);
44 mp_set(&(a
.terms
[0]), 4);
50 /* b = 7x^2 + 5x + 7 */
51 mp_set(&(b
.terms
[2]), 7);
52 mp_set(&(b
.terms
[1]), 5);
53 mp_set(&(b
.terms
[0]), 7);
69 /* now test clearing */
70 printf("Shifting previous up one\n");
77 printf("previous add (test if excess cleared)\n");
81 printf("Multiply:\n");
96 /* now hijack the char */
97 mp_set(&(a
.characteristic
), 17);
98 mp_set(&(b
.characteristic
), 17);
99 mp_set(&(c
.characteristic
), 17);
100 mp_set(&(d
.characteristic
), 17);
101 mp_set(&(e
.characteristic
), 17);
103 /* perform modular addition */
104 printf("a + b (in GF(17))\n");
110 /* perform modular subtaction */
111 printf("a - b (in GF(17))\n");
114 printf("b - a (in GF(17))\n");
118 /* perform division */
119 printf("Division (b/a)\n");
120 pb_div(&b
, &a
, &c
, &d
);
123 printf("Q == \n"); draw_poly(&c
);
124 printf("R == \n"); draw_poly(&d
);
129 printf("aQ + R =\n"); draw_poly(&c
);
133 printf("b mod a == "); draw_poly(&c
);
135 /* test GCD of (x^2 - 1) and 5*x^4+5*x^3+7*x^2+8*x+1 [should be x+1] */
136 printf("GCD Test\n");
138 mp_set(&(a
.terms
[2]), 1);
139 mp_set(&(a
.terms
[0]), 16);
146 mp_set(&(b
.terms
[4]), 5);
147 mp_set(&(b
.terms
[3]), 5);
148 mp_set(&(b
.terms
[2]), 7);
149 mp_set(&(b
.terms
[1]), 8);
150 mp_set(&(b
.terms
[0]), 1);
157 printf("GCD: "); draw_poly(&c
);
160 pb_div(&a
, &c
, &d
, &e
);
161 printf("a/c == "); draw_poly(&d
); printf("a mod c == "); draw_poly(&e
); pb_mul(&d
, &c
, &e
); printf("should be a: "); draw_poly(&e
);
162 pb_div(&b
, &c
, &d
, &e
);
163 printf("b/c == "); draw_poly(&d
); printf("b mod c == "); draw_poly(&e
); pb_mul(&d
, &c
, &e
); printf("should be b: "); draw_poly(&e
);
165 /* test modular inverse... x^2 + 3 or so should work nice */
166 printf("Modular Inverse\n");
168 mp_set(&(a
.terms
[2]), 1);
169 mp_set(&(a
.terms
[1]), 0);
170 mp_set(&(a
.terms
[0]), 3);
176 /* take inverse of 2x + 9 */
178 mp_set(&(b
.terms
[1]), 2);
179 mp_set(&(b
.terms
[0]), 9);
186 pb_invmod(&b
, &a
, &c
);
191 pb_mulmod(&b
, &c
, &a
, &d
);
193 printf("This should be 1 : "); draw_poly(&d
);
194 printf("This should be equal to k*a + 1: "); draw_poly(&e
);
196 /* now b has order [dividing] 17^2 - 1 == 288 so b^287 should equal c */
197 printf("exptmod test\n");
199 pb_exptmod(&b
, &aa
, &a
, &d
);
200 printf("This should be invmod : "); draw_poly(&d
);
203 printf("Irreducibility testing\n");
204 pb_isirreduc(&a
, &res
);
205 printf("This should be 1 : %d\n", res
);
207 pb_isirreduc(&b
, &res
);
208 printf("This should be 0 : %d\n", res
);