/*对mp_int进行初始化操作*/18/* init a new mp_int *//*输入参数是一个指向mp_int类型整数a的指针*/19intmp_init(mp_int * a)20{21int i;2223/* allocate memory required and clear it *//*分配内存空间*/24 a->dp =OPT_CAST(mp_digit)XMALLOC(sizeof(mp_digit)* MP_PREC);25if(a->dp ==NULL){26return MP_MEM;27}2829/* set the digits to zero *//*赋初值为0*/30for(i =0; i < MP_PREC; i++){31 a->dp[i]=0;32}3334/* set the used to zero, allocated digits to the default precision*/35/* and sign to positive *//*对used、alloc和sign赋初值*/36 a->used =0;37 a->alloc = MP_PREC;38 a->sign = MP_ZPOS;3940return MP_OKAY;41}
/*对mp_int进行清除操作*/18/* clear one (frees) */19void/*输入参数是一个指向mp_int类型整数的指针*/20mp_clear(mp_int * a)21{22int i;2324/* only do anything if a hasn't been freed previously *//*判断a->dp是否为NULL,不为NULL则说明被使用过,要进行clear操作*/25if(a->dp !=NULL){26/* first zero the digits *//*将所有位赋值为0,不用memset是为了移植性*/27for(i =0; i < a->used; i++){28 a->dp[i]=0;29}3031/* free ram *//*使用free()释放空间*/32XFREE(a->dp);3334/* reset members to make debugging easier *//*为其他成员设置最终值*/35 a->dp =NULL;36 a->alloc = a->used =0;37 a->sign = MP_ZPOS;38}39}
/*对mp_int整数重新分配大小*/18/* grow as required */19intmp_grow(mp_int * a,int size)20{21int i;22 mp_digit *tmp;23/*如果a->alloc>大于size,则进行grow操作,否则直接返回MP_OKAY即可*/24/* if the alloc size is smaller alloc more ram */25if(a->alloc < size){/*此处执行的操作是伪代码中的步骤2和3*//*本质就是让size向上取整,取到MP_PREC的倍数,然后再加一个MP_PREC*/26/* ensure there are always at least MP_PREC digits extra on top */27 size +=(MP_PREC *2)-(size % MP_PREC);28/*分配空间*/29/* reallocate the array a->dp*/30/**/31/* We store the return in a temporary variable*/32/* in case the operation failed we don't want*/33/* to overwrite the dp member of a.*/34/**/35 tmp =OPT_CAST(mp_digit)XREALLOC(a->dp,sizeof(mp_digit)* size);36if(tmp ==NULL){37/* reallocation failed but "a" is still valid [can be freed] */38return MP_MEM;39}40/*如果tmp不为空,则说明空间分配成功,然后将tmp赋值给a->dp*/41/* reallocation succeeded so set a->dp */42 a->dp = tmp;43/*重新设定a->alloc的大小,并对a->alloc和v-1之间的位进行赋值0*/44/* zero excess digits */45 i = a->alloc;46 a->alloc = size;47for(; i < a->alloc; i++){48 a->dp[i]=0;49}50}51return MP_OKAY;52}
/*根据指定大小对一个mp_int整数进行初始化*/18/* init an mp_init for a given size *//*输入参数是一个mp_int整数指针和一个整型值*/19intmp_init_size(mp_int * a,int size)20{21int x;22/*这里的运算和mp_grow一样,不再赘述*/23/* pad size so there are always extra digits */24 size +=(MP_PREC *2)-(size % MP_PREC);25/*分配空间*/26/* alloc mem */27 a->dp =OPT_CAST(mp_digit)XMALLOC(sizeof(mp_digit)* size);28if(a->dp ==NULL){29return MP_MEM;30}31/*为结构成员设定值*/32/* set the members */33 a->used =0;34 a->alloc = size;35 a->sign = MP_ZPOS;36/*对mp_int中的位进行赋值0操作,即对数据进行初始化*/37/* zero the digits */38for(x =0; x < size; x++){39 a->dp[x]=0;40}4142return MP_OKAY;43}
/*初始化多个mp_int变量*/17 #include <stdarg.h>18/*输入参数是可变长度的mp_int变量数组V_k,数组大小为k。最后要加一个指针NULL,这个NULL是作为列表的结束符使用的*/19intmp_init_multi(mp_int *mp,...)20{21 mp_err res = MP_OKAY;/* Assume ok until proven otherwise */22int n =0;/* Number of ok inits */23 mp_int* cur_arg = mp;24 va_list args;2526va_start(args, mp);/* init args to next argument from caller */27while(cur_arg !=NULL){/*如果初始化函数返回的值不是MP_OKAY*/28if(mp_init(cur_arg)!= MP_OKAY){29/* Oops - error! Back-track and mp_clear what we already*/30/* succeeded in init-ing, then return error.*/31/**/32 va_list clean_args;3334/* end the current list */35va_end(args);3637/* now start cleaning up */38 cur_arg = mp;39va_start(clean_args, mp);/*对之前已经初始化的变量进行清除操作*/40while(n--){41mp_clear(cur_arg);42 cur_arg =va_arg(clean_args, mp_int*);43}44va_end(clean_args);45 res = MP_MEM;46break;47}48 n++;49 cur_arg =va_arg(args, mp_int*);50}51va_end(args);52return res;/* Assumed ok, if error flagged above. */53}
/*对mp_int整数进行压缩多余位操作*/18/* trim unused digits
19 /**/20/* This is used to ensure that leading zero digits are*/21/* trimed and the leading "used" digit will be non-zero*/22/* Typically very fast. Also fixes the sign if there*/23/* are no more leading digits*/24/**/25void26mp_clamp(mp_int * a)27{28/* decrease used while the most significant digit is*/29/* zero.*/30/**//*如果user不为0且第used-1的位未被使用,则让used减1*/31while(a->used >0&& a->dp[a->used -1]==0){32--(a->used);33}34/*如果used等于0的话,就设定sign的值*/35/* reset the sign flag if used == 0 */36if(a->used ==0){37 a->sign = MP_ZPOS;38}39}