# Path: crysp/utils/operators.py
# def ror(x,n):
#     return (x>>n | x<<(x.size-n))
# 
# def rol(x,n):
#     return (x<<n | x>>(x.size-n))
# 
# def concat(L,bigend=False):
#     if len(L)==1: return L[0]
#     if bigend: L = reversed(L)
#     return reduce(lambda x,y: x//y, L)


from crysp.bits import *
from crysp.utils.operators import ror,rol,concat
# This code is part of crysp
# Copyright (C) 2009 Axel Tillequin (bdcht3@gmail.com)
# published under GPLv2 license


# -----------------------------------------------------------------------------
# Serpent-1 block cipher primitive
class Serpent(object):
    size = 128
    blocksize = 128

    def __init__(self,K):
        self.K = Bits(K,bitorder=1)
        if len(self.K)<256:
            self.K = self.K//Bits(1,1)
        self.K.size = 256
        # key schelule:
        prekey = []
        phi = Bits(0x9e3779b9,32)
        for p in range(0,256,32):
            prekey.append(self.K[p:p+32])
        for i in range(132):

# Path: crysp/utils/operators.py
# def ror(x,n):
#     return (x>>n | x<<(x.size-n))
# 
# def rol(x,n):
#     return (x<<n | x>>(x.size-n))
# 
# def concat(L,bigend=False):
#     if len(L)==1: return L[0]
#     if bigend: L = reversed(L)
#     return reduce(lambda x,y: x//y, L)


# Path: crysp/serpent.py
from crysp.bits import *
from crysp.utils.operators import ror,rol,concat

# This code is part of crysp
# Copyright (C) 2009 Axel Tillequin (bdcht3@gmail.com)
# published under GPLv2 license


# -----------------------------------------------------------------------------
# Serpent-1 block cipher primitive
class Serpent(object):
    size = 128
    blocksize = 128

    def __init__(self,K):
        self.K = Bits(K,bitorder=1)
        if len(self.K)<256:
            self.K = self.K//Bits(1,1)
        self.K.size = 256
        # key schelule:
        prekey = []
        phi = Bits(0x9e3779b9,32)
        for p in range(0,256,32):
            prekey.append(self.K[p:p+32])
        for i in range(132):

            wi = rol((prekey[-8]^prekey[-5]^prekey[-3]^prekey[-1]^phi^i),11)