-
Notifications
You must be signed in to change notification settings - Fork 0
/
mul256x256.c
165 lines (138 loc) · 5.11 KB
/
mul256x256.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
156
157
158
159
160
161
162
163
164
165
/*
funciton mul256x256_512 multiplies unaliased bigint values; out <- a*b
gcc -std=c99 callmul256x256_512.c mul256x256_512.s -fno-pie -no-pie
*/
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
//#define COUNT_CYCLES_X86_64 0
#ifdef COUNT_CYCLES_X86_64
#include <x86intrin.h> // for __rdtsc();
#endif
#if TO512
#define mul256x256 mul256x256_512
#else
#define mul256x256 mul256x256_256
#endif
void mul256x256(uint32_t* out, uint32_t* a, uint32_t* b);
void bench(uint32_t* out, uint32_t* a, uint32_t* b){
for (int i=0; i<10000;i++){
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b); mul256x256(out,a,b);
}
}
// hex string to int array conversion
// input is string of hex characters, without 0x prefix
// also converts to little endian (ie least significant nibble first)
void hexstr_to_bytearray(char* in, uint8_t* out){
//printf("hexstr_to_intarray(%s)\n",in);
size_t len = strlen(in);
uint8_t byte = 0;
uint8_t nibble = 0;
int i;
for (i=len-1; i>=0 ;i--){
nibble = in[i];
if (nibble >= '0' && nibble <= '9') nibble = nibble - '0';
else if (nibble >= 'a' && nibble <='f') nibble = nibble - 'a' + 10;
else if (nibble >= 'A' && nibble <='F') nibble = nibble - 'A' + 10;
else printf("ERROR: %s is not hex string.\n",in);
if (!((i+len%2)%2)) {
byte = (nibble<<4) + byte;
*(out+(len-i)/2-1) = byte;
byte=0;
}
else byte = nibble;
}
if (byte)
*(out+(len-i)/2-1) = byte;
}
int main(int argc, char** argv) {
uint32_t a[16], b[16], out[16], out_expected[16];
for (int i=0; i<16;i++){
a[i]=0;
b[i]=0;
out[i]=0;
out_expected[i]=0;
}
if (argc==1){
hexstr_to_bytearray("000000000000000000000000000000000000000000000000ffffffffffffffff",(uint8_t*)a);
hexstr_to_bytearray("000000000000000000000000000000000000000000000000ffffffffffffffff",(uint8_t*)b);
hexstr_to_bytearray("00000000000000000000000000000000fffffffffffffffe0000000000000001",(uint8_t*)out_expected);
//hexstr_to_bytearray("00000000000000000000000000000000000000000000000000000000ffffffff",(uint8_t*)a);
//hexstr_to_bytearray("00000000000000000000000000000000000000000000000000000000ffffffff",(uint8_t*)b);
//hexstr_to_bytearray("000000000000000000000000000000000000000000000000fffffffe00000001",(uint8_t*)out_expected);
}
else{
hexstr_to_bytearray(argv[1]+2,(uint8_t*)a);
hexstr_to_bytearray(argv[2]+2,(uint8_t*)b);
hexstr_to_bytearray(argv[3]+2,(uint8_t*)out_expected);
}
#if BENCHMARK
bench(out,a,b);
#else // no bench, just evaluate
#ifdef COUNT_CYCLES_X86_64
uint64_t cycles1 = __rdtsc();
#endif
mul256x256(out,a,b);
#ifdef COUNT_CYCLES_X86_64
uint64_t cycles2 = __rdtsc();
printf("num cycles %u\n", (uint32_t)(cycles2-cycles1));
#endif
#endif
int error = 0;
for (int i=0; i<16; i++){
if (out[i]!=out_expected[i])
error=1;
}
//error = 1;
/*
if (error){
for (int i=0; i<16; i++)
printf(" %08x", ((uint32_t*)a)[i]);
printf("\n");
for (int i=0; i<16; i++)
printf(" %08x", ((uint32_t*)b)[i]);
printf("\n");
for (int i=0; i<16; i++)
printf(" %08x", ((uint32_t*)out)[i]);
printf("\n");
for (int i=0; i<16; i++)
printf(" %08x", ((uint32_t*)out_expected)[i]);
printf("\n");
printf("\n");
}
*/
if (error){
for (int i=0; i<8; i+=2)
printf(" %08x%08x", ((uint32_t*)a)[i+1],((uint32_t*)a)[i]);
printf("\n");
for (int i=0; i<8; i+=2)
printf(" %08x%08x", ((uint32_t*)b)[i+1],((uint32_t*)b)[i]);
printf("\n");
for (int i=0; i<16; i+=2)
printf(" %08x%08x", ((uint32_t*)out)[i+1],((uint32_t*)out)[i]);
printf("\n");
for (int i=0; i<16; i+=2)
printf(" %08x%08x", ((uint32_t*)out_expected)[i+1],((uint32_t*)out_expected)[i]);
printf("\n");
printf("\n");
}
else
printf(".");
//printf("\n");
return 0;
}