Ecc 2
Ecc 2
Ecc 2
Class: TY-CS-D
Roll No:34
Date: 6/9/23
Prn No:12110937
Batch: 2
ASSIGNMENT-5:
Code:
#include <assert.h>
#include <stdlib.h> //rand
#include <time.h> //for srand
class G {
public:
G() {} // Default constructor
t = mod(t, this->p);
xr = mod(t * t - this->x - g.x, this->p);
yr = t * (this->x - xr) - this->y;
yr = mod(yr, this->p);
private:
// Helper function to compute the modular inverse of a number
int inv(const int& num, const int& mod) const {
auto exgcd = [](const int& a, const int& b, int& g, int&
x, int& y) {
std::function<void(const int&, const int&, int&, int&,
int&)> exgcd;
exgcd = [&](const int& a, const int& b, int& g, int&
x, int& y) {
if (!b)
g = a, x = 1, y = 0;
else
exgcd(b, a % b, g, y, x), y -= x * (a / b);
};
return exgcd(a, b, g, x, y);
};
int g, x, y;
exgcd(num, mod, g, x, y);
return ((x % mod) + mod) % mod;
}
class Elliptic_curve {
public:
// Constructor to initialize the elliptic curve parameters and
a base point
Elliptic_curve(const int& a, const int& b, const int& q, const
G& g) {
set(a, b, q, g);
}
private:
int a, b, q;
G g; // Base point on the elliptic curve
};
int main() {
int q = 11, a = 1, b = 6;
G g(2, 7, q, a);
Elliptic_curve ec(a, b, q, g);
int nb = 7;
G pb = ec.getPublicKey(nb,g); // Compute the public key for
receiver nb
cout << "pb=" << pb.x << "," << pb.y << endl;
int na = 3;
G pm(10, 9, q, a); // Define the message point Pm
pair<G, G> cm = ec.getEncryptedMessage(na, pb, pm); //
Encrypt the message for receiver pb
cout << "cm1=" << cm.first.x << "," << cm.first.y << endl;
cout << "cm2=" << cm.second.x << "," << cm.second.y << endl;
return 0;
}
Output: