无符号256位整数运算

这个源代码包括两个文件,分别是uint256.h和uint256.c。

有关计算是基于类型uint128_t上实现的。

uint256.h代码如下:



  1. /*******************************************************************************
  2. * Ledger Blue
  3. * (c) 2016 Ledger
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. ********************************************************************************/

  17. // Adapted from https://github.com/calccrypto/uint256_t

  18. #include <stdint.h>
  19. #include <stdbool.h>

  20. typedef struct uint128_t { uint64_t elements[2]; } uint128_t;

  21. typedef struct uint256_t { uint128_t elements[2]; } uint256_t;

  22. #define UPPER_P(x) x->elements[0]
  23. #define LOWER_P(x) x->elements[1]
  24. #define UPPER(x) x.elements[0]
  25. #define LOWER(x) x.elements[1]

  26. void readu128BE(uint8_t *buffer, uint128_t *target);
  27. void readu256BE(uint8_t *buffer, uint256_t *target);
  28. bool zero128(uint128_t *number);
  29. bool zero256(uint256_t *number);
  30. void copy128(uint128_t *target, uint128_t *number);
  31. void copy256(uint256_t *target, uint256_t *number);
  32. void clear128(uint128_t *target);
  33. void clear256(uint256_t *target);
  34. void shiftl128(uint128_t *number, uint32_t value, uint128_t *target);
  35. void shiftr128(uint128_t *number, uint32_t value, uint128_t *target);
  36. void shiftl256(uint256_t *number, uint32_t value, uint256_t *target);
  37. void shiftr256(uint256_t *number, uint32_t value, uint256_t *target);
  38. uint32_t bits128(uint128_t *number);
  39. uint32_t bits256(uint256_t *number);
  40. bool equal128(uint128_t *number1, uint128_t *number2);
  41. bool equal256(uint256_t *number1, uint256_t *number2);
  42. bool gt128(uint128_t *number1, uint128_t *number2);
  43. bool gt256(uint256_t *number1, uint256_t *number2);
  44. bool gte128(uint128_t *number1, uint128_t *number2);
  45. bool gte256(uint256_t *number1, uint256_t *number2);
  46. void add128(uint128_t *number1, uint128_t *number2, uint128_t *target);
  47. void add256(uint256_t *number1, uint256_t *number2, uint256_t *target);
  48. void minus128(uint128_t *number1, uint128_t *number2, uint128_t *target);
  49. void minus256(uint256_t *number1, uint256_t *number2, uint256_t *target);
  50. void or128(uint128_t *number1, uint128_t *number2, uint128_t *target);
  51. void or256(uint256_t *number1, uint256_t *number2, uint256_t *target);
  52. void mul128(uint128_t *number1, uint128_t *number2, uint128_t *target);
  53. void mul256(uint256_t *number1, uint256_t *number2, uint256_t *target);
  54. void divmod128(uint128_t *l, uint128_t *r, uint128_t *div, uint128_t *mod);
  55. void divmod256(uint256_t *l, uint256_t *r, uint256_t *div, uint256_t *mod);
  56. bool tostring128(uint128_t *number, uint32_t base, char *out,
  57. uint32_t outLength);
  58. bool tostring256(uint256_t *number, uint32_t base, char *out,
  59. uint32_t outLength);


uint256.c代码如下:



  1. /*******************************************************************************
  2. * Ledger Blue
  3. * (c) 2016 Ledger
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. ********************************************************************************/

  17. // Adapted from https://github.com/calccrypto/uint256_t

  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include "uint256.h"

  21. static const char HEXDIGITS[] = "0123456789abcdef";

  22. static uint64_t readUint64BE(uint8_t *buffer) {
  23. return (((uint64_t)buffer[0]) << 56) | (((uint64_t)buffer[1]) << 48) |
  24. (((uint64_t)buffer[2]) << 40) | (((uint64_t)buffer[3]) << 32) |
  25. (((uint64_t)buffer[4]) << 24) | (((uint64_t)buffer[5]) << 16) |
  26. (((uint64_t)buffer[6]) << 8) | (((uint64_t)buffer[7]));
  27. }

  28. void readu128BE(uint8_t *buffer, uint128_t *target) {
  29. UPPER_P(target) = readUint64BE(buffer);
  30. LOWER_P(target) = readUint64BE(buffer + 8);
  31. }

  32. void readu256BE(uint8_t *buffer, uint256_t *target) {
  33. readu128BE(buffer, &UPPER_P(target));
  34. readu128BE(buffer + 16, &LOWER_P(target));
  35. }

  36. bool zero128(uint128_t *number) {
  37. return ((LOWER_P(number) == 0) && (UPPER_P(number) == 0));
  38. }

  39. bool zero256(uint256_t *number) {
  40. return (zero128(&LOWER_P(number)) && zero128(&UPPER_P(number)));
  41. }

  42. void copy128(uint128_t *target, uint128_t *number) {
  43. UPPER_P(target) = UPPER_P(number);
  44. LOWER_P(target) = LOWER_P(number);
  45. }

  46. void copy256(uint256_t *target, uint256_t *number) {
  47. copy128(&UPPER_P(target), &UPPER_P(number));
  48. copy128(&LOWER_P(target), &LOWER_P(number));
  49. }

  50. void clear128(uint128_t *target) {
  51. UPPER_P(target) = 0;
  52. LOWER_P(target) = 0;
  53. }

  54. void clear256(uint256_t *target) {
  55. clear128(&UPPER_P(target));
  56. clear128(&LOWER_P(target));
  57. }

  58. void shiftl128(uint128_t *number, uint32_t value, uint128_t *target) {
  59. if (value >= 128) {
  60. clear128(target);
  61. } else if (value == 64) {
  62. UPPER_P(target) = LOWER_P(number);
  63. LOWER_P(target) = 0;
  64. } else if (value == 0) {
  65. copy128(target, number);
  66. } else if (value < 64) {
  67. UPPER_P(target) =
  68. (UPPER_P(number) << value) + (LOWER_P(number) >> (64 - value));
  69. LOWER_P(target) = (LOWER_P(number) << value);
  70. } else if ((128 > value) && (value > 64)) {
  71. UPPER_P(target) = LOWER_P(number) << (value - 64);
  72. LOWER_P(target) = 0;
  73. } else {
  74. clear128(target);
  75. }
  76. }

  77. void shiftl256(uint256_t *number, uint32_t value, uint256_t *target) {
  78. if (value >= 256) {
  79. clear256(target);
  80. } else if (value == 128) {
  81. copy128(&UPPER_P(target), &LOWER_P(number));
  82. clear128(&LOWER_P(target));
  83. } else if (value == 0) {
  84. copy256(target, number);
  85. } else if (value < 128) {
  86. uint128_t tmp1;
  87. uint128_t tmp2;
  88. uint256_t result;
  89. shiftl128(&UPPER_P(number), value, &tmp1);
  90. shiftr128(&LOWER_P(number), (128 - value), &tmp2);
  91. add128(&tmp1, &tmp2, &UPPER(result));
  92. shiftl128(&LOWER_P(number), value, &LOWER(result));
  93. copy256(target, &result);
  94. } else if ((256 > value) && (value > 128)) {
  95. shiftl128(&LOWER_P(number), (value - 128), &UPPER_P(target));
  96. clear128(&LOWER_P(target));
  97. } else {
  98. clear256(target);
  99. }
  100. }

  101. void shiftr128(uint128_t *number, uint32_t value, uint128_t *target) {
  102. if (value >= 128) {
  103. clear128(target);
  104. } else if (value == 64) {
  105. UPPER_P(target) = 0;
  106. LOWER_P(target) = UPPER_P(number);
  107. } else if (value == 0) {
  108. copy128(target, number);
  109. } else if (value < 64) {
  110. uint128_t result;
  111. UPPER(result) = UPPER_P(number) >> value;
  112. LOWER(result) =
  113. (UPPER_P(number) << (64 - value)) + (LOWER_P(number) >> value);
  114. copy128(target, &result);
  115. } else if ((128 > value) && (value > 64)) {
  116. LOWER_P(target) = UPPER_P(number) >> (value - 64);
  117. UPPER_P(target) = 0;
  118. } else {
  119. clear128(target);
  120. }
  121. }

  122. void shiftr256(uint256_t *number, uint32_t value, uint256_t *target) {
  123. if (value >= 256) {
  124. clear256(target);
  125. } else if (value == 128) {
  126. copy128(&LOWER_P(target), &UPPER_P(number));
  127. clear128(&UPPER_P(target));
  128. } else if (value == 0) {
  129. copy256(target, number);
  130. } else if (value < 128) {
  131. uint128_t tmp1;
  132. uint128_t tmp2;
  133. uint256_t result;
  134. shiftr128(&UPPER_P(number), value, &UPPER(result));
  135. shiftr128(&LOWER_P(number), value, &tmp1);
  136. shiftl128(&UPPER_P(number), (128 - value), &tmp2);
  137. add128(&tmp1, &tmp2, &LOWER(result));
  138. copy256(target, &result);
  139. } else if ((256 > value) && (value > 128)) {
  140. shiftr128(&UPPER_P(number), (value - 128), &LOWER_P(target));
  141. clear128(&UPPER_P(target));
  142. } else {
  143. clear256(target);
  144. }
  145. }

  146. uint32_t bits128(uint128_t *number) {
  147. uint32_t result = 0;
  148. if (UPPER_P(number)) {
  149. result = 64;
  150. uint64_t up = UPPER_P(number);
  151. while (up) {
  152. up >>= 1;
  153. result++;
  154. }
  155. } else {
  156. uint64_t low = LOWER_P(number);
  157. while (low) {
  158. low >>= 1;
  159. result++;
  160. }
  161. }
  162. return result;
  163. }

  164. uint32_t bits256(uint256_t *number) {
  165. uint32_t result = 0;
  166. if (!zero128(&UPPER_P(number))) {
  167. result = 128;
  168. uint128_t up;
  169. copy128(&up, &UPPER_P(number));
  170. while (!zero128(&up)) {
  171. shiftr128(&up, 1, &up);
  172. result++;
  173. }
  174. } else {
  175. uint128_t low;
  176. copy128(&low, &LOWER_P(number));
  177. while (!zero128(&low)) {
  178. shiftr128(&low, 1, &low);
  179. result++;
  180. }
  181. }
  182. return result;
  183. }

  184. bool equal128(uint128_t *number1, uint128_t *number2) {
  185. return (UPPER_P(number1) == UPPER_P(number2)) &&
  186. (LOWER_P(number1) == LOWER_P(number2));
  187. }

  188. bool equal256(uint256_t *number1, uint256_t *number2) {
  189. return (equal128(&UPPER_P(number1), &UPPER_P(number2)) &&
  190. equal128(&LOWER_P(number1), &LOWER_P(number2)));
  191. }

  192. bool gt128(uint128_t *number1, uint128_t *number2) {
  193. if (UPPER_P(number1) == UPPER_P(number2)) {
  194. return (LOWER_P(number1) > LOWER_P(number2));
  195. }
  196. return (UPPER_P(number1) > UPPER_P(number2));
  197. }

  198. bool gt256(uint256_t *number1, uint256_t *number2) {
  199. if (equal128(&UPPER_P(number1), &UPPER_P(number2))) {
  200. return gt128(&LOWER_P(number1), &LOWER_P(number2));
  201. }
  202. return gt128(&UPPER_P(number1), &UPPER_P(number2));
  203. }

  204. bool gte128(uint128_t *number1, uint128_t *number2) {
  205. return gt128(number1, number2) || equal128(number1, number2);
  206. }

  207. bool gte256(uint256_t *number1, uint256_t *number2) {
  208. return gt256(number1, number2) || equal256(number1, number2);
  209. }

  210. void add128(uint128_t *number1, uint128_t *number2, uint128_t *target) {
  211. UPPER_P(target) =
  212. UPPER_P(number1) + UPPER_P(number2) +
  213. ((LOWER_P(number1) + LOWER_P(number2)) < LOWER_P(number1));
  214. LOWER_P(target) = LOWER_P(number1) + LOWER_P(number2);
  215. }

  216. void add256(uint256_t *number1, uint256_t *number2, uint256_t *target) {
  217. uint128_t tmp;
  218. add128(&UPPER_P(number1), &UPPER_P(number2), &UPPER_P(target));
  219. add128(&LOWER_P(number1), &LOWER_P(number2), &tmp);
  220. if (gt128(&LOWER_P(number1), &tmp)) {
  221. uint128_t one;
  222. UPPER(one) = 0;
  223. LOWER(one) = 1;
  224. add128(&UPPER_P(target), &one, &UPPER_P(target));
  225. }
  226. add128(&LOWER_P(number1), &LOWER_P(number2), &LOWER_P(target));
  227. }

  228. void minus128(uint128_t *number1, uint128_t *number2, uint128_t *target) {
  229. UPPER_P(target) =
  230. UPPER_P(number1) - UPPER_P(number2) -
  231. ((LOWER_P(number1) - LOWER_P(number2)) > LOWER_P(number1));
  232. LOWER_P(target) = LOWER_P(number1) - LOWER_P(number2);
  233. }

  234. void minus256(uint256_t *number1, uint256_t *number2, uint256_t *target) {
  235. uint128_t tmp;
  236. minus128(&UPPER_P(number1), &UPPER_P(number2), &UPPER_P(target));
  237. minus128(&LOWER_P(number1), &LOWER_P(number2), &tmp);
  238. if (gt128(&tmp, &LOWER_P(number1))) {
  239. uint128_t one;
  240. UPPER(one) = 0;
  241. LOWER(one) = 1;
  242. minus128(&UPPER_P(target), &one, &UPPER_P(target));
  243. }
  244. minus128(&LOWER_P(number1), &LOWER_P(number2), &LOWER_P(target));
  245. }

  246. void or128(uint128_t *number1, uint128_t *number2, uint128_t *target) {
  247. UPPER_P(target) = UPPER_P(number1) | UPPER_P(number2);
  248. LOWER_P(target) = LOWER_P(number1) | LOWER_P(number2);
  249. }

  250. void or256(uint256_t *number1, uint256_t *number2, uint256_t *target) {
  251. or128(&UPPER_P(number1), &UPPER_P(number2), &UPPER_P(target));
  252. or128(&LOWER_P(number1), &LOWER_P(number2), &LOWER_P(target));
  253. }

  254. void mul128(uint128_t *number1, uint128_t *number2, uint128_t *target) {
  255. uint64_t top[4] = {UPPER_P(number1) >> 32, UPPER_P(number1) & 0xffffffff,
  256. LOWER_P(number1) >> 32, LOWER_P(number1) & 0xffffffff};
  257. uint64_t bottom[4] = {UPPER_P(number2) >> 32, UPPER_P(number2) & 0xffffffff,
  258. LOWER_P(number2) >> 32,
  259. LOWER_P(number2) & 0xffffffff};
  260. uint64_t products[4][4];
  261. uint128_t tmp, tmp2;

  262. for (int y = 3; y > -1; y--) {
  263. for (int x = 3; x > -1; x--) {
  264. products[3 - x][y] = top[x] * bottom[y];
  265. }
  266. }

  267. uint64_t fourth32 = products[0][3] & 0xffffffff;
  268. uint64_t third32 = (products[0][2] & 0xffffffff) + (products[0][3] >> 32);
  269. uint64_t second32 = (products[0][1] & 0xffffffff) + (products[0][2] >> 32);
  270. uint64_t first32 = (products[0][0] & 0xffffffff) + (products[0][1] >> 32);

  271. third32 += products[1][3] & 0xffffffff;
  272. second32 += (products[1][2] & 0xffffffff) + (products[1][3] >> 32);
  273. first32 += (products[1][1] & 0xffffffff) + (products[1][2] >> 32);

  274. second32 += products[2][3] & 0xffffffff;
  275. first32 += (products[2][2] & 0xffffffff) + (products[2][3] >> 32);

  276. first32 += products[3][3] & 0xffffffff;

  277. UPPER(tmp) = first32 << 32;
  278. LOWER(tmp) = 0;
  279. UPPER(tmp2) = third32 >> 32;
  280. LOWER(tmp2) = third32 << 32;
  281. add128(&tmp, &tmp2, target);
  282. UPPER(tmp) = second32;
  283. LOWER(tmp) = 0;
  284. add128(&tmp, target, &tmp2);
  285. UPPER(tmp) = 0;
  286. LOWER(tmp) = fourth32;
  287. add128(&tmp, &tmp2, target);
  288. }

  289. void mul256(uint256_t *number1, uint256_t *number2, uint256_t *target) {
  290. uint128_t top[4];
  291. uint128_t bottom[4];
  292. uint128_t products[4][4];
  293. uint128_t tmp, tmp2, fourth64, third64, second64, first64;
  294. uint256_t target1, target2;
  295. UPPER(top[0]) = 0;
  296. LOWER(top[0]) = UPPER(UPPER_P(number1));
  297. UPPER(top[1]) = 0;
  298. LOWER(top[1]) = LOWER(UPPER_P(number1));
  299. UPPER(top[2]) = 0;
  300. LOWER(top[2]) = UPPER(LOWER_P(number1));
  301. UPPER(top[3]) = 0;
  302. LOWER(top[3]) = LOWER(LOWER_P(number1));
  303. UPPER(bottom[0]) = 0;
  304. LOWER(bottom[0]) = UPPER(UPPER_P(number2));
  305. UPPER(bottom[1]) = 0;
  306. LOWER(bottom[1]) = LOWER(UPPER_P(number2));
  307. UPPER(bottom[2]) = 0;
  308. LOWER(bottom[2]) = UPPER(LOWER_P(number2));
  309. UPPER(bottom[3]) = 0;
  310. LOWER(bottom[3]) = LOWER(LOWER_P(number2));

  311. for (int y = 3; y > -1; y--) {
  312. for (int x = 3; x > -1; x--) {
  313. mul128(&top[x], &bottom[y], &products[3 - x][y]);
  314. }
  315. }

  316. UPPER(fourth64) = 0;
  317. LOWER(fourth64) = LOWER(products[0][3]);
  318. UPPER(tmp) = 0;
  319. LOWER(tmp) = LOWER(products[0][2]);
  320. UPPER(tmp2) = 0;
  321. LOWER(tmp2) = UPPER(products[0][3]);
  322. add128(&tmp, &tmp2, &third64);
  323. UPPER(tmp) = 0;
  324. LOWER(tmp) = LOWER(products[0][1]);
  325. UPPER(tmp2) = 0;
  326. LOWER(tmp2) = UPPER(products[0][2]);
  327. add128(&tmp, &tmp2, &second64);
  328. UPPER(tmp) = 0;
  329. LOWER(tmp) = LOWER(products[0][0]);
  330. UPPER(tmp2) = 0;
  331. LOWER(tmp2) = UPPER(products[0][1]);
  332. add128(&tmp, &tmp2, &first64);

  333. UPPER(tmp) = 0;
  334. LOWER(tmp) = LOWER(products[1][3]);
  335. add128(&tmp, &third64, &tmp2);
  336. copy128(&third64, &tmp2);
  337. UPPER(tmp) = 0;
  338. LOWER(tmp) = LOWER(products[1][2]);
  339. add128(&tmp, &second64, &tmp2);
  340. UPPER(tmp) = 0;
  341. LOWER(tmp) = UPPER(products[1][3]);
  342. add128(&tmp, &tmp2, &second64);
  343. UPPER(tmp) = 0;
  344. LOWER(tmp) = LOWER(products[1][1]);
  345. add128(&tmp, &first64, &tmp2);
  346. UPPER(tmp) = 0;
  347. LOWER(tmp) = UPPER(products[1][2]);
  348. add128(&tmp, &tmp2, &first64);

  349. UPPER(tmp) = 0;
  350. LOWER(tmp) = LOWER(products[2][3]);
  351. add128(&tmp, &second64, &tmp2);
  352. copy128(&second64, &tmp2);
  353. UPPER(tmp) = 0;
  354. LOWER(tmp) = LOWER(products[2][2]);
  355. add128(&tmp, &first64, &tmp2);
  356. UPPER(tmp) = 0;
  357. LOWER(tmp) = UPPER(products[2][3]);
  358. add128(&tmp, &tmp2, &first64);

  359. UPPER(tmp) = 0;
  360. LOWER(tmp) = LOWER(products[3][3]);
  361. add128(&tmp, &first64, &tmp2);
  362. copy128(&first64, &tmp2);

  363. clear256(&target1);
  364. shiftl128(&first64, 64, &UPPER(target1));
  365. clear256(&target2);
  366. UPPER(UPPER(target2)) = UPPER(third64);
  367. shiftl128(&third64, 64, &LOWER(target2));
  368. add256(&target1, &target2, target);
  369. clear256(&target1);
  370. copy128(&UPPER(target1), &second64);
  371. add256(&target1, target, &target2);
  372. clear256(&target1);
  373. copy128(&LOWER(target1), &fourth64);
  374. add256(&target1, &target2, target);
  375. }

  376. void divmod128(uint128_t *l, uint128_t *r, uint128_t *retDiv,
  377. uint128_t *retMod) {
  378. uint128_t copyd, adder, resDiv, resMod;
  379. uint128_t one;
  380. UPPER(one) = 0;
  381. LOWER(one) = 1;
  382. uint32_t diffBits = bits128(l) - bits128(r);
  383. clear128(&resDiv);
  384. copy128(&resMod, l);
  385. if (gt128(r, l)) {
  386. copy128(retMod, l);
  387. clear128(retDiv);
  388. } else {
  389. shiftl128(r, diffBits, &copyd);
  390. shiftl128(&one, diffBits, &adder);
  391. if (gt128(&copyd, &resMod)) {
  392. shiftr128(&copyd, 1, &copyd);
  393. shiftr128(&adder, 1, &adder);
  394. }
  395. while (gte128(&resMod, r)) {
  396. if (gte128(&resMod, &copyd)) {
  397. minus128(&resMod, &copyd, &resMod);
  398. or128(&resDiv, &adder, &resDiv);
  399. }
  400. shiftr128(&copyd, 1, &copyd);
  401. shiftr128(&adder, 1, &adder);
  402. }
  403. copy128(retDiv, &resDiv);
  404. copy128(retMod, &resMod);
  405. }
  406. }

  407. void divmod256(uint256_t *l, uint256_t *r, uint256_t *retDiv,
  408. uint256_t *retMod) {
  409. uint256_t copyd, adder, resDiv, resMod;
  410. uint256_t one;
  411. clear256(&one);
  412. UPPER(LOWER(one)) = 0;
  413. LOWER(LOWER(one)) = 1;
  414. uint32_t diffBits = bits256(l) - bits256(r);
  415. clear256(&resDiv);
  416. copy256(&resMod, l);
  417. if (gt256(r, l)) {
  418. copy256(retMod, l);
  419. clear256(retDiv);
  420. } else {
  421. shiftl256(r, diffBits, &copyd);
  422. shiftl256(&one, diffBits, &adder);
  423. if (gt256(&copyd, &resMod)) {
  424. shiftr256(&copyd, 1, &copyd);
  425. shiftr256(&adder, 1, &adder);
  426. }
  427. while (gte256(&resMod, r)) {
  428. if (gte256(&resMod, &copyd)) {
  429. minus256(&resMod, &copyd, &resMod);
  430. or256(&resDiv, &adder, &resDiv);
  431. }
  432. shiftr256(&copyd, 1, &copyd);
  433. shiftr256(&adder, 1, &adder);
  434. }
  435. copy256(retDiv, &resDiv);
  436. copy256(retMod, &resMod);
  437. }
  438. }

  439. static void reverseString(char *str, uint32_t length) {
  440. uint32_t i, j;
  441. for (i = 0, j = length - 1; i < j; i++, j--) {
  442. uint8_t c;
  443. c = str[i];
  444. str[i] = str[j];
  445. str[j] = c;
  446. }
  447. }

  448. bool tostring128(uint128_t *number, uint32_t baseParam, char *out,
  449. uint32_t outLength) {
  450. uint128_t rDiv;
  451. uint128_t rMod;
  452. uint128_t base;
  453. copy128(&rDiv, number);
  454. clear128(&rMod);
  455. clear128(&base);
  456. LOWER(base) = baseParam;
  457. uint32_t offset = 0;
  458. if ((baseParam < 2) || (baseParam > 16)) {
  459. return false;
  460. }
  461. do {
  462. if (offset > (outLength - 1)) {
  463. return false;
  464. }
  465. divmod128(&rDiv, &base, &rDiv, &rMod);
  466. out[offset++] = HEXDIGITS[(uint8_t)LOWER(rMod)];
  467. } while (!zero128(&rDiv));
  468. out[offset] = '\0';
  469. reverseString(out, offset);
  470. return true;
  471. }

  472. bool tostring256(uint256_t *number, uint32_t baseParam, char *out,
  473. uint32_t outLength) {
  474. uint256_t rDiv;
  475. uint256_t rMod;
  476. uint256_t base;
  477. copy256(&rDiv, number);
  478. clear256(&rMod);
  479. clear256(&base);
  480. UPPER(LOWER(base)) = 0;
  481. LOWER(LOWER(base)) = baseParam;
  482. uint32_t offset = 0;
  483. if ((baseParam < 2) || (baseParam > 16)) {
  484. return false;
  485. }
  486. do {
  487. if (offset > (outLength - 1)) {
  488. return false;
  489. }
  490. divmod256(&rDiv, &base, &rDiv, &rMod);
  491. out[offset++] = HEXDIGITS[(uint8_t)LOWER(LOWER(rMod))];
  492. } while (!zero256(&rDiv));
  493. out[offset] = '\0';
  494. reverseString(out, offset);
  495. return true;
  496. }