/* Check that the given Rabin signature is valid. */ int rs_validate_rabin (const mpz_t sig, /* purported signature of app */ int f, /* f value */ const mpz_t hash, /* MD5 hash of app */ const RSKey* key) /* key structure */ { mpz_t a, b; int result; if (!mpz_sgn(key->n)) { rs_error(key, NULL, "unable to validate: public key missing"); return RS_ERR_MISSING_PUBLIC_KEY; } if (f < 0 || f > 3) return RS_SIGNATURE_INCORRECT; mpz_init(a); mpz_init(b); mpz_mul(a, sig, sig); mpz_mod(a, a, key->n); applyf(b, hash, key->n, f); result = mpz_cmp(a, b); mpz_clear(a); mpz_clear(b); return (result ? RS_SIGNATURE_INCORRECT : RS_SUCCESS); }
int test_verify_many_floats(void) { float ret; int err = 0; arg_t args[] = { { T_FLOAT, (value_t)42.0f }, { T_FLOAT, (value_t)1.0f }, { T_FLOAT, (value_t)10.f }, { T_FLOAT, (value_t)100.f }, { T_FLOAT, (value_t)1000.f }, { T_FLOAT, (value_t)10000.f }, { T_FLOAT, (value_t)10000.f }, { T_FLOAT, (value_t)1000.f }, { T_FLOAT, (value_t)100.f }, { T_FLOAT, (value_t)10.f }, { T_FLOAT, (value_t)1.0f } }; ret = applyf(verify10f, args, len(args)); if (42.f == ret) printf("ok\ttest_verify_many_floats()\n"); else { printf("fail\tgot %f instead of 42.f\n", ret); err = 1; } return err; }
/* * Compute the Rabin signature with a given f. */ static void rabsigf(mpz_t res, /* mpz to store result */ const mpz_t m, /* MD5 hash */ const mpz_t n, /* public key */ const mpz_t p, /* first factor */ const mpz_t q, /* second factor */ const mpz_t qinv, /* q^(p-2) mod p */ int f, /* f (0, 1, 2, 3) */ int rootnum) /* root number (0, 1, 2, 3) */ { mpz_t mm; mpz_t r,s; mpz_init(r); mpz_init(s); mpz_init(mm); applyf(mm, m, n, f); mpz_sqrtm(r, mm, p); mpz_sqrtm(s, mm, q); if (rootnum & 1) { mpz_sub(r, p, r); } if (rootnum & 2) { mpz_sub(s, q, s); } mpz_crt(res, r, s, p, q, qinv); mpz_clear(r); mpz_clear(s); mpz_clear(mm); }
void levelorder(t_btree *root, void (*applyf)(void *item, int current_level, int is_first_elem), int *i, int *u) { t_iterativ **nxt; int *tab; t_iterativ *q; q = create_new(0, root, 0); tab = malloc(sizeof(int) * btree_level_count_if(root) * 10); nxt = malloc(sizeof(t_iterativ *) * btree_level_count_if(root) * 10); while (q) { applyf(q->node->item, q->level, q->is_first); if (q->node->left) { nxt[*i] = create_new(q, q->node->left, tab[q->level + 1]); *i += 1; tab[q->level + 1] = 1; } if (q->node->right) { nxt[*i] = create_new(q, q->node->right, tab[q->level + 1]); *i += 1; tab[q->level + 1] = 1; } q = nxt[*u]; *u += 1; } }
void btree_apply_prefix(t_btree *root, void (*applyf)(void *)) { applyf(root->item); if (root->left) btree_apply_prefix(root->left, (applyf)); if (root->right) btree_apply_prefix(root->right, (applyf)); }
void btree_apply_infix(t_btree *root, void (*applyf)(void *)) { if (root->left) btree_apply_infix(root->left, applyf); applyf(root->item); if (root->right) btree_apply_infix(root->right, applyf); }
void btree_apply_suffix(t_btree *root, void (*applyf)(void *)) { if (root) { btree_apply_suffix(root->left); btree_apply_suffix(root->right); applyf(root->item); } }
void ft_btree_apply_infix(t_btree *root, void (*applyf)(t_btree*)) { if (!root || !applyf) return ; ft_btree_apply_infix(root->left, applyf); applyf(root); ft_btree_apply_infix(root->right, applyf); return ; }
void btree_apply_suffix(t_btree *root, void (*applyf)(void *)) { if (root != NULL) { btree_apply_suffix(root->left, applyf); btree_apply_suffix(root->right, applyf); if (root->item != 0) applyf(root->item); } }
void btree_apply_prefix(t_btree *root, void (*applyf)(void *)) { if (root != 0) { applyf(root->item); if (root->left != 0) btree_apply_prefix(root->left, applyf); else if (root->right != 0) btree_apply_prefix(root->right, applyf); } }
void btree_apply_prefix(t_btree *root, void (*applyf)(void *)) { t_btree *a; a = root; applyf(a->item); if (a->left) btree_apply_prefix(a->left, applyf); if (a->right) btree_apply_prefix(a->right, applyf); }
void btree_apply_prefix(t_btree *root, void (*applyf)(void *)) { if (root && applyf) { applyf(root->data); if (root->left) btree_apply_prefix(root->left, applyf); if (root->right) btree_apply_prefix(root->right, applyf); } }
void ft_dlst_apply(t_dlist *head, void (*applyf)(t_dlist *)) { t_dlist *it; t_dlist *next; it = head->next; while (it != head) { next = it->next; applyf(it); it = next; } return ; }
void ft_btree_apply_suffix(t_btree *root, void (*applyf)(void *)) { if (root) { if (root->left) { ft_btree_apply_suffix(root->left, applyf); } if (root->right) { ft_btree_apply_suffix(root->right, applyf); } applyf(root->item); } }
void btree_apply_prefix(t_btree *root, void (*applyf)(void *)) { if (root) { applyf(root->item); printf("go gauche\n"); btree_apply_prefix(root->left, applyf); printf("go droite\n"); btree_apply_prefix(root->right, applyf); printf("done both of %s\n", (char*)root->item); } else { printf("going up\n"); } }
int test_sum_of_floats(void) { float ret; int err = 0; arg_t args[] = { { T_FLOAT, (value_t)1.0f }, { T_FLOAT, (value_t)10.f }, { T_FLOAT, (value_t)100.f }, { T_FLOAT, (value_t)1000.f }, { T_FLOAT, (value_t)10000.f } }; ret = applyf(sum5f, args, len(args)); if (11111.f == ret) printf("ok\ttest_sum_of_floats()\n"); else { printf("fail\tgot %f instead of 0.11111\n", ret); err = 1; } return err; }
/* * Compute the Rabin signature and the useful value of f. */ int rs_sign_rabin(mpz_t res, /* mpz to store signature */ int* f, /* f value chosen */ const mpz_t hash, /* MD5 hash of app */ int rootnum, /* root number (0, 1, 2, 3) */ RSKey* key) /* key structure */ { mpz_t mm; int mLp, mLq; int pm8, qm8; if (!mpz_sgn(key->n)) { rs_error(key, NULL, "unable to sign: public key missing"); return RS_ERR_MISSING_PUBLIC_KEY; } if (!mpz_sgn(key->p) || !mpz_sgn(key->q)) { rs_error(key, NULL, "unable to sign: private key missing"); return RS_ERR_MISSING_PRIVATE_KEY; } mpz_init(mm); /* Calculate q^-1 if necessary */ if (!mpz_sgn(key->qinv)) { #ifndef USE_MPZ_GCDEXT mpz_sub_ui(mm, key->p, 2); mpz_powm(key->qinv, key->q, mm, key->p); #else mpz_gcdext(mm, key->qinv, NULL, key->q, key->p); if (mpz_cmp_ui(mm, 1)) { mpz_clear(mm); rs_error(key, NULL, "unable to sign: unsuitable key"); return RS_ERR_UNSUITABLE_RABIN_KEY; } #endif } applyf(mm, hash, key->n, 2); mLp = mpz_legendre(mm, key->p); mLq = mpz_legendre(mm, key->q); pm8 = mpz_get_ui(key->p) % 8; qm8 = mpz_get_ui(key->q) % 8; if (pm8 == 1 || qm8 == 1 || (pm8 % 2) == 0 || (qm8 % 2) == 0) { mpz_clear(mm); rs_error(key, NULL, "unable to sign: unsuitable key"); return RS_ERR_UNSUITABLE_RABIN_KEY; } *f = ftab[(mLp == 1 ? 0 : 1) + (mLq == 1 ? 0 : 2) + (((qm8 - 3) / 2) * 4) + (((pm8 - 3) / 2) * 12)]; if (*f == 99) { mpz_clear(mm); rs_error(key, NULL, "unable to sign: unsuitable key"); return RS_ERR_UNSUITABLE_RABIN_KEY; } rabsigf(res, hash, key->n, key->p, key->q, key->qinv, *f, rootnum); mpz_clear(mm); return RS_SUCCESS; }