Showing my work: /:~~.hex_header 0123456789abcdef #:i.16 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 '0123456789abcdef'i.hex_header 0 1 0 0 0 0 0 0 8 1 12 13 0 2 10 11 7 14 5 6 9 14 8 11 12 13 9 3 1 7 14 2 15 14 9 9 15 2 13 14 4 4 13 4 9 10 11 2 11 8 8 5 1 11 10 4 10 3 0 8 0 0 0 0 0 0 0 0 0 0 0 0 14 3 2 0 11 6 12 2 15 15 15 12 8 13 7 5 0 4 2 3 13 11 8 11 1 14 11 9 4 2 10 14 7 1 0 14 9 ...
So, ,(#:i.16){~'0123456789abcdef'i.hex_header 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 1 0 1 1 1 1 1 1 0 0 1 0 1 0 1 1 0 1 0 0 1 1 1 1 0 1 0 0 0 1 0 1 1 1 1 0 0 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 0 1 1 1 1 1 1 0 0 0 1 0 ... On Sun, Apr 12, 2020 at 4:40 AM bill lam <bbill....@gmail.com> wrote: > try > _3&(128!:6)^:2 bfh header_hex > > On Sun, Apr 12, 2020, 3:50 PM Thomas McGuire <tmcguir...@gmail.com> wrote: > > > In a round about way (it started with reading slashdot) I was researching > > Bitcoin hashing. I came across a python explanation of taking a bitcoin > > header and SHA256 hashing it twice to see if it produces the desired > number > > of leading (or terminating depending on the endian of your machine) '0’s. > > > > The snippet of python code is here at a bitcoin wiki site: > > https://en.bitcoin.it/wiki/Block_hashing_algorithm < > > https://en.bitcoin.it/wiki/Block_hashing_algorithm> > > > > I won’t reproduce it in its entirety here since it’s only a few lines of > > code I figured I should be able to do the same in J. > > > > First I grabbed the header as ascii hex > > > > hex_header =: > > > '0100000081cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122bc7f5d74df2b9441a42a14695' > > > > Then came the issue of turning this into binary. After reviewing the J > > programming archives finding a thread on packed python structures, a few > > attempts in J and some code in C to make sure I was doing it right, I > came > > to the realization that I only had to index pairs of hex characters into > > ‘a.’ After using the ‘dfh’ function to convert the pair. So I made a bfs > > tacit to do the work: > > > > bfh =: a. {~ [: dfh _2 ]\ ] > > > > (As an aside I cheated and used the 13 : functional form and let J figure > > out the tacit.) > > > > NB. Python double hash: > > hashlib.sha256(hashlib.sha256(header_bin).digest()).digest(). > > > > 3&(128!:6)^:2 bfh header_hex NB. Seemingly direct implementation > in J > > 8ac7142a625bdd47e177ab584d60de449d0a844eb56173bd1c9aa0dbe69b61a4 > > > > Now the above is the incorrect answer. The reason being is that our SHA > > foreigns convert the answer from underlying binary to a hex string > > representation. I need to convert the answer with ‘bfh’ and then call the > > SHA256 algorithm again: > > > > 3&(128!:6) bfh 3&(128!:6) bfh header_hex > > 1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000 > > > > This is the correct double hash that is agreement with the wiki article > > listed above. > > > > So 2 questions from all of this: > > > > 1) is there a more efficient way of converting a string of hex characters > > into a binary character array? > > > > 2) Shouldn’t the SHA algorithms send the raw binary back so that you can > > use the power conjunction with it to do multiple hashes? > > ---------------------------------------------------------------------- > > For information about J forums see http://www.jsoftware.com/forums.htm > > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > -- Devon McCormick, CFA Quantitative Consultant ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm