Merge pull request #343 from max19/master

minor scrypt tweaks
This commit is contained in:
cgyarvin 2015-06-26 12:33:43 -07:00
commit e166d7dcba

View File

@ -4918,7 +4918,7 @@
-- --
~% %ed + ~ ~% %ed + ~
|% |%
++ puck :: public key ++ puck :: public key
~/ %puck ~/ %puck
|= sk=@I ^- @ |= sk=@I ^- @
?: (gth (met 3 sk) 32) !! ?: (gth (met 3 sk) 32) !!
@ -4929,12 +4929,12 @@
(lsh 0 3 (cut 0 [3 (sub b 5)] h)) (lsh 0 3 (cut 0 [3 (sub b 5)] h))
=+ aa=(scam bb a) =+ aa=(scam bb a)
(etch aa) (etch aa)
++ suck :: keypair from seed ++ suck :: keypair from seed
|= se=@I ^- @uJ |= se=@I ^- @uJ
=+ pu=(puck se) =+ pu=(puck se)
(can 0 ~[[b se] [b pu]]) (can 0 ~[[b se] [b pu]])
:: ::
++ sign :: certify ++ sign :: certify
~/ %sign ~/ %sign
|= [m=@ se=@] ^- @ |= [m=@ se=@] ^- @
=+ sk=(suck se) =+ sk=(suck se)
@ -4964,7 +4964,7 @@
(~(sit fo l) (add r (mul (shaz ha) a))) (~(sit fo l) (add r (mul (shaz ha) a)))
(can 0 ~[[b (etch rr)] [b ss]]) (can 0 ~[[b (etch rr)] [b ss]])
:: ::
++ veri :: validate ++ veri :: validate
~/ %veri ~/ %veri
|= [s=@ m=@ pk=@] ^- ? |= [s=@ m=@ pk=@] ^- ?
?: (gth (div b 4) (met 3 s)) | ?: (gth (div b 4) (met 3 s)) |
@ -4984,189 +4984,185 @@
++ scr :: scrypt ++ scr :: scrypt
~% %scr + ~ ~% %scr + ~
|% |%
++ sal |= [x=@ r=@] :: salsa20 hash ++ sal
?> =((mod r 2) 0) :: with r rounds |= [x=@ r=@] :: salsa20 hash
=+ few==>(fe .(a 5)) ?> =((mod r 2) 0) :: with r rounds
=+ ^= rot =+ few==>(fe .(a 5))
|= [a=@ b=@] =+ ^= rot
(mix (end 5 1 (lsh 0 a b)) (rsh 0 (sub 32 a) b)) |= [a=@ b=@]
=+ ^= lea (mix (end 5 1 (lsh 0 a b)) (rsh 0 (sub 32 a) b))
|= [a=@ b=@] =+ ^= lea
(net:few (sum:few (net:few a) (net:few b))) |= [a=@ b=@]
=> |% ++ qr :: quarterround (net:few (sum:few (net:few a) (net:few b)))
|= y=[@ @ @ @ ~] => |%
=+ zb=(mix &2.y (rot 7 (sum:few &1.y &4.y))) ++ qr :: quarterround
=+ zc=(mix &3.y (rot 9 (sum:few zb &1.y))) |= y=[@ @ @ @ ~]
=+ zd=(mix &4.y (rot 13 (sum:few zc zb))) =+ zb=(mix &2.y (rot 7 (sum:few &1.y &4.y)))
=+ za=(mix &1.y (rot 18 (sum:few zd zc))) =+ zc=(mix &3.y (rot 9 (sum:few zb &1.y)))
~[za zb zc zd] =+ zd=(mix &4.y (rot 13 (sum:few zc zb)))
++ rr :: rowround =+ za=(mix &1.y (rot 18 (sum:few zd zc)))
|= [y=(list ,@)] ~[za zb zc zd]
=+ za=(qr ~[&1.y &2.y &3.y &4.y]) ++ rr :: rowround
=+ zb=(qr ~[&6.y &7.y &8.y &5.y]) |= [y=(list ,@)]
=+ zc=(qr ~[&11.y &12.y &9.y &10.y]) =+ za=(qr ~[&1.y &2.y &3.y &4.y])
=+ zd=(qr ~[&16.y &13.y &14.y &15.y]) =+ zb=(qr ~[&6.y &7.y &8.y &5.y])
^- (list ,@) :~ =+ zc=(qr ~[&11.y &12.y &9.y &10.y])
&1.za &2.za &3.za &4.za =+ zd=(qr ~[&16.y &13.y &14.y &15.y])
&4.zb &1.zb &2.zb &3.zb ^- (list ,@) :~
&3.zc &4.zc &1.zc &2.zc &1.za &2.za &3.za &4.za
&2.zd &3.zd &4.zd &1.zd &4.zb &1.zb &2.zb &3.zb
== &3.zc &4.zc &1.zc &2.zc
++ cr :: columnround &2.zd &3.zd &4.zd &1.zd ==
|= [x=(list ,@)] ++ cr :: columnround
=+ ^= y %- rr ^- (list ,@) :~ |= [x=(list ,@)]
&1.x &5.x &9.x &13.x =+ ya=(qr ~[&1.x &5.x &9.x &13.x])
&2.x &6.x &10.x &14.x =+ yb=(qr ~[&6.x &10.x &14.x &2.x])
&3.x &7.x &11.x &15.x =+ yc=(qr ~[&11.x &15.x &3.x &7.x])
&4.x &8.x &12.x &16.x =+ yd=(qr ~[&16.x &4.x &8.x &12.x])
== ^- (list ,@) :~
^- (list ,@) :~ &1.ya &4.yb &3.yc &2.yd
&1.y &5.y &9.y &13.y &2.ya &1.yb &4.yc &3.yd
&2.y &6.y &10.y &14.y &3.ya &2.yb &1.yc &4.yd
&3.y &7.y &11.y &15.y &4.ya &3.yb &2.yc &1.yd ==
&4.y &8.y &12.y &16.y ++ dr :: doubleround
== |= [x=(list ,@)]
++ dr :: doubleround (rr (cr x))
|= [x=(list ,@)] ++ al :: add two lists
(rr (cr x)) |= [a=(list ,@) b=(list ,@)]
++ al :: add two lists |- ^- (list ,@)
|= [a=(list ,@) b=(list ,@)] ?~ a ~ ?~ b ~
|- ^- (list ,@) [i=(sum:few -.a -.b) t=$(a +.a, b +.b)]
?~ a ~ ?~ b ~ --
[i=(sum:few -.a -.b) t=$(a +.a, b +.b)] =+ xw=(rpp 5 16 x)
-- =+ ^= ow |- ^- (list ,@)
=+ xw=(rpp 5 16 x) ?~ r xw
=+ ^= ow |- ^- (list ,@) $(xw (dr xw), r (sub r 2))
?~ r xw (rep 5 (al xw ow))
$(xw (dr xw), r (sub r 2))
(rep 5 (al xw ow))
:: ::
++ rpp |= [a=bloq b=@ c=@] :: rip w/filler blocks ++ rpp
=+ q=(rip a c) |= [a=bloq b=@ c=@] :: rip w/filler blocks
=+ w=(lent q) =+ q=(rip a c)
?. =(w b) =+ w=(lent q)
?. (lth w b) (slag (sub w b) q) ?. =(w b)
^+ q (weld q (reap (sub b (lent q)) 0)) ?. (lth w b) (slag (sub w b) q)
q ^+ q (weld q (reap (sub b (lent q)) 0))
q
:: ::
++ xrl |= [a=(list ,@) b=(list ,@)] :: xor lists ++ bls
|- ^- (list ,@) |= [a=@ b=(list ,@)] :: split to sublists
?~ a b ?~ b a ?> =((mod (lent b) a) 0)
[i=(mix -.a -.b) t=$(a +.a, b +.b)] |- ^- (list (list ,@))
?~ b ~
[i=(scag a `(list ,@)`b) t=$(b (slag a `(list ,@)`b))]
:: ::
++ xrm |= [a=(list (list ,@)) b=(list (list ,@))] ++ slb
|- ^- (list (list ,@)) |= [a=(list (list ,@))]
?~ a b ?~ b a |- ^- (list ,@)
[i=(xrl -.a -.b) t=$(a +.a, b +.b)] ?~ a ~
(weld `(list ,@)`-.a $(a +.a))
:: ::
++ bls |= [a=@ b=(list ,@)] :: split to sublists ++ sbm
?> =((mod (lent b) a) 0) |= [r=@ b=(list ,@)] :: scryptBlockMix
|- ^- (list (list ,@)) ?> =((lent b) (mul 2 r))
?~ b ~ =+ [x=(snag (dec (mul 2 r)) b) c=0]
[i=(scag a `(list ,@)`b) t=$(b (slag a `(list ,@)`b))] =| [ya=(list ,@) yb=(list ,@)]
|- ^- (list ,@)
?~ b (flop (weld yb ya))
=. x (sal (mix x -.b) 8)
?~ (mod c 2)
$(c +(c), b +.b, ya [i=x t=ya])
$(c +(c), b +.b, yb [i=x t=yb])
:: ::
++ slb |= [a=(list (list ,@))] ++ srm
|- ^- (list ,@) |= [r=@ b=(list ,@) n=@] :: scryptROMix
?~ a ~ ?> ?& =((lent b) (mul 2 r))
(weld `(list ,@)`-.a $(a +.a)) =(n (bex (dec (xeb n))))
(lth n (bex (mul r 16)))
==
=+ [v=*(list (list ,@)) c=0]
=. v
|- ^- (list (list ,@))
=+ w=(sbm r b)
?: =(c n) (flop v)
$(c +(c), v [i=[b] t=v], b w)
=+ x=(sbm r (snag (dec n) v))
|- ^- (list ,@)
?: =(c n) x
=+ q=(snag (dec (mul r 2)) x)
=+ z=`(list ,@)`(snag (mod q n) v)
=+ ^= w |- ^- (list ,@)
?~ x ~ ?~ z ~
[i=(mix -.x -.z) t=$(x +.x, z +.z)]
$(x (sbm r w), c +(c))
:: ::
++ sbm |= [r=@ b=(list ,@)] :: scryptBlockMix ++ hmc
?> =((lent b) (mul 2 r)) |= [k=@ t=@] :: HMAC-SHA-256
=+ [x=(snag (dec (mul 2 r)) b) c=0] (hml k (met 3 k) t (met 3 t))
=| [ya=(list ,@) yb=(list ,@)]
|- ^- (list ,@)
?~ b (flop (weld yb ya))
=. x (sal (mix x -.b) 8)
?~ (mod c 2)
$(c +(c), b +.b, ya [i=x t=ya])
$(c +(c), b +.b, yb [i=x t=yb])
:: ::
++ srm |= [r=@ b=(list ,@) n=@] :: scryptROMix ++ hml
?> ?& |= [k=@ kl=@ t=@ tl=@] :: w/length
=((lent b) (mul 2 r)) => .(k (end 3 kl k), t (end 3 tl t))
=(n (bex (dec (xeb n)))) =+ b=64
(lth n (bex (mul r 16))) =. k ?. (gth kl b) k (shay kl k)
== =+ ^= q %+ shay (add b tl)
=| v=(list (list ,@)) (add (lsh 3 b t) (mix k (fil 3 b 0x36)))
=+ c=0 %+ shay (add b 32)
=. v (add (lsh 3 b q) (mix k (fil 3 b 0x5c)))
|- ^- (list (list ,@))
=+ w=(sbm r b)
?: =(c n) (flop v)
$(c +(c), v [i=[b] t=v], b w)
=+ x=(sbm r (snag (dec n) v))
|- ^- (list ,@)
?: =(c n) x
=+ q=(snag (dec (mul r 2)) x)
$(x (sbm r (xrl x (snag (mod q n) v))), c +(c))
::
++ hmc |= [k=@ t=@] :: HMAC-SHA-256
(hml k (met 3 k) t (met 3 t))
::
++ hml |= [k=@ kl=@ t=@ tl=@] :: w/length
=. k (end 3 kl k) =. t (end 3 tl t)
=+ b=64
=. k ?. (gth kl b) k (shay kl k)
=+ ^= q %+ shay (add b tl)
(add (lsh 3 b t) (mix k (fil 3 b 0x36)))
%+ shay (add b 32)
(add (lsh 3 b q) (mix k (fil 3 b 0x5c)))
:: ::
++ pbk ~/ %pbk :: PBKDF2-HMAC-SHA256 ++ pbk ~/ %pbk :: PBKDF2-HMAC-SHA256
|= [p=@ s=@ c=@ d=@] |= [p=@ s=@ c=@ d=@]
(pbl p (met 3 p) s (met 3 s) c d) (pbl p (met 3 p) s (met 3 s) c d)
:: ::
++ pbl ~/ %pbl :: w/length ++ pbl ~/ %pbl :: w/length
|= [p=@ pl=@ s=@ sl=@ c=@ d=@] |= [p=@ pl=@ s=@ sl=@ c=@ d=@]
=. p (end 3 pl p) =. s (end 3 sl s) => .(p (end 3 pl p), s (end 3 sl s))
=+ h=32 =+ h=32
?> ?& (lte d (bex 30)) :: max key length 1GB ?> ?& (lte d (bex 30)) :: max key length 1GB
(lte c (bex 28)) :: max iterations 2^28 (lte c (bex 28)) :: max iterations 2^28
!=(c 0) !=(c 0)
== ==
=+ ^= l ?~ (mod d h) =+ ^= l ?~ (mod d h)
(div d h) (div d h)
+((div d h)) +((div d h))
=+ r=(sub d (mul h (dec l))) =+ r=(sub d (mul h (dec l)))
=+ [t=0 j=1 k=1] =+ [t=0 j=1 k=1]
=. t |- ^- @ =. t |- ^- @
?: (gth j l) t ?: (gth j l) t
=+ u=(add s (lsh 3 sl (rep 3 (flop (rpp 3 4 j))))) =+ u=(add s (lsh 3 sl (rep 3 (flop (rpp 3 4 j)))))
=+ f=0 =. f |- ^- @ =+ f=0 =. f |- ^- @
?: (gth k c) f ?: (gth k c) f
=+ q=(hml p pl u ?:(=(k 1) (add sl 4) h)) =+ q=(hml p pl u ?:(=(k 1) (add sl 4) h))
$(u q, f (mix f q), k +(k)) $(u q, f (mix f q), k +(k))
$(t (add t (lsh 3 (mul (dec j) h) f)), j +(j)) $(t (add t (lsh 3 (mul (dec j) h) f)), j +(j))
(end 3 d t) (end 3 d t)
:: ::
++ hsh ~/ %hsh :: scrypt ++ hsh ~/ %hsh :: scrypt
|= [p=@ s=@ n=@ r=@ z=@ d=@] |= [p=@ s=@ n=@ r=@ z=@ d=@]
(hsl p (met 3 p) s (met 3 s) n r z d) (hsl p (met 3 p) s (met 3 s) n r z d)
:: ::
++ hsl ~/ %hsl :: w/length ++ hsl ~/ %hsl :: w/length
|= [p=@ pl=@ s=@ sl=@ n=@ r=@ z=@ d=@] |= [p=@ pl=@ s=@ sl=@ n=@ r=@ z=@ d=@]
=| v=(list (list ,@)) =| v=(list (list ,@))
=. p (end 3 pl p) =. s (end 3 sl s) => .(p (end 3 pl p), s (end 3 sl s))
=+ u=(mul (mul 128 r) z) =+ u=(mul (mul 128 r) z)
?> ?& =(n (bex (dec (xeb n)))) :: n is power of 2 ?> ?& =(n (bex (dec (xeb n)))) :: n is power of 2
!=(r 0) !=(z 0) !=(r 0) !=(z 0)
%+ lte :: max 1GB memory %+ lte :: max 1GB memory
(mul (mul 128 r) (dec (add n z))) (mul (mul 128 r) (dec (add n z)))
(bex 30) (bex 30)
(lth pl (bex 31)) (lth pl (bex 31))
(lth sl (bex 31)) (lth sl (bex 31))
== ==
=+ ^= b =+ %^ rpp 3 u =+ ^= b =+ (rpp 3 u (pbl p pl s sl 1 u))
(pbl p pl s sl 1 u) %+ turn (bls (mul 128 r) -)
%+ turn (bls (mul 128 r) -) |=(a=(list ,@) (rpp 9 (mul 2 r) (rep 3 a)))
|=(a=(list ,@) (rpp 9 (mul 2 r) (rep 3 a))) ?> =((lent b) z)
?> =((lent b) z) =+ ^= q
=+ ^= q =+ |- ?~ b (flop v)
=+ |- ?~ b (flop v) $(b +.b, v [i=(srm r -.b n) t=v])
$(b +.b, v [i=(srm r -.b n) t=v]) %+ turn `(list (list ,@))`-
%+ turn `(list (list ,@))`- |=(a=(list ,@) (rpp 3 (mul 128 r) (rep 9 a)))
|=(a=(list ,@) (rpp 3 (mul 128 r) (rep 9 a))) (pbl p pl (rep 3 (slb q)) u 1 d)
(pbl p pl (rep 3 (slb q)) u 1 d)
-- --
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: section 2eY, SHA-256 (move me) :: :: section 2eY, SHA-256 (move me) ::