/*拷贝mp_int结构a到mp_int结构b*/18/* copy, b = a */19int20mp_copy(mp_int * a, mp_int * b)21{22int res, n;23/*mp_int结构a和b是同一个,则立刻返回*/24/* if dst == src do nothing */25if(a == b){26return MP_OKAY;27}28/*如果b的alloc小于a的used(即b分配的内存太小),则对b进行grow操作*/29/* grow dest */30if(b->alloc < a->used){31if((res =mp_grow(b, a->used))!= MP_OKAY){32return res;33}34}3536/* zero b and copy the parameters over */37{/*Register变量是C语言中允许将局部变量的值放在CPU中的寄存器中需要时直接从寄存器中取出来参加运算,不必再到内存中提
取,但是计算机系统中寄存器数量有限,不能任意定义任意多的存储器,局部静态变量不能定义为寄存器变量。*/38register mp_digit *tmpa,*tmpb;3940/* pointer aliases */4142/* source */43 tmpa = a->dp;4445/* destination */46 tmpb = b->dp;47/*对于a.used到b.used-1之间的n,执行b_n=a_n*/48/* copy all the digits */49for(n =0; n < a->used; n++){50*tmpb++=*tmpa++;51}52/*对于a.used到b.used-1之间的n,执行b_n=0*/53/* clear high digits */54for(; n < b->used; n++){55*tmpb++=0;56}57}58/*将a的used值和sign值赋值给b*/59/* copy used count and sign */60 b->used = a->used;61 b->sign = a->sign;62return MP_OKAY;63}
18/* creates "a" then copies b into it */19intmp_init_copy(mp_int * a, mp_int * b)20{21int res;22/*对a进行初始化*/23if((res =mp_init(a))!= MP_OKAY){24return res;25}/*将b复制给a*/26returnmp_copy(b, a);27}
18/* b = |a| */19/**/20/* Simple function copies the input and fixes the sign to positive*/21/**/22int23mp_abs(mp_int * a, mp_int * b)24{25int res;26/*如果a!=b,则将a复制到b*/27/* copy a to b */28if(a != b){29if((res =mp_copy(a, b))!= MP_OKAY){30return res;31}32}33/*将b的sign赋值为MP_ZPOS*/34/* force the sign of b to positive */35 b->sign = MP_ZPOS;3637return MP_OKAY;38}
/*将mp_int设置成一个相对较小的值*/18/* set to a digit */19voidmp_set(mp_int * a, mp_digit b)20{/*将a清零*/21mp_zero(a);/*对a_0进行赋值*/22 a->dp[0]= b & MP_MASK;/*对used进行赋值*/23 a->used =(a->dp[0]!=0)?1:0;24}
/*对mp_int整数a赋值一个长数据类型*/18/* set a 32-bit const */19intmp_set_int(mp_int * a,unsignedlong b)20{21int x, res;22/*将a清零*/23mp_zero(a);24/*一次移动4位*/25/* set four bits at a time */26for(x =0; x <8; x++){27/* shift the number up four bits */28if((res =mp_mul_2d(a,4, a))!= MP_OKAY){29return res;30}3132/* OR in the top four bits of the source */33 a->dp[0]|=(b >>28)&15;3435/* shift the source up to the next four bits */36 b <<=4;3738/* ensure that digits are not clamped off */39 a->used +=1;40}41mp_clamp(a);42return MP_OKAY;43}
/*无符号数的比较*/18/* compare maginitude of two ints (unsigned) */19intmp_cmp_mag(mp_int * a, mp_int * b)20{21int n;22 mp_digit *tmpa,*tmpb;23/*如果a.used大于b.used,则直接返回a>b*/24/* compare based on # of non-zero digits */25if(a->used > b->used){26return MP_GT;27}28/*如果a.used小于b.used,则直接返回a<b*/29if(a->used < b->used){30return MP_LT;31}3233/* alias for a */34 tmpa = a->dp +(a->used -1);3536/* alias for b */37 tmpb = b->dp +(a->used -1);38/*从高位开始进行比较*/39/* compare based on digits */40for(n =0; n < a->used;++n,--tmpa,--tmpb){41if(*tmpa >*tmpb){42return MP_GT;43}4445if(*tmpa <*tmpb){46return MP_LT;47}48}49return MP_EQ;50}
mp_cmp_mag的使用
#include<stdio.h>#include<stdlib.h>#include"utils.h"intmain(){printf("hello libtommath\n");
mp_int a,b;mp_init_multi(&a,&b,NULL);mp_read_radix(&a,"0A0B0C0D",16);mp_read_radix(&b,"-0A0B0C0D",16);int ret =mp_cmp_mag(&a,&b);printf("a和b比较的结果:%d\n",ret);return0;}=================================================:~/workspace/tommath_test$ ./a.out
hello libtommath
a和b比较的结果:0
3.6.2 有符号数比较
mp_cmp的原理
mp_cmp的实现
/*有符号数比较*/18/* compare two ints (signed)*/19int20mp_cmp(mp_int * a, mp_int * b)21{/*如果a和b的符号不同*/22/* compare based on sign */23if(a->sign != b->sign){/*如果a的符号为负,则b的符号为正,a<b*/24if(a->sign == MP_NEG){25return MP_LT;/*如果a的符号为正,则b的符号为负,a>b*/26}else{27return MP_GT;28}29}30/*a和b符号相同的情况*/31/* compare digits *//*如果a和b都是负数,则比较mp_cmp_mag(b,a)*/32if(a->sign == MP_NEG){33/* if negative compare opposite direction */34returnmp_cmp_mag(b, a);/*如果a和b都是正数,则比较mp_cmp_mag(a,b)*/35}else{36returnmp_cmp_mag(a, b);37}38}
mp_cmp的使用
#include<stdio.h>#include<stdlib.h>#include"utils.h"intmain(){printf("hello libtommath\n");
mp_int a,b;mp_init_multi(&a,&b,NULL);mp_read_radix(&a,"0A0B0C0D",16);mp_read_radix(&b,"-0A0B0C0D",16);int ret =mp_cmp(&a,&b);printf("a和b比较的结果:%d\n",ret);return0;}=================================================:~/workspace/tommath_test$ ./a.out
hello libtommath
a和b比较的结果:1