[关闭]
@zhongdao 2017-11-04T17:10:50.000000Z 字数 5712 阅读 1648

代码验证

未分类


对4个字母的哈希验证merkle树.

注意: 不能把少于4个字节的整数传给函数.

php验证1

  1. <?php
  2. $a = hash('sha256', "a", true);
  3. $b = hash('sha256', "b", true);
  4. $c = hash('sha256', "c", true);
  5. $d = hash('sha256', "d", true);
  6. $e = hash('sha256', $a . $b, true);
  7. $f = hash('sha256', $c . $d, true);
  8. $g = hash('sha256', $e . $f);
  9. var_dump($g);
  1. $ php program.php
  2. string(64) "14ede5e8e97ad9372327728f5099b95604a39593cac3bd38a343ad76205213e7"

c验证1

  1. #define TFM_DESC
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #include "string.h"
  5. #include "tfm.h"
  6. #include "tomcrypt.h"
  7. char *get_sha256(char *buffer, int size)
  8. {
  9. hash_state resource;
  10. char *digest;
  11. digest = malloc(32);
  12. sha256_init(&resource);
  13. sha256_process(&resource, buffer, size);
  14. sha256_done(&resource, digest);
  15. return digest;
  16. }
  17. char *get_merkle_root(char *old, int size)
  18. {
  19. char *new, output[32], input[64];
  20. int index;
  21. printf("activation (%d)%c", size, 10);
  22. if (size == 1) {
  23. new = malloc(32);
  24. memcpy(new, old, 32);
  25. free(old);
  26. return new;
  27. }
  28. if (size % 2) {
  29. new = malloc(32 * (size + 1));
  30. memcpy(new, old, size * 32);
  31. memcpy(new + size * 32, old + (size - 1) * 32, 32);
  32. free(old);
  33. return get_merkle_root(new, size + 1);
  34. }
  35. new = malloc(32 * size / 2);
  36. for (index = 0; index < size; index += 2) {
  37. memcpy(input, old + index * 32, 64);
  38. memcpy(new + (index / 2) * 32, get_sha256(input, 64), 32);
  39. }
  40. free(old);
  41. return get_merkle_root(new, size / 2);
  42. }
  43. void print_digest(char *digest) // 用unsigned char* ?
  44. {
  45. int index;
  46. for (index = 0; index < 32; index++)
  47. printf("%02hhx", digest[index]); // 注意这里的hh !
  48. // printf 默认打印
  49. printf("%c", 10);
  50. }
  51. void main()
  52. {
  53. ltc_mp = tfm_desc;
  54. register_prng(&sprng_desc);
  55. register_hash(&sha256_desc);
  56. char *collection, *digest;
  57. collection = malloc(128);
  58. memcpy(collection, get_sha256("a", 1), 32);
  59. memcpy(collection + 32, get_sha256("b", 1), 32);
  60. memcpy(collection + 64, get_sha256("c", 1), 32);
  61. memcpy(collection + 96, get_sha256("d", 1), 32);
  62. print_digest(get_merkle_root(collection, 4));
  63. }

$ tcc -c root.c -o root.o && tcc -o root.elf root.o ../libtomcrypt.a ../libtfm.a && ./root.elf
activation (4)
activation (2)
activation (1)
14ede5e8e97ad9372327728f5099b95604a39593cac3bd38a343ad76205213e7

注意点:

char与signed char, unsigned char的区别?
http://blog.csdn.net/sinat_20265495/article/details/51733015
char 与 unsigned char的本质区别
http://www.cnblogs.com/languoliang/archive/2013/03/15/char.html

C语言中 unsigned char 与 signed char 不同输出格式一些认识
http://blog.csdn.net/ssopp24/article/details/52838210

char, signed char 与 unsigned char 区别

  1. 1.ANSI C 提供了3种字符类型,分别是charsigned charunsigned char
  2. 2.三者都占1个字节
  3. 3.signed char取值范围是 -128 127(有符号位) ; unsigned char 取值范围是 0 255
  4. 4.内存中一串二进制,它的含义,就是这个类型来说明的。
  5. 5.所谓signed char unsigned char 其实是相对“运算”而说的,已经脱离了我们字面含义“字符”,表示的范围有限。
  6. char signed 还是 unsigned ?
  7. C标准中对char Impementation Defined,就是未明确定义
  8. 1)那它由什么定义?坦白说,具体的编译器明确定义,一般都是用signed charunsigned char来实现char的,也就是说不同的编译器对char是什么定义不一样
  9. 2)为什么要这样定义?因为这三种类型的对象在存储介质中的表现形式是一样的(都是一个占8bit01串,只是解析的时候不同)
  10. 3)到底是signed char还是unsigned char?这得看编译器:VC编译器、x86上的GCC都把char定义为signed char,而arm-linux-gcc却把char定义为 unsigned char

创造自己的区块和交易

2个区块与相应交易示例.

  1. #1: 3930
  2. [8629] -> YUANXUN(100)
  3. #2: 4479
  4. [4140] -> YUANXUN(100)
  5. [7159] YUANXUN/8629/1 -> YUANXUN(99) PANPENG(1)

实现代码

