2025-03-13 19:43:54 +05:00
|
|
|
#pragma once
|
2025-03-13 04:09:27 +05:00
|
|
|
#include <string.h>
|
2025-03-13 19:43:54 +05:00
|
|
|
__device__ void raw_add(unsigned char* x, const unsigned char* p) {
|
|
|
|
unsigned short c = 0;
|
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
c += ((unsigned short)x[i]) + ((unsigned short)p[i]);
|
|
|
|
x[i] = (unsigned char)c;
|
2025-03-13 04:09:27 +05:00
|
|
|
c >>= 8;
|
|
|
|
}
|
|
|
|
}
|
2025-03-13 19:43:54 +05:00
|
|
|
__device__ void fprime_select(unsigned char* dst, const unsigned char* zero, const unsigned char* one, unsigned char condition) {
|
|
|
|
const unsigned char mask = -condition;
|
|
|
|
for (int i = 0; i < 32; i++)
|
2025-03-13 04:09:27 +05:00
|
|
|
dst[i] = zero[i] ^ (mask & (one[i] ^ zero[i]));
|
|
|
|
}
|
2025-03-13 19:43:54 +05:00
|
|
|
__device__ void raw_try_sub(unsigned char* x, const unsigned char* p) {
|
|
|
|
unsigned char minusp[32];
|
|
|
|
unsigned short c = 0;
|
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
c = ((unsigned short)x[i]) - ((unsigned short)p[i]) - c;
|
|
|
|
minusp[i] = (unsigned char)c;
|
2025-03-13 04:09:27 +05:00
|
|
|
c = (c >> 8) & 1;
|
|
|
|
}
|
|
|
|
fprime_select(x, minusp, x, c);
|
|
|
|
}
|
2025-03-13 19:43:54 +05:00
|
|
|
__device__ int prime_msb(const unsigned char* p) {
|
2025-03-13 04:09:27 +05:00
|
|
|
int i;
|
2025-03-13 19:43:54 +05:00
|
|
|
unsigned char x;
|
|
|
|
for (i = 32 - 1; i >= 0; i--) {
|
|
|
|
if (p[i]) break;
|
2025-03-13 04:09:27 +05:00
|
|
|
}
|
|
|
|
x = p[i];
|
|
|
|
i <<= 3;
|
|
|
|
while (x) {
|
|
|
|
x >>= 1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i - 1;
|
|
|
|
}
|
2025-03-13 19:43:54 +05:00
|
|
|
__device__ void shift_n_bits(unsigned char* x, int n) {
|
|
|
|
unsigned short c = 0;
|
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
c |= ((unsigned short)x[i]) << n;
|
|
|
|
x[i] = (unsigned char)c;
|
2025-03-13 04:09:27 +05:00
|
|
|
c >>= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__device__ inline int min_int(int a, int b) {
|
|
|
|
return a < b ? a : b;
|
|
|
|
}
|
2025-03-13 19:43:54 +05:00
|
|
|
__device__ void fprime_from_bytes(unsigned char* n, const unsigned char* x, unsigned long len, const unsigned char* modulus) {
|
2025-03-13 04:09:27 +05:00
|
|
|
const int preload_total = min_int(prime_msb(modulus) - 1, (int)(len << 3));
|
|
|
|
const int preload_bytes = preload_total >> 3;
|
|
|
|
const int preload_bits = preload_total & 7;
|
|
|
|
const int rbits = (len << 3) - preload_total;
|
2025-03-13 19:43:54 +05:00
|
|
|
memset(n, 0, 32);
|
2025-03-13 04:09:27 +05:00
|
|
|
for (int i = 0; i < preload_bytes; i++)
|
|
|
|
n[i] = x[len - preload_bytes + i];
|
|
|
|
if (preload_bits) {
|
|
|
|
shift_n_bits(n, preload_bits);
|
|
|
|
n[0] |= x[len - preload_bytes - 1] >> (8 - preload_bits);
|
|
|
|
}
|
|
|
|
for (int i = rbits - 1; i >= 0; i--) {
|
2025-03-13 19:43:54 +05:00
|
|
|
const unsigned char bit = (x[i >> 3] >> (i & 7)) & 1;
|
2025-03-13 04:09:27 +05:00
|
|
|
shift_n_bits(n, 1);
|
|
|
|
n[0] |= bit;
|
|
|
|
raw_try_sub(n, modulus);
|
|
|
|
}
|
|
|
|
}
|
2025-03-13 19:43:54 +05:00
|
|
|
__device__ void fprime_add(unsigned char* r, const unsigned char* a, const unsigned char* modulus) {
|
2025-03-13 04:09:27 +05:00
|
|
|
raw_add(r, a);
|
|
|
|
raw_try_sub(r, modulus);
|
|
|
|
}
|
2025-03-13 19:43:54 +05:00
|
|
|
__device__ inline void fprime_copy(unsigned char* x, const unsigned char* a) {
|
|
|
|
memcpy(x, a, 32);
|
2025-03-13 04:09:27 +05:00
|
|
|
}
|
2025-03-13 19:43:54 +05:00
|
|
|
__device__ void fprime_mul(unsigned char* r, const unsigned char* a, const unsigned char* b, const unsigned char* modulus) {
|
|
|
|
memset(r, 0, 32);
|
2025-03-13 04:09:27 +05:00
|
|
|
for (int i = prime_msb(modulus); i >= 0; i--) {
|
2025-03-13 19:43:54 +05:00
|
|
|
const unsigned char bit = (b[i >> 3] >> (i & 7)) & 1;
|
|
|
|
unsigned char plusa[32];
|
2025-03-13 04:09:27 +05:00
|
|
|
shift_n_bits(r, 1);
|
|
|
|
raw_try_sub(r, modulus);
|
|
|
|
fprime_copy(plusa, r);
|
|
|
|
fprime_add(plusa, a, modulus);
|
|
|
|
fprime_select(r, r, plusa, bit);
|
|
|
|
}
|
2025-03-13 19:43:54 +05:00
|
|
|
}
|