sha1_extend :: HInt -> HArr (HExp Word8) -> Hardware (HIrr (HExp Word32))
sha1_extend blocks pad =
  do ipad :: HIrr (HExp Word8)  <- unsafeFreezeArr pad
     ex   :: HArr (HExp Word32) <- newArr (80 + blocks)
     -- truncate original block.
     for (0) (blocks-1) $ (\(b :: HExp Integer) -> do
       po <- shareM (b * 16)
       bo <- shareM (b * 80)
       for (0) (15) (\(i :: HExp Integer) ->
         setArr ex (b+i)
           (   (i2n $ ipad ! (po+(i*4)))
             + (i2n $ ipad ! (po+(i*4)+1)) `shiftL` (8  :: HExp Integer)
             + (i2n $ ipad ! (po+(i*4)+2)) `shiftL` (16 :: HExp Integer)
             + (i2n $ ipad ! (po+(i*4)+3)) `shiftL` (24 :: HExp Integer)
           )))
     -- extend block with new words.
     iex :: HIrr (HExp Word32) <- unsafeFreezeArr ex
     for (0) (blocks-1) (\(b :: HExp Integer) -> do
       bo <- shareM (b * 80)
       for (bo+16) (bo+79) (\(i :: HExp Integer) ->
         setArr ex i $ flip rotateL (1 :: HExp Word32)
           (       (iex ! (i-3))
             `xor` (iex ! (i-8))
             `xor` (iex ! (i-14))
             `xor` (iex ! (i-16))
           )))
     unsafeFreezeArr ex

-- Translate a 160-bit block into an array of 20 8-bits.
sha1_extend :: HInt -> HArr (HExp Word8) -> Hardware (HIrr (HExp Word32))
sha1_extend blocks pad =
  do ipad :: HIrr (HExp Word8)  <- unsafeFreezeArr pad
     ex   :: HArr (HExp Word32) <- newArr (80 + blocks)
     -- truncate original block.
     for (0) (blocks-1) $ (\(b :: HExp Integer) -> do
       po <- shareM (b * 16)
       bo <- shareM (b * 80)
       for (0) (15) (\(i :: HExp Integer) ->
         setArr ex (b+i)
           (   (i2n $ ipad ! (po+(i*4)))
             + (i2n $ ipad ! (po+(i*4)+1)) `shiftL` (8  :: HExp Integer)
             + (i2n $ ipad ! (po+(i*4)+2)) `shiftL` (16 :: HExp Integer)
             + (i2n $ ipad ! (po+(i*4)+3)) `shiftL` (24 :: HExp Integer)
           )))
     -- extend block with new words.
     iex :: HIrr (HExp Word32) <- unsafeFreezeArr ex
     for (0) (blocks-1) (\(b :: HExp Integer) -> do
       bo <- shareM (b * 80)
       for (bo+16) (bo+79) (\(i :: HExp Integer) ->
         setArr ex i $ flip rotateL (1 :: HExp Word32)
           (       (iex ! (i-3))
             `xor` (iex ! (i-8))
             `xor` (iex ! (i-14))
             `xor` (iex ! (i-16))
           )))
     unsafeFreezeArr ex

-- Translate a 160-bit block into an array of 20 8-bits.
sha1_extend blocks pad =
  do ipad :: HIrr (HExp Word8)  <- unsafeFreezeArr pad
     ex   :: HArr (HExp Word32) <- newArr (80 + blocks)
     -- truncate original block.
     for (0) (blocks-1) $ (\(b :: HExp Integer) -> do
       po <- shareM (b * 16)
       bo <- shareM (b * 80)
       for (0) (15) (\(i :: HExp Integer) ->
         setArr ex (b+i)
           (   (i2n $ ipad ! (po+(i*4)))
             + (i2n $ ipad ! (po+(i*4)+1)) `shiftL` (8  :: HExp Integer)
             + (i2n $ ipad ! (po+(i*4)+2)) `shiftL` (16 :: HExp Integer)
             + (i2n $ ipad ! (po+(i*4)+3)) `shiftL` (24 :: HExp Integer)
           )))
     -- extend block with new words.
     iex :: HIrr (HExp Word32) <- unsafeFreezeArr ex
     for (0) (blocks-1) (\(b :: HExp Integer) -> do
       bo <- shareM (b * 80)
       for (bo+16) (bo+79) (\(i :: HExp Integer) ->
         setArr ex i $ flip rotateL (1 :: HExp Word32)
           (       (iex ! (i-3))
             `xor` (iex ! (i-8))
             `xor` (iex ! (i-14))
             `xor` (iex ! (i-16))
           )))
     unsafeFreezeArr ex

-- Translate a 160-bit block into an array of 20 8-bits.