I was going off https://en.wikipedia.org/wiki/SHA-256 (pseudocode section)


----- Original Message -----
From: Raul Miller <rauldmil...@gmail.com>
To: Programming forum <programm...@jsoftware.com>
Cc: 
Sent: Wednesday, February 26, 2014 3:37:08 PM
Subject: Re: [Jprogramming] using dll's in wine (linux/mac) (also a windows 
implementation of hash functions)

I might try to get sha256 working (not today - I have other things I
need to get done today). But before I could do that, I'd need to find
a copy of the relevant standard (otherwise I will not have a clue how
to debug it).

It's honestly not all that hard to do, you just need to read the
standard and read the code (and conduct tests to help you understand
the code and isolate any errors). A lot of it is just proofreading.
Though with that many numbers, I'd be tempted to use copy and paste
from a standards document to verify all the details.

And, once you have a working implementation, it gets even easier
because then you can just look for where things start deviating from
what they should be.

Thanks,

-- 
Raul

On Wed, Feb 26, 2014 at 2:06 PM, Pascal Jasmin <godspiral2...@yahoo.ca> wrote:
> That is cool Raul, since you are interested in this, allow me to show you 
> some native code for sha256 that does not produce correct results.  I 
> originally took code from (I think Michael's github page) that did not work.  
> I am probably not doing the rotate/shift correctly.
>
> Earlier this morning, I posted code linked to OpenSSL library which I 
> understand is available on all platforms.
>
> This code does not work:
> NB. SHA-256 implementation(s) in J
> NB. ==========================================================================
>
> NB. these are the basic operations we use.
> V=: 23 b.             NB. bitwise or
> X=: 22 b.             NB. bitwise xor
> A=: 17 b.             NB. bitwise and
> N=: 30 b.             NB. bitwise nand
> G=:{~                 NB. a G i -> a[i]
> P=: 16bffffffff A +   NB. 32 bit unsigned addition
> LSH=: 33 b.           NB. eg: 1 2 LSH 8 -> 16 32
> RSH=: -@:[ 33 b. ]    NB. eg: 1 2 RSH 8 -> 4 2
>
> NB. R is an operation that rotates the bits of uint32 y rightward by x bits
> NB. it is the primitive instruction 'ROR' on intel cpus
> NB. R :: u5 -> u32 -> u32
> NB. ==========================================================================
> R =: 4 : 0
>   NB. TODO: x (32 b.) y  is left rotate in j... try just:  -@:[ 32 b. ]
>   (x RSH y) V ((32-x) LSH y)
> )
> R =: 32 b.
> NB. XR xors the result of multiple rotations of y
> NB. XR :: u5[i] -> u32 -> u32
> NB. ==========================================================================
> XR =: 4 : 0
>   X / x R y
> )
> XR =:   X /@:R
> NB. k is just an array of 64 constants
> NB. ==========================================================================
> k=:   16b428a2f98 16b71374491 16bb5c0fbcf 16be9b5dba5
> k=:k, 16b3956c25b 16b59f111f1 16b923f82a4 16bab1c5ed5
> k=:k, 16bd807aa98 16b12835b01 16b243185be 16b550c7dc3
> k=:k, 16b72be5d74 16b80deb1fe 16b9bdc06a7 16bc19bf174
> k=:k, 16be49b69c1 16befbe4786 16b0fc19dc6 16b240ca1cc
> k=:k, 16b2de92c6f 16b4a7484aa 16b5cb0a9dc 16b76f988da
> k=:k, 16b983e5152 16ba831c66d 16bb00327c8 16bbf597fc7
> k=:k, 16bc6e00bf3 16bd5a79147 16b06ca6351 16b14292967
> k=:k, 16b27b70a85 16b2e1b2138 16b4d2c6dfc 16b53380d13
> k=:k, 16b650a7354 16b766a0abb 16b81c2c92e 16b92722c85
> k=:k, 16ba2bfe8a1 16ba81a664b 16bc24b8b70 16bc76c51a3
> k=:k, 16bd192e819 16bd6990624 16bf40e3585 16b106aa070
> k=:k, 16b19a4c116 16b1e376c08 16b2748774c 16b34b0bcb5
> k=:k, 16b391c0cb3 16b4ed8aa4a 16b5b9cca4f 16b682e6ff3
> k=:k, 16b748f82ee 16b78a5636f 16b84c87814 16b8cc70208
> k=:k, 16b90befffa 16ba4506ceb 16bbef9a3f7 16bc67178f2
>
> NB. k =: 2553252418 2437166961 3489382581 2782639593 1539462713 4044484953 
> 2759999378 3579714731 2561279960 22774546   3196399908 3279752277 1952300658 
> 4273069696 2802244763 1961991105 3244923876 2252848879 3332227343 3433106468 
> 1865214253 2860807242 3702108252 3666409846 1381056152 1841705384 3358000048 
> 3347012031 4077641926 1200728021 1365494278 1730750740 2232071975 941693742  
> 4235013197 319633491  1416825445 3138022006 784974465  2234282642 2716385186 
> 1264982696 1888177090 2740022471 434672337  604412374  2234846964 1889561104 
> 381789209  141309726  1282885671 3049041972 3003915321 1252710478 1338678363 
> 4084149864 4001533812 1868801400 343459972  134399884  4211064464 3949744292 
> 4154718654 4067979718
>
> NB. W :: u32[16] -> u32[64]
> NB. ==========================================================================
> NB. the first 16 entries are simply y
> W =: 3 : 0@: fromtext
>    w =.  y
>   NB. these are scrambled to create the other 48 entries.
>   NB. note that referencing (ii - 2){w  makes this an
>   NB. inherently sequential process.
>   for_ii. 16 + i.48 do.
>     'wa wb wc wd' =. (ii - 2 7 15 16) { w
>     w =. w, ((_17&R X _19&R X _10&LSH ) wa) P wb P ((_7&R X _18&R X _3&LSH) 
>wc) P wd
>   end.
> )
>
> fromtext =: 3 : '(0,~#m) ,~ _4 tobyte32 inv@:|.\ p=. 56{. 128,~ m =. a. i. y'
> NB. fromtext =: 3 : '(0,#m) ,~ _4 tobyte32 inv\ p=. 56{. 128,~ m =. a. i. y'
> NB. sha256 :: u32[16] -> u32[16]
> NB. ==========================================================================
>
>
> NB. version 1: straightforward, imperative loop
> NB. --------------------------------------------------------------------------
> sha256v1 =: 3 :0
>   w =. W y
>    'a b c d e f g h' =.  H =.  1743128938 2242799547 1928556092 989155237 
>2136084049 2355627419 2883158815 432922715
>
>   NB.'a b c d e f g h' =.  H =.   16b6a09e667 16bbb67ae85 16b3c6ef372 
>16ba54ff53a 16b510e527f 16b9b05688c 16b1f83d9ab 16b5be0cd19
>
>   for_ii. i. 64 do.
>     t1 =. P/ h, (_6 _11 _25 & XR e), ((f A e) X (e 28 b. 1) A g), (k&G , w&G) 
>ii
>     t2 =. P/ (_2 _13 _22 XR a), X/((b A c), b&A , a&A) c
>   H =. H +  'a b c d e f g h' =. (t1 P t2), a, b, c, (d P t1), e, f, g
>   end.
>   a,b,c,d,e,f,g,h
> )
>
>
>
>
>
> ----- Original Message -----
> From: Raul Miller <rauldmil...@gmail.com>
> To: Programming forum <programm...@jsoftware.com>
> Cc:
> Sent: Wednesday, February 26, 2014 11:32:45 AM
> Subject: Re: [Jprogramming] using dll's in wine (linux/mac) (also a windows 
> implementation of hash functions)
>
> I threw together a slightly faster version of that J implementation of
> SHA-1 (because it's looking like I'm going to need a hashing
> capability). Unfortunately, I only got a factor of 2 speed
> improvement. It's up on rosettacode.
>
> Thanks,
>
> --
> Raul
>
>
> On Tue, Feb 25, 2014 at 8:46 PM, Raul Miller <rauldmil...@gmail.com> wrote:
>> There are some obvious ways to speed up that J implementation. I just
>> did not want to take the time to do them. Maybe later.
>>
>> Thanks,
>>
>> --
>> Raul
>>
>> On Tue, Feb 25, 2014 at 8:29 PM, Pascal Jasmin <godspiral2...@yahoo.ca> 
>> wrote:
>>> That is cool.  It works too (md5 in convert does not produce the test 
>>> results).  Your version is 20x slower but still usable.
>>>
>>>
>>>
>>>
>>> ----- Original Message -----
>>> From: Raul Miller <rauldmil...@gmail.com>
>>> To: Programming forum <programm...@jsoftware.com>
>>> Cc:
>>> Sent: Tuesday, February 25, 2014 8:01:43 PM
>>> Subject: Re: [Jprogramming] using dll's in wine (linux/mac) (also a windows 
>>> implementation of hash functions)
>>>
>>> I haven't used WINE for ages, and never did learn how to debug for it.
>>> Also, I've mostly been using native windows (and my other laptops are
>>> openbsd, though I've not had the time to use them much, lately.).
>>>
>>> I did write an SHA-1 in J though:
>>> http://rosettacode.org/wiki/SHA-1#J
>>>
>>> Yours is probably much faster, and likely to stay that way for some
>>> time since we do not have a J compiler. (I'd really like to define a J
>>> subset front end for gcc).
>>>
>>> Thanks,
>>>
>>> --
>>> Raul
>>>
>>>
>>>
>>> On Tue, Feb 25, 2014 at 7:50 PM, Pascal Jasmin <godspiral2...@yahoo.ca> 
>>> wrote:
>>>> I've posted some hash functions sha1 sha2 and md5 that use Microsoft's 
>>>> advapi32.dll that comes with windows.  Could someone let me know what/if 
>>>> changes are needed to work with wine?
>>>>
>>>> http://www.jsoftware.com/jwiki/PascalJasmin/SHA%201%2C%202%20and%20MD5%20for%20windows
>>>>
>>>> ----------------------------------------------------------------------
>>>> For information about J forums see http://www.jsoftware.com/forums.htm

>>> ----------------------------------------------------------------------
>>> For information about J forums see http://www.jsoftware.com/forums.htm
>>>
>>> ----------------------------------------------------------------------
>>> For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to