Skip to content

Instantly share code, notes, and snippets.

@sordina
Last active August 29, 2015 14:06
Show Gist options
  • Save sordina/eeaf09417ca5c430a4cf to your computer and use it in GitHub Desktop.
Save sordina/eeaf09417ca5c430a4cf to your computer and use it in GitHub Desktop.
Adding xorish operations to Data.Bits.Lens
-- http://en.wikipedia.org/wiki/Rijndael_S-box
affineX' :: Word8 -> Word8 -> Word8
affineX' c b = b &~ forM_ [0..7] twiddleBits
where
twiddleBits :: Int -> StateT Word8 Identity ()
twiddleBits j = do
bitAt j .= f b 4
bitAt j .^|.= f b 5
bitAt j .^|.= f b 6
bitAt j .^|.= f b 7
bitAt j .^|.= f c 0
where
f n o = n ^. bitAt (mod (o + j) 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment