a portable and fast pairing-based cryptography library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mcl/sample/ecdh.cpp

64 lines
1.1 KiB

10 years ago
/*
sample of Elliptic Curve Diffie-Hellman key sharing
*/
#include <iostream>
#include <fstream>
#include <mcl/ec.hpp>
10 years ago
typedef mcl::FpT<mcl::FpTag, 256> Fp;
typedef mcl::FpT<mcl::ZnTag, 256> Fr;
10 years ago
typedef mcl::EcT<Fp> Ec;
void put(const char *msg, const Ec& P)
{
std::cout << msg << P.getStr(mcl::IoEcAffine | 16) << std::endl;
}
10 years ago
int main()
{
/*
Ec is an elliptic curve over Fp
the cyclic group of <P> is isomorphic to Fr
10 years ago
*/
Ec P;
mcl::initCurve<Ec, Fr>(MCL_SECP256K1, &P);
put("P=", P);
10 years ago
/*
Alice setups a private key a and public key aP
*/
Fr a;
10 years ago
Ec aP;
a.setByCSPRNG();
Ec::mul(aP, P, a); // aP = a * P;
10 years ago
put("aP=", aP);
10 years ago
/*
Bob setups a private key b and public key bP
*/
Fr b;
10 years ago
Ec bP;
b.setByCSPRNG();
Ec::mul(bP, P, b); // bP = b * P;
10 years ago
put("bP=", bP);
10 years ago
Ec abP, baP;
// Alice uses bP(B's public key) and a(A's priavte key)
Ec::mul(abP, bP, a); // abP = a * (bP)
10 years ago
// Bob uses aP(A's public key) and b(B's private key)
Ec::mul(baP, aP, b); // baP = b * (aP)
10 years ago
if (abP == baP) {
std::cout << "key sharing succeed:" << abP << std::endl;
} else {
std::cout << "ERR(not here)" << std::endl;
}
}