Home | History | Annotate | Download | only in bn

Lines Matching refs:mont

126 static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont);
130 BN_MONT_CTX *mont, BN_CTX *ctx)
135 int num = mont->N.top;
140 if (bn_mul_mont(r->d,a->d,b->d,mont->N.d,mont->n0,num))
165 if (!BN_from_montgomery_word(r,tmp,mont)) goto err;
167 if (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;
177 static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
183 n= &(mont->N);
184 /* mont->ri is the size of mont->N in bits (rounded up
186 al=ri=mont->ri/BN_BITS2;
208 n0=mont->n0[0];
244 /* mont->ri will be a multiple of the word size and below code
245 * is kind of BN_rshift(ret,r,mont->ri) equivalent */
327 if (BN_ucmp(ret, &(mont->N)) >= 0)
329 if (!BN_usub(ret,ret,&(mont->N))) return(0);
338 int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
347 retn = BN_from_montgomery_word(ret,t,mont);
358 BN_mask_bits(t1,mont->ri);
360 if (!BN_mul(t2,t1,&mont->Ni,ctx)) goto err;
361 BN_mask_bits(t2,mont->ri);
363 if (!BN_mul(t1,t2,&mont->N,ctx)) goto err;
365 if (!BN_rshift(ret,t2,mont->ri)) goto err;
367 if (BN_ucmp(ret, &(mont->N)) >= 0)
369 if (!BN_usub(ret,ret,&(mont->N))) goto err;
401 void BN_MONT_CTX_free(BN_MONT_CTX *mont)
403 if(mont == NULL)
406 BN_free(&(mont->RR));
407 BN_free(&(mont->N));
408 BN_free(&(mont->Ni));
409 if (mont->flags & BN_FLG_MALLOCED)
410 OPENSSL_free(mont);
413 int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
420 R= &(mont->RR); /* grab RR as a temp */
421 if (!BN_copy(&(mont->N),mod)) goto err; /* Set N */
422 mont->N.neg = 0;
434 mont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;
469 mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
470 mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;
493 mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
494 mont->n0[1] = 0;
499 mont->ri=BN_num_bits(&mont->N);
501 if (!BN_set_bit(R,mont->ri)) goto err; /* R = 2^ri */
503 if ((BN_mod_inverse(Ri,R,&mont->N,ctx)) == NULL)
505 if (!BN_lshift(Ri,Ri,mont->ri)) goto err; /* R*Ri */
508 if (!BN_div(&(mont->Ni),NULL,Ri,&mont->N,ctx)) goto err;
513 BN_zero(&(mont->RR));
514 if (!BN_set_bit(&(mont->RR),mont->ri*2)) goto err;
515 if (!BN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx)) goto err;