ARM Shanghai IoT Team (Internal) / newMiniTLS-GPL

Fork of MiniTLS-GPL by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fp_sqr_comba_generic.c Source File

fp_sqr_comba_generic.c

00001 /* TomsFastMath, a fast ISO C bignum library.
00002  * 
00003  * This project is meant to fill in where LibTomMath
00004  * falls short.  That is speed ;-)
00005  *
00006  * This project is public domain and free for all purposes.
00007  * 
00008  * Tom St Denis, tomstdenis@gmail.com
00009  */
00010 
00011 #define TFM_DEFINES
00012 #include "fp_sqr_comba.c"
00013 
00014 /* generic comba squarer */
00015 void fp_sqr_comba(fp_int *A, fp_int *B)
00016 {
00017   int       pa, ix, iz;
00018   fp_digit  c0, c1, c2;
00019   fp_int    tmp, *dst;
00020 #ifdef TFM_ISO
00021   fp_word   tt;
00022 #endif    
00023 
00024   /* get size of output and trim */
00025   pa = A->used + A->used;
00026   if (pa >= FP_SIZE) {
00027      pa = FP_SIZE-1;
00028   }
00029 
00030   /* number of output digits to produce */
00031   COMBA_START;
00032   CLEAR_CARRY;
00033 
00034   if (A == B) {
00035      fp_zero(&tmp);
00036      dst = &tmp;
00037   } else {
00038      fp_zero(B);
00039      dst = B;
00040   }
00041 
00042   for (ix = 0; ix < pa; ix++) { 
00043       int      tx, ty, iy;
00044       fp_digit *tmpy, *tmpx;
00045 
00046       /* get offsets into the two bignums */
00047       ty = MIN(A->used-1, ix);
00048       tx = ix - ty;
00049 
00050       /* setup temp aliases */
00051       tmpx = A->dp + tx;
00052       tmpy = A->dp + ty;
00053 
00054       /* this is the number of times the loop will iterrate,
00055          while (tx++ < a->used && ty-- >= 0) { ... }
00056        */
00057       iy = MIN(A->used-tx, ty+1);
00058 
00059       /* now for squaring tx can never equal ty 
00060        * we halve the distance since they approach 
00061        * at a rate of 2x and we have to round because 
00062        * odd cases need to be executed
00063        */
00064       iy = MIN(iy, (ty-tx+1)>>1);
00065 
00066       /* forward carries */
00067       CARRY_FORWARD;
00068 
00069       /* execute loop */
00070       for (iz = 0; iz < iy; iz++) {
00071           SQRADD2(*tmpx++, *tmpy--);
00072       }
00073 
00074       /* even columns have the square term in them */
00075       if ((ix&1) == 0) {
00076           SQRADD(A->dp[ix>>1], A->dp[ix>>1]);
00077       }
00078 
00079       /* store it */
00080       COMBA_STORE(dst->dp[ix]);
00081   }
00082 
00083   COMBA_FINI;
00084 
00085   /* setup dest */
00086   dst->used = pa;
00087   fp_clamp (dst);
00088   if (dst != B) {
00089      fp_copy(dst, B);
00090   }
00091 }
00092 
00093 /* $Source: /cvs/libtom/tomsfastmath/src/sqr/Attic/fp_sqr_comba_generic.c,v $ */
00094 /* $Revision: 1.3 $ */
00095 /* $Date: 2007/02/15 00:31:32 $ */