Xoodoo.cpp 5.2 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
/*
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/
*/

#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <vector>

#include "Xoodoo.h"

static const int RowSize = 4;
static const int ColSize = 3;
static const int LaneSize = 32;

static void reduceX(int &x)
{
	x = ((x % RowSize) + RowSize) % RowSize;
}

static void reduceZ(int &z)
{
	z = ((z % LaneSize) + LaneSize) % LaneSize;
}

Lane shiftLane(const Lane &a, int dz)
{
	reduceZ(dz);
	return a << dz;
}

Lane cyclicShiftLane(const Lane &a, int dz)
{
	reduceZ(dz);

	if (dz == 0)
	{
		return a;
	}
	else
	{
		return ((a << dz) | (a >> (LaneSize - dz)));
	}
}

XoodooPlane::XoodooPlane() : lanes(RowSize)
{
}

XoodooPlane::XoodooPlane(const Lane *state)
{
	lanes.assign(state, state + RowSize);
}

const Lane &XoodooPlane::operator[](unsigned int i) const
{
	return lanes[i];
}

Lane &XoodooPlane::operator[](unsigned int i)
{
	return lanes[i];
}

void XoodooPlane::write(Lane *state) const
{
	std::copy(lanes.begin(), lanes.end(), state);
}

XoodooPlane cyclicShiftPlane(const XoodooPlane &A, int dx, int dz)
{
	XoodooPlane p;

	for (unsigned int i = 0; i < RowSize; i++)
	{
		int index = i - dx;
		reduceX(index);
		p[i] = cyclicShiftLane(A[index], dz);
	}

	return p;
}

XoodooPlane operator^(const XoodooPlane &lhs, const XoodooPlane &rhs)
{
	XoodooPlane p;

	for (unsigned int i = 0; i < RowSize; i++)
	{
		p[i] = lhs[i] ^ rhs[i];
	}

	return p;
}

XoodooPlane operator&(const XoodooPlane &lhs, const XoodooPlane &rhs)
{
	XoodooPlane p;

	for (unsigned int i = 0; i < RowSize; i++)
	{
		p[i] = lhs[i] & rhs[i];
	}

	return p;
}

XoodooPlane operator~(const XoodooPlane &A)
{
	XoodooPlane p;

	for (unsigned int i = 0; i < RowSize; i++)
	{
		p[i] = ~A[i];
	}

	return p;
}

XoodooState::XoodooState() : planes(ColSize)
{
}

XoodooState::XoodooState(const UINT8 *state)
{
	const Lane *l = reinterpret_cast<const Lane *>(state);

	for (unsigned int i = 0; i < ColSize; i++)
	{
		planes.push_back(XoodooPlane(l + i * RowSize));
	}
}

XoodooPlane &XoodooState::operator[](unsigned int y)
{
	return planes[y];
}

void XoodooState::write(UINT8 *state) const
{
	Lane *l = reinterpret_cast<Lane *>(state);

	for (unsigned int i = 0; i < ColSize; i++)
	{
		planes[i].write(l + i * RowSize);
	}
}

void XoodooState::dump(std::ostream &os) const
{
	std::ios::fmtflags f(os.flags());

	os << std::hex << std::noshowbase << std::setfill('0');

	for (unsigned int y = 0; y < ColSize; y++)
	{
		for (unsigned int x = 0; x < RowSize; x++)
		{
			os << "0x" << std::setw(8) << planes[y][x] << ' ';
		}
	}

	os << '\n';
	os.flags(f);
}

void Xoodoo::calculateRoundConstants()
{
	Lane s = 1;
	Lane p = 1;

	for (unsigned int i = 0; i < 6; i++)
	{
		rc_s.push_back(s);
		s = (s * 5) % 7;
	}

	for (unsigned int i = 0; i < 7; i++)
	{
		rc_p.push_back(p);
		p = p ^ (p << 2);
		if ((p & 16) != 0) p ^= 22;
		if ((p &  8) != 0) p ^= 11;
	}
}

void Xoodoo::stepTheta(XoodooState &A) const
{
	XoodooPlane P = A[0] ^ A[1] ^ A[2];
	XoodooPlane E = cyclicShiftPlane(P, 1, 5) ^ cyclicShiftPlane(P, 1, 14);

	for (unsigned int i = 0; i < ColSize; i++)
	{
		A[i] = A[i] ^ E;
	}
}

void Xoodoo::stepRhoWest(XoodooState &A) const
{
	A[1] = cyclicShiftPlane(A[1], 1, 0);
	A[2] = cyclicShiftPlane(A[2], 0, 11);
}

void Xoodoo::stepIota(XoodooState &A, int i) const
{
	Lane rc = (rc_p[-i % 7] ^ 8) << rc_s[-i % 6];
	A[0][0] = A[0][0] ^ rc;
}

void Xoodoo::stepChi(XoodooState &A) const
{
	XoodooState B;
	B[0] = ~A[1] & A[2];
	B[1] = ~A[2] & A[0];
	B[2] = ~A[0] & A[1];

	for (unsigned int i = 0; i < ColSize; i++)
	{
		A[i] = A[i] ^ B[i];
	}
}

void Xoodoo::stepRhoEast(XoodooState &A) const
{
	A[1] = cyclicShiftPlane(A[1], 0, 1);
	A[2] = cyclicShiftPlane(A[2], 2, 8);
}

void Xoodoo::round(XoodooState &A, int i) const
{
	stepTheta(A);
	stepRhoWest(A);
	stepIota(A, i);
	stepChi(A);
	stepRhoEast(A);
}

void Xoodoo::permute(XoodooState &A) const
{
	for (int i = 1 - rounds; i <= 0; i++)
	{
		round(A, i);

		if (logType == LOG_ROUND)
		{
			std::ios::fmtflags f(log->flags());
			*log << "(Round " << std::setfill('0') << std::setw(2) << (i + rounds) << ") ";
			log->flags(f);
			A.dump(*log);
		}
	}

	if (logType == LOG_FINAL)
	{
		A.dump(*log);
	}
}

Xoodoo::Xoodoo(unsigned int width, unsigned int vrounds)
	: rounds(vrounds), logType(LOG_NONE), log(NULL)
{
	if (width != 384) throw Exception("Unsupported width");
	if (rounds > 42) throw Exception("Unsupported number of rounds");

	calculateRoundConstants();
}

void Xoodoo::operator()(UINT8 *state) const
{
	XoodooState A(state);
	permute(A);
	A.write(state);
}

void Xoodoo::unsetLog()
{
	logType = LOG_NONE;
	log = NULL;
}

void Xoodoo::setLog(XoodooLog type, std::ostream &os)
{
	logType = type;
	log = &os;
}