this is a prebuilt version of the
application so if we run the server and
two clients
and this will be the server and this is the 
first client and this one the second one
and for the purpose of this tutorial
suppose that the first user that logs in
enters the domain parameters so let's have
Alice pick this number as her private key
and Bob picking this number and they end up
generating the same shared key or point
on the curve
so we are using an elliptic curve with
those two values for the curve
coefficients a and b and this value for
the prime number p and this one for n
the number of points in the cyclic
subgroup and this value for h the
cofactor and this is the generator point
that would generate the cyclic subgroup
and Alice picks this secret key and
uses it to calculate her public key so
we have three hops from the generator
point so 3 times the generator point and
we end up at the public key point and Bob
picks this number as his secret key so
we have 10 hops from the generator point
and we end up at the public key point
uppercase B now Alice calculates her
shared key and Bob calculates his
shared key they end up being the same
and the way Alice does that is she
multiplies her secret key by the
public key of Bob and Bob multiplies
his secret key by the public key of
Alice so here we have 3 hops from this
point
and we end up at
this point and here we have ten hops
from this point and we end up at this
point
now Passive Eve can see all the
communication happening between both
parties so now she knows the elliptic curve
that is being used and the generator point
and she knows Alice's public key and
Bob's public key so she have a starting
point meaning the generator point and an ending
point or Alice's public key and she
needs to obtain Alice's secret key
meaning the number of hops lowercase a
and use it to calculate the common key
meaning the shared final end point and
this is the elliptic curve discrete
logarithm problem the elliptical curve analog
of computing a discrete logarithm or she
could have tried to obtain Bob's
secret key and use it to calculate the
common key now before proceeding with
the code let's go over a few items
including the elliptic curve point
addition and point doubling and a proof
of the elliptic curve diffie-hellman key
exchange so to do cryptography we use a
curve over a finite field for example
prime field where all arithmetic is
performed in mode prime number p and the elliptic curve over Zp where p is
bigger than 3 is the set of all pair x
and y in Zp such that y squared is
congruent to x cube plus ax plus b in mod p and a and b are in Zp
and O is an imaginary point at
infinity and this equation is different
than zero in mod p so for point
addition this is the slope s which is y2
minus y1 over x2 minus x1 in mod p and
this is the x-coordinate so s squared
minus x1 minus x2 mod p and this is the
y-coordinate s times x1 minus xr minus
y1 in pod p and s is the slope of the
line through P and Q so P has
coordinates x1 y1 and Q has coordinates
x2 y2 and for point doubling
those are the equations for the x and y
coordinates so same as here except
x1 and x2 are the same so we end up with
minus 2 x1 here and for the slope we end
up with 3 x1 squared plus a over 2 y1 in
mod p and s is the slope of the tangent
line through B with coordinates x1
y1 now elliptic curve cryptography
provides the same level of security as
RSA or discrete logarithm based systems
but with much shorter operands and ECC
is based on the generalized discrete
logarithm problem meaning we can use
elliptic curve with the diffie-hellman
key exchange and the proof for elliptic
curve diffie-hellman is that Alice will
compute her shared key by doing
lowercase a multiplied by uppercase B
and lowercase a is Alice's secret key
and uppercase B is Bob's public key and
Bob will compute his shared key by
doing lowercase b multiplied by
uppercase a and lowercase b is Bob's
secret key and uppercase A as Alice's
public key and using the associativity
of point addition those two end up being
the same now the geometric
interpretation of elliptical curve discrete
logarithm problem and this is over real
numbers is that given a starting point
G the generator point and an ending
point T is a public key key we need to
find the private key d which is the number
of hops on the elliptic curve to get
from point G to T so we need to do G
Plus G Plus G d times so d times G
will equal to T I will start by creating
a new project
and here we will have two packages
one for the server side and another one
for the client side and on the server
side we will have two classes a
server class with a main method and a
server thread class
which will extend from thread and on the
client side we will have four classes a
client class with a main method and
a client thread class extending
from thread and this class will
represent an elliptic curve and this one
a point on that curve
and we will start with the server-side and
this class keeps track of the server and
a buffered reader and a print writer
and it initializes those in the
constructor which takes in a socket and
a server instance and it uses the socket
to initialize the bufferedreader and the
print  writer and it uses the
printwriter to do message forwarding and
the server will be listening on this
port and it will have a server socket
and a set of server threads for the
various clients it is interacting with
and for display purposes we will keep
track of the generator point for the
cyclic group and those are get and set
methods and here we instantiate a
server and a server socket given the
port number and indicate that Passive
Eve is listening on all communications
and while the server is up
we accept incoming client connections
and dedicate a server thread for that
and add
that server thread to the set of server
threads and call the start method on it
which ends up triggering a call to the
run method on the server thread when the
operating system decides to do so and
this method uses the forward message
method on the server thread to do
message forwarding from Alice to Bob or
the other way around and we need to do
JSON on this project so let's copy this
jar that we used it before and go to
properties
and Java build path
and this will be the code that we will
have in the run method here so the clients
are using JSON to communicate
and here we are displaying the
domain parameters so the generator x and
y coordinates and the elliptic curve a b
and the prime number p and the
number of points in the cyclic group
n and the cofactor h and here we are
displaying the public keys for Alice
and Bob and Passive Eve is indicating
that she have the starting point which
is the generator point and ending point
which is the public key for either Alice
or Bob and she needs to obtain the
private keys for Alice or for Bob which
is the number of hops from the generator
point to the ending point and if she
obtains the private key for either Alice
or Bob she can use that to calculate the
common key which is the shared final end
point and this is the elliptic curve
discrete logarithm problem which is the
elliptic curve analog of computing a
discrete logarithm and here we are just
forwarding the message from Alice to Bob
or the other way around
so this should do it for the server side
and here we will keep track of the
elliptic curve that we are using and
the generator point and the private key for
this user and the other party public key
and the other party public key name and
this flag indicates if we are
running in hex mode or in decimal mode
so if the user enters numbers in base 16
or in base 10 and we will have those get
and set methods and this is common key
flag method and those are the x and y
coordinates for a point on the curve
and we initialize those in the
constructor and we have 2 get methods
and for convenience we will define
those static values
and an elliptic curve will keep track of
its a and b coefficients and prime
number p and number of points in the
cyclic group and the cofactor h and
the order of the elliptic curve and the
curve that we will be using will have
the h is equal to 1 so the order of
that curve will be equal to the number
of points in the cyclic subgroup and so
here we're picking up the order h
multiply n so we initialize those
values in the constructor for a a b pn and
h and we will have get methods and the toString method will return the
equation of the elliptic curve so y
squared is congruent to x cube plus a
the value of a times x plus the value of
b in mod p
and the client thread will have a
bufferedreader instance variable and a
client instance variable and they are
initialized in the constructor which
takes in a socket and a client instance
and it uses the socket to initialize
the buffered reader instance and if we are
running in hex mode then we display that
to the user and here we instantiate a
client and a socket so the server will
be running for the purpose of this
tutorial it will be on localhost and
it will be listening on this port and we
instantiate a client thread and
call the start method on it which will
trigger a call to the run method when
the operating system decides to do so
and this method does the pickup and
processing of user input and we will
call it from here depending on hex flag so
the input will be in base 16 or in base
10
so here we need to do three things we
need to initialize the domain parameters
and pick up the private key from the
user and use it to calculate the public
key and the last thing we need to do
is calculate the common key so this is
the code that holds all three methods
init domain parameters and calculate
public key and calculate common
key and let's go here and define
infinity to be this huge number so point
at infinity will have infinity as its
y-coordinate and now we can have the
equals method and 2 toString methods
so this one is for decimal
values of the x and y coordinates and
this one for hexadecimal or decimal or
any base and the equals method will
indicate if this point and the passed in
point are the same
and to do point addition and
point doubling will have those two
methods and this method checks to see if
two points are inverse of each other
so those are the equations for point
addition so this is the slope y2 minus
y1 over x2 minus x1 in mode p and this
is a value for the x-coordinate and this
one for the y-coordinate s squared minus
x1 minus x2 and s times x1 minus xr
this one minus y1 in mode p and we
are implementing those here and here we
are checking if those two points are
inverses of each other and in that case
adding them will give us O the point
at infinity and those are the equations
for doubling a point so this is the
slope equation and this is the x and
y-coordinates and we are implementing
that here so this gives us the slope and
this is the x-coordinate and this
one is the y-coordinate and for a point
to be on the curve its x and y
coordinates have to satisfy this
equation y squared is congruent to x cube
plus a x plus b in mod p so this method
checks to see if a point is on the curve
so we have y squared in mod p we check if
it's equal to x cube plus a x plus b
in mod p and for a curve to be
non-singular it has to satisfy this
equation and this method implements that
so it checks to see if a curve with
those two coefficients a and b is
non-singular
so 4 multiply a cube and we add 27 b
squared and if that equals to zero or not
and this method implements the double
and add algorithm so we want to add
point G to itself d times and we start
by turning d into binary and then we go
through the digits of that binary string
so we pick up each bit and we do a
double and if the bit is 1 then we also
do an add and we return point T which
contains the result and this should do
it for this class and this method
picks up the values for a b p n and h and
uses the two coefficients a and b to
make sure that the elliptic curve is
non-singular and in that case we
instantiate a new elliptic curve and
return it and this method initializes
the domain parameters
so we first check if the elliptic curve
has not been specified yet and in that
case we check if the other user is
entering the curve or not and if he is
not then we send a hold message that we
are in the process of entering the curve
and we call the handle elliptic curve
input to pick up the curve and we have a
while always true loop that we will pick
up the generator point inside and
finally we put everything in a JSON
object so a a b p n and h and the
coordinates for the generator point and
we send it and this method calculates
the public key
so we pick up the private key for this
user and we use that to calculate the
public key and send it in a JSON object
and this method calculates the common
key so we use this user private key and
we multiply it with the other party
public key and we end up with that shared
key
and this should do it for this class
and this is the implementation of the run
method
so here we handle a hold message by
displaying this to the user and here
we handle a message containing the
domain parameter so a b p n and h and
the generator point x and y-coordinates
and here we handle a message containing
the public key x and y-coordinates so we
do set other party public key name and
set other party public key and here we handle being ready to generate
the common key so this should do
it for this class
and for convenience I have the test data used in
this tutorial here so this is for hex
input the Bitcoin curve and this is for
decimal input and finally let's go ahead
and test run the application so we'll
have a server and two clients
and this will be the server and this is
the first client and this one the second
one and let's pick up the data from here
and the generator point in hex
and maybe let's have Alice pick this number
as her secret key and have Bob pick this
number as his secret key
so we end up with this shared key
with this x coordinate and this y
coordinate
and they can use either coordinate to
derive a session key and Passive Eve
can see all the communications so she
knows the elliptic curve that is being used
and here we are displaying in decimal
and she knows the generator point
and Alice's public key uppercase A and
Bob's public key uppercase B and she can
do one of two things either figure out
lowercase a or lowercase b so
Alice's secret key or Bob's secret key
so she have a starting point meaning the
generator point G and an ending
point or Alice's public key so she
has lowercase a multiplied by the
generator point
which give us the public key and she
need to obtain Alice's secret key or the
number of hops lowercase a and use it
to calculate the common key or the
shared final end point and this is the
elliptic curve discrete logarithm
problem or the elliptic curve analog of
computing a discrete logarithm and
same thing here if she obtained b
she will use it to calculate the common
key or shared final endpoint
