Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions testvectors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This folder contains the test vectors for the primitives.

To call the test-vectors: go to the directory containing the primitive and run ```make```.
14 changes: 14 additions & 0 deletions testvectors/sparkle/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CFLAGS += -I . -march=native -mtune=native -O3

all: sparkle-test.exe

sparkle-test.exe: sparkle_vectors.h sparkle-test.c
$(CC) $(CFLAGS) $^ -o $@
./$@

.PHONY : clean
clean:
rm -rf *.o *.exe

include $(patsubst %.c,%.d,$(SOURCES))

52 changes: 52 additions & 0 deletions testvectors/sparkle/sparkle-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "sparkle_vectors.h"
#include "sparkle.c"


static bool compare_and_print (size_t len, uint32_t* comp, uint32_t* exp) {
bool ok = true;
for (size_t i = 0; i < len; i++)
ok = ok & (exp[i] == comp[i]);
if (!ok) {
printf("***FAILURE***\n");
printf("computed:");
for (size_t i = 0; i < len; i++)
printf("0x%0x, " , comp[i]);
printf("\n");

printf("expected:");
for (size_t i = 0; i < len; i++)
printf("0x%0x, " , exp[i]);
printf("\n");
}
return ok;
}


static bool sparkle_wrapper(sparkle_tv tv) {
uint32_t* temp = malloc (sizeof (uint32_t) * tv.input_len);
for (size_t i = 0; i < tv.input_len; i++)
{
temp[i] = tv.input[i];
}

sparkle(temp, tv.brans, tv.steps);
return compare_and_print(tv.input_len, temp, tv.output);
}


int main()
{
bool ok = true;
for (int i = 0; i < sizeof(vectors)/sizeof(sparkle_tv); ++i) {
ok &= sparkle_wrapper(vectors[i]);
}
if (ok)
printf("The testing executed successfully! \n");
return 0;

}
175 changes: 175 additions & 0 deletions testvectors/sparkle/sparkle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
///////////////////////////////////////////////////////////////////////////////
// sparkle.c: Optimized C implementation of the SPARKLE permutation family. //
// This file is part of the SPARKLE package that was sent to NIST during the //
// 3rd round of the Lightweight Cryptography (LWC) standardization project. //
// Version 1.2.1 (18-Oct-21), see <http://github.com/cryptolu/> for updates. //
// Authors: The SPARKLE Group (Christof Beierle, Alex Biryukov, Luan Cardoso //
// dos Santos, Johann Groszschaedl, Amir Moradi, Leo Perrin, Aein Rezaei //
// Shahmirzadi, Aleksei Udovenko, Vesselin Velichkov, and Qingju Wang). //
// License: GPLv3 (see LICENSE file), other licenses available upon request. //
// Copyright (C) 2019-2021 University of Luxembourg <http://www.uni.lu/>. //
// ------------------------------------------------------------------------- //
// This program is free software: you can redistribute it and/or modify it //
// under the terms of the GNU General Public License as published by the //
// Free Software Foundation, either version 3 of the License, or (at your //
// option) any later version. This program is distributed in the hope that //
// it will be useful, but WITHOUT ANY WARRANTY; without even the implied //
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. You should have received a //
// copy of the GNU General Public License along with this program. If not, //
// see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////


#include <stdio.h>
#include "sparkle.h"


#define ROT(x, n) (((x) >> (n)) | ((x) << (32-(n))))
#define ELL(x) (ROT(((x) ^ ((x) << 16)), 16))


// Round constants
static const uint32_t RCON[MAX_BRANCHES] = { \
0xB7E15162, 0xBF715880, 0x38B4DA56, 0x324E7738, \
0xBB1185EB, 0x4F7C7B57, 0xCFBFA1C8, 0xC2B3293D \
};


void sparkle(uint32_t *state, int brans, int steps)
{
int i, j; // Step and branch counter
uint32_t rc, tmpx, tmpy, x0, y0;

for(i = 0; i < steps; i ++) {
// Add round constant
state[1] ^= RCON[i%MAX_BRANCHES];
state[3] ^= i;
// ARXBOX layer
for(j = 0; j < 2*brans; j += 2) {
rc = RCON[j>>1];
state[j] += ROT(state[j+1], 31);
state[j+1] ^= ROT(state[j], 24);
state[j] ^= rc;
state[j] += ROT(state[j+1], 17);
state[j+1] ^= ROT(state[j], 17);
state[j] ^= rc;
state[j] += state[j+1];
state[j+1] ^= ROT(state[j], 31);
state[j] ^= rc;
state[j] += ROT(state[j+1], 24);
state[j+1] ^= ROT(state[j], 16);
state[j] ^= rc;
}
// Linear layer
tmpx = x0 = state[0];
tmpy = y0 = state[1];
for(j = 2; j < brans; j += 2) {
tmpx ^= state[j];
tmpy ^= state[j+1];
}
tmpx = ELL(tmpx);
tmpy = ELL(tmpy);
for (j = 2; j < brans; j += 2) {
state[j-2] = state[j+brans] ^ state[j] ^ tmpy;
state[j+brans] = state[j];
state[j-1] = state[j+brans+1] ^ state[j+1] ^ tmpx;
state[j+brans+1] = state[j+1];
}
state[brans-2] = state[brans] ^ x0 ^ tmpy;
state[brans] = x0;
state[brans-1] = state[brans+1] ^ y0 ^ tmpx;
state[brans+1] = y0;
}
}


void sparkle_inv(uint32_t *state, int brans, int steps)
{
int i, j; // Step and branch counter
uint32_t rc, tmpx, tmpy, xb1, yb1;

for(i = steps-1; i >= 0; i --) {
// Linear layer
tmpx = tmpy = 0;
xb1 = state[brans-2];
yb1 = state[brans-1];
for (j = brans-2; j > 0; j -= 2) {
tmpx ^= (state[j] = state[j+brans]);
state[j+brans] = state[j-2];
tmpy ^= (state[j+1] = state[j+brans+1]);
state[j+brans+1] = state[j-1];
}
tmpx ^= (state[0] = state[brans]);
state[brans] = xb1;
tmpy ^= (state[1] = state[brans+1]);
state[brans+1] = yb1;
tmpx = ELL(tmpx);
tmpy = ELL(tmpy);
for(j = brans-2; j >= 0; j -= 2) {
state[j+brans] ^= (tmpy ^ state[j]);
state[j+brans+1] ^= (tmpx ^ state[j+1]);
}
// ARXBOX layer
for(j = 0; j < 2*brans; j += 2) {
rc = RCON[j>>1];
state[j] ^= rc;
state[j+1] ^= ROT(state[j], 16);
state[j] -= ROT(state[j+1], 24);
state[j] ^= rc;
state[j+1] ^= ROT(state[j], 31);
state[j] -= state[j+1];
state[j] ^= rc;
state[j+1] ^= ROT(state[j], 17);
state[j] -= ROT(state[j+1], 17);
state[j] ^= rc;
state[j+1] ^= ROT(state[j], 24);
state[j] -= ROT(state[j+1], 31);
}
// Add round constant
state[1] ^= RCON[i%MAX_BRANCHES];
state[3] ^= i;
}
}


void clear_state(uint32_t *state, int brans)
{
int i;

for (i = 0; i < 2*brans; i ++) {
state[i] = 0;
}
}


void print_state(const uint32_t *state, int brans)
{
uint8_t *sbytes = (uint8_t *) state;
int i, j;

for (i = 0; i < brans; i ++) {
j = 8*i;
printf("(%02x%02x%02x%02x %02x%02x%02x%02x)", \
sbytes[j], sbytes[j+1], sbytes[j+2], sbytes[j+3], \
sbytes[j+4], sbytes[j+5], sbytes[j+6], sbytes[j+7]);
if (i < brans-1) printf(" ");
}
printf("\n");
}


void test_sparkle(int brans, int steps)
{
uint32_t state[2*MAX_BRANCHES] = { 0 };

printf("input:\n");
print_state(state, brans);
sparkle(state, brans, steps);
printf("sparkle:\n");
print_state(state, brans);
sparkle_inv(state, brans, steps);
printf("sparkle inv:\n");
print_state(state, brans);
printf("\n");
}
37 changes: 37 additions & 0 deletions testvectors/sparkle/sparkle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
///////////////////////////////////////////////////////////////////////////////
// sparkle.h: Optimized C implementation of the SPARKLE permutation family. //
// This file is part of the SPARKLE package that was sent to NIST during the //
// 3rd round of the Lightweight Cryptography (LWC) standardization project. //
// Version 1.2.1 (18-Oct-21), see <http://github.com/cryptolu/> for updates. //
// Authors: The SPARKLE Group (Christof Beierle, Alex Biryukov, Luan Cardoso //
// dos Santos, Johann Groszschaedl, Amir Moradi, Leo Perrin, Aein Rezaei //
// Shahmirzadi, Aleksei Udovenko, Vesselin Velichkov, and Qingju Wang). //
// License: GPLv3 (see LICENSE file), other licenses available upon request. //
// Copyright (C) 2019-2021 University of Luxembourg <http://www.uni.lu/>. //
// ------------------------------------------------------------------------- //
// This program is free software: you can redistribute it and/or modify it //
// under the terms of the GNU General Public License as published by the //
// Free Software Foundation, either version 3 of the License, or (at your //
// option) any later version. This program is distributed in the hope that //
// it will be useful, but WITHOUT ANY WARRANTY; without even the implied //
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. You should have received a //
// copy of the GNU General Public License along with this program. If not, //
// see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////

#ifndef SPARKLE_H
#define SPARKLE_H

#include <stdint.h>

#define MAX_BRANCHES 8

// function prototypes
void sparkle(uint32_t *state, int brans, int steps);
void sparkle_inv(uint32_t *state, int brans, int steps);
void clear_state(uint32_t *state, int brans);
void print_state(const uint32_t *state, int brans);
void test_sparkle(int brans, int steps);

#endif // SPARKLE_H
Loading