Point operations on an elliptic curve in a prime field in c - c

I am trying to write a program to perform point operations on a elliptic curve in a prime field I am using the standard formulaes for point additions and doubling in my code and these operations are performed by functions that are called but I am getting output for certain points but not all so please help me to fix the problem that are present in this code.
structure point_multiply(int x, int y, int k )
{
int xk;
int yk,m;
xk=x;
yk=y;
m=1;
int xL,yL,s,e;
e=findInverse((2*yk),211);
if((((3*(xk*xk))*e)% 211)>0)
{s = (((3*(xk*xk))*e)% 211);
}
else
s=(((3*(xk*xk))*e)% 211)+ 211;
if((((s*s)- (2*xk)) % 211)>0)
{xL=(((s*s)- (2*xk)) % 211);
}
else
xL=(((s*s)- (2*xk)) % 211) + 211;
if(((-yk+ s*(xk-xL)) % 211) > 0)
yL=(-yk+ s*(xk-xL)) % 211;
else
yL=(-yk+ s*(xk-xL)) % 211 + 211;
xk=xL;
yk=yL;
m=m+1;
while(k>m)
{
sn=point_addition(xk,yk,x,y);
xk=sn.a;
yk=sn.b;
m++;
}
s1.a=xk;
s1.b=yk;
return s1;
}
structure point_addition(int x1, int y1, int x2, int y2)
{
int s,xL,yL;
if((x1-x2)!=0)
{
if ( x1 == 0 && y1 == 0 )
{
xL = x2;
yL = y2;
s7.a=xL;
s7.b=yL;
return s7;
}
if ( x2 == 0 && y2 == 0 )
{
xL = x1;
yL = y1;
s7.a=xL;
s7.b=yL;
return s7;
}
if ( y1 == -y2 )
{
xL = yL = 0;
s7.a=xL;
s7.b=yL;
return s7;
}
l=findInverse((x1-x2),211);
if ((((y1-y2)*l) % 211)>=0)
s=((((y1-y2)*l) % 211));
else
s=(((y1-y2)*l) % 211) + 211;
if ((((s*s)-(x1+x2)) % 211)>0)
xL= (((s*s)-(x1+x2)) % 211) ;
else
xL= (((s*s)-(x1+x2)) % 211) + 211;
if(((-y1+s*(x1-xL)))>=0)
yL= ((-y1+s*(x1-xL)) % 211);
else
yL= ((-y1+s*(x1-xL)) % 211) + 211;
}
else
{
xL= 0 ;
yL= 0;
}
s7.a= xL;
s7.b= yL;
return s7 ;
}
int findInverse(int a, int b)
{
int x[3];
int y[3];
int quotient = a / b;
int remainder = a % b;
x[0] = 0;
y[0] = 1;
x[1] = 1;
y[1] = quotient * -1;
int i = 2;
for (; (b % (a%b)) != 0; i++)
{
a = b;
b = remainder;
quotient = a / b;
remainder = a % b;
x[i % 3] = (quotient * -1 * x[(i - 1) % 3]) + x[(i - 2) % 3];
y[i % 3] = (quotient * -1 * y[(i - 1) % 3]) + y[(i - 2) % 3];
}
//x[i — 1 % 3] is inverse of a
//y[i — 1 % 3] is inverse of b
if(x[(i - 1) % 3]<0)
return x[(i - 1) % 3]+211;
else
//x[i — 1 % 3] is inverse of a
//y[i — 1 % 3] is inverse of b
return x[(i - 1) % 3];
}
Edited and added main c code which uses these function to perform elliptic curve cryptography
int main()
{
int y,z=0,x=2,i[200],j[200],h=0,g,k;
while(x<200)
{
y=sqrt((x*x*x)-4);
z=modulo(y,211);
if(z!=0)
{
i[h]=x;
j[h]=z;
s[h].a=i[h];
s[h].b=j[h];
s[h+1].a=i[h];
s[h+1].b=(211 - j[h]);
printf("\nh=%d X= %d Y= %d \nh=%d X= %d Y= %d",h,s[h].a,s[h].b,h+1,s[h+1].a,s[h+1].b);
h=h+2;
}
x++;
}
printf("The total no of points we have on our elliptic curve for cryptography is %d",h-1);
x=5;
y=11;
printf("\n %d %d\n",x,y );
printf("\nEnter A number between 0 and the private key");
scanf("%d",&k);
s2=point_multiply(x,y,k);
printf("\n The public key is \n %d %d \n ",s2.a,s2.b );
printf("Enter a RANDOM number to generate the cipher texts");
scanf("\n%d",&g);
s3= point_multiply(x,y,g);
s4=point_multiply(s2.a,s2.b,g );
label:
printf("\n Enter a number to send");
scanf("%d",&h);
s6=point_addition(s4.a,s4.b,s[h].a,s[h].b);
printf("The points to be sent are X= %d Y=%d",s[h].a,s[h].b);
printf(" \n X= %d Y=%d\n X = %d Y= %d ",s3.a,s3.b,s6.a,s6.b);
//RECIEVER
s8=point_multiply(s3.a,s3.b,k);
s9=point_addition((s8.a) ,-((s8.b)%211),s6.a,s6.b);
printf(" The decrypted points are \n %d %d",s9.a,s9.b);
printf("\n If you have more no to send press 1 else press 0");
scanf("\n %d", &x1);
if(x1==1)
goto label;
else
return 0;
}
s1, s2, s3 etc are structures which hold a 2 integers which act as x and y co-ordinates
I am getting output by entering k=3,g=4, h=5 and many other cases mostly with small numbers but not for larger numbers. What could be wrong with the code?
Further edit: I guess that normal square root method is not applicable to find square roots of a modular no?.. Please tell me how to find the modular square root of a no?

