本文整理汇总了C++中cryptopp::Integer类的典型用法代码示例。如果您正苦于以下问题:C++ Integer类的具体用法?C++ Integer怎么用?C++ Integer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Integer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: invalid_argument
const vector<unsigned char> CryptoPpDlogZpSafePrime::decodeGroupElementToByteArray(GroupElement * groupElement) {
ZpSafePrimeElementCryptoPp * zp_element = dynamic_cast<ZpSafePrimeElementCryptoPp *>(groupElement);
if (!(zp_element))
throw invalid_argument("element type doesn't match the group type");
//Given a group element y, find the two inverses z,-z. Take z to be the value between 1 and (p-1)/2. Return s=z-1
biginteger y = zp_element->getElementValue();
biginteger p = ((ZpGroupParams * ) groupParams)->getP();
MathAlgorithms::SquareRootResults roots = MathAlgorithms::sqrtModP_3_4(y, p);
biginteger goodRoot;
biginteger halfP = (p - 1) / 2;
if (roots.getRoot1()>1 && roots.getRoot1() < halfP)
goodRoot = roots.getRoot1();
else
goodRoot = roots.getRoot2();
goodRoot -= 1;
CryptoPP::Integer cpi = biginteger_to_cryptoppint(goodRoot);
int len = ceil((cpi.BitCount() + 1) / 8.0); //ceil(find_log2_floor(goodRoot) / 8.0);
byte * output = new byte[len];
cpi.Encode(output, len);
vector<byte> res;
// Remove the padding byte at the most significant position (that was added while encoding)
for (int i = 1; i < len; ++i)
res.push_back(output[i]);
return res;
}
示例2: VerifyPublicKeyValid
bool CryptoECDSA::VerifyPublicKeyValid(SecureBinaryData const & pubKey65)
{
if(CRYPTO_DEBUG)
{
cout << "BinPub: " << pubKey65.toHexStr() << endl;
}
// Basically just copying the ParsePublicKey method, but without
// the assert that would throw an error from C++
SecureBinaryData pubXbin(pubKey65.getSliceRef( 1,32));
SecureBinaryData pubYbin(pubKey65.getSliceRef(33,32));
CryptoPP::Integer pubX;
CryptoPP::Integer pubY;
pubX.Decode(pubXbin.getPtr(), pubXbin.getSize(), UNSIGNED);
pubY.Decode(pubYbin.getPtr(), pubYbin.getSize(), UNSIGNED);
BTC_ECPOINT publicPoint(pubX, pubY);
// Initialize the public key with the ECP point just created
BTC_PUBKEY cppPubKey;
cppPubKey.Initialize(CryptoPP::ASN1::secp256k1(), publicPoint);
// Validate the public key -- not sure why this needs a PRNG
static BTC_PRNG prng;
return cppPubKey.Validate(prng, 3);
}
示例3: SerializePrivateKey
SecureBinaryData CryptoECDSA::SerializePrivateKey(BTC_PRIVKEY const & privKey)
{
CryptoPP::Integer privateExp = privKey.GetPrivateExponent();
SecureBinaryData privKeyData(32);
privateExp.Encode(privKeyData.getPtr(), privKeyData.getSize(), UNSIGNED);
return privKeyData;
}
示例4:
std::vector<byte> encode_signature(CryptoPP::Integer r, CryptoPP::Integer s) {
std::vector<byte> signature;
signature.resize(64);
r.Encode(signature.data(), 32);
s.Encode(signature.data() + 32, 32);
return signature;
}
示例5: ParsePrivateKey
BTC_PRIVKEY CryptoECDSA::ParsePrivateKey(SecureBinaryData const & privKeyData)
{
BTC_PRIVKEY cppPrivKey;
CryptoPP::Integer privateExp;
privateExp.Decode(privKeyData.getPtr(), privKeyData.getSize(), UNSIGNED);
cppPrivKey.Initialize(CryptoPP::ASN1::secp256k1(), privateExp);
return cppPrivKey;
}
示例6: SerializePublicKey
SecureBinaryData CryptoECDSA::SerializePublicKey(BTC_PUBKEY const & pubKey)
{
BTC_ECPOINT publicPoint = pubKey.GetPublicElement();
CryptoPP::Integer pubX = publicPoint.x;
CryptoPP::Integer pubY = publicPoint.y;
SecureBinaryData pubData(65);
pubData.fill(0x04); // we fill just to set the first byte...
pubX.Encode(pubData.getPtr()+1, 32, UNSIGNED);
pubY.Encode(pubData.getPtr()+33, 32, UNSIGNED);
return pubData;
}
示例7: Mul
CryptoPP::ECP::Point Mul (const CryptoPP::ECP::Point& p, const CryptoPP::Integer& e) const
{
CryptoPP::ECP::Point res {0, 1};
if (!e.IsZero ())
{
auto bitCount = e.BitCount ();
for (int i = bitCount - 1; i >= 0; i--)
{
res = Sum (res, res);
if (e.GetBit (i)) res = Sum (res, p);
}
}
return res;
}
示例8: str
std::string security::str(const CryptoPP::Integer& input)
{
const auto input_size = input.MinEncodedSize();
CryptoPP::SecByteBlock block(input_size);
input.Encode(block.BytePtr(), input_size);
return str(block);
// NOTE: CryptoPP::Integer has a weird ostream format (uses prefixes to specify numeric base)
//
/*std::ostringstream oss;
oss << std::hex << input;
return oss.str();*/
}
示例9: ComputeChainedPublicKey
/////////////////////////////////////////////////////////////////////////////
// Deterministically generate new public key using a chaincode
SecureBinaryData CryptoECDSA::ComputeChainedPublicKey(
SecureBinaryData const & binPubKey,
SecureBinaryData const & chainCode,
SecureBinaryData* multiplierOut)
{
if(CRYPTO_DEBUG)
{
cout << "ComputeChainedPUBLICKey:" << endl;
cout << " BinPub: " << binPubKey.toHexStr() << endl;
cout << " BinChn: " << chainCode.toHexStr() << endl;
}
static SecureBinaryData SECP256K1_ORDER_BE = SecureBinaryData::CreateFromHex(
"fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
// Added extra entropy to chaincode by xor'ing with hash256 of pubkey
BinaryData chainMod = binPubKey.getHash256();
BinaryData chainOrig = chainCode.getRawCopy();
BinaryData chainXor(32);
for(uint8_t i=0; i<8; i++)
{
uint8_t offset = 4*i;
*(uint32_t*)(chainXor.getPtr()+offset) =
*(uint32_t*)( chainMod.getPtr()+offset) ^
*(uint32_t*)(chainOrig.getPtr()+offset);
}
// Parse the chaincode as a big-endian integer
CryptoPP::Integer mult;
mult.Decode(chainXor.getPtr(), chainXor.getSize(), UNSIGNED);
// "new" init as "old", to make sure it's initialized on the correct curve
BTC_PUBKEY oldPubKey = ParsePublicKey(binPubKey);
BTC_PUBKEY newPubKey = ParsePublicKey(binPubKey);
// Let Crypto++ do the EC math for us, serialize the new public key
newPubKey.SetPublicElement( oldPubKey.ExponentiatePublicElement(mult) );
if(multiplierOut != NULL)
(*multiplierOut) = SecureBinaryData(chainXor);
//LOGINFO << "Computed new chained public key using:";
//LOGINFO << " Public key: " << binPubKey.toHexStr().c_str();
//LOGINFO << " PubKeyHash: " << chainMod.toHexStr().c_str();
//LOGINFO << " Chaincode: " << chainOrig.toHexStr().c_str();
//LOGINFO << " Multiplier: " << chainXor.toHexStr().c_str();
return CryptoECDSA::SerializePublicKey(newPubKey);
}
示例10: verify
bool verify(CryptoPP::ECPPoint Q, byte *message, unsigned message_length, CryptoPP::Integer r, CryptoPP::Integer s){
auto ec = common::ec_parameters().GetCurve();
auto G = common::ec_parameters().GetSubgroupGenerator();
auto n = common::ec_parameters().GetGroupOrder();
Integer z = hash_m_to_int(message, message_length, n.ByteCount());
// verify
if (Q == ec.Identity()){
cerr << "Q == O" << endl;
return false;
}
if (!(ec.Multiply(n, Q) == ec.Identity())){
cerr << "n x Q != O" << endl;
return false;
}
if (r <= 0 || r >= n){
cerr << "incorrect r" << endl;
return false;
}
if (s <= 0 || s >= n){
cerr << "incorrect s" << endl;
return false;
}
Integer w = s.InverseMod(n);
Integer u1 = a_times_b_mod_c(z, w, n);
Integer u2 = a_times_b_mod_c(r, w, n);
ECPPoint P2 = ec.Add(ec.Multiply(u1, G), ec.Multiply(u2, Q));
if (P2.x != r){
cerr << "P2.x != r" << endl;
return false;
}
return true;
}
示例11: ECVerifyPoint
bool CryptoECDSA::ECVerifyPoint(BinaryData const & x,
BinaryData const & y)
{
BTC_PUBKEY cppPubKey;
CryptoPP::Integer pubX;
CryptoPP::Integer pubY;
pubX.Decode(x.getPtr(), x.getSize(), UNSIGNED);
pubY.Decode(y.getPtr(), y.getSize(), UNSIGNED);
BTC_ECPOINT publicPoint(pubX, pubY);
// Initialize the public key with the ECP point just created
cppPubKey.Initialize(CryptoPP::ASN1::secp256k1(), publicPoint);
// Validate the public key -- not sure why this needs a PRNG
BTC_PRNG prng;
return cppPubKey.Validate(prng, 3);
}
示例12: Read
template<> unsigned long Read(CryptoPP::Integer & val)
{
std::vector<unsigned char> data;
unsigned long rValue = SimpleReader::Read(data);
val.Decode(&data[0], data.size());
return rValue;
}
示例13: RecoverX
CryptoPP::Integer RecoverX (const CryptoPP::Integer& y) const
{
auto y2 = y.Squared ();
auto xx = (y2 - CryptoPP::Integer::One())*(d*y2 + CryptoPP::Integer::One()).InverseMod (q);
auto x = a_exp_b_mod_c (xx, (q + CryptoPP::Integer (3)).DividedBy (8), q);
if (!(x.Squared () - xx).Modulo (q).IsZero ())
x = a_times_b_mod_c (x, I, q);
if (x.IsOdd ()) x = q - x;
return x;
}
示例14: CopyInteger
CK_RV CKKeyObject::CopyInteger(CK_ATTRIBUTE& attribute,
const CryptoPP::Integer& intValue) const
{
CK_ULONG intSize = intValue.MinEncodedSize();
if (attribute.pValue == NULL_PTR)
{
attribute.ulValueLen = intSize;
}
else if (attribute.ulValueLen < intSize)
{
attribute.ulValueLen = (CK_ULONG)-1;
return CKR_BUFFER_TOO_SMALL;
}
else
{
attribute.ulValueLen = intSize;
intValue.Encode((byte*)attribute.pValue, attribute.ulValueLen);
}
return CKR_OK;
}
示例15: ParsePublicKey
BTC_PUBKEY CryptoECDSA::ParsePublicKey(SecureBinaryData const & pubKeyX32B,
SecureBinaryData const & pubKeyY32B)
{
BTC_PUBKEY cppPubKey;
CryptoPP::Integer pubX;
CryptoPP::Integer pubY;
pubX.Decode(pubKeyX32B.getPtr(), pubKeyX32B.getSize(), UNSIGNED);
pubY.Decode(pubKeyY32B.getPtr(), pubKeyY32B.getSize(), UNSIGNED);
BTC_ECPOINT publicPoint(pubX, pubY);
// Initialize the public key with the ECP point just created
cppPubKey.Initialize(CryptoPP::ASN1::secp256k1(), publicPoint);
// Validate the public key -- not sure why this needs a PRNG
BTC_PRNG prng;
assert(cppPubKey.Validate(prng, 3));
return cppPubKey;
}