I'm trying to implement the Feige Fiat Shamir Identification Scheme in C (Arduino) and it works, but only when e = 0. When e = 1 it doesn't work.
How can I make it work?
#include <Wire.h>
int getGCD(int a, int b)
{
int c;
while (a != 0)
{
c = a;
a = b % a;
b = c;
}
return b;
}
int getCoprime(int n)
{
int coprime;
do
{
coprime = random(1, n);
}
while (getGCD(n, coprime) != 1);
return coprime;
}
//Preparation
int n = 7 * 3;
int s = getCoprime(n);
int v = (s * s) % n;
void loop ()
{
e = random(0, 2);
r = random(1, n);
int y = (r * (int)pow(s, e)) % n;
int x = (r * r) % n;
int ysqmodn = y * y % n;
int test = (x * (int)pow(v, e)) % n;
if(ysqmodn == test)
{
Serial.print("The current ICC matches. \n");
}
else
{
Serial.print(String(e));
Serial.print("\n");
}
delay(500);
}
It does work when e==1. When e==0 the computation is trivial, since s and v fall out due to power of 0 always being 1. This is the code copied and altered only enough to get it to compile.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
int random (int low, int high) {
return low + rand() % (high - low);
}
int getGCD(int a, int b) {
int c;
while (a != 0)
{
c = a;
a = b % a;
b = c;
}
return b;
}
int getCoprime(int n) {
int coprime;
do
{
coprime = random(1, n);
}
while (getGCD(n, coprime) != 1);
return coprime;
}
int main(void) {
int e, x, y, r, n, s, v, test, ysqmodn;
srand((unsigned)time(NULL));
n = 7 * 3;
s = getCoprime(n);
v = (s * s) % n;
e = random(0, 2);
r = random(1, n);
printf("n=%d, s=%d, e=%d, r=%d\n", n,s,e,r);
y = (r * (int)pow(s, e)) % n;
x = (r * r) % n;
ysqmodn = y * y % n;
test = (x * (int)pow(v, e)) % n;
if(ysqmodn == test)
printf("The current ICC matches. \n");
else
printf("%d\n", e);
return 0;
}
Sample results:
n=21, s=2, e=1, r=2
The current ICC matches.
n=21, s=11, e=0, r=12
The current ICC matches.
n=21, s=8, e=1, r=14
The current ICC matches.
n=21, s=17, e=1, r=13
The current ICC matches.
n=21, s=1, e=0, r=9
The current ICC matches.
n=21, s=4, e=0, r=13
The current ICC matches.
Related
Currently on one of the cs50x problem sets 'Cash', which is a simple 'ask for how much change is owed, then calculate how many coins are required' task, so not here asking for a solution but, I don't understand why this won't work.
While it does ask for an input, when I type in a float such as 5.96, it simply hangs. No returns, no errors whatsoever. I have to force it shut. The other thing is the while loop doing the same when set to 0, which is the intended way of doing things in order to get the exact number of coins.
I know how inefficient this code is and there are simpler ways of doing things. I just wish to understand the whys in order to avoid making the same mistakes moving on. Thanks.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
// changes and other containers
int p = 1;
int n = 5;
int d = 10;
int q = 25;
int x = 0;
float c;
// get how much change is owed in float
do
{
c = get_float("Change owed: ");
}
while (c < 0);
// int conversion to avoid imprecision
int a = round(c * 100);
// 1 because 0 spits out an unknown error
while (a >= 1)
{
// if the converted amount is bigger than a quarter
if (a >= q)
{
// x = number of coins, a = amount left
x = a / q;
a = a % q;
}
else if (a >= d)
{
x = x + a / d;
a = a - a % d;
}
else if (a >= n)
{
x = x + a / n;
a = a - a % n;
}
else
{
x = x + a / p;
a = a - a % p;
}
}
printf("%i\n", x);
printf("%i\n", a);
}
Thanks to WhozCraig, I figured out that my logic was at fault.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
// changes and other containers
int p = 1;
int n = 5;
int d = 10;
int q = 25;
int x = 0;
float c;
// get how much change is owed in float
do
{
c = get_float("Change owed: ");
}
while (c < 0);
// int conversion to avoid imprecision
int a = round(c * 100);
// 1 because 0 spits out an unknown error
while (a >= 1)
{
// if the converted amount is bigger than a quarter
if (a >= q)
{
// x = number of coins, a = amount left
x = a / q;
a = a % q;
}
else if (a >= d)
{
x = x + a / d;
a = a % d;
}
else if (a >= n)
{
x = x + a / n;
a = a % n;
}
else
{
x = x + a / p;
a = a % p;
}
}
printf("%i\n", x);
printf("%i\n", a);
}
I am trying to calculate value of sin(x) in C but I am getting black screen in code:block after execution, its taking long for compilation and execution.
#include<stdio.h>
float mult(float x, int m, int i) {
float a = x;
if (i == m) {
return x;
} else {
i++;
a = a * mult(x, m, i);
return a;
}
}
int fact(int m) {
printf("%d! ", m); fflush(stdout);
int b;
if (m == 1) {
return 1;
} else {
b = m * fact(m - 1);
return b;
}
}
float term(float x, int m) {
float a = 0, b = 0, c = 0;
int i = 0;
a = mult(x, m, i);
b = fact(m);
c = a / (1.0 * b);
return c;
}
float sinof(float x, int m, int n) {
float b = 0;
if (m >= 10) {
return (0);
} else {
printf("......%d ", m); fflush(stdout);
b = term(x, m);
m = m + 2;
n = -n;
b = b + (n * sinof(x, m, n));
return b;
}
}
int main() {
float x = 0, sin = 0;
int m = 1, n = 1;
printf("Enter the angle in radians:");
scanf("%f", &x);
sin = sinof(x, m, n);
printf("%f", sin);
}
I hope the logic is correct.
m is odd. Below fails to stop recursion.
if(m==10){ // Never true
return(0);
} else{
b=term(x,m);
m=m+2;n=-n; // ***********
b=b+(n*sinof(x,m,n));
return b;
}
I recommend OP get own code working first. There are various other issues.
For a simplified recursive sine(), mouse over to see.
static double my_sin_helper(double xx, double term, unsigned n) {
if (term + 1.0 == 1.0) {
return term;
}
return term - my_sin_helper(xx, xx *term / ((n + 1) * (n + 2)), n + 2);
}
// valid for [-pi/2 + pi/2]
double my_sin_primary(double x) {
return x * my_sin_helper(x * x, 1.0, 1);
}
I am currently doing exercices on Kattis and I meet a problem with I think the float.
I must compare the size of matchstick with the size of box
Sibice problem on Kattis.com
Here is a picture of the exercise
I can make examples but when I submit I past only the first two...
Here is a picture of my submission
I don't have a trace or explication for know my errors... I tried to change the type of my variables but there is no change... I think the problems is float but I need it.
Here is my code
#include <stdio.h>
void sibice(float n, float w, float h)
{
float v = 0;
for(float i = 0; i != n; i += 1) {
scanf("%f", &v);
if(v < w + h / 2)
printf("DA\n");
if(v == w + h / 2)
printf("DA\n");
if(v > w + h / 2) {
printf("NE\n");
}
}
}
int main(void)
{
float n = 0;
float w = 0;
float h = 0;
scanf("%f %f %f", &n, &w, &h);
sibice(n, w, h);
return (0);
}
Do you think that I can optimize my code ?
Incorrect test
If a match can fit is a more like v*v <= h*h + w*w than v < w + h / 2. Can it diagonally fit?
See Pythagorean theorem
#include <math.h>
...
float hyp = hypotf(h,w); // sqrt(h*h + w*w)
for(float i = 0; i != n; i += 1) {
scanf("%f", &v);
// if(v < w + h / 2)
if(v < hyp)
No need for 3 tests
One test is sufficient.
if(v <= hyp) {
printf("DA\n");
} else {
printf("NE\n");
}
Integer math
As all calculations are whole numbers, code could use int only math.
void sibice(int n, int w, int h) {
int hyp2 = h*h + w*w;
for (int i = 0; i != n; i += 1) {
int v;
scanf("%d", &v);
if (v*v <= hyp2)
...
Other issues may exist
Why do you need to use float? With these changes the code passes the tests:
#include <stdio.h>
#include <stdlib.h>
void sibice(int n, int w, int h)
{
int v;
int result;
for (int i = 0; i != n; i += 1) {
result = scanf("%d", &v);
if (1 != result)
exit(1);
if (v * v <= w * w + h * h)
printf("DA\n");
else
printf("NE\n");
}
}
int main(void)
{
int n = 0;
int w = 0;
int h = 0;
int result = 0;
result = scanf("%d %d %d", &n, &w, &h);
if (3 != result)
return 1;
sibice(n, w, h);
return (0);
}
I'm trying to hone my skills in C as well as get a better understanding of RSA, so I took it upon myself to try and make my own generator. I've generated both base prime numbers, p and q, calculated the value o = (p-1) * (q-1). These are working correctly. The next steps is where I'm having issues. I'm pretty close, but I can't seem to find the correct way to calculate the encryption and decryption factors of the 1 mod r value. Any help for generating this would be awesome. (or just write the code yourself and i can learn off that) Plus if there's any other issues let me know!
Here's my code:
#include<stdio.h>
#include<time.h>
#include"../hdr/test.h" //fake header just to satisfy my makefile
//Receives: number that was generated in main
//Returns: 1 for false, 0 for true
//Purpose: checks if the numbers is prime and returns 1 or 0
int isPrime(int number)
{
if (number <= 1)
return 0;
int i;
for (i=2; i<number; i++) {
if (number % i == 0)
return 0;
}
return 1;
}
/*Receives: an integer ((p-1) * (q-1))
Returns: integer (e)
Purpose: Generates the relatively prime number to ((p-1) * (q-1))*/
int gen_E(int o)
{
puts("Generating E...");
int e;
do {e = rand() % o;} while (GCD(e, o) != 1);
return e;
}
/*Receives: integer E and O from the main
Returns: newly calculated integer D
Purpose: Generates D for the private key*/
int gen_D(int e, int o)
{
puts("Generating D...");
int e = a;
int o = b;
int phin, d;
for (phin = 1; phin < o; phin ++)
for (d = 1; d < 998001; d ++)
if ((e * d) == (o * phin) + 1) return d;
}
/*Receives: integer E and O from main
Returns: greatest common divisor's remainder
Purpose: Calculate if the two numbers share a divisor.*/
int GCD(int e, int o)
{
int c;
while (e != 0) {
c = e; e = o%e; o = c;
}
return o;
}
int main()
{
puts("Generating Prime P...");
srand(time(NULL));
int p = rand() % 1000;
while (isPrime(p) != 1) {
p = rand() % 1000;
}
puts("Generating Prime Q...");
int q = rand() % 1000;
while (isPrime(q) != 1) {
q = rand() % 1000;
}
int n = p * q;
int o = (p - 1) * (q - 1);
int e = gen_E(o);
int d = gen_D(e, o);
printf("p: %d\nq: %d\nn: %d\no: %d\ne: %d\nd: %d\n", p, q, n, o, e, d);
printf("KU = {%d, %d}\n", e, n);
printf("KR = {%d, %d}\n", d, n);
}
You will need a function to compute GCD (greatest common divider) of two numbers. After that, choose random e such that GCD(e,o) == 1.
Of course in practice it's probably more complicated than that but for homework it's fine.
how to get the value of an integer x, indicated by x!, it is the product of the numbers 1 to x.
Example: 5! 1x2x3x4x5 = 120.
int a , b = 1, c = 1, d = 1;
printf("geheel getal x = ");
scanf("%d", &a);
printf("%d! = ", a);
for(b = 1; b <= a; b++)
{
printf("%d x ", c);
c++;
d = d*a;
}
printf(" = %d", d);
how to get the som of an integer x, indicated by x!, is the product of the numbers 1 to x.
Did you mean factorial of x ?
Change d = d*a; to d = d*b inside the loop
You can simply do:
for(b = 1; b <= a; b++) {
d *= b;
}
// d now has a!
This is the optimal implementation in size and speed:
int factorial(int x)
{
static const int f[13] = { 1, 1, 2, 6, 24, 120, /* ... */ };
if ((unsigned)x < (sizeof f/sizeof f[0])) return f[x];
else return INT_MAX+1; /* or your favorite undefined behavior */
}
Hint: x! (x factorial) does not fit in an int except for very very small values of x.
Try
d = d * b;
instead of
d = d * a
and it should work fine
You actually have a lot of redundant code there, that might be why you did not spot the error yourself.
To calculate the factorial, you only need the accumulator (d in the above code) and the input (a). Why?
My code is not good as other but it works for me:
#include <iostream>
using namespace std;
unsigned int fattoriale (int n){
if (n == 1){
return 1;
}
else {
return n * fattoriale(n-1);
}
}
int main() {
int tmp, num;
cin >> num;
tmp = fattoriale(num);
cout << "Stampo il fattoriale del numero inserito: " << tmp << endl;
}
int factorial(int x)
{
int f;
if (x == 0)
{
f = 1;
}
else if (x > 0)
{
f = x*factorial(x-1);
}
return f;
}
int main()
{
int n = 0;
cout << factorial(n);
return 0;
}