Related

This is about complex three number

Thats one wrong with my code and I dont have any idea about this wrong
Please be attention to that I can just use from for and while and if
The question is:
Write a code that gets the natural number n then tries to find x,y,z (natural numbers) in some way:
n=x+y+z
Then if the following is true of x, y, z, print these three numbers in the output, otherwise print the Not Found statement:
x = y ^ 2 + z ^ 2
(x or y or z) = i + (i + 1) + (i + 2)
Where i is a natural number.
Be it. Then if the following is true of x, y, z, print these three numbers in the output, otherwise print the Not Found statement:
x = y ^ 2 + z ^ 2
(x or y or z) = i + (i + 1) + (i + 2)
Where i is a natural number.
(Note that the input n is such that the int variable is sufficient and does not overflow.)
Input
The input contains a line in which a natural number is given.
Output
The output must either consist of three lines, each integer x, y, and z, respectively, from small to large, or the expression Not Found.
Example
Sample Input 1
48
Copy
Sample output 1
2 6 40
Copy
Sample input 2
5
Copy
Sample output 2
Not found
#include <stdio.h>
int main() {
int z,x,y,n;
scanf("%u",&n);
for(y=1;y<(n/3);y++) {
for(z=y;z<=((2*n)/3);z++) {
(x=(n-(y+z)));
if(x==((y*y)+(z*z))) {
if(((((y-3)%3)!=0)||(y==3))&&((((z-3)%3)!=0)||(z==3))&&((((x-3)%3)!=0)||(x==3))) {
continue;
}
printf("%d\n",y);
printf("%d\n",z);
printf("%d",x); return 0;
}
}
}
printf("Not found");
return 0;
}
This syntax (if(x==((yy)+(zz))) is wrong. It should be if (x == ((y*y)+(z*z))) or use pow function from math.h library like this (if(x == (pow(y,2)+pow(z,2)))
int main()
{
int z, x, y, n;
scanf("%u", &n);
for (y = 1; y < (n / 3); y++)
{
for (z = y; z <= ((2 * n) / 3); z++)
{
(x = (n - (y + z)));
if (x == ((y*y)+(z*z)))
{
if (((((y - 3) % 3) != 0) || (y == 3)) && ((((z - 3) % 3) != 0) || (z == 3)) && ((((x - 3) % 3) != 0) || (x == 3)))
{
continue;
}
printf("%d\n", y);
printf("%d\n", z);
printf("%d", x);
return 0;
}
}
}
printf("Not found");
return 0;
}

Program Crashing and some questions about arithmetic operations in coding

Hi stackoverflow community! I'm having some problems with my code. I'm currently a student so basically I'm a beginner. Using Euclidean Algorithm, the code shown below should divide and divide two numbers until the quotient reach 0 but it just stops at the last dividing process just before the quotient turns 0. I don't know if the program crashes because of this. Hoping for a beginner-friendly reply from you guys. Thanks! (sorry if there's already an existing question like this)
Here's the code:
int quotient,quotient2,remainder,remainder2,x,y;
int foo()
{
printf("Enter a number: ");
scanf("%d", &x);
printf("Enter another number: ");
scanf("%d", &y);
if(y >= x){
quotient2 = y / x;
remainder2 = y % x;
printf("%d = %d(%d) + %d\n", y,x,quotient2,remainder2);
if(quotient2 != 0){
do{
y = x;
x = remainder2;
quotient2 = y / x;
remainder2 = y % x;
printf("%d = %d(%d) + %d\n", y,x,quotient2,remainder2);
} while(quotient2 != 0);
}
} else if(x > y){
quotient = x / y;
remainder = x % y;
printf("%d = %d(%d) + %d\n", x,y,quotient,remainder);
if(quotient != 0){
do{
x = y;
y = remainder;
quotient = x / y;
remainder = x % y;
printf("%d = %d(%d) + %d\n", x,y,quotient,remainder);
} while(quotient != 0);
}
}
system("pause");
return 0;
}
If your "Euclidean algorithm" is computing the GCD of two numbers, here is one way to do it. Note it does not divide by 0, it stops when the divisor would be 0.
So the point is, rather than guard against dividing by 0, that's the iteration's end condition anyway.
#include <stdio.h>
unsigned gcd(unsigned x, unsigned y) {
unsigned z;
if (x == 0 || y == 0) {
return 0;
}
while ((z = y % x) != 0) {
y = x;
x = z;
}
return x;
}
int main(void)
{
printf("20 ~ 20 : %u\n", gcd(20, 20));
printf("20 ~ 0 : %u\n", gcd(20, 0));
printf(" 0 ~ 20 : %u\n", gcd( 0, 20));
printf("20 ~ 16 : %u\n", gcd(20, 16));
printf("16 ~ 20 : %u\n", gcd(16, 20));
printf("20 ~ 15 : %u\n", gcd(20, 15));
printf("15 ~ 20 : %u\n", gcd(15, 20));
printf(" 1 ~ 2 : %u\n", gcd( 1, 2));
printf(" 2 ~ 1 : %u\n", gcd( 2, 1));
return 0;
}
Program output:
20 ~ 20 : 20
20 ~ 0 : 0
0 ~ 20 : 0
20 ~ 16 : 4
16 ~ 20 : 4
20 ~ 15 : 5
15 ~ 20 : 5
1 ~ 2 : 1
2 ~ 1 : 1
Note there is no need to swap the arguments. The algorithm works no matter which way round they are.
Notice that with x = remainder2;, x could take on the value of 0. Then the next quotient2 = y / x; remainder2 = y % x; both perform operations (divide by 0 and remainder 0) which are both undefined behavior. The program crash is certainly due to this.
if(y >= x){
quotient2 = y / x;
remainder2 = y % x;
printf("%d = %d(%d) + %d\n", y,x,quotient2,remainder2);
if(quotient2 != 0){
do{
y = x;
x = remainder2;
quotient2 = y / x;
remainder2 = y % x;
printf("%d = %d(%d) + %d\n", y,x,quotient2,remainder2);
} while(quotient2 != 0);
}
As code is swapping the roles of x,y with each operation, code could simplify to: (Also see What is gcd(0,a)gcd(0,a), where a is a positive integer?
unsigned gcd(unsigned a, unsigned b) {
while (b) {
a %= b;
if (a == 0) return b;
b %= a;
}
return a;
}

Why does program output x1 = Nan, x2 = Nan?

I can't work out why my program isn't doing what it's supposed to do. The task was to make a c program that can solve quadratic using two c files. This is the code called interface.c that we got to work with, nothing has to be changed on this one:
#include <stdio.h>
void abc (void);
int a, b, c;
extern double x1real, x1imag, x2real, x2imag;
static void get_parameters (void)
{
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
}
void print_solution(void)
{
printf("The roots of %dx^2 + %dx + %d are:\n",a,b,c);
if(x1imag == 0 && x2imag == 0)
{
if(x1real == x2real)
{
printf("x = %.4f\n", x1real);
}
else
{
printf("x1 = %.4f, x2 = %.4f\n", x1real, x2real);
}
}
else
{
printf("x1 = %.4f+%.4fi, x2 = %.4f-%.4fi\n", x1real, x1imag, x2real, x2imag);
}
}
int main (void)
{
int runs, run;
scanf("%d",&runs);
for(run=0; run < runs; run++)
{
get_parameters();
abc();
print_solution();
}
return 0;
}
Next is the code I made which isn't working, there seems to go something wrong with the integer types I think. With every quadratic formula it will output x1 = nan x2 = nan. There is something wrong with the integer type but can't figure out which.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
extern int a, b, c;
double x1real, x1imag, x2real, x2imag;
int discriminant(void)
{
int discriminant;
discriminant = (pow(b,2)-4*a*c);
return discriminant;
}
void abc (void)
{
if (discriminant() > 0)
{
x1real = (-b - sqrt(discriminant()))/(2*a);
x2real = (-b + sqrt(discriminant()))/(2*a);
x1imag = 0;
x2imag = 0;
}
else
{
x1real = x2real = (-b) / (2*a);
x1imag = (-b - sqrt(-discriminant())) / (2*a);
x2imag = (-b + sqrt(-discriminant())) / (2*a);
}
return;
}
input:
4
2 0 0
1 3 2
3 4 9
1 0 1
output:
The roots of 2x^2 + 0x + 0 are:
x = 0.0000
The roots of 1x^2 + 3x + 2 are:
x1 = -1.0000, x2 = -2.0000
The roots of 3x^2 + 4x + 9 are:
x1 = -0.6667+-2.2653i, x2 = -0.6667-0.9319i
The roots of 1x^2 + 0x + 1 are:
x1 = 0.0000+-1.0000i, x2 = 0.0000-1.0000i
suspected output:
The roots of 2x^2 + 0x + 0 are:
x = 0.0000
The roots of 1x^2 + 3x + 2 are:
x1 = -1.0000, x2 = -2.0000
The roots of 3x^2 + 4x + 9 are:
x1 = -0.6667+1.5986i, x2 = -0.6667-1.5986i
The roots of 1x^2 + 0x + 1 are:
x1 = 0.0000+1.0000i, x2 = 0.0000-1.0000i
Quadratic formula?
conditional expression of「b^2-4ac≧0」 is not called, is it?
int discriminant(void)
{
int discriminant;
discriminant = (pow(b,2)-4*a*c);
return discriminant;
}
void abc (void)
{
// if (discriminant > 0) // it doesn't call [int discriminant(void)]
if (discriminant() >= 0)
{
x1real = (-b - sqrt(discriminant()))/(2*a);
x2real = (-b + sqrt(discriminant()))/(2*a);
x1imag = 0;
x2imag = 0;
}
else
{
x1real = x2real = (-b) / (2*a);
x1imag = (-b - sqrt(-discriminant())) / (2*a);
x2imag = (-b + sqrt(-discriminant())) / (2*a);
}
return;
}
(An addition)
static void get_parameters (void)
{
do {
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
} while(a == 0)
}

What is wrong with my GCD algorithm?

Here is my attempt at computing the GCD of two input numbers:
int rep;
do{
system ("cls");
int a, b, gcd=2, e, d;
cin >> a >> b;
if(a % b != 0 || b % a != 0){
do{
gcd = gcd + 1;
d = a % gcd;
e = b % gcd;
} while(d==0 && e==0);
cout << gcd-1;
}else if(a == 1 || b == 1){
gcd=1;
cout << gcd;
}else if(a >= b){
gcd = a;
cout << gcd;
}else if(b >= a){
gcd = b;
cout << gcd;
}
cin >> rep;
} while(rep == 1);
If I input 8 and 24, it gives me 2 as the answer. Can anyone spot the problem in my code?
The problem is that the algorithm gives up the first time a test GCD fails. In most cases, finding the greatest means going past some values that do not work. In this case, working up to 8 means getting past 3, 5 and 7.
8 % 24 == 8. So the do loop runs at least once. gcd becomes 3 and is tested, but does not divide evenly into 8, so the while condition evaluates to false. Then 3 - 1 (2) is streamed to cout. It's not the correct GCD, though.
You could revise your algorithm to start with the lesser of the 2 inputs, and work downward until there is a success (8 here), and then a failure (7 here).
The meat of the GCD algorithm here is only 3 lines, the rest is devoted to preventing silliness.
#include <stdio.h>
unsigned GCD(unsigned x, unsigned y) {
unsigned z;
if (x < y) {
z = x; // swap
x = y;
y = z;
}
if (y == 0)
return 0;
while (z = x % y) { // perform the GCD with implicit 0 test
x = y;
y = z;
}
return y;
}
int main(void)
{
printf("GCD of %u, %u = %u\n", 1, 0, GCD(1, 0)); // billed as C
printf("GCD of %u, %u = %u\n", 0, 1, GCD(0, 1));
printf("GCD of %u, %u = %u\n", 1, 1, GCD(1, 1));
printf("GCD of %u, %u = %u\n", 8, 24, GCD(8, 24));
return 0;
}
Program output:
GCD of 1, 0 = 0
GCD of 0, 1 = 0
GCD of 1, 1 = 1
GCD of 8, 24 = 8

Detecting if angle is more than 180 degrees

I'm working on a problem that the professor assigned, and I'm having a problem looking for a way to detect if the angle between 3 points is more than 180 degrees, e.g:
I want to detect if alpha is more than 180 degrees. Anyways, my professor has a code that solves the problem, but he has a function called zcross, but I don't exactly know how it works. Could anyone tell me? His code is here:
#include <fstream.h>
#include <math.h>
#include <stdlib.h>
struct point {
double x;
double y;
double angle;
};
struct vector {
double i;
double j;
};
point P[10000];
int hull[10000];
int
zcross (vector * u, vector * v)
{
double p = u->i * v->j - v->i * u->j;
if (p > 0)
return 1;
if (p < 0)
return -1;
return 0;
}
int
cmpP (const void *a, const void *b)
{
if (((point *) a)->angle < ((point *) b)->angle)
return -1;
if (((point *) a)->angle > ((point *) b)->angle)
return 1;
return 0;
}
void
main ()
{
int N, i, hullstart, hullend, a, b;
double midx, midy, length;
vector v1, v2;
ifstream fin ("fc.in");
fin >> N;
midx = 0, midy = 0;
for (i = 0; i < N; i++) {
fin >> P[i].x >> P[i].y;
midx += P[i].x;
midy += P[i].y;
}
fin.close ();
midx = (double) midx / N;
midy = (double) midy / N;
for (i = 0; i < N; i++)
P[i].angle = atan2 (P[i].y - midy, P[i].x - midx);
qsort (P, N, sizeof (P[0]), cmpP);
hull[0] = 0;
hull[1] = 1;
hullend = 2;
for (i = 2; i < N - 1; i++) {
while (hullend > 1) {
v1.i = P[hull[hullend - 2]].x - P[hull[hullend - 1]].x;
v1.j = P[hull[hullend - 2]].y - P[hull[hullend - 1]].y;
v2.i = P[i].x - P[hull[hullend - 1]].x;
v2.j = P[i].y - P[hull[hullend - 1]].y;
if (zcross (&v1, &v2) < 0)
break;
hullend--;
}
hull[hullend] = i;
hullend++;
}
while (hullend > 1) {
v1.i = P[hull[hullend - 2]].x - P[hull[hullend - 1]].x;
v1.j = P[hull[hullend - 2]].y - P[hull[hullend - 1]].y;
v2.i = P[i].x - P[hull[hullend - 1]].x;
v2.j = P[i].y - P[hull[hullend - 1]].y;
if (zcross (&v1, &v2) < 0)
break;
hullend--;
}
hull[hullend] = i;
hullstart = 0;
while (true) {
v1.i = P[hull[hullend - 1]].x - P[hull[hullend]].x;
v1.j = P[hull[hullend - 1]].y - P[hull[hullend]].y;
v2.i = P[hull[hullstart]].x - P[hull[hullend]].x;
v2.j = P[hull[hullstart]].y - P[hull[hullend]].y;
if (hullend - hullstart > 1 && zcross (&v1, &v2) >= 0) {
hullend--;
continue;
}
v1.i = P[hull[hullend]].x - P[hull[hullstart]].x;
v1.j = P[hull[hullend]].y - P[hull[hullstart]].y;
v2.i = P[hull[hullstart + 1]].x - P[hull[hullstart]].x;
v2.j = P[hull[hullstart + 1]].y - P[hull[hullstart]].y;
if (hullend - hullstart > 1 && zcross (&v1, &v2) >= 0) {
hullstart++;
continue;
}
break;
}
length = 0;
for (i = hullstart; i <= hullend; i++) {
a = hull[i];
if (i == hullend)
b = hull[hullstart];
else
b = hull[i + 1];
length += sqrt ((P[a].x - P[b].x) * (P[a].x - P[b].x) + (P[a].y - P[b].y) * (P[a].y - P[b].y));
}
ofstream fout ("fc.out");
fout.setf (ios: :fixed);
fout.precision (2);
fout << length << '\n';
fout.close ();
}
First, we know that if sin(a) is negative, then the angle is more than 180 degrees.
How do we find the sign of sin(a)? Here is where cross product comes into play.
First, let's define two vectors:
v1 = p1-p2
v2 = p3-p2
This means that the two vectors start at p2 and one points to p1 and the other points to p3.
Cross product is defined as:
(x1, y1, z1) x (x2, y2, z2) = (y1z2-y2z1, z1x2-z2x1, x1y2-x2y1)
Since your vectors are in 2d, then z1 and z2 are 0 and hence:
(x1, y1, 0) x (x2, y2, 0) = (0, 0, x1y2-x2y1)
That is why they call it zcross because only the z element of the product has a value other than 0.
Now, on the other hand, we know that:
||v1 x v2|| = ||v1|| * ||v2|| * abs(sin(a))
where ||v|| is the norm (size) of vector v. Also, we know that if the angle a is less than 180, then v1 x v2 will point upwards (right hand rule), while if it is larger than 180 it will point down. So in your special case:
(v1 x v2).z = ||v1|| * ||v2|| * sin(a)
Simply put, if the z value of v1 x v2 is positive, then a is smaller than 180. If it is negative, then it's bigger (The z value was x1y2-x2y1). If the cross product is 0, then the two vectors are parallel and the angle is either 0 or 180, depending on whether the two vectors have respectively same or opposite direction.
zcross is using the sign of the vector cross product (plus or minus in the z direction) to determine if the angle is more or less than 180 degrees, as you've put it.
In 3D, find the cross product of the vectors, find the minimum length for the cross product which is basically just finding the smallest number of x, y and z.
If the smallest value is smaller than 0, the angle of the vectors is negative.
So in code:
float Vector3::Angle(const Vector3 &v) const
{
float a = SquareLength();
float b = v.SquareLength();
if (a > 0.0f && b > 0.0f)
{
float sign = (CrossProduct(v)).MinLength();
if (sign < 0.0f)
return -acos(DotProduct(v) / sqrtf(a * b));
else
return acos(DotProduct(v) / sqrtf(a * b));
}
return 0.0f;
}
Another way to do it would be as follows:
calculate vector v1=p2-p1, v2 = p2 -p3.
Then, use the cross-product formula : u.v = ||u|| ||v|| cos(theta)

Resources