40namespace elliptic_curve_key_exchange {
64 op << p.x <<
" " << p.y;
83 number = number % mod;
86 ans = (ans * number) % mod;
90 number = (number * number) % mod;
116 if (a.x != b.x || a.y != b.y) {
121 uint256_t num = (b.y - a.y + mod), den = (b.x - a.x + mod);
122 lambda = (num * (
exp(den, mod - 2, mod))) % mod;
144 (((axsq + curve_a_coeff) % mod) *
exp(a_2, mod - 2, mod)) % mod;
149 c.x = ((lambda * lambda) % mod + (mod << 1) - a.x - b.x) % mod;
150 c.y = (((lambda * (a.x + mod - c.x)) % mod) + mod - a.y) % mod;
172 Point Q = {inf, inf};
175 if (Q.x == inf && Q.y == inf) {
179 Q =
addition(Q, N, curve_a_coeff, mod);
184 N =
addition(N, N, curve_a_coeff, mod);
200 assert(a + b == 2434);
201 assert(b - a == 2190);
202 assert(a * b == 282064);
204 assert(b % a == 116);
205 assert((a & b) == 8);
206 assert((a | b) == 2426);
207 assert((a ^ b) == 2418);
208 assert((a << 64) ==
uint128_t(
"2250502776992565297152"));
209 assert((b >> 7) == 18);
214 assert(a + b ==
uint128_t(
"12321421424255265334"));
215 assert(a - b ==
uint128_t(
"12321421424209018910"));
216 assert(a * b ==
uint128_t(
"284910839733861759501135864"));
217 assert(a / b == 532859423865LL);
218 assert(a % b == 3887742);
219 assert((a & b) == 18912520);
220 assert((a | b) ==
uint128_t(
"12321421424236352814"));
221 assert((a ^ b) ==
uint128_t(
"12321421424217440294"));
222 assert((a << 64) ==
uint128_t(
"227290107637132170748078080907806769152"));
233 assert(a + b == 2434);
234 assert(b - a == 2190);
235 assert(a * b == 282064);
237 assert(b % a == 116);
238 assert((a & b) == 8);
239 assert((a | b) == 2426);
240 assert((a ^ b) == 2418);
241 assert((a << 64) ==
uint256_t(
"2250502776992565297152"));
242 assert((b >> 7) == 18);
245 a =
uint256_t(
"12321423124513251424232142122");
246 b =
uint256_t(
"23124312431243243215354315132413213212");
247 assert(a + b ==
uint256_t(
"23124312443564666339867566556645355334"));
249 assert(a - b ==
uint256_t(
"115792089237316195423570985008687907853246860353"
250 "221642219366742944204948568846"));
251 assert(a * b ==
uint256_t(
"284924437928789743312147393953938013677909398222"
252 "169728183872115864"));
253 assert(b / a ==
uint256_t(
"1876756621"));
254 assert(b % a ==
uint256_t(
"2170491202688962563936723450"));
255 assert((a & b) ==
uint256_t(
"3553901085693256462344"));
256 assert((a | b) ==
uint256_t(
"23124312443564662785966480863388892990"));
257 assert((a ^ b) ==
uint256_t(
"23124312443564659232065395170132430646"));
258 assert((a << 128) ==
uint256_t(
"4192763024643754272961909047609369343091683"
259 "376561852756163540549632"));
271 uint256_t a(
"4451685225093714772084598273548424"),
272 b(
"2061118396808653202902996166388514"),
273 mod(
"4451685225093714772084598273548427");
277 uint256_t(
"188281465057972534892223778713752"),
278 uint256_t(
"3419875491033170827167861896082688")};
282 std::cout <<
"For alice:\n";
284 uint256_t alice_private_key(
"164330438812053169644452143505618");
286 multiply(ptr, a, alice_private_key, mod);
287 std::cout <<
"\tPrivate key: " << alice_private_key <<
"\n";
288 std::cout <<
"\tPublic Key: " << alice_public_key << std::endl;
291 std::cout <<
"For Bob:\n";
293 uint256_t bob_private_key(
"1959473333748537081510525763478373");
295 multiply(ptr, a, bob_private_key, mod);
296 std::cout <<
"\tPrivate key: " << bob_private_key <<
"\n";
297 std::cout <<
"\tPublic Key: " << bob_public_key << std::endl;
303 alice_private_key, mod),
306 bob_private_key, mod);
308 std::cout <<
"Shared keys:\n";
309 std::cout << alice_shared_key << std::endl;
310 std::cout << bob_shared_key << std::endl;
313 assert(alice_shared_key == bob_shared_key);
class for 128-bit unsigned integer
class for 256-bit unsigned integer
static void uint256_t_tests()
Function to test the uint256_t header.
static void uint128_t_tests()
Function to test the uint128_t header.
static void test()
Function to test the provided algorithm above.
Point multiply(const Point &a, const uint256_t &curve_a_coeff, uint256_t p, const uint256_t &mod)
multiply Point and integer
uint256_t exp(uint256_t number, uint256_t power, const uint256_t &mod)
This function calculates number raised to exponent power under modulo mod using Modular Exponentiatio...
Point addition(Point a, Point b, const uint256_t &curve_a_coeff, uint256_t mod)
Addition of points.
struct ciphers::elliptic_curve_key_exchange::Point Point
Definition of struct Point.
Algorithms for encryption and decryption.
int multiply(int x, int res[], int res_size)
Definition of struct Point.
bool operator==(const Point &p)
x and y co-ordinates
friend std::ostream & operator<<(std::ostream &op, const Point &p)
ostream operator for printing Point