]>
Commit | Line | Data |
---|---|---|
7e907fdd | 1 | // |
2 | // main.c | |
3 | // spawn | |
4 | // | |
5 | // Created by Mac on 4/7/18. | |
6 | // Copyright © 2018 SuperNET. All rights reserved. | |
7 | // | |
8 | ||
9 | #include <stdio.h> | |
10 | #include <stdint.h> | |
11 | #include <stdlib.h> | |
12 | #include <memory.h> | |
13 | ||
14 | uint64_t ASSETCHAINS_COMMISSION; | |
15 | uint32_t ASSETCHAINS_MAGIC = 2387029918; | |
16 | uint8_t ASSETCHAINS_OVERRIDE_PUBKEY33[33]; | |
17 | ||
18 | struct sha256_vstate { uint64_t length; uint32_t state[8],curlen; uint8_t buf[64]; }; | |
19 | struct rmd160_vstate { uint64_t length; uint8_t buf[64]; uint32_t curlen, state[5]; }; | |
20 | union _bits256 { uint8_t bytes[32]; uint16_t ushorts[16]; uint32_t uints[8]; uint64_t ulongs[4]; uint64_t txid; }; | |
21 | typedef union _bits256 bits256; | |
22 | ||
23 | // following is ported from libtom | |
24 | ||
25 | #define STORE32L(x, y) \ | |
26 | { (y)[3] = (uint8_t)(((x)>>24)&255); (y)[2] = (uint8_t)(((x)>>16)&255); \ | |
27 | (y)[1] = (uint8_t)(((x)>>8)&255); (y)[0] = (uint8_t)((x)&255); } | |
28 | ||
29 | #define LOAD32L(x, y) \ | |
30 | { x = (uint32_t)(((uint64_t)((y)[3] & 255)<<24) | \ | |
31 | ((uint32_t)((y)[2] & 255)<<16) | \ | |
32 | ((uint32_t)((y)[1] & 255)<<8) | \ | |
33 | ((uint32_t)((y)[0] & 255))); } | |
34 | ||
35 | #define STORE64L(x, y) \ | |
36 | { (y)[7] = (uint8_t)(((x)>>56)&255); (y)[6] = (uint8_t)(((x)>>48)&255); \ | |
37 | (y)[5] = (uint8_t)(((x)>>40)&255); (y)[4] = (uint8_t)(((x)>>32)&255); \ | |
38 | (y)[3] = (uint8_t)(((x)>>24)&255); (y)[2] = (uint8_t)(((x)>>16)&255); \ | |
39 | (y)[1] = (uint8_t)(((x)>>8)&255); (y)[0] = (uint8_t)((x)&255); } | |
40 | ||
41 | #define LOAD64L(x, y) \ | |
42 | { x = (((uint64_t)((y)[7] & 255))<<56)|(((uint64_t)((y)[6] & 255))<<48)| \ | |
43 | (((uint64_t)((y)[5] & 255))<<40)|(((uint64_t)((y)[4] & 255))<<32)| \ | |
44 | (((uint64_t)((y)[3] & 255))<<24)|(((uint64_t)((y)[2] & 255))<<16)| \ | |
45 | (((uint64_t)((y)[1] & 255))<<8)|(((uint64_t)((y)[0] & 255))); } | |
46 | ||
47 | #define STORE32H(x, y) \ | |
48 | { (y)[0] = (uint8_t)(((x)>>24)&255); (y)[1] = (uint8_t)(((x)>>16)&255); \ | |
49 | (y)[2] = (uint8_t)(((x)>>8)&255); (y)[3] = (uint8_t)((x)&255); } | |
50 | ||
51 | #define LOAD32H(x, y) \ | |
52 | { x = (uint32_t)(((uint64_t)((y)[0] & 255)<<24) | \ | |
53 | ((uint32_t)((y)[1] & 255)<<16) | \ | |
54 | ((uint32_t)((y)[2] & 255)<<8) | \ | |
55 | ((uint32_t)((y)[3] & 255))); } | |
56 | ||
57 | #define STORE64H(x, y) \ | |
58 | { (y)[0] = (uint8_t)(((x)>>56)&255); (y)[1] = (uint8_t)(((x)>>48)&255); \ | |
59 | (y)[2] = (uint8_t)(((x)>>40)&255); (y)[3] = (uint8_t)(((x)>>32)&255); \ | |
60 | (y)[4] = (uint8_t)(((x)>>24)&255); (y)[5] = (uint8_t)(((x)>>16)&255); \ | |
61 | (y)[6] = (uint8_t)(((x)>>8)&255); (y)[7] = (uint8_t)((x)&255); } | |
62 | ||
63 | #define LOAD64H(x, y) \ | |
64 | { x = (((uint64_t)((y)[0] & 255))<<56)|(((uint64_t)((y)[1] & 255))<<48) | \ | |
65 | (((uint64_t)((y)[2] & 255))<<40)|(((uint64_t)((y)[3] & 255))<<32) | \ | |
66 | (((uint64_t)((y)[4] & 255))<<24)|(((uint64_t)((y)[5] & 255))<<16) | \ | |
67 | (((uint64_t)((y)[6] & 255))<<8)|(((uint64_t)((y)[7] & 255))); } | |
68 | ||
69 | // Various logical functions | |
70 | #define RORc(x, y) ( ((((uint32_t)(x)&0xFFFFFFFFUL)>>(uint32_t)((y)&31)) | ((uint32_t)(x)<<(uint32_t)(32-((y)&31)))) & 0xFFFFFFFFUL) | |
71 | #define Ch(x,y,z) (z ^ (x & (y ^ z))) | |
72 | #define Maj(x,y,z) (((x | y) & z) | (x & y)) | |
73 | #define S(x, n) RORc((x),(n)) | |
74 | #define R(x, n) (((x)&0xFFFFFFFFUL)>>(n)) | |
75 | #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22)) | |
76 | #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25)) | |
77 | #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3)) | |
78 | #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10)) | |
79 | #define MIN(x, y) ( ((x)<(y))?(x):(y) ) | |
80 | ||
81 | static inline int32_t sha256_vcompress(struct sha256_vstate * md,uint8_t *buf) | |
82 | { | |
83 | uint32_t S[8],W[64],t0,t1,i; | |
84 | for (i=0; i<8; i++) // copy state into S | |
85 | S[i] = md->state[i]; | |
86 | for (i=0; i<16; i++) // copy the state into 512-bits into W[0..15] | |
87 | LOAD32H(W[i],buf + (4*i)); | |
88 | for (i=16; i<64; i++) // fill W[16..63] | |
89 | W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; | |
90 | ||
91 | #define RND(a,b,c,d,e,f,g,h,i,ki) \ | |
92 | t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ | |
93 | t1 = Sigma0(a) + Maj(a, b, c); \ | |
94 | d += t0; \ | |
95 | h = t0 + t1; | |
96 | ||
97 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98); | |
98 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491); | |
99 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf); | |
100 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5); | |
101 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b); | |
102 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1); | |
103 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4); | |
104 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5); | |
105 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98); | |
106 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01); | |
107 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be); | |
108 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3); | |
109 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74); | |
110 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe); | |
111 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7); | |
112 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174); | |
113 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1); | |
114 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786); | |
115 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6); | |
116 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc); | |
117 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f); | |
118 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa); | |
119 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc); | |
120 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da); | |
121 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152); | |
122 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d); | |
123 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8); | |
124 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7); | |
125 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3); | |
126 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147); | |
127 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351); | |
128 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967); | |
129 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85); | |
130 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138); | |
131 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc); | |
132 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13); | |
133 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354); | |
134 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb); | |
135 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e); | |
136 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85); | |
137 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1); | |
138 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b); | |
139 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70); | |
140 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3); | |
141 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819); | |
142 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624); | |
143 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585); | |
144 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070); | |
145 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116); | |
146 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08); | |
147 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c); | |
148 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5); | |
149 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3); | |
150 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a); | |
151 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f); | |
152 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3); | |
153 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee); | |
154 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f); | |
155 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814); | |
156 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208); | |
157 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa); | |
158 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb); | |
159 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7); | |
160 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2); | |
161 | #undef RND | |
162 | for (i=0; i<8; i++) // feedback | |
163 | md->state[i] = md->state[i] + S[i]; | |
164 | return(0); | |
165 | } | |
166 | ||
167 | #undef RORc | |
168 | #undef Ch | |
169 | #undef Maj | |
170 | #undef S | |
171 | #undef R | |
172 | #undef Sigma0 | |
173 | #undef Sigma1 | |
174 | #undef Gamma0 | |
175 | #undef Gamma1 | |
176 | ||
177 | static inline void sha256_vinit(struct sha256_vstate * md) | |
178 | { | |
179 | md->curlen = 0; | |
180 | md->length = 0; | |
181 | md->state[0] = 0x6A09E667UL; | |
182 | md->state[1] = 0xBB67AE85UL; | |
183 | md->state[2] = 0x3C6EF372UL; | |
184 | md->state[3] = 0xA54FF53AUL; | |
185 | md->state[4] = 0x510E527FUL; | |
186 | md->state[5] = 0x9B05688CUL; | |
187 | md->state[6] = 0x1F83D9ABUL; | |
188 | md->state[7] = 0x5BE0CD19UL; | |
189 | } | |
190 | ||
191 | static inline int32_t sha256_vprocess(struct sha256_vstate *md,const uint8_t *in,uint64_t inlen) | |
192 | { | |
193 | uint64_t n; int32_t err; | |
194 | if ( md->curlen > sizeof(md->buf) ) | |
195 | return(-1); | |
196 | while ( inlen > 0 ) | |
197 | { | |
198 | if ( md->curlen == 0 && inlen >= 64 ) | |
199 | { | |
200 | if ( (err= sha256_vcompress(md,(uint8_t *)in)) != 0 ) | |
201 | return(err); | |
202 | md->length += 64 * 8, in += 64, inlen -= 64; | |
203 | } | |
204 | else | |
205 | { | |
206 | n = MIN(inlen,64 - md->curlen); | |
207 | memcpy(md->buf + md->curlen,in,(size_t)n); | |
208 | md->curlen += n, in += n, inlen -= n; | |
209 | if ( md->curlen == 64 ) | |
210 | { | |
211 | if ( (err= sha256_vcompress(md,md->buf)) != 0 ) | |
212 | return(err); | |
213 | md->length += 8*64; | |
214 | md->curlen = 0; | |
215 | } | |
216 | } | |
217 | } | |
218 | return(0); | |
219 | } | |
220 | ||
221 | static inline int32_t sha256_vdone(struct sha256_vstate *md,uint8_t *out) | |
222 | { | |
223 | int32_t i; | |
224 | if ( md->curlen >= sizeof(md->buf) ) | |
225 | return(-1); | |
226 | md->length += md->curlen * 8; // increase the length of the message | |
227 | md->buf[md->curlen++] = (uint8_t)0x80; // append the '1' bit | |
228 | // if len > 56 bytes we append zeros then compress. Then we can fall back to padding zeros and length encoding like normal. | |
229 | if ( md->curlen > 56 ) | |
230 | { | |
231 | while ( md->curlen < 64 ) | |
232 | md->buf[md->curlen++] = (uint8_t)0; | |
233 | sha256_vcompress(md,md->buf); | |
234 | md->curlen = 0; | |
235 | } | |
236 | while ( md->curlen < 56 ) // pad upto 56 bytes of zeroes | |
237 | md->buf[md->curlen++] = (uint8_t)0; | |
238 | STORE64H(md->length,md->buf+56); // store length | |
239 | sha256_vcompress(md,md->buf); | |
240 | for (i=0; i<8; i++) // copy output | |
241 | STORE32H(md->state[i],out+(4*i)); | |
242 | return(0); | |
243 | } | |
244 | ||
245 | void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len) | |
246 | { | |
247 | struct sha256_vstate md; | |
248 | sha256_vinit(&md); | |
249 | sha256_vprocess(&md,src,len); | |
250 | sha256_vdone(&md,hash); | |
251 | } | |
252 | ||
253 | bits256 bits256_doublesha256(char *deprecated,uint8_t *data,int32_t datalen) | |
254 | { | |
255 | bits256 hash,hash2; int32_t i; | |
256 | vcalc_sha256(0,hash.bytes,data,datalen); | |
257 | vcalc_sha256(0,hash2.bytes,hash.bytes,sizeof(hash)); | |
258 | for (i=0; i<sizeof(hash); i++) | |
259 | hash.bytes[i] = hash2.bytes[sizeof(hash) - 1 - i]; | |
260 | return(hash); | |
261 | } | |
262 | ||
263 | ||
264 | // rmd160: the five basic functions F(), G() and H() | |
265 | #define F(x, y, z) ((x) ^ (y) ^ (z)) | |
266 | #define G(x, y, z) (((x) & (y)) | (~(x) & (z))) | |
267 | #define H(x, y, z) (((x) | ~(y)) ^ (z)) | |
268 | #define I(x, y, z) (((x) & (z)) | ((y) & ~(z))) | |
269 | #define J(x, y, z) ((x) ^ ((y) | ~(z))) | |
270 | #define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) | |
271 | ||
272 | /* the ten basic operations FF() through III() */ | |
273 | #define FF(a, b, c, d, e, x, s) \ | |
274 | (a) += F((b), (c), (d)) + (x);\ | |
275 | (a) = ROLc((a), (s)) + (e);\ | |
276 | (c) = ROLc((c), 10); | |
277 | ||
278 | #define GG(a, b, c, d, e, x, s) \ | |
279 | (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\ | |
280 | (a) = ROLc((a), (s)) + (e);\ | |
281 | (c) = ROLc((c), 10); | |
282 | ||
283 | #define HH(a, b, c, d, e, x, s) \ | |
284 | (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\ | |
285 | (a) = ROLc((a), (s)) + (e);\ | |
286 | (c) = ROLc((c), 10); | |
287 | ||
288 | #define II(a, b, c, d, e, x, s) \ | |
289 | (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\ | |
290 | (a) = ROLc((a), (s)) + (e);\ | |
291 | (c) = ROLc((c), 10); | |
292 | ||
293 | #define JJ(a, b, c, d, e, x, s) \ | |
294 | (a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\ | |
295 | (a) = ROLc((a), (s)) + (e);\ | |
296 | (c) = ROLc((c), 10); | |
297 | ||
298 | #define FFF(a, b, c, d, e, x, s) \ | |
299 | (a) += F((b), (c), (d)) + (x);\ | |
300 | (a) = ROLc((a), (s)) + (e);\ | |
301 | (c) = ROLc((c), 10); | |
302 | ||
303 | #define GGG(a, b, c, d, e, x, s) \ | |
304 | (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\ | |
305 | (a) = ROLc((a), (s)) + (e);\ | |
306 | (c) = ROLc((c), 10); | |
307 | ||
308 | #define HHH(a, b, c, d, e, x, s) \ | |
309 | (a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\ | |
310 | (a) = ROLc((a), (s)) + (e);\ | |
311 | (c) = ROLc((c), 10); | |
312 | ||
313 | #define III(a, b, c, d, e, x, s) \ | |
314 | (a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\ | |
315 | (a) = ROLc((a), (s)) + (e);\ | |
316 | (c) = ROLc((c), 10); | |
317 | ||
318 | #define JJJ(a, b, c, d, e, x, s) \ | |
319 | (a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\ | |
320 | (a) = ROLc((a), (s)) + (e);\ | |
321 | (c) = ROLc((c), 10); | |
322 | ||
323 | static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf) | |
324 | { | |
325 | uint32_t aa,bb,cc,dd,ee,aaa,bbb,ccc,ddd,eee,X[16]; | |
326 | int i; | |
327 | ||
328 | /* load words X */ | |
329 | for (i = 0; i < 16; i++){ | |
330 | LOAD32L(X[i], buf + (4 * i)); | |
331 | } | |
332 | ||
333 | /* load state */ | |
334 | aa = aaa = md->state[0]; | |
335 | bb = bbb = md->state[1]; | |
336 | cc = ccc = md->state[2]; | |
337 | dd = ddd = md->state[3]; | |
338 | ee = eee = md->state[4]; | |
339 | ||
340 | /* round 1 */ | |
341 | FF(aa, bb, cc, dd, ee, X[ 0], 11); | |
342 | FF(ee, aa, bb, cc, dd, X[ 1], 14); | |
343 | FF(dd, ee, aa, bb, cc, X[ 2], 15); | |
344 | FF(cc, dd, ee, aa, bb, X[ 3], 12); | |
345 | FF(bb, cc, dd, ee, aa, X[ 4], 5); | |
346 | FF(aa, bb, cc, dd, ee, X[ 5], 8); | |
347 | FF(ee, aa, bb, cc, dd, X[ 6], 7); | |
348 | FF(dd, ee, aa, bb, cc, X[ 7], 9); | |
349 | FF(cc, dd, ee, aa, bb, X[ 8], 11); | |
350 | FF(bb, cc, dd, ee, aa, X[ 9], 13); | |
351 | FF(aa, bb, cc, dd, ee, X[10], 14); | |
352 | FF(ee, aa, bb, cc, dd, X[11], 15); | |
353 | FF(dd, ee, aa, bb, cc, X[12], 6); | |
354 | FF(cc, dd, ee, aa, bb, X[13], 7); | |
355 | FF(bb, cc, dd, ee, aa, X[14], 9); | |
356 | FF(aa, bb, cc, dd, ee, X[15], 8); | |
357 | ||
358 | /* round 2 */ | |
359 | GG(ee, aa, bb, cc, dd, X[ 7], 7); | |
360 | GG(dd, ee, aa, bb, cc, X[ 4], 6); | |
361 | GG(cc, dd, ee, aa, bb, X[13], 8); | |
362 | GG(bb, cc, dd, ee, aa, X[ 1], 13); | |
363 | GG(aa, bb, cc, dd, ee, X[10], 11); | |
364 | GG(ee, aa, bb, cc, dd, X[ 6], 9); | |
365 | GG(dd, ee, aa, bb, cc, X[15], 7); | |
366 | GG(cc, dd, ee, aa, bb, X[ 3], 15); | |
367 | GG(bb, cc, dd, ee, aa, X[12], 7); | |
368 | GG(aa, bb, cc, dd, ee, X[ 0], 12); | |
369 | GG(ee, aa, bb, cc, dd, X[ 9], 15); | |
370 | GG(dd, ee, aa, bb, cc, X[ 5], 9); | |
371 | GG(cc, dd, ee, aa, bb, X[ 2], 11); | |
372 | GG(bb, cc, dd, ee, aa, X[14], 7); | |
373 | GG(aa, bb, cc, dd, ee, X[11], 13); | |
374 | GG(ee, aa, bb, cc, dd, X[ 8], 12); | |
375 | ||
376 | /* round 3 */ | |
377 | HH(dd, ee, aa, bb, cc, X[ 3], 11); | |
378 | HH(cc, dd, ee, aa, bb, X[10], 13); | |
379 | HH(bb, cc, dd, ee, aa, X[14], 6); | |
380 | HH(aa, bb, cc, dd, ee, X[ 4], 7); | |
381 | HH(ee, aa, bb, cc, dd, X[ 9], 14); | |
382 | HH(dd, ee, aa, bb, cc, X[15], 9); | |
383 | HH(cc, dd, ee, aa, bb, X[ 8], 13); | |
384 | HH(bb, cc, dd, ee, aa, X[ 1], 15); | |
385 | HH(aa, bb, cc, dd, ee, X[ 2], 14); | |
386 | HH(ee, aa, bb, cc, dd, X[ 7], 8); | |
387 | HH(dd, ee, aa, bb, cc, X[ 0], 13); | |
388 | HH(cc, dd, ee, aa, bb, X[ 6], 6); | |
389 | HH(bb, cc, dd, ee, aa, X[13], 5); | |
390 | HH(aa, bb, cc, dd, ee, X[11], 12); | |
391 | HH(ee, aa, bb, cc, dd, X[ 5], 7); | |
392 | HH(dd, ee, aa, bb, cc, X[12], 5); | |
393 | ||
394 | /* round 4 */ | |
395 | II(cc, dd, ee, aa, bb, X[ 1], 11); | |
396 | II(bb, cc, dd, ee, aa, X[ 9], 12); | |
397 | II(aa, bb, cc, dd, ee, X[11], 14); | |
398 | II(ee, aa, bb, cc, dd, X[10], 15); | |
399 | II(dd, ee, aa, bb, cc, X[ 0], 14); | |
400 | II(cc, dd, ee, aa, bb, X[ 8], 15); | |
401 | II(bb, cc, dd, ee, aa, X[12], 9); | |
402 | II(aa, bb, cc, dd, ee, X[ 4], 8); | |
403 | II(ee, aa, bb, cc, dd, X[13], 9); | |
404 | II(dd, ee, aa, bb, cc, X[ 3], 14); | |
405 | II(cc, dd, ee, aa, bb, X[ 7], 5); | |
406 | II(bb, cc, dd, ee, aa, X[15], 6); | |
407 | II(aa, bb, cc, dd, ee, X[14], 8); | |
408 | II(ee, aa, bb, cc, dd, X[ 5], 6); | |
409 | II(dd, ee, aa, bb, cc, X[ 6], 5); | |
410 | II(cc, dd, ee, aa, bb, X[ 2], 12); | |
411 | ||
412 | /* round 5 */ | |
413 | JJ(bb, cc, dd, ee, aa, X[ 4], 9); | |
414 | JJ(aa, bb, cc, dd, ee, X[ 0], 15); | |
415 | JJ(ee, aa, bb, cc, dd, X[ 5], 5); | |
416 | JJ(dd, ee, aa, bb, cc, X[ 9], 11); | |
417 | JJ(cc, dd, ee, aa, bb, X[ 7], 6); | |
418 | JJ(bb, cc, dd, ee, aa, X[12], 8); | |
419 | JJ(aa, bb, cc, dd, ee, X[ 2], 13); | |
420 | JJ(ee, aa, bb, cc, dd, X[10], 12); | |
421 | JJ(dd, ee, aa, bb, cc, X[14], 5); | |
422 | JJ(cc, dd, ee, aa, bb, X[ 1], 12); | |
423 | JJ(bb, cc, dd, ee, aa, X[ 3], 13); | |
424 | JJ(aa, bb, cc, dd, ee, X[ 8], 14); | |
425 | JJ(ee, aa, bb, cc, dd, X[11], 11); | |
426 | JJ(dd, ee, aa, bb, cc, X[ 6], 8); | |
427 | JJ(cc, dd, ee, aa, bb, X[15], 5); | |
428 | JJ(bb, cc, dd, ee, aa, X[13], 6); | |
429 | ||
430 | /* parallel round 1 */ | |
431 | JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8); | |
432 | JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9); | |
433 | JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9); | |
434 | JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11); | |
435 | JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13); | |
436 | JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15); | |
437 | JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15); | |
438 | JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5); | |
439 | JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7); | |
440 | JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7); | |
441 | JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8); | |
442 | JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11); | |
443 | JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14); | |
444 | JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14); | |
445 | JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12); | |
446 | JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6); | |
447 | ||
448 | /* parallel round 2 */ | |
449 | III(eee, aaa, bbb, ccc, ddd, X[ 6], 9); | |
450 | III(ddd, eee, aaa, bbb, ccc, X[11], 13); | |
451 | III(ccc, ddd, eee, aaa, bbb, X[ 3], 15); | |
452 | III(bbb, ccc, ddd, eee, aaa, X[ 7], 7); | |
453 | III(aaa, bbb, ccc, ddd, eee, X[ 0], 12); | |
454 | III(eee, aaa, bbb, ccc, ddd, X[13], 8); | |
455 | III(ddd, eee, aaa, bbb, ccc, X[ 5], 9); | |
456 | III(ccc, ddd, eee, aaa, bbb, X[10], 11); | |
457 | III(bbb, ccc, ddd, eee, aaa, X[14], 7); | |
458 | III(aaa, bbb, ccc, ddd, eee, X[15], 7); | |
459 | III(eee, aaa, bbb, ccc, ddd, X[ 8], 12); | |
460 | III(ddd, eee, aaa, bbb, ccc, X[12], 7); | |
461 | III(ccc, ddd, eee, aaa, bbb, X[ 4], 6); | |
462 | III(bbb, ccc, ddd, eee, aaa, X[ 9], 15); | |
463 | III(aaa, bbb, ccc, ddd, eee, X[ 1], 13); | |
464 | III(eee, aaa, bbb, ccc, ddd, X[ 2], 11); | |
465 | ||
466 | /* parallel round 3 */ | |
467 | HHH(ddd, eee, aaa, bbb, ccc, X[15], 9); | |
468 | HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7); | |
469 | HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15); | |
470 | HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11); | |
471 | HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8); | |
472 | HHH(ddd, eee, aaa, bbb, ccc, X[14], 6); | |
473 | HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6); | |
474 | HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14); | |
475 | HHH(aaa, bbb, ccc, ddd, eee, X[11], 12); | |
476 | HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13); | |
477 | HHH(ddd, eee, aaa, bbb, ccc, X[12], 5); | |
478 | HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14); | |
479 | HHH(bbb, ccc, ddd, eee, aaa, X[10], 13); | |
480 | HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13); | |
481 | HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7); | |
482 | HHH(ddd, eee, aaa, bbb, ccc, X[13], 5); | |
483 | ||
484 | /* parallel round 4 */ | |
485 | GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15); | |
486 | GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5); | |
487 | GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8); | |
488 | GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11); | |
489 | GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14); | |
490 | GGG(ccc, ddd, eee, aaa, bbb, X[11], 14); | |
491 | GGG(bbb, ccc, ddd, eee, aaa, X[15], 6); | |
492 | GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14); | |
493 | GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6); | |
494 | GGG(ddd, eee, aaa, bbb, ccc, X[12], 9); | |
495 | GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12); | |
496 | GGG(bbb, ccc, ddd, eee, aaa, X[13], 9); | |
497 | GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12); | |
498 | GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5); | |
499 | GGG(ddd, eee, aaa, bbb, ccc, X[10], 15); | |
500 | GGG(ccc, ddd, eee, aaa, bbb, X[14], 8); | |
501 | ||
502 | /* parallel round 5 */ | |
503 | FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8); | |
504 | FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5); | |
505 | FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12); | |
506 | FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9); | |
507 | FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12); | |
508 | FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5); | |
509 | FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14); | |
510 | FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6); | |
511 | FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8); | |
512 | FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13); | |
513 | FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6); | |
514 | FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5); | |
515 | FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15); | |
516 | FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13); | |
517 | FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11); | |
518 | FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11); | |
519 | ||
520 | /* combine results */ | |
521 | ddd += cc + md->state[1]; /* final result for md->state[0] */ | |
522 | md->state[1] = md->state[2] + dd + eee; | |
523 | md->state[2] = md->state[3] + ee + aaa; | |
524 | md->state[3] = md->state[4] + aa + bbb; | |
525 | md->state[4] = md->state[0] + bb + ccc; | |
526 | md->state[0] = ddd; | |
527 | ||
528 | return 0; | |
529 | } | |
530 | ||
531 | /** | |
532 | Initialize the hash state | |
533 | @param md The hash state you wish to initialize | |
534 | @return 0 if successful | |
535 | */ | |
536 | int rmd160_vinit(struct rmd160_vstate * md) | |
537 | { | |
538 | md->state[0] = 0x67452301UL; | |
539 | md->state[1] = 0xefcdab89UL; | |
540 | md->state[2] = 0x98badcfeUL; | |
541 | md->state[3] = 0x10325476UL; | |
542 | md->state[4] = 0xc3d2e1f0UL; | |
543 | md->curlen = 0; | |
544 | md->length = 0; | |
545 | return 0; | |
546 | } | |
547 | #define HASH_PROCESS(func_name, compress_name, state_var, block_size) \ | |
548 | int func_name (struct rmd160_vstate * md, const unsigned char *in, unsigned long inlen) \ | |
549 | { \ | |
550 | unsigned long n; \ | |
551 | int err; \ | |
552 | if (md->curlen > sizeof(md->buf)) { \ | |
553 | return -1; \ | |
554 | } \ | |
555 | while (inlen > 0) { \ | |
556 | if (md->curlen == 0 && inlen >= block_size) { \ | |
557 | if ((err = compress_name (md, (unsigned char *)in)) != 0) { \ | |
558 | return err; \ | |
559 | } \ | |
560 | md->length += block_size * 8; \ | |
561 | in += block_size; \ | |
562 | inlen -= block_size; \ | |
563 | } else { \ | |
564 | n = MIN(inlen, (block_size - md->curlen)); \ | |
565 | memcpy(md->buf + md->curlen, in, (size_t)n); \ | |
566 | md->curlen += n; \ | |
567 | in += n; \ | |
568 | inlen -= n; \ | |
569 | if (md->curlen == block_size) { \ | |
570 | if ((err = compress_name (md, md->buf)) != 0) { \ | |
571 | return err; \ | |
572 | } \ | |
573 | md->length += 8*block_size; \ | |
574 | md->curlen = 0; \ | |
575 | } \ | |
576 | } \ | |
577 | } \ | |
578 | return 0; \ | |
579 | } | |
580 | ||
581 | /** | |
582 | Process a block of memory though the hash | |
583 | @param md The hash state | |
584 | @param in The data to hash | |
585 | @param inlen The length of the data (octets) | |
586 | @return 0 if successful | |
587 | */ | |
588 | HASH_PROCESS(rmd160_vprocess, rmd160_vcompress, rmd160, 64) | |
589 | ||
590 | /** | |
591 | Terminate the hash to get the digest | |
592 | @param md The hash state | |
593 | @param out [out] The destination of the hash (20 bytes) | |
594 | @return 0 if successful | |
595 | */ | |
596 | int rmd160_vdone(struct rmd160_vstate * md, unsigned char *out) | |
597 | { | |
598 | int i; | |
599 | if (md->curlen >= sizeof(md->buf)) { | |
600 | return -1; | |
601 | } | |
602 | /* increase the length of the message */ | |
603 | md->length += md->curlen * 8; | |
604 | ||
605 | /* append the '1' bit */ | |
606 | md->buf[md->curlen++] = (unsigned char)0x80; | |
607 | ||
608 | /* if the length is currently above 56 bytes we append zeros | |
609 | * then compress. Then we can fall back to padding zeros and length | |
610 | * encoding like normal. | |
611 | */ | |
612 | if (md->curlen > 56) { | |
613 | while (md->curlen < 64) { | |
614 | md->buf[md->curlen++] = (unsigned char)0; | |
615 | } | |
616 | rmd160_vcompress(md, md->buf); | |
617 | md->curlen = 0; | |
618 | } | |
619 | /* pad upto 56 bytes of zeroes */ | |
620 | while (md->curlen < 56) { | |
621 | md->buf[md->curlen++] = (unsigned char)0; | |
622 | } | |
623 | /* store length */ | |
624 | STORE64L(md->length, md->buf+56); | |
625 | rmd160_vcompress(md, md->buf); | |
626 | /* copy output */ | |
627 | for (i = 0; i < 5; i++) { | |
628 | STORE32L(md->state[i], out+(4*i)); | |
629 | } | |
630 | return 0; | |
631 | } | |
632 | ||
633 | void calc_rmd160(char deprecated[41],uint8_t buf[20],uint8_t *msg,int32_t len) | |
634 | { | |
635 | struct rmd160_vstate md; | |
636 | rmd160_vinit(&md); | |
637 | rmd160_vprocess(&md,msg,len); | |
638 | rmd160_vdone(&md, buf); | |
639 | } | |
640 | #undef F | |
641 | #undef G | |
642 | #undef H | |
643 | #undef I | |
644 | #undef J | |
645 | #undef ROLc | |
646 | #undef FF | |
647 | #undef GG | |
648 | #undef HH | |
649 | #undef II | |
650 | #undef JJ | |
651 | #undef FFF | |
652 | #undef GGG | |
653 | #undef HHH | |
654 | #undef III | |
655 | #undef JJJ | |
656 | ||
657 | static const uint32_t crc32_tab[] = { | |
658 | 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, | |
659 | 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, | |
660 | 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, | |
661 | 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, | |
662 | 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, | |
663 | 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, | |
664 | 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, | |
665 | 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, | |
666 | 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, | |
667 | 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, | |
668 | 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, | |
669 | 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, | |
670 | 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, | |
671 | 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, | |
672 | 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, | |
673 | 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, | |
674 | 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, | |
675 | 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, | |
676 | 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, | |
677 | 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, | |
678 | 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, | |
679 | 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, | |
680 | 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, | |
681 | 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, | |
682 | 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, | |
683 | 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, | |
684 | 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, | |
685 | 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, | |
686 | 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, | |
687 | 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, | |
688 | 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, | |
689 | 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, | |
690 | 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, | |
691 | 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, | |
692 | 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, | |
693 | 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, | |
694 | 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, | |
695 | 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, | |
696 | 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, | |
697 | 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, | |
698 | 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, | |
699 | 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, | |
700 | 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d | |
701 | }; | |
702 | ||
703 | uint32_t calc_crc32(uint32_t crc,const void *buf,size_t size) | |
704 | { | |
705 | const uint8_t *p; | |
706 | ||
707 | p = (const uint8_t *)buf; | |
708 | crc = crc ^ ~0U; | |
709 | ||
710 | while (size--) | |
711 | crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); | |
712 | ||
713 | return crc ^ ~0U; | |
714 | } | |
715 | ||
716 | void calc_rmd160_sha256(uint8_t rmd160[20],uint8_t *data,int32_t datalen) | |
717 | { | |
718 | bits256 hash; | |
719 | vcalc_sha256(0,hash.bytes,data,datalen); | |
720 | calc_rmd160(0,rmd160,hash.bytes,sizeof(hash)); | |
721 | } | |
722 | ||
723 | int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp) | |
724 | { | |
725 | int32_t i; uint64_t x; | |
726 | if ( rwflag == 0 ) | |
727 | { | |
728 | x = 0; | |
729 | for (i=len-1; i>=0; i--) | |
730 | { | |
731 | x <<= 8; | |
732 | x |= serialized[i]; | |
733 | } | |
734 | switch ( len ) | |
735 | { | |
736 | case 1: *(uint8_t *)endianedp = (uint8_t)x; break; | |
737 | case 2: *(uint16_t *)endianedp = (uint16_t)x; break; | |
738 | case 4: *(uint32_t *)endianedp = (uint32_t)x; break; | |
739 | case 8: *(uint64_t *)endianedp = (uint64_t)x; break; | |
740 | } | |
741 | } | |
742 | else | |
743 | { | |
744 | x = 0; | |
745 | switch ( len ) | |
746 | { | |
747 | case 1: x = *(uint8_t *)endianedp; break; | |
748 | case 2: x = *(uint16_t *)endianedp; break; | |
749 | case 4: x = *(uint32_t *)endianedp; break; | |
750 | case 8: x = *(uint64_t *)endianedp; break; | |
751 | } | |
752 | for (i=0; i<len; i++,x >>= 8) | |
753 | serialized[i] = (uint8_t)(x & 0xff); | |
754 | } | |
755 | return(len); | |
756 | } | |
757 | ||
758 | uint32_t komodo_assetmagic(char *symbol,uint64_t supply,uint8_t *extraptr,int32_t extralen) | |
759 | { | |
760 | uint8_t buf[512]; uint32_t crc0=0; int32_t len = 0; bits256 hash; | |
761 | if ( strcmp(symbol,"KMD") == 0 ) | |
762 | return(0x8de4eef9); | |
763 | len = iguana_rwnum(1,&buf[len],sizeof(supply),(void *)&supply); | |
764 | strcpy((char *)&buf[len],symbol); | |
765 | len += strlen(symbol); | |
766 | if ( extraptr != 0 && extralen != 0 ) | |
767 | { | |
768 | vcalc_sha256(0,hash.bytes,extraptr,extralen); | |
769 | crc0 = hash.uints[0]; | |
770 | } | |
771 | return(calc_crc32(crc0,buf,len)); | |
772 | } | |
773 | ||
774 | uint16_t komodo_assetport(uint32_t magic,int32_t extralen) | |
775 | { | |
776 | if ( magic == 0x8de4eef9 ) | |
777 | return(7770); | |
778 | else if ( extralen == 0 ) | |
779 | return(8000 + (magic % 7777)); | |
780 | else return(16000 + (magic % 49500)); | |
781 | } | |
782 | ||
783 | uint16_t komodo_port(char *symbol,uint64_t supply,uint32_t *magicp,uint8_t *extraptr,int32_t extralen) | |
784 | { | |
785 | if ( symbol == 0 || symbol[0] == 0 || strcmp("KMD",symbol) == 0 ) | |
786 | { | |
787 | *magicp = 0x8de4eef9; | |
788 | return(7770); | |
789 | } | |
790 | *magicp = komodo_assetmagic(symbol,supply,extraptr,extralen); | |
791 | return(komodo_assetport(*magicp,extralen)); | |
792 | } | |
793 | ||
c010c533 | 794 | uint16_t komodo_calcport(char *name,uint64_t supply,uint64_t endsubsidy,uint64_t reward,uint64_t halving,uint64_t decay,uint64_t commission,uint8_t staked,int32_t cc) |
7e907fdd | 795 | { |
c010c533 | 796 | uint8_t extrabuf[4096],*extraptr=0; int32_t extralen=0; uint64_t val; |
7e907fdd | 797 | if ( halving != 0 && halving < 1440 ) |
798 | { | |
799 | halving = 1440; | |
800 | printf("halving must be at least 1440 blocks\n"); | |
801 | } | |
802 | if ( decay == 100000000 && endsubsidy == 0 ) | |
803 | { | |
804 | decay = 0; | |
805 | printf("decay of 100000000 means linear and that needs endsubsidy\n"); | |
806 | } | |
807 | else if ( decay > 100000000 ) | |
808 | { | |
809 | decay = 0; | |
810 | printf("decay cant be more than 100000000\n"); | |
811 | } | |
d9f176ac | 812 | if ( endsubsidy != 0 || reward != 0 || halving != 0 || decay != 0 || commission != 0 || cc != 0 || staked != 0 || ASSETCHAINS_OVERRIDE_PUBKEY33[0] != 0 ) |
7e907fdd | 813 | { |
c010c533 | 814 | //printf("end.%llu reward.%llu halving.%llu decay.%llu perc.%llu\n",(long long)endsubsidy,(long long)reward,(long long)halving,(long long)decay,(long long)commission); |
7e907fdd | 815 | extraptr = extrabuf; |
816 | memcpy(extraptr,ASSETCHAINS_OVERRIDE_PUBKEY33,33), extralen = 33; | |
817 | extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(endsubsidy),(void *)&endsubsidy); | |
818 | extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(reward),(void *)&reward); | |
819 | extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(halving),(void *)&halving); | |
820 | extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(decay),(void *)&decay); | |
c010c533 | 821 | val = commission | (((uint64_t)staked & 0xff) << 32) | (((uint64_t)cc & 0xffffff) << 40); |
822 | extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(val),(void *)&val); | |
7e907fdd | 823 | } |
824 | return(komodo_port(name,supply,&ASSETCHAINS_MAGIC,extraptr,extralen)); | |
825 | } | |
826 | ||
827 | int main(int argc, char * argv[]) | |
828 | { | |
5f20a85d | 829 | uint16_t rpcport; int32_t i,j,offset=0,jsonflag=0,num = 1; uint64_t supply=10,commission=0,endsubsidy,reward,halving,decay; uint8_t *allocated=0,staked=0; uint32_t cc = 3; |
7e907fdd | 830 | endsubsidy = reward = halving = decay = 0; |
831 | if ( argc < 2 ) | |
832 | { | |
c010c533 | 833 | // staked, commission and cc hardcoded |
c244ea42 | 834 | printf("%s name supply endsubsidy reward halving decay\n",argv[0]); |
835 | printf("%s -gen num name supply endsubsidy reward halving decay\n",argv[0]); | |
7e907fdd | 836 | return(-1); |
837 | } | |
5f20a85d | 838 | if ( strncmp(argv[1],"-gen",3) == 0 ) |
c244ea42 | 839 | { |
840 | num = atoi(argv[2]); | |
5f20a85d | 841 | if ( strcmp(argv[1],"-genjson") == 0 ) |
842 | jsonflag = 1; | |
c244ea42 | 843 | offset = 2; |
844 | allocated = calloc(1,1 << 16); | |
845 | } | |
846 | if ( argc > offset + 2 ) | |
847 | supply = (long long)atof(argv[offset + 2]); | |
848 | if ( argc > offset + 3 ) | |
849 | endsubsidy = (long long)atof(argv[offset + 3]); | |
850 | if ( argc > offset + 4 ) | |
851 | reward = (long long)atof(argv[offset + 4]); | |
852 | if ( argc > offset + 5 ) | |
853 | halving = (long long)atof(argv[offset + 5]); | |
854 | if ( argc > offset + 6 ) | |
855 | decay = (long long)atof(argv[offset + 6]); | |
c010c533 | 856 | rpcport = 1 + komodo_calcport(argv[offset + 1],supply,endsubsidy,reward,halving,decay,commission,staked,cc); |
5f20a85d | 857 | printf("./komodod -ac_name=%s -ac_cc=%u -ac_supply=%llu -ac_end=%llu -ac_reward=%llu -ac_halving=%llu -ac_decay=%llu & # rpcport %u\n[",argv[offset + 1],cc,(long long)supply,(long long)endsubsidy,(long long)reward,(long long)halving,(long long)decay,rpcport); |
c244ea42 | 858 | if ( allocated != 0 ) |
859 | { | |
860 | char name[64],newname[64]; | |
861 | strcpy(name,argv[offset + 1]); | |
862 | allocated[rpcport] = 1; | |
863 | allocated[rpcport-1] = 1; | |
864 | for (i=0; i<num; i++) | |
865 | { | |
c010c533 | 866 | sprintf(newname,"%s%03x",name,i); // limit 4096 |
c244ea42 | 867 | j = 0; |
868 | while ( 1 ) | |
869 | { | |
c010c533 | 870 | rpcport = 1 + komodo_calcport(newname,supply+j,endsubsidy,reward,halving,decay,commission,staked,cc); |
c244ea42 | 871 | if ( allocated[rpcport] == 0 && allocated[rpcport-1] == 0 ) |
872 | { | |
5f20a85d | 873 | if ( jsonflag == 0 ) |
874 | { | |
875 | printf("./komodod -ac_name=%s -ac_cc=%u -ac_supply=%llu -ac_end=%llu -ac_reward=%llu -ac_halving=%llu -ac_decay=%llu & # rpcport %u\n",newname,cc,(long long)supply+j,(long long)endsubsidy,(long long)reward,(long long)halving,(long long)decay,rpcport); | |
876 | } | |
877 | else | |
878 | { | |
879 | printf("{ \"assetname\": \"%s\", \"p2p\": %u, \"rpc\": %u, \"supply\": %llu, \"ac_cc\": %u, \"ac_reward\": %llu, \"notarize\": %s }%s ",newname,rpcport-1,rpcport,(long long)supply+j,cc,(long long)reward,i<64?"true":"false",i<num-1?",":""); | |
880 | } | |
c244ea42 | 881 | allocated[rpcport] = 1; |
882 | allocated[rpcport-1] = 1; | |
883 | break; | |
884 | } | |
885 | j++; | |
886 | } | |
887 | } | |
888 | free(allocated); | |
889 | } | |
5f20a85d | 890 | printf("]\n"); |
7e907fdd | 891 | return(0); |
892 | } |