I'm trying to recreate the Diffie-Hellman Key Exchange using addition but i get errors - c

#include<stdio.h>
#include<math.h>
long long int add(long long int a, long long int b,
long long int G)
{
if (b == 1)
return a;
else
return (((long long int)(a + b)) % G);
}
int main()
{
long long int G, x, a, y, b, ka, kb;
G = 43; //the agreed number
printf("The value of G : %lld\n\n", G);
a = 23; //private key for a
printf("The private key a for A : %lld\n", a);
x = add(G, a); //gets the generated key
b = 19; //private key for b
printf("The private key b for B : %lld\n\n", b);
y = add(G, b); // gets the generated key
ka = add(y, a, G); // Secret key for a
kb = add(x, b, G); // Secret key for b
printf("Secret key for the A is : %lld\n", ka);
printf("Secret Key for the B is : %lld\n", kb);
return 0;
}
this is the flow of the code
THIS IS THE EXPECTED OUTPUT/FLOW OF THE PROGRAM but my code has problems i attached an image to show the problem.
A and B will agree upon a number
G = 43 is the agreed number
A will generate a private number a = 23
B will generate a private number b = 19
A will calculate G=43 + a=23 mod G=43 OR 43 + 23 mod 43 = 66 (let's call it x) x = 66
B will calculate G=43 + b=19 mod G=43 OR 43+19mod43 = 62 (let's call it y) y = 62
for A we get x = 66
for B we get y = 62
They will then exchange x and y
x = 66 will go to B
y = 62 will go to A
A will now calculate y + a mod G OR 62+23 mod 43 = 85 (secret number is ka) ka = 85
B will now calculate x + b mod G OR 66+19 mod 43 = 85 (secret number is kb) kb = 85
This is the error that I get

Your 'add' function is declared as taking 3 arguments
long long int add(long long int a, long long int b, long long int G)
but twice you try to call it only passing 2, here
x = add(G, a); //gets the generated key
and here
y = add(G, b); // gets the generated key
reading the logic you posted those should be
x = add(G, a, G); //gets the generated key
and
y = add(G, b, G); // gets the generated key

Related

RSA algorithm for generating decryption key in C

The formula for generating decryption key for RSA algorithm is ed = 1 mod T where T is generated using the formula (p-1)(q-1). p and q are two non identical prime number. e is the Encryption Key. So as per the formula if I like to implement the ed = 1 mod T in C program the code block should be
d = (1%T)/e;
However, I found most of the coding websites(coding website) use d*e = 1 + k * T to generate the decryption key.
I can not understand from where they get k?
The modular multiplicative inverse can be found with the extended Euclidean algorithm.
Here is a simple demonstration implementation. Cryptographic implementations generally needed extended-precision arithmetic.
#include <stdio.h>
#include <stdlib.h>
// Return the multiplicative inverse of e modulo t.
static int invert(int e, int t)
{
/* We will work with equations in the form:
x = d*e + s*t
where e and t are fixed, and x, d, and s change as we go along.
Initially, we know these two equations:
t = 0*e + 1*t.
e = 1*e + 0*t.
We will use the Euclidean algorithm to reduce the left side to the
greatest common divisor of t and e. If they are relatively prime,
this eventually produces an equation with 1 on the left side, giving
us:
1 = d*e + s*t
and then d is the multiplicative inverse of e since d*e is congruent to
1 modulo t.
*/
// Now we start with our first values of x, d, and s:
int x0 = t, d0 = 0, s0 = 1;
int x1 = e, d1 = 1, s1 = 0;
// Then we iteratively reduce the equations.
while (1)
{
/* Find the largest q such that we can subtract q*x1 from x0 without
getting a negative result.
*/
int q = x0/x1;
/* Subtract the equation x1 = d1*e + s1*t from the equation
x0 = d0*e + s0*t.
*/
int x2 = x0 - q*x1;
int d2 = d0 - q*d1;
int s2 = s0 - q*s1;
/* If we found the inverse, return it.
We could return d2 directly; it is mathematically correct.
However, if it is negative, the positive equivalent might be
preferred, so we return that.
*/
if (x2 == 1)
return d2 < 0 ? d2+t : d2;
if (x2 == 0)
{
fprintf(stderr, "Error, %d is not relatively prime to %d.\n", e, t);
exit(EXIT_FAILURE);
}
/* Slide equation 1 to equation 0 and equation 2 to equation 1 so we
can work on a new one.
*/
x0 = x1; x1 = x2;
d0 = d1; d1 = d2;
s0 = s1; s1 = s2;
}
}
int main(void)
{
int e = 3, t = 3127, d = invert(e, t);
printf("The multiplicative inverse of %d modulo %d is %d.\n", e, t, d);
}
Output:
The multiplicative inverse of 3 modulo 3127 is 2085.

How to change my output from 10.000 to 10

I've just started learning c++ and I saw a question where I need to find out the largest number among n entered numbers. I want to change the output from 10.0000 to 10, but I don't know how to do it? `
#include<stdio.h>
int main()
{
int n,i;
float c,big;
scanf("%d", &n);
printf("", n);
scanf("%f", &big);
for(i = 2; i <= n; i++)
{
printf("", i);
scanf("%f", &c);
if(big < c)
big = c;
}
printf("%f",n, big);
return 0;
}
Comments on top of each method explain it all. You need to pick the right method based on your requirement.
#include<stdio.h>
#include<math.h>
int main()
{
float a = 15.567125;
float b = 15.432345;
/* Method 01
When you want to consider 3 or 0 digits after integer part */
printf("M11: a = %0.3f & b = %0.3f\n",a, b);
printf("M12: a = %0.0f & b = %0.0f\n",a, b);
/* Method 02
When you want to ceil or floor the output */
printf("M21: a = %0.0f & b = %0.0f\n",ceil(a), ceil(b));
printf("M21: a = %0.0f & b = %0.0f\n",floor(a), floor(b));
/* Method 03
When you simply want to eliminate data after integer part */
int a1 = a;
int b1 = b;
printf("M31: a = %d & b = %d\n",a1, b1);
printf("M32: a = %d & b = %d\n",(int)a, (int)b);
return 0;
}
Here is the output:
M11: a = 15.567 & b = 15.432
M12: a = 16 & b = 15
M21: a = 16 & b = 16
M21: a = 15 & b = 15
M31: a = 15 & b = 15
M32: a = 15 & b = 15
Replace this line
printf("%f",n, big);
with
printf("%.0f", big);

Finding GCD using divison method in C

So I wanted to write a function to calculate the Gcd or HCF of two numbers using the Divison method.This is my code:
#include <stdio.h>
#include <stdlib.h>
void gcd(int x, int y)
{
int g,l;
if (x >= y)
{
g = x;
l = y;
}
else
{
g = y;
l = x;
}
while (g % l != 0)
{
g = l;
l = g % l;
}
printf("The GCD of %d and %d is %d", x, y, l);
}
int main(void)
{
gcd(8, 3);
return 0;
}
I am getting no output with this(error?): Process returned -1073741676 (0xC0000094)
Is there a problem with my loop?
In:
g = l;
l = g % l;
the assignment g = l loses the value of g before g % l is calculated. Change it to:
int t = g % l;
g = l;
l = t;
I use this loop while to find the gcd of two numbers like this:
void gcd(int x, int y)
{
int k=x,l=y;
if(x>0&&y>0)
{
while(x!=0&&y!=0)
{
if(x>y)
{
x=x-y;
}
else
{
y=y-x;
}
}
}
printf("\nThe GCD of %d and %d is %d", k, l, x);
}
int main(void)
{
gcd(758,306);
return 0;
}
Examples:
Input:
x=758 , y=306
x=27 , y=45
x=3 , y=8
Output:
printf("\nThe GCD of 758 and 306 is 2");
printf("\nThe GCD of 27 and 45 is 9");
printf("\nThe GCD of 3 and 8 is 1");
First of all, take into account that you are exchanging the numbers when x >= y which means that you try to put in x the smaller of the two. For GDC, there's no need to do
this, as the remainder of a division by a bigger number is always the original number, so if you have the sequence 3, 8, the first remainder will be 3, and the numbers switch positions automatically as part of the algorithm. So there's no need to operate with g (I guess for greater) and l (for lesser) so you can avoid that.
#include <stdio.h>
#include <stdlib.h>
void gcd(int x, int y)
{
int g,l;
if (x >= y)
{
g = x;
l = y;
}
else
{
g = y;
l = x;
}
Then, in this second part (the loop part) you have to take into account that you are calculating g % l twice in the same loop run (this is not the Euclides' algorithm).
while (g % l != 0)
{
g = l;
l = g % l;
}
You should better use a new variable r (for remainder, but I should recommend you to use longer, descriptive names) so you have always an idea of what the variable holds.
int r;
while ((r = g % l) != 0) {
g = l;
l = r;
}
you see? I just do one division per loop, but you make a l = g % l; which modifies the value of l, making you go through two iterations of the loop in one.
The final program is:
#include <stdio.h>
#include <stdlib.h>
int gcd(int greater, int lower)
{
int remainder;
while ((remainder = greater % lower) != 0) {
printf("g=%d, l=%d, r=%d\n", greater, lower, remainder);
greater = lower;
lower = remainder;
}
return lower; /* remember that remainder got 0 in the last loop */
}
int main(void)
{
int x = 6, y = 8;
printf("The GCD of %d and %d is %d\n",
x, y, gcd(x, y));
printf("The GCD of %d and %d is %d\n",
y, x, gcd(y, x));
return EXIT_SUCCESS;
}
(I have added a trace printf in the gcd() loop to show how the variables are changing, and both calculations ---changing the parameter values--- to show what I said above about the automatic change in order. Also, it's better to use the gcd() as a function that returns the value, and let main() decide if it wants to print results, or use the value for something else.
Enjoy it!! :)

How to calculate inverse modular exponentation in c?

I want to take modular inverse(k≥1) of integer and then multiply the result to another integer, as explain in following expression:
result=((x^(-k)))*y mod z
How can i implement this expression, where k≥1?
You need to define four function:
uint64_t modular_exponentiation(uint64_t x, uint64_t y, uint64_t z)
{
uint64_t res = 1;
x = x % z;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1; // y = y/2
x = (x*x) % z;
}
return res;
}
uint64_t moduloMultiplication(uint64_t a, uint64_t b,uint64_t z)
{
uint64_t res = 0;
a %= z;
while (b)
{
if (b & 1)
res = (res + a) % z;
a = (2 * a) % p;
b >>= 1; // b = b / 2
}
return res;
}
void extendedEuclid(uint64_t A, uint64_t B)
{
uint64_t temp;
if(B == 0)
{
d = A;
x = 1;
y = 0;
}
else
{
extendedEuclid(B,A%B);
temp = x;
x = y;
y = temp - (A/B)*y;
}
}
int modInverse(uint64_t A, uint64_t M)
{
extendedEuclid(A,M);
if (x < 0)
x += M;
return (x);
}
In main():
uint64_t result=0x00;
result=modular_exponentiation(x,k,z); // (x^k) mod z
result=modInverse(result,z); // ((x^k)^-1) mod z == x^(-k) mod z
result=moduloMultiplication(result,y,z);// x^(-k) * y mod z
You will need the extended greatest common divisor to compute the inverse of x for modulus z. When x and zare relatively prime you have a * x + b * z = 1 = gcd(x, z). And thus, a * x = 1 - b * z or a * x = 1 mod z, and a is the inverse to x in the modulus z.
Now you may compute result with x^-1 = a mod z:
result = power(a, k) * y % z
with ordinary integer arithmetic in C, where power() is the ordinary integer exponentiation.
Since the coefficients in such calculations can become very large very quickly, it is better to use ready-made libraries (e.g. gmp).
You can try the mod_inv C function :
// return a modular multiplicative inverse of n with respect to the modulus.
// return 0 if the linear congruence has no solutions.
unsigned mod_inv(unsigned ra, unsigned rb) {
unsigned rc, sa = 1, sb = 0, sc, i = 0;
if (rb > 1) do {
rc = ra % rb;
sc = sa - (ra / rb) * sb;
sa = sb, sb = sc;
ra = rb, rb = rc;
} while (++i, rc);
sa *= (i *= ra == 1) != 0;
sa += (i & 1) * sb;
return sa;
}
This is basically the standard algorithm, when n = 1 and mod = 0 the output is 0, not 1, i think we have not many computations to execute modulo 0.
The modular multiplicative inverse of an integer N modulo m is an integer n such as the inverse of N modulo m equals n, if a modular inverse exists then it is unique. To calculate the value of the modulo inverse, use the extended euclidean algorithm which finds solutions to the Bezout identity.
Example of usage :
#include <assert.h>
int main(void) {
unsigned n, mod, res;
n = 52, mod = 107;
res = mod_inv(n, mod);
assert(res == 35); // 35 is a solution of the linear congruence.
n = 66, mod = 123;
res = mod_inv(n, mod);
assert(res == 0); // 66 does note have an inverse modulo 123.
}
/*
n = 7 and mod = 45 then res = 13 so 1 == ( 13 * 7 ) % 45
n = 52 and mod = 107 then res = 35 so 1 == ( 35 * 52 ) % 107
n = 213 and mod = 155 then res = 147 so 1 == ( 147 * 213 ) % 155
n = 392 and mod = 45 then res = 38 so 1 == ( 38 * 392 ) % 45
n = 687 and mod = 662 then res = 53 so 1 == ( 53 * 687 ) % 662
n = 451 and mod = 799 then res = 512 so 1 == ( 512 * 451 ) % 799
n = 1630 and mod = 259 then res = 167 so 1 == ( 167 * 1630 ) % 259
n = 4277 and mod = 4722 then res = 191 so 1 == ( 191 * 4277 ) % 4722
*/
Source

Modular multiplicative inverse

I calculating ((A^B)/C)%M, but my code is not working when A,B,C,M are large in numbers. This code is giving right answer when A,B,C,D is small int.
What is wrong here?
Here C and M is co-prime
Sample input
2 3 4 5
Sample output
2
Code fails for these input
969109092 60139073 122541116 75884463
C program
#include <stdio.h>
int d,x,y;
Modular exponential (A^B)%M
int power(int A, int B, int M)
{
long long int result=1;
while(B>0)
{
if(B % 2 ==1)
{
result=(result * A)%M;
}
A=(A*A)%M;
B=B/2;
}
return result;
}
Modular multiplicative inverse
void extendedEuclid(int A, int B)
{
if(B == 0)
{
d = A;
x = 1;
y = 0;
}
else
{
extendedEuclid(B,A%B);
int temp = x;
x = y;
y = temp - (A/B)*y;
}
}
int modInv(int A, int M)
{
extendedEuclid(A,M);
return (x%M+M)%M;
}
main()
int main()
{
int A,B,C,M;
scanf("%d %d %d %d",&A,&B,&C,&M);
int inv = modInv(C,M)%M;
printf("%d\n",inv);
long long int p = (power(A,B,M))%M;
printf("%d\n",p);
long long int ans = (p * inv)%M;
//printf("%d",((modInv(C,M)*(power(A,B,M))))%M);
printf("%lld",ans);
return 0;
}
Code has at least the following issues:
int overflow in A*A. Code needs to calculate the product A*A using wider math. That is why code works with small values, but not large.
// A=(A*A)%M;
A = ((long long)A*A) % M;
// or
A = (1LL*A*A) % M;
Wrong print specifier. This implies compiler warnings are not fully enabled. Save time, Enable them all.
long long int p = (power(A,B,M))%M;
// printf("%d\n",p);
printf("%lld\n",p);
Code is amiss with negative values. Rather than patch that int hole, use unsigned types.
unsigned power(unsigned A, unsigned B, unsigned M) {
unsigned long long result = 1;
...
Failed corner case in power(A,0,1). result should be 0 when M==1.
// long long int result=1;
long long int result=1%M;
Test version with fixes noted in comments:
#include <stdio.h>
int d,x,y;
int power(int A, int B, int M)
{
long long int result=1;
long long int S = A; /* fix */
while(B>0)
{
if(B % 2 ==1)
{
result=(result * S)%M; /* fix */
}
S=(S*S)%M; /* fix */
B=B/2;
}
return (int)result;
}
void extendedEuclid(int A, int B)
{
int temp; /* C */
if(B == 0)
{
d = A;
x = 1;
y = 0;
}
else
{
extendedEuclid(B,A%B);
temp = x;
x = y;
y = temp - (A/B)*y;
}
}
int modInv(int A, int M)
{
extendedEuclid(A,M);
/* x = x%M; ** not needed */
if (x < 0) /* fix */
x += M; /* fix */
return (x); /* fix */
}
int main()
{
int A,B,C,M; /* C */
int inv, p, ans; /* C */
A = 969109092; /* 2^2 × 3^2 ×7 × 1249 × 3079 */
B = 60139073; /* 60139073 */
C = 122541116; /* 2^2 × 1621 × 18899 */
M = 75884463; /* 3^2 × 8431607 */
inv = modInv(C,M)%M; /* 15543920 */
printf("%d\n",inv);
p = power(A,B,M)%M; /* 6704397 */
printf("%d\n",p);
ans = (unsigned)(((unsigned long long)p * inv)%M); /* fix 22271562 */
printf("%d\n",ans);
return 0;
}
The value of int is probably not large enough, try with long, or double.
Be careful because power returns an int not long long int
You can try the mod_inv C function :
// return a modular multiplicative inverse of n with respect to the modulus.
// return 0 if the linear congruence has no solutions.
unsigned mod_inv(unsigned ra, unsigned rb) {
unsigned rc, sa = 1, sb = 0, sc, i = 0;
if (rb > 1) do {
rc = ra % rb;
sc = sa - (ra / rb) * sb;
sa = sb, sb = sc;
ra = rb, rb = rc;
} while (++i, rc);
sa *= (i *= ra == 1) != 0;
sa += (i & 1) * sb;
return sa;
}
This is basically the standard algorithm, to avoid overflows the signs are stored into the d variable, you could use a struct to do this. Also, when n = 1 and mod = 0 the output is 0, not 1, i think we have not many computations to execute modulo 0.
The modular multiplicative inverse of an integer N modulo m is an integer n such as the inverse of N modulo m equals n, if a modular inverse exists then it is unique. To calculate the value of the modulo inverse, use the extended euclidean algorithm which finds solutions to the Bezout identity.
Example of usage :
#include <assert.h>
int main(void) {
unsigned n, mod, res;
n = 52, mod = 107;
res = mod_inv(n, mod);
assert(res == 35); // 35 is a solution of the linear congruence.
n = 66, mod = 123;
res = mod_inv(n, mod);
assert(res == 0); // 66 does note have an inverse modulo 123.
}
/*
n = 7 and mod = 45 then res = 13 so 1 == ( 13 * 7 ) % 45
n = 52 and mod = 107 then res = 35 so 1 == ( 35 * 52 ) % 107
n = 213 and mod = 155 then res = 147 so 1 == ( 147 * 213 ) % 155
n = 392 and mod = 45 then res = 38 so 1 == ( 38 * 392 ) % 45
n = 687 and mod = 662 then res = 53 so 1 == ( 53 * 687 ) % 662
n = 451 and mod = 799 then res = 512 so 1 == ( 512 * 451 ) % 799
n = 1630 and mod = 259 then res = 167 so 1 == ( 167 * 1630 ) % 259
n = 4277 and mod = 4722 then res = 191 so 1 == ( 191 * 4277 ) % 4722
*/
Source

Resources