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

📄 modexppc.cpp

📁 几十种常用的加密算法库 vc6 模块封装良好方便使用
💻 CPP
字号:
// modexppc.cpp - written and placed in the public domain by Wei Dai

#include "pch.h"
#include "modexppc.h"
#include "asn.h"

#include "algebra.cpp"
#include "eprecomp.cpp"

NAMESPACE_BEGIN(CryptoPP)

ModExpPrecomputation::~ModExpPrecomputation() {}

ModExpPrecomputation::ModExpPrecomputation(const Integer &modulus, const Integer &base, unsigned int maxExpBits, unsigned int storage)
{
	Precompute(modulus, base, maxExpBits, storage);
}

ModExpPrecomputation::ModExpPrecomputation(const ModExpPrecomputation &mep)
	: mr(new MontgomeryRepresentation(*mep.mr)), 
	  mg(new MR_MG(*mr)), 
	  ep(new ExponentiationPrecomputation<MR_MG>(*mg, *mep.ep))
{
}

void ModExpPrecomputation::Precompute(const Integer &modulus, const Integer &base, unsigned int maxExpBits, unsigned int storage)
{
	if (!mr.get() || mr->GetModulus()!=modulus)
	{
		mr.reset(new MontgomeryRepresentation(modulus));
		mg.reset(new MR_MG(*mr));
		ep.reset(NULL);
	}

	if (!ep.get() || ep->storage < storage)
		ep.reset(new ExponentiationPrecomputation<MR_MG>(*mg, mr->ConvertIn(base), maxExpBits, storage));
}

void ModExpPrecomputation::Load(const Integer &modulus, BufferedTransformation &bt)
{
	if (!mr.get() || mr->GetModulus()!=modulus)
	{
		mr.reset(new MontgomeryRepresentation(modulus));
		mg.reset(new MR_MG(*mr));
	}

	ep.reset(new ExponentiationPrecomputation<MR_MG>(*mg));
	BERSequenceDecoder seq(bt);
	ep->storage = (unsigned int)(Integer(seq).ConvertToLong());
	ep->exponentBase.BERDecode(seq);
	ep->g.resize(ep->storage);
	for (unsigned i=0; i<ep->storage; i++)
		ep->g[i].BERDecode(seq);
}

void ModExpPrecomputation::Save(BufferedTransformation &bt) const
{
	assert(ep.get());
	DERSequenceEncoder seq(bt);
	Integer(ep->storage).DEREncode(seq);
	ep->exponentBase.DEREncode(seq);
	for (unsigned i=0; i<ep->storage; i++)
		ep->g[i].DEREncode(seq);
}

Integer ModExpPrecomputation::Exponentiate(const Integer &exponent) const
{
	assert(mr.get() && ep.get());
	return mr->ConvertOut(ep->Exponentiate(exponent));
}

Integer ModExpPrecomputation::CascadeExponentiate(const Integer &exponent, const ModExpPrecomputation &pc2, const Integer &exponent2) const
{
	assert(mr.get() && ep.get());
	return mr->ConvertOut(ep->CascadeExponentiate(exponent, *pc2.ep, exponent2));
}

NAMESPACE_END

⌨️ 快捷键说明

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