Skip to content

Instantly share code, notes, and snippets.

@tmt514
Created March 20, 2018 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmt514/40d27d7944ead4d5448f72c1908fec19 to your computer and use it in GitHub Desktop.
Save tmt514/40d27d7944ead4d5448f72c1908fec19 to your computer and use it in GitHub Desktop.
#ifndef ZP_H
#define ZP_H
#include <iostream>
#include <utility>
#include "uberzahl.h"
using namespace std;
/*
* Parameters for EC and transform into constants
*/
#define PRIME_STR "11"
#define ORDER_STR "13"
#define A_STR "1"
#define B_STR "6" //in hex
#define GX_STR "2"
#define GY_STR "7"
#define MESSAGE0_STR "3"
#define MESSAGE1_STR "6"
#define XA_STR "7" //private Key of receiver
#define XB_STR "9" //private key of sender
const uberzahl PRIME(PRIME_STR);
const uberzahl ORDER(ORDER_STR);
const uberzahl A(A_STR);
const uberzahl B(B_STR,16);
const uberzahl GX(GX_STR,16);
const uberzahl GY(GY_STR,16);
const uberzahl MESSAGE0(MESSAGE0_STR);
const uberzahl MESSAGE1(MESSAGE1_STR);
const uberzahl XA(XA_STR); //private key of receiver
const uberzahl XB(XB_STR); //private key of sender
/*
* class Zp: An element in GF(p)
*/
class Zp{
// Overloading cout
friend ostream& operator<<(ostream& output, const Zp& a);
private:
uberzahl value;
public:
Zp(){}
Zp(const uberzahl v){
value = v;
if(value >= PRIME || value < "0")
value = value % PRIME;
}
Zp(const int v){
value = v;
if(value>=PRIME || value < "0")
value = value % PRIME;
}
uberzahl getValue() const { return value; }
Zp operator + (const Zp &a) const;
Zp operator - (const Zp &a) const;
Zp operator - () const;
Zp operator * (const Zp &a) const;
bool operator == (const Zp &a) const;
Zp inverse() const;
};
/*
* class ECpoint: A point on an elliptic curve
*/
class ECpoint{
// Overloading cout
friend ostream& operator<<(ostream& output, const ECpoint& a);
public:
Zp x;
Zp y;
bool infinityPoint; //If true, the point is the infinity point
ECpoint(){
infinityPoint = false;}
ECpoint(Zp xx, Zp yy){ x = xx; y = yy; infinityPoint = false;}
ECpoint(bool inf){ infinityPoint = inf;}
ECpoint repeatSum(ECpoint p, uberzahl v) const;
bool operator == (const ECpoint &a) const;
ECpoint operator + (const ECpoint &a) const;
};
ECpoint operator * (const uberzahl &a, const ECpoint &b);
/*
* class ECsystem: Encryption and decryption functions of ec
*/
class ECsystem{
private:
uberzahl privateKey;
ECpoint publicKey;
ECpoint G; //Generator G
public:
Zp power(Zp base, uberzahl pow);
uberzahl pointCompress(ECpoint e);
ECpoint pointDecompress(uberzahl compressedPoint);
//public:
ECsystem(){ G = ECpoint(GX, GY);}
ECpoint getPublicKey(){ return publicKey;}
pair <ECpoint, uberzahl> generateKeys();
pair <pair<Zp,Zp>,uberzahl> encrypt(ECpoint publicKey, uberzahl privateKey, Zp plaintext0, Zp plaintext1);
pair <Zp,Zp> decrypt(pair<pair<Zp,Zp>,uberzahl> cm);
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment