]> Git Repo - VerusCoin.git/blame - src/komodo_utils.h
test
[VerusCoin.git] / src / komodo_utils.h
CommitLineData
d019c447 1/******************************************************************************
2 * Copyright © 2014-2016 The SuperNET Developers. *
3 * *
4 * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
5 * the top-level directory of this distribution for the individual copyright *
6 * holder information and the developer policies on copyright and licensing. *
7 * *
8 * Unless otherwise agreed in a custom licensing agreement, no part of the *
9 * SuperNET software, including this file may be copied, modified, propagated *
10 * or distributed except according to the terms contained in the LICENSE file *
11 * *
12 * Removal or modification of this copyright notice is prohibited. *
13 * *
14 ******************************************************************************/
3eea72f2 15
32b1b443 16#define dstr(x) ((double)(x) / SATOSHIDEN)
17
48fa27d9 18union _bits256 { uint8_t bytes[32]; uint16_t ushorts[16]; uint32_t uints[8]; uint64_t ulongs[4]; uint64_t txid; };
19typedef union _bits256 bits256;
20
d019c447 21#include "mini-gmp.c"
22#include "uthash.h"
3eea72f2 23
24#define CRYPTO777_PUBSECPSTR "020e46e79a2a8d12b9b5d12c7a91adb4e454edfae43c0a0cb805427d2ac7613fd9"
b62d7030 25#define CRYPTO777_KMDADDR "RXL3YXG2ceaB6C5hfJcN4fvmLH2C34knhA"
26
d019c447 27#define KOMODO_PUBTYPE 60
28
d019c447 29struct sha256_vstate { uint64_t length; uint32_t state[8],curlen; uint8_t buf[64]; };
30struct rmd160_vstate { uint64_t length; uint8_t buf[64]; uint32_t curlen, state[5]; };
31
32// following is ported from libtom
33
34#define STORE32L(x, y) \
35{ (y)[3] = (uint8_t)(((x)>>24)&255); (y)[2] = (uint8_t)(((x)>>16)&255); \
36(y)[1] = (uint8_t)(((x)>>8)&255); (y)[0] = (uint8_t)((x)&255); }
37
38#define LOAD32L(x, y) \
39{ x = (uint32_t)(((uint64_t)((y)[3] & 255)<<24) | \
40((uint32_t)((y)[2] & 255)<<16) | \
41((uint32_t)((y)[1] & 255)<<8) | \
42((uint32_t)((y)[0] & 255))); }
43
44#define STORE64L(x, y) \
45{ (y)[7] = (uint8_t)(((x)>>56)&255); (y)[6] = (uint8_t)(((x)>>48)&255); \
46(y)[5] = (uint8_t)(((x)>>40)&255); (y)[4] = (uint8_t)(((x)>>32)&255); \
47(y)[3] = (uint8_t)(((x)>>24)&255); (y)[2] = (uint8_t)(((x)>>16)&255); \
48(y)[1] = (uint8_t)(((x)>>8)&255); (y)[0] = (uint8_t)((x)&255); }
49
50#define LOAD64L(x, y) \
51{ x = (((uint64_t)((y)[7] & 255))<<56)|(((uint64_t)((y)[6] & 255))<<48)| \
52(((uint64_t)((y)[5] & 255))<<40)|(((uint64_t)((y)[4] & 255))<<32)| \
53(((uint64_t)((y)[3] & 255))<<24)|(((uint64_t)((y)[2] & 255))<<16)| \
54(((uint64_t)((y)[1] & 255))<<8)|(((uint64_t)((y)[0] & 255))); }
55
56#define STORE32H(x, y) \
57{ (y)[0] = (uint8_t)(((x)>>24)&255); (y)[1] = (uint8_t)(((x)>>16)&255); \
58(y)[2] = (uint8_t)(((x)>>8)&255); (y)[3] = (uint8_t)((x)&255); }
59
60#define LOAD32H(x, y) \
61{ x = (uint32_t)(((uint64_t)((y)[0] & 255)<<24) | \
62((uint32_t)((y)[1] & 255)<<16) | \
63((uint32_t)((y)[2] & 255)<<8) | \
64((uint32_t)((y)[3] & 255))); }
65
66#define STORE64H(x, y) \
67{ (y)[0] = (uint8_t)(((x)>>56)&255); (y)[1] = (uint8_t)(((x)>>48)&255); \
68(y)[2] = (uint8_t)(((x)>>40)&255); (y)[3] = (uint8_t)(((x)>>32)&255); \
69(y)[4] = (uint8_t)(((x)>>24)&255); (y)[5] = (uint8_t)(((x)>>16)&255); \
70(y)[6] = (uint8_t)(((x)>>8)&255); (y)[7] = (uint8_t)((x)&255); }
71
72#define LOAD64H(x, y) \
73{ x = (((uint64_t)((y)[0] & 255))<<56)|(((uint64_t)((y)[1] & 255))<<48) | \
74(((uint64_t)((y)[2] & 255))<<40)|(((uint64_t)((y)[3] & 255))<<32) | \
75(((uint64_t)((y)[4] & 255))<<24)|(((uint64_t)((y)[5] & 255))<<16) | \
76(((uint64_t)((y)[6] & 255))<<8)|(((uint64_t)((y)[7] & 255))); }
77
78// Various logical functions
79#define RORc(x, y) ( ((((uint32_t)(x)&0xFFFFFFFFUL)>>(uint32_t)((y)&31)) | ((uint32_t)(x)<<(uint32_t)(32-((y)&31)))) & 0xFFFFFFFFUL)
80#define Ch(x,y,z) (z ^ (x & (y ^ z)))
81#define Maj(x,y,z) (((x | y) & z) | (x & y))
82#define S(x, n) RORc((x),(n))
83#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
84#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
85#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
86#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
87#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
88#define MIN(x, y) ( ((x)<(y))?(x):(y) )
89
90static inline int32_t sha256_vcompress(struct sha256_vstate * md,uint8_t *buf)
91{
92 uint32_t S[8],W[64],t0,t1,i;
93 for (i=0; i<8; i++) // copy state into S
94 S[i] = md->state[i];
95 for (i=0; i<16; i++) // copy the state into 512-bits into W[0..15]
96 LOAD32H(W[i],buf + (4*i));
97 for (i=16; i<64; i++) // fill W[16..63]
98 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
99
100#define RND(a,b,c,d,e,f,g,h,i,ki) \
101t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
102t1 = Sigma0(a) + Maj(a, b, c); \
103d += t0; \
104h = t0 + t1;
105
106 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
107 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
108 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
109 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
110 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
111 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
112 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
113 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
114 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
115 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
116 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
117 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
118 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
119 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
120 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
121 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
122 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
123 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
124 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
125 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
126 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
127 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
128 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
129 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
130 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
131 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
132 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
133 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
134 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
135 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
136 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
137 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
138 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
139 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
140 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
141 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
142 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
143 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
144 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
145 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
146 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
147 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
148 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
149 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
150 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
151 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
152 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
153 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
154 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
155 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
156 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
157 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
158 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
159 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
160 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
161 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
162 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
163 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
164 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
165 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
166 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
167 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
168 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
169 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
170#undef RND
171 for (i=0; i<8; i++) // feedback
172 md->state[i] = md->state[i] + S[i];
173 return(0);
174}
175
176#undef RORc
177#undef Ch
178#undef Maj
179#undef S
180#undef R
181#undef Sigma0
182#undef Sigma1
183#undef Gamma0
184#undef Gamma1
185
186static inline void sha256_vinit(struct sha256_vstate * md)
187{
188 md->curlen = 0;
189 md->length = 0;
190 md->state[0] = 0x6A09E667UL;
191 md->state[1] = 0xBB67AE85UL;
192 md->state[2] = 0x3C6EF372UL;
193 md->state[3] = 0xA54FF53AUL;
194 md->state[4] = 0x510E527FUL;
195 md->state[5] = 0x9B05688CUL;
196 md->state[6] = 0x1F83D9ABUL;
197 md->state[7] = 0x5BE0CD19UL;
198}
199
200static inline int32_t sha256_vprocess(struct sha256_vstate *md,const uint8_t *in,uint64_t inlen)
201{
202 uint64_t n; int32_t err;
203 if ( md->curlen > sizeof(md->buf) )
204 return(-1);
205 while ( inlen > 0 )
206 {
207 if ( md->curlen == 0 && inlen >= 64 )
208 {
209 if ( (err= sha256_vcompress(md,(uint8_t *)in)) != 0 )
210 return(err);
211 md->length += 64 * 8, in += 64, inlen -= 64;
212 }
213 else
214 {
215 n = MIN(inlen,64 - md->curlen);
216 memcpy(md->buf + md->curlen,in,(size_t)n);
217 md->curlen += n, in += n, inlen -= n;
218 if ( md->curlen == 64 )
219 {
220 if ( (err= sha256_vcompress(md,md->buf)) != 0 )
221 return(err);
222 md->length += 8*64;
223 md->curlen = 0;
224 }
225 }
226 }
227 return(0);
228}
229
230static inline int32_t sha256_vdone(struct sha256_vstate *md,uint8_t *out)
231{
232 int32_t i;
233 if ( md->curlen >= sizeof(md->buf) )
234 return(-1);
235 md->length += md->curlen * 8; // increase the length of the message
236 md->buf[md->curlen++] = (uint8_t)0x80; // append the '1' bit
237 // if len > 56 bytes we append zeros then compress. Then we can fall back to padding zeros and length encoding like normal.
238 if ( md->curlen > 56 )
239 {
240 while ( md->curlen < 64 )
241 md->buf[md->curlen++] = (uint8_t)0;
242 sha256_vcompress(md,md->buf);
243 md->curlen = 0;
244 }
245 while ( md->curlen < 56 ) // pad upto 56 bytes of zeroes
246 md->buf[md->curlen++] = (uint8_t)0;
247 STORE64H(md->length,md->buf+56); // store length
248 sha256_vcompress(md,md->buf);
249 for (i=0; i<8; i++) // copy output
250 STORE32H(md->state[i],out+(4*i));
251 return(0);
252}
253
254void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len)
255{
256 struct sha256_vstate md;
257 sha256_vinit(&md);
258 sha256_vprocess(&md,src,len);
259 sha256_vdone(&md,hash);
260}
261
262bits256 bits256_doublesha256(char *deprecated,uint8_t *data,int32_t datalen)
263{
264 bits256 hash,hash2; int32_t i;
265 vcalc_sha256(0,hash.bytes,data,datalen);
266 vcalc_sha256(0,hash2.bytes,hash.bytes,sizeof(hash));
267 for (i=0; i<sizeof(hash); i++)
268 hash.bytes[i] = hash2.bytes[sizeof(hash) - 1 - i];
269 return(hash);
270}
271
272// rmd160: the five basic functions F(), G() and H()
273#define F(x, y, z) ((x) ^ (y) ^ (z))
274#define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
275#define H(x, y, z) (((x) | ~(y)) ^ (z))
276#define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
277#define J(x, y, z) ((x) ^ ((y) | ~(z)))
278#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
279
280/* the ten basic operations FF() through III() */
281#define FF(a, b, c, d, e, x, s) \
282(a) += F((b), (c), (d)) + (x);\
283(a) = ROLc((a), (s)) + (e);\
284(c) = ROLc((c), 10);
285
286#define GG(a, b, c, d, e, x, s) \
287(a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\
288(a) = ROLc((a), (s)) + (e);\
289(c) = ROLc((c), 10);
290
291#define HH(a, b, c, d, e, x, s) \
292(a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\
293(a) = ROLc((a), (s)) + (e);\
294(c) = ROLc((c), 10);
295
296#define II(a, b, c, d, e, x, s) \
297(a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\
298(a) = ROLc((a), (s)) + (e);\
299(c) = ROLc((c), 10);
300
301#define JJ(a, b, c, d, e, x, s) \
302(a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\
303(a) = ROLc((a), (s)) + (e);\
304(c) = ROLc((c), 10);
305
306#define FFF(a, b, c, d, e, x, s) \
307(a) += F((b), (c), (d)) + (x);\
308(a) = ROLc((a), (s)) + (e);\
309(c) = ROLc((c), 10);
310
311#define GGG(a, b, c, d, e, x, s) \
312(a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\
313(a) = ROLc((a), (s)) + (e);\
314(c) = ROLc((c), 10);
315
316#define HHH(a, b, c, d, e, x, s) \
317(a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\
318(a) = ROLc((a), (s)) + (e);\
319(c) = ROLc((c), 10);
320
321#define III(a, b, c, d, e, x, s) \
322(a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\
323(a) = ROLc((a), (s)) + (e);\
324(c) = ROLc((c), 10);
325
326#define JJJ(a, b, c, d, e, x, s) \
327(a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\
328(a) = ROLc((a), (s)) + (e);\
329(c) = ROLc((c), 10);
330
331static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf)
332{
333 uint32_t aa,bb,cc,dd,ee,aaa,bbb,ccc,ddd,eee,X[16];
334 int i;
335
336 /* load words X */
337 for (i = 0; i < 16; i++){
338 LOAD32L(X[i], buf + (4 * i));
339 }
340
341 /* load state */
342 aa = aaa = md->state[0];
343 bb = bbb = md->state[1];
344 cc = ccc = md->state[2];
345 dd = ddd = md->state[3];
346 ee = eee = md->state[4];
347
348 /* round 1 */
349 FF(aa, bb, cc, dd, ee, X[ 0], 11);
350 FF(ee, aa, bb, cc, dd, X[ 1], 14);
351 FF(dd, ee, aa, bb, cc, X[ 2], 15);
352 FF(cc, dd, ee, aa, bb, X[ 3], 12);
353 FF(bb, cc, dd, ee, aa, X[ 4], 5);
354 FF(aa, bb, cc, dd, ee, X[ 5], 8);
355 FF(ee, aa, bb, cc, dd, X[ 6], 7);
356 FF(dd, ee, aa, bb, cc, X[ 7], 9);
357 FF(cc, dd, ee, aa, bb, X[ 8], 11);
358 FF(bb, cc, dd, ee, aa, X[ 9], 13);
359 FF(aa, bb, cc, dd, ee, X[10], 14);
360 FF(ee, aa, bb, cc, dd, X[11], 15);
361 FF(dd, ee, aa, bb, cc, X[12], 6);
362 FF(cc, dd, ee, aa, bb, X[13], 7);
363 FF(bb, cc, dd, ee, aa, X[14], 9);
364 FF(aa, bb, cc, dd, ee, X[15], 8);
365
366 /* round 2 */
367 GG(ee, aa, bb, cc, dd, X[ 7], 7);
368 GG(dd, ee, aa, bb, cc, X[ 4], 6);
369 GG(cc, dd, ee, aa, bb, X[13], 8);
370 GG(bb, cc, dd, ee, aa, X[ 1], 13);
371 GG(aa, bb, cc, dd, ee, X[10], 11);
372 GG(ee, aa, bb, cc, dd, X[ 6], 9);
373 GG(dd, ee, aa, bb, cc, X[15], 7);
374 GG(cc, dd, ee, aa, bb, X[ 3], 15);
375 GG(bb, cc, dd, ee, aa, X[12], 7);
376 GG(aa, bb, cc, dd, ee, X[ 0], 12);
377 GG(ee, aa, bb, cc, dd, X[ 9], 15);
378 GG(dd, ee, aa, bb, cc, X[ 5], 9);
379 GG(cc, dd, ee, aa, bb, X[ 2], 11);
380 GG(bb, cc, dd, ee, aa, X[14], 7);
381 GG(aa, bb, cc, dd, ee, X[11], 13);
382 GG(ee, aa, bb, cc, dd, X[ 8], 12);
383
384 /* round 3 */
385 HH(dd, ee, aa, bb, cc, X[ 3], 11);
386 HH(cc, dd, ee, aa, bb, X[10], 13);
387 HH(bb, cc, dd, ee, aa, X[14], 6);
388 HH(aa, bb, cc, dd, ee, X[ 4], 7);
389 HH(ee, aa, bb, cc, dd, X[ 9], 14);
390 HH(dd, ee, aa, bb, cc, X[15], 9);
391 HH(cc, dd, ee, aa, bb, X[ 8], 13);
392 HH(bb, cc, dd, ee, aa, X[ 1], 15);
393 HH(aa, bb, cc, dd, ee, X[ 2], 14);
394 HH(ee, aa, bb, cc, dd, X[ 7], 8);
395 HH(dd, ee, aa, bb, cc, X[ 0], 13);
396 HH(cc, dd, ee, aa, bb, X[ 6], 6);
397 HH(bb, cc, dd, ee, aa, X[13], 5);
398 HH(aa, bb, cc, dd, ee, X[11], 12);
399 HH(ee, aa, bb, cc, dd, X[ 5], 7);
400 HH(dd, ee, aa, bb, cc, X[12], 5);
401
402 /* round 4 */
403 II(cc, dd, ee, aa, bb, X[ 1], 11);
404 II(bb, cc, dd, ee, aa, X[ 9], 12);
405 II(aa, bb, cc, dd, ee, X[11], 14);
406 II(ee, aa, bb, cc, dd, X[10], 15);
407 II(dd, ee, aa, bb, cc, X[ 0], 14);
408 II(cc, dd, ee, aa, bb, X[ 8], 15);
409 II(bb, cc, dd, ee, aa, X[12], 9);
410 II(aa, bb, cc, dd, ee, X[ 4], 8);
411 II(ee, aa, bb, cc, dd, X[13], 9);
412 II(dd, ee, aa, bb, cc, X[ 3], 14);
413 II(cc, dd, ee, aa, bb, X[ 7], 5);
414 II(bb, cc, dd, ee, aa, X[15], 6);
415 II(aa, bb, cc, dd, ee, X[14], 8);
416 II(ee, aa, bb, cc, dd, X[ 5], 6);
417 II(dd, ee, aa, bb, cc, X[ 6], 5);
418 II(cc, dd, ee, aa, bb, X[ 2], 12);
419
420 /* round 5 */
421 JJ(bb, cc, dd, ee, aa, X[ 4], 9);
422 JJ(aa, bb, cc, dd, ee, X[ 0], 15);
423 JJ(ee, aa, bb, cc, dd, X[ 5], 5);
424 JJ(dd, ee, aa, bb, cc, X[ 9], 11);
425 JJ(cc, dd, ee, aa, bb, X[ 7], 6);
426 JJ(bb, cc, dd, ee, aa, X[12], 8);
427 JJ(aa, bb, cc, dd, ee, X[ 2], 13);
428 JJ(ee, aa, bb, cc, dd, X[10], 12);
429 JJ(dd, ee, aa, bb, cc, X[14], 5);
430 JJ(cc, dd, ee, aa, bb, X[ 1], 12);
431 JJ(bb, cc, dd, ee, aa, X[ 3], 13);
432 JJ(aa, bb, cc, dd, ee, X[ 8], 14);
433 JJ(ee, aa, bb, cc, dd, X[11], 11);
434 JJ(dd, ee, aa, bb, cc, X[ 6], 8);
435 JJ(cc, dd, ee, aa, bb, X[15], 5);
436 JJ(bb, cc, dd, ee, aa, X[13], 6);
437
438 /* parallel round 1 */
439 JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8);
440 JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9);
441 JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9);
442 JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11);
443 JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13);
444 JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15);
445 JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15);
446 JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5);
447 JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7);
448 JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7);
449 JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8);
450 JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11);
451 JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14);
452 JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
453 JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12);
454 JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6);
455
456 /* parallel round 2 */
457 III(eee, aaa, bbb, ccc, ddd, X[ 6], 9);
458 III(ddd, eee, aaa, bbb, ccc, X[11], 13);
459 III(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
460 III(bbb, ccc, ddd, eee, aaa, X[ 7], 7);
461 III(aaa, bbb, ccc, ddd, eee, X[ 0], 12);
462 III(eee, aaa, bbb, ccc, ddd, X[13], 8);
463 III(ddd, eee, aaa, bbb, ccc, X[ 5], 9);
464 III(ccc, ddd, eee, aaa, bbb, X[10], 11);
465 III(bbb, ccc, ddd, eee, aaa, X[14], 7);
466 III(aaa, bbb, ccc, ddd, eee, X[15], 7);
467 III(eee, aaa, bbb, ccc, ddd, X[ 8], 12);
468 III(ddd, eee, aaa, bbb, ccc, X[12], 7);
469 III(ccc, ddd, eee, aaa, bbb, X[ 4], 6);
470 III(bbb, ccc, ddd, eee, aaa, X[ 9], 15);
471 III(aaa, bbb, ccc, ddd, eee, X[ 1], 13);
472 III(eee, aaa, bbb, ccc, ddd, X[ 2], 11);
473
474 /* parallel round 3 */
475 HHH(ddd, eee, aaa, bbb, ccc, X[15], 9);
476 HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7);
477 HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15);
478 HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11);
479 HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8);
480 HHH(ddd, eee, aaa, bbb, ccc, X[14], 6);
481 HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6);
482 HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14);
483 HHH(aaa, bbb, ccc, ddd, eee, X[11], 12);
484 HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13);
485 HHH(ddd, eee, aaa, bbb, ccc, X[12], 5);
486 HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14);
487 HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
488 HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13);
489 HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7);
490 HHH(ddd, eee, aaa, bbb, ccc, X[13], 5);
491
492 /* parallel round 4 */
493 GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
494 GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5);
495 GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8);
496 GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11);
497 GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14);
498 GGG(ccc, ddd, eee, aaa, bbb, X[11], 14);
499 GGG(bbb, ccc, ddd, eee, aaa, X[15], 6);
500 GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14);
501 GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6);
502 GGG(ddd, eee, aaa, bbb, ccc, X[12], 9);
503 GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12);
504 GGG(bbb, ccc, ddd, eee, aaa, X[13], 9);
505 GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12);
506 GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5);
507 GGG(ddd, eee, aaa, bbb, ccc, X[10], 15);
508 GGG(ccc, ddd, eee, aaa, bbb, X[14], 8);
509
510 /* parallel round 5 */
511 FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8);
512 FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5);
513 FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12);
514 FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9);
515 FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12);
516 FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5);
517 FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14);
518 FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6);
519 FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8);
520 FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13);
521 FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6);
522 FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5);
523 FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15);
524 FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13);
525 FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11);
526 FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11);
527
528 /* combine results */
529 ddd += cc + md->state[1]; /* final result for md->state[0] */
530 md->state[1] = md->state[2] + dd + eee;
531 md->state[2] = md->state[3] + ee + aaa;
532 md->state[3] = md->state[4] + aa + bbb;
533 md->state[4] = md->state[0] + bb + ccc;
534 md->state[0] = ddd;
535
536 return 0;
537}
538
539/**
540 Initialize the hash state
541 @param md The hash state you wish to initialize
542 @return 0 if successful
543 */
544int rmd160_vinit(struct rmd160_vstate * md)
545{
546 md->state[0] = 0x67452301UL;
547 md->state[1] = 0xefcdab89UL;
548 md->state[2] = 0x98badcfeUL;
549 md->state[3] = 0x10325476UL;
550 md->state[4] = 0xc3d2e1f0UL;
551 md->curlen = 0;
552 md->length = 0;
553 return 0;
554}
555#define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
556int func_name (struct rmd160_vstate * md, const unsigned char *in, unsigned long inlen) \
557{ \
558unsigned long n; \
559int err; \
560if (md->curlen > sizeof(md->buf)) { \
561return -1; \
562} \
563while (inlen > 0) { \
564if (md->curlen == 0 && inlen >= block_size) { \
565if ((err = compress_name (md, (unsigned char *)in)) != 0) { \
566return err; \
567} \
568md->length += block_size * 8; \
569in += block_size; \
570inlen -= block_size; \
571} else { \
572n = MIN(inlen, (block_size - md->curlen)); \
573memcpy(md->buf + md->curlen, in, (size_t)n); \
574md->curlen += n; \
575in += n; \
576inlen -= n; \
577if (md->curlen == block_size) { \
578if ((err = compress_name (md, md->buf)) != 0) { \
579return err; \
580} \
581md->length += 8*block_size; \
582md->curlen = 0; \
583} \
584} \
585} \
586return 0; \
587}
588
589/**
590 Process a block of memory though the hash
591 @param md The hash state
592 @param in The data to hash
593 @param inlen The length of the data (octets)
594 @return 0 if successful
595 */
596HASH_PROCESS(rmd160_vprocess, rmd160_vcompress, rmd160, 64)
597
598/**
599 Terminate the hash to get the digest
600 @param md The hash state
601 @param out [out] The destination of the hash (20 bytes)
602 @return 0 if successful
603 */
604int rmd160_vdone(struct rmd160_vstate * md, unsigned char *out)
605{
606 int i;
607 if (md->curlen >= sizeof(md->buf)) {
608 return -1;
609 }
610 /* increase the length of the message */
611 md->length += md->curlen * 8;
612
613 /* append the '1' bit */
614 md->buf[md->curlen++] = (unsigned char)0x80;
615
616 /* if the length is currently above 56 bytes we append zeros
617 * then compress. Then we can fall back to padding zeros and length
618 * encoding like normal.
619 */
620 if (md->curlen > 56) {
621 while (md->curlen < 64) {
622 md->buf[md->curlen++] = (unsigned char)0;
623 }
624 rmd160_vcompress(md, md->buf);
625 md->curlen = 0;
626 }
627 /* pad upto 56 bytes of zeroes */
628 while (md->curlen < 56) {
629 md->buf[md->curlen++] = (unsigned char)0;
630 }
631 /* store length */
632 STORE64L(md->length, md->buf+56);
633 rmd160_vcompress(md, md->buf);
634 /* copy output */
635 for (i = 0; i < 5; i++) {
636 STORE32L(md->state[i], out+(4*i));
637 }
638 return 0;
639}
640
641void calc_rmd160(char deprecated[41],uint8_t buf[20],uint8_t *msg,int32_t len)
642{
643 struct rmd160_vstate md;
644 rmd160_vinit(&md);
645 rmd160_vprocess(&md,msg,len);
646 rmd160_vdone(&md, buf);
647}
3eea72f2 648
1e81ccb7 649static const uint32_t crc32_tab[] = {
650 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
651 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
652 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
653 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
654 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
655 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
656 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
657 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
658 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
659 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
660 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
661 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
662 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
663 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
664 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
665 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
666 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
667 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
668 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
669 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
670 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
671 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
672 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
673 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
674 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
675 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
676 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
677 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
678 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
679 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
680 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
681 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
682 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
683 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
684 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
685 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
686 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
687 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
688 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
689 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
690 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
691 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
692 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
693};
694
695uint32_t calc_crc32(uint32_t crc,const void *buf,size_t size)
696{
697 const uint8_t *p;
698
699 p = (const uint8_t *)buf;
700 crc = crc ^ ~0U;
701
702 while (size--)
703 crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
704
705 return crc ^ ~0U;
706}
707
d019c447 708void calc_rmd160_sha256(uint8_t rmd160[20],uint8_t *data,int32_t datalen)
709{
710 bits256 hash;
711 vcalc_sha256(0,hash.bytes,data,datalen);
712 calc_rmd160(0,rmd160,hash.bytes,sizeof(hash));
713}
714
715int32_t bitcoin_addr2rmd160(uint8_t *addrtypep,uint8_t rmd160[20],char *coinaddr)
716{
717 bits256 hash; uint8_t *buf,_buf[25]; int32_t len;
718 memset(rmd160,0,20);
719 *addrtypep = 0;
720 buf = _buf;
721 if ( (len= bitcoin_base58decode(buf,coinaddr)) >= 4 )
722 {
723 // validate with trailing hash, then remove hash
724 hash = bits256_doublesha256(0,buf,21);
725 *addrtypep = *buf;
726 memcpy(rmd160,buf+1,20);
727 if ( (buf[21]&0xff) == hash.bytes[31] && (buf[22]&0xff) == hash.bytes[30] &&(buf[23]&0xff) == hash.bytes[29] && (buf[24]&0xff) == hash.bytes[28] )
728 {
729 //printf("coinaddr.(%s) valid checksum addrtype.%02x\n",coinaddr,*addrtypep);
730 return(20);
731 }
732 else
733 {
734 int32_t i;
735 if ( len > 20 )
736 {
737 hash = bits256_doublesha256(0,buf,len);
738 }
739 for (i=0; i<len; i++)
740 printf("%02x ",buf[i]);
7637aa7f 741 printf("\nhex checkhash.(%s) len.%d mismatch %02x %02x %02x %02x vs %02x %02x %02x %02x\n",coinaddr,len,buf[len-1]&0xff,buf[len-2]&0xff,buf[len-3]&0xff,buf[len-4]&0xff,hash.bytes[31],hash.bytes[30],hash.bytes[29],hash.bytes[28]);
d019c447 742 }
743 }
744 return(0);
745}
746
747char *bitcoin_address(char *coinaddr,uint8_t addrtype,uint8_t *pubkey_or_rmd160,int32_t len)
748{
749 int32_t i; uint8_t data[25]; bits256 hash;// char checkaddr[65];
750 if ( len != 20 )
751 calc_rmd160_sha256(data+1,pubkey_or_rmd160,len);
752 else memcpy(data+1,pubkey_or_rmd160,20);
753 //btc_convrmd160(checkaddr,addrtype,data+1);
754 data[0] = addrtype;
755 hash = bits256_doublesha256(0,data,21);
756 for (i=0; i<4; i++)
757 data[21+i] = hash.bytes[31-i];
758 if ( (coinaddr= bitcoin_base58encode(coinaddr,data,25)) != 0 )
759 {
760 //uint8_t checktype,rmd160[20];
761 //bitcoin_addr2rmd160(&checktype,rmd160,coinaddr);
762 //if ( strcmp(checkaddr,coinaddr) != 0 )
763 // printf("checkaddr.(%s) vs coinaddr.(%s) %02x vs [%02x] memcmp.%d\n",checkaddr,coinaddr,addrtype,checktype,memcmp(rmd160,data+1,20));
764 }
765 return(coinaddr);
766}
767
1e81ccb7 768int32_t _unhex(char c)
769{
770 if ( c >= '0' && c <= '9' )
771 return(c - '0');
772 else if ( c >= 'a' && c <= 'f' )
773 return(c - 'a' + 10);
774 else if ( c >= 'A' && c <= 'F' )
775 return(c - 'A' + 10);
776 return(-1);
777}
778
779int32_t is_hexstr(char *str,int32_t n)
780{
781 int32_t i;
782 if ( str == 0 || str[0] == 0 )
783 return(0);
784 for (i=0; str[i]!=0; i++)
785 {
786 if ( n > 0 && i >= n )
787 break;
788 if ( _unhex(str[i]) < 0 )
789 break;
790 }
791 if ( n == 0 )
792 return(i);
793 return(i == n);
794}
795
796int32_t unhex(char c)
797{
798 int32_t hex;
799 if ( (hex= _unhex(c)) < 0 )
800 {
801 //printf("unhex: illegal hexchar.(%c)\n",c);
802 }
803 return(hex);
804}
805
806unsigned char _decode_hex(char *hex) { return((unhex(hex[0])<<4) | unhex(hex[1])); }
807
808int32_t decode_hex(uint8_t *bytes,int32_t n,char *hex)
809{
810 int32_t adjust,i = 0;
811 //printf("decode.(%s)\n",hex);
812 if ( is_hexstr(hex,n) == 0 )
813 {
814 memset(bytes,0,n);
815 return(n);
816 }
817 if ( n == 0 || (hex[n*2+1] == 0 && hex[n*2] != 0) )
818 {
819 if ( n > 0 )
820 {
821 bytes[0] = unhex(hex[0]);
822 printf("decode_hex n.%d hex[0] (%c) -> %d hex.(%s) [n*2+1: %d] [n*2: %d %c] len.%ld\n",n,hex[0],bytes[0],hex,hex[n*2+1],hex[n*2],hex[n*2],(long)strlen(hex));
823 }
824 bytes++;
825 hex++;
826 adjust = 1;
827 } else adjust = 0;
828 if ( n > 0 )
829 {
830 for (i=0; i<n; i++)
831 bytes[i] = _decode_hex(&hex[i*2]);
832 }
833 //bytes[i] = 0;
834 return(n + adjust);
835}
836
32b1b443 837char hexbyte(int32_t c)
838{
839 c &= 0xf;
840 if ( c < 10 )
841 return('0'+c);
842 else if ( c < 16 )
843 return('a'+c-10);
844 else return(0);
845}
846
847int32_t init_hexbytes_noT(char *hexbytes,unsigned char *message,long len)
848{
849 int32_t i;
850 if ( len <= 0 )
851 {
852 hexbytes[0] = 0;
853 return(1);
854 }
855 for (i=0; i<len; i++)
856 {
857 hexbytes[i*2] = hexbyte((message[i]>>4) & 0xf);
858 hexbytes[i*2 + 1] = hexbyte(message[i] & 0xf);
859 //printf("i.%d (%02x) [%c%c]\n",i,message[i],hexbytes[i*2],hexbytes[i*2+1]);
860 }
861 hexbytes[len*2] = 0;
862 //printf("len.%ld\n",len*2+1);
863 return((int32_t)len*2+1);
864}
865
866char *bits256_str(char hexstr[65],bits256 x)
867{
868 init_hexbytes_noT(hexstr,x.bytes,sizeof(x));
869 return(hexstr);
870}
871
1e81ccb7 872int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp)
873{
874 int32_t i; uint64_t x;
875 if ( rwflag == 0 )
876 {
877 x = 0;
878 for (i=len-1; i>=0; i--)
879 {
880 x <<= 8;
881 x |= serialized[i];
882 }
883 switch ( len )
884 {
885 case 1: *(uint8_t *)endianedp = (uint8_t)x; break;
886 case 2: *(uint16_t *)endianedp = (uint16_t)x; break;
887 case 4: *(uint32_t *)endianedp = (uint32_t)x; break;
888 case 8: *(uint64_t *)endianedp = (uint64_t)x; break;
889 }
890 }
891 else
892 {
893 x = 0;
894 switch ( len )
895 {
896 case 1: x = *(uint8_t *)endianedp; break;
897 case 2: x = *(uint16_t *)endianedp; break;
898 case 4: x = *(uint32_t *)endianedp; break;
899 case 8: x = *(uint64_t *)endianedp; break;
900 }
901 for (i=0; i<len; i++,x >>= 8)
902 serialized[i] = (uint8_t)(x & 0xff);
903 }
904 return(len);
905}
906
907int32_t iguana_rwbignum(int32_t rwflag,uint8_t *serialized,int32_t len,uint8_t *endianedp)
908{
909 int32_t i;
910 if ( rwflag == 0 )
911 {
912 for (i=0; i<len; i++)
913 endianedp[i] = serialized[i];
914 }
915 else
916 {
917 for (i=0; i<len; i++)
918 serialized[i] = endianedp[i];
919 }
920 return(len);
921}
922
923int32_t komodo_opreturnscript(uint8_t *script,uint8_t type,uint8_t *opret,int32_t opretlen)
924{
925 int32_t offset = 0;
926 script[offset++] = 0x6a;
927 opretlen++;
928 if ( opretlen >= 0x4c )
929 {
930 if ( opretlen > 0xff )
931 {
932 script[offset++] = 0x4d;
933 script[offset++] = opretlen & 0xff;
934 script[offset++] = (opretlen >> 8) & 0xff;
935 }
936 else
937 {
938 script[offset++] = 0x4c;
939 script[offset++] = opretlen;
940 }
941 } else script[offset++] = opretlen;
4aa2c64e 942 script[offset] = type; // covered by opretlen
1e81ccb7 943 memcpy(&script[offset],opret,opretlen);
944 return(opretlen + offset);
945}
438d3f35 946
fd1799a6 947long _stripwhite(char *buf,int accept)
948{
949 int32_t i,j,c;
950 if ( buf == 0 || buf[0] == 0 )
951 return(0);
952 for (i=j=0; buf[i]!=0; i++)
953 {
954 buf[j] = c = buf[i];
955 if ( c == accept || (c != ' ' && c != '\n' && c != '\r' && c != '\t' && c != '\b') )
956 j++;
957 }
958 buf[j] = 0;
959 return(j);
960}
961
2c36c760 962char *clonestr(char *str)
963{
964 char *clone;
965 if ( str == 0 || str[0] == 0 )
966 {
967 printf("warning cloning nullstr.%p\n",str);
968#ifdef __APPLE__
969 while ( 1 ) sleep(1);
970#endif
971 str = (char *)"<nullstr>";
972 }
973 clone = (char *)malloc(strlen(str)+16);
974 strcpy(clone,str);
975 return(clone);
976}
977
fd1799a6 978char *parse_conf_line(char *line,char *field)
979{
980 line += strlen(field);
981 for (; *line!='='&&*line!=0; line++)
982 break;
983 if ( *line == 0 )
984 return(0);
985 if ( *line == '=' )
986 line++;
83c2d095 987 while ( line[strlen(line)-1] == '\r' || line[strlen(line)-1] == '\n' || line[strlen(line)-1] == ' ' )
5ae7efc1 988 line[strlen(line)-1] = 0;
e78fb5d3 989 //printf("LINE.(%s)\n",line);
fd1799a6 990 _stripwhite(line,0);
991 return(clonestr(line));
992}
993
438d3f35 994void komodo_configfile(char *symbol,uint16_t port)
995{
62b751ab 996 FILE *fp; char fname[512],buf[128],line[4096],*str,*rpcuser,*rpcpassword;
1e2b1c77 997 srand((uint32_t)time(NULL));
438d3f35 998 sprintf(buf,"%s.conf",symbol);
fd1799a6 999 BITCOIND_PORT = port;
438d3f35 1000#ifdef WIN32
1001 sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),buf);
1002#else
1003 sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),buf);
1004#endif
1005 if ( (fp= fopen(fname,"rb")) == 0 )
1006 {
1007 if ( (fp= fopen(fname,"wb")) != 0 )
1008 {
c0160cc3 1009 fprintf(fp,"rpcuser=user%u\nrpcpassword=pass%u\nrpcport=%u\nserver=1\ntxindex=1\n",rand(),rand(),port);
438d3f35 1010 fclose(fp);
1011 printf("Created (%s)\n",fname);
1012 }
1013 }
fd1799a6 1014 else
1015 {
fd1799a6 1016 fclose(fp);
46ff7513 1017 strcpy(fname,GetDataDir(false).string().c_str());
1018#ifdef WIN32
1019 while ( fname[strlen(fname)-1] != '\\' )
1020 fname[strlen(fname)-1] = 0;
1021 strcat(fname,".komodo/komodo.conf");
1022#else
1023 while ( fname[strlen(fname)-1] != '/' )
1024 fname[strlen(fname)-1] = 0;
1025 strcat(fname,".komodo/komodo.conf");
1026#endif
62b751ab 1027 printf("KOMODO.(%s)\n",fname);
46ff7513 1028 if ( (fp= fopen(fname,"rb")) != 0 )
1029 {
62b751ab 1030 rpcuser = rpcpassword = 0;
46ff7513 1031 while ( fgets(line,sizeof(line),fp) != 0 )
1032 {
1033 if ( line[0] == '#' )
1034 continue;
1035 //printf("line.(%s) %p %p\n",line,strstr(line,(char *)"rpcuser"),strstr(line,(char *)"rpcpassword"));
1036 if ( (str= strstr(line,(char *)"rpcuser")) != 0 )
1037 rpcuser = parse_conf_line(str,(char *)"rpcuser");
1038 else if ( (str= strstr(line,(char *)"rpcpassword")) != 0 )
1039 rpcpassword = parse_conf_line(str,(char *)"rpcpassword");
46ff7513 1040 }
1041 if ( rpcuser != 0 && rpcpassword != 0 )
1042 {
1043 sprintf(USERPASS,"%s:%s",rpcuser,rpcpassword);
1044 }
62b751ab 1045 printf("rpcuser.(%s) rpcpassword.(%s) USERPASS.(%s) %u\n",rpcuser,rpcpassword,USERPASS,port);
46ff7513 1046 if ( rpcuser != 0 )
1047 free(rpcuser);
1048 if ( rpcpassword != 0 )
1049 free(rpcpassword);
1050 fclose(fp);
1051 } else printf("couldnt open.(%s)\n",fname);
fd1799a6 1052 }
438d3f35 1053}
62e09770 1054
1055double OS_milliseconds()
1056{
1057 struct timeval tv; double millis;
1058 gettimeofday(&tv,NULL);
1059 millis = ((double)tv.tv_sec * 1000. + (double)tv.tv_usec / 1000.);
1060 //printf("tv_sec.%ld usec.%d %f\n",tv.tv_sec,tv.tv_usec,millis);
1061 return(millis);
1062}
This page took 0.14181 seconds and 4 git commands to generate.