/** * Test if (X + a) ^ n != X ^ n + a (mod X ^ r - 1,n) */ int check_poly(mpz_t n, mpz_t a, mpz_t r) { unsigned int i, terms, equality_holds; mpz_t tmp, neg_a, loop; mpz_init(tmp); mpz_init(neg_a); mpz_init(loop); terms = mpz_get_ui(r) + 1; mpz_t* poly = init_poly(terms); mpz_t* ptmp = init_poly(terms); mpz_t* stmp; mpz_mul_ui(neg_a, a, -1); mpz_set(poly[0], neg_a); mpz_set_ui(poly[1], 1); for (mpz_set_ui(loop, 2); mpz_cmp(loop, n) <= 0; mpz_mul(loop, loop, loop)) { polymul(ptmp, poly, terms, poly, terms, n); stmp = poly; poly = ptmp; ptmp = stmp; } mpz_t* xMinusA = init_poly(2); mpz_set(ptmp[0], neg_a); mpz_set_ui(ptmp[1], 1); for (; mpz_cmp(loop, n) <= 0; mpz_add_ui(loop, loop, 1)) { polymul(ptmp, poly, terms, xMinusA, 2, n); stmp = poly; poly = ptmp; ptmp = stmp; } clear_poly(xMinusA, 2); equality_holds = TRUE; if (mpz_cmp(poly[0], neg_a) != 0 || mpz_cmp_ui(poly[terms - 1], 1) != 0) { equality_holds = FALSE; } else { for (i = 1; i < terms - 1; i++) { if (mpz_cmp_ui(poly[i], 0) != 0) { equality_holds = FALSE; break; } } } clear_poly(poly, terms); clear_poly(ptmp, terms); mpz_clear(tmp); mpz_clear(neg_a); mpz_clear(loop); return equality_holds; }
int main() { poly a, b, c; init_poly(&a); init_poly(&b); init_poly(&c); //inits the polys a.quof[4]=2; a.quof[3]=-11; a.quof[2]=-6; a.quof[1]=64; a.quof[0]=32; //set a to someting; printandfind(&a); init_poly(&a); a.quof[2]=1; a.quof[1]=-7; a.quof[0]=12; printandfind(&a); return 0; }
bi_poly * create_poly(int size, int * ary, int len) { int i; bi_poly * ret = init_poly(size); for (i = 0; i < len; i++) flip_coeff(ret, ary[i]); ret->deg = ary[len - 1]; return ret; }
bi_poly * copy_poly(bi_poly * bp) { int i = 0; bi_poly * ret = init_poly(bp->sz); ret->deg = bp->deg; for (i = 0; i < bp->sz; i++) { ret->coeff[i] = bp->coeff[i]; } return ret; }
static void DoRPolys() { STri t; int i; #ifdef DBG printf("\nDoRPolys---------num = %d\n",axis[0].num); #endif for (i=0; i<axis[0].num; i++) { init_poly(); #ifdef DBG printf("---------\n"); #endif t = axis[0].t[i]; outv(&t.v); P_refl(&t,&t); outv(&t.v); Q_refl(&t,&t); outv(&t.v); P_refl(&t,&t); outv(&t.v); outpoly(); } }
static void DoQPolys() { int i,j; STri t; #ifdef DBG printf("\nDoQPolys---------num = %d, nqsides = %d\n",axis[2].num,nsides[2]); #endif for (i=0; i<axis[2].num; i++) { #ifdef DBG printf("---------\n"); #endif t = axis[2].t[i]; init_poly(); for (j=0; j<nsides[2]; j++) { P_refl(&t,&t); outv(&t.v); R_refl(&t,&t); outv(&t.v); } outpoly(); } }
// Decrypts the input using AES-128 driven by tablefile; stores in output[4][4] void decrypt_block(unsigned char input_block[16], unsigned char output_block[16], unsigned char round_key[44][4], FILE* table, bool first) { unsigned char state[4][4] = { { 0 } }; // Carries state of block through encryption unsigned char* inv_poly = (unsigned char*)calloc(1,4); // Initialize Substitution Boxes unsigned char* s_box = (unsigned char*)calloc(1,256); unsigned char* inv_s_box = (unsigned char*)calloc(1,256); init_s_box(s_box, table); invert_s_box(s_box, inv_s_box); free(s_box); init_poly(inv_poly, table, "INVP="); // Copy input to initital state for(int i=0; i<16; i++) { state[i%4][i/4] = input_block[i]; } // Print initial inputs and key schedule inv_print_state(state, 0, "iinput", first); inv_print_state(&(round_key[4*10]), 0, "ik_sch", first); // Add initial round key add_round_key(state, round_key, 10); // Perform operations for rounds 1 to 9 for(int i=9; i>0; i--) { inv_print_state(state, 10-i, "istart", first); // Inverse Shift Rows inv_shift_rows(state); inv_print_state(state, 10-i, "is_row", first); // Substitute Bytes sub_bytes(state, inv_s_box); inv_print_state(state, 10-i, "is_box", first); // Add Round Key inv_print_state(&(round_key[4*i]), 10-i, "ik_sch", first); add_round_key(state, round_key, i); inv_print_state(state, 10-i, "ik_add", first); // Mix Columns mix_columns(state, inv_poly); } // Final round operations inv_print_state(state, 10, "istart", first); // Shift Rows inv_shift_rows(state); inv_print_state(state, 10, "is_row", first); // Substitute Bytes sub_bytes(state, inv_s_box); inv_print_state(state, 10, "is_box", first); // Add Round Key add_round_key(state, round_key, 0); inv_print_state(&(round_key[4*0]), 10, "ik_sch", first); // Store final state value in output_block, done for(int i=0; i<16; i++) { output_block[i] = state[i%4][i/4]; } inv_print_state(state, 10, "ioutput", first); free(inv_poly); free(inv_s_box); }