初始设置

  1. /*
  2. INITIAL = 33000000
  3. RATE = (60 / PERIOD) * 24 * 365
  4. SUPPLY(HEIGHT) = INITIAL * (1 + 0.04 / RATE) ^ HEIGHT
  5. DEMURRAGE(HEIGHT) = SUPPLY(HEIGHT) - SUPPLY(HEIGHT - 1)
  6. 30 * 24 * 365 = 262,800.
  7. 16,000,000 * (1 + 0.4 / 262800)^262800.
  8. */
  1. /* 参数说明: 初始发行的币, 几分钟生成1个区块, ? */
  2. uint64_t calculate_supply(uint64_t initial, uint32_t period, uint32_t height) {
  3. double _0687, _8326, _3014;
  4. _0687 = (double) initial;
  5. _8326 = (60 * 24 * 365) / period;
  6. _3014 = _0687 * pow(1 + 0.04 / _8326, height);
  7. return ((uint64_t) _3014);
  8. }

因为ecc的签名算法,随机数的原因,输出签名的长度在69-71之间变化.
wc 的最后1列表示字节数.

  1. $ ./engine |wc
  2. 1 13 474
  3. $ ./engine |wc
  4. 2 15 473
  5. $ ./engine |wc
  6. 1 14 472
  7. $ ./engine |wc
  8. 2 14 473
  9. $ ./engine |wc
  10. 1 14 473
  1. $ xxd 1.block
  2. 0000000: bb7e 00b5 e58d 54c3 cd08 8333 596e dcbf .~....T....3Yn..
  3. 0000010: 13f0 b820 fa92 345a 157d 065c 6fd6 266d ... ..4Z.}.\o.&m
  4. 0000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................
  5. 0000030: 0000 0000 0000 0000 0000 0000 0000 0000 ................
  6. 0000040: 0000 0000 ffff ffff ffff ffff ffff ffff ................
  7. 0000050: ffff ffff ffff ffff ffff ffff ffff ffff ................
  8. 0000060: ffff ffff bf7f fd59 0100 0000 5c00 0000 .......Y....\...
  9. 0000070: 0100 0000 0100 0000 3000 0000 0000 0000 ........0.......
  10. 0000080: 0000 0000 0000 0000 0000 0000 0000 0000 ................
  11. 0000090: 0000 0000 0000 0000 0000 0000 0000 0000 ................
  12. 00000a0: 0000 0000 0000 0000 2000 0000 1000 0000 ........ .......
  13. 00000b0: 7afd 3bd3 f876 930d be40 b59e 9ba8 6b31 z.;..v...@....k1
  14. 00000c0: 6400 0000 0000 0000 d.......
  1. $ xxd 2.block
  2. 0000000: 58c1 5725 2eee cb38 94d0 e962 2976 87d3 X.W%...8...b)v..
  3. 0000010: 4ee0 8154 4b5e cc28 6e64 041d 89ec 8a7e N..TK^.(nd.....~
  4. 0000020: 0000 0000 bdca ccd1 f766 4054 c719 9f36 .........f@T...6
  5. 0000030: 80d5 2309 1bd7 d911 7650 d681 dcc7 e131 ..#.....vP.....1
  6. 0000040: aeea 8a33 ffff ffff ffff ffff ffff ffff ...3............
  7. 0000050: ffff ffff ffff ffff ffff ffff ffff ffff ................
  8. 0000060: ffff ffff ce7f fd59 0200 0000 5c00 0000 .......Y....\...
  9. 0000070: 0100 0000 0100 0000 3000 0000 0000 0000 ........0.......
  10. 0000080: 0000 0000 0000 0000 0000 0000 0000 0000 ................
  11. 0000090: 0000 0000 0000 0000 0000 0000 0000 0000 ................
  12. 00000a0: 0000 0000 0000 0000 2000 0000 1000 0000 ........ .......
  13. 00000b0: 7afd 3bd3 f876 930d be40 b59e 9ba8 6b31 z.;..v...@....k1
  14. 00000c0: 6400 0000 0000 0000 1101 0000 0100 0000 d...............
  15. 00000d0: 0200 0000 c500 0000 0100 0000 4e00 0000 ............N...
  16. 00000e0: 304c 0302 0700 0201 2002 207f 5e56 4655 0L...... . .^VFU
  17. 00000f0: 10ab 820d 50a1 9a4b 5cfd b974 1c5f 13e9 ....P..K\..t._..
  18. 0000100: 79dc 672b 413f ab9c ec8c b502 2100 f8ce y.g+A?......!...
  19. 0000110: 98b2 ca0a 465e ba4d fbf0 a950 08b5 c661 ....F^.M...P...a
  20. 0000120: cc77 6593 6962 ddb1 a8f4 d2e8 10bf 4700 .we.ib........G.
  21. 0000130: 0000 3045 0220 5e64 de30 5cd7 a2d5 107c ..0E. ^d.0\....|
  22. 0000140: 67e9 e5a5 b5b7 4f48 8715 d85c a587 7c98 g.....OH...\..|.
  23. 0000150: 89ea 13b3 5863 0221 00a1 816a ffbd 9b35 ....Xc.!...j...5
  24. 0000160: b2cc e4d0 29ed 981b abd0 fe63 ee8b 63ff ....)......c..c.
  25. 0000170: 47e0 8b10 8758 93f0 d7bb 7e00 b5e5 8d54 G....X....~....T
  26. 0000180: c3cd 0883 3359 6edc bf13 f0b8 20fa 9234 ....3Yn..... ..4
  27. 0000190: 5a15 7d06 5c6f d626 6d20 0000 0010 0000 Z.}.\o.&m ......
  28. 00001a0: 007a fd3b d3f8 7693 0dbe 40b5 9e9b a86b .z.;..v...@....k
  29. 00001b0: 3163 0000 0000 0000 0020 0000 0010 0000 1c....... ......
  30. 00001c0: 0019 fda2 6e18 9d51 a452 6e1d 23cc ce56 ....n..Q.Rn.#..V
  31. 00001d0: 2401 0000 0000 0000 00 $........
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注