Skip to content

Instantly share code, notes, and snippets.

@itzmeanjan
Last active April 14, 2023 11:30
Show Gist options
  • Save itzmeanjan/504113021dec30a0909e5f5b47a5bde5 to your computer and use it in GitHub Desktop.
Save itzmeanjan/504113021dec30a0909e5f5b47a5bde5 to your computer and use it in GitHub Desktop.
Generate Known Answer Tests for Xoofff - Deck function instantiated with Xoodoo permutation
diff --git a/Reference/C++/Sources/Farfalle.cpp b/Reference/C++/Sources/Farfalle.cpp
index dc988fd..c1e3e99 100644
--- a/Reference/C++/Sources/Farfalle.cpp
+++ b/Reference/C++/Sources/Farfalle.cpp
@@ -57,15 +57,18 @@ BitString Farfalle::operator()(const BitString &K, const BitStrings &Mseq, unsig
unsigned int b = width();
if (!(K.size() <= b - 1)) throw Exception("Key length must be less than b bits");
unsigned int m = Mseq.size();
+ std::cout << "key: " << K << "\n";
BitString Kp = K || BitString::pad10(b, K.size());
BitString k = p_b(Kp);
BitString x = BitString::zeroes(b);
unsigned int I = 0;
+ std::cout << "msg: ";
for (unsigned int j = 0; j <= m - 1; j++)
{
+ std::cout << Mseq[j] << "\n";
unsigned int mu = (Mseq[j].size() + b) / b;
BitString M = Mseq[j] || BitString::pad10(mu * b, Mseq[j].size());
Blocks mblocks(M, b);
@@ -88,7 +91,9 @@ BitString Farfalle::operator()(const BitString &K, const BitStrings &Mseq, unsig
zblocks[j] = p_e(roll_e(y, j)) ^ kp;
}
+ std::cout << "q: " << std::dec << (q / 8) << "\n";
BitString Z = BitString::substring(zblocks.bits(), q, n);
+ std::cout << "out: " << Z << "\n\n";
return Z;
}
diff --git a/Reference/C++/Sources/Xoofff-test.cpp b/Reference/C++/Sources/Xoofff-test.cpp
index 24aec41..70f0f5d 100644
--- a/Reference/C++/Sources/Xoofff-test.cpp
+++ b/Reference/C++/Sources/Xoofff-test.cpp
@@ -54,7 +54,7 @@ uint8_t random8( void );
static void randomize( unsigned char* data, unsigned int length)
{
#if !defined(EMBEDDED)
- srand((unsigned int)time(0));
+ // srand((unsigned int)time(0));
#endif
while (length--)
{
@@ -76,6 +76,36 @@ static void generateSimpleRawMaterial(unsigned char* data, unsigned int length,
}
}
+void genXoofffKAT() {
+ srand((unsigned int)0);
+
+ const size_t MIN_KEY_LEN = 16;
+ const size_t MAX_KEY_LEN = XnP_widthInBytes - 1;
+
+ const size_t MIN_MSG_LEN = 0;
+ const size_t MAX_MSG_LEN = 119;
+
+ const size_t MIN_Q = 0;
+ const size_t MAX_Q = XnP_widthInBytes;
+
+ const size_t OUT_LEN = 32;
+
+ for(size_t klen = MIN_KEY_LEN; klen <= MAX_KEY_LEN; klen++) {
+ auto key = static_cast<UINT8*>(malloc(klen));
+ randomize(key, klen);
+
+ for(size_t mlen = MIN_MSG_LEN; mlen <= MAX_MSG_LEN; mlen++) {
+ auto msg = static_cast<UINT8*>(malloc(mlen));
+ randomize(msg, mlen);
+
+ for (size_t q = MIN_Q; q <= MAX_Q; q++) {
+ Xoofff xp;
+ xp(BitString(key, klen*8), BitString(msg, mlen*8), OUT_LEN*8, q*8);
+ }
+ }
+ }
+}
+
static void performTestXoofffOneInput(BitLength keyLen, BitLength inputLen, BitLength outputLen, int /*flags*/, Keccak &rSpongeChecksum, unsigned int mode)
{
BitSequence input[inputByteSize];
diff --git a/Reference/C++/Sources/Xoofff-test.h b/Reference/C++/Sources/Xoofff-test.h
index 5dd0c48..bafc6ec 100644
--- a/Reference/C++/Sources/Xoofff-test.h
+++ b/Reference/C++/Sources/Xoofff-test.h
@@ -13,5 +13,6 @@ http://creativecommons.org/publicdomain/zero/1.0/
#define _XOOFFFTEST_H_
void testXoofff(void);
+void genXoofffKAT();
#endif
diff --git a/Reference/C++/Sources/bitstring.cpp b/Reference/C++/Sources/bitstring.cpp
index 1d6dd21..af7c75a 100644
--- a/Reference/C++/Sources/bitstring.cpp
+++ b/Reference/C++/Sources/bitstring.cpp
@@ -292,12 +292,12 @@ std::ostream &operator<<(std::ostream &os, const BitString &S)
os.fill('0');
os << std::hex << ((int)UINT8(*i));
++i;
- if ( i != S.v.end()) {
- os << " ";
- }
- else {
- os << "(" << ((S.vSize - 1) % 8 + 1) << ")";
- }
+ // if ( i != S.v.end()) {
+ // os << " ";
+ // }
+ // else {
+ // os << "(" << ((S.vSize - 1) % 8 + 1) << ")";
+ // }
}
return os;
}
diff --git a/Reference/C++/Sources/main.cpp b/Reference/C++/Sources/main.cpp
index 82fcda0..37a1475 100644
--- a/Reference/C++/Sources/main.cpp
+++ b/Reference/C++/Sources/main.cpp
@@ -49,9 +49,10 @@ int main(int argc, char *argv[])
try
{
//testXoodoo(384, std::cout);
- testXoofff();
- testXooModes();
- testXoodyak();
+ genXoofffKAT();
+ // testXoofff();
+ // testXooModes();
+ // testXoodyak();
std::cout << std::flush;
}
diff --git a/Reference/C++/makefile b/Reference/C++/makefile
index 183687c..7c1d5b4 100644
--- a/Reference/C++/makefile
+++ b/Reference/C++/makefile
@@ -9,7 +9,7 @@ $(BINDIR):
OBJECTS = $(addprefix $(BINDIR)/, $(notdir $(patsubst %.cpp,%.o,$(SOURCES))))
-CFLAGS = -O3 -g0 -Wreorder
+CFLAGS = -O3 -std=c++14 -g0 -Wreorder
VPATH = Sources

This gist holds steps for generating Known Answer Tests from Xoofff's reference C++ implementation ( more @ https://github.com/KeccakTeam/Xoodoo.git ), by applying following git patch s.t. these KATs can be used for ensuring functional correctness of my Rust library implementation of Xoofff ( more @ https://github.com/itzmeanjan/xoofff ).

  • Clone repository holding reference implementation of Xoofff
git clone https://github.com/KeccakTeam/Xoodoo.git
  • Pin repository state to specific commit.
git checkout 37de9b9b4e5efebaa57f34cc66b8be4f41f13efe
  • Ensure you've a C++ compiler with C++14 standard library support.
  • Clone/ download ( and unzip ) this gist.
  • Copy git patch file gen_xoofff_kat.patch into Xoodoo.
  • Issue following command for applying the git patch.
git apply gen_xoofff_kat.patch
git status # see files changed
git diff   # see changes
  • Compile and execute program for generating KATs.
pushd Reference/C++/

make                                       # compile
./bin/XoodooReference | tee Xoofff_KAT.txt # generate KATs

popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment