aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / om / agora / test / pow.om
| 1 | note pow.om -- test proof-of-work solver for the Agora wire. |
| 2 | note Build: { printf 'om2\n'; cat sha256.om test/pow.om; } | omc > pow |
| 3 | note Usage: pow <challenge_hex32> <bits> < rest_of_body |
| 4 | note Prints the smallest decimal nonce n such that SHA-256(challenge "\n" n "\n" rest) starts with |
| 5 | note <bits> zero bits, where rest is the request body after the nonce line. |
| 6 | |
| 7 | global g_in uchar 16384 |
| 8 | |
| 9 | fn lzb d view uchar 32 to ulong |
| 10 | z = ulong 0 |
| 11 | for i ulong 32 |
| 12 | c = get d i |
| 13 | if eq c 0 |
| 14 | z = add z 8 |
| 15 | continue |
| 16 | end |
| 17 | for b ulong 8 |
| 18 | if ne and c shr uchar 128 b 0 |
| 19 | ret z |
| 20 | end |
| 21 | z = add z 1 |
| 22 | end |
| 23 | ret z |
| 24 | end |
| 25 | ret z |
| 26 | end |
| 27 | |
| 28 | fn cat b edit uchar 16500 p ulong v ulong to ulong |
| 29 | d = new uchar 20 0 |
| 30 | i = ulong 20 |
| 31 | x = v |
| 32 | for it ulong 20 |
| 33 | i = sub i 1 |
| 34 | put d i uchar add rem x 10 48 |
| 35 | x = div x 10 |
| 36 | if eq x 0 |
| 37 | break |
| 38 | end |
| 39 | end |
| 40 | q = p |
| 41 | for k ulong sub 20 i |
| 42 | put b q get d add i k |
| 43 | q = add q 1 |
| 44 | end |
| 45 | ret q |
| 46 | end |
| 47 | |
| 48 | fn try b edit uchar 16500 n ulong rn ulong nonce ulong need ulong to bool |
| 49 | dg = new uchar 32 0 |
| 50 | q = cat b 33 nonce |
| 51 | put b q 10 |
| 52 | q = add q 1 |
| 53 | for i ulong rn |
| 54 | put b add q i get g_in i |
| 55 | end |
| 56 | sha256_hash view part b 0 add q rn dg |
| 57 | ret ge lzb dg need |
| 58 | end |
| 59 | |
| 60 | fn rdin to ulong |
| 61 | n = ulong 0 |
| 62 | for it ulong 4096 |
| 63 | if ge n 16384 |
| 64 | ret n |
| 65 | end |
| 66 | r = read 0 g_in n sub 16384 n |
| 67 | if le r 0 |
| 68 | ret n |
| 69 | end |
| 70 | n = add n ulong r |
| 71 | end |
| 72 | ret n |
| 73 | end |
| 74 | |
| 75 | fn main to int |
| 76 | b = new uchar 16500 0 |
| 77 | ab = new uchar 64 0 |
| 78 | c = argread 1 b 0 32 |
| 79 | if ne c 32 |
| 80 | ret 2 |
| 81 | end |
| 82 | put b 32 10 |
| 83 | k = argread 2 ab 0 8 |
| 84 | need = ulong 0 |
| 85 | for i ulong ulong k |
| 86 | need = add mul need 10 ulong sub get ab i 48 |
| 87 | end |
| 88 | rn = rdin |
| 89 | nonce = ulong 0 |
| 90 | for hi ulong 65536 |
| 91 | for lo ulong 65536 |
| 92 | if try b 33 rn nonce need |
| 93 | q = cat b 33 nonce |
| 94 | put b q 10 |
| 95 | w = write 1 b 33 sub q 32 |
| 96 | ret 0 |
| 97 | end |
| 98 | nonce = add nonce 1 |
| 99 | end |
| 100 | end |
| 101 | ret 1 |
| 102 | end |