forked from dple/ecdsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
155 lines (133 loc) · 2.62 KB
/
utils.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* utils.c
*
* Created on: Apr 14, 2011
* Author: dple
*/
#include "ecdsa.h"
#include "utils.h"
char trapkey(void) {
int c1, c2, ctr;
char choice;
do {
ctr = 0;
//printf("\nCommand (h for help)> ");
c1 = getchar();
if(c1 != '\n' && c1 != EOF) {
++ctr;
while((c2 = getchar()) != EOF && c2 != '\n')
++ctr;
if(ctr != 1)
printf("Unknown command, press 'h' for a list of available commands");
}
choice = (char) c1;
}
while(ctr != 1);
return choice;
}
/*
* Return the length in bits of a big integer
*/
int bitlength(mpz_t x) {
return mpz_sizeinbase(x, 2);
}
/** Choose a or b depending the value of bit 'bit'
* \params a, b, bit
* \return If bit = 0, return a, otherwise return b
*
*/
int select_int(int a, int b, int bit) {
/* -0 = 0, -1 = 0xFFFFFFFF */
int mask = -bit;
int ret = mask & (a^b);
ret = ret ^ a;
return ret;
}
/*
#ifdef _WIN32
#include <time.h>
static inline double GetCurrTime()
{
return clock() / double(CLOCKS_PER_SEC);
}
#else
#include <sys/time.h>
#include <stdio.h>
static inline double GetCurrTime()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + (double) tv.tv_usec * 1e-6;
}
#endif
static inline uint64 getRdtsc()
{
#ifdef _MSC_VER
return __rdtsc();
#else
unsigned int eax, edx;
__asm__ volatile("rdtsc" : "=a"(eax), "=d"(edx));
return ((uint64)edx << 32) | eax;
#endif
}
*/
// Accepts a decimal integer and returns a binary coded string
char* dectobin(long i) {
char* tmp = "";
while( i > 0 ) {
/* bitwise AND operation with the last bit */
(i & 0x1) ? (*tmp++='1') : (*tmp++='0');
/* bit shift to the right, when there are no bits left the value is 0, so the loop ends */
i >>= 1;
}
return tmp;
}
// Show a decimal number in binary
void showbits(int h) {
if (h==1)
printf("%d ",h);
else {
showbits(h/2);
printf("%d ",h%2);
}
}
// Count the number of 1-bits, the Hamming weight for an integer
int hammingWeight(unsigned int n) {
int count = 0;
while (n) {
count += n & 0x1u;
n >>= 1;
}
return count;
}
/*
* Count the number of bits
*/
int bitcount(unsigned int n) {
int count = 8 * sizeof(int);
unsigned int tmp = 1;
tmp <<= count - 1;
do {
count--;
tmp >>=1;
}while (!(n & tmp)) ;
return count;
}
// If the bit at position pos is 1, function return true. Else return false
bool bit(int n, int pos) {
int _bit = 1;
_bit <<= pos - 1;
return (n & _bit ? true : false);
}
// Extract a chain of bit from a number n
int extract(int n, int tail, int size) {
int i, ret = 1;
for (i = 1; i < size; i++) {
ret <<= 1;
ret++;
}
ret <<= tail;
ret = ret & n;
ret >>= tail;
return ret;
}