Debian Codegraph @

fastmint_library.c

raw symbols SCIP
1 #include "sha1.h"
2 #include "libfastmint.h"
3
4 #if defined( DEBUG )
5 #include <stdio.h>
6 #endif
7
8 int minter_library_test(void)
9 {
10 /* This minter runs on any hardware */
11 return 1;
12 }
13
14 unsigned long minter_library(int bits, int* best, unsigned char *block, const uInt32 IV[5], int tailIndex, unsigned long maxIter, MINTER_CALLBACK_ARGS)
15 {
16 MINTER_CALLBACK_VARS;
17 unsigned long iters = 0;
18 int t = 0, gotBits = 0, maxBits = (bits > 16) ? 16 : bits;
19 uInt32 bitMask1Low = 0 , bitMask1High = 0 , s = 0 ;
20 const char *p = encodeAlphabets[EncodeBase64];
21 uInt32 A = 0, B = 0;
22 int addressMask = 0 ;
23 static const int endTest = 3;
24 unsigned char *output = (unsigned char*) block;
25 unsigned char Xa[ SHA1_INPUT_BYTES*2 ]; /* worst case */
26 unsigned char* X = Xa;
27 uInt32 *W = (uInt32*)Xa;
28 uInt32 H[ SHA1_DIGEST_WORDS ];
29 int blocks = (tailIndex+1+8) > SHA1_INPUT_BYTES ? 2 : 1;
30 #if defined( DEBUG )
31 int i;
32 #endif
33
34 if ( *best > 0 ) { maxBits = *best+1; }
35
36 /* Work out whether we need to swap bytes during encoding */
37 addressMask = ( *(char*)&endTest );
38
39 for(t=0; t < 16*blocks; t++) { W[t] = GET_WORD(output + t*4); }
40
41 #if defined( DEBUG )
42 fprintf( stderr, "maxBits = %d\n", maxBits );
43 #endif
44
45 /* Work out which bits to mask out for test */
46 if(maxBits < 32) {
47 if ( bits == 0 ) { bitMask1Low = 0; } else {
48 bitMask1Low = ~((((uInt32) 1) << (32 - maxBits)) - 1);
49 }
50 bitMask1High = 0;
51 } else {
52 bitMask1Low = ~0;
53 bitMask1High = ~((((uInt32) 1) << (64 - maxBits)) - 1);
54 }
55 maxBits = 0;
56
57 #if defined( DEBUG )
58 fprintf( stderr, "mask0 = " );
59 for ( i = 0; i < 32; i++ ) {
60 fputc( (bitMask1High & (1UL << i) )? '1' : '0', stderr );
61 }
62 fputc( ' ', stderr );
63 for ( i = 0; i < 32; i++ ) {
64 fputc( (bitMask1Low & (1UL << i) )? '1' : '0', stderr );
65 }
66 fputc( '\n', stderr );
67 #endif
68
69 /* The Tight Loop - everything in here should be extra efficient */
70 for(iters=0; iters < maxIter; iters++) {
71 /* Encode iteration count into tail */
72 X[(tailIndex - 1) ^ addressMask] = p[(iters ) & 0x3f];
73 if(!(iters & 0x3f)) {
74 if ( iters >> 6 ) {
75 X[(tailIndex - 2) ^ addressMask] = p[(iters >> 6) & 0x3f];
76 }
77 if ( iters >> 12 ) {
78 X[(tailIndex - 3) ^ addressMask] = p[(iters >> 12) & 0x3f];
79 }
80 if ( iters >> 18 ) {
81 X[(tailIndex - 4) ^ addressMask] = p[(iters >> 18) & 0x3f];
82 }
83 if ( iters >> 24 ) {
84 X[(tailIndex - 5) ^ addressMask] = p[(iters >> 24) & 0x3f];
85 }
86 if ( iters >> 30 ) {
87 X[(tailIndex - 6) ^ addressMask] = p[(iters >> 30) & 0x3f];
88 }
89 }
90
91 memcpy( H, IV, SHA1_DIGEST_BYTES );
92 SHA1_Transform( H, X );
93 if ( blocks == 2 ) {
94 SHA1_Transform( H, X+SHA1_INPUT_BYTES );
95 }
96
97 A = H[0];
98 B = H[1];
99
100 #if defined( DEBUG )
101 #define P(n) (X[n] ? ( (X[n] == 0x80) ?'$' : ((X[n]<32)?'#':X[n]) ) : '@')
102
103 fprintf( stderr,
104 "H(%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
105 "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
106 "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c"
107 "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%02x%02x"
108 ") = 0x%08x%08x\n",
109 P(0+3),P(0+2),P(0+1),P(0+0),
110 P(4+3),P(4+2),P(4+1),P(4+0),
111 P(8+3),P(8+2),P(8+1),P(8+0),
112 P(12+3),P(12+2),P(12+1),P(12+0),
113 P(16+3),P(16+2),P(16+1),P(16+0),
114 P(20+3),P(20+2),P(20+1),P(20+0),
115 P(24+3),P(24+2),P(24+1),P(24+0),
116 P(28+3),P(28+2),P(28+1),P(28+0),
117 P(32+3),P(32+2),P(32+1),P(32+0),
118 P(36+3),P(36+2),P(36+1),P(36+0),
119 P(40+3),P(40+2),P(40+1),P(40+0),
120 P(44+3),P(44+2),P(44+1),P(44+0),
121 P(48+3),P(48+2),P(48+1),P(48+0),
122 P(52+3),P(52+2),P(52+1),P(52+0),
123 P(56+3),P(56+2),P(56+1),P(56+0),
124 P(60+3),P(60+2),X[60+1],X[60+0],
125 A, B );
126 #endif
127
128 /* Is this the best bit count so far? */
129 if(!(A & bitMask1Low) && !(B & bitMask1High)) {
130 /* Count bits */
131 gotBits = 0;
132 if(A) {
133 s = A;
134 while(!(s & 0x80000000)) {
135 s <<= 1;
136 gotBits++;
137 }
138 } else {
139 gotBits = 32;
140 if(B) {
141 s = B;
142 while(!(s & 0x80000000)) {
143 s <<= 1;
144 gotBits++;
145 }
146 } else {
147 gotBits = 64;
148 }
149 }
150
151 if ( gotBits > *best ) { *best = gotBits; }
152 /* Regenerate the bit mask */
153 maxBits = gotBits+1;
154 #if defined( DEBUG )
155 fprintf( stderr, "maxBits = %d\n", maxBits );
156 #endif
157
158 if(maxBits < 32) {
159 bitMask1Low = ~((((uInt32) 1) << (32 - maxBits)) - 1);
160 bitMask1High = 0;
161 } else {
162 bitMask1Low = ~0;
163 bitMask1High = ~((((uInt32) 1) << (64 - maxBits)) - 1);
164 }
165
166 /* Copy this result back to the block buffer */
167 for(t=0; t < 16*blocks; t++) {
168 PUT_WORD(output + t*4, W[t]);
169 }
170 #if defined( DEBUG )
171 fprintf( stderr, "output: %s\n", output );
172 #endif
173
174 #if defined( DEBUG )
175 fprintf( stderr, "maskN = " );
176 for ( i = 0; i < 32; i++ ) {
177 fputc( (bitMask1High & (1UL << i) )? '1' : '0', stderr );
178 }
179 fputc( ' ', stderr );
180 for ( i = 0; i < 32; i++ ) {
181 fputc( (bitMask1Low & (1UL << i) )? '1' : '0', stderr );
182 }
183 fputc( '\n', stderr );
184 #endif
185
186 /* Is it good enough to bail out? */
187 if(gotBits >= bits) {
188 return iters+1;
189 }
190 }
191 MINTER_CALLBACK();
192 }
193
194 return iters+1;
195 }