From d796b12dbb838974705d995369161d3836f9dad2 Mon Sep 17 00:00:00 2001 From: bg Date: Fri, 14 Feb 2014 23:10:30 +0100 Subject: [PATCH] improving bigint stuff and fixing problems for 32-bit systems --- bigint/bigint.c | 209 ++++++++++++++++++++---------------- bigint/bigint.h | 22 +++- bigint/bigint_io.c | 20 ++-- host/bigint_test.rb | 6 +- test_src/main-bigint-test.c | 191 ++++++++++++++++---------------- 5 files changed, 248 insertions(+), 200 deletions(-) diff --git a/bigint/bigint.c b/bigint/bigint.c index af243cf..c5f799e 100644 --- a/bigint/bigint.c +++ b/bigint/bigint.c @@ -50,8 +50,8 @@ #define MIN(a,b) (((a) < (b)) ? (a) : (b)) #endif -#define SET_FBS(a, v) do{(a)->info &= ~BIGINT_FBS_MASK; (a)->info |= (v);} while(0) -#define GET_FBS(a) ((a)->info&BIGINT_FBS_MASK) +#define SET_FBS(a, v) do {(a)->info &= ~BIGINT_FBS_MASK; (a)->info |= (v);} while(0) +#define GET_FBS(a) ((a)->info & BIGINT_FBS_MASK) #define SET_NEG(a) (a)->info |= BIGINT_NEG_MASK #define SET_POS(a) (a)->info &= ~BIGINT_NEG_MASK #define XCHG(a,b) do{(a) ^= (b); (b) ^= (a); (a) ^= (b);} while(0) @@ -63,18 +63,18 @@ /******************************************************************************/ void bigint_adjust(bigint_t *a){ - while(a->length_W!=0 && a->wordv[a->length_W-1]==0){ + while (a->length_W != 0 && a->wordv[a->length_W - 1] == 0) { a->length_W--; } - if(a->length_W==0){ + if (a->length_W == 0) { a->info=0; return; } bigint_word_t t; - uint8_t i = BIGINT_WORD_SIZE-1; - t = a->wordv[a->length_W-1]; - while((t&(1L<<(BIGINT_WORD_SIZE-1)))==0 && i){ - t<<=1; + uint8_t i = BIGINT_WORD_SIZE - 1; + t = a->wordv[a->length_W - 1]; + while ((t & (((bigint_word_t)1) << (BIGINT_WORD_SIZE - 1))) == 0 && i) { + t <<= 1; i--; } SET_FBS(a, i); @@ -98,10 +98,10 @@ bigint_length_t bigint_length_B(const bigint_t *a){ /******************************************************************************/ uint32_t bigint_get_first_set_bit(const bigint_t *a){ - if(a->length_W==0){ + if(a->length_W == 0) { return (uint32_t)(-1); } - return (a->length_W-1)*sizeof(bigint_word_t)*8+GET_FBS(a); + return (a->length_W-1) * sizeof(bigint_word_t) * CHAR_BIT + GET_FBS(a); } @@ -222,7 +222,7 @@ void bigint_add_scale_u(bigint_t *dest, const bigint_t *a, bigint_length_t scale void bigint_sub_u(bigint_t *dest, const bigint_t *a, const bigint_t *b){ int8_t borrow = 0; int8_t r; - bigint_wordplus_signed_t t = 0LL; + bigint_wordplus_signed_t t = 0; bigint_length_t i; if(b->length_W == 0){ bigint_copy(dest, a); @@ -251,11 +251,7 @@ void bigint_sub_u(bigint_t *dest, const bigint_t *a, const bigint_t *b){ } t -= borrow; dest->wordv[i] = (bigint_word_t)t; - if(t<0){ - borrow = 1; - }else{ - borrow = 0; - } + borrow = t < 0 ? 1 : 0; } SET_POS(dest); dest->length_W = i; @@ -377,34 +373,37 @@ void bigint_shiftleft(bigint_t *a, bigint_length_t shift){ uint8_t bitshift; bigint_word_t *p; bigint_wordplus_t t = 0; - if(shift == 0){ + + if (shift == 0) { return; } byteshift = shift / 8; bitshift = shift & 7; - - if(byteshift){ + if (byteshift % sizeof(bigint_word_t)) { + a->wordv[a->length_W + byteshift / sizeof(bigint_t)] = 0; + } + if (byteshift) { memmove(((uint8_t*)a->wordv) + byteshift, a->wordv, a->length_W * sizeof(bigint_word_t)); memset(a->wordv, 0, byteshift); } - if(bitshift == 0){ + if (bitshift == 0) { a->length_W += (byteshift + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); bigint_adjust(a); return; } - p = a->wordv + byteshift / sizeof(bigint_word_t); - words_to_shift = a->length_W + (byteshift % sizeof(bigint_word_t)?1:0); - for(i=0; i < words_to_shift; ++i){ + p = &a->wordv[byteshift / sizeof(bigint_word_t)]; + words_to_shift = a->length_W + (byteshift % sizeof(bigint_word_t) ? 1 : 0); + for (i = 0; i < words_to_shift; ++i) { t |= ((bigint_wordplus_t)p[i]) << bitshift; p[i] = (bigint_word_t)t; t >>= BIGINT_WORD_SIZE; } - if(t){ + if (t) { p[i] = (bigint_word_t)t; a->length_W += 1; } a->length_W += (byteshift + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); - bigint_adjust(a); + bigint_adjust(a); } /******************************************************************************/ @@ -417,22 +416,19 @@ void bigint_shiftright(bigint_t *a, bigint_length_t shift){ byteshift = shift / 8; bitshift = shift & 7; - if(byteshift >= a->length_W * sizeof(bigint_word_t)){ /* we would shift out more than we have */ - bigint_set_zero(a); - return; - } - if(byteshift == a->length_W * sizeof(bigint_word_t) - 1 && bitshift > GET_FBS(a)){ + if(bigint_get_first_set_bit(a) < shift){ /* we would shift out more than we have */ bigint_set_zero(a); return; } if(byteshift){ memmove(a->wordv, (uint8_t*)a->wordv + byteshift, a->length_W * sizeof(bigint_word_t) - byteshift); + memset((uint8_t*)&a->wordv[a->length_W] - byteshift, 0, byteshift); } - byteshift /= sizeof(bigint_word_t); /* byteshift is now wordshift */ - a->length_W -= byteshift; - if(bitshift != 0 && a->length_W){ + a->length_W -= byteshift / sizeof(bigint_word_t); + + if(bitshift != 0 && a->length_W){ /* shift to the right */ i = a->length_W - 1; do{ @@ -690,7 +686,7 @@ void bigint_square(bigint_t *dest, const bigint_t *a){ /******************************************************************************/ void bigint_square(bigint_t *dest, const bigint_t *a) { - union { + union __attribute__((packed)) { bigint_word_t u[2]; bigint_wordplus_t uv; } acc; @@ -710,7 +706,7 @@ void bigint_square(bigint_t *dest, const bigint_t *a) { if(dest->wordv == a->wordv){ bigint_t d; - bigint_word_t d_b[a->length_W*2]; + bigint_word_t d_b[a->length_W * 2]; d.wordv = d_b; bigint_square(&d, a); bigint_copy(dest, &d); @@ -720,12 +716,12 @@ void bigint_square(bigint_t *dest, const bigint_t *a) { memset(dest->wordv, 0, a->length_W * 2 * sizeof(bigint_word_t)); for (i = 0; i < a->length_W; ++i) { - acc.uv = a->wordv[i] * a->wordv[i] + dest->wordv[2 * i]; + acc.uv = (bigint_wordplus_t)a->wordv[i] * (bigint_wordplus_t)a->wordv[i] + (bigint_wordplus_t)dest->wordv[2 * i]; dest->wordv[2 * i] = acc.u[0]; c1 = acc.u[1]; c2 = 0; for (j = i + 1; j < a->length_W; ++j) { - acc.uv = a->wordv[i] * a->wordv[j]; + acc.uv = (bigint_wordplus_t)a->wordv[i] * (bigint_wordplus_t)a->wordv[j]; q = acc.u[1] >> (BIGINT_WORD_SIZE - 1); acc.uv <<= 1; acc.uv += dest->wordv[i + j]; @@ -733,13 +729,15 @@ void bigint_square(bigint_t *dest, const bigint_t *a) { acc.uv += c1; q += (acc.uv < c1); dest->wordv[i + j] = acc.u[0]; - c1 = acc.u[1] + c2; + c1 = (bigint_wordplus_t)acc.u[1] + c2; c2 = q + (c1 < c2); } dest->wordv[i + a->length_W] += c1; - dest->wordv[i + a->length_W + 1] = c2 + (dest->wordv[i + a->length_W] < c1); + if (i < a->length_W - 1) { + dest->wordv[i + a->length_W + 1] = c2 + (dest->wordv[i + a->length_W] < c1); + } } - SET_POS(dest); + dest->info = 0; dest->length_W = 2 * a->length_W; bigint_adjust(dest); } @@ -751,7 +749,7 @@ void bigint_sub_u_bitscale(bigint_t *a, const bigint_t *b, bigint_length_t bitsc bigint_word_t tmp_b[b->length_W + 1]; const bigint_length_t word_shift = bitscale / BIGINT_WORD_SIZE; - if(a->length_W < b->length_W + word_shift){ + if (a->length_W < b->length_W + word_shift) { #if DEBUG cli_putstr("\r\nDBG: *bang*\r\n"); #endif @@ -775,32 +773,33 @@ void bigint_sub_u_bitscale(bigint_t *a, const bigint_t *b, bigint_length_t bitsc void bigint_reduce(bigint_t *a, const bigint_t *r){ uint8_t rfbs = GET_FBS(r); - if(r->length_W==0 || a->length_W==0){ + if (r->length_W == 0 || a->length_W == 0) { return; } - if(bigint_length_b(a) + 3 > bigint_length_b(r)){ - if((r->length_W*sizeof(bigint_word_t)<=4) && (a->length_W*sizeof(bigint_word_t)<=4)){ - uint32_t p=0, q=0; - memcpy(&p, a->wordv, a->length_W*sizeof(bigint_word_t)); - memcpy(&q, r->wordv, r->length_W*sizeof(bigint_word_t)); + if (bigint_length_b(a) + 3 > bigint_length_b(r)) { + if ((r->length_W * sizeof(bigint_word_t) <= 4) && (a->length_W * sizeof(bigint_word_t) <= 4)) { + uint32_t p = 0, q = 0; + memcpy(&p, a->wordv, a->length_W * sizeof(bigint_word_t)); + memcpy(&q, r->wordv, r->length_W * sizeof(bigint_word_t)); p %= q; - memcpy(a->wordv, &p, a->length_W*sizeof(bigint_word_t)); + memcpy(a->wordv, &p, a->length_W * sizeof(bigint_word_t)); + a->length_W = r->length_W; bigint_adjust(a); return; } - bigint_length_t shift; - while(a->length_W > r->length_W){ - shift = (a->length_W - r->length_W) * 8 * sizeof(bigint_word_t) + GET_FBS(a) - rfbs - 1; + unsigned shift; + while (a->length_W > r->length_W) { + shift = (a->length_W - r->length_W) * CHAR_BIT * sizeof(bigint_word_t) + GET_FBS(a) - rfbs - 1; bigint_sub_u_bitscale(a, r, shift); } - while((GET_FBS(a) > rfbs) && (a->length_W == r->length_W)){ - shift = GET_FBS(a)-rfbs-1; + while ((GET_FBS(a) > rfbs) && (a->length_W == r->length_W)) { + shift = GET_FBS(a) - rfbs - 1; bigint_sub_u_bitscale(a, r, shift); } } - while(bigint_cmp_u(a,r)>=0){ - bigint_sub_u(a,a,r); + while (bigint_cmp_u(a, r) >= 0) { + bigint_sub_u(a, a, r); } bigint_adjust(a); } @@ -816,17 +815,12 @@ void bigint_expmod_u_sam(bigint_t *dest, const bigint_t *a, const bigint_t *exp, } bigint_t res, base; - bigint_word_t t, base_w[MAX(a->length_W,r->length_W)], res_w[r->length_W*2]; + bigint_word_t t, base_w[MAX(a->length_W, r->length_W)], res_w[r->length_W * 2]; bigint_length_t i; uint8_t j; -// bigint_length_t *xaddr = &i; -// cli_putstr("\r\npre-alloc ("); -// cli_hexdump_rev(&xaddr, 4); -// cli_putstr(") ..."); res.wordv = res_w; base.wordv = base_w; bigint_copy(&base, a); -// cli_putstr("\r\npost-copy"); bigint_reduce(&base, r); res.wordv[0] = 1; res.length_W = 1; @@ -836,20 +830,20 @@ void bigint_expmod_u_sam(bigint_t *dest, const bigint_t *a, const bigint_t *exp, bigint_copy(dest, &res); return; } + bigint_copy(&res, &base); uint8_t flag = 0; - t=exp->wordv[exp->length_W - 1]; - for(i = exp->length_W; i > 0; --i){ + t = exp->wordv[exp->length_W - 1]; + for (i = exp->length_W; i > 0; --i) { t = exp->wordv[i - 1]; - for(j = BIGINT_WORD_SIZE; j > 0; --j){ - if(!flag){ - if(t & (1 << (BIGINT_WORD_SIZE - 1))){ + for (j = BIGINT_WORD_SIZE; j > 0; --j) { + if (!flag) { + if (t & (((bigint_word_t)1) << (BIGINT_WORD_SIZE - 1))) { flag = 1; } - } - if(flag){ + } else { bigint_square(&res, &res); bigint_reduce(&res, r); - if(t & (1 << (BIGINT_WORD_SIZE - 1))){ + if(t & (((bigint_word_t)1) << (BIGINT_WORD_SIZE - 1))){ bigint_mul_u(&res, &res, &base); bigint_reduce(&res, r); } @@ -858,7 +852,6 @@ void bigint_expmod_u_sam(bigint_t *dest, const bigint_t *a, const bigint_t *exp, } } -// cli_putc('+'); SET_POS(&res); bigint_copy(dest, &res); } @@ -964,8 +957,8 @@ void bigint_gcdext(bigint_t *gcd, bigint_t *a, bigint_t *b, const bigint_t *x, c d_.info = 0; bigint_set_zero(&b_); bigint_set_zero(&c_); - do{ - while((u.wordv[0] & 1) == 0){ + do { + while ((u.wordv[0] & 1) == 0) { bigint_shiftright(&u, 1); if((a_.wordv[0] & 1) || (b_.wordv[0] & 1)){ bigint_add_s(&a_, &a_, &y_); @@ -974,8 +967,8 @@ void bigint_gcdext(bigint_t *gcd, bigint_t *a, bigint_t *b, const bigint_t *x, c bigint_shiftright(&a_, 1); bigint_shiftright(&b_, 1); } - while((v.wordv[0] & 1) == 0){ - bigint_shiftright(&v, 1); + while ((v.wordv[0] & 1) == 0) { + bigint_shiftright(&v, 1); if((c_.wordv[0] & 1) || (d_.wordv[0] & 1)){ bigint_add_s(&c_, &c_, &y_); bigint_sub_s(&d_, &d_, &x_); @@ -992,7 +985,7 @@ void bigint_gcdext(bigint_t *gcd, bigint_t *a, bigint_t *b, const bigint_t *x, c bigint_sub_s(&c_, &c_, &a_); bigint_sub_s(&d_, &d_, &b_); } - }while(u.length_W); + } while(u.length_W); if(gcd){ bigint_mul_s(gcd, &v, &g); } @@ -1064,6 +1057,7 @@ void bigint_clip(bigint_t *dest, bigint_length_t length_W){ /* * m_ = m * m'[0] + * dest = (a * b) % m (?) */ void bigint_mont_mul(bigint_t *dest, const bigint_t *a, const bigint_t *b, const bigint_t *m, const bigint_t *m_){ @@ -1136,6 +1130,11 @@ void bigint_mont_red(bigint_t *dest, const bigint_t *a, const bigint_t *m, const void bigint_mont_gen_m_(bigint_t* dest, const bigint_t* m){ bigint_word_t x_w[2], m_w_0[1]; bigint_t x, m_0; +#if 0 + printf("\nm = "); + bigint_print_hex(m); + uart0_flush(); +#endif if (m->length_W == 0) { bigint_set_zero(dest); return; @@ -1158,8 +1157,25 @@ void bigint_mont_gen_m_(bigint_t* dest, const bigint_t* m){ m_0.wordv[0] = m->wordv[0]; bigint_adjust(&x); bigint_adjust(&m_0); +#if 0 + printf("\nm0 = "); + bigint_print_hex(&m_0); + printf("\nx = "); + bigint_print_hex(&x); + uart0_flush(); +#endif bigint_inverse(dest, &m_0, &x); +#if 0 + printf("\nm0^-1 = "); + bigint_print_hex(dest); + uart0_flush(); +#endif bigint_sub_s(&x, &x, dest); +#if 0 + printf("\n-m0^-1 = "); + bigint_print_hex(&x); + uart0_flush(); +#endif bigint_copy(dest, m); bigint_mul_word_u(dest, x.wordv[0]); } @@ -1175,7 +1191,7 @@ void bigint_mont_trans(bigint_t *dest, const bigint_t *a, const bigint_t *m){ t.wordv = t_w; memset(t_w, 0, m->length_W * sizeof(bigint_word_t)); - memcpy(t_w + m->length_W * sizeof(bigint_word_t), a->wordv, a->length_W * sizeof(bigint_word_t)); + memcpy(&t_w[m->length_W], a->wordv, a->length_W * sizeof(bigint_word_t)); t.info = a->info; t.length_W = a->length_W + m->length_W; bigint_reduce(&t, m); @@ -1186,23 +1202,18 @@ void bigint_mont_trans(bigint_t *dest, const bigint_t *a, const bigint_t *m){ /* calculate dest = a**exp % r */ /* using square&multiply */ -void bigint_expmod_u_mont_sam(bigint_t *dest, const bigint_t *a, const bigint_t *exp, const bigint_t *r){ +void bigint_expmod_u_mont_accel(bigint_t *dest, const bigint_t *a, const bigint_t *exp, const bigint_t *r, const bigint_t *m_){ if(r->length_W == 0) { return; } - if(a->length_W == 0) { - bigint_set_zero(dest); - return; - } + bigint_length_t s = r->length_W; - bigint_t res, m_, ax; - bigint_word_t t, res_w[r->length_W*2], ax_w[MAX(s, a->length_W)]; - bigint_word_t m_w_[s + 1]; + bigint_t res, ax; + bigint_word_t t, res_w[r->length_W * 2], ax_w[MAX(s, a->length_W)]; bigint_length_t i; uint8_t j; res.wordv = res_w; - m_.wordv = m_w_; ax.wordv = ax_w; res.wordv[0] = 1; @@ -1217,29 +1228,45 @@ void bigint_expmod_u_mont_sam(bigint_t *dest, const bigint_t *a, const bigint_t bigint_reduce(&ax, r); bigint_mont_trans(&ax, &ax, r); bigint_mont_trans(&res, &res, r); - bigint_mont_gen_m_(&m_, r); uint8_t flag = 0; t = exp->wordv[exp->length_W - 1]; for (i = exp->length_W; i > 0; --i) { t = exp->wordv[i - 1]; for(j = BIGINT_WORD_SIZE; j > 0; --j){ if (!flag) { - if(t & (1 << (BIGINT_WORD_SIZE - 1))){ + if(t & (((bigint_wordplus_t)1) << (BIGINT_WORD_SIZE - 1))){ flag = 1; } } if (flag) { bigint_square(&res, &res); - bigint_mont_red(&res, &res, r, &m_); - if (t & (1 << (BIGINT_WORD_SIZE - 1))) { - bigint_mont_mul(&res, &res, &ax, r, &m_); + bigint_mont_red(&res, &res, r, m_); + if (t & (((bigint_wordplus_t)1) << (BIGINT_WORD_SIZE - 1))) { + bigint_mont_mul(&res, &res, &ax, r, m_); } } t <<= 1; } } SET_POS(&res); - bigint_mont_red(dest, &res, r, &m_); + bigint_mont_red(dest, &res, r, m_); +} + +/******************************************************************************/ + +void bigint_expmod_u_mont_sam(bigint_t *dest, const bigint_t *a, const bigint_t *exp, const bigint_t *r){ + if(r->length_W == 0) { + return; + } + if(a->length_W == 0) { + bigint_set_zero(dest); + return; + } + bigint_t m_; + bigint_word_t m_w_[r->length_W + 1]; + m_.wordv = m_w_; + bigint_mont_gen_m_(&m_, r); + bigint_expmod_u_mont_accel(dest, a, exp, r,&m_); } /******************************************************************************/ diff --git a/bigint/bigint.h b/bigint/bigint.h index f42ae18..f909795 100644 --- a/bigint/bigint.h +++ b/bigint/bigint.h @@ -30,13 +30,28 @@ #include #include +#include +#define BIGINT_WORD_SIZE 8 + +#if BIGINT_WORD_SIZE == 8 typedef uint8_t bigint_word_t; typedef uint16_t bigint_wordplus_t; typedef int16_t bigint_wordplus_signed_t; -#define BIGINT_WORD_SIZE 8 - -#define BIGINT_FBS_MASK (BIGINT_WORD_SIZE-1) /* the last five bits indicate which is the first bit set */ +#elif BIGINT_WORD_SIZE == 16 +typedef uint16_t bigint_word_t; +typedef uint32_t bigint_wordplus_t; +typedef int32_t bigint_wordplus_signed_t; +#elif BIGINT_WORD_SIZE == 32 +typedef uint32_t bigint_word_t; +typedef uint64_t bigint_wordplus_t; +typedef int64_t bigint_wordplus_signed_t; +#else +#error "INVALID VALUE FOR BIGINT_WORD_SIZE" +#endif + + +#define BIGINT_FBS_MASK (BIGINT_WORD_SIZE - 1) /* the last five bits indicate which is the first bit set */ #define BIGINT_NEG_MASK 0x80 /* this bit indicates a negative value */ typedef size_t bigint_length_t; @@ -87,6 +102,7 @@ void bigint_mont_trans(bigint_t *dest, const bigint_t *a, const bigint_t *m); void bigint_expmod_u(bigint_t *dest, const bigint_t *a, const bigint_t *exp, const bigint_t *r); void bigint_expmod_u_sam(bigint_t *dest, const bigint_t *a, const bigint_t *exp, const bigint_t *r); void bigint_expmod_u_mont_sam(bigint_t *dest, const bigint_t *a, const bigint_t *exp, const bigint_t *r); +void bigint_expmod_u_mont_accel(bigint_t *dest, const bigint_t *a, const bigint_t *exp, const bigint_t *r, const bigint_t *m_); #endif /*BIGINT_H_*/ diff --git a/bigint/bigint_io.c b/bigint/bigint_io.c index 97c6846..44917b4 100644 --- a/bigint/bigint_io.c +++ b/bigint/bigint_io.c @@ -90,20 +90,21 @@ uint8_t bigint_read_hex_echo(bigint_t *a) { uint16_t allocated = 0; uint8_t shift4 = 0; uint16_t t, idx = 0; + uint8_t *buf = NULL; a->length_W = 0; a->wordv = NULL; a->info = 0; for (;;) { if (allocated - idx < 1) { - bigint_word_t *p; - p = realloc(a->wordv, allocated += BLOCKSIZE); + uint8_t *p; + p = realloc(buf, (allocated += BLOCKSIZE) * sizeof(bigint_word_t)); if (p == NULL) { cli_putstr("\r\nERROR: Out of memory!"); - free(a->wordv); + free(buf); return 0xff; } - memset((uint8_t*)p + allocated - BLOCKSIZE, 0, BLOCKSIZE); - a->wordv = p; + memset((uint8_t*)p + (allocated - BLOCKSIZE) * sizeof(bigint_word_t), 0, BLOCKSIZE * sizeof(bigint_word_t)); + buf = p; } t = read_byte(); if (idx == 0) { @@ -120,11 +121,11 @@ uint8_t bigint_read_hex_echo(bigint_t *a) { } } if (t <= 0x00ff) { - ((uint8_t*)(a->wordv))[idx++] = (uint8_t)t; + buf[idx++] = (uint8_t)t; } else { if (t & 0x0200) { shift4 = 1; - ((uint8_t*)(a->wordv))[idx++] = (uint8_t)((t & 0x0f) << 4); + buf[idx++] = (uint8_t)((t & 0x0f) << 4); } break; } @@ -133,14 +134,15 @@ uint8_t bigint_read_hex_echo(bigint_t *a) { uint8_t tmp; uint8_t *p, *q; a->length_W = (idx + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); - p = (uint8_t*)(a->wordv); - q = (uint8_t*)a->wordv + a->length_W * sizeof(bigint_word_t) - 1; + p = buf; + q = buf + idx - 1; while (q > p) { tmp = *p; *p = *q; *q = tmp; p++; q--; } + a->wordv = (bigint_word_t*)buf; bigint_adjust(a); if (shift4) { bigint_shiftright(a, 4); diff --git a/host/bigint_test.rb b/host/bigint_test.rb index 90c46b3..37bd0dd 100644 --- a/host/bigint_test.rb +++ b/host/bigint_test.rb @@ -172,7 +172,7 @@ def screen_progress(v) end putc((v)?('*'):('!')) $testno += 1 - $linepos = ($linepos+1)%$linewidth + $linepos = ($linepos+1) % $linewidth end ################################################################################ @@ -850,7 +850,7 @@ def run_test_expmodmont(skip=0) screen_progress(v) end (0..16).each do |i| - b_size = rand(length_b_B+1) + b_size = rand(length_b_B + 10) a = rand(256 ** length_a_B) b = rand(256 ** b_size) + 1 c = rand(256 ** b_size) / 2 * 2 +1 @@ -988,7 +988,7 @@ if File.exists?(logfilename) n1 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt') n2 = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt') File.rename(n1, n2) - printf("%s -> %s\n", n1, n2) +# printf("%s -> %s\n", n1, n2) i-=1 end n1 = sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt') diff --git a/test_src/main-bigint-test.c b/test_src/main-bigint-test.c index 41dd45b..caf5b15 100644 --- a/test_src/main-bigint-test.c +++ b/test_src/main-bigint-test.c @@ -32,15 +32,18 @@ char *algo_name = "BigInt"; +#define MAX(a,b) ((a) > (b) ? (a) : (b)) +#define MIN(a,b) ((a) < (b) ? (a) : (b)) + /***************************************************************************** * additional validation-functions * *****************************************************************************/ -void test_echo_bigint(void){ +void test_echo_bigint(void) { bigint_t a; cli_putstr_P(PSTR("\r\necho test\r\n")); - for(;;){ + for (;;) { cli_putstr_P(PSTR("\r\nenter hex number:")); - if(bigint_read_hex_echo(&a)){ + if (bigint_read_hex_echo(&a)) { cli_putstr_P(PSTR("\r\n end echo test")); return; } @@ -54,14 +57,14 @@ void test_echo_bigint(void){ void test_add_bigint(void){ bigint_t a, b, c; cli_putstr_P(PSTR("\r\nadd test\r\n")); - for(;;){ + for (;;) { cli_putstr_P(PSTR("\r\nenter a:")); - if(bigint_read_hex_echo(&a)){ + if (bigint_read_hex_echo(&a)) { cli_putstr_P(PSTR("\r\n end add test")); return; } cli_putstr_P(PSTR("\r\nenter b:")); - if(bigint_read_hex_echo(&b)){ + if (bigint_read_hex_echo(&b)) { free(a.wordv); cli_putstr_P(PSTR("\r\n end add test")); return; @@ -71,9 +74,9 @@ void test_add_bigint(void){ cli_putstr_P(PSTR(" + ")); bigint_print_hex(&b); cli_putstr_P(PSTR(" = ")); - uint8_t *c_b; - c_b = malloc(((a.length_W>b.length_W)?a.length_W:b.length_W)+2); - if(c_b==NULL){ + bigint_word_t *c_b; + c_b = malloc((MAX(a.length_W, b.length_W) + 2) * sizeof(bigint_word_t)); + if(c_b == NULL){ cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); free(a.wordv); free(b.wordv); @@ -93,14 +96,14 @@ void test_add_scale_bigint(void){ bigint_t a, b, c; uint16_t scale; cli_putstr_P(PSTR("\r\nadd-scale test\r\n")); - for(;;){ + for (;;) { cli_putstr_P(PSTR("\r\nenter a:")); - if(bigint_read_hex_echo(&a)){ + if (bigint_read_hex_echo(&a)) { cli_putstr_P(PSTR("\r\n end add-scale test")); return; } cli_putstr_P(PSTR("\r\nenter b:")); - if(bigint_read_hex_echo(&b)){ + if (bigint_read_hex_echo(&b)) { cli_putstr_P(PSTR("\r\n end add-scale test")); return; } @@ -117,8 +120,8 @@ void test_add_scale_bigint(void){ return; } */ - uint8_t *c_b; - c_b = malloc(((a.length_W>(b.length_W+scale))?a.length_W:(b.length_W+scale))+2); + bigint_word_t *c_b; + c_b = malloc((MAX(a.length_W, b.length_W+scale) + 2) * sizeof(bigint_word_t)); if(c_b==NULL){ cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); free(a.wordv); @@ -146,14 +149,14 @@ void test_add_scale_bigint(void){ void test_mul_bigint(void){ bigint_t a, b, c; cli_putstr_P(PSTR("\r\nmul test\r\n")); - for(;;){ + for (;;) { cli_putstr_P(PSTR("\r\nenter a:")); - if(bigint_read_hex_echo(&a)){ + if (bigint_read_hex_echo(&a)) { cli_putstr_P(PSTR("\r\n end mul test")); return; } cli_putstr_P(PSTR("\r\nenter b:")); - if(bigint_read_hex_echo(&b)){ + if (bigint_read_hex_echo(&b)) { free(a.wordv); cli_putstr_P(PSTR("\r\n end mul test")); return; @@ -163,9 +166,9 @@ void test_mul_bigint(void){ cli_putstr_P(PSTR(" * ")); bigint_print_hex(&b); cli_putstr_P(PSTR(" = ")); - uint8_t *c_b; - c_b = malloc((((a.length_W>b.length_W)?a.length_W:b.length_W)+1)*2); - if(c_b==NULL){ + bigint_word_t *c_b; + c_b = malloc((MAX(a.length_W, b.length_W) + 1) * 2 * sizeof(bigint_word_t)); + if (c_b==NULL) { cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); free(a.wordv); free(b.wordv); @@ -185,20 +188,20 @@ void test_mul_mont_bigint(void){ bigint_t a, b, c, a_, b_, m_, res; bigint_length_t s; cli_putstr_P(PSTR("\r\nmul-mont test ( (a * b) % c )\r\n")); - for(;;){ + for (;;) { cli_putstr_P(PSTR("\r\nenter a:")); - if(bigint_read_hex_echo(&a)){ + if (bigint_read_hex_echo(&a)) { cli_putstr_P(PSTR("\r\n end mul test")); return; } cli_putstr_P(PSTR("\r\nenter b:")); - if(bigint_read_hex_echo(&b)){ + if (bigint_read_hex_echo(&b)) { free(a.wordv); cli_putstr_P(PSTR("\r\n end mul test")); return; } cli_putstr_P(PSTR("\r\nenter c:")); - if(bigint_read_hex_echo(&c)){ + if (bigint_read_hex_echo(&c)) { free(a.wordv); free(b.wordv); cli_putstr_P(PSTR("\r\n end mul test")); @@ -234,14 +237,14 @@ void test_mul_word_bigint(void){ bigint_t a, b; bigint_word_t *t; cli_putstr_P(PSTR("\r\nmul test\r\n")); - for(;;){ + for (;;) { cli_putstr_P(PSTR("\r\nenter a:")); - if(bigint_read_hex_echo(&a)){ + if (bigint_read_hex_echo(&a)) { cli_putstr_P(PSTR("\r\n end mul test")); return; } cli_putstr_P(PSTR("\r\nenter b:")); - if(bigint_read_hex_echo(&b)){ + if (bigint_read_hex_echo(&b)) { free(a.wordv); cli_putstr_P(PSTR("\r\n end mul test")); return; @@ -252,14 +255,14 @@ void test_mul_word_bigint(void){ bigint_print_hex(&b); cli_putstr_P(PSTR(" = ")); - if(b.length_W > 1){ + if (b.length_W > 1) { free(a.wordv); free(b.wordv); cli_putstr_P(PSTR("\r\n end mul test")); } - t = realloc(a.wordv, a.length_W + 3); - if(t == NULL){ + t = realloc(a.wordv, (a.length_W + 3) * sizeof(bigint_word_t)); + if (t == NULL) { cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); free(a.wordv); free(b.wordv); @@ -286,9 +289,9 @@ void test_square_bigint(void){ cli_putstr_P(PSTR("\r\n ")); bigint_print_hex(&a); cli_putstr_P(PSTR("**2 = ")); - uint8_t *c_b; - c_b = malloc(a.length_W*2); - if(c_b==NULL){ + bigint_word_t *c_b; + c_b = malloc(a.length_W * 2 * sizeof(bigint_word_t)); + if(c_b == NULL){ cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); free(a.wordv); continue; @@ -305,14 +308,14 @@ void test_square_bigint(void){ void test_reduce_bigint(void){ bigint_t a, b; cli_putstr_P(PSTR("\r\nreduce test\r\n")); - for(;;){ + for (;;) { cli_putstr_P(PSTR("\r\nenter a:")); - if(bigint_read_hex_echo(&a)){ + if (bigint_read_hex_echo(&a)) { cli_putstr_P(PSTR("\r\n end reduce test")); return; } cli_putstr_P(PSTR("\r\nenter b:")); - if(bigint_read_hex_echo(&b)){ + if (bigint_read_hex_echo(&b)) { free(a.wordv); cli_putstr_P(PSTR("\r\n end reduce test")); return; @@ -332,28 +335,28 @@ void test_reduce_bigint(void){ /* d = a**b % c */ void test_expmod_bigint(void){ bigint_t a, b, c, d; - uint8_t *d_b; + bigint_word_t *d_b; cli_putstr_P(PSTR("\r\nexpnonentiation-modulo test\r\n")); - for(;;){ + for (;;) { cli_putstr_P(PSTR("\r\nenter a:")); - if(bigint_read_hex_echo(&a)){ + if (bigint_read_hex_echo(&a)) { cli_putstr_P(PSTR("\r\n end expmod test")); return; } cli_putstr_P(PSTR("\r\nenter b:")); - if(bigint_read_hex_echo(&b)){ + if (bigint_read_hex_echo(&b)) { free(a.wordv); cli_putstr_P(PSTR("\r\n end expmod test")); return; } cli_putstr_P(PSTR("\r\nenter c:")); - if(bigint_read_hex_echo(&c)){ + if (bigint_read_hex_echo(&c)) { free(a.wordv); free(b.wordv); cli_putstr_P(PSTR("\r\n end expmod test")); return; } - d_b = malloc(c.length_W); + d_b = malloc(c.length_W * sizeof(bigint_word_t)); if(d_b==NULL){ cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); free(a.wordv); @@ -369,7 +372,7 @@ void test_expmod_bigint(void){ cli_putstr_P(PSTR(" % ")); bigint_print_hex(&c); cli_putstr_P(PSTR(" = ")); - bigint_expmod_u(&d, &a, &b, &c); + bigint_expmod_u_sam(&d, &a, &b, &c); bigint_print_hex(&d); cli_putstr_P(PSTR("\r\n")); free(a.wordv); @@ -383,29 +386,29 @@ void test_expmod_bigint(void){ /* d = a**b % c */ void test_expmod_mont_bigint(void){ bigint_t a, b, c, d; - uint8_t *d_b; + bigint_word_t *d_b; cli_putstr_P(PSTR("\r\nexpnonentiation-modulo-montgomory test\r\n")); - for(;;){ + for (;;) { cli_putstr_P(PSTR("\r\nenter a:")); - if(bigint_read_hex_echo(&a)){ + if (bigint_read_hex_echo(&a)) { cli_putstr_P(PSTR("\r\n end expmod test")); return; } cli_putstr_P(PSTR("\r\nenter b:")); - if(bigint_read_hex_echo(&b)){ + if (bigint_read_hex_echo(&b)) { free(a.wordv); cli_putstr_P(PSTR("\r\n end expmod test")); return; } cli_putstr_P(PSTR("\r\nenter c:")); - if(bigint_read_hex_echo(&c)){ + if (bigint_read_hex_echo(&c)) { free(a.wordv); free(b.wordv); cli_putstr_P(PSTR("\r\n end expmod test")); return; } - d_b = malloc(c.length_W); - if(d_b==NULL){ + d_b = malloc(c.length_W * sizeof(bigint_word_t)); + if (d_b == NULL) { cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); free(a.wordv); free(b.wordv); @@ -434,21 +437,21 @@ void test_expmod_mont_bigint(void){ void test_gcdext_bigint(void){ bigint_t a, b, c, d, e; cli_putstr_P(PSTR("\r\ngcdext test\r\n")); - for(;;){ + for (;;) { cli_putstr_P(PSTR("\r\nenter a:")); - if(bigint_read_hex_echo(&a)){ + if (bigint_read_hex_echo(&a)) { cli_putstr_P(PSTR("\r\n end gcdext test")); return; } cli_putstr_P(PSTR("\r\nenter b:")); - if(bigint_read_hex_echo(&b)){ + if (bigint_read_hex_echo(&b)) { free(a.wordv); cli_putstr_P(PSTR("\r\n end gcdext test")); return; } - c.wordv = malloc((a.length_Wb.length_W)?a.length_W:b.length_W); - e.wordv = malloc(1+(a.length_W>b.length_W)?a.length_W:b.length_W); + c.wordv = malloc(MIN(a.length_W, b.length_W) * sizeof(bigint_word_t)); + d.wordv = malloc((MAX(a.length_W, b.length_W) + 1) * sizeof(bigint_word_t)); + e.wordv = malloc((MAX(a.length_W, b.length_W) + 1) * sizeof(bigint_word_t)); cli_putstr_P(PSTR("\r\n gcdext( ")); bigint_print_hex(&a); @@ -474,10 +477,10 @@ void test_gcdext_bigint(void){ void test_simple(void){ bigint_t a, b, c; - uint8_t a_b[1], b_b[1], c_b[2]; - a.wordv=a_b; - b.wordv=b_b; - c.wordv=c_b; + bigint_word_t a_b[1], b_b[1], c_b[2]; + a.wordv = a_b; + b.wordv = b_b; + c.wordv = c_b; a.length_W = 1; b.length_W = 1; a_b[0] = 1; @@ -517,12 +520,12 @@ void test_mul_simple(void){ uint8_t b_b[8] = {0xe6, 0xdd, 0xce, 0x00, 0x44, 0x60, 0xda, 0x0d}; uint8_t c_b[16]; - a.wordv=a_b; - b.wordv=b_b; - c.wordv=c_b; - a.length_W = 8; - b.length_W = 8; - a.info=0x80; + a.wordv = (bigint_word_t*)a_b; + b.wordv = (bigint_word_t*)b_b; + c.wordv = (bigint_word_t*)c_b; + a.length_W = 8 / sizeof(bigint_word_t); + b.length_W = 8 / sizeof(bigint_word_t); + a.info = 0x80; bigint_adjust(&a); bigint_adjust(&b); bigint_mul_s(&c, &a, &b); @@ -558,12 +561,12 @@ const uint8_t square_test_data[] PROGMEM = { void test_square_simple(void){ bigint_t a, c; - uint8_t a_b[11] = {0xe6, 0x70, 0x7d, 0x43, 0x74, 0x07, 0x20, 0x22, 0x6a, 0xb8, 0xf4}; - uint8_t c_b[22]; - a.wordv=a_b; - c.wordv=c_b; - a.length_W = 11; - a.info=0x00; + uint8_t a_b[16] = {0xe6, 0x70, 0x7d, 0x43, 0x74, 0x07, 0x20, 0x22, 0x6a, 0xb8, 0xf4, 0, 0, 0, 0, 0}; + uint8_t c_b[32]; + a.wordv = (bigint_word_t*)a_b; + c.wordv = (bigint_word_t*)c_b; + a.length_W = 16 / sizeof(bigint_word_t); + a.info = 0x00; bigint_adjust(&a); bigint_square(&c, &a); cli_putstr_P(PSTR("\r\n test: ")); @@ -576,18 +579,18 @@ void test_square_simple(void){ void test_reduce_simple(void){ bigint_t a, b, c; - uint8_t a_b[2] = {0x62, 0xA8}; - uint8_t b_b[2] = {0x52, 0x27}; - uint8_t c_b[2]; - a.wordv=a_b; - a.length_W = 2; - a.info=0x00; + uint8_t a_b[4] = {0x62, 0xA8}; + uint8_t b_b[4] = {0x52, 0x27}; + uint8_t c_b[4]; + a.wordv = (bigint_word_t*)a_b; + a.length_W = 4 / sizeof(bigint_word_t); + a.info = 0x00; bigint_adjust(&a); - b.wordv=b_b; - b.length_W = 2; - b.info=0x00; + b.wordv = (bigint_word_t*)b_b; + b.length_W = 4 / sizeof(bigint_word_t); + b.info = 0x00; bigint_adjust(&b); - c.wordv = c_b; + c.wordv = (bigint_word_t*)c_b; bigint_copy(&c, &a); bigint_reduce(&c, &b); cli_putstr_P(PSTR("\r\n test: ")); @@ -605,20 +608,20 @@ void test_reduce_simple(void){ void test_gcdext_simple(void){ bigint_t a, b, c, d, e; - uint8_t a_b[5] = {0x71, 0x07, 0x00, 0x09, 0x16}; - uint8_t b_b[5] = {0x72, 0x7D, 0x57, 0xAC, 0X6F}; - uint8_t c_b[6], d_b[6], e_b[6]; - a.wordv=a_b; - a.length_W = 5; - a.info=0x00; + uint8_t a_b[8] = {0x71, 0x07, 0x00, 0x09, 0x16}; + uint8_t b_b[8] = {0x72, 0x7D, 0x57, 0xAC, 0X6F}; + uint8_t c_b[8], d_b[8], e_b[8]; + a.wordv = (bigint_word_t*)a_b; + a.length_W = 8 / sizeof(bigint_word_t); + a.info = 0x00; bigint_adjust(&a); - b.wordv=b_b; - b.length_W = 5; - b.info=0x00; + b.wordv = (bigint_word_t*)b_b; + b.length_W = 8 / sizeof(bigint_word_t); + b.info = 0x00; bigint_adjust(&b); - c.wordv = c_b; - d.wordv = d_b; - e.wordv = e_b; + c.wordv = (bigint_word_t*)c_b; + d.wordv = (bigint_word_t*)d_b; + e.wordv = (bigint_word_t*)e_b; bigint_gcdext(&c, &d, &e, &a, &b); cli_putstr_P(PSTR("\r\n test: gcd( ")); bigint_print_hex(&a); -- 2.39.5