forked from shivamag437/garbled_circuits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_conversion.c
68 lines (54 loc) · 1.37 KB
/
test_conversion.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
#include<stdio.h>
#include<stdlib.h>
#include <gmp.h>
//Converts a given bitstring to a character array of equivalent binary value
char* bin_to_char(mpz_t inputstring, int length)
{
mpz_t char_value,temp,bitstring;
mpz_init(char_value);
mpz_init(temp);
mpz_init(bitstring);
mpz_set(bitstring,inputstring);
int n=0;
//Allocating space for character array
char* string = (char *) malloc((length/8)*sizeof(char));
char* test = (char *) malloc((8)*sizeof(char));
mpz_set_ui(temp, 1);
mpz_mul_2exp(temp, temp, 8);
mpz_sub_ui(temp,temp,1);
for(int i=length;i>0;i-=8){
//Extract bottom 8 bits from bitstring
mpz_and(char_value, bitstring, temp);
mpz_get_str(test,10,char_value);
printf("%s\n",test);
//Store obtained bits in the character array
string[n]=(char)mpz_get_si(char_value);
n++;
//Remove bottom 8 bits from bitstring
mpz_fdiv_q_2exp(bitstring,bitstring,8);
}
return string;
}
int main()
{
mpz_t a;
char* str;
//str = (unsigned char *) malloc(20*sizeof(char));
mpz_init(a);
int seed;
printf("Enter seed: ");
scanf("%d",&seed);
gmp_randstate_t state;
gmp_randinit_mt(state);
gmp_randseed_ui(state, seed);
mpz_rrandomb(a, state, 64);
//mpz_get_str(str, 10, a);
//printf("%s\n", str);
//mpz_set_ui(a,66);
str = bin_to_char(a,64);
//printf("%s\n", str);
for(int i=0;i<8;i++){
printf("%c (%d)",str[i],(int)str[i]);
}
printf("\n");
}