-
Notifications
You must be signed in to change notification settings - Fork 0
/
main128.c
131 lines (113 loc) · 4.06 KB
/
main128.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
#include <stdlib.h>
#include <stdio.h>
#include "gf2_16.h"
#include "gf2_128.h"
const unsigned long MAX_ROUNDS = 100000000;
//const unsigned long MAX_ROUNDS = 100;
int main(void) {
unsigned int randreg;
FILE* urand = fopen("/dev/urandom", "r");
fread( &randreg, sizeof(randreg), 1, urand );
fclose(urand);
//randreg = 0x1650338b;
printf( "randreg = %#.8x\n", randreg );
uint128_t x, y, z, w, v;
uint64_t i;
for( i = 0; i < MAX_ROUNDS; i++ ) {
x = (uint128_t) rand_r( &randreg );
x <<= 32;
x |= (uint128_t) rand_r( &randreg );
x <<= 32;
x |= (uint128_t) rand_r( &randreg );
x <<= 32;
x |= (uint128_t) rand_r( &randreg );
/* y = (uint128_t) rand_r( &randreg ); */
/* y <<= 32; */
/* y |= (uint128_t) rand_r( &randreg ); */
/* y <<= 32; */
/* y |= (uint128_t) rand_r( &randreg ); */
/* y <<= 32; */
/* y |= (uint128_t) rand_r( &randreg ); */
/* z = (uint128_t) rand_r( &randreg ); */
/* z <<= 32; */
/* z |= (uint128_t) rand_r( &randreg ); */
/* z <<= 32; */
/* z |= (uint128_t) rand_r( &randreg ); */
/* z <<= 32; */
/* z |= (uint128_t) rand_r( &randreg ); */
#if 0
// Commutivity test
w = gf2_128_mult( x, y );
v = gf2_128_mult( y, x );
if( w == v ) {
} else {
printf( "Commutivity fail:\n" );
printf( " x = 0x%.16llx%.16llx\n", (uint64_t) (x>>64), (uint64_t) x );
printf( " y = 0x%.16llx%.16llx\n", (uint64_t) (y>>64), (uint64_t) y );
printf( " w = 0x%.16llx%.16llx\n", (uint64_t) (w>>64), (uint64_t) w );
printf( " v = 0x%.16llx%.16llx\n", (uint64_t) (v>>64), (uint64_t) v );
}
// Associativity test
w = gf2_128_mult( x, gf2_128_mult( y, z ) );
v = gf2_128_mult( gf2_128_mult( x, y ), z );
if( w == v ) {
} else {
printf( "Associativity fail:\n" );
printf( " x = 0x%.16llx%.16llx\n", (uint64_t) (x>>64), (uint64_t) x );
printf( " y = 0x%.16llx%.16llx\n", (uint64_t) (y>>64), (uint64_t) y );
printf( " z = 0x%.16llx%.16llx\n", (uint64_t) (z>>64), (uint64_t) z );
printf( " w = 0x%.16llx%.16llx\n", (uint64_t) (w>>64), (uint64_t) w );
printf( " v = 0x%.16llx%.16llx\n", (uint64_t) (v>>64), (uint64_t) v );
}
// Distrbutivity test
w = gf2_128_mult( x, y ^ z);
v = gf2_128_mult( x, y ) ^ gf2_128_mult( x, z );
if( w == v ) {
} else {
printf( "Distributivity fail:\n" );
printf( " x = 0x%.16llx%.16llx\n", (uint64_t) (x>>64), (uint64_t) x );
printf( " y = 0x%.16llx%.16llx\n", (uint64_t) (y>>64), (uint64_t) y );
printf( " z = 0x%.16llx%.16llx\n", (uint64_t) (z>>64), (uint64_t) z );
printf( " w = 0x%.16llx%.16llx\n", (uint64_t) (w>>64), (uint64_t) w );
printf( " v = 0x%.16llx%.16llx\n", (uint64_t) (v>>64), (uint64_t) v );
}
// Square test
w = gf2_128_mult( x, x );
v = gf2_128_square( x );
if( w == v ) {
} else {
printf( "Square fail:\n" );
printf( " x = 0x%.16llx%.16llx\n", (uint64_t) (x>>64), (uint64_t) x );
printf( " w = 0x%.16llx%.16llx\n", (uint64_t) (w>>64), (uint64_t) w );
printf( " v = 0x%.16llx%.16llx\n", (uint64_t) (v>>64), (uint64_t) v );
}
// Square^16 test
w = x;
for (int j=0; j<16; j++) {
w = gf2_128_square( w );
}
v = gf2_128_square16( x );
if( w == v ) {
} else {
printf( "Square16 fail:\n" );
printf( " x = 0x%.16llx%.16llx\n", (uint64_t) (x>>64), (uint64_t) x );
printf( " w = 0x%.16llx%.16llx\n", (uint64_t) (w>>64), (uint64_t) w );
printf( " v = 0x%.16llx%.16llx\n", (uint64_t) (v>>64), (uint64_t) v );
}
#endif
// inverse test
if( x != 0 ) {
w = gf2_128_inv( x );
v = gf2_128_mult( x, w );
if( v == 0x1 ) {
} else {
printf( "Inversion fail:\n" );
printf( " x = 0x%.16llx%.16llx\n", (uint64_t) (x>>64), (uint64_t) x );
printf( " w = 0x%.16llx%.16llx\n", (uint64_t) (w>>64), (uint64_t) w );
printf( " v = 0x%.16llx%.16llx\n", (uint64_t) (v>>64), (uint64_t) v );
}
}
}
printf( "%lu Tests completed\n", MAX_ROUNDS );
return 0;
}