transformations.h 3.55 KB
Newer Older
lwc-tester committed
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/*
Implementation by Seth Hoffert, hereby denoted as "the implementer".

For more information, feedback or questions, please refer to our websites:
https://keccak.team/xoodoo.html

To the extent possible under law, the implementer has waived all copyright
and related or neighboring rights to the source code in this file.
http://creativecommons.org/publicdomain/zero/1.0/
*/

#ifndef _TRANSFORMATIONS_H_
#define _TRANSFORMATIONS_H_
#include <iostream>
#include <string>
#include "bitstring.h"
#include "types.h"

/**
  * Abstract class that represents a transformation from n bits to n bits.
  */
class Transformation {
public:
    Transformation() {};
    /**
      * Virtual destructor - necessary because this is an abstract class.
      */
    virtual ~Transformation() {};
    /**
      * Abstract method that returns the number of bits of its domain and range.
      */
    virtual unsigned int getWidth() const = 0;
    /**
      * Abstract method that applies the transformation onto the parameter
      * @a state.
      *
      * @param  state   A buffer on which to apply the transformation.
      *                 The state must have a size of at least
      *                 ceil(getWidth()/8.0) bytes.
      */
    virtual void operator()(UINT8 * state) const = 0;
    /**
      * Abstract method that returns a string with a description of itself.
      */
    virtual std::string getDescription() const = 0;
    /**
      * Method that prints a brief description of the transformation.
      */
    friend std::ostream& operator<<(std::ostream& a, const Transformation& transformation);
};

/**
  * Abstract class that represents a permutation from n bits to n bits.
  */
class Permutation : public Transformation {
public:
    Permutation() : Transformation() {};
    /**
      * Abstract method that applies the <em>inverse</em> of the permutation
      * onto the parameter @a state.
      *
      * @param  state   A buffer on which to apply the inverse permutation.
      *                 The state must have a size of at least
      *                 ceil(getWidth()/8.0) bytes.
      */
    virtual void inverse(UINT8 * state) const = 0;
};

/**
  * Class that implements the simplest possible permutation: the identity.
  */
class Identity : public Permutation {
protected:
    unsigned int width;
public:
    Identity(unsigned int aWidth) : Permutation(), width(aWidth) {};
    virtual unsigned int getWidth() const { return width; }
    virtual void operator()(UINT8 * state) const {(void)state;}
    virtual std::string getDescription() const { return "Identity"; }
    virtual void inverse(UINT8 * state) const {(void)state;}
};

/**
 * Class implementing an iterable permutation
 */
class BaseIterableTransformation
{
	public:
		const unsigned int            width;
		const unsigned int            rounds;

		BaseIterableTransformation(unsigned int width, unsigned int rounds) : width(width), rounds(rounds) {}

		virtual BitString operator()(const BitString &state) const = 0;
};

template<class T>
class IterableTransformation : public BaseIterableTransformation
{
	protected:
		T                     f;

	public:
		IterableTransformation(unsigned int width) : BaseIterableTransformation(width, 0), f(width) {}
		IterableTransformation(unsigned int width, unsigned int rounds) : BaseIterableTransformation(width, rounds), f(width, rounds) {}

		BitString operator()(const BitString &state) const
		{
			BitString state2 = state;
			f(state2.array());
			return state2;
		}
};

#endif