代码编译连接都ok,运行半天最后报错 ,就是调用 计算数乘nP的函数Point_Multiply 时就卡住了,单步跟踪到这就执行不下去了。
调试怀疑是内存溢出,水平有限,希望大神们帮帮我
Point Point_Calculate(Point P,Point Q) //点加和倍点加
{
BigInteger r,temp0(0),temp2(2),temp3(3);
Point R;
if (Compare(P.x)&&Compare(P.y))
{
R=Q;return R;
}
else if (Compare(Q.x)&&Compare(Q.y))
{
R=P;return R;
}
else if (Compare(P.x,Q.x))
{
r=(Q.y-P.y)*Inv(Q.x-P.x,q)%q;
}
else if (Compare(P.y,Q.y))
{
R.x=R.y=temp0;return R;
}
else if (Compare(Q.y))
{
R.x=R.y=temp0;return R;
}
else
{
r=(temp3*P.x*P.x+a)*Inv(temp2*P.y,q)%q;
}
R.x=(r*r%q-P.x-Q.x)%q;
if (Compare(R.x,temp0)==-1)
{
R.x=R.x+q;
}
R.y=(r*(P.x-R.x)-P.y)%q;
if (Compare(R.y,temp0)==-1)
{
R.y=R.y+q;
}
return R;
}
//计算数乘nP,采用重复平方法计算
Point Point_Multiply(Point P,BigInteger n)
{
Point result;
BigInteger temp,temp2(2),temp0(0);
int b[160],count;
result.x=result.y=temp0;//初始化为O点
n.BigNumToIndex(b,count);//得到n的二进制表示
//按重复平方法求解nP
for(int i=count-1;i>=0;i--)
{
result=Point_Calculate(result,result);
if (b[i])
{
result=Point_Calculate(result,P);
}
}
return result;
}
//将大整数转化为二进制数,并不影响*this的值
void BigInteger::BigNumToIndex(int b[],int &count)
{
BigInteger BigNum=*this,temp2(2),temp;
count=0;
while (int(BigNum.Head->Num))
{
temp=BigNum%temp2;
b[count++]=int(temp.Head->Num);
BigNum=BigNum/temp2;
}
}
//大整数的定义
BigInteger::BigInteger() //构造函数,将每个节点置空
{
Head=End=TempNode=NULL;
}
BigInteger::BigInteger(char i) //构造函数,只拥有一位的大整数
{
Head=End=TempNode=NULL;
TempNode=new Node;
TempNode->Num=i;
TempNode->Prev=NULL;
Head=End=TempNode;
TempNode->Next=NULL;
}
BigInteger::BigInteger(const BigInteger &BigNum) //拷贝构造
{
Node *p;
Head=End=TempNode=NULL;
p=BigNum.Head;
while(p)
{
AddEnd(p->Num);
p=p->Next;
}
}
BigInteger::~BigInteger() //析构
{
Node *NextNode;
if(Head==NULL)
return;
TempNode=Head;
while(TempNode)
{
NextNode=TempNode->Next;
delete TempNode;
TempNode=NextNode;
}
Head=NULL;
End=NULL;
TempNode=NULL;
}
void BigInteger::AddHead(char Num) //在链表头插入节点的操作
{
TempNode=new Node;
TempNode->Num=Num;
TempNode->Prev=NULL;
if(!Head)
{
Head=End=TempNode;
TempNode->Next=NULL;
}
else
{
TempNode->Next=Head;
Head->Prev=TempNode;
Head=TempNode;
}
}
void BigInteger::AddEnd(char Num) //在链表尾插入节点的操作
{
TempNode=new Node;
TempNode->Num=Num;
TempNode->Next=NULL;
if(!Head)
{
Head=End=TempNode;
TempNode->Prev=NULL;
}
else
{
TempNode->Prev=End;
End->Next=TempNode;
End=TempNode;
}
}