Skip to content

Commit

Permalink
More tests for fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastinas committed Apr 11, 2023
1 parent 319e069 commit 738e9ce
Show file tree
Hide file tree
Showing 4 changed files with 366 additions and 65 deletions.
51 changes: 47 additions & 4 deletions fields.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@

#include "fields.h"

static const uint8_t bf8_modulus = (1 << 4) | (1 << 3) | (1 << 1) | 1;
static const uint64_t bf64_modulus = (1 << 4) | (1 << 3) | (1 << 1) | 1;
// GF(2^8) with X^8 + X^4 + X^3 + X^1 + 1
static const uint8_t bf8_modulus = (1 << 4) | (1 << 3) | (1 << 1) | 1;
// GF(2^64) with X^64 + X^4 + X^3 + X^1 + 1
static const uint64_t bf64_modulus = (1 << 4) | (1 << 3) | (1 << 1) | 1;
// GF(2^128) with X^128 + X^7 + X^2 + X^1 + 1
// static const uint64_t bf128_modulus = (1 << 7) | (1 << 2) | (1 << 1) | 1;
// GF(2^192) with X^192 + X^7 + X^2 + X^1 + 1
// static const uint64_t bf192_modulus = (1 << 7) | (1 << 2) | (1 << 1) | 1;
// GF(2^256) with X^256 + X^10 + X^5 + X^2 + 1
// static const uint64_t bf256_modulus = (1 << 10) | (1 << 5) | (1 << 2) | 1;

// GF(2^8) implementation

bf8_t bf8_add(bf8_t lhs, bf8_t rhs) {
return lhs ^ rhs;
Expand All @@ -24,11 +30,13 @@ bf8_t bf8_mul(bf8_t lhs, bf8_t rhs) {
for (unsigned int idx = 8; idx; --idx, rhs >>= 1) {
result ^= (-(rhs & 1)) & lhs;
const uint8_t mask = -((lhs >> 7) & 1);
lhs = (lhs << 1) ^ (mask & bf8_modulus);
lhs = (lhs << 1) ^ (mask & bf8_modulus);
}
return result;
}

// GF(2^64) implementation

bf64_t bf64_add(bf64_t lhs, bf64_t rhs) {
return lhs ^ rhs;
}
Expand All @@ -38,7 +46,42 @@ bf64_t bf64_mul(bf64_t lhs, bf64_t rhs) {
for (unsigned int idx = 64; idx; --idx, rhs >>= 1) {
result ^= (-(rhs & 1)) & lhs;
const uint64_t mask = -((lhs >> 63) & 1);
lhs = (lhs << 1) ^ (mask & bf64_modulus);
lhs = (lhs << 1) ^ (mask & bf64_modulus);
}
return result;
}

// GF(2^128) implementation

bf128_t bf128_add(bf128_t lhs, bf128_t rhs) {
for (unsigned int i = 0; i != ARRAY_SIZE(lhs.values); ++i) {
lhs.values[i] ^= rhs.values[i];
}
return lhs;
}

bf128_t bf128_mul(bf128_t lhs, bf128_t rhs) {
return lhs;
}

bf192_t bf192_add(bf192_t lhs, bf192_t rhs) {
for (unsigned int i = 0; i != ARRAY_SIZE(lhs.values); ++i) {
lhs.values[i] ^= rhs.values[i];
}
return lhs;
}

bf192_t bf192_mul(bf192_t lhs, bf192_t rhs) {
return lhs;
}

bf256_t bf256_add(bf256_t lhs, bf256_t rhs) {
for (unsigned int i = 0; i != ARRAY_SIZE(lhs.values); ++i) {
lhs.values[i] ^= rhs.values[i];
}
return lhs;
}

bf256_t bf256_mul(bf256_t lhs, bf256_t rhs) {
return lhs;
}
9 changes: 9 additions & 0 deletions fields.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ bf8_t bf8_mul(bf8_t lhs, bf8_t rhs);
bf64_t bf64_add(bf64_t lhs, bf64_t rhs);
bf64_t bf64_mul(bf64_t lhs, bf64_t rhs);

bf128_t bf128_add(bf128_t lhs, bf128_t rhs);
bf128_t bf128_mul(bf128_t lhs, bf128_t rhs);

bf192_t bf192_add(bf192_t lhs, bf192_t rhs);
bf192_t bf192_mul(bf192_t lhs, bf192_t rhs);

bf256_t bf256_add(bf256_t lhs, bf256_t rhs);
bf256_t bf256_mul(bf256_t lhs, bf256_t rhs);

FAEST_END_C_DECL

#endif
3 changes: 3 additions & 0 deletions macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,7 @@
#define faest_declassify(x, len)
#endif

/* number of elements in an array */
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

#endif
Loading

0 comments on commit 738e9ce

Please sign in to comment.