Cryptography Fundamentals Lab Assignment - 6: ECC and Digital Signature Verification
Cryptography Fundamentals Lab Assignment - 6: ECC and Digital Signature Verification
Cryptography Fundamentals Lab Assignment - 6: ECC and Digital Signature Verification
CRYPTOGRAPHY FUNDAMENTALS
LAB ASSIGNMENT - 6
Code:
typedef struct {
long a, b;
dlong N;
epnt G;
dlong r;
} curve;
typedef struct {
long a, b;
} pair;
curve e;
epnt zerO;
int inverr;
while (v) {
q = u / v;
17BCE0147 VIJAY SURYA
t = u - q * v;
u = v; v = t;
t = r - q * s;
r = s; s = t;
}
if (u != 1) {
printf (" impossible inverse mod N, gcd = %d\n", u);
inverr = 1;
}
return r;
}
if (p.x != q.x) {
t = p.y - q.y;
la = modn(t * exgcd(p.x - q.x, e.N));
}
else
if ((p.y == q.y) && (p.y != 0)) {
t = modn(3 * modn(p.x * p.x) + e.a);
la = modn(t * exgcd (2 * p.y, e.N));
}
else
{*r = zerO; return;}
// R:= multiple kP
void pmul (epnt *r, epnt p, long k)
{
epnt s = zerO, q = p;
for (; k; k >>= 1) {
if (k & 1) padd(&s, s, q);
if (inverr) {s = zerO; break;}
padd(&q, q, q);
}
*r = s;
}
if (isO (p))
printf ("%s (0)\n", f);
else {
if (y > e.N - y) y -= e.N;
printf ("%s (%lld, %lld)\n", f, p.x, y);
17BCE0147 VIJAY SURYA
}
}
e.a = modn(a);
e.b = modn(b);
e.G.x = modn(i[3]);
e.G.y = modn(i[4]);
e.r = i[5];
return 1;
}
double rnd(void)
{
return rand() / ((double)RAND_MAX + 1);
}
sg.a = c; sg.b = d;
return sg;
}
t = (disc() == 0);
t |= isO (e.G);
pmul (&W, e.G, e.r);
t |= ! isO (W);
t |= ! ison (e.G);
if (t) goto errmsg;
17BCE0147 VIJAY SURYA
printf ("\nkey generation\n");
s = 1 + (long)(rnd() * (e.r - 1));
pmul (&W, e.G, s);
printf ("private key s = %d\n", s);
pprint ("public key W = sG", W);
t = e.r;
for (i = 1; i < 32; i <<= 1)
t |= t >> i;
while (f > t) f >>= 1;
printf ("\naligned hash %x\n", f);
if (d > 0) {
while (d > t) d >>= 1;
f ^= d;
printf ("\ncorrupted hash %x\n", f);
}
return;
errmsg:
printf ("invalid parameter set\n");
printf ("_____________________\n");
}
f = 0x789abcde; d = 0;
else
break;
}
}
OUTPUT:
17BCE0147 VIJAY SURYA
ECC :
DUMP = FALSE
A = A + MOD ;
X1 = 1;
X2 = 0;
X3 = MOD ;
Y1 = 0;
Y2 = 1;
Y3 = A;
Q = INT(X3 / Y3)
T1 = X1 - Q*Y1
T2 = X2 - Q*Y2
T3 = X3 - (Q*Y3)
IF DUMP == TRUE:
PRINT("Q\TX1\TX2\TX3\TY1\TY2\TY3\TT1\TT2\TT3")
PRINT("----------------------------------------------------------------------------")
PRINT(Q,"\T",X1,"\T",X2,"\T",X3,"\T",Y1,"\T",Y2,"\T",Y3,"\T",T1,"\T",T2,"\T",T3)
WHILE(Y3 != 1):
X1 = Y1; X2 = Y2; X3 = Y3
Y1 = T1; Y2 = T2; Y3 = T3
IF DUMP == TRUE:
PRINT(Q,"\T",X1,"\T",X2,"\T",X3,"\T",Y1,"\T",Y2,"\T",Y3,"\T",T1,"\T",T2,"\T",T3)
PRINT("-----------------------------------------`-----------------------------------")
PRINT("")
Y2 = Y2 + MOD
RETURN Y2
ELSE:
#POINT ADDITION
X3 = X3 % MOD Y3 = Y3 % MOD
X3 = X3 + MOD
Y3 = Y3 + MOD
RETURN X3, Y3
X_TEMP = X0
Y_TEMP = Y0
IF CURRENTBIT == '1':
APPLYKEYEXCHANGE = TRUE
APPLYDIGITALSIGNATURE = TRUE
APPLYSYMMETRICENCRYPTION = TRUE
APPLYORDEROFGROUP = FALSE
APPLYECDLP = FALSE
NABLEBITCOINPARAMS = TRUE
IF ENABLEBITCOINPARAMS == TRUE:
MOD = POW(2, 256) - POW(2, 32) - POW(2, 9) - POW(2, 8) - POW(2, 7) - POW(2, 6) - POW(2, 4) - POW(2,
0)
ORDER =
115792089237316195423570985008687907852837564279074904382605163141518161494337
ELSE:
#CURVE CONFIGURATION
A=0
B=7
IF ENABLEBITCOINPARAMS == TRUE:
X0 = 55066263022277343669578718895168534326250603453777594175500187360389116729240 Y0
= 32670510020758816978083085130507043184471273380659243275938904335757337482424
ELSE:
X0 = 2
Y0 = 24
PRINT("---------------------")
PRINT("INITIAL CONFIGURATION")
PRINT("---------------------")
PRINT("CURVE: Y^2 = X^3 + ",A,"*X + ",B, " MOD ", MOD," , #F(",MOD,") = ", ORDER) PRINT("BASE
POINT: (",X0,", ",Y0,")")
IF APPLYBRUTEFORCE == TRUE:
NEW_X, NEW_Y = POINTADDITION(X0, Y0, X0, Y0, A, B, MOD) PRINT("2 P: (",NEW_X,", ",NEW_Y,")")
TRY:
EXCEPT:
BREAK
IF APPLYKEYEXCHANGE == TRUE:
ALICEPRIVATE = 2010000000000017
BOBPRIVATE = 2010000000000061
PRINT("")
#------------------------------------
#DIGITAL SIGNATURE
IF APPLYDIGITALSIGNATURE == TRUE:
PRIVATEKEY =
75263518707598184987916378021939673586055614731957507592904438851787542395619
PUBLICKEYX, PUBLICKEYY = APPLYDOUBLEANDADDMETHOD(X0, Y0, PRIVATEKEY, A, B, MOD)
RANDOMKEY =
28695618543805844332113829720373285210420739438570883203839696518176414791234 """IMPORT
RANDOM
RANDOMKEY = RANDOM.GETRANDBITS(128)"""
#SIGNING
R = RANDOMPOINTX % ORDER
S = HASH + (R * PRIVATEKEY)
#VERIFICATION PRINT("\NVERIFICATION...")
W = FINDMODULARINVERSE(S, ORDER)
IF(CHECKPOINTX == R):
PRINT("SIGNATURE IS VALID...")
ELSE:
#SYMMETRIC ENCRYPTION
IF APPLYSYMMETRICENCRYPTION == TRUE:
#1000P
PLAINTEXTX =
33614996735103061868086131503312627786077049888376966084542785773152043381677
PLAINTEXTY =
84557594361191031609962062080128931200952163654712344162477769532776951195137
SECRETKEY =
75263518707598184987916378021939673586055614731957507592904438851787542395619
PUBLICKEYX, PUBLICKEYY = APPLYDOUBLEANDADDMETHOD(X0, Y0, SECRETKEY, A, B, MOD)
#ENCRYPTION
RANDOMKEY =
28695618543805844332113829720373285210420739438570883203839696518176414791234 """IMPORT
RANDOM
RANDOMKEY = RANDOM.GETRANDBITS(128)"""
#DECRYPTION
#MESSAGE = C2 - SECRETKEY * C1
IF APPLYORDEROFGROUP == TRUE:
PRINT("\N------------------------------------------")
PRINT("------------------------------------------")
M = INT(SQRT(SQRT(MOD))) + 1
TERMINATE = FALSE
TRY:
EXCEPT:
ORDEROFGROUP = ORDEROFGROUP + J
TERMINATE = TRUE
BREAK
TRY:
EXCEPT:
ORDEROFGROUP = ORDEROFGROUP - J
TERMINATE = TRUE
BREAK
PRINT()
IF TERMINATE == TRUE:
BREAK
IF APPLYECDLP == TRUE:
PRINT("\N------------------------------------------")
PRINT("------------------------------------------")
K = 177
#------------------------
M = INT(SQRT(ORDER)) + 1
IF IP == CHECKPOINT:
TERMINATE = TRUE
BREAK
IF TERMINATE == TRUE:
BREAK
OUTPUT:
17BCE0147 VIJAY SURYA