@@ -94,16 +94,13 @@ const std::wstring bin2hex(const unsigned char *p, size_t len)
94
94
return f.str ();
95
95
}
96
96
97
- size_t hex2bin (unsigned char *p , const char *hexstr, size_t len)
98
- {
99
- size_t wlen = 0 ;
100
- while ((wlen<len) && *hexstr && *(hexstr+1 )) // last condition cause np if check fails on middle one.thats coz of short-circuit evaluation
101
- {
102
- len*=sscanf (hexstr, " %2hhx" ,p++); // 0 or 1 (maybe smth else too) . Slow stuff , np
103
- hexstr += 2 ;
104
- wlen++;
97
+ size_t hex2bin (unsigned char *p , const char *hexstr,const size_t length) {
98
+ size_t wcount = 0 ;
99
+ while ( wcount++ < length && *hexstr && *(hexstr + 1 )) { // last condition cause np if check fails on middle one.thats coz of short-circuit evaluation
100
+ sscanf (hexstr, " %2hhx" ,p++); // 7x slower than tables but doesnt metter
101
+ hexstr = hexstr+2 ;
105
102
}
106
- return (wlen == len - 1 )*len ; // zero if error .had enough
103
+ return --wcount ; // error check here is a waste
107
104
}
108
105
109
106
int bren=5 ;
@@ -119,129 +116,82 @@ VOID c(VOID *x)
119
116
SETTEXTEX fw;
120
117
fw.flags =4 ;
121
118
fw.codepage =1200 ;
119
+ std::string timestamp = CW2A ((LPCWSTR)mount_tx->m_timestamp );
120
+ std::string pubkey = CW2A ((LPCWSTR)mount_tx->m_pubkey );
121
+ uint32_t nBits = mount_tx->m_nb ;
122
122
123
- unsigned char hash1[32 ];
124
- uint32_t timestamp_length = wcslen (mount_tx->m_timestamp );
125
- CW2A timestamp ((LPCWSTR)mount_tx->m_timestamp );
126
- CW2A pubkey ((LPCWSTR)mount_tx->m_pubkey );
127
- Transaction transaction={ {},/* version */ 1 ,1 , {},0xFFFFFFFF ,/* sequ */ 0xFFFFFFFF ,1 ,50 *COIN , pubscriptlength , {} ,0 };
128
-
129
- // Encode pubkey to binary and prepend pubkey size, then append the OP_CHECKSIG byte
130
- transaction.pubkeyScript [0 ] = 0x41 ; // A public key is 32 bytes X coordinate, 32 bytes Y coordinate and one byte 0x04, so 65 bytes i.e 0x41 in Hex.
131
- hex2bin (transaction.pubkeyScript + 1 , pubkey ,65 ); // No error checking, yeah.
132
- transaction.pubkeyScript [66 ] = OP_CHECKSIG;
133
-
134
- /* nbits related stuff , first is real size of value in bytes , next is value with
135
- null bytes on the left removed */
136
- uint8_t serializedData[500 ];
137
- uint32_t serializedData_pos = 0 ;
138
- memcpy (serializedData, &transaction.version , 41 );
139
- serializedData_pos = serializedData_pos + 41 ;
140
- /* fo' sho' */
141
- // std::cout << offsetof(Transaction,prevoutIndex) + 4 /* size of prevoutIndex */ - offsetof(Transaction,version);/* OK output : " 41 " */
142
- serializedData_pos++; // +41 byte reserved
143
-
144
- short pl= 0x01 +(mount_tx->m_nb >> 8 > 0 )+(mount_tx->m_nb >> 16 > 0 )+(mount_tx->m_nb >> 24 > 0 );
145
- serializedData[serializedData_pos++] =pl; // statement (smth > 0) returns 0 or 1
146
- memcpy (serializedData + serializedData_pos, &mount_tx->m_nb , pl);
123
+ unsigned char hash1[32 ], serializedData[857 ];
124
+ Transaction transaction = {/* version*/ 1 ,/* inputs*/ 1 , {},/* poutindex*/ 0xFFFFFFFF ,/* sequ*/ 0xFFFFFFFF ,/* outputs*/ 1 ,50 *COIN ,pubscriptlength, {0x41 }/* pubscript[0]:(?)opcode or size */ };
125
+ hex2bin (transaction.pubkeyScript + 1 , pubkey.c_str (), pubkey.length ()/2 ); // pubkey to bytes , then append the OP_CHECKSIG byte
126
+ transaction.pubkeyScript [pubscriptlength - 1 ] = OP_CHECKSIG;
127
+ short sizeone = offsetof (Transaction, sequence); /* , OK output : 41 */
128
+ short sizetwo = sizeof transaction - sizeone;/* OK output : 85 */
129
+ memcpy (serializedData, &transaction, sizeone);
130
+ uint32_t serializedData_pos = sizeone + 1 ;// 42nd byte reserved
131
+ short pl = 0x01 + (nBits >> 8 > 0 ) + (nBits >> 16 >0 )+(nBits >> 24 >0 ); // size of nbits , not neccesary same as sizeof(nbits)
132
+ memcpy (serializedData + serializedData_pos, &nBits, serializedData[serializedData_pos++]/* a:1*/ = pl); // scriptSig {
147
133
serializedData_pos = serializedData_pos + pl;
148
-
149
- // It should essentially mean PUSH 1 byte on the stack which in this case is 0x04 or just 4
150
- serializedData[serializedData_pos++] = 0x01 ;
151
- serializedData[serializedData_pos++] = 0x04 ;
152
- serializedData[serializedData_pos++] = timestamp_length;
153
- uint32_t scriptSig_length = serializedData_pos - 42 + timestamp_length; // serializedData_pos inc'ed already to go as size
154
- serializedData[41 ] = scriptSig_length;
155
-
156
- uint32_t serializedLength =
157
- 4 // tx version
158
- +1 // number of inputs
159
- +32 // hash of previous output
160
- +4 // previous output's index
161
- +1 // 1 byte for the size of scriptSig (?)
162
- +scriptSig_length
163
- +4 // size of sequence
164
- +1 // number of outputs
165
- +8 // 8 bytes for coin value
166
- +1 // 1 byte to represent size of the pubkey Script
167
- +pubscriptlength
168
- +4 ; // 4 bytes for lock time
169
-
170
- memcpy (serializedData + serializedData_pos, timestamp, timestamp_length);
171
- serializedData_pos = serializedData_pos + timestamp_length;
172
- /* scriptsig is done*/
173
-
174
- memcpy (serializedData + serializedData_pos, &transaction.sequence , 85 );
175
-
176
- // hash it with SHA256 and then hash that result to get merkle hash
177
- SHA256 (serializedData, serializedLength, hash1);
178
- SHA256 (hash1, 32 , transaction.merkleHash );
179
-
180
- std::wstring merkleHash = bin2hex (transaction.merkleHash , 32 );
181
- std::reverse (transaction.merkleHash ,transaction.merkleHash + 32 );
182
- std::wstring merkleHashSwapped = bin2hex (transaction.merkleHash , 32 );
183
- std::wstring txScriptSig = bin2hex (serializedData + 42 , scriptSig_length);
134
+ *(short *)(serializedData + serializedData_pos) = 0x0401 /* b:2*/ ; // {0x01,0x04} ,script op's
135
+ serializedData_pos = serializedData_pos + 2 ;
136
+ memcpy (serializedData + serializedData_pos, timestamp.c_str (), serializedData[serializedData_pos++]/* c:1*/ = timestamp.length ()); // scriptSig end
137
+ memcpy (serializedData + serializedData_pos + timestamp.length (), &transaction.sequence , sizetwo);
138
+ uint32_t scriptSig_length = serializedData[sizeone] = pl + 4 /* abc round-up*/ + timestamp.length ();
139
+ SHA256 (serializedData, sizeof transaction + 1 + scriptSig_length, hash1); /* + 1 coz of serializedData[sizeone]*/
140
+ blockheader block_header = {1 /* version*/ ,{}/* hprev*/ ,{}/* merk*/ ,mount_tx->m_ut == 0 ? time (NULL ) : mount_tx->m_ut ,nBits ,mount_tx->m_nonce };
141
+ SHA256 (hash1, 32 , block_header.merk );// hash it with SHA256 and then hash that result to get merkle hash
142
+ std::wstring merkleHash = bin2hex ( block_header.merk , 32 );
143
+ std::reverse (block_header.merk ,block_header.merk +32 );
144
+ std::wstring merkleHashSw = bin2hex (block_header.merk , 32 );
145
+ std::reverse (block_header.merk ,block_header.merk +32 );
146
+ std::wstring txScriptSig = bin2hex (serializedData + sizeone + 1 /* same*/ , scriptSig_length);
184
147
std::wstring pubScriptSig = bin2hex (transaction.pubkeyScript , pubscriptlength);
185
-
186
- t<<L" Coinbase: " << txScriptSig <<L" \n\n PubkeyScript: " <<pubScriptSig <<L" \n\n Merkle Hash: " <<merkleHash<<L" \n Byteswapped: " << merkleHashSwapped << std::endl;
187
- t<<L" Generating block...\n " ;
148
+ t<<L" Coinbase: " << txScriptSig <<L" \n\n PubkeyScript: " <<pubScriptSig <<L" \n\n Merkle Hash: " <<merkleHash<<L" \n Byteswapped: " << merkleHashSw <<std::endl <<L" Generating block...\n " ;
188
149
189
150
SendMessage (hc,EM_SETTEXTEX,(WPARAM)&fw,(LPARAM)(LPCWSTR)t.str ().c_str ());
190
151
PostMessage (hc, WM_VSCROLL, SB_BOTTOM, 0 );
191
-
192
- unsigned char block_hash1[32 ];
193
- blockheader block_header={/* drift */ 1 , {} , {} , mount_tx->m_ut == 0 ? time (NULL ) + c : mount_tx->m_ut + c, mount_tx->m_nb , mount_tx->m_nonce };
194
- blockhash block_hashf;
195
- unsigned char * block_headerp=(unsigned char *)&block_header;
196
- unsigned char * block_hashfp=(unsigned char *)&block_hashf;
197
- std::reverse (transaction.merkleHash ,transaction.merkleHash + 32 );
198
- memcpy (&block_header.merk , transaction.merkleHash , 32 );
152
+ unsigned char block_hash1[32 ] , block_hashfp[32 ];
199
153
unsigned int counter=0 , start = time (NULL );
200
- std::wstringstream w;
201
- std::ios_base::iostate xh=0 ;
202
- while (1 ) {
203
- {
204
- SHA256 (block_headerp , 80 , block_hash1);
205
- SHA256 (block_hash1, 32 , block_hashfp);
154
+ std::wstringstream w;
155
+ std::ios_base::iostate xh=0 ;
156
+ while (1 )
157
+ {
158
+ SHA256 (( unsigned char *)&block_header , 80 , block_hash1);
159
+ SHA256 (block_hash1, 32 , block_hashfp);
206
160
207
161
// The hash is in little-endian, so we check the last 4 bytes.
208
- if (block_hashf. checkbytes == 0 ) // { .. , 0x00, 0x00, 0x00, 0x00 }
209
- {
162
+ if (*( uint32_t *)(block_hashfp + 28 ) == 0 ) // { .. , 0x00, 0x00, 0x00, 0x00 }
163
+ {
210
164
std::reverse (block_hashfp,block_hashfp +32 );
211
165
std::wstring blockHash = bin2hex (block_hashfp, 32 );
212
166
t<<L" \n Block found!\n Hash: " << blockHash <<L" \n Nonce: " << block_header.startNonce << L" \n Unix time: " << block_header.unixtime << std::endl;
213
167
SendMessage (hc,EM_SETTEXTEX,(WPARAM)&fw,(LPARAM)(LPCWSTR)t.str ().c_str ());
214
168
PostMessage (hc, WM_VSCROLL, SB_BOTTOM, 0 );
215
169
b=9 ;
216
- }
170
+ }
217
171
218
- if (time (NULL )-start > 0 )
219
- {
172
+ if (time (NULL )-start > 0 )
173
+ {
220
174
w<<std::setw (7 )<< counter<<L" Hashes/s, Nonce " << std::setw (10.10 )<< block_header.startNonce ;
221
175
counter = 0 ;
222
176
start = time (NULL );
223
177
b7[c]->SetWindowTextW ((LPCWSTR)w.str ().c_str ());
224
178
w.clear (xh, 0 );
225
179
w.str (L" " );
226
- }
180
+ }
227
181
228
- if (block_header.startNonce == 0x100000000 - 1 )
229
- {
230
- block_header.unixtime =block_header.unixtime + 3 ;// trick is that to change pre-start time to find a block(really it's smth else) faster then nonce wraps
231
- }
232
- block_header.startNonce ++; // see what happens on out-bounds
233
- counter++;
182
+ if (++block_header.startNonce == 0 ) block_header.unixtime =block_header.unixtime + 3 ;// trick is that to change pre-start time to find a block(really it's smth else) faster then nonce wraps
183
+ counter++;
184
+
185
+ if (b)
186
+ {
187
+ q->EnableWindow (0 );
188
+ bh->EnableWindow ();
189
+ if (terminator) { PostMessage (hz, WM_CLOSE, NULL , NULL );}
190
+ else { }
191
+ b -= 3 ;
192
+ bren = 5 ;
193
+ break ;
234
194
}
235
-
236
- if (b) {
237
- q->EnableWindow (0 );
238
- bh->EnableWindow ();
239
- if (terminator) { PostMessage (hz, WM_CLOSE, NULL , NULL );}
240
- else { }
241
- b -= 3 ;
242
- bren = 5 ;
243
- break ;
244
- }
245
195
}
246
196
}
247
197
0 commit comments