int inverse(bn_st * inv_in, bn_st * in, bn_st * modulus, int sizeof_mod) { // Compute the inverse of in using an extended Euclidian Algorithm // This cooresponds to the formula out = inv_in*in + useless*modulus // Where, since mod is prime, out should be equal to 1 and 1 = in*inv_in // Otherwise throws an error bn_mod_basic(in,in,modulus); bn_st out, useless; bn_new_size(&out, 2*sizeof_mod+1); bn_new_size(&useless, sizeof_mod); bn_gcd_ext(&out, inv_in, &useless, in, modulus); // Check if out is actually one bn_st is_one; bn_new_size(&is_one, sizeof_mod); bn_set_2b(&is_one, 0); // Set an integer to one if (bn_cmp(&out, &is_one) != CMP_EQ) { error_hdl(-1,"The modulus is not prime, can't calculate the inverse"); return 1; } bn_clean(&is_one); bn_clean(&out); bn_clean(&useless); return 0; }
void fp_prime_clean() { ctx_t *ctx = core_get(); ctx->fp_id = ctx->sps_len = 0; memset(ctx->sps, 0, sizeof(ctx->sps)); #if FP_RDC == MONTY || !defined(STRIP) bn_clean(&(ctx->one)); bn_clean(&(ctx->conv)); #endif bn_clean(&(ctx->prime)); }
void ep2_curve_clean(void) { ctx_t *ctx = core_get(); #ifdef EP_PRECO for (int i = 0; i < EP_TABLE; i++) { fp2_free(ctx->ep2_pre[i].x); fp2_free(ctx->ep2_pre[i].y); fp2_free(ctx->ep2_pre[i].z); } #endif bn_clean(&(ctx->ep2_r)); bn_clean(&(ctx->ep2_h)); }
int bn_free_array(bn_st * array, int size) { for (int i = size-1; i >= 0; --i) { bn_clean(array + i); } return 0; }
int bn_sqr_mod(bn_st * out, bn_st * in, bn_st * modulus) { bn_t temp; bn_new_size(temp, 2*RELIC_DIGS); bn_sqr_basic(temp, in); bn_mod_basic(out,temp, modulus); bn_clean(temp); return 0; }
void eb_curve_clean(void) { int i; #if ALLOC == STATIC fb_free(curve_g.x); fb_free(curve_g.y); fb_free(curve_g.z); for (i = 0; i < EB_TABLE; i++) { fb_free(table[i].x); fb_free(table[i].y); fb_free(table[i].z); } #endif bn_clean(&curve_r); #if defined(EB_KBLTZ) && (EB_MUL == LWNAF || !defined(STRIP)) bn_clean(&curve_vm); bn_clean(&curve_s0); bn_clean(&curve_s1); #endif }
void eb_curve_clean(void) { ctx_t *ctx = core_get(); #if ALLOC == STATIC fb_free(ctx->eb_g.x); fb_free(ctx->eb_g.y); fb_free(ctx->eb_g.z); for (int i = 0; i < EB_TABLE; i++) { fb_free(ctx->eb_pre[i].x); fb_free(ctx->eb_pre[i].y); fb_free(ctx->eb_pre[i].z); } #endif bn_clean(&(ctx->eb_r)); bn_clean(&(ctx->eb_h)); #if defined(EB_KBLTZ) && (EB_MUL == LWNAF || !defined(STRIP)) bn_clean(&(ctx->eb_vm)); bn_clean(&(ctx->eb_s0)); bn_clean(&(ctx->eb_s1)); #endif }
int bn_add_mod(bn_st * out, bn_st * in1, bn_st * in2, bn_st * modulus) { // Temporary variable in which to store the reuslt of the addition // before appplying the modular reduction bn_st temp; bn_new_size(&temp, RELIC_DIGS+1); // Addition bn_add(&temp,in1,in2); // Modular reduction bn_mod_basic(out, &temp, modulus); bn_clean(&temp); return 0; }
void ep_curve_clean(void) { ctx_t *ctx = core_get(); #if ALLOC == STATIC fp_free(ctx->ep_g.x); fp_free(ctx->ep_g.y); fp_free(ctx->ep_g.z); #ifdef EP_PRECO for (int i = 0; i < RELIC_EP_TABLE; i++) { fp_free(ctx->ep_pre[i].x); fp_free(ctx->ep_pre[i].y); fp_free(ctx->ep_pre[i].z); } #endif #endif bn_clean(&ctx->ep_r); bn_clean(&ctx->ep_h); #if defined(EP_ENDOM) && (EP_MUL == LWNAF || EP_FIX == LWNAF || !defined(STRIP)) for (int i = 0; i < 3; i++) { bn_clean(&(ctx->ep_v1[i])); bn_clean(&(ctx->ep_v2[i])); } #endif }
void fp_invn_low(dig_t *c, const dig_t *a) { bn_st e; bn_init(&e, RLC_FP_DIGS); e.used = RLC_FP_DIGS; dv_copy(e.dp, fp_prime_get(), RLC_FP_DIGS); bn_sub1_low(e.dp, e.dp, 2, RLC_FP_DIGS); #if AUTO == ALLOC fp_exp(c, a, &e); #else fp_exp(c, (const fp_t)a, &e); #endif bn_clean(&e); }