X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=bigint%2Fbigint.c;h=c1a82ab496689b854541fda034ca1a268cfcb9d4;hb=ad20a79e4270ac6d3d7c0af08efb287865dca42a;hp=f832d5fd8682b8c3fd55f798198d7a7dacaf8328;hpb=262edc3c040a073636f91af03f1b64030c5b1b31;p=arm-crypto-lib.git diff --git a/bigint/bigint.c b/bigint/bigint.c index f832d5f..c1a82ab 100644 --- a/bigint/bigint.c +++ b/bigint/bigint.c @@ -81,6 +81,53 @@ void bigint_adjust(bigint_t* a){ /******************************************************************************/ +uint16_t bigint_length_b(bigint_t* a){ + if(!a->length_B || a->length_B==0){ + return 0; + } + return (a->length_B-1) * BIGINT_WORD_SIZE + GET_FBS(a); +} + +/******************************************************************************/ + +uint16_t bigint_length_B(bigint_t* a){ + return (bigint_length_b(a)+7)/8; +} + +/******************************************************************************/ + +uint32_t bigint_get_first_set_bit(bigint_t* a){ + if(a->length_B==0){ + return (uint32_t)(-1); + } + return (a->length_B-1)*sizeof(bigint_word_t)*8+GET_FBS(a); +} + + +/******************************************************************************/ + +uint32_t bigint_get_last_set_bit(bigint_t* a){ + uint32_t r=0; + uint8_t b=0; + bigint_word_t x=1; + if(a->length_B==0){ + return (uint32_t)(-1); + } + while(a->wordv[r]==0 && rlength_B){ + ++r; + } + if(a->wordv[r] == 0){ + return (uint32_t)(-1); + } + while((x&a->wordv[r])==0){ + ++b; + x <<= 1; + } + return r*BIGINT_WORD_SIZE+b; +} + +/******************************************************************************/ + void bigint_copy(bigint_t* dest, const bigint_t* src){ memcpy(dest->wordv, src->wordv, src->length_B*sizeof(bigint_word_t)); dest->length_B = src->length_B; @@ -619,7 +666,7 @@ void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){ /******************************************************************************/ void bigint_reduce(bigint_t* a, const bigint_t* r){ -// bigint_adjust(r); +// bigint_adjust((bigint_t*)r); uint8_t rfbs = GET_FBS(r); // cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a); @@ -638,8 +685,13 @@ void bigint_reduce(bigint_t* a, const bigint_t* r){ } uint16_t shift; while(a->length_B > r->length_B){ - shift = (a->length_B-r->length_B)*8*sizeof(bigint_word_t)+GET_FBS(a)-rfbs-1; + shift = (a->length_B - r->length_B) * 8 * sizeof(bigint_word_t) + GET_FBS(a) - rfbs - 1; + if(a->wordv[a->length_B-1] > r->wordv[r->length_B-1]){ + shift += 1; + } // cli_putstr("\r\nDBG: (p) shift = "); cli_hexdump_rev(&shift, 2); +// cli_putstr(" a_len = "); cli_hexdump_rev(&a->length_B, 2); +// cli_putstr(" r_len = "); cli_hexdump_rev(&r->length_B, 2); // uart_flush(0); bigint_sub_u_bitscale(a, r, shift); // cli_putstr("\r\nDBG: (1) = "); bigint_print_hex(a); @@ -672,14 +724,21 @@ void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, con bigint_word_t t, base_b[MAX(a->length_B,r->length_B*2)], res_b[r->length_B*2]; uint16_t i; uint8_t j; +// uint16_t *xaddr = &i; +// cli_putstr("\r\npre-alloc ("); +// cli_hexdump_rev(&xaddr, 4); +// cli_putstr(") ..."); res.wordv = res_b; base.wordv = base_b; bigint_copy(&base, a); +// cli_putstr("\r\npost-copy"); bigint_reduce(&base, r); res.wordv[0]=1; res.length_B=1; res.info = 0; +// cli_putstr("\r\nadjust "); bigint_adjust(&res); +// cli_putstr("\r\nexpmod "); for(i=0; i+1length_B; ++i){ t=exp->wordv[i]; for(j=0; jwordv[i]; + +// cli_putc('+'); while(t){ if(t&1){ bigint_mul_u(&res, &res, &base);