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

Reply via email to