Identity Sponge Construction

This code implements a sponge construction with an identity permutation, multi-rate padding, and a block_size or bitrate of 6. The capacity is arbitrarily set to 2, although it does not influence the output of a sponge construction with an identity permutation.

Sponge construction Sponge Construction by Chris Wilson is licensed under CC BY-NC 2.0.

The code has been tested with python-bitstring 3.12 on Python 2.7.1 and Python 2.7.5. No modifications are necessary for these combinations.

It also works on Python 3.3.2 with python-bitstring r982, with one minor patch. Line 1311 of (located at /usr/local/lib/python3.3/dist-packages/ on my system) must be changed to specify an encoding. UTF-8 is chosen for ASCII compatibility:

Original for Python 3.3.2

data = bytearray(data)

Patched for Python 3.3.2

data = bytearray(data, encoding="utf-8")

Usage: ./ [input data ...]

#!/usr/bin/env python

import sys
from bitstring import BitArray

is_debug = True

class Sponge(object):
    def __init__(self, block_size, capacity):
        self.block_size = block_size
        self.capacity   = capacity
        self.state      = [BitArray(length=block_size),
        self.input_data = BitArray()

    def pad(self):
        data_size   = len(self.input_data) + 1
        padding     = abs(data_size % -self.block_size)
        if padding >  1:

        if is_debug: 
            print("pad() data_size: %d" % (data_size - 2))
            print("padding: %d" % (padding + 2))
            print("pad()'d data: %s" % self.input_data.bin)

    def chunk(self):
        if is_debug:
            print("chunk() data_size: %d\n" % len(self.input_data))
            current_block = 0

        while True:
            if not self.input_data: return

            if is_debug: 
                print("current_block: %d" % current_block)
                current_block += 1

            yield self.input_data[:self.block_size]
            del self.input_data[:self.block_size]

    def permute(self):

    def absorb(self, input_data):

        for chunk in self.chunk():
            self.state[0] = self.state[0] ^ chunk

            if is_debug:
                print("chunk: %s" % chunk.bin)
                print("state: %s\n" % self.state[0].bin)

    def squeeze(self):
        while True:
            yield self.state[0]

if (__name__ == '__main__'):
    raw_input_data  = ' '.join(sys.argv[1:])
    input_data      = BitArray(bytes=raw_input_data)
    a_sponge        = Sponge(6, 2)

    print("input: %s" % raw_input_data)
    print("input (bin): %s" % input_data.bin)

    for (current_squeeze, a_squeeze) in enumerate(a_sponge.squeeze()):
        print("squeeze %d: %s" % (current_squeeze, a_squeeze.bin))

        if current_squeeze == 10: break