forked from shivamag437/Oblivious_Transfer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bob_ot1.c
87 lines (73 loc) · 1.89 KB
/
bob_ot1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include<gmp.h>
#include "rsa.h"
int main(){
//Open required files
FILE *randomx, *public, *c_out, *k_out, *enc_messages;
randomx = fopen("randomx.txt", "r"); //file to store randomly generated x_0, x_1
public = fopen("public_key.txt", "r"); //file containing Alice's public key
c_out = fopen("bob_c.txt", "w"); //stores c = (x_b + k^e) mod n
k_out = fopen("bob_private.txt","w"); //Stores the random k generated by Bob and his choice of b
enc_messages = fopen("enc_messages.txt", "r"); // to write m'_0 (= m_0 - v_0) and m'_1 (= m_1 - v_1) to "enc_messages.txt"
//Variable initialisation
mpz_t c,x_b,k,e,n,x_0,x_1,temp;
int b;
mpz_init(x_b);
mpz_init(x_1);
mpz_init(x_0);
mpz_init(c);
mpz_init(k);
mpz_init(e);
mpz_init(n);
mpz_init(temp);
int seed;
printf("Enter seed: ");
scanf("%d",&seed);
gmp_randstate_t state;
gmp_randinit_mt(state);
gmp_randseed_ui(state,seed);
//Generating random k
mpz_urandomb(k, state, 500);
mpz_out_str(k_out,10,k);
//Reading n and e from public_key.txt
gmp_fscanf(public, "%Zd", n);
gmp_fscanf(public, "%Zd", e);
//reading x_0 and x_1 from randomx.txt
gmp_fscanf(randomx, "%Zd", x_0);
gmp_fscanf(randomx, "%Zd", x_1);
//closing public_key.txt and randomx.txt
fclose(public);
fclose(randomx);
//Bob chooses b
printf("Enter b: ");
scanf("%d",&b);
while((b != 0) && (b != 1)){
printf("b can only be 1 or 0!\n");
printf("Enter b: ");
scanf("%d",&b);
}
fprintf(k_out,"\n%d",b);
fclose(k_out);
//Initialising x_b based on b
if(b==1)
mpz_set(x_b,x_1);
else
mpz_set(x_b,x_0);
mpz_clear(x_0);
mpz_clear(x_1);
//calculating c=(x_b + (k^e)) mod n
mpz_powm(c,k,e,n);
mpz_fdiv_r(temp,x_b,n);
mpz_add(c,c,temp);
mpz_fdiv_r(c,c,n);
//Writing c to bob_c.txt
mpz_out_str(c_out,10,c);
fclose(c_out);
mpz_clear(temp);
mpz_clear(n);
mpz_clear(e);
mpz_clear(k);
mpz_clear(c);
mpz_clear(x_b);
}