/* Construct a generic GFMethod for arithmetic over prime fields with * irreducible irr. */ GFMethod * GFMethod_consGFp_mont(const mp_int *irr) { mp_err res = MP_OKAY; int i; GFMethod *meth = NULL; mp_mont_modulus *mmm; meth = GFMethod_consGFp(irr); if (meth == NULL) return NULL; mmm = (mp_mont_modulus *) malloc(sizeof(mp_mont_modulus)); if (mmm == NULL) { res = MP_MEM; goto CLEANUP; } meth->field_mul = &ec_GFp_mul_mont; meth->field_sqr = &ec_GFp_sqr_mont; meth->field_div = &ec_GFp_div_mont; meth->field_enc = &ec_GFp_enc_mont; meth->field_dec = &ec_GFp_dec_mont; meth->extra1 = mmm; meth->extra2 = NULL; meth->extra_free = &ec_GFp_extra_free_mont; mmm->N = meth->irr; i = mpl_significant_bits(&meth->irr); i += MP_DIGIT_BIT - 1; mmm->b = i - i % MP_DIGIT_BIT; mmm->n0prime = 0 - s_mp_invmod_radix(MP_DIGIT(&meth->irr, 0)); CLEANUP: if (res != MP_OKAY) { GFMethod_free(meth); return NULL; } return meth; }
/* Construct a generic ECGroup for elliptic curves over prime fields. */ ECGroup * ECGroup_consGFp(const mp_int *irr, const mp_int *curvea, const mp_int *curveb, const mp_int *genx, const mp_int *geny, const mp_int *order, int cofactor) { mp_err res = MP_OKAY; ECGroup *group = NULL; group = ECGroup_new(); if (group == NULL) return NULL; group->meth = GFMethod_consGFp(irr); if (group->meth == NULL) { res = MP_MEM; goto CLEANUP; } MP_CHECKOK(mp_copy(curvea, &group->curvea)); MP_CHECKOK(mp_copy(curveb, &group->curveb)); MP_CHECKOK(mp_copy(genx, &group->genx)); MP_CHECKOK(mp_copy(geny, &group->geny)); MP_CHECKOK(mp_copy(order, &group->order)); group->cofactor = cofactor; group->point_add = &ec_GFp_pt_add_aff; group->point_sub = &ec_GFp_pt_sub_aff; group->point_dbl = &ec_GFp_pt_dbl_aff; group->point_mul = &ec_GFp_pt_mul_jm_wNAF; group->base_point_mul = NULL; group->points_mul = &ec_GFp_pts_mul_jac; group->validate_point = &ec_GFp_validate_point; CLEANUP: if (res != MP_OKAY) { ECGroup_free(group); return NULL; } return group; }