Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
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

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
aFirst point
bSecond point
curve_a_coeffCoefficient a of the given curve (y^2 = x^3 + ax + b) % mod
modGiven 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)

111  {
112  uint256_t lambda(0); /// Slope
113  uint256_t zero(0); /// value zero
114  lambda = zero = 0;
115  uint256_t inf = ~zero;
116  if (a.x != b.x || a.y != b.y) {
117  // Slope being infinite.
118  if (b.x == a.x) {
119  return {inf, inf};
120  }
121  uint256_t num = (b.y - a.y + mod), den = (b.x - a.x + mod);
122  lambda = (num * (exp(den, mod - 2, mod))) % mod;
123  } else {
124  /**
125  * slope when the line is tangent to curve. This operation is performed
126  * while doubling. Taking derivative of `y^2 = x^3 + ax + b`
127  * => `2y dy = (3 * x^2 + a)dx`
128  * => `(dy/dx) = (3x^2 + a)/(2y)`
129  */
130  /**
131  * if y co-ordinate is zero, the slope is infinite, return inf.
132  * else calculate the slope (here % mod and store in lambda)
133  */
134  if (!a.y) {
135  return {inf, inf};
136  }
137  uint256_t axsq = ((a.x * a.x)) % mod;
138  // Mulitply by 3 adjustment
139  axsq += (axsq << 1);
140  axsq %= mod;
141  // Mulitply by 2 adjustment
142  uint256_t a_2 = (a.y << 1);
143  lambda =
144  (((axsq + curve_a_coeff) % mod) * exp(a_2, mod - 2, mod)) % mod;
145  }
146  Point c;
147  // new point: x = ((lambda^2) - x1 - x2)
148  // y = (lambda * (x1 - x)) - y1
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;
151  return c;
152 }
class for 256-bit unsigned integer
Definition: uint256_t.hpp:33
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...
Definition: elliptic_curve_key_exchange.cpp:78
Definition: line_segment_intersection.cpp:12
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
numberinteger base
powerunsigned integer exponent
modinteger modulo
Returns
number raised to power modulo mod
78  {
79  if (!power) {
80  return uint256_t(1);
81  }
82  uint256_t ans(1);
83  number = number % mod;
84  while (power) {
85  if ((power & 1)) {
86  ans = (ans * number) % mod;
87  }
88  power >>= 1;
89  if (power) {
90  number = (number * number) % mod;
91  }
92  }
93  return ans;
94 }
math::fibonacci_sum::matrix power(math::fibonacci_sum::matrix T, uint64_t ex)
Definition: fibonacci_sum.cpp:57
ll ans(ll n)
Definition: matrix_exponentiation.cpp:91
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
aPoint to multiply
curve_a_coeffCoefficient of given curve (y^2 = x^3 + ax + b) % mod
pThe scalar value
modGiven field
Returns
the resultant point
166  {
167  Point N = a;
168  N.x %= mod;
169  N.y %= mod;
170  uint256_t inf{};
171  inf = ~uint256_t(0);
172  Point Q = {inf, inf};
173  while (p) {
174  if ((p & 1)) {
175  if (Q.x == inf && Q.y == inf) {
176  Q.x = N.x;
177  Q.y = N.y;
178  } else {
179  Q = addition(Q, N, curve_a_coeff, mod);
180  }
181  }
182  p >>= 1;
183  if (p) {
184  N = addition(N, N, curve_a_coeff, mod);
185  }
186  }
187  return Q;
188 }
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
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
Here is the call graph for this function: