⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vlong.cpp

📁 椭圆曲线密码C实现的
💻 CPP
字号:
// vlong.cpp: implementation of the vlong class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "vlong.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

vlong::vlong (unsigned x)
{
	pValue = new vlong_value;
	negative = 0;
	pValue->init(x);
}

vlong::vlong ( const vlong& x ) // copy constructor
{
	negative = x.negative;
	pValue = x.pValue;
	pValue->share += 1;
}
/**/
vlong::~vlong()
{
	if ( pValue->share ) pValue->share -=1; else delete pValue;
}

void vlong::docopy()
{
	if ( pValue->share )
	{
		pValue->share -= 1;
		vlong_value * nv = new vlong_value;
		nv->copy(*pValue);
		pValue = nv;
	}
}

int vlong::cf( const vlong x ) const
{
	int neg = negative && !pValue->is_zero();
	if ( neg == (x.negative && !x.pValue->is_zero()) )
		return pValue->cf( *x.pValue );
	else if ( neg ) return -1;
	else return +1;
}

vlong& vlong::operator =(const vlong& x)
{
	if ( pValue->share ) pValue->share -=1; else delete pValue;
	pValue = x.pValue;
	pValue->share += 1;
	negative = x.negative;
	return *this;
}
/**/
vlong::operator unsigned () // conversion to unsigned
{
	return *pValue;
}

vlong& vlong::operator +=(const vlong& x)
{
	if ( negative == x.negative )
	{
		docopy();
		pValue->add( *x.pValue );
	}
	else if ( pValue->cf( *x.pValue ) >= 0 )
	{
		docopy();
		pValue->subtract( *x.pValue );
	}
	else
	{
		vlong tmp = *this;
		*this = x;
		*this += tmp;
	}
	return *this;
}

vlong& vlong::operator -=(const vlong& x)
{
	if ( negative != x.negative )
	{
		docopy();
		pValue->add( *x.pValue );
	}
	else if ( pValue->cf( *x.pValue ) >= 0 )
	{
		docopy();
		pValue->subtract( *x.pValue );
	}
	else
	{
		vlong tmp = *this;
		*this = x;
		*this -= tmp;
		negative = 1 - negative;
	}
	return *this;
}

/////////////////////////////////////////////////////////////////////////////


vlong operator +( const vlong& x, const vlong& y )
{
  vlong result = x;
  result += y;
  return result;
}

vlong operator -( const vlong& x, const vlong& y )
{
  vlong result = x;
  result -= y;
  return result;
}

vlong operator *( const vlong& x, const vlong& y )
{
  vlong result;
  result.pValue->mul( *x.pValue, *y.pValue );
  result.negative = x.negative ^ y.negative;
  return result;
}

vlong operator /( const vlong& x, const vlong& y )
{
  vlong result;
  vlong_value rem;
  result.pValue->divide( *x.pValue, *y.pValue, rem );
  result.negative = x.negative ^ y.negative;
  return result;
}

vlong operator %( const vlong& x, const vlong& y )
{
  vlong result;
  vlong_value divide;
  divide.divide( *x.pValue, *y.pValue, *result.pValue );
  result.negative = x.negative; // not sure about this?
  return result;
}
vlong modexp( const vlong & x, const vlong & e, const vlong & m )
{
	monty me(m);
	return me.exp( x,e );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -