# Algorithms_in_C++: ciphers::elliptic_curve_key_exchange Namespace Reference

Loading...
Searching...
No Matches
ciphers::elliptic_curve_key_exchange Namespace Reference

namespace elliptic_curve_key_exchange More...

## Classes

struct | Point
| Definition of struct Point. More...

## Typedefs

typedef struct ciphers::elliptic_curve_key_exchange::Point | Point
| Definition of struct Point. More...

## Functions

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 Exponentiation. More...
Point | addition (Point a, Point b, const uint256_t &curve_a_coeff, uint256_t mod)
| Addition of points. More...
Point | multiply (const Point &a, const uint256_t &curve_a_coeff, uint256_t p, const uint256_t &mod)
| multiply Point and integer More...

## Detailed Description

namespace elliptic_curve_key_exchange

Demonstration of Elliptic Curve Diffie-Hellman key exchange.

## Typedef Documentation

## Point

typedef struct ciphers::elliptic_curve_key_exchange::Point ciphers::elliptic_curve_key_exchange::Point

Definition of struct Point.

Definition of Point in the curve.

## Function Documentation

## addition()

Point ciphers::elliptic_curve_key_exchange::addition | ( | Point | a,
|  | Point | b,
|  | const uint256_t & | curve_a_coeff,
|  | uint256_t | mod
| ) |  |

Addition of points.

Add given point to generate third point. More description can be found here, and here

Parameters

  a | First point
  b | Second point
  curve_a_coeff | Coefficient `a` of the given curve (y^2 = x^3 + ax + b) % mod
  mod | Given field

Returns
  the resultant point

Slope

value zero

slope when the line is tangent to curve. This operation is performed while doubling. Taking derivative of `y^2 = x^3 + ax + b` => `2y dy = (3 * x^2 + a)dx` => `(dy/dx) = (3x^2 + a)/(2y)`

if y co-ordinate is zero, the slope is infinite, return inf. else calculate the slope (here % mod and store in lambda)

{
uint256_t lambda(0); /// Slope
uint256_t zero(0); /// value zero
lambda = zero = 0;
uint256_t inf = ~zero;
if (a.x != b.x || a.y != b.y) {
// Slope being infinite.
if (b.x == a.x) {
return {inf, inf};
}
uint256_t num = (b.y - a.y + mod), den = (b.x - a.x + mod);
lambda = (num * (exp(den, mod - 2, mod))) % mod;
} else {
/**
* slope when the line is tangent to curve. This operation is performed
* while doubling. Taking derivative of `y^2 = x^3 + ax + b`
* => `2y dy = (3 * x^2 + a)dx`
* => `(dy/dx) = (3x^2 + a)/(2y)`
*/
/**
* if y co-ordinate is zero, the slope is infinite, return inf.
* else calculate the slope (here % mod and store in lambda)
*/
if (!a.y) {
return {inf, inf};
}
uint256_t axsq = ((a.x * a.x)) % mod;
// Mulitply by 3 adjustment
axsq += (axsq << 1);
axsq %= mod;
// Mulitply by 2 adjustment
uint256_t a_2 = (a.y << 1);
lambda =
(((axsq + curve_a_coeff) % mod) * exp(a_2, mod - 2, mod)) % mod;
}
Point c;
// new point: x = ((lambda^2) - x1 - x2)
// y = (lambda * (x1 - x)) - y1
c.x = ((lambda * lambda) % mod + (mod << 1) - a.x - b.x) % mod;
c.y = (((lambda * (a.x + mod - c.x)) % mod) + mod - a.y) % mod;
return c;
}
uint256_t
class for 256-bit unsigned integer
Definition: uint256_t.hpp:33
std::exp
T exp(T... args)
Point
Definition: line_segment_intersection.cpp:12
Point::y
int y
Point respect to x coordinate.
Definition: line_segment_intersection.cpp:14
Here is the call graph for this function:

## exp()

uint256_t ciphers::elliptic_curve_key_exchange::exp | ( | uint256_t | number,
|  | uint256_t | power,
|  | const uint256_t & | mod
| ) |  |

This function calculates number raised to exponent power under modulo mod using Modular Exponentiation.

Parameters

  number | integer base
  power | unsigned integer exponent
  mod | integer modulo

Returns
  number raised to power modulo mod

{
if (!power) {
return uint256_t(1);
}
uint256_t ans(1);
number = number % mod;
while (power) {
if ((power & 1)) {
ans = (ans * number) % mod;
}
power >>= 1;
if (power) {
number = (number * number) % mod;
}
}
return ans;
}
power
void power(int x, int n)
Definition: power_for_huge_numbers.cpp:56
Here is the call graph for this function:

## multiply()

Point ciphers::elliptic_curve_key_exchange::multiply | ( | const Point & | a,
|  | const uint256_t & | curve_a_coeff,
|  | uint256_t | p,
|  | const uint256_t & | mod
| ) |  |

multiply Point and integer

Multiply Point by a scalar factor (here it is a private key p). The multiplication is called as double and add method

Parameters

  a | Point to multiply
  curve_a_coeff | Coefficient of given curve (y^2 = x^3 + ax + b) % mod
  p | The scalar value
  mod | Given field

Returns
  the resultant point

{
Point N = a;
N.x %= mod;
N.y %= mod;
uint256_t inf{};
inf = ~uint256_t(0);
Point Q = {inf, inf};
while (p) {
if ((p & 1)) {
if (Q.x == inf && Q.y == inf) {
Q.x = N.x;
Q.y = N.y;
} else {
Q = addition(Q, N, curve_a_coeff, mod);
}
}
p >>= 1;
if (p) {
N = addition(N, N, curve_a_coeff, mod);
}
}
return Q;
}
data_structures::sparse_table::N
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....
Definition: sparse_table.cpp:47
ciphers::elliptic_curve_key_exchange::addition
Point addition(Point a, Point b, const uint256_t &curve_a_coeff, uint256_t mod)
Addition of points.
Definition: elliptic_curve_key_exchange.cpp:110
ciphers::elliptic_curve_key_exchange::Point
Definition of struct Point.
Definition: elliptic_curve_key_exchange.cpp:46
Here is the call graph for this function: