]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
initial part of bigint-library; some bugs left
authorbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Wed, 3 Mar 2010 19:20:03 +0000 (19:20 +0000)
committerbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Wed, 3 Mar 2010 19:20:03 +0000 (19:20 +0000)
bigint/bigint.c [new file with mode: 0644]
bigint/bigint.h [new file with mode: 0644]
bigint/bigint_add_u.S [new file with mode: 0644]
bigint/bigint_io.c [new file with mode: 0644]
bigint/bigint_io.h [new file with mode: 0644]
host/bigint_test.rb [new file with mode: 0644]
mkfiles/bigint.mk [new file with mode: 0644]
test_src/main-bigint-test.c [new file with mode: 0644]

diff --git a/bigint/bigint.c b/bigint/bigint.c
new file mode 100644 (file)
index 0000000..fb2f524
--- /dev/null
@@ -0,0 +1,573 @@
+/* bigint.c */
+/*
+    This file is part of the AVR-Crypto-Lib.
+    Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * \file               bigint.c
+ * \author             Daniel Otte
+ * \date               2010-02-22
+ * 
+ * \license        GPLv3 or later
+ * 
+ */
+
+#include "bigint.h"
+#include <string.h>
+
+#include "bigint_io.h"
+#include "cli.h"
+
+#ifndef MAX
+ #define MAX(a,b) (((a)>(b))?(a):(b))
+#endif
+
+#ifndef MIN
+ #define MIN(a,b) (((a)<(b))?(a):(b))
+#endif
+
+#define SET_FBS(a, v) do{(a)->info &=0xF8; (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)
+#define XCHG_PTR(a,b)    do{ a = (void*)(((uint16_t)(a)) ^ ((uint16_t)(b))); \
+                                b = (void*)(((uint16_t)(a)) ^ ((uint16_t)(b))); \
+                                a = (void*)(((uint16_t)(a)) ^ ((uint16_t)(b)));}while(0)
+
+#define GET_SIGN(a) ((a)->info&BIGINT_NEG_MASK)
+
+/******************************************************************************/
+void bigint_adjust(bigint_t* a){
+       while(a->length_B!=0 && a->wordv[a->length_B-1]==0){
+               a->length_B--;
+       }
+       if(a->length_B==0){
+               a->info=0;
+               return;
+       }
+       uint8_t t;
+       uint8_t i = 0x07;
+       t = a->wordv[a->length_B-1];
+       while((t&0x80)==0 && i){
+               t<<=1;
+               i--;
+       }
+       SET_FBS(a, i);
+}
+
+/******************************************************************************/
+
+void bigint_copy(bigint_t* dest, const bigint_t* src){
+       memcpy(dest->wordv, src->wordv, src->length_B);
+       dest->length_B = src->length_B;
+       dest->info = src->info;
+}
+
+/******************************************************************************/
+
+/* this should be implemented in assembly */
+/*
+void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
+       uint16_t t=0, i;
+       if(a->length_B < b->length_B){
+               XCHG_PTR(a,b);
+       }
+       for(i=0; i<b->length_B; ++i){
+               t = a->wordv[i] + b->wordv[i] + t;
+               dest->wordv[i] = (uint8_t)t;
+               t>>=8;
+       }
+       for(; i<a->length_B; ++i){
+               t = a->wordv[i] + t;
+               dest->wordv[i] = (uint8_t)t;
+               t>>=8;
+       }
+       dest->wordv[i++] = t;
+       dest->length_B = i;
+       bigint_adjust(dest);
+}
+*/
+/******************************************************************************/
+
+/* this should be implemented in assembly */
+void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){
+       uint16_t i,j=0;
+       uint16_t t=0;
+       if(scale>dest->length_B)
+               memset(dest->wordv+dest->length_B, 0, scale-dest->length_B);
+       for(i=scale; i<a->length_B+scale; ++i,++j){
+               t = a->wordv[j] + t;
+               if(dest->length_B>i){
+                       t += dest->wordv[i];
+               }
+               dest->wordv[i] = (uint8_t)t;
+               t>>=8;
+       }
+       while(t){
+               if(dest->length_B>i){
+                       t = dest->wordv[i] + t;
+               }
+               dest->wordv[i] = (uint8_t)t;
+               t>>=8;
+               ++i;
+       }
+       if(dest->length_B < i){
+               dest->length_B = i;
+       }
+       bigint_adjust(dest);
+}
+
+/******************************************************************************/
+
+/* this should be implemented in assembly */
+void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
+       int8_t borrow=0;
+       int8_t  r;
+       int16_t t;
+       uint16_t i, min, max;
+       min = MIN(a->length_B, b->length_B);
+       max = MAX(a->length_B, b->length_B);
+       r = bigint_cmp_u(a,b);
+       if(r==0){
+               dest->length_B = 0;
+               dest->wordv[0] = 0;
+               bigint_adjust(dest);
+               return;
+       }
+       if(b->length_B==0){
+               dest->length_B = a->length_B;
+               memcpy(dest->wordv, a->wordv, a->length_B);
+               dest->info = a->info;
+               SET_POS(dest);
+               return;
+       }
+       if(a->length_B==0){
+                       dest->length_B = b->length_B;
+                       memcpy(dest->wordv, b->wordv, b->length_B);
+                       dest->info = b->info;
+                       SET_NEG(dest);
+                       return;
+       }
+       if(r<0){
+               for(i=0; i<min; ++i){
+                       t = a->wordv[i] - b->wordv[i] - borrow;
+                       if(t<1){
+                               borrow = 0;
+                               dest->wordv[i]=(uint8_t)(-t);
+                       }else{
+                               borrow = -1;
+                               dest->wordv[i]=(uint8_t)(-t);
+                       }
+               }
+               for(;i<max; ++i){
+                       t = b->wordv[i] + borrow;
+                       if(t<1){
+                               borrow = 0;
+                               dest->wordv[i]=(uint8_t)t;
+                       }else{
+                               borrow = -1;
+                               dest->wordv[i]=(uint8_t)t;
+                       }
+               }
+               SET_NEG(dest);
+               dest->length_B = i;
+               bigint_adjust(dest);
+       }else{
+               for(i=0; i<min; ++i){
+                       t = a->wordv[i] - b->wordv[i] - borrow;
+                       if(t<0){
+                               borrow = 1;
+                               dest->wordv[i]=(uint8_t)t;
+                       }else{
+                               borrow = 0;
+                               dest->wordv[i]=(uint8_t)t;
+                       }
+               }
+               for(;i<max; ++i){
+                       t = a->wordv[i] - borrow;
+                       if(t<0){
+                               borrow = 1;
+                               dest->wordv[i]=(uint8_t)t;
+                       }else{
+                               borrow = 0;
+                               dest->wordv[i]=(uint8_t)t;
+                       }
+
+               }
+               SET_POS(dest);
+               dest->length_B = i;
+               bigint_adjust(dest);
+       }
+}
+
+/******************************************************************************/
+
+int8_t bigint_cmp_u(const bigint_t* a, const bigint_t* b){
+       if(a->length_B > b->length_B){
+               return 1;
+       }
+       if(a->length_B < b->length_B){
+               return -1;
+       }
+       if(GET_FBS(a) > GET_FBS(b)){
+               return 1;
+       }
+       if(GET_FBS(a) < GET_FBS(b)){
+               return -1;
+       }
+       if(a->length_B==0){
+               return 0;
+       }
+       uint16_t i;
+       i = a->length_B-1;
+       do{
+               if(a->wordv[i]!=b->wordv[i]){
+                       if(a->wordv[i]>b->wordv[i]){
+                               return 1;
+                       }else{
+                               return -1;
+                       }
+               }
+       }while(i--);
+       return 0;
+}
+
+/******************************************************************************/
+
+void bigint_add_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
+       uint8_t s;
+       s  = GET_SIGN(a)?2:0;
+       s |= GET_SIGN(b)?1:0;
+       switch(s){
+               case 0: /* both positive */
+                       bigint_add_u(dest, a,b);
+                       SET_POS(dest);
+                       break;
+               case 1: /* a positive, b negative */
+                       bigint_sub_u(dest, a, b);
+                       break;
+               case 2: /* a negative, b positive */
+                       bigint_sub_u(dest, b, a);
+                       break;
+               case 3: /* both negative */
+                       bigint_add_u(dest, a, b);
+                       SET_NEG(dest);
+                       break;
+               default: /* how can this happen?*/
+                       break;
+       }
+}
+
+/******************************************************************************/
+
+void bigint_sub_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
+       uint8_t s;
+       s  = GET_SIGN(a)?2:0;
+       s |= GET_SIGN(b)?1:0;
+       switch(s){
+               case 0: /* both positive */
+                       bigint_sub_u(dest, a,b);
+                       break;
+               case 1: /* a positive, b negative */
+                       bigint_add_u(dest, a, b);
+                       SET_POS(dest);
+                       break;
+               case 2: /* a negative, b positive */
+                       bigint_add_u(dest, a, b);
+                       SET_NEG(dest);
+                       break;
+               case 3: /* both negative */
+                       bigint_sub_u(dest, b, a);
+                       break;
+               default: /* how can this happen?*/
+                                       break;
+       }
+
+}
+
+/******************************************************************************/
+
+int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){
+       uint8_t s;
+       s  = GET_SIGN(a)?2:0;
+       s |= GET_SIGN(b)?1:0;
+       switch(s){
+               case 0: /* both positive */
+                       return bigint_cmp_u(a, b);
+                       break;
+               case 1: /* a positive, b negative */
+                       return 1;
+                       break;
+               case 2: /* a negative, b positive */
+                       return -1;
+                       break;
+               case 3: /* both negative */
+                       return bigint_cmp_u(b, a);
+                       break;
+               default: /* how can this happen?*/
+                                       break;
+       }
+       return 0; /* just to satisfy the compiler */
+}
+
+/******************************************************************************/
+
+void bigint_shiftleft(bigint_t* a, uint16_t shift){
+       uint16_t byteshift;
+       uint16_t i;
+       uint8_t bitshift;
+       uint16_t t=0;
+       byteshift = (shift+3)/8;
+       bitshift = shift&7;
+       memmove(a->wordv+byteshift, a->wordv, a->length_B);
+       memset(a->wordv, 0, byteshift);
+       if(bitshift!=0){
+               if(bitshift<=4){ /* shift to the left */
+                       for(i=byteshift; i<a->length_B+byteshift; ++i){
+                               t |= (a->wordv[i])<<bitshift;
+                               a->wordv[i] = (uint8_t)t;
+                               t >>= 8;
+                       }
+                       a->wordv[i] = (uint8_t)t;
+               }else{ /* shift to the right */
+                       bitshift = 8 - bitshift;
+                       for(i=a->length_B+byteshift-1; i>byteshift; --i){
+                               t |= (a->wordv[i])<<(8-bitshift);
+                               a->wordv[i] = (uint8_t)(t>>8);
+                               t <<= 8;
+                       }
+                       t |= (a->wordv[i])<<(8-bitshift);
+                       a->wordv[i] = (uint8_t)(t>>8);
+               }
+
+       }
+       a->length_B += byteshift+1;
+       bigint_adjust(a);
+}
+
+/******************************************************************************/
+
+void bigint_shiftright(bigint_t* a, uint16_t shift){
+       uint16_t byteshift;
+       uint16_t i;
+       uint8_t bitshift;
+       uint16_t t=0;
+       byteshift = shift/8;
+       bitshift = shift&7;
+       if(byteshift >= a->length_B){ /* we would shift out more than we have */
+               a->length_B=0;
+               a->wordv[0] = 0;
+               SET_FBS(a, 0);
+               return;
+       }
+       if(byteshift == a->length_B-1 && bitshift>GET_FBS(a)){
+               a->length_B=0;
+               a->wordv[0] = 0;
+               SET_FBS(a, 0);
+               return;
+       }
+       if(byteshift){
+               memmove(a->wordv, a->wordv+byteshift, a->length_B-byteshift);
+               memset(a->wordv+a->length_B-byteshift, 0,  byteshift);
+       }
+       if(bitshift!=0){
+        /* shift to the right */
+               for(i=a->length_B-byteshift-1; i>0; --i){
+                       t |= (a->wordv[i])<<(8-bitshift);
+                       a->wordv[i] = (uint8_t)(t>>8);
+                       t <<= 8;
+               }
+               t |= (a->wordv[0])<<(8-bitshift);
+               a->wordv[0] = (uint8_t)(t>>8);
+       }
+       a->length_B -= byteshift;
+       bigint_adjust(a);
+}
+
+/******************************************************************************/
+
+void bigint_xor(bigint_t* dest, const bigint_t* a){
+       uint16_t i;
+       for(i=0; i<a->length_B; ++i){
+               dest->wordv[i] ^= a->wordv[i];
+       }
+       bigint_adjust(dest);
+}
+
+/******************************************************************************/
+
+void bigint_set_zero(bigint_t* a){
+       a->length_B=0;
+}
+
+/******************************************************************************/
+
+/* using the Karatsuba-Algorithm */
+/* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */
+void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
+       if(a->length_B==0 || b->length_B==0){
+               bigint_set_zero(dest);
+               depth -= 1;
+               return;
+       }
+       if(a->length_B==1 || b->length_B==1){
+               if(a->length_B!=1){
+                       XCHG_PTR(a,b);
+               }
+               uint16_t i, t=0;
+               uint8_t x = a->wordv[0];
+               for(i=0; i<b->length_B; ++i){
+                       t += b->wordv[i]*x;
+                       dest->wordv[i] = (uint8_t)t;
+                       t>>=8;
+               }
+               dest->wordv[i] = (uint8_t)t;
+               dest->length_B=i+1;
+               bigint_adjust(dest);
+               return;
+       }
+       if(a->length_B<=4 && b->length_B<=4){
+               uint32_t p=0, q=0;
+               uint64_t r;
+               memcpy(&p, a->wordv, a->length_B);
+               memcpy(&q, b->wordv, b->length_B);
+               r = (uint64_t)p*(uint64_t)q;
+               memcpy(dest->wordv, &r, a->length_B+b->length_B);
+               dest->length_B =  a->length_B+b->length_B;
+               bigint_adjust(dest);
+               return;
+       }
+       bigint_set_zero(dest);
+       /* split a in xh & xl; split b in yh & yl */
+       uint16_t n;
+       n=(MAX(a->length_B, b->length_B)+1)/2;
+       bigint_t xl, xh, yl, yh;
+       xl.wordv = a->wordv;
+       yl.wordv = b->wordv;
+       if(a->length_B<=n){
+               xh.info=0;
+               xh.length_B = 0;
+               xl.length_B = a->length_B;
+               xl.info = 0;
+       }else{
+               xl.length_B=n;
+               xl.info = 0;
+               bigint_adjust(&xl);
+               xh.wordv = a->wordv+n;
+               xh.length_B = a->length_B-n;
+               xh.info = 0;
+       }
+       if(b->length_B<=n){
+               yh.info=0;
+               yh.length_B = 0;
+               yl.length_B = b->length_B;
+               yl.info = b->info;
+       }else{
+               yl.length_B=n;
+               yl.info = 0;
+               bigint_adjust(&yl);
+               yh.wordv = b->wordv+n;
+               yh.length_B = b->length_B-n;
+               yh.info = 0;
+       }
+       /* now we have split up a and b */
+       uint8_t  tmp_b[2*n+2], m_b[2*(n+1)];
+       bigint_t tmp, tmp2, m;
+       /* calculate h=xh*yh; l=xl*yl; sx=xh+xl; sy=yh+yl */
+       tmp.wordv = tmp_b;
+       tmp2.wordv = tmp_b+n+1;
+       m.wordv = m_b;
+
+       bigint_mul_u(dest, &xl, &yl);  /* dest <= xl*yl     */
+       bigint_add_u(&tmp2, &xh, &xl); /* tmp2 <= xh+xl     */
+       bigint_add_u(&tmp, &yh, &yl);  /* tmp  <= yh+yl     */
+       bigint_mul_u(&m, &tmp2, &tmp); /* m    <= tmp*tmp   */
+       bigint_mul_u(&tmp, &xh, &yh);  /* h    <= xh*yh     */
+       bigint_sub_u(&m, &m, dest);    /* m    <= m-dest    */
+    bigint_sub_u(&m, &m, &tmp);    /* m    <= m-h       */
+       bigint_add_scale_u(dest, &m, n);
+       bigint_add_scale_u(dest, &tmp, 2*n);
+}
+
+/******************************************************************************/
+
+void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
+       uint8_t s;
+       s  = GET_SIGN(a)?2:0;
+       s |= GET_SIGN(b)?1:0;
+       switch(s){
+               case 0: /* both positive */
+                       bigint_mul_u(dest, a,b);
+                       SET_POS(dest);
+                       break;
+               case 1: /* a positive, b negative */
+                       bigint_mul_u(dest, a,b);
+                       SET_NEG(dest);
+                       break;
+               case 2: /* a negative, b positive */
+                       bigint_mul_u(dest, a,b);
+                       SET_NEG(dest);
+                       break;
+               case 3: /* both negative */
+                       bigint_mul_u(dest, a,b);
+                       SET_POS(dest);
+                       break;
+               default: /* how can this happen?*/
+                       break;
+       }
+}
+
+/******************************************************************************/
+
+/* square */
+/* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */
+void bigint_square(bigint_t* dest, const bigint_t* a){
+       if(a->length_B<=4){
+               uint64_t r=0;
+               memcpy(&r, a->wordv, a->length_B);
+               r = r*r;
+               memcpy(dest->wordv, &r, 2*a->length_B);
+               SET_POS(dest);
+               dest->length_B=2*a->length_B;
+               bigint_adjust(dest);
+               return;
+       }
+       uint16_t n;
+       n=(a->length_B+1)/2;
+       bigint_t xh, xl, tmp; /* x-high, x-low, temp */
+       uint8_t buffer[2*n+1];
+       xl.wordv = a->wordv;
+       xl.length_B = n;
+       xh.wordv = a->wordv+n;
+       xh.length_B = a->length_B-n;
+       tmp.wordv = buffer;
+       bigint_square(dest, &xl);
+       bigint_square(&tmp, &xh);
+       bigint_add_scale_u(dest, &tmp, 2*n);
+       bigint_mul_u(&tmp, &xl, &xh);
+       bigint_shiftleft(&tmp, 1);
+       bigint_add_scale_u(dest, &tmp, n);
+
+}
+
+
+
+
+
+
+
diff --git a/bigint/bigint.h b/bigint/bigint.h
new file mode 100644 (file)
index 0000000..39bb173
--- /dev/null
@@ -0,0 +1,63 @@
+/* bigint.h */
+/*
+    This file is part of the AVR-Crypto-Lib.
+    Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+/**
+ * \file               bigint.h
+ * \author             Daniel Otte
+ * \date               2010-02-22
+ * 
+ * \license        GPLv3 or later
+ * 
+ */
+
+#ifndef BIGINT_H_
+#define BIGINT_H_
+
+#include <stdint.h>
+
+#define BIGINT_FBS_MASK 0x07 /* the last three bits indicate which is the first bit set */
+#define BIGINT_NEG_MASK 0x80 /* this bit indicates a negative value */
+typedef struct{
+       uint16_t length_B;
+       uint8_t info;
+       uint8_t *wordv; /* word vector, pointing to the LSB */
+}bigint_t;
+
+
+/******************************************************************************/
+
+void   bigint_adjust(bigint_t* a);
+void bigint_copy(bigint_t* dest, const bigint_t* src);
+void   bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b);
+void   bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale);
+void   bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b);
+int8_t bigint_cmp_u(const bigint_t * a, const bigint_t * b);
+void   bigint_add_s(bigint_t* dest, const bigint_t* a, const bigint_t* b);
+void   bigint_sub_s(bigint_t* dest, const bigint_t* a, const bigint_t* b);
+int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b);
+void   bigint_shiftleft(bigint_t* a, uint16_t shift);
+void   bigint_shiftright(bigint_t* a, uint16_t shift);
+void   bigint_xor(bigint_t* dest, const bigint_t* a);
+void   bigint_set_zero(bigint_t* a);
+void   bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b);
+void   bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b);
+void   bigint_square(bigint_t* dest, const bigint_t* a);
+
+/******************************************************************************/
+
+#endif /*BIGINT_H_*/
diff --git a/bigint/bigint_add_u.S b/bigint/bigint_add_u.S
new file mode 100644 (file)
index 0000000..7c34f1a
--- /dev/null
@@ -0,0 +1,137 @@
+/* bigint_add_u.S */
+/*
+    This file is part of the AVR-Crypto-Lib.
+    Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * \file     bigint_add_u.S
+ * \email    daniel.otte@rub.de
+ * \author   Daniel Otte
+ * \date     2010-03-01
+ * \license  GPLv3 or later
+ *
+ */
+
+#include "avr-asm-macros.S"
+
+/*
+ param dest: r24:r25
+ param a:    r22:r23
+ param b:    r20:r21
+*/
+LEN_A_0 = 22
+LEN_A_1 = 23
+LEN_B_0 = 20
+LEN_B_1 = 21
+
+
+.global bigint_add_u
+bigint_add_u:
+       push_range 28, 29
+       push_range 24, 25
+       movw r26, r24 ; X is our destination pointer
+       movw r30, r22 ; Z = a
+       movw r28, r20 ; Y = b
+       ldd LEN_A_0, Z+0
+       ldd LEN_A_1, Z+1
+       ldd LEN_B_0, Y+0
+       ldd LEN_B_1, Y+1
+       cp LEN_A_0, LEN_B_0
+       cpc LEN_A_1, LEN_B_1
+       brsh 3f
+       movw r18, LEN_A_0    ; swap length values
+       movw LEN_A_0, LEN_B_0
+       movw LEN_B_0, r18
+       movw r18, r30        ; swap pointers
+       movw r30, r28
+       movw r28, r18
+3:     ; now a is the longer integer
+    movw r24, LEN_A_0
+    adiw r24, 0
+    brne 4f
+    st X+, r1 ; store length
+    st X+, r1
+    st X+, r1 ; store 0 in info field
+    rjmp 9f
+4:
+    adiw r24, 1
+    st X+, r24 ; store length
+    st X+, r25
+    st X+, r1 ; store 0 in info field
+    ld r18, X+
+    ld r19, X+
+    movw r26, r18
+    adiw r30, 3 ; adjust pointers to point at wordv
+    ld r18, Z+
+    ld r19, Z+
+    movw r30, r18
+    adiw r28, 3
+    ld r18, Y+
+    ld r19, Y+
+    movw r28, r18
+
+    sub LEN_A_0, LEN_B_0
+    sbc LEN_A_1, LEN_B_1
+    movw r24, LEN_B_0
+    clr r0
+    adiw r24, 0
+    breq 6f
+    clc
+5:
+    ld r0, Z+
+    ld r1, Y+
+    adc r0, r1
+    st X+, r0
+    dec r24
+       brne 5b
+       rol r0 ; store carry bit
+       tst r25
+       breq 6f
+       dec r25
+       dec r24
+       ror r0 ; write carry back
+       rjmp 5b
+6: /* the main part is done */
+       movw r24, LEN_A_0
+       clr r1
+       adiw r24, 0
+       breq 8f
+62:
+       ror r0 ; write carry back
+7:
+    ld r0, Z+
+    adc r0, r1
+    st X+, r0
+    dec r24
+       brne 7b
+       rol r0 ; store carry bit
+       tst r25
+       breq 8f
+       dec r25
+       dec r24
+       rjmp 62b
+8:
+       ror r0
+       clr r0
+       rol r0
+       st X+, r0
+9:
+       pop_range 24, 25
+       pop_range 28, 29
+       jmp bigint_adjust
+
+
diff --git a/bigint/bigint_io.c b/bigint/bigint_io.c
new file mode 100644 (file)
index 0000000..1a2b12c
--- /dev/null
@@ -0,0 +1,128 @@
+/* bigint_io.c */
+/*
+    This file is part of the AVR-Crypto-Lib.
+    Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "cli.h"
+#include "bigint.h"
+#include <stdlib.h>
+#include <string.h>
+
+void bigint_print_hex(const bigint_t* a){
+       if(a->info&BIGINT_NEG_MASK){
+               cli_putc('-');
+       }
+//     cli_putc((a->info&BIGINT_NEG_MASK)?'-':'+'); /* print sign */
+       if(a->length_B!=0){
+               cli_hexdump_rev(a->wordv, a->length_B);
+       }else{
+               cli_putc('0');
+       }
+}
+
+#define BLOCKSIZE 20
+
+static uint8_t char2nibble(char c){
+       if(c>='0' && c <='9'){
+               return c-'0';
+       }
+       c |= 'A'^'a'; /* to lower case */
+       if(c>='a' && c <='f'){
+               return c-'a'+10;
+       }
+       return 0xff;
+}
+
+static uint16_t read_byte(void){
+       uint8_t t1, t2;
+       char c;
+       c = cli_getc_cecho();
+       if(c=='-'){
+               return 0x0500;
+       }
+       t1 = char2nibble(c);
+       if(t1 == 0xff){
+               return 0x0100;
+       }
+       c = cli_getc_cecho();
+       t2 = char2nibble(c);
+       if(t2 == 0xff){
+               return 0x0200|t1;
+       }
+       return (t1<<4)|t2;
+}
+
+uint8_t bigint_read_hex_echo(bigint_t* a){
+       uint16_t allocated=0;
+       uint8_t  shift4=0;
+       uint16_t  t;
+       a->length_B = 0;
+       a->wordv = NULL;
+       a->info = 0;
+       for(;;){
+               if(allocated-a->length_B < 1){
+                       uint8_t *p;
+                       p = realloc(a->wordv, allocated+=BLOCKSIZE);
+                       if(p==NULL){
+                               cli_putstr_P(PSTR("\r\nERROR: Out of memory!"));
+                               free(a->wordv);
+                               return 0xff;
+                       }
+                       a->wordv=p;
+               }
+               t = read_byte();
+               if(a->length_B==0){
+                       if(t&0x0400){
+                               /* got minus */
+                               a->info |= BIGINT_NEG_MASK;
+                               continue;
+                       }else{
+                               if(t==0x0100){
+                                       free(a->wordv);
+                                       a->wordv=NULL;
+                                       return 1;
+                               }
+                       }
+               }
+               if(t<=0x00ff){
+                       a->wordv[a->length_B++] = (uint8_t)t;
+               }else{
+                       if(t&0x0200){
+                               shift4 = 1;
+                               a->wordv[a->length_B++] = (uint8_t)((t&0x0f)<<4);
+                       }
+                       break;
+               }
+       }
+       /* we have to reverse the byte array */
+       uint8_t tmp;
+       uint8_t *p, *q;
+       p = a->wordv;
+       q = a->wordv+a->length_B-1;
+       while(q>p){
+               tmp = *p;
+               *p = *q;
+               *q = tmp;
+               p++; q--;
+       }
+       if(shift4){
+               bigint_adjust(a);
+               bigint_shiftright(a, 4);
+       }
+       bigint_adjust(a);
+       return 0;
+}
diff --git a/bigint/bigint_io.h b/bigint/bigint_io.h
new file mode 100644 (file)
index 0000000..e47391d
--- /dev/null
@@ -0,0 +1,28 @@
+/* bigint_io.h */
+/*
+    This file is part of the AVR-Crypto-Lib.
+    Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef BIGINT_IO_H_
+#define BIGINT_IO_H_
+
+#include "bigint.h"
+
+void    bigint_print_hex(const bigint_t* a);
+uint8_t bigint_read_hex_echo(bigint_t* a);
+
+#endif /* BIGINT_IO_H_ */
diff --git a/host/bigint_test.rb b/host/bigint_test.rb
new file mode 100644 (file)
index 0000000..9048f89
--- /dev/null
@@ -0,0 +1,428 @@
+#!/usr/bin/ruby
+# bigint_test.rb
+=begin
+    This file is part of the AVR-Crypto-Lib.
+    Copyright (C) 2008, 2009  Daniel Otte (daniel.otte@rub.de)
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+=end
+
+$debug = true
+$debug = false
+require 'rubygems'
+require 'serialport'
+require 'getopt/std'
+require 'ftools'
+require 'date'
+$buffer_size = 0
+$conffile_check = Hash.new
+$conffile_check.default = 0
+
+################################################################################
+# readconfigfile                                                               #
+################################################################################
+
+def readconfigfile(fname, conf)
+  return conf if $conffile_check[fname]==1
+  $conffile_check[fname]=1
+  section = "default"
+  if not File.exists?(fname)
+    return conf
+  end
+  file = File.open(fname, "r")
+  until file.eof
+    line = file.gets()
+       next if /[\s]*#/.match(line)
+       if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
+         section=m[1]
+         conf[m[1]] = Hash.new
+         next
+       end
+       next if not /=/.match(line)
+       m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
+       if m[1]=="include"
+         Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
+       else
+         conf[section][m[1]] = m[2]
+       end
+  end
+  file.close()
+  return conf
+end
+
+################################################################################
+# reset_system                                                                 #
+################################################################################
+
+def reset_system
+  $sp.print("exit\r")
+  sleep 0.1
+  $sp.print("exit\r")
+  sleep 0.1
+end
+
+################################################################################
+# init_system                                                                  #
+################################################################################
+
+def init_system(test_prog)
+  $sp.print("echo off \r")
+  print("DBG i: " + "echo off \r"+"\n") if $debug
+  sleep 0.1
+  $sp.print("#{test_prog}\r")
+  print("DBG i: " + "#{test_prog} \r"+"\n") if $debug
+  sleep 1
+end
+
+
+################################################################################
+# screen_progress                                                              #
+################################################################################
+def screen_progress(v)
+  if $linepos==0
+    printf("\n%5d [%04d]: ", $testno, $size)
+  end
+  putc((v)?('*'):('!'))
+  $testno += 1
+  $linepos = ($linepos+1)%$linewidth
+end
+
+################################################################################
+# add_test                                                                     #
+################################################################################
+
+def add_test(a,b)
+  begin
+    line = $sp.gets()
+    line = "" if line==nil
+    puts("DBG got: "+line) if $debug
+    if /^Error:.*/.match(line)
+      puts line
+      return false
+    end
+  end while not /[\s]*enter a:[\s]*/.match(line)
+  $sp.print(a.to_s(16)+" ")
+  begin
+    line = $sp.gets()
+    line = "" if line==nil
+    puts("DBG got: "+line) if $debug
+    if /^Error:.*/.match(line)
+      puts line
+      return false
+    end
+  end while not /[\s]*enter b:[\s]*/.match(line)
+  $sp.print(b.to_s(16)+" ")
+  begin
+    line = $sp.gets()
+    line = "" if line==nil
+    puts("DBG got: "+line) if $debug
+    if /^Error:.*/.match(line)
+      puts line
+      return false
+    end
+  end while not m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
+  a_ = m[1].to_i(16)
+  b_ = m[2].to_i(16)
+  c_ = m[3].to_i(16)
+  line.chomp!
+  if(a_== a && b_ == b && c_ == (a+b))
+    $logfile.printf("[pass]: %s\n", line)
+    return true
+  else
+    $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
+    $logfile.printf(" ; should %s + %s = %s\n", a.to_s(16), b.to_s(16), (a+b).to_s(16))
+    return false
+  end
+  return false
+end
+
+################################################################################
+# mul_test                                                                     #
+################################################################################
+
+def mul_test(a,b)
+  begin
+    line = $sp.gets()
+    line = "" if line==nil
+    puts("DBG got: "+line) if $debug
+    if /^Error:.*/.match(line)
+      puts line
+      return false
+    end
+  end while not /[\s]*enter a:[\s]*/.match(line)
+  $sp.print(a.to_s(16)+" ")
+  begin
+    line = $sp.gets()
+    line = "" if line==nil
+    puts("DBG got: "+line) if $debug
+    if /^Error:.*/.match(line)
+      puts line
+      return false
+    end
+  end while not /[\s]*enter b:[\s]*/.match(line)
+  $sp.print(b.to_s(16)+" ")
+  begin
+    line = $sp.gets()
+    line = "" if line==nil
+    puts("DBG got: "+line) if $debug
+    if /^Error:.*/.match(line)
+      puts line
+      return false
+    end
+  end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+\*[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
+  a_ = m[1].to_i(16)
+  b_ = m[2].to_i(16)
+  c_ = m[3].to_i(16)
+  line.chomp!
+  if(a_== a && b_ == b && c_ == (a*b))
+    $logfile.printf("[pass]: %s\n", line)
+    return true
+  else
+    $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
+    $logfile.printf(" ; should %s * %s = %s\n", a.to_s(16), b.to_s(16), (a*b).to_s(16))
+    return false
+  end
+  return false
+end
+
+################################################################################
+# square_test                                                                  #
+################################################################################
+
+def square_test(a)
+  begin
+    line = $sp.gets()
+    line = "" if line==nil
+    puts("DBG got: "+line) if $debug
+    if /^Error:.*/.match(line)
+      puts line
+      return false
+    end
+  end while not /[\s]*enter a:[\s]*/.match(line)
+  $sp.print(a.to_s(16)+" ")
+  begin
+    line = $sp.gets()
+    line = "" if line==nil
+    puts("DBG got: "+line) if $debug
+    if /^Error:.*/.match(line)
+      puts line
+      return false
+    end
+  end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
+  a_ = m[1].to_i(16)
+  c_ = m[2].to_i(16)
+  line.chomp!
+  if(a_== a && c_ == (a**2))
+    $logfile.printf("[pass]: %s\n", line)
+    return true
+  else
+    $logfile.printf("[fail (%s%s)]: %s", (a==a_)?"":"a", (c_==a**2)?"":"c",line)
+    $logfile.printf(" ; should %s **2 = %s\n", a.to_s(16), (a**2).to_s(16))
+    return false
+  end
+  return false
+end
+
+################################################################################
+# run_test_add                                                                 #
+################################################################################
+
+def run_test_add(skip=0)
+  length_a_B = skip+1
+  length_b_B = skip+1
+  begin
+    $size = length_a_B
+    (0..16).each do |i|
+      s_a = rand(2)*2-1
+      s_b = rand(2)*2-1
+      a = rand(256**length_a_B) * s_a
+      b = rand(256**length_a_B) * s_b
+      v = add_test(a, b)
+      screen_progress(v)
+      v = add_test(b, a)
+      screen_progress(v)
+    end
+    (0..16).each do |i|
+      s_a = rand(2)-1
+      s_b = rand(2)-1
+      b_size = rand(length_b_B+1)
+      a = rand(256**length_a_B) * s_a
+      b = rand(256**b_size) * s_b
+      v = add_test(a, b)
+      screen_progress(v)      
+      v = add_test(b, a)
+      screen_progress(v)
+
+    end
+    length_a_B += 1
+    length_b_B += 1
+  end while length_a_B<4096/8
+end
+
+################################################################################
+# run_test_mul                                                                 #
+################################################################################
+
+def run_test_mul(skip=0)
+  length_a_B = skip+1
+  length_b_B = skip+1
+  begin
+    $size = length_a_B
+    (0..16).each do |i|
+      s_a = rand(2)*2-1
+      s_b = rand(2)*2-1
+      a = rand(256**length_a_B) * s_a
+      b = rand(256**length_a_B) * s_b
+      v = mul_test(a, b)
+      screen_progress(v)
+      v = mul_test(b, a)
+      screen_progress(v)
+    end
+    (0..16).each do |i|
+      s_a = rand(2)-1
+      s_b = rand(2)-1
+      b_size = rand(length_b_B+1)
+      a = rand(256**length_a_B) * s_a
+      b = rand(256**b_size) * s_b
+      v = mul_test(a, b)
+      screen_progress(v)      
+      v = mul_test(b, a)
+      screen_progress(v)
+    end
+    length_a_B += 1
+    length_b_B += 1
+  end while length_a_B<4096/8
+end
+
+################################################################################
+# run_test_square                                                              #
+################################################################################
+
+def run_test_square(skip=0)
+  length_a_B = skip+1
+  begin
+    $size = length_a_B
+    (0..16).each do |i|
+      a = rand(256**length_a_B)
+      v = square_test(a)
+      screen_progress(v)
+    end
+    length_a_B += 1
+  end while length_a_B<4096/8
+end
+
+################################################################################
+# MAIN                                                                         #
+################################################################################
+
+opts = Getopt::Std.getopts("s:f:i:a:hd")
+
+conf = Hash.new
+conf = readconfigfile("/etc/testport.conf", conf)
+conf = readconfigfile("~/.testport.conf", conf)
+conf = readconfigfile("testport.conf", conf)
+conf = readconfigfile(opts["f"], conf) if opts["f"]
+
+#puts conf.inspect
+
+puts("serial port interface version: " + SerialPort::VERSION);
+$linewidth = 64
+$linepos = 0
+$testno = 0
+params = { "baud"       => conf["PORT"]["baud"].to_i,
+            "data_bits" => conf["PORT"]["databits"].to_i,
+            "stop_bits" => conf["PORT"]["stopbits"].to_i,
+            "parity"    => SerialPort::NONE }
+params["paraty"] = SerialPort::ODD   if conf["PORT"]["paraty"].downcase == "odd"
+params["paraty"] = SerialPort::EVEN  if conf["PORT"]["paraty"].downcase == "even"
+params["paraty"] = SerialPort::MARK  if conf["PORT"]["paraty"].downcase == "mark"
+params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
+
+puts("\nPort: "+conf["PORT"]["port"]+"@"    +
+                params["baud"].to_s      +
+                " "                      +
+                params["data_bits"].to_s +
+                conf["PORT"]["paraty"][0,1].upcase +
+                params["stop_bits"].to_s +
+                "\n")
+
+$sp = SerialPort.new(conf["PORT"]["port"], params)
+
+$sp.read_timeout=1000; # 5 minutes
+$sp.flow_control = SerialPort::SOFT
+
+reset_system()
+
+if opts['d']
+  $debug = true
+end
+
+logfilename = conf['PORT']['testlogbase']+'bigint.txt'
+if File.exists?(logfilename)
+  i=1
+  begin
+    logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
+    i+=1
+  end while(File.exists?(logfilename))
+  while(i>2) do
+    File.move(sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt'), 
+              sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt'), true)
+    i-=1
+  end
+    File.move(sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt'), 
+              sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt'), true)
+  logfilename = conf['PORT']['testlogbase']+'bigint.txt' 
+end
+$logfile = File.open(logfilename, 'w')
+printf("logfile: %s\n", logfilename)
+
+$logfile.printf("bigint test from: %s\n", Time.now.to_s)
+$logfile.printf("skip = %s\n", opts['s']) if opts['s']
+$logfile.printf("seed = 0x%X\n", 0xdeadbeef)
+
+tests = Hash.new
+tests['a'] = proc {|x| run_test_add(x) }
+tests['m'] = proc {|x| run_test_mul(x) }
+tests['s'] = proc {|x| run_test_square(x) }
+init_str = Hash.new
+init_str['a'] = 'add-test'
+init_str['m'] = 'mul-test'
+init_str['s'] = 'square-test'
+
+srand(0xdeadbeef)
+
+if opts['a']
+  opts['a'].each_char do |x|
+    if tests[x]
+      puts init_str[x]
+      init_system(init_str[x])
+      tests[x].call(opts['s']?opts['s'].to_i():0) 
+    else
+      puts "no test defiened for '#{x}'"
+    end  
+  end
+else
+  'ams'.each_char do |x|
+    if tests[x]
+      puts init_str[x]
+      init_system(init_str[x])
+      tests[x].call(opts['s']?opts['s'].to_i():0) 
+    else
+      puts "no test defiened for '#{x}'"
+    end  
+  end
+end
+
+
+$logile.close()
diff --git a/mkfiles/bigint.mk b/mkfiles/bigint.mk
new file mode 100644 (file)
index 0000000..de7100e
--- /dev/null
@@ -0,0 +1,13 @@
+# Makefile for BigInt
+ALGO_NAME := BIGINT
+
+# comment out the following line for removement of base64 from the build process
+ENCODINGS += $(ALGO_NAME)
+
+$(ALGO_NAME)_DIR      := bigint/
+$(ALGO_NAME)_OBJ      := bigint.o bigint_io.o bigint_add_u.o 
+$(ALGO_NAME)_TEST_BIN := main-bigint-test.o $(CLI_STD)  \
+                         performance_test.o noekeon_asm.o noekeon_prng.o memxor.o
+                        
+$(ALGO_NAME)_PERFORMANCE_TEST := performance
+
diff --git a/test_src/main-bigint-test.c b/test_src/main-bigint-test.c
new file mode 100644 (file)
index 0000000..3d5bd21
--- /dev/null
@@ -0,0 +1,284 @@
+/* main-base64-test.c */
+/*
+    This file is part of the AVR-Crypto-Lib.
+    Copyright (C) 2008, 2009  Daniel Otte (daniel.otte@rub.de)
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+/*
+ * base64 test-suit
+ * 
+*/
+
+#include "config.h"
+
+#include "uart_i.h"
+#include "debug.h"
+
+#include "noekeon.h"
+#include "noekeon_prng.h"
+#include "bigint.h"
+#include "bigint_io.h"
+
+#include "cli.h"
+#include "performance_test.h"
+
+#include <stdint.h>
+#include <string.h>
+#include <stdlib.h>
+
+char* algo_name = "BigInt";
+
+/*****************************************************************************
+ *  additional validation-functions                                                                                     *
+ *****************************************************************************/
+void test_echo_bigint(void){
+       bigint_t a;
+       cli_putstr_P(PSTR("\r\necho test\r\n"));
+       for(;;){
+               cli_putstr_P(PSTR("\r\nenter hex number:"));
+               if(bigint_read_hex_echo(&a)){
+                       cli_putstr_P(PSTR("\r\n end echo test"));
+                       return;
+               }
+               cli_putstr_P(PSTR("\r\necho: "));
+               bigint_print_hex(&a);
+               cli_putstr_P(PSTR("\r\n"));
+               free(a.wordv);
+       }
+}
+
+void test_add_bigint(void){
+       bigint_t a, b, c;
+       cli_putstr_P(PSTR("\r\nadd test\r\n"));
+       for(;;){
+               cli_putstr_P(PSTR("\r\nenter 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)){
+                       free(a.wordv);
+                       cli_putstr_P(PSTR("\r\n end add test"));
+                       return;
+               }
+               cli_putstr_P(PSTR("\r\n "));
+               bigint_print_hex(&a);
+               cli_putstr_P(PSTR(" + "));
+               bigint_print_hex(&b);
+               cli_putstr_P(PSTR(" = "));
+               uint8_t *c_b;
+               c_b = malloc(((a.length_B>b.length_B)?a.length_B:b.length_B)+2);
+               if(c_b==NULL){
+                       cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
+                       free(a.wordv);
+                       free(b.wordv);
+                       continue;
+               }
+               c.wordv = c_b;
+               bigint_add_s(&c, &a, &b);
+               bigint_print_hex(&c);
+               cli_putstr_P(PSTR("\r\n"));
+               free(a.wordv);
+               free(b.wordv);
+               free(c_b);
+       }
+}
+
+void test_mul_bigint(void){
+       bigint_t a, b, c;
+       cli_putstr_P(PSTR("\r\nmul test\r\n"));
+       for(;;){
+               cli_putstr_P(PSTR("\r\nenter 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)){
+                       free(a.wordv);
+                       cli_putstr_P(PSTR("\r\n end mul test"));
+                       return;
+               }
+               cli_putstr_P(PSTR("\r\n "));
+               bigint_print_hex(&a);
+               cli_putstr_P(PSTR(" * "));
+               bigint_print_hex(&b);
+               cli_putstr_P(PSTR(" = "));
+               uint8_t *c_b;
+               c_b = malloc((((a.length_B>b.length_B)?a.length_B:b.length_B)+1)*2);
+               if(c_b==NULL){
+                       cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
+                       free(a.wordv);
+                       free(b.wordv);
+                       continue;
+               }
+               c.wordv = c_b;
+               bigint_mul_s(&c, &a, &b);
+               bigint_print_hex(&c);
+               cli_putstr_P(PSTR("\r\n"));
+               free(a.wordv);
+               free(b.wordv);
+               free(c_b);
+       }
+}
+
+void test_square_bigint(void){
+       bigint_t a, c;
+       cli_putstr_P(PSTR("\r\nsquare test\r\n"));
+       for(;;){
+               cli_putstr_P(PSTR("\r\nenter a:"));
+               if(bigint_read_hex_echo(&a)){
+                       cli_putstr_P(PSTR("\r\n end square test"));
+                       return;
+               }
+               cli_putstr_P(PSTR("\r\n "));
+               bigint_print_hex(&a);
+               cli_putstr_P(PSTR("**2 = "));
+               uint8_t *c_b;
+               c_b = malloc(a.length_B*2);
+               if(c_b==NULL){
+                       cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
+                       free(a.wordv);
+                       continue;
+               }
+               c.wordv = c_b;
+               bigint_square(&c, &a);
+               bigint_print_hex(&c);
+               cli_putstr_P(PSTR("\r\n"));
+               free(a.wordv);
+               free(c_b);
+       }
+}
+
+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;
+       a.length_B = 1;
+       b.length_B = 1;
+       a_b[0] = 1;
+       b_b[0] = 2;
+       bigint_add_u(&c, &a, &b);
+       cli_putstr_P(PSTR("\r\n 1+2="));
+       bigint_print_hex(&c);
+}
+/*
+void test_mul_simple(void){
+       bigint_t a, b, c;
+       uint8_t a_b[5] = {0x79, 0x36, 0x9e, 0x72, 0xec};
+       uint8_t b_b[5] = {0x4a, 0x47, 0x0d, 0xec, 0xfd};
+       uint8_t c_b[12];
+       a.wordv=a_b;
+       b.wordv=b_b;
+       c.wordv=c_b;
+       a.length_B = 5;
+       b.length_B = 5;
+       bigint_adjust(&a);
+       bigint_adjust(&b);
+       bigint_mul_s(&c, &a, &b);
+       cli_putstr_P(PSTR("\r\n test: "));
+       bigint_print_hex(&c);
+}
+*/
+
+// -3d1d 6db7 8251 f371 * -7a18 3791 d18b b7c5 = 1d25ce4fdf93390f8d6c709f4d711cf5
+// -20538248dece6d29068d * 400b1411b874f81394c6 = -81646b193d95136a6fedb73cee6d30c39fb950e
+// -BC8B 7D53 4921 853D * 0DDA 6044 00CE DDE6   =  -a33eb0c5847db8837589c22db395dce
+void test_mul_simple(void){
+       bigint_t a, b, c;
+
+//     uint8_t a_b[10] = {0x8d, 0x06, 0x29, 0x6d, 0xce, 0xde, 0x48, 0x82, 0x53, 0x20};
+//     uint8_t b_b[10] = {0xc6, 0x94, 0x13, 0xf8, 0x74, 0xb8, 0x11, 0x14, 0x0b, 0x40};
+       uint8_t a_b[8] = {0x3d, 0x85, 0x21, 0x49, 0x53, 0x7d, 0x8b, 0xbc};
+       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_B = 8;
+       b.length_B = 8;
+       a.info=0x80;
+       bigint_adjust(&a);
+       bigint_adjust(&b);
+       bigint_mul_s(&c, &a, &b);
+       cli_putstr_P(PSTR("\r\n test: "));
+       bigint_print_hex(&a);
+       cli_putstr_P(PSTR(" * "));
+       bigint_print_hex(&b);
+       cli_putstr_P(PSTR(" = "));
+       bigint_print_hex(&c);
+}
+
+// f4 b86a 2220 0774 437d 70e6 **2 = e9f00f29ca1c876a7a682bd1e04f6925caffd6660ea4
+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_B = 11;
+       a.info=0x00;
+       bigint_adjust(&a);
+       bigint_square(&c, &a);
+       cli_putstr_P(PSTR("\r\n test: "));
+       bigint_print_hex(&a);
+       cli_putstr_P(PSTR("**2 = "));
+       bigint_print_hex(&c);
+}
+
+
+void testrun_performance_bigint(void){
+
+}
+/*****************************************************************************
+ *  main                                                                                                                                        *
+ *****************************************************************************/
+
+const char echo_test_str[]        PROGMEM = "echo-test";
+const char add_test_str[]         PROGMEM = "add-test";
+const char mul_test_str[]         PROGMEM = "mul-test";
+const char square_test_str[]      PROGMEM = "square-test";
+const char quick_test_str[]       PROGMEM = "quick-test";
+const char performance_str[]      PROGMEM = "performance";
+const char echo_str[]             PROGMEM = "echo";
+
+cmdlist_entry_t cmdlist[] PROGMEM = {
+       { add_test_str,         NULL, test_add_bigint               },
+       { mul_test_str,         NULL, test_mul_bigint               },
+       { square_test_str,      NULL, test_square_bigint            },
+       { quick_test_str,       NULL, test_mul_simple               },
+       { echo_test_str,        NULL, test_echo_bigint              },
+       { performance_str,      NULL, testrun_performance_bigint    },
+       { echo_str,         (void*)1, (void_fpt)echo_ctrl           },
+       { NULL,                 NULL, NULL                          }
+};
+
+int main (void){
+       DEBUG_INIT();
+       
+       cli_rx = (cli_rx_fpt)uart0_getc;
+       cli_tx = (cli_tx_fpt)uart0_putc;
+       for(;;){
+               cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
+               cli_putstr(algo_name);
+               cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
+               cmd_interface(cmdlist);
+       }
+}