-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecc.h
More file actions
126 lines (100 loc) · 3.06 KB
/
ecc.h
File metadata and controls
126 lines (100 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Elliptic Curve Cryptography class header file
* this header file implements ECC class, which is designed to work with Elliptic Curve Cryptography
*
*
* Functionality:
* int * encodeMessage(int message) -- encodes user message. Returns pointer to array of 4 ellements
* int decodeMessage(int cyperText[4]) -- decodes cyperText and returns int - message
*
*/
#include <cstdio>
#include <iostream>
#include <time.h>
#include "ec_utils.h"
using namespace std;
#define MAX_Y 329
// The first coordinate is concidered to be the value, the second one is ignored
class ECC {
public:
int a = 4; // elliptic curve coefficient
int b = 20; // elliptic curve coefficient
static const int p = 329; // prime number to provide finite field
int points[p*p][2]; // points satisfying the curve
int PrivKey = 19; // Private Key of Receiver
int PubKey[2] = {0,0}; // Public key of Receiver
int k = 19; // Random Number required for Encoding
int Pbase[2] = {0,0}; // Base point
int *temp;
int P[2]; // kG, k*Pbase - first part of cyper text
int R[2]; // Pm+kPb, k*PubKey + message - second part of cyper text
ECC(){
generate();
// Deciding the base point
Pbase[0] = points[7][0];
Pbase[1] = points[7][1];
temp=genKey(PrivKey,Pbase); // generate public key, which is multiplication
PubKey[0] = *temp;
PubKey[1] = *(temp+1);
}
int * encodeMessage(int userMessage) {
temp = ec::sclr_mult(k,Pbase,a,p);
P[0] = *temp;
P[1] = *(temp+1);
int message[2];
message[0] = userMessage;
message[1] = 122;
// message[1] = 1 + rand() % MAX_Y;
int Q[2]; // k*PubKey, interim result when calculating second part of cyper text
temp = ec::sclr_mult(k,PubKey,a,p);
Q[0] = *temp;
Q[1] = *(temp+1);
temp = ec::add(message,Q,a,p);
R[0] = *temp;
R[1] = *(temp+1);
int *encoded = (int*) malloc(4*sizeof(int));
*encoded = P[0];
*(encoded+1) = P[1];
*(encoded+2) = R[0];
*(encoded+3) = R[1];
return encoded;
}
int decodeMessage(int cyperText[4]) {
int O[2]; // nBkG
int P[2] = {cyperText[0], cyperText[1]};
temp = ec::sclr_mult(PrivKey,P,a,p);
O[0] = *temp;
O[1] = p - *(temp+1);
int R[2] = {cyperText[2], cyperText[3]};
temp = ec::add(R,O,a,p);
O[0] = *temp;
O[1] = *(temp+1);
return O[0];
}
void generate() { // computes all integer points on curve in range
srand(time(0));
int rhs,lhs,i=0;
for(int x = 0; x < p; x++)
{
rhs=((x*x*x)+(a*x)+b)%p;
for(int y = 0; y < p; y++)
{
lhs = (y*y)%p;
if (lhs==rhs)
{
points[i][0]=x;
points[i][1]=y;
i+=1;
}
}
}
}
int * genKey(int X,int P[2]){ // generate public key, which is multiplication
int *temp;
int *Q = (int*) malloc(2*sizeof(int));
temp=ec::sclr_mult(X,P,a,p);
Q[0]=*temp;
Q[1]=*(temp+1);
return Q;
}
};