0% found this document useful (0 votes)
10 views5 pages

Ecc 2

Uploaded by

sakshi bhegade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Ecc 2

Uploaded by

sakshi bhegade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Siddhant Kundlik Thaware

Class: TY-CS-D
Roll No:34
Date: 6/9/23
Prn No:12110937
Batch: 2

ASSIGNMENT-5:

ELLIPTIC CURVE CRYTOGRAPHY:

Code:

#include <assert.h>
#include <stdlib.h> //rand
#include <time.h> //for srand

#include <functional> //require c++11


#include <iostream>
using namespace std;

class G {
public:
G() {} // Default constructor

// Constructor with parameters to initialize the object


G(const int& x, const int& y, const int& p, const int& a) {
this->x = x, this->y = y, this->a = a, this->p = p;
}

// Overloaded '+' operator for adding two points on an


elliptic curve
G operator+(const G& g) const {
assert(this->a == g.a); // Ensure both points are on the
same curve

int t, xr, yr;


if (this->y == 0)
return G(g.x, g.y, this->p, this->a);
else if (g.y == 0)
return G(this->x, this->y, this->p, this->a);
else if (this->x == g.x && this->y == -g.y)
return G(this->x, 0, this->p, this->a);
else if (this->x == g.x && this->y == g.y)
t = mod(3 * this->x * this->x + this->a, this->p) *
inv(2 * this->y, this->p);
else
t = mod(g.y - this->y, this->p) * inv(mod(g.x - this-
>x, this->p), this->p);

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);

return G(xr, yr, this->p, this->a);


}

// Overloaded '-' operator for subtracting two points on an


elliptic curve
G operator-(const G& g) const {
return *this + G(g.x, -g.y, this->p, g.a);
}

// Overloaded '*' operator for scalar multiplication of a


point on an elliptic curve
G operator*(const int& n) const {
G g = *this;
for (int i = 1; i < n; i++)
g = g + *this;
return g;
}

int x, y, p, a; // Public parameters of the point

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;
}

// Helper function to compute the modulo of a number


int mod(const int& num, const int& mod) const {
return (num % mod + mod) % mod;
}
};

// Overloaded '*' operator for scalar multiplication with the


scalar on the left-hand side
G operator*(const int& n, const G& g) {
return g * n;
}

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);
}

// Setter function to update the curve parameters and base


point
void set(const int& a, const int& b, const int& q, const G& g)
{
this->a = a, this->b = b, this->q = q, this->g = g;
}

// Function to generate a random private key in the range [1,


q-1]
int getPrivateKey(const int& q) const {
return (rand() % (q - 1)) + 1; // Random number between 1
and q-1
}

// Function to compute the public key corresponding to a


private key
G getPublicKey(const int& privateKey, const G& g) const {
return privateKey * g; // Scalar multiplication of the
base point
}

// Function to encrypt a message given sender's private key,


receiver's public key, and message point
pair<G, G> getEncryptedMessage(const int& senderPrivateKey,
const G& receiverPublicKey, const G& message) const {
G c1 = senderPrivateKey * g; //
k*G
G c2 = message + senderPrivateKey * receiverPublicKey; //
Pm + k*Pb
return {c1, c2};
}
// Function to decrypt an encrypted message given the pair of
points and the receiver's private key
G getDecryptedMessage(const pair<G, G>& encryptedMessage,
const int& receiverPrivateKey) const {
return encryptedMessage.second - receiverPrivateKey *
encryptedMessage.first; // C2 - nb*C1
}

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;

G pm1 = ec.getDecryptedMessage(cm, nb); // Decrypt the


message using the receiver's private key
cout << "pm=" << pm1.x << "," << pm1.y << endl;

return 0;
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy