111_008_ECDSA_v-20
Elliptic-curve cryptography (ECC) is an approach to public-key cryptography based on the algebraic structure of elliptic curves over finite fields. 
ECC requires smaller keys compared to non-ECC cryptography to provide equivalent security.
For example, to achieve the same security ensured by ECC having private key of 256 bit length,
it is required to use over 3000 bit private key length for RSA cryptosystem and others.
Elliptic curves are applicable for key agreement, digital signatures, pseudo-random generators and other tasks. 
Indirectly, they can be used for encryption by combining the key agreement with a symmetric encryption scheme. 
Elliptic Curve Digital Signature Algorithm - Bitcoin Wiki  (ECDSA)
[link] 10, 2015
Elliptic Curve Digital Signature Algorithm or ECDSA is a cryptographic algorithm used by Bitcoin, Ethereum and other blockchain methods to ensure that funds can only be spent by their owner.
[link]
Except division by 0
2 is a generator
21 mod 11
22 mod 11
g=7
Coordinate systems XOY in subsequent examples are defined in the plane of real numbers. 
P(xp, yp)= (2,4)
Q(xQ, yQ)= (4,1)
P+Q= (2+4, 4+1)
T=P+Q= (6,5)
T
P
                      ⊞
T= P(xp, yp) + Q(xQ, yQ)
T=(xp+ XQ, yP+yQ) = T(XT, YT)
XT=(xP + XQ)
YT=(YP+YQ)
T2=P+P= 2P
Q
T
T
XT=2+4=6
yT=2+4=6
|T|= 62+62
Q
Q
P
P
Special case
addition of two points stays on the same line
When P is moved to other direction,
 the resulting point will be outside the line
 a and b are numbers
Operations are performed in field Fₚ
 ⊞͐ means point addition
P⊞͐ Q ∈ EC  
the set points addition also belong to EC
T ⊞͐ (−T) = 0   denoted by ∞ 
  not part of EC        
T=(Tx, Ty)
-T
P
T=(Tx, Ty)
∞=  N/(X2-X1)
Elliptic curve ha a property that if line crosses two points, then there is a third crossing point in the curve.
Points in the plane or plane curve we denote by the capital letters, e.g. A, G, P, Q, etc.
Numbers-scalars we denote by the lowercase letters, e.g., a, g, x, y, z, etc. 
Addition of points P and Q in EC:  P ⊞͐Q = T
P(xP,yP) + Q(xQ,yQ) = T(xT,yT)
2Q
-2Q
Q+Q=2Q
5 − 5 mod 10 = 0                  
7 + 0 mod 10 = 7
When Z is large, Z ~ 2²⁵⁶ → |Z| = 256 bits:
Doubling of points allows effectively compute point
 A = ZG                          a = gˣ mod p
G+G=2G
T − T = 0 → T + (−T) = 0 ≡ ∞
T + ∞ = T
3
Y
8G = 23G
Point G, tangent line crosses EC and Symmetric point will be 2G
Adding points, we get 4G and symmetric point will be -4G
add again points we get 8G

ECDSA animacija
Signing and Verifying Ethereum Signatures – Yos Riady · Software Craftsman
[link]
For current cryptographic purposes, an elliptic curve is a plane curve over a finite field 
Fp={0, 1, 2, 3, …, p-1}, (rather than the real numbers) p-is prime.
Which consists of the points satisfying the equation over Fp 
                                                               y2=x3+ax+b  mod p
along with a distinguished point at infinity, denoted by 0 (∞).
Finite field is an algebraic structure, where 4 algebraic operations: +mod p, -mod p, ´mod p, :mod p 
are defined except the division by 0 excluded.
Elliptic Curve Group (ECG)
Number of points N of Elliptic Curve with coordinates (x, y) is an order of ECG. 
Addition operation ⊞ of points in ECG: let points P(xP,yP) and Q(xQ,yQ) are in EC with coordinates (xP,yP) and (xQ,yQ) then P ⊞ Q = T with coordinates (xT,yT) in EC.
Neutral element is group zero 0 at the infinity (∞) of [XOY] plane.
Multiplication of any EC point G by scalar z: T=z*G; T=G ⊞ G ⊞ G ⊞ …⊞ G; z–times.   
Generator–Base Point G: ECG={ i*G; i=1,2,…,N}; N*G=0 and q*G≠0 if q<N.
Set of N*G covers all EC points
Fp = Zp = {0, 1, 2, 3, …, p-1} 
When p = 17, then 
F17 = Z17 = {0, 1, 2, 3, …, 16}
G
2G
x
A= z * G
3Q
ECDEF:
ECMul:

𝑧∗𝐺=𝐺⊕𝐺⊕𝐺⊕⋯⊕𝐺 z times

ECDEF𝑥+𝑦∗𝐺=ECDEF𝑥∗𝐺⊕ECDEF𝑦∗𝐺
                       Z                                    P                              Q
=𝑃⊕𝑄=𝑻
DEF: gx mod p=a;
ECDEF : x*G = A = (XA,  yA)
z - is a random number of 256 bit length and is a private key PrK in Elliptic Curve Cryptography - ECC
A - is a point of Elliptic Curve (EC) and is a public key PuK in Elliptic Curve Cryptography - ECC
ECDEF(x*G) ⊞͐  ECDEF (y*G) = T
     P               ⊞͐            Q           = T
Elliptic Curve Cryptosystem - ECC 
ElGamal Cryptosystem (CS)
Elliptic Curve Cryptosystem (CS)
PP=(strongprime p, generator g);
p=255996887; g=22;
PP=(EC secp256k1; BasePoint-Generator G; prime p; param. a, b); 
Parameters a, b defines EC equation  y2=x3+ax+b mod p over Fp.
PrK=x;
>> x=randi(p-1). 
PrKECC=z; 
>> z=randi(p-1).
PuK=a=gx mod p.
PuKECC=A=z*G.
Alice A: x=1975596; a=210649132;
Alice A: z=…..; A=(xA, yA);
Let us consider abstract EC defined in XOY and expressed by the equation:
                                                         y2 = x3 + ax + b mod p.
EC points are computed by choosing coordinate x and computing coordinate y2.
To compute coordinate y it is needed to extract root square of y2.
                                                        y = ±√y2 mod p.
Notice that from y2 we obtain 2 points in EC, namely y and -y no matter computations are performed  with integers mod p or with real numbers.
Notice also that since EC is symmetric with respect to x-axis, the points y and -y are symmetric in EC. Since all arithmetic operations are computed mod p then according to the definition of negative points in Fp points y and -y must satisfy the condition 
                                                       y + (-y) = 0  mod p.
Then evidently
                                                       y2 = (-y)2 mod p.
For example:
-2 mod 11 = 9
22 mod 11 = 4  &  92 mod 11 = 4
>> mod(9^2,11)
ans = 4
-2 mod 11=   9 mod 11
(+2+(-2)) mod 11 =  (2+9) mod 11=  11 mod 11=0 
ECDSA: Signature creation for message M
Signature is formed on the h-value h of Hash function H of M. 
In Bitcoin H-function is SHA256 algorithm
In Ethereum H-function is keccak-256 algorithm it is an algorithm of the fakily of SHA-3 standard.
h = H(M)=SHA256(M);
i <-- randi; |i|≤ 256 bits; >> gcd(i,p)=1 --> Then there exists unique i-1 mod p such that ii-1 = 1 mod p.
R =   i*G = R(xR, yR);
r = xR mod p;  
s = (h + z • r) • i-1 mod p; |s|≤ 256 bits;  // Since i satisfies the condition that gcd(i,p)=1, then exists i-1 mod p.
                                                                      // >> i_m1=mulinv(i,p)      % in Octave
Sign(PrKECC=z, PP, h) = ϭ = (r, s) 
Signature vrification: Ver(PuK=A, ϭ, h)
Calculate u1 = h • s-1 mod p and u2 = r • s-1 mod p  
Calculate the curve point V = u1*G + u2*A=V(xV, yV)  
The signature is valid if  R=V; r=xV=xR mod p.
ECDSA
ElGamal Signature
h = H(m);
h = H(m);
Compute  i-1 mod p
i ßrandi; gcd(i, p-1)=1
Compute i-1 mod (p-1)
R = i*G = i*(xG, yG) = (xR, yR);
r = xR mod p;  |i|≤ 256 bits;
r=gi mod p;
s=(h+z•r)i-1 mod p; |s|≤ 256 bits;
s=(h-x•r)i-1 mod (p-1);
 s-1=(h+z•r)-1i mod p;
h=xr+is mod (p-1).
Sign(PrKECC=z, h) = (r, s) = ϭ; 
Sign(PrK=x, h) = (r, s) = ϭ; 
ECDSA Verification
ElGamal Signature Verification
Compute u1=h•s-1 mod p and
                u2=r•s-1 mod p;
Compute: u1= gh mod p;  
and           u2= arrs mod p
Compute R = u1*G + u2*A = (xR, yR);  
Signature is valid if: u1= u2
The signature is valid if r=xR mod p.
Schnorr Signature
h = H(m);
r=gi mod p;
s=(i+x•h) mod (p-1);
Sign(PrK=x, h) = (r, s) = ϭ; 
Schnorr Signature Verification
Compute: u1= gs mod p.
and           u2= rah mod p                
Signature is valid if: u1= u2
V, r,s     Ethereum Signature
ECDSA using secp256k1 software:
1.Install Python 3.9.1.
2.Launch script Packages for joining a libraries.
3.Launch file ECC.
4.If window is escaping, then open hidden windows
    in icon near the Start icon.
PrK:   has 256 bit length or 64 hexadecimal digits:
0x4f30dcb4235b9d52d0cd9aef9eb11b9c75b07ef25fc2249491b071f48803e5d3
PuK:   has 512 bit length or 128 hexadecimal digits:   
0x0beffa8fb3b2665089333456bc98b3c003181d7b7c23750c1dde4fa070608442638d7391846bda26aad95accba5810fc326611e804506f95e93ffa006d15b0f0
x
ECDSA Signature Computation Example
Step 1: Install Python
Go to [link]
Download the latest Python version 
Step 2: Install Visual Studio Code
Go to [link]
Download and install VS Code
Open VS Code after installation
Step 3: Install Required Python Libraries
Open Terminal in VS Code
Run the following command to install the libraries:
pip install ecdsa pycryptodome ecdsa eth-utils
These libraries are used to generate cryptographic keys, hash messages, sign data securely, and work with Ethereum-specific utilities.
Step 4: Write the Code
Create a new file in VS Code and save it with ".py" extension to make it a python file (e.g., [file]) and write the code below to test the file or python configurations

Save the file
Run it using:

python [file]
Below is the ECDSA Signature Computation Example in python 
 Keys Generation 
In this step, a private key is generated using the secp256k1 elliptic curve, which is widely used in blockchain systems such as Ethereum and Bitcoin. The public key is then derived from the private key. The private key is kept secret and is used to sign data, while the public key can be shared and is used to verify signatures.
Hash the Message
The Keccak hash function is imported from the Crypto.Hash module to generate the message hash.
Before signing, the message must be converted into a fixed-length format called a hash.
3.Sign the Hash Using secp256k1
The hashed message is signed using the private key and the secp256k1 elliptic curve.
This process generates a digital signature, which proves that the message was created by the owner of the private key and that the message has not been altered.
 Signature Verification
In this step, the generated signature is verified using the corresponding public key.
If the signature matches the original message hash, the verification is successful, confirming the authenticity and integrity of the message.
Alice and Bob Example