mirror of
https://github.com/sheumann/65816-crypto.git
synced 2024-09-27 21:57:27 +00:00
Unroll SHA-1 loop with variables substituted to avoid cycling values around.
SHA-1 has five state variables (a,b,c,d,e), and each iteration of the core loop cycles their values around (e.g. a->b), in some cases with modifications. By unrolling the loop by a factor of five and appropriately substituting the values operated on in each iteration, we can avoid actually doing the copies implied by this cycling, which gives a fairly significant performance gain.
This commit is contained in:
parent
aab47e38e7
commit
2d8c85bd98
150
sha1.macros
150
sha1.macros
@ -20,64 +20,64 @@
|
|||||||
* Left-rotate 32-bit value in &loc (DP or 16-bit address) by &n positions
|
* Left-rotate 32-bit value in &loc (DP or 16-bit address) by &n positions
|
||||||
macro
|
macro
|
||||||
ROTL4 &loc,&n
|
ROTL4 &loc,&n
|
||||||
aif &n>16,.dorotr
|
aif &n>16,.dorotr2
|
||||||
lda &loc
|
lda &loc
|
||||||
lcla &i
|
lcla &i
|
||||||
&i seta &n
|
&i seta &n
|
||||||
.rotlloop
|
.rotlloop2
|
||||||
asl a ;to set carry
|
asl a ;to set carry
|
||||||
rol &loc+2
|
rol &loc+2
|
||||||
rol &loc
|
rol &loc
|
||||||
&i seta &i-1
|
&i seta &i-1
|
||||||
aif &i>0,.rotlloop
|
aif &i>0,.rotlloop2
|
||||||
ago .end
|
ago .end2
|
||||||
.dorotr
|
.dorotr2
|
||||||
ROTR4 &loc,32-&n
|
ROTR4 &loc,32-&n
|
||||||
.end
|
.end2
|
||||||
mend
|
mend
|
||||||
|
|
||||||
* &to := &from ROTR4 &n
|
* &to := &from ROTR4 &n
|
||||||
macro
|
macro
|
||||||
ROTR4MOVE &to,&from,&n
|
ROTR4MOVE &to,&from,&n
|
||||||
aif &n>16,.dorotl
|
aif &n>16,.dorotl3
|
||||||
lda &from
|
lda &from
|
||||||
sta &to
|
sta &to
|
||||||
lda &from+2
|
lda &from+2
|
||||||
sta &to+2
|
sta &to+2
|
||||||
lcla &i
|
lcla &i
|
||||||
&i seta &n
|
&i seta &n
|
||||||
.rotrloop
|
.rotrloop3
|
||||||
lsr a ;to set carry
|
lsr a ;to set carry
|
||||||
ror &to
|
ror &to
|
||||||
ror &to+2
|
ror &to+2
|
||||||
&i seta &i-1
|
&i seta &i-1
|
||||||
aif &i>0,.rotrloop
|
aif &i>0,.rotrloop3
|
||||||
ago .end
|
ago .end3
|
||||||
dorotl
|
dorotl3
|
||||||
ROTL4MOVE &to,&from,32-&n
|
ROTL4MOVE &to,&from,32-&n
|
||||||
.end
|
.end3
|
||||||
mend
|
mend
|
||||||
|
|
||||||
* &to := &from ROTL4 &n
|
* &to := &from ROTL4 &n
|
||||||
macro
|
macro
|
||||||
ROTL4MOVE &to,&from,&n
|
ROTL4MOVE &to,&from,&n
|
||||||
aif &n>16,.dorotr
|
aif &n>16,.dorotr4
|
||||||
lda &from+2
|
lda &from+2
|
||||||
sta &to+2
|
sta &to+2
|
||||||
lda &from
|
lda &from
|
||||||
sta &to
|
sta &to
|
||||||
lcla &i
|
lcla &i
|
||||||
&i seta &n
|
&i seta &n
|
||||||
.rotlloop
|
.rotlloop4
|
||||||
asl a ;to set carry
|
asl a ;to set carry
|
||||||
rol &to+2
|
rol &to+2
|
||||||
rol &to
|
rol &to
|
||||||
&i seta &i-1
|
&i seta &i-1
|
||||||
aif &i>0,.rotlloop
|
aif &i>0,.rotlloop4
|
||||||
ago .end
|
ago .end4
|
||||||
.dorotr
|
.dorotr4
|
||||||
ROTR4MOVE &to,&from,32-&n
|
ROTR4MOVE &to,&from,32-&n
|
||||||
.end
|
.end4
|
||||||
mend
|
mend
|
||||||
|
|
||||||
|
|
||||||
@ -208,67 +208,68 @@ dorotl
|
|||||||
mend
|
mend
|
||||||
|
|
||||||
|
|
||||||
* One part of the loop for processing blocks (&part is 1, 2, 3, or 4)
|
|
||||||
macro
|
|
||||||
BlockLoopPart &part
|
|
||||||
|
|
||||||
loop&part anop
|
* One iteration of the loop for processing blocks.
|
||||||
|
* The a,b,c,d,e variables are given as parameters so we can aviod cycling them.
|
||||||
|
macro
|
||||||
|
BlockLoopIter &a,&b,&c,&d,&e,&part
|
||||||
|
|
||||||
stx idx
|
stx idx
|
||||||
ROTL4MOVE temp,a_,5
|
ROTL4MOVE temp,&a,5
|
||||||
|
|
||||||
* f_0 to f_19
|
* f_0 to f_19
|
||||||
aif &part<>1,.skip1
|
aif &part<>1,.skip1
|
||||||
lda c
|
lda &c
|
||||||
eor d
|
eor &d
|
||||||
and b
|
and &b
|
||||||
eor d
|
eor &d
|
||||||
clc
|
clc
|
||||||
adc #$7999
|
adc #$7999
|
||||||
sta f_plus_k
|
sta f_plus_k
|
||||||
|
|
||||||
lda c+2
|
lda &c+2
|
||||||
eor d+2
|
eor &d+2
|
||||||
and b+2
|
and &b+2
|
||||||
eor d+2
|
eor &d+2
|
||||||
adc #$5A82
|
adc #$5A82
|
||||||
sta f_plus_k+2
|
sta f_plus_k+2
|
||||||
.skip1
|
.skip1
|
||||||
|
|
||||||
* f_20 to f_39
|
* f_20 to f_39
|
||||||
aif &part<>2,.skip2
|
aif &part<>2,.skip2
|
||||||
lda b
|
lda &b
|
||||||
eor c
|
eor &c
|
||||||
eor d
|
eor &d
|
||||||
clc
|
clc
|
||||||
adc #$EBA1
|
adc #$EBA1
|
||||||
sta f_plus_k
|
sta f_plus_k
|
||||||
|
|
||||||
lda b+2
|
lda &b+2
|
||||||
eor c+2
|
eor &c+2
|
||||||
eor d+2
|
eor &d+2
|
||||||
adc #$6ED9
|
adc #$6ED9
|
||||||
sta f_plus_k+2
|
sta f_plus_k+2
|
||||||
.skip2
|
.skip2
|
||||||
|
|
||||||
* f_40 to f_59
|
* f_40 to f_59
|
||||||
aif &part<>3,.skip3
|
aif &part<>3,.skip3
|
||||||
lda c
|
lda &c
|
||||||
ora d
|
ora &d
|
||||||
and b
|
and &b
|
||||||
sta f40temp
|
sta f40temp
|
||||||
lda c
|
lda &c
|
||||||
and d
|
and &d
|
||||||
ora f40temp
|
ora f40temp
|
||||||
clc
|
clc
|
||||||
adc #$BCDC
|
adc #$BCDC
|
||||||
sta f_plus_k
|
sta f_plus_k
|
||||||
|
|
||||||
lda c+2
|
lda &c+2
|
||||||
ora d+2
|
ora &d+2
|
||||||
and b+2
|
and &b+2
|
||||||
sta f40temp
|
sta f40temp
|
||||||
lda c+2
|
lda &c+2
|
||||||
and d+2
|
and &d+2
|
||||||
ora f40temp
|
ora f40temp
|
||||||
adc #$8F1B
|
adc #$8F1B
|
||||||
sta f_plus_k+2
|
sta f_plus_k+2
|
||||||
@ -276,16 +277,16 @@ loop&part anop
|
|||||||
|
|
||||||
* f_60 to f_79
|
* f_60 to f_79
|
||||||
aif &part<>4,.skip4
|
aif &part<>4,.skip4
|
||||||
lda b
|
lda &b
|
||||||
eor c
|
eor &c
|
||||||
eor d
|
eor &d
|
||||||
clc
|
clc
|
||||||
adc #$C1D6
|
adc #$C1D6
|
||||||
sta f_plus_k
|
sta f_plus_k
|
||||||
|
|
||||||
lda b+2
|
lda &b+2
|
||||||
eor c+2
|
eor &c+2
|
||||||
eor d+2
|
eor &d+2
|
||||||
adc #$CA62
|
adc #$CA62
|
||||||
sta f_plus_k+2
|
sta f_plus_k+2
|
||||||
.skip4
|
.skip4
|
||||||
@ -300,44 +301,41 @@ loop&part anop
|
|||||||
tax
|
tax
|
||||||
clc
|
clc
|
||||||
tya
|
tya
|
||||||
adc e
|
adc &e
|
||||||
tay
|
tay
|
||||||
txa
|
txa
|
||||||
adc e+2
|
adc &e+2
|
||||||
tax
|
tax
|
||||||
clc
|
clc
|
||||||
tya
|
tya
|
||||||
adc f_plus_k
|
adc f_plus_k
|
||||||
tay
|
sta &e
|
||||||
txa
|
txa
|
||||||
adc f_plus_k+2
|
adc f_plus_k+2
|
||||||
tax
|
sta &e+2
|
||||||
|
|
||||||
lda d
|
ROTL4 &b,30
|
||||||
sta e
|
|
||||||
lda d+2
|
|
||||||
sta e+2
|
|
||||||
|
|
||||||
lda c
|
|
||||||
sta d
|
|
||||||
lda c+2
|
|
||||||
sta d+2
|
|
||||||
|
|
||||||
ROTL4MOVE c,b,30
|
|
||||||
|
|
||||||
lda a_
|
|
||||||
sta b
|
|
||||||
lda a_+2
|
|
||||||
sta b+2
|
|
||||||
|
|
||||||
sty a_
|
|
||||||
stx a_+2
|
|
||||||
|
|
||||||
ldx idx
|
ldx idx
|
||||||
inx
|
inx
|
||||||
inx
|
inx
|
||||||
inx
|
inx
|
||||||
inx
|
inx
|
||||||
|
mend
|
||||||
|
|
||||||
|
|
||||||
|
* One part of the loop for processing blocks (20 iterations)
|
||||||
|
macro
|
||||||
|
BlockLoopPart &part
|
||||||
|
|
||||||
|
loop&part anop
|
||||||
|
|
||||||
|
BlockLoopIter a_,b,c,d,e,&part
|
||||||
|
BlockLoopIter e,a_,b,c,d,&part
|
||||||
|
BlockLoopIter d,e,a_,b,c,&part
|
||||||
|
BlockLoopIter c,d,e,a_,b,&part
|
||||||
|
BlockLoopIter b,c,d,e,a_,&part
|
||||||
|
|
||||||
cpx #20*4
|
cpx #20*4
|
||||||
bge endloop&part
|
bge endloop&part
|
||||||
jmp loop&part
|
jmp loop&part
|
||||||
|
Loading…
Reference in New Issue
Block a user