Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #17811 > unrolled thread
| Started by | mhx@iae.nl (Marcel Hendrix) |
|---|---|
| First post | 2012-12-02 23:48 +0200 |
| Last post | 2012-12-08 22:08 +0200 |
| Articles | 11 on this page of 31 — 5 participants |
Back to article view | Back to comp.lang.forth
SHA-512 mhx@iae.nl (Marcel Hendrix) - 2012-12-02 23:48 +0200
Re: SHA-512 jzakiya@gmail.com - 2012-12-07 14:47 -0800
Re: SHA-512 Coos Haak <chforth@hccnet.nl> - 2012-12-08 00:49 +0100
Re: SHA-512 jzakiya@gmail.com - 2012-12-07 16:54 -0800
Re: SHA-512 Coos Haak <chforth@hccnet.nl> - 2012-12-08 18:21 +0100
Re: SHA-512 jzakiya@gmail.com - 2012-12-08 18:36 -0800
Re: SHA-512 mhx@iae.nl (Marcel Hendrix) - 2012-12-09 09:14 +0200
Re: SHA-512 jzakiya@gmail.com - 2012-12-09 17:23 -0800
Re: SHA-512 jzakiya@gmail.com - 2012-12-09 17:29 -0800
Re: SHA-512 jzakiya@gmail.com - 2012-12-09 20:46 -0800
Re: SHA-512 m.a.m.hendrix@tue.nl - 2012-12-10 03:53 -0800
Re: SHA-512 jzakiya@gmail.com - 2012-12-10 13:05 -0800
Re: SHA-512 mhx@iae.nl (Marcel Hendrix) - 2012-12-10 23:17 +0200
Re: SHA-512 jzakiya@gmail.com - 2012-12-13 06:30 -0800
Re: SHA-512 mhx@iae.nl (Marcel Hendrix) - 2012-12-13 21:33 +0200
Re: SHA-512 jzakiya@gmail.com - 2012-12-13 19:38 -0800
Re: SHA-512 mhx@iae.nl (Marcel Hendrix) - 2012-12-14 22:52 +0200
Re: SHA-512 jzakiya@gmail.com - 2012-12-14 16:39 -0800
Re: SHA-512 mhx@iae.nl (Marcel Hendrix) - 2012-12-15 19:11 +0200
Re: SHA-512 mhx@iae.nl (Marcel Hendrix) - 2012-12-16 16:00 +0200
Re: SHA-512 jzakiya@gmail.com - 2012-12-16 17:33 -0800
Re: SHA-512 jzakiya@gmail.com - 2012-12-16 22:35 -0800
Re: SHA-512 mhx@iae.nl (Marcel Hendrix) - 2012-12-17 10:14 +0200
Re: SHA-512 mhx@iae.nl (Marcel Hendrix) - 2012-12-17 10:51 +0200
Re: SHA-512 jzakiya@gmail.com - 2012-12-17 09:13 -0800
Re: SHA-512 mhx@iae.nl (Marcel Hendrix) - 2012-12-17 22:24 +0200
Re: SHA-512 jzakiya@gmail.com - 2013-01-15 20:38 -0800
Re: SHA-512 jzakiya@gmail.com - 2013-01-15 20:54 -0800
Re: SHA-512 Howerd <howerdo@yahoo.co.uk> - 2012-12-13 13:06 -0800
Re: SHA-512 Howerd <howerdo@yahoo.co.uk> - 2012-12-13 13:03 -0800
Re: SHA-512 mhx@iae.nl (Marcel Hendrix) - 2012-12-08 22:08 +0200
Page 2 of 2 — ← Prev page 1 [2]
| From | jzakiya@gmail.com |
|---|---|
| Date | 2012-12-16 17:33 -0800 |
| Message-ID | <cd9d5192-14e2-46d9-8117-84df2b72d9cd@googlegroups.com> |
| In reply to | #18039 |
On Sunday, December 16, 2012 9:00:07 AM UTC-5, Marcel Hendrix wrote:
> mhx@iae.nl (Marcel Hendrix) writes Re: SHA-512
>
>
>
> > jzakiya@gmail.com writes Re: SHA-512
>
> [..]
>
> > I have looked at my own (PICK-less) idea of SHA-512 again, and it couldn't work.
>
> > However, I found a way to improve your algorithm (literalize H[x]). ATM test3
>
> > runs in 11.048 seconds on my 2.66 GHz i7 system. SHAfile needs 2.741 seconds
>
> > to process 385,241,618 bytes (64.9.. 50 MB/s/GHz). Hopefully your fix of 15 PICK
>
> > will create additional room for speedup.
>
> [..]
>
>
>
> The PICK fix works now too, but the speedup is only slight.
>
> Performance is 65.95 MB/s/GHz for strings, 52.16 MB/s/GHz for file.
>
> This should be faster than the NIST reference (99MB/s on a 1.8 GHz Core2).
>
>
>
> The following code is not portable.
>
>
>
> -marcel
>
> --
>
> (*
>
> * LANGUAGE : ANS Forth with extensions
>
> * PROJECT : Forth Environments
>
> * DESCRIPTION : SHA-512 64-bit Hash algorithm
>
> * CATEGORY : Utility
>
> * AUTHOR : Copyright (c) 2012 Jabari Zakiya -- jzakiya@mail.com 12/07/2012
>
> * LAST CHANGE : December 16, 2012, Marcel Hendrix
>
> *)
>
>
>
>
>
>
>
> NEEDS -miscutil
>
>
>
> REVISION -sha-512 "--- SHA-512 Version 1.01 ---"
>
>
>
> PRIVATES
>
>
>
> DOC
>
> (*
>
> NIST spec at: http://csrc.nist.gov/encryption/tkhash.html
>
>
>
> FORTH> SHAtest
>
> SHA-512 test suite:
>
>
>
> ""
>
> cf83e1357eefb8bd f1542850d66d8007 d620e4050b5715dc 83f4a921d36ce9ce 47d0d13c5d85f2b0 ff8318d2877eec2f 63b931bd47417a81 a538327af927da3e
>
> cf83e1357eefb8bd f1542850d66d8007 d620e4050b5715dc 83f4a921d36ce9ce 47d0d13c5d85f2b0 ff8318d2877eec2f 63b931bd47417a81 a538327af927da3e
>
>
>
> "abc"
>
> ddaf35a193617aba cc417349ae204131 12e6fa4e89a97ea2 0a9eeee64b55d39a 2192992a274fc1a8 36ba3c23a3feebbd 454d4423643ce80e 2a9ac94fa54ca49f
>
> ddaf35a193617aba cc417349ae204131 12e6fa4e89a97ea2 0a9eeee64b55d39a 2192992a274fc1a8 36ba3c23a3feebbd 454d4423643ce80e 2a9ac94fa54ca49f
>
>
>
> "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
>
> 8e959b75dae313da 8cf4f72814fc143f 8f7779c6eb9f7fa1 7299aeadb6889018 501d289e4900f7e4 331b99dec4b5433a c7d329eeb6dd2654 5e96e55b874be909
>
> 8e959b75dae313da 8cf4f72814fc143f 8f7779c6eb9f7fa1 7299aeadb6889018 501d289e4900f7e4 331b99dec4b5433a c7d329eeb6dd2654 5e96e55b874be909
>
>
>
> "The quick brown fox jumps over the lazy dog"
>
> 07e547d9586f6a73 f73fbac0435ed769 51218fb7d0c8d788 a309d785436bbb64 2e93a252a954f239 12547d1e8a3b5ed6 e1bfd7097821233f a0538f3db854fee6
>
> 07e547d9586f6a73 f73fbac0435ed769 51218fb7d0c8d788 a309d785436bbb64 2e93a252a954f239 12547d1e8a3b5ed6 e1bfd7097821233f a0538f3db854fee6
>
>
>
> 2 million copies of ASCII 'a' (61h)
>
> 9bc68759247e3332 bec1c79d128d28a8 931d0c9f96c8aa97 5731b563475fdddd df7f873c25086908 effe270e23c5a01e 5dfb3289bf5d091d 8fb454b1bcf98dda
>
>
>
> 400,000 copies of ASCII BL (20h)
>
> 19ef4876e03c9347 6e00b486c62108d4 d7136f6d4782817b 449bacf44d0963e0 3f5ab474cb6822c2 963a979e8e15298c d60f22841c51f616 1620c0e031c65f4a
>
> ok
>
>
>
> FORTH> SHAbm
>
> SHA-512 test for EX1, 1000 loops: 0.001 seconds elapsed.
>
> SHA-512 test for EX2, 1000 loops: 0.003 seconds elapsed.
>
> SHA-512 test for EX3, 1000 loops: 10.872 seconds elapsed. ok
>
>
>
> 2e6 (bytes) 1000e f* ( loops) 10.872e ( s) f/ 2.66e ( GHz) f/ 20e f2^x ( 1MB) f/ f. ( 65.95 MB/s/GHz )
>
> ( NIST ref.: 99 MB/s on 1.8 GHz Core2 )
>
>
>
> FORTH> TIMER-RESET S" C:\idfwforth\df_snapshot_4.0_2010_08_19.tar.gz" @SHAfile .ELAPSED
>
> Bytesize: 385241618
>
> SHA-512 : 45800c7d8fe7c13f 6670176b93b82c34 6f6f072ff1f50aee 586ccd6e7b02ef7c e27eb8f5a3cb814a 74e1d18b3107ea9b 8fdf171bf573a420 2a666c67703437bf
>
> 2.648 seconds elapsed. ok
>
> 385241618e ( bytes) 2.741e ( s) f/ 2.66e ( GHz) f/ 20e f2^x ( 1MB) f/ f. ( 52.16 MB/s/GHz )
>
> *)
>
> ENDDOC
>
>
>
> #64 =: CELLSIZE PRIVATE -- CPU bitsize
>
>
>
> 0. DVALUE SHAlen PRIVATE -- Holds byte length of string < 2^128 bits|2^125 bytes
>
> CREATE SHAval PRIVATE 8 CELLS ALLOT -- Holds hash after each block
>
> CREATE SHAsh PRIVATE #88 CELLS ALLOT -- Fully extended hash array
>
> CREATE W PRIVATE #16 CELLS ALLOT -- Holds message block
>
> ALIGN1024 -- this helps, why??
>
> CREATE X PRIVATE #80 CELLS ALLOT -- Holds temp processed data
>
>
>
> 0 VALUE =X PRIVATE -- Pointer to addr of temp data
>
> 0 VALUE =H PRIVATE -- Pointer to addr of hash value H for each round
>
> CREATE cbuffer PRIVATE $8000 CHARS ALLOT
>
>
>
> : >cb cbuffer CELLPLACE+ BL cbuffer CELLCHAR+ ; PRIVATE ( c-addr u -- )
>
> : InitMake SHAsh TO =H X TO =X cbuffer 0! S" : SHA512 ( -- )" >cb ; PRIVATE
>
> : EndMake S" ;" >cb cbuffer @+ EVALUATE ; PRIVATE
>
> : :=: CREATE PRIVATE , DOES> @ (H.) >cb ; PRIVATE
>
>
>
> $428a2f98d728ae22 :=: K0 $7137449123ef65cd :=: K1 $b5c0fbcfec4d3b2f :=: K2 $e9b5dba58189dbbc :=: K3
>
> $3956c25bf348b538 :=: K4 $59f111f1b605d019 :=: K5 $923f82a4af194f9b :=: K6 $ab1c5ed5da6d8118 :=: K7
>
> $d807aa98a3030242 :=: K8 $12835b0145706fbe :=: K9 $243185be4ee4b28c :=: K10 $550c7dc3d5ffb4e2 :=: K11
>
> $72be5d74f27b896f :=: K12 $80deb1fe3b1696b1 :=: K13 $9bdc06a725c71235 :=: K14 $c19bf174cf692694 :=: K15
>
> $e49b69c19ef14ad2 :=: K16 $efbe4786384f25e3 :=: K17 $0fc19dc68b8cd5b5 :=: K18 $240ca1cc77ac9c65 :=: K19
>
> $2de92c6f592b0275 :=: K20 $4a7484aa6ea6e483 :=: K21 $5cb0a9dcbd41fbd4 :=: K22 $76f988da831153b5 :=: K23
>
> $983e5152ee66dfab :=: K24 $a831c66d2db43210 :=: K25 $b00327c898fb213f :=: K26 $bf597fc7beef0ee4 :=: K27
>
> $c6e00bf33da88fc2 :=: K28 $d5a79147930aa725 :=: K29 $06ca6351e003826f :=: K30 $142929670a0e6e70 :=: K31
>
> $27b70a8546d22ffc :=: K32 $2e1b21385c26c926 :=: K33 $4d2c6dfc5ac42aed :=: K34 $53380d139d95b3df :=: K35
>
> $650a73548baf63de :=: K36 $766a0abb3c77b2a8 :=: K37 $81c2c92e47edaee6 :=: K38 $92722c851482353b :=: K39
>
> $a2bfe8a14cf10364 :=: K40 $a81a664bbc423001 :=: K41 $c24b8b70d0f89791 :=: K42 $c76c51a30654be30 :=: K43
>
> $d192e819d6ef5218 :=: K44 $d69906245565a910 :=: K45 $f40e35855771202a :=: K46 $106aa07032bbd1b8 :=: K47
>
> $19a4c116b8d2d0c8 :=: K48 $1e376c085141ab53 :=: K49 $2748774cdf8eeb99 :=: K50 $34b0bcb5e19b48a8 :=: K51
>
> $391c0cb3c5c95a63 :=: K52 $4ed8aa4ae3418acb :=: K53 $5b9cca4f7763e373 :=: K54 $682e6ff3d6b2b8a3 :=: K55
>
> $748f82ee5defb2fc :=: K56 $78a5636f43172f60 :=: K57 $84c87814a1f0ab72 :=: K58 $8cc702081a6439ec :=: K59
>
> $90befffa23631e28 :=: K60 $a4506cebde82bde9 :=: K61 $bef9a3f7b2c67915 :=: K62 $c67178f2e372532b :=: K63
>
> $ca273eceea26619c :=: K64 $d186b8c721c0c207 :=: K65 $eada7dd6cde0eb1e :=: K66 $f57d4f7fee6ed178 :=: K67
>
> $06f067aa72176fba :=: K68 $0a637dc5a2c898a6 :=: K69 $113f9804bef90dae :=: K70 $1b710b35131c471b :=: K71
>
> $28db77f523047d84 :=: K72 $32caab7b40c72493 :=: K73 $3c9ebe0a15c9bebc :=: K74 $431d67c49c100d4c :=: K75
>
> $4cc5d4becb3e42b6 :=: K76 $597f299cfc657e2a :=: K77 $5fcb6fab3ad6faec :=: K78 $6c44198c4a475817 :=: K79
>
>
>
> : H[H] =H (H.) >cb ; PRIVATE -- Return H addr
>
> : H[G] =H 1 CELLS + (H.) >cb ; PRIVATE -- Return G addr
>
> : H[F] =H 2 CELLS + (H.) >cb ; PRIVATE -- Return F addr
>
> : H[E] =H 3 CELLS + (H.) >cb ; PRIVATE -- Return E addr
>
> : H[D] =H 4 CELLS + (H.) >cb ; PRIVATE -- Return D addr
>
> : H[C] =H 5 CELLS + (H.) >cb ; PRIVATE -- Return C addr
>
> : H[B] =H 6 CELLS + (H.) >cb ; PRIVATE -- Return B addr
>
> : H[A] =H 7 CELLS + (H.) >cb ; PRIVATE -- Return A addr
>
>
>
> : SHAinit ( -- ) \ Load initial hash values H0 - H7
>
> $6a09e667f3bcc908 ( H0) $bb67ae8584caa73b ( H1)
>
> $3c6ef372fe94f82b ( H2) $a54ff53a5f1d36f1 ( H3)
>
> $510e527fade682d1 ( H4) $9b05688c2b3e6c1f ( H5)
>
> $1f83d9abfb41bd6b ( H6) $5be0cd19137e2179 ( H7)
>
> SHAsh 7 0 DO TUCK ! CELL+ LOOP ! \ Put initial hash in SHAsh array
>
> SHAsh SHAval 8 CELLS CMOVE ; PRIVATE \ Put copy in SHAval array
>
>
>
> : UpDateHash ( a b c -- ) \ Update hash values and load arrays with new values
>
> 8 0 DO DUP >R @ SWAP DUP >R @ + DUP \ Compute updated hash subvalue
>
> R@ ! OVER ! CELL+ R> CELL+ R> CELL+ \ Store updated hash subvalue
>
> LOOP 3DROP ; PRIVATE \ Clear stack when done
>
>
>
> : >offs ( u -- addr ) CELLS =X + (H.) >cb ; PRIVATE
>
>
>
> : sig0 DUP DUP 1 ROR SWAP 8 ROR XOR SWAP 7 RSHIFT XOR ; PRIVATE ( x -- n )
>
> : sig1 DUP DUP #19 ROR SWAP #61 ROR XOR SWAP 6 RSHIFT XOR ; PRIVATE ( x -- n )
>
> : Wi #-16 >offs S" D@ sig0 +" >cb -7 >offs S" @ +" >cb -2 >offs S" @ sig1 + DUP" >cb 0 >offs S" !" >cb CELL +TO =X ; PRIVATE ( ..Wi --..Wi' Wi')
>
> : Wi@ S" DUP @ DUP" >cb 0 >offs S" !" >cb CELL +TO =X ; PRIVATE \ dup @ tuck
>
> : Ch H[F] S" 2@ OVER AND SWAP INVERT" >cb H[G] S" @ AND XOR" >cb ; PRIVATE ( -- n )
>
> : Maj H[C] S" DUP >R CELL+ 2@ OVER AND SWAP R@ @ AND XOR R> 2@ AND XOR" >cb ; PRIVATE ( -- n )
>
> : T1x Ch H[E] S" @ DUP >R #14 ror R@ #18 ror XOR R> #41 ror XOR +" >cb H[H] S" @ +" >cb ; PRIVATE ( -- n )
>
> : T2 Maj H[A] S" @ DUP >R #28 ror R@ #34 ror XOR R> #39 ror XOR +" >cb ; PRIVATE ( -- n )
>
> : subrnd S" DUP" >cb H[D] S" +!" >cb T2 S" +" >cb H[A] S" CELL+ !" >cb CELL +TO =H ; PRIVATE
>
> : rndi[ Wi@ T1x S" +" >cb ; PRIVATE
>
> : rndn[ Wi T1x S" +" >cb ; PRIVATE
>
> : ]rndi S" +" >cb subrnd S" CELL+" >cb ; PRIVATE
>
> : ]rndn S" +" >cb subrnd ; PRIVATE
>
> : ~rndi S" DROP" >cb ; PRIVATE
>
> : ~rndn S" SHAsh SHAval" >cb H[H] S" UpDateHash" >cb ; PRIVATE
>
>
>
> InitMake
>
> rndi[ K0 ]rndi rndi[ K1 ]rndi rndi[ K2 ]rndi rndi[ K3 ]rndi
>
> rndi[ K4 ]rndi rndi[ K5 ]rndi rndi[ K6 ]rndi rndi[ K7 ]rndi
>
> rndi[ K8 ]rndi rndi[ K9 ]rndi rndi[ K10 ]rndi rndi[ K11 ]rndi
>
> rndi[ K12 ]rndi rndi[ K13 ]rndi rndi[ K14 ]rndi rndi[ K15 ]rndi ~rndi
>
> rndn[ K16 ]rndn rndn[ K17 ]rndn rndn[ K18 ]rndn rndn[ K19 ]rndn
>
> rndn[ K20 ]rndn rndn[ K21 ]rndn rndn[ K22 ]rndn rndn[ K23 ]rndn
>
> rndn[ K24 ]rndn rndn[ K25 ]rndn rndn[ K26 ]rndn rndn[ K27 ]rndn
>
> rndn[ K28 ]rndn rndn[ K29 ]rndn rndn[ K30 ]rndn rndn[ K31 ]rndn
>
> rndn[ K32 ]rndn rndn[ K33 ]rndn rndn[ K34 ]rndn rndn[ K35 ]rndn
>
> rndn[ K36 ]rndn rndn[ K37 ]rndn rndn[ K38 ]rndn rndn[ K39 ]rndn
>
> rndn[ K40 ]rndn rndn[ K41 ]rndn rndn[ K42 ]rndn rndn[ K43 ]rndn
>
> rndn[ K44 ]rndn rndn[ K45 ]rndn rndn[ K46 ]rndn rndn[ K47 ]rndn
>
> rndn[ K48 ]rndn rndn[ K49 ]rndn rndn[ K50 ]rndn rndn[ K51 ]rndn
>
> rndn[ K52 ]rndn rndn[ K53 ]rndn rndn[ K54 ]rndn rndn[ K55 ]rndn
>
> rndn[ K56 ]rndn rndn[ K57 ]rndn rndn[ K58 ]rndn rndn[ K59 ]rndn
>
> rndn[ K60 ]rndn rndn[ K61 ]rndn rndn[ K62 ]rndn rndn[ K63 ]rndn
>
> rndn[ K64 ]rndn rndn[ K65 ]rndn rndn[ K66 ]rndn rndn[ K67 ]rndn
>
> rndn[ K68 ]rndn rndn[ K69 ]rndn rndn[ K70 ]rndn rndn[ K71 ]rndn
>
> rndn[ K72 ]rndn rndn[ K73 ]rndn rndn[ K74 ]rndn rndn[ K75 ]rndn
>
> rndn[ K76 ]rndn rndn[ K77 ]rndn rndn[ K78 ]rndn rndn[ K79 ]rndn ~rndn
>
> EndMake
>
>
>
> : storelen D2* D2* D2* ( bytes->bits) W #112 CHARS + ! W #120 CHARS + ! ; PRIVATE ( lo hi -- )
>
> : setlen SHAlen storelen ; PRIVATE ( -- )
>
> : cellsreverse 0 ?DO DUP @ BSWAP OVER ! CELL+ LOOP DROP ; PRIVATE ( addr n -- )
>
> : endian16 DUP #16 cellsreverse ; PRIVATE ( addr1 -- addr2 )
>
> : endian14 DUP #14 cellsreverse ; PRIVATE ( addr1 -- addr2 )
>
>
>
> -- Do all 128 byte blocks leaving remainder block
>
> : hashfullblocks ( addr1 dcount -- addr2 dcount ) \ dcount is double number: lo hi
>
> SWAP DUP >R 7 RSHIFT ( addr1 hi lo* ) \ Store lo on return, lo*=lo/128
>
> OVER CELLSIZE 7 - LSHIFT OR >R ( addr1 hi ) \ Return is now: :R lo lo'
>
> ( hi) 7 RSHIFT
>
> 0 ?DO ( addr1 ) \ Do if hi'= hi/128 > 0
>
> 0 0 DO DUP endian16 SHA512 #128 +
>
> LOOP ( addr' ) \ Hash for 2^cellsize full blocks
>
> LOOP ( addr' ) \ Hash for hi'*2^cellsize full blocks
>
> R> 0 ?DO DUP endian16 SHA512 #128 + LOOP ( addr' ) \ Hash for lo' count full 128 byte blocks
>
> R> ( lo) #127 AND ; PRIVATE ( addr2 cnt ) \ Leave address and count for partial block
>
>
>
> : hashfinal ( addr count -- ) \ Hash partial and/or last block
>
> DUP >R W SWAP CMOVE \ Move bytes into block W array
>
> W R@ + #128 OVER C! ( addr ) \ Put 80h after last message byte
>
> CHAR+ #111 R@ - ( addr # ) \ Compute tentative 0 byte FILL count
>
> R> #111 > ( addr # ? ) \ Is partial block byte count > 111 ?
>
> IF #16 + ERASE ( -- ) \ If yes, FILL rest of block w/zeroes
>
> W endian16 SHA512 ( -- ) \ Endian adjust block if required, then hash
>
> W #112 ( addr 112 ) \ Now setup last block containing bit count
>
> ENDIF ( addr # )
>
> ERASE setlen W endian14 SHA512 ; \ Zero FILL last block, set message bit count
>
> PRIVATE \ Endian adjust, except bit count, then hash
>
>
>
> -- Compute SHA512 from a counted buffer of text
>
> : SHAbuffer ( addr dcount -- ) SHAinit 2DUP TO SHAlen hashfullblocks hashfinal ;
>
>
>
> -- =============== Hash string display wordset ===============
>
> -- Array of digits 0123456789abcdef
>
> : digit$ ( -- addr ) S" 0123456789abcdef" DROP ; PRIVATE
>
> : intdigits ( -- ) PAD 0! ; PRIVATE
>
> : savedigit ( n -- ) PAD C@ 1+ DUP PAD C! PAD + C! ; PRIVATE
>
> : bytedigits ( n -- ) DUP 4 RSHIFT digit$ + C@ savedigit #15 AND digit$ + C@ savedigit ; PRIVATE
>
> : celldigits ( addr -- ) DUP 7 + DO I C@ bytedigits -1 +LOOP BL savedigit ; PRIVATE
>
> : SHAstring ( -- addr u ) intdigits SHAval 7 CELLS + 8 0 DO DUP celldigits CELL- LOOP DROP PAD COUNT ;
>
> : HASH. ( -- ) CR SHAstring TYPE SPACE ; -- Display SHA-512 hash value in hex
>
> : QuoteString ( addr cnt -- ) CR CR &" EMIT TYPE &" EMIT ; PRIVATE
>
>
>
> -- ==================== File hash wordset ====================
>
> 0 VALUE rfileid PRIVATE -- Holds fileid of input file
>
> : bytes@ ( addr n -- ) rfileid READ-FILE 2DROP ; PRIVATE
>
> : block@ ( -- ) W #128 bytes@ ; PRIVATE
>
>
>
> : getpartial ( cnt -- W' cnt2 ? )
>
> W 2DUP SWAP DUP >R bytes@ ( cnt1 addr1 )
>
> + #128 OVER C! CHAR+ #111 R@ - R> #111 > ; PRIVATE ( addr2 cnt2 ? )
>
>
>
> : @SHAfile ( c-addr u -- )
>
> R/O BIN OPEN-FILE SWAP TO rfileid ( ior) ?FILE
>
> SHAinit
>
> rfileid FILE-SIZE DROP ( ud ) \ Get bytesize of input file
>
> 0. D- \ Adjust to hash subset of file
>
> CR ." Bytesize: " 2DUP D. \ Display hash size to screen
>
> 2DUP 2>R \ ( lo hi ) Save file byte cnt on RETURN
>
> OVER 7 RSHIFT OVER \ ( lo hi lo* hi )
>
> CELLSIZE 7 - LSHIFT OR SWAP 7 RSHIFT \ ( lo lo' hi') lo' hi' now full block count
>
> 0 ?DO 0 0 DO block@ W endian16 SHA512 LOOP LOOP \ Hash hi*2^cellsize full blocks
>
> 0 ?DO block@ W endian16 SHA512 LOOP \ Hash lo count full 128 byte blocks
>
> ( lo) #127 AND ( rembytes) getpartial ( addr cnt ? ) \ Read remaining bytes
>
> IF #16 + ERASE W endian16 SHA512 W #112 ENDIF \ Do if rembytes > 111
>
> ERASE 2R> storelen W endian14 SHA512 \ Do last block
>
> CR ." SHA-512 : " CR SHAstring TYPE CR \ Show SHA-512 hash for file
>
> rfileid CLOSE-FILE ?FILE ; \ Close the input file
>
>
>
> NESTING @ 1 =
>
> [IF]
>
>
>
> -- ==================== SHA-512 Test Suite ================================================================================================================
>
> -- Load W array with data on stack
>
> : WLoad W #15 CELLS + ( d0..d15 W[15] ) #16 0 DO TUCK ! CELL- LOOP DROP ; PRIVATE ( d0..d15 -- )
>
>
>
> -- ----------------------------------------------------------------------------------------------------------------------------------------------------------
>
> -- EXAMPLE 0: from Wikipedia
>
> : EX0 S" " 2DUP QuoteString
>
> U>D ( addr dcount) SHAbuffer HASH.
>
> CR S" cf83e1357eefb8bd f1542850d66d8007 d620e4050b5715dc 83f4a921d36ce9ce 47d0d13c5d85f2b0 ff8318d2877eec2f 63b931bd47417a81 a538327af927da3e" TYPE ;
>
> -- ----------------------------------------------------------------------------------------------------------------------------------------------------------
>
> -- EXAMPLE 1: from FIPS PUB
>
> : EX1 S" abc" 2DUP QuoteString
>
> U>D ( addr dcount) SHAbuffer HASH.
>
> CR S" ddaf35a193617aba cc417349ae204131 12e6fa4e89a97ea2 0a9eeee64b55d39a 2192992a274fc1a8 36ba3c23a3feebbd 454d4423643ce80e 2a9ac94fa54ca49f" TYPE ;
>
> -- ----------------------------------------------------------------------------------------------------------------------------------------------------------
>
> -- EXAMPLE 2: from FIPS PUB
>
> : EX2a S" abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" ;
>
> : EX2 EX2a 2DUP QuoteString
>
> U>D ( addr dcount) SHAbuffer HASH.
>
> CR S" 8e959b75dae313da 8cf4f72814fc143f 8f7779c6eb9f7fa1 7299aeadb6889018 501d289e4900f7e4 331b99dec4b5433a c7d329eeb6dd2654 5e96e55b874be909" TYPE ;
>
>
>
> : EX5 S" The quick brown fox jumps over the lazy dog" 2DUP QuoteString
>
> U>D ( addr dcount) SHAbuffer HASH.
>
> CR S" 07e547d9586f6a73 f73fbac0435ed769 51218fb7d0c8d788 a309d785436bbb64 2e93a252a954f239 12547d1e8a3b5ed6 e1bfd7097821233f a0538f3db854fee6" TYPE ;
>
> -- ----------------------------------------------------------------------------------------------------------------------------------------------------------
>
> -- EXAMPLE 3:
>
> -- Message: 1 million copies of 'a' (61h), (8 million bits)
>
> -- Hash = ?
>
> -- Load block of all 'a's (61h), must hash 15,625 times
>
> : EX3a W #128 'a' FILL ;
>
> -- Last message block: 1st bit a '1', bit-count = 16 million
>
> : EX3b $8000000000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 #16000000 WLoad ;
>
> -- Do hash for message of 2 million copies of ASCII 'a' (61h)
>
> : EX3 SHAinit EX3a #15625 0 DO W SHA512 LOOP EX3b W SHA512 HASH. ;
>
> -- -------------------------------------------------------------
>
> -- EXAMPLE 4:
>
> -- Message: 400,000 SPACES 'BL' (20h), (3,200,000 bits)
>
> -- Hash = ?
>
> -- Load block of all "BL' (20h), hash 156 full blocks + 16 bytes
>
> : EX4a W #128 BLANK ;
>
> -- Last message block: 1st bit a '1', bit-count = 3,200,000
>
> : EX4b $8000000000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 #3200000 WLoad ;
>
> -- Do hash for message of 400,000 SPACES 'BL' (20h)
>
> : EX4 SHAinit EX4a #3125 0 DO W SHA512 LOOP EX4b W SHA512 HASH. ;
>
>
>
> : SHATest ( -- )
>
> CR ." SHA-512 test suite:"
>
> EX0 EX1 EX2 EX5
>
> CR CR S" 2 million copies of ASCII 'a' (61h)" TYPE EX3
>
> CR CR S" 400,000 copies of ASCII BL (20h)" TYPE EX4 CR ;
>
>
>
> : [EX1] S" abc" U>D ( addr dcount) SHAbuffer ; PRIVATE
>
> : [EX2] EX2a U>D SHAbuffer ; PRIVATE
>
> : [EX3] SHAinit EX3a #15625 0 DO W SHA512 LOOP EX3b W SHA512 ; PRIVATE
>
> : test1 CR ." SHA-512 test for EX1, 1000 loops: " TIMER-RESET #1000 0 DO [EX1] LOOP .ELAPSED ;
>
> : test2 CR ." SHA-512 test for EX2, 1000 loops: " TIMER-RESET #1000 0 DO [EX2] LOOP .ELAPSED ;
>
> : test3 CR ." SHA-512 test for EX3, 1000 loops: " TIMER-RESET #1000 0 DO [EX3] LOOP .ELAPSED ;
>
> : SHAbm ( -- ) test1 test2 test3 ;
>
>
>
> :ABOUT CR ." Try: SHAtest -- test SHA-512 (roughly)"
>
> CR ." SHAbm -- speed benchmark"
>
> CR ." ( c-addr ud -- ) SHAbuffer HASH. -- print hash value of string"
>
> CR ." ( c-addr u -- ) @SHAfile -- print hash value of file." ;
>
>
>
> [ELSE]
>
>
>
> :ABOUT CR ." Try: ( c-addr ud -- ) SHAbuffer HASH. -- print hash value of string"
>
> CR ." ( c-addr u -- ) @SHAfile -- print hash value of file." ;
>
>
>
> [THEN]
>
>
>
> NESTING @ 1 = [IF] .ABOUT -sha-512 CR [THEN]
>
> DEPRIVE
>
>
>
> (* End of Source *)
You may get a 3-5% speed increase using these better versions of 'UpDateHash. Use the one the works the best. With VFX the first version takes 85 bytes and the 2nd 77 bytes, but the first one is faster.
If you want even more speed unroll the loop and do it explicitly.
------------------------------ New UpDateHash -----------------------------
: UpDateHash ( -) \ Update hash values: SHAsh(i) = SHAval(i) = SHAval(i-1) + H[H](i-1)
H[H] 8 0 DO DUP @ I CELLS DUP >R SHAval + TUCK +! \ Compute updated hash subvalue
@ SHAsh R> + ! CELL+ LOOP DROP \ Store updated hash subvalue
SHAsh TO H[H] \ Point H[H] to updated SHAsh(0)=h
;
: UpDateHash ( -) \ Update hash values: SHAsh(i) = SHAval(i) = SHAval(i-1) + H[H](i-1)
8 0 DO H[H] I CELLS DUP >R + @ SHAval R@ + TUCK +! \ Compute updated hash subvalue
@ SHAsh R> + ! LOOP \ Store updated hash subvalue
SHAsh TO H[H] \ Point H[H] to updated SHAsh(0)=h
;
----------------------------------------------------------------------------
jz
[toc] | [prev] | [next] | [standalone]
| From | jzakiya@gmail.com |
|---|---|
| Date | 2012-12-16 22:35 -0800 |
| Message-ID | <c9c0448b-2eb3-4d2a-b5d4-b4aa8e02da4d@googlegroups.com> |
| In reply to | #18040 |
Found a website which lists SHA512s of iso you can check against. http://ftp.netbsd.org/pub/NetBSD/iso/5.1.2/SHA512 jz
[toc] | [prev] | [next] | [standalone]
| From | mhx@iae.nl (Marcel Hendrix) |
|---|---|
| Date | 2012-12-17 10:14 +0200 |
| Message-ID | <90899318918435@frunobulax.edu> |
| In reply to | #18042 |
jzakiya@gmail.com writes Re: SHA-512 > Found a website which lists SHA512s of iso you can check against. > http://ftp.netbsd.org/pub/NetBSD/iso/5.1.2/SHA512 SHA512 (sourcecd-5.1.2.iso) = c809baa170082aadea5255c4239f61d2ebc5042ad9bb67eb8bb134d7ab7327d0 a6218b31bff56a3709bae6b99fcd73db157eca7b83f6695c7801b570b216a82d FORTH> S" C:\Users\marcel\Desktop\sourcecd-5.1.2.iso" @SHAfile Bytesize: 324468736 SHA-512 : c809baa170082aad ea5255c4239f61d2 ebc5042ad9bb67eb 8bb134d7ab7327d0 a6218b31bff56a37 09bae6b99fcd73db 157eca7b83f6695c 7801b570b216a82d So, it apperas to work correctly for up to 300MB. I think (looking at the code) that files up to 18,446,744,073,709,551,616 bytes will be OK. An actual test with larger values will have to wait until the release of Windows 9, -marcel
[toc] | [prev] | [next] | [standalone]
| From | mhx@iae.nl (Marcel Hendrix) |
|---|---|
| Date | 2012-12-17 10:51 +0200 |
| Message-ID | <91849318918435@frunobulax.edu> |
| In reply to | #18040 |
jzakiya@gmail.com writes Re: SHA-512
> On Sunday, December 16, 2012 9:00:07 AM UTC-5, Marcel Hendrix wrote:
> mhx@iae.nl (Marcel Hendrix) writes Re: SHA-512
>>
>> > jzakiya@gmail.com writes Re: SHA-512
>> [..]
>> > I have looked at my own (PICK-less) idea of SHA-512 again, and it couldn't work.
>> > However, I found a way to improve your algorithm (literalize H[x]). ATM test3
>> > runs in 11.048 seconds on my 2.66 GHz i7 system. SHAfile needs 2.741 seconds
>> > to process 385,241,618 bytes (64.9.. 50 MB/s/GHz). Hopefully your fix of 15 PICK
>> > will create additional room for speedup.
>> [..]
> You may get a 3-5% speed increase using these better versions of 'UpDateHash.
> Use the one the works the best. With VFX the first version takes 85 bytes and the
> 2nd 77 bytes, but the first one is faster.
> If you want even more speed unroll the loop and do it explicitly.
The speed variation between runs is much larger than the improvement.
If there is any, it is probably around 1%. The code is indeed much shorter.
-marcel
-- ------------------------------------------------------------------
: UpDateHash ( a b c -- ) \ Update hash values and load arrays with new values
8 0 DO DUP >R @ SWAP DUP >R @ + DUP \ Compute updated hash subvalue
R@ ! OVER ! CELL+ R> CELL+ R> CELL+ \ Store updated hash subvalue
LOOP 3DROP ; \ Clear stack when done
FORTH> ' updatehash idis
$0140BA00 : [trashed]
$0140BA0A mov rcx, 8 d#
$0140BA11 xor rbx, rbx
$0140BA14 call (DO) offset NEAR
$0140BA1E nop
$0140BA1F nop
$0140BA20 pop rdi
$0140BA21 mov rax, [rbx] qword
$0140BA24 add rax, [rdi] qword
$0140BA27 mov [rdi] qword, rax
$0140BA2A pop rdx
$0140BA2B mov [rdx] qword, rax
$0140BA2E lea rax, [rdx 8 +] qword
$0140BA32 push rax
$0140BA33 lea rdi, [rdi 8 +] qword
$0140BA37 push rdi
$0140BA38 lea rbx, [rbx 8 +] qword
$0140BA3C add [rbp 0 +] qword, 1 b#
$0140BA41 add [rbp 8 +] qword, 1 b#
$0140BA46 jno $0140BA20 offset NEAR
$0140BA4C add rbp, #24 b#
$0140BA50 pop rdi
$0140BA51 pop rax
$0140BA52 ;
: UpDateHash ( h[h] -) \ Update hash values: SHAsh(i) = SHAval(i) = SHAval(i-1) + H[H](i-1)
8 0 DO DUP @ I CELLS DUP >R SHAval + TUCK +! \ Compute updated hash subvalue
@ SHAsh R> + ! CELL+ LOOP DROP \ Store updated hash subvalue
;
FORTH> ' updatehash idis
$0140BA00 : [trashed]
$0140BA0A mov rcx, 8 d#
$0140BA11 xor rbx, rbx
$0140BA14 call (DO) offset NEAR
$0140BA1E nop
$0140BA1F nop
$0140BA20 mov rdi, [rbp 0 +] qword
$0140BA24 mov rax, [rbx] qword
$0140BA27 add [rdi*8 $01401720 +] qword, rax
$0140BA2F mov rax, [rdi*8 $01401720 +] qword
$0140BA37 mov [rdi*8 $01401770 +] qword, rax
$0140BA3F lea rbx, [rbx 8 +] qword
$0140BA43 add [rbp 0 +] qword, 1 b#
$0140BA48 add [rbp 8 +] qword, 1 b#
$0140BA4D jno $0140BA20 offset NEAR
$0140BA53 add rbp, #24 b#
$0140BA57 ;
[toc] | [prev] | [next] | [standalone]
| From | jzakiya@gmail.com |
|---|---|
| Date | 2012-12-17 09:13 -0800 |
| Message-ID | <bc497962-8b67-4b1a-a8e3-ef1311baf16b@googlegroups.com> |
| In reply to | #18044 |
On Monday, December 17, 2012 3:51:12 AM UTC-5, Marcel Hendrix wrote:
> jzakiya@gmail.com writes Re: SHA-512
>
>
>
> > On Sunday, December 16, 2012 9:00:07 AM UTC-5, Marcel Hendrix wrote:
>
> > mhx@iae.nl (Marcel Hendrix) writes Re: SHA-512
>
> >>
>
> >> > jzakiya@gmail.com writes Re: SHA-512
>
> >> [..]
>
> >> > I have looked at my own (PICK-less) idea of SHA-512 again, and it couldn't work.
>
> >> > However, I found a way to improve your algorithm (literalize H[x]). ATM test3
>
> >> > runs in 11.048 seconds on my 2.66 GHz i7 system. SHAfile needs 2.741 seconds
>
> >> > to process 385,241,618 bytes (64.9.. 50 MB/s/GHz). Hopefully your fix of 15 PICK
>
> >> > will create additional room for speedup.
>
> >> [..]
>
>
>
> > You may get a 3-5% speed increase using these better versions of 'UpDateHash.
>
> > Use the one the works the best. With VFX the first version takes 85 bytes and the
>
> > 2nd 77 bytes, but the first one is faster.
>
>
>
> > If you want even more speed unroll the loop and do it explicitly.
>
>
>
> The speed variation between runs is much larger than the improvement.
>
> If there is any, it is probably around 1%. The code is indeed much shorter.
>
>
>
> -marcel
>
>
>
> -- ------------------------------------------------------------------
>
> : UpDateHash ( a b c -- ) \ Update hash values and load arrays with new values
>
> 8 0 DO DUP >R @ SWAP DUP >R @ + DUP \ Compute updated hash subvalue
>
> R@ ! OVER ! CELL+ R> CELL+ R> CELL+ \ Store updated hash subvalue
>
> LOOP 3DROP ; \ Clear stack when done
>
>
>
> FORTH> ' updatehash idis
>
> $0140BA00 : [trashed]
>
> $0140BA0A mov rcx, 8 d#
>
> $0140BA11 xor rbx, rbx
>
> $0140BA14 call (DO) offset NEAR
>
> $0140BA1E nop
>
> $0140BA1F nop
>
> $0140BA20 pop rdi
>
> $0140BA21 mov rax, [rbx] qword
>
> $0140BA24 add rax, [rdi] qword
>
> $0140BA27 mov [rdi] qword, rax
>
> $0140BA2A pop rdx
>
> $0140BA2B mov [rdx] qword, rax
>
> $0140BA2E lea rax, [rdx 8 +] qword
>
> $0140BA32 push rax
>
> $0140BA33 lea rdi, [rdi 8 +] qword
>
> $0140BA37 push rdi
>
> $0140BA38 lea rbx, [rbx 8 +] qword
>
> $0140BA3C add [rbp 0 +] qword, 1 b#
>
> $0140BA41 add [rbp 8 +] qword, 1 b#
>
> $0140BA46 jno $0140BA20 offset NEAR
>
> $0140BA4C add rbp, #24 b#
>
> $0140BA50 pop rdi
>
> $0140BA51 pop rax
>
> $0140BA52 ;
>
>
>
> : UpDateHash ( h[h] -) \ Update hash values: SHAsh(i) = SHAval(i) = SHAval(i-1) + H[H](i-1)
>
> 8 0 DO DUP @ I CELLS DUP >R SHAval + TUCK +! \ Compute updated hash subvalue
>
> @ SHAsh R> + ! CELL+ LOOP DROP \ Store updated hash subvalue
>
> ;
>
>
>
> FORTH> ' updatehash idis
>
> $0140BA00 : [trashed]
>
> $0140BA0A mov rcx, 8 d#
>
> $0140BA11 xor rbx, rbx
>
> $0140BA14 call (DO) offset NEAR
>
> $0140BA1E nop
>
> $0140BA1F nop
>
> $0140BA20 mov rdi, [rbp 0 +] qword
>
> $0140BA24 mov rax, [rbx] qword
>
> $0140BA27 add [rdi*8 $01401720 +] qword, rax
>
> $0140BA2F mov rax, [rdi*8 $01401720 +] qword
>
> $0140BA37 mov [rdi*8 $01401770 +] qword, rax
>
> $0140BA3F lea rbx, [rbx 8 +] qword
>
> $0140BA43 add [rbp 0 +] qword, 1 b#
>
> $0140BA48 add [rbp 8 +] qword, 1 b#
>
> $0140BA4D jno $0140BA20 offset NEAR
>
> $0140BA53 add rbp, #24 b#
>
> $0140BA57 ;
Unrolling UpDateHash on VFX reduced time from 16.405 secs to 16.030.
: UpDateHash ( -) \ Update hash values: SHAsh(i) = SHAval(i) = SHAval(i-1) + H[H](i-1)
H[H] DUP @ [ SHAval 0 CELLS+ ]L TUCK +! @ [ SHAsh 0 CELLS+ ]L ! CELL+
DUP @ [ SHAval 1 CELLS+ ]L TUCK +! @ [ SHAsh 1 CELLS+ ]L ! CELL+
DUP @ [ SHAval 2 CELLS+ ]L TUCK +! @ [ SHAsh 2 CELLS+ ]L ! CELL+
DUP @ [ SHAval 3 CELLS+ ]L TUCK +! @ [ SHAsh 3 CELLS+ ]L ! CELL+
DUP @ [ SHAval 4 CELLS+ ]L TUCK +! @ [ SHAsh 4 CELLS+ ]L ! CELL+
DUP @ [ SHAval 5 CELLS+ ]L TUCK +! @ [ SHAsh 5 CELLS+ ]L ! CELL+
DUP @ [ SHAval 6 CELLS+ ]L TUCK +! @ [ SHAsh 6 CELLS+ ]L ! CELL+
@ [ SHAval 7 CELLS+ ]L TUCK +! @ [ SHAsh 7 CELLS+ ]L !
SHAsh TO H[H]
;
jz
[toc] | [prev] | [next] | [standalone]
| From | mhx@iae.nl (Marcel Hendrix) |
|---|---|
| Date | 2012-12-17 22:24 +0200 |
| Message-ID | <65111318918435@frunobulax.edu> |
| In reply to | #18047 |
jzakiya@gmail.com writes Re: SHA-512 [..] > Unrolling UpDateHash on VFX reduced time from 16.405 secs to 16.030. [..] I can't measure the improvement that gives, but I added it anyway. Today I backported SHA-512 to SHA-256. It took only a few minutes. Unfortunately, SHA-256 runs only on iForth32 as ROR for iForth64 rotates 64 bits, which is not suitable. Fixing this to let SHA-256 run on a 64bit Forth is rather inefficient. I see that SHA256's test3 only processes 1 million characters. The test3 for SHA512 is doing 2 million characters. Take care when comparing times for SHA-256 with SHA-512! SHA256, iForth32, i7 920, 2.77 GHz, test3: 8.621 seconds or 41.58 MB/s/GHz. It seems VFX is doing rather poorly on SHA256 if it needs 16.030 seconds? -marcel
[toc] | [prev] | [next] | [standalone]
| From | jzakiya@gmail.com |
|---|---|
| Date | 2013-01-15 20:38 -0800 |
| Message-ID | <c0fd0525-52e8-4a2f-b0ec-918fcf65cd6f@googlegroups.com> |
| In reply to | #18053 |
On Monday, December 17, 2012 3:24:38 PM UTC-5, Marcel Hendrix wrote: > jzakiya@gmail.com writes Re: SHA-512 > > [..] > > > Unrolling UpDateHash on VFX reduced time from 16.405 secs to 16.030. > > [..] > > > > I can't measure the improvement that gives, but I added it anyway. > > > > Today I backported SHA-512 to SHA-256. It took only a few minutes. > > > > Unfortunately, SHA-256 runs only on iForth32 as ROR for iForth64 > > rotates 64 bits, which is not suitable. Fixing this to let SHA-256 > > run on a 64bit Forth is rather inefficient. > > > > I see that SHA256's test3 only processes 1 million characters. > > The test3 for SHA512 is doing 2 million characters. Take care > > when comparing times for SHA-256 with SHA-512! > > > > SHA256, iForth32, i7 920, 2.77 GHz, test3: 8.621 seconds or 41.58 MB/s/GHz. > > > > It seems VFX is doing rather poorly on SHA256 if it needs 16.030 seconds? > > > > -marcel Hi Marcel, OK, I went on a coding rampage. I implemented all four 1024-bit block Secure Hash Algorithms SHA-384, SHA-512, SHA-512/256, and SHA-512/224 in one file, and you just select the one you want to use when you load it. Through **extensive** test with VFX with SHA-256 and SHA-224 the phrasings of the hashing words in the code optimize performance in VFX (32-bit), but under IForth different phrasing may produce better performance. The versions I posted implemented the rounds within loops (versus unrolling the round loops because it gave either better (VFX) or no worse performance than the unrolled loop versions. Your mileage may vary. The file, SHA-384||512xxx.F is in my 4shared.com repository here: www.4shared.com/dir/TcMrUvTB/sharing.htm Or at http://gist.gitgub.com/jzakiya or directly at https://gist.github.com/4544478
[toc] | [prev] | [next] | [standalone]
| From | jzakiya@gmail.com |
|---|---|
| Date | 2013-01-15 20:54 -0800 |
| Message-ID | <bc280052-b791-4b44-999f-c712616b30d4@googlegroups.com> |
| In reply to | #18053 |
On Monday, December 17, 2012 3:24:38 PM UTC-5, Marcel Hendrix wrote: > jzakiya@gmail.com writes Re: SHA-512 > > [..] > > > Unrolling UpDateHash on VFX reduced time from 16.405 secs to 16.030. > > [..] > > > > I can't measure the improvement that gives, but I added it anyway. > > > > Today I backported SHA-512 to SHA-256. It took only a few minutes. > > > > Unfortunately, SHA-256 runs only on iForth32 as ROR for iForth64 > > rotates 64 bits, which is not suitable. Fixing this to let SHA-256 > > run on a 64bit Forth is rather inefficient. > > > > I see that SHA256's test3 only processes 1 million characters. > > The test3 for SHA512 is doing 2 million characters. Take care > > when comparing times for SHA-256 with SHA-512! > > > > SHA256, iForth32, i7 920, 2.77 GHz, test3: 8.621 seconds or 41.58 MB/s/GHz. > > > > It seems VFX is doing rather poorly on SHA256 if it needs 16.030 seconds? > > > > -marcel Hi Marcel, I went on a coding rampage since my last post on this. I implemented all four 1024-bit block Secure Hash Algorithms SHA-384, SHA-512, SHA-512/256, and SHA-512/224 in one file, that you select the one you want to use when you load it. Through **extensive** test with VFX using SHA-256 and SHA-224, the phrasings of the hashing words optimize performance in VFX (32-bit), but under IForth different phrasing may produce better performance. The versions I posted perform the rounds within loops (versus unrolling the round loops) because it gave either better (VFX) or no worse performance than the unrolled loop versions. Your mileage may vary. The file SHA-384||512xxx.F is in my 4shared.com repository here: https://www.4shared.com/folder/4o-httdZ/Forth.html or thru here: https://gist.github.com/jzakiya or directly here: https://gist.github.com/4544478 Jabari
[toc] | [prev] | [next] | [standalone]
| From | Howerd <howerdo@yahoo.co.uk> |
|---|---|
| Date | 2012-12-13 13:06 -0800 |
| Message-ID | <740e9412-edbb-4452-a157-ff91a094b72b@googlegroups.com> |
| In reply to | #17996 |
On Thursday, December 13, 2012 3:30:07 PM UTC+1, jza...@gmail.com wrote: > On Monday, December 10, 2012 4:17:24 PM UTC-5, Marcel Hendrix wrote: > > > jzaxxx@gmail.com writes Re: SHA-512 > > > > > > > > > > > > > On Monday, December 10, 2012 6:53:44 AM UTC-5, m.a.m....@tue.nl wrote: > > > > > > >> On Monday, December 10, 2012 2:29:04 AM UTC+1, jza...@gmail.com wrote: > > > > > > >> > > > > > > >> [..] > > > > > > >> > > > > > > >> [ Egg on my face, should not have touched the DECIMAL/HEX issues yet. ] > > > > > > > > > > > > > >> Yes, the code now runs and gives the correct results for S" abc". > > > > > > >> > > > > > > >> This would be the first working SHA-512 in 64-bit Forth! > > > > > > > > > > > > > Hey GREAT! :-) > > > > > > > > > > > > > Be sure to do the longer example also to make sure there are no > > > > > > > other problems (which I don't forsee). > > > > > > > > > > > > Unfortunately, the longer string does not hash properly. To avoid > > > > > > more egg on my face, I downloaded your latest code from 4share. > > > > > > To make it work in iForth64 I needed the patch that I have appended > > > > > > (Enable PLACE and rewrite timer-start and ms?, typos in SHA256 and > > > > > > SHAinit, extra stack item in EX2a dropped.) > > > > > > > > > > > > [..] > > > > > > > > > > > > > I ALWAYS start from the algorithm description to do Forth versions, even if > > > > > > > I look at other existing language implementations, because I think so much > > > > > > > differently when I create in Forth. > > > > > > > > > > > > Ok. Please forget about macro's, ]L and EVALUATE for the time being. Also, > > > > > > the 15 PICKs you may get rid off without any ill effects :-) > > > > > > > > > > > > Here are the current results with your patched code: > > > > > > > > > > > > FORTH> in > > > > > > Redefining split-at-char > > > > > > Redefining MACRO > > > > > > Redefining MS? ok > > > > > > FORTH> SHAtest > > > > > > SHA-512 test suite: > > > > > > cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e "" > > > > > > ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f "abc" > > > > > > 8cc1fec83f21176a675306d9f80b26597469eff277ab9d0f03a725b15d57d33a2e3aadea4727cf632a3e5ec3c744bcc0eb2183c41c717cc4a84fa6dbf8244243 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" > > > > > > 9bc68759247e3332bec1c79d128d28a8931d0c9f96c8aa975731b563475fdddddf7f873c25086908effe270e23c5a01e5dfb3289bf5d091d8fb454b1bcf98dda 2 million copies of ASCII 'a' (61h) > > > > > > 19ef4876e03c93476e00b486c62108d4d7136f6d4782817b449bacf44d0963e03f5ab474cb6822c2963a979e8e15298cd60f22841c51f6161620c0e031c65f4a 400,000 copies of ASCII BL (20h) > > > > > > ok > > > > > > > > > > > > -marcel > > > > > > > > > > > > -- ---------- > > > > > > Compare: (<)C:\Users\marcel\Desktop\SHA-512.frt > > > > > > with: (>)C:\Users\marcel\Desktop\SHA-512VFX.F > > > > > > > > > > > > 1d1 > > > > > > < ANEW -sha-512 > > > > > > 19c18,19 > > > > > > < : PLACE ( caddr n addr -) 2DUP C! CHAR+ SWAP CHARS MOVE ; > > > > > > --- > > > > > > > \ Not needed for SwiftForth v 2.00.3, needed for Win32Forth V 4.10 > > > > > > > \ : PLACE ( caddr n addr -) 2DUP C! CHAR+ SWAP CHARS MOVE ; > > > > > > 161c161 > > > > > > < : SHA512 ( Wadr - ) > > > > > > --- > > > > > > > : SHA256 ( Wadr - ) > > > > > > 233c233 > > > > > > < SHAinit 2DUP SHAlen 2! hashfullblocks hashfinal > > > > > > --- > > > > > > > SHAinit 2DUP SHAlen 2 ! hashfullblocks hashfinal > > > > > > 375,376c375,376 > > > > > > < EX1 S" abc" QuoteString > > > > > > < EX2 EX2a DROP QuoteString > > > > > > --- > > > > > > > EX1 S" abc" QuoteString > > > > > > > EX2 EX2a QuoteString > > > > > > 381,384c381,391 > > > > > > < VARIABLE start-ms > > > > > > < > > > > > > < : TIMER-START ( -- ) ?MS start-ms ! ; > > > > > > < : MS? ( -- u ) ?MS start-ms @ - ( abs ) ; > > > > > > --- > > > > > > > \ =========== VFX Forth specific performance test =========== > > > > > > > > > > > > > > [undefined] GetTickCount > > > > > > > [IF] extern: DWORD PASCAL GetTickCount( void ) [THEN] > > > > > > > > > > > > > > VARIABLE start-ms > > > > > > > > > > > > > > : TIMER-START ( -- ) GetTickCount start-ms ! ; > > > > > > > > > > > > > > : MS? ( -- u ) GetTickCount start-ms @ - ( abs ) ; > > > > > > > > > > > Marcel, could you run EX2 by itself, and see what the output is. > > > > I think it may have to do with my making the count from the string word EX2a return a double number count instead of a single, so I changed it as below to make it consistent with 'abc' > > > > : EX2a S" abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" ; > > : EX2 EX2a U>D ( adr ud) SHAbuffer HASH. > > > > Also, do you know the words the current SwiftForth uses for timing. > > I'm running SwiftForth i386-Win32 3.4.5 03-Oct-2012 under WINE on Linux and the old cold that uses ucounter and utimer bombs on this version. > > > > You may be more pleased with the newer code I'll release on all the SHA-xxx algorithms, which will make it a little more upto date from the code I did circa 2001, after all the issues with SHA512 are fixed. :-) Hi Jabari, Thanks for doing all of this work on the SHA series, and sorry that I thought that you had copied the C versions - it was a false assumption... Please let us all know when you have both versions polished :-) Best regards, Howerd
[toc] | [prev] | [next] | [standalone]
| From | Howerd <howerdo@yahoo.co.uk> |
|---|---|
| Date | 2012-12-13 13:03 -0800 |
| Message-ID | <95cfefe2-1a05-4ca3-8955-1cdc76e7ebee@googlegroups.com> |
| In reply to | #17967 |
On Monday, December 10, 2012 10:17:24 PM UTC+1, Marcel Hendrix wrote: > jzaxxxx@gmail.com writes Re: SHA-512 > > > > > On Monday, December 10, 2012 6:53:44 AM UTC-5, m.a.m....@tue.nl wrote: > > >> On Monday, December 10, 2012 2:29:04 AM UTC+1, jza...@gmail.com wrote: > > >> > > >> [..] > > >> > > >> [ Egg on my face, should not have touched the DECIMAL/HEX issues yet. ] > > > > > >> Yes, the code now runs and gives the correct results for S" abc". > > >> > > >> This would be the first working SHA-512 in 64-bit Forth! > > > > > Hey GREAT! :-) > > > > > Be sure to do the longer example also to make sure there are no > > > other problems (which I don't forsee). > > > > Unfortunately, the longer string does not hash properly. To avoid > > more egg on my face, I downloaded your latest code from 4share. > > To make it work in iForth64 I needed the patch that I have appended > > (Enable PLACE and rewrite timer-start and ms?, typos in SHA256 and > > SHAinit, extra stack item in EX2a dropped.) > > > > [..] > > > > > I ALWAYS start from the algorithm description to do Forth versions, even if > > > I look at other existing language implementations, because I think so much > > > differently when I create in Forth. > > > > Ok. Please forget about macro's, ]L and EVALUATE for the time being. Also, > > the 15 PICKs you may get rid off without any ill effects :-) > > > > Here are the current results with your patched code: > > > > FORTH> in > > Redefining split-at-char > > Redefining MACRO > > Redefining MS? ok > > FORTH> SHAtest > > SHA-512 test suite: > > cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e "" > > ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f "abc" > > 8cc1fec83f21176a675306d9f80b26597469eff277ab9d0f03a725b15d57d33a2e3aadea4727cf632a3e5ec3c744bcc0eb2183c41c717cc4a84fa6dbf8244243 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" > > 9bc68759247e3332bec1c79d128d28a8931d0c9f96c8aa975731b563475fdddddf7f873c25086908effe270e23c5a01e5dfb3289bf5d091d8fb454b1bcf98dda 2 million copies of ASCII 'a' (61h) > > 19ef4876e03c93476e00b486c62108d4d7136f6d4782817b449bacf44d0963e03f5ab474cb6822c2963a979e8e15298cd60f22841c51f6161620c0e031c65f4a 400,000 copies of ASCII BL (20h) > > ok > > > > -marcel > > > > -- ---------- > > Compare: (<)C:\Users\marcel\Desktop\SHA-512.frt > > with: (>)C:\Users\marcel\Desktop\SHA-512VFX.F > > > > 1d1 > > < ANEW -sha-512 > > 19c18,19 > > < : PLACE ( caddr n addr -) 2DUP C! CHAR+ SWAP CHARS MOVE ; > > --- > > > \ Not needed for SwiftForth v 2.00.3, needed for Win32Forth V 4.10 > > > \ : PLACE ( caddr n addr -) 2DUP C! CHAR+ SWAP CHARS MOVE ; > > 161c161 > > < : SHA512 ( Wadr - ) > > --- > > > : SHA256 ( Wadr - ) > > 233c233 > > < SHAinit 2DUP SHAlen 2! hashfullblocks hashfinal > > --- > > > SHAinit 2DUP SHAlen 2 ! hashfullblocks hashfinal > > 375,376c375,376 > > < EX1 S" abc" QuoteString > > < EX2 EX2a DROP QuoteString > > --- > > > EX1 S" abc" QuoteString > > > EX2 EX2a QuoteString > > 381,384c381,391 > > < VARIABLE start-ms > > < > > < : TIMER-START ( -- ) ?MS start-ms ! ; > > < : MS? ( -- u ) ?MS start-ms @ - ( abs ) ; > > --- > > > \ =========== VFX Forth specific performance test =========== > > > > > > [undefined] GetTickCount > > > [IF] extern: DWORD PASCAL GetTickCount( void ) [THEN] > > > > > > VARIABLE start-ms > > > > > > : TIMER-START ( -- ) GetTickCount start-ms ! ; > > > > > > : MS? ( -- u ) GetTickCount start-ms @ - ( abs ) ; > > > Hi Marcel, > Also, the 15 PICKs you may get rid off without any ill effects :-) I'm curious - how do you do this? It would be great to remove them from the SHA-256 code too :-) Best regards, Howerd
[toc] | [prev] | [next] | [standalone]
| From | mhx@iae.nl (Marcel Hendrix) |
|---|---|
| Date | 2012-12-08 22:08 +0200 |
| Message-ID | <58951395918435@frunobulax.edu> |
| In reply to | #17919 |
jzakiya@gmail.com writes Re: SHA-512
[..]
> I've corrected the SHA-256 code, and written a SHA-512 version for 64-bit cpus/OSs.
> Since I don't have a 64-bit forth I haven't run it but I'm pretty
> sure it works, as the architecture is the same as SHA-256, with just different
> sized constants and shifts/rotates.
It runs with minor edits (see below). I removed ]L and EVALUATE as it is unnecessary
and slows down the code.
Unfortunately, the results are wrong.
The speed is OK, 2Mbytes*1000/14.417s/2.67GHz = 51.9 MB/s/GHz
FORTH> SHAtest
SHA-512 test suite:
1ca51eba68fd0ca4284ec2b2bd024de70a231b14b914411996269e0f5c61ec7f02a2054b467965def1d526c52c525a1103c8c3aea8cc5e406f60e740bcd05969 ""
3e9e4c5b0b4eacc5f38516a9afb1c79ea5a959679b9bc6e5de04eedbaa41a198b227138994715fa52fdcb64b3689061926496b9b9d197613c0d35eafc269a023 "abc"
914d0fc46d27cabc63a868fba6b8eb0a4996553265854a33344481c3a0e00536b36c2bf0df78eb059fcaadcc25687d7eb95cbaa07591fa9e6f08839f0e7bc626 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
7e4256b1fcf2c09b867e71bc8189f631999302f27f8c5c2c27ef6c39e1d3016afd6b1bdfa452847695ca18905429a365fa19d071f30a440b0c548a4d4f99fe49 2 million copies of ASCII 'a' (61h)
f4568c36ddc5a2fa022a5af467ba59b25bde16a098ea3fb83c879104c9473795a18df1e4d407379f0fe7d266dc7cfcb3d205770c35c90e8d174d4c7ed1dc0f37 400,000 copies of ASCII BL (20h)
ok
FORTH> test1 test2 test3
SHA-512 test for EX1 for 1000 loops in milliseconds is 2
SHA-512 test for EX2 for 1000 loops in milliseconds is 4
SHA-512 test for EX3 for 1000 loops in milliseconds is 14417 ok
-marcel
--
ANEW -sha-512
\ Forth code for Secure Hash Algorithm 512 (SHA-512)
\ NIST spec at: http://csrc.nist.gov/encryption/tkhash.html
\ For Little/Big Endian byte addressable CPUs, e.g. Intel/Power PC
\ DEPENDENCIES: CORE EXT WORDSET ; COMMON USAGE 3DROP ?DO CELL-
\ Use of this code is free subject to acknowledgment of copyright.
\ Copyright (c) 2012 Jabari Zakiya -- jzakiya@mail.com 12/07/2012
\ ======================= MACRO Wordset ========================
\ MACRO wordset from Wil Baden's Tool Belt series in
\ Forth Dimensions (FD) Vol. 19, No. 2, July/August 1997
\ Original code has been modified to make more efficient
\ MACRO allows insertion of parameters following the macro
\ "\" represents place where parameter is inserted
\ Example: MACRO ?? " IF \ THEN "
\ : FOO .. ?? EXIT .... ; ?? compiles to -- IF EXIT THEN
\ PLACE and STRING for system if needed
\ Not needed for SwiftForth v 2.00.3, needed for Win32Forth V 4.10
\ : PLACE ( caddr n addr -) 2DUP C! CHAR+ SWAP CHARS MOVE ;
: SSTRING ( char "ccc" -) WORD COUNT HERE OVER 1+ CHARS ALLOT PACK DROP ;
\ Versions of /STRING and ANEW if system doesn't have them
\ : /STRING ( a n k - a+k n-k) ( OVER MIN) TUCK - >R CHARS + R> ;
\ : ANEW >IN @ BL WORD FIND IF EXECUTE ELSE DROP THEN >IN ! MARKER ;
\ : split-at-char ( a n char - a k a+k n-k)
\ >R 2DUP BEGIN DUP WHILE OVER C@ R@ -
\ WHILE 1 /STRING REPEAT THEN
\ R> DROP TUCK 2>R - 2R>
\ ;
: DOES>MACRO \ Compile the macro, including external parameters
DOES> COUNT BEGIN [CHAR] \ split-at-char 2>R EVALUATE R@
WHILE BL WORD COUNT EVALUATE 2R> 1 /STRING REPEAT
R> DROP R> DROP
;
\ Macro creation word which allows parameter insertion
: MACRO CREATE IMMEDIATE CHAR SSTRING DOES>MACRO ;
\ ==================== Start SHA-256 Code ====================
DECIMAL
64 CONSTANT CELLSIZE \ CPU bitsize
2VARIABLE SHAlen \ Holds byte length of string < 2^128 bits|2^125 bytes
CREATE SHAval 8 CELLS ALLOT \ Holds hash after each block
CREATE SHAsh 88 CELLS ALLOT \ Fully extended hash array
CREATE W 16 CELLS ALLOT \ Holds message block
1 W ! \ For compile time endian testing
HEX
\ SHA-512 round constants
428a2f98d728ae22 CONSTANT K0 7137449123ef65cd CONSTANT K1
b5c0fbcfec4d3b2f CONSTANT K2 e9b5dba58189dbbc CONSTANT K3
3956c25bf348b538 CONSTANT K4 59f111f1b605d019 CONSTANT K5
923f82a4af194f9b CONSTANT K6 ab1c5ed5da6d8118 CONSTANT K7
d807aa98a3030242 CONSTANT K8 12835b0145706fbe CONSTANT K9
243185be4ee4b28c CONSTANT K10 550c7dc3d5ffb4e2 CONSTANT K11
72be5d74f27b896f CONSTANT K12 80deb1fe3b1696b1 CONSTANT K13
9bdc06a725c71235 CONSTANT K14 c19bf174cf692694 CONSTANT K15
e49b69c19ef14ad2 CONSTANT K16 efbe4786384f25e3 CONSTANT K17
0fc19dc68b8cd5b5 CONSTANT K18 240ca1cc77ac9c65 CONSTANT K19
2de92c6f592b0275 CONSTANT K20 4a7484aa6ea6e483 CONSTANT K21
5cb0a9dcbd41fbd4 CONSTANT K22 76f988da831153b5 CONSTANT K23
983e5152ee66dfab CONSTANT K24 a831c66d2db43210 CONSTANT K25
b00327c898fb213f CONSTANT K26 bf597fc7beef0ee4 CONSTANT K27
c6e00bf33da88fc2 CONSTANT K28 d5a79147930aa725 CONSTANT K29
06ca6351e003826f CONSTANT K30 142929670a0e6e70 CONSTANT K31
27b70a8546d22ffc CONSTANT K32 2e1b21385c26c926 CONSTANT K33
4d2c6dfc5ac42aed CONSTANT K34 53380d139d95b3df CONSTANT K35
650a73548baf63de CONSTANT K36 766a0abb3c77b2a8 CONSTANT K37
81c2c92e47edaee6 CONSTANT K38 92722c851482353b CONSTANT K39
a2bfe8a14cf10364 CONSTANT K40 a81a664bbc423001 CONSTANT K41
c24b8b70d0f89791 CONSTANT K42 c76c51a30654be30 CONSTANT K43
d192e819d6ef5218 CONSTANT K44 d69906245565a910 CONSTANT K45
f40e35855771202a CONSTANT K46 106aa07032bbd1b8 CONSTANT K47
19a4c116b8d2d0c8 CONSTANT K48 1e376c085141ab53 CONSTANT K49
2748774cdf8eeb99 CONSTANT K50 34b0bcb5e19b48a8 CONSTANT K51
391c0cb3c5c95a63 CONSTANT K52 4ed8aa4ae3418acb CONSTANT K53
5b9cca4f7763e373 CONSTANT K54 682e6ff3d6b2b8a3 CONSTANT K55
748f82ee5defb2fc CONSTANT K56 78a5636f43172f60 CONSTANT K57
84c87814a1f0ab72 CONSTANT K58 8cc702081a6438ec CONSTANT K59
90befffa23631e28 CONSTANT K60 a4506cebde82bde9 CONSTANT K61
bef9a3f7b2c67915 CONSTANT K62 c67178f2e372532b CONSTANT K63
ca273eceea26619c CONSTANT K64 d186b8c721c0c207 CONSTANT K65
eada7dd6cde0eb1e CONSTANT K66 f57d4f7fee6ed178 CONSTANT K67
06f067aa72176fba CONSTANT K68 0a637dc5a2c898a6 CONSTANT K69
113f9804bef90dae CONSTANT K70 1b710b35131c471b CONSTANT K71
28db77f523047d84 CONSTANT K72 32caab7b40c72493 CONSTANT K73
3c9ebe0a15c9bebc CONSTANT K74 431d67c49c100d4c CONSTANT K75
4cc5d4becb3e42b6 CONSTANT K76 597f299cfc657e2a CONSTANT K77
5fcb6fab3ad6faec CONSTANT K78 6c44198c4a475817 CONSTANT K79
DECIMAL
0 VALUE H[H] \ Pointer to addr of hash value H for each round
: H[G] H[H] 1 CELLS + ; \ Return G adr
: H[F] H[H] 2 CELLS + ; \ Return F adr
: H[E] H[H] 3 CELLS + ; \ Return E adr
: H[D] H[H] 4 CELLS + ; \ Return D adr
: H[C] H[H] 5 CELLS + ; \ Return C adr
: H[A] H[H] 7 CELLS + ; \ Return A adr
: SHAinit ( -) \ Load initial hash values H0 - H7
[ HEX ] 6a09e667f3bcc908 ( H0) bb67ae8584caa73b ( H1)
3c6ef372fe94f82b ( H2) a54ff53a5f1d36f1 ( H3)
510e527fade682d1 ( H4) 9b05688c2b3e6c1f ( H5)
1f83d9abfb41bd6b ( H6) 5be0cd19137e2179 ( H7)
[ DECIMAL ]
SHAsh 7 0 DO TUCK ! CELL+ LOOP ! \ Put initial hash in SHAsh array
SHAsh SHAval 8 CELLS CMOVE \ Put copy in SHAval array
SHAsh TO H[H] \ Init pointer to last hash value
;
: UpDateHash ( -) \ Update hash values and load arrays with new values
SHAsh SHAval H[H] \ Place array addresses on stack
8 0 DO DUP >R @ SWAP DUP >R @ + DUP \ Compute updated hash subvalue
R@ ! OVER ! CELL+ R> CELL+ R> CELL+ \ Store updated hash subvalue
LOOP 3DROP \ Clear stack when done
SHAsh TO H[H] \ Init pointer to last subvalue
;
\ ( - n ) n = (E AND F) XOR (~E AND G)
: Ch H[F] 2@ OVER AND SWAP INVERT H[G] @ AND XOR ;
\ ( - n ) n = (A AND B) XOR (A AND C) XOR (B AND C)
: Maj H[C] DUP >R CELL+ 2@ OVER AND SWAP R@ @ AND XOR R> 2@ AND XOR ;
\ ( - n ) T1x = Ch(e,f,g) + Sig1(e) + h
: T1x Ch H[E] @ DUP >R 14 ror R@ 18 ror XOR R> 41 ror XOR + H[H] @ + ;
\ ( - n ) T2 = Maj(a,b,c) + Sig0(a)
: T2 Maj H[A] @ DUP >R 28 ror R@ 34 ror XOR R> 39 ror XOR + ;
\ ( x - n ) n = ROR1(X) XOR ROR8(X) XOR SHR7(X)
: sig0 ( x - n ) DUP DUP 1 ROR SWAP 8 ROR XOR SWAP 7 RSHIFT XOR ;
\ ( x - n ) n = ROR19(X) XOR ROR61(X) XOR SHR6(X)
: sig1 ( x - n ) DUP DUP 19 ROR SWAP 61 ROR XOR SWAP 6 RSHIFT XOR ;
\ Put two copies of original Wi on stack, keep its address
: Wi@ ( [Wi] - wi [Wi] wi) DUP @ TUCK ;
\ Create 2 copies of new Wi' from Wi on stack ( ..Wi -..Wi' Wi')
: Wi 15 PICK 15 PICK sig0 + 7 PICK + 2 PICK sig1 + DUP ;
\ Drop 80 Wi cells from stack ( W0..W79 - )
: WiDROP 5 0 DO 2DROP 2DROP 2DROP 2DROP 2DROP 2DROP 2DROP 2DROP LOOP ;
: subrnd DUP H[D] +! T2 + H[G] TO H[H] H[A] ! ;
MACRO rndi\ " Wi@ T1x + \ + subrnd CELL+"
MACRO rndn\ " Wi T1x + \ + subrnd "
: SHA512 ( Wadr - )
rndi\ K0 rndi\ K1 rndi\ K2 rndi\ K3 \ Wi = Mi for 1st 16 rounds
rndi\ K4 rndi\ K5 rndi\ K6 rndi\ K7
rndi\ K8 rndi\ K9 rndi\ K10 rndi\ K11
rndi\ K12 rndi\ K13 rndi\ K14 rndi\ K15 DROP ( W0..W15 )
rndn\ K16 rndn\ K17 rndn\ K18 rndn\ K19 \ Wj now function of Wi
rndn\ K20 rndn\ K21 rndn\ K22 rndn\ K23
rndn\ K24 rndn\ K25 rndn\ K26 rndn\ K27
rndn\ K28 rndn\ K29 rndn\ K30 rndn\ K31
rndn\ K32 rndn\ K33 rndn\ K34 rndn\ K35
rndn\ K36 rndn\ K37 rndn\ K38 rndn\ K39
rndn\ K40 rndn\ K41 rndn\ K42 rndn\ K43
rndn\ K44 rndn\ K45 rndn\ K46 rndn\ K47
rndn\ K48 rndn\ K49 rndn\ K50 rndn\ K51
rndn\ K52 rndn\ K53 rndn\ K54 rndn\ K55
rndn\ K56 rndn\ K57 rndn\ K58 rndn\ K59
rndn\ K60 rndn\ K61 rndn\ K62 rndn\ K63
rndn\ K64 rndn\ K65 rndn\ K66 rndn\ K67
rndn\ K68 rndn\ K69 rndn\ K70 rndn\ K71
rndn\ K72 rndn\ K73 rndn\ K74 rndn\ K75
rndn\ K76 rndn\ K77 rndn\ K78 rndn\ K79 WiDROP ( - )
UpDateHash
;
: setlen ( -- ) \ Store bit count into last two cells
SHAlen 2@ D2* D2* D2* ( bytes->bits) W 112 CHARS + ! W 120 CHARS + ! ;
: bytes>< ( m -- w ) \ Reverse cell bytes: 1234567890abcdef <-> efcdab9078563412
[ HEX ] DUP >R 38 LSHIFT R@ FF00 AND 28 LSHIFT OR
R@ FF0000 AND 18 LSHIFT OR R@ FF000000 AND 8 LSHIFT OR
R@ 20 RSHIFT FF AND OR R@ 18 RSHIFT FF00 AND OR R@ 10 RSHIFT FF0000 AND OR
R> 8 RSHIFT FF000000 AND OR [ DECIMAL ]
;
: cellsreverse ( adr n -- ) \ Reverse bytes of n cells in array
0 DO DUP @ bytes>< OVER ! CELL+ LOOP DROP
;
W C@ [IF] \ if little ENDIAN, e.g. Intel/AMD
: endian16 ( adr -- adr ) DUP 16 cellsreverse ;
: endian14 ( adr -- adr ) DUP 14 cellsreverse ;
[ELSE] \ if big ENDIAN, e.g. Macs
: endian16 ( adr -- adr ) ; \ Do nothing
: endian14 ( adr -- adr ) ; \ Do nothing
[THEN]
\ Do all 128 byte blocks leaving remainder block
: hashfullblocks ( adr1 dcount -- adr2 count ) \ dcount is double number: lo hi
SWAP DUP >R 7 RSHIFT ( adr1 hi lo* ) \ Store lo on return, do lo*=lo/128
OVER CELLSIZE 7 - LSHIFT OR >R ( adr1 hi ) \ Return is now: :R lo lo'
( hi) 7 RSHIFT 0 ?DO ( adr1 ) \ Do if hi'= hi/128 > 0
0 0 DO DUP endian16 SHA512 128 + LOOP ( lo' adr' ) \ Hash for 2^cellsize full blocks
LOOP ( adr' ) \ Hash for hi'*2^cellsize full blocks
R> 0 ?DO DUP endian16 SHA512 128 + LOOP ( adr' ) \ Hash block for lo count full 128 byte blocks
R> ( lo) 127 AND ( adr2 cnt2 ) \ Leave address and count for partial block
;
: hashfinal ( addr count -- ) \ Hash partial and/or last block
DUP >R W SWAP CMOVE \ Move bytes into block W array
W R@ + 128 OVER C! ( adr ) \ Put 80h after last message byte
CHAR+ 111 R@ - ( adr # ) \ Compute tentative 0 byte FILL count
R> 111 > ( adr # ? ) \ Is partial block byte count > 111 ?
IF 16 + 0 FILL ( -- ) \ If yes, FILL rest of block w/zeroes
W endian16 SHA512 ( -- ) \ Endian adjust block if required, then hash
W 112 ( adr 112 ) \ Now setup last block containing bit count
THEN ( adr # )
0 FILL setlen W endian14 SHA512 \ Zero FILL last block, set message bit count
; ( -- ) \ Endian adjust, except bit count, then hash
\ Compute SHA512 from a counted buffer of text
: SHAbuffer ( addr dcount -- )
SHAinit 2DUP SHAlen 2! hashfullblocks hashfinal ;
\ =============== Hash string display wordset ===============
DECIMAL
\ Array of digits 0123456789abcdef
: digit$ ( -- adr ) S" 0123456789abcdef" DROP ;
: intdigits ( -- ) 0 PAD ! ;
: savedigit ( n -- ) PAD C@ 1+ DUP PAD C! PAD + C! ;
: bytedigits ( n1 -- )
DUP 4 RSHIFT digit$ + C@ savedigit 15 AND digit$ + C@ savedigit
;
W C@ [IF] \ little ENDIAN
: celldigits ( a1 -- ) DUP 7 + DO I C@ bytedigits -1 +LOOP ;
[ELSE] \ big ENDIAN
: celldigits ( a1 -- ) DUP 8 + SWAP DO I C@ bytedigits LOOP ;
[THEN]
: SHAstring ( -- adr count ) \ Return counted SHA-512 string array
intdigits SHAval 7 CELLS + 8 0 DO DUP celldigits CELL- LOOP DROP PAD COUNT ;
\ Display SHA-512 hash value in hex ( A B C D E F G H )
: HASH. CR SHAstring TYPE SPACE ;
: QuoteString ( adr cnt --) [CHAR] " EMIT TYPE [CHAR] " EMIT ;
\ ==================== File hash wordset ====================
VARIABLE rfileid \ Holds fileid number of input file
: InputFileName ( -- ior)
CR CR ." Filename: " PAD DUP 80 ACCEPT ( adr #)
R/O OPEN-FILE SWAP rfileid ! ( ior)
;
: TryAgain? ( -- ?)
CR CR ." Invalid iput file, try again? (Y/N)"
KEY DUP EMIT DUP [CHAR] N = SWAP [CHAR] n = OR
;
\ Read n bytes from input file, store at addr array
: bytes@ ( adr n - ) rfileid @ READ-FILE 2DROP ;
: storelen ( lo hi - ) \ Store bit count into last two cells
D2* D2* D2* ( bytes->bits) W 112 CHARS + ! W 120 CHARS + !
;
: getpartial ( cnt -- W' cnt2 ?)
W 2DUP SWAP DUP >R bytes@ ( cnt1 adr1 )
+ 128 OVER C! CHAR+ 111 R@ - R> 111 > ( adr2 cnt2 ?)
;
: block@ W 128 bytes@ ;
: SHAfile ( -- )
BEGIN InputFileName ( ior) \ Enter filename
WHILE TryAgain? IF EXIT THEN \ Not valid, try (not) again
REPEAT SHAinit \ Valid file, init transform
rfileid @ FILE-SIZE DROP ( ud ) \ Get bytesize of input file
2 0 D- \ Dec cnt by 2 for CR|LF EOF
CR ." Bytesize: " 2DUP D. \ Display filesize to screen
2DUP 2>R \ ( lo hi ) Save file byte cnt on RETURN
OVER 7 RSHIFT OVER \ ( lo hi lo* hi )
CELLSIZE 7 - LSHIFT OR SWAP 7 RSHIFT \ ( lo lo' hi') full block count
0 ?DO 0 0 DO block@ W endian16 SHA512 LOOP LOOP \ Hash hi*2^cellsize full blocks
0 ?DO block@ W endian16 SHA512 LOOP \ Hash lo count full 128 byte blocks
( lo) 127 AND ( rembytes) getpartial ( adr cnt ?) \ Read remaining bytes
IF 16 + 0 FILL W endian16 SHA512 W 112 THEN \ Do if rembytes > 111
0 FILL 2R> storelen W endian14 SHA512 \ Do last block
CR ." SHA-512 : " SHAstring TYPE CR \ Show SHA-512 hash for file
rfileid @ CLOSE-FILE DROP \ Close the input file
;
\ ==================== SHA-512 Test Suite =====================
DECIMAL
\ Load W array with data on stack
: WLoad ( d0..d15 -- ) W 15 CELLS + ( d0..d15 W[15] )
16 0 DO TUCK ! CELL- LOOP DROP
;
\ -------------------------------------------------------------
\ EXAMPLE 1: from FIPS PUB
\ Message: ASCII string 'abc'
\ Hash = DDAF35A1 93617ABA CC417349 AE204131 12E6FA4E 89A97EA2 0A9EEEE6 4B55D39A
\ 2192992A 274FC1A8 36BA3C23 A3FEEBBD 454D4423 643CE80E 2A9AC94F A54CA49F
\ Compute and display hash for ASCII string 'abc'
: EX1 S" abc" U>D ( adr dcount) SHAbuffer HASH. ;
\ -------------------------------------------------------------
\ EXAMPLE 2: from FIPS PUB
\ Message:"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
\ Hash = 8E959B75 DAE313DA 8CF4F728 14FC143F 8F7779C6 EB9F7FA1 7299AEAD B6889018
\ 501D289E 4900F7E4 331B99DE C4B5433A C7D329EE B6DD2654 5E96E55B 874BE909
: EX2a S" abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" U>D ( adr dcount) ;
: EX2 EX2a SHAbuffer HASH. ;
\ -------------------------------------------------------------
\ EXAMPLE 3:
\ Message: 2 million copies of 'a' (61h), (16 million bits)
\ Hash =
\ Load block of all 'a's (61h), must hash 15,625 times
: EX3a W 128 [CHAR] a FILL ;
\ Last message block: 1st bit a '1', bit-count = 16 million
: EX3b [ HEX ] 8000000000000000 0 0 0 0 0 0 0 0 0 0 0 0 0
[ DECIMAL ] 0 16000000 WLoad
;
\ Do hash for message of 2 million copies of ASCII 'a' (61h)
: EX3 SHAinit EX3a 15625 0 DO W SHA512 LOOP EX3b W SHA512 HASH. ;
\ -------------------------------------------------------------
\ EXAMPLE 4:
\ Message: 400,000 SPACES 'BL' (20h), (3,200,000 bits)
\ Hash =
\ Load block of all "BL' (20h), hash 156 full blocks + 16 bytes
: EX4a W 128 BL FILL ;
\ Last message block: 1st bit a '1', bit-count = 3,200,000
: EX4b [ HEX ] 8000000000000000 0 0 0 0 0 0 0 0 0 0 0 0 0
[ DECIMAL ] 0 3200000 WLoad
;
\ Do hash for message of 400,000 SPACES 'BL' (20h)
: EX4 SHAinit EX4a 3125 0 DO W SHA512 LOOP EX4b W SHA512 HASH. ;
\ -------------------------------------------------------------
\ Message: blank string ''
\ Hash =
: SHATest ( -- )
CR ." SHA-512 test suite:"
S" " U>D ( adr dcount) SHAbuffer HASH. S" " QuoteString
EX1 S" abc" QuoteString
EX2 EX2a DROP QuoteString
EX3 S" 2 million copies of ASCII 'a' (61h)" TYPE
EX4 S" 400,000 copies of ASCII BL (20h)" TYPE CR ;
\ =========== Forth specific performance test ===========
VARIABLE start-ms
: TIMER-START ( -- ) ?MS start-ms ! ;
: MS? ( -- u ) ?MS start-ms @ - ( abs ) ;
: .### ( -) BASE @ >R DECIMAL MS? 0 <# # # # [CHAR] . HOLD #S #> R> BASE ! TYPE ;
DECIMAL 1000 VALUE N#
: [EX1] S" abc" U>D ( adr dcount) SHAbuffer ;
: [EX2] EX2a SHAbuffer ;
: [EX3] SHAinit EX3a 15625 0 DO W SHA512 LOOP EX3b W SHA512 ;
: test1 [ DECIMAL ]
cr ." SHA-512 test for EX1 for " N# . ." loops in milliseconds is "
TIMER-START N# 0 DO [EX1] LOOP MS? U.
;
: test2 [ DECIMAL ]
cr ." SHA-512 test for EX2 for " N# . ." loops in milliseconds is "
TIMER-START N# 0 DO [EX2] LOOP MS? U.
;
: test3 [ DECIMAL ]
cr ." SHA-512 test for EX3 for " N# . ." loops in milliseconds is "
TIMER-START N# 0 DO [EX3] LOOP MS? U.
;
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.forth
csiph-web