Here is what I believe is a literal translation of mix:
lim=:2^32
xor=:22 b.
mix=:3 :0
a=:lim|a xor lim|b*2048
d=:lim|d+a
b=:(lim|b+c) xor <.c%4
e=:lim|e+b
c=:(lim|c+d) xor lim|d*256
f=:lim|f+c
d=:(lim|d+e) xor <.e%65536
g=:lim|g+d
e=:(lim|e+f) xor lim|f*1024
h=:lim|h+e
f=:(lim|f+g) xor <.g%16
a=:lim|a+f
g=:(lim|g+h) xor lim|h*256
b=:lim|b+g
h=:(lim|h+a) xor <.a%512
c=:lim|c+h
a=:lim|a+b
i.0 0
)
I guess maybe compare what yours does with my implementation and if
they differ we can talk through the example and see if we can agree on
what should be happening?
Here's a couple examples of my version in action:
]'a b c d e f g h'=: p:?8#1e8
1559724181 1819407257 657839053 1224641147 1110824551 1517135723
1813617683 465630923
mix''
x:a,b,c,d,e,f,g,h
1259998676 2283964259 2433348896 4178215340 2284046404 4018945500
3988247502 1723678275
]'a b c d e f g h'=: p:?8#1e8
169723387 722453911 1940704391 782367893 435164203 1618331417
785423467 254791547
mix''
x:a,b,c,d,e,f,g,h
382398673 269961084 3701615829 1058780093 12620698 2458004314
2378147389 380065214
Thanks,
--
Raul
On Fri, Sep 4, 2015 at 10:55 PM, Dan Bron <[email protected]> wrote:
> Periodically I review the list of open J tasks on RosettaCode [1]. Today I
> came across an obscure but (if you believe the task description) meritorious
> cipher, called ISAAC ("Indirection, Shift, Accumulate, Add, and Count”).
>
> I understand the basic concepts of cryptography, but am generally unversed in
> the practical details of implementation. In other words: at the moment, I’m
> blindly transliterating the C implementation (marked as canonical) to J,
> hewing as closely to the source material as possible, loops and all.
>
> Which, of course, results in some damn ugly J. Take, for example, this C
> preprocessor macro, which I’ve dutifully transliterated as in the post-script.
>
> #define mix(a,b,c,d,e,f,g,h) \
> { \
> a^=b<<11; d+=a; b+=c; \
> b^=c>>2; e+=b; c+=d; \
> c^=d<<8; f+=c; d+=e; \
> d^=e>>16; g+=d; e+=f; \
> e^=f<<10; h+=e; f+=g; \
> f^=g>>4; a+=f; g+=h; \
> g^=h<<8; b+=g; h+=a; \
> h^=a>>9; c+=h; a+=b; \
> }
>
> Can you write this function in idiomatic, perhaps even elegant, J? Extra
> brownie points for something along the lines of f/ a,b,c,d,e,f,g,h ,
>
> If it gives you a jumpstart, here is the pattern of variable names, as well
> as the bitshift direction and degree:
>
> abdabc < 11
> bcebcd > 2
> cdfcde < 8
> degdef > 16
> efhefg < 10
> fgafgh > 4
> ghbgha < 8
> hachab > 9
>
> Expressed as left-hand arguments to |.!.0, the bitshifts are 11 _2 8 _16 10
> _4 8 _9 .
>
> And the variable name matrix, expressed as indices into ‘abcdefgh’ is:
>
> 0 1 3 0 1 2
> 1 2 4 1 2 3
> 2 3 5 2 3 4
> 3 4 6 3 4 5
> 4 5 7 4 5 6
> 5 6 0 5 6 7
> 6 7 1 6 7 0
> 7 0 2 7 0 1
>
> Which pattern can be expressed as 0 1 3 0 1 2 (|."0 1) i.8 if you
> conceive of it column-wise, or 0 1 3 0 1 2 (8 | +/~) i.8 if row-wise.
> Either way, the salient structure appears to be 0 1 3 0 1 2 .
>
>
> -Dan
>
> [1] Tasks on RosettaCode with no J implementation:
> http://rosettacode.org/wiki/Reports:Tasks_not_implemented_in_J
> <http://rosettacode.org/wiki/Reports:Tasks_not_implemented_in_J>
>
> [2] The “wash shuffle” (which Wikipedia calls the “Corgi shuffle”) is that
> basic, no-frills move you see at the cheap tables in Atlantic City: the
> dealer simply spreads all the cards face down on the table and slides them
> all around and over each other. Not pretty, but effective, and less
> susceptible to sleight-of-hand cheating.
> https://en.wikipedia.org/wiki/Shuffling#Corgi_shuffle
> <https://en.wikipedia.org/wiki/Shuffling#Corgi_shuffle>
>
> PS: Here is a direct transliteration, but be WARNED: I haven’t gotten the
> overall ISAAC cypher to produce the correct outputs yet, so this is untested
> and may be incorrect.
>
>
> NB. ISAAC operates on 32-bit ints, meaning b. will break it on 64-bit systems
> bww =: (32#2)&#:
> hlfUnd =: 2 : 'v^:_1@:(u v)’
>
> bwXor =: ~:&.bww
> bwShift =: |.!.0 hlfUnd bww
>
> mix =: verb define
> 'a b c d e f g h'=.y
>
> NB. a^=b<<11; d+=a; b+=c;
> a =. a bwXor 11 bwShift b
> d =. d + a
> b =. b + c
>
> NB. b^=c>>2; e+=b; c+=d;
> b =. b bwXor _2 bwShift c
> e =. e + b
> c =. c + d
>
> NB. c^=d<<8; f+=c; d+=e;
> c =. c bwXor 8 bwShift d
> f =. f + c
> d =. d + e
>
> NB. d^=e>>16; g+=d; e+=f;
> d =. d bwXor _16 bwShift e
> g =. g + d
> e =. e + f
>
> NB. e^=f<<10; h+=e; f+=g;
> e =. e bwXor 10 bwShift f
> h =. h + e
> f =. f + g
>
> NB. f^=g>>4; a+=f; g+=h;
> f =. f bwXor _4 bwShift g
> a =. a + f
> g =. g + h
>
> NB. g^=h<<8; b+=g; h+=a;
> g =. g bwXor 8 bwShift h
> b =. b + g
> h =. h + a
>
> NB. h^=a>>9; c+=h; a+=b;
> h =. h bwXor _9 bwShift a
> c =. c + h
> a =. a + b
>
> ".&> ;: 'a b c d e f g h'
> )
>
>
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm