longchute

about

Identity Sponge Construction

15 Nov 2013

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 bitstring.py (located at /usr/local/lib/python3.3/dist-packages/bitstring.py on my system) must be changed to specify an encoding. UTF-8 is chosen for ASCII compatibility:

Original bitstring.py for Python 3.3.2

data = bytearray(data) 

Patched bitstring.py for Python 3.3.2

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


Usage: ./sponge.py [input data ...]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/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),
                           BitArray(length=capacity)]
        self.input_data = BitArray()

    def pad(self):
        self.input_data.append(BitArray('0b1'))
        data_size   = len(self.input_data) + 1
        padding     = abs(data_size % -self.block_size)
        if padding >  1:
            self.input_data.append(BitArray(length=padding))
        self.input_data.append(BitArray('0b1'))

        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):
        pass          

    def absorb(self, input_data):
        self.input_data.append(input_data)
        self.pad()

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

            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]
            self.permute()

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)
    a_sponge.absorb(input_data)

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