optimized

This commit is contained in:
rcxpony 2025-03-15 04:42:31 +05:00
parent 616f28f4c8
commit 1702531336
11 changed files with 122 additions and 183 deletions

View File

@ -12,30 +12,24 @@ __device__ __constant__ struct ed25519_pt ed25519_base = {
__device__ __constant__ struct ed25519_pt ed25519_neutral = {
{0}, {1,0}, {0}, {1,0}
};
__device__ __constant__ unsigned char ed25519_d[F25519_SIZE] = {
__device__ __constant__ unsigned char ed25519_d[32] = {
0xa3,0x78,0x59,0x13,0xca,0x4d,0xeb,0x75,0xab,0xd8,0x41,0x41,0x4d,0x0a,0x70,0x00,
0x98,0xe8,0x79,0x77,0x79,0x40,0xc7,0x8c,0x73,0xfe,0x6f,0x2b,0xee,0x6c,0x03,0x52
};
__device__ __constant__ unsigned char ed25519_k[F25519_SIZE] = {
__device__ __constant__ unsigned char ed25519_k[32] = {
0x59,0xf1,0xb2,0x26,0x94,0x9b,0xd6,0xeb,0x56,0xb1,0x83,0x82,0x9a,0x14,0xe0,0x00,
0x30,0xd1,0xf3,0xee,0xf2,0x80,0x8e,0x19,0xe7,0xfc,0xdf,0x56,0xdc,0xd9,0x06,0x24
};
__device__ __forceinline__ void ed25519_project(struct ed25519_pt* p, const unsigned char* x, const unsigned char* y) {
f25519_copy(p->x, x);
f25519_copy(p->y, y);
f25519_load(p->z, 1);
f25519_mul__distinct(p->t, x, y);
}
__device__ void ed25519_unproject(unsigned char* x, unsigned char* y, const struct ed25519_pt* p) {
unsigned char z1[F25519_SIZE];
__device__ void ed25519_unproject(unsigned char* __restrict__ x, unsigned char* __restrict__ y, const struct ed25519_pt* __restrict__ p) {
unsigned char z1[32];
f25519_inv__distinct(z1, p->z);
f25519_mul__distinct(x, p->x, z1);
f25519_mul__distinct(y, p->y, z1);
f25519_normalize(x);
f25519_normalize(y);
}
__device__ void ed25519_pack(unsigned char* __restrict__ c, const unsigned char* x, const unsigned char* y) {
unsigned char tmp[F25519_SIZE];
__device__ void ed25519_pack(unsigned char* __restrict__ c, const unsigned char* __restrict__ x, const unsigned char* __restrict__ y) {
unsigned char tmp[32];
unsigned char parity;
f25519_copy(tmp, x);
f25519_normalize(tmp);
@ -44,9 +38,8 @@ __device__ void ed25519_pack(unsigned char* __restrict__ c, const unsigned char*
f25519_normalize(c);
c[31] |= parity;
}
__device__ __forceinline__ void ed25519_add(struct ed25519_pt* r, const struct ed25519_pt* p1, const struct ed25519_pt* p2) {
unsigned char a[F25519_SIZE], b[F25519_SIZE], c[F25519_SIZE], d[F25519_SIZE];
unsigned char e[F25519_SIZE], f[F25519_SIZE], g[F25519_SIZE], h[F25519_SIZE];
__device__ __forceinline__ void ed25519_add(struct ed25519_pt* __restrict__ r, const struct ed25519_pt* __restrict__ p1, const struct ed25519_pt* __restrict__ p2) {
unsigned char a[32], b[32], c[32], d[32], e[32], f[32], g[32], h[32];
f25519_sub(c, p1->y, p1->x);
f25519_sub(d, p2->y, p2->x);
f25519_mul__distinct(a, c, d);
@ -66,9 +59,8 @@ __device__ __forceinline__ void ed25519_add(struct ed25519_pt* r, const struct e
f25519_mul__distinct(r->t, e, h);
f25519_mul__distinct(r->z, f, g);
}
__device__ __forceinline__ void ed25519_double(struct ed25519_pt* r, const struct ed25519_pt* p) {
unsigned char a[F25519_SIZE], b[F25519_SIZE], c[F25519_SIZE];
unsigned char e[F25519_SIZE], f[F25519_SIZE], g[F25519_SIZE], h[F25519_SIZE];
__device__ __forceinline__ void ed25519_double(struct ed25519_pt* __restrict__ r, const struct ed25519_pt* __restrict__ p) {
unsigned char a[32], b[32], c[32], e[32], f[32], g[32], h[32];
f25519_mul__distinct(a, p->x, p->x);
f25519_mul__distinct(b, p->y, p->y);
f25519_mul__distinct(c, p->z, p->z);
@ -86,15 +78,15 @@ __device__ __forceinline__ void ed25519_double(struct ed25519_pt* r, const struc
f25519_mul__distinct(r->t, e, h);
f25519_mul__distinct(r->z, f, g);
}
__device__ __forceinline__ void ed25519_copy(struct ed25519_pt* dst, const struct ed25519_pt* src) {
__device__ __forceinline__ void ed25519_copy(struct ed25519_pt* __restrict__ dst, const struct ed25519_pt* __restrict__ src) {
f25519_copy(dst->x, src->x);
f25519_copy(dst->y, src->y);
f25519_copy(dst->t, src->t);
f25519_copy(dst->z, src->z);
}
__device__ void ed25519_smult(struct ed25519_pt* r_out, const struct ed25519_pt* p, const unsigned char* e) {
struct ed25519_pt r = ed25519_neutral;
#pragma unroll
__device__ void ed25519_smult(ed25519_pt* r_out, const struct ed25519_pt* __restrict__ p, const unsigned char* __restrict__ e) {
ed25519_pt r = ed25519_neutral;
#pragma unroll 256
for (int i = 255; i >= 0; i--) {
struct ed25519_pt s;
ed25519_double(&r, &r);
@ -107,7 +99,7 @@ __device__ void ed25519_smult(struct ed25519_pt* r_out, const struct ed25519_pt*
}
ed25519_copy(r_out, &r);
}
__device__ void ed25519_prepare(unsigned char* e) {
__device__ void ed25519_prepare(unsigned char* __restrict__ e) {
e[0] &= 0xf8;
e[31] &= 0x7f;
e[31] |= 0x40;

View File

@ -1,12 +1,10 @@
#ifndef __ED25519_CUH
#define __ED25519_CUH
#define F25519_SIZE 32
struct ed25519_pt { unsigned char x[F25519_SIZE], y[F25519_SIZE], t[F25519_SIZE], z[F25519_SIZE]; };
struct ed25519_pt { unsigned char x[32], y[32], t[32], z[32]; };
extern __device__ __constant__ struct ed25519_pt ed25519_base;
extern __device__ __constant__ struct ed25519_pt ed25519_neutral;
extern __device__ __constant__ unsigned char ed25519_d[F25519_SIZE];
extern __device__ __constant__ unsigned char ed25519_k[F25519_SIZE];
__device__ __forceinline__ void ed25519_project(struct ed25519_pt* p, const unsigned char* x, const unsigned char* y);
extern __device__ __constant__ unsigned char ed25519_d[32];
extern __device__ __constant__ unsigned char ed25519_k[32];
__device__ void ed25519_unproject(unsigned char* x, unsigned char* y, const struct ed25519_pt* p);
__device__ void ed25519_pack(unsigned char* c, const unsigned char* x, const unsigned char* y);
__device__ __forceinline__ void ed25519_add(struct ed25519_pt* r, const struct ed25519_pt* p1, const struct ed25519_pt* p2);

View File

@ -3,13 +3,13 @@
#include <sha512.cuh>
__device__ void expand_key(unsigned char* expanded, const unsigned char* secret) {
struct sha512_state s;
sha512_init(&s);
sha512_final(&s, secret, 32);
sha512_get(&s, expanded, 0, 64);
memcpy(&s, &sha512_initial_state, sizeof(s));
sha512_final(&s, secret);
sha512_get(&s, expanded);
ed25519_prepare(expanded);
}
__device__ void pp(unsigned char* packed, const struct ed25519_pt* p) {
unsigned char x[F25519_SIZE], y[F25519_SIZE];
unsigned char x[32], y[32];
ed25519_unproject(x, y, p);
ed25519_pack(packed, x, y);
}
@ -23,16 +23,18 @@ __device__ void edsign_sec_to_pub(unsigned char* pub, const unsigned char* secre
expand_key(expanded, secret);
sm_pack(pub, expanded);
}
__device__ void compact_wipe(void* __restrict__ data, unsigned long length) {
__device__ void compact_wipe(void* __restrict__ data) {
volatile unsigned char* p = (volatile unsigned char*)data;
unsigned long i = 0;
for (; i + 3 < length; i += 4) {
#pragma unroll
for (; i + 3 < 32; i += 4) {
p[i] = 0;
p[i + 1] = 0;
p[i + 2] = 0;
p[i + 3] = 0;
}
for (; i < length; i++) {
#pragma unroll
for (; i < 32; i++) {
p[i] = 0;
}
}
@ -40,5 +42,5 @@ __device__ void ed25519_keygen(unsigned char private_key[64], unsigned char publ
edsign_sec_to_pub(public_key, random_seed);
memcpy(private_key, random_seed, 32);
memcpy(private_key + 32, public_key, 32);
compact_wipe(random_seed, 32);
compact_wipe(random_seed);
}

View File

@ -4,6 +4,6 @@ __device__ void expand_key(unsigned char* expanded, const unsigned char* secret)
__device__ void pp(unsigned char* packed, const struct ed25519_pt* p);
__device__ void sm_pack(unsigned char* r, const unsigned char* k);
__device__ void edsign_sec_to_pub(unsigned char* pub, const unsigned char* secret);
__device__ void compact_wipe(void* __restrict__ data, unsigned long length);
__device__ void compact_wipe(void* __restrict__ data);
__device__ void ed25519_keygen(unsigned char private_key[64], unsigned char public_key[32], unsigned char random_seed[32]);
#endif

View File

@ -1,60 +1,49 @@
#include <f25519.cuh>
__device__ __forceinline__ void f25519_load(unsigned char* __restrict__ x, unsigned int c) {
#pragma unroll
for (unsigned int i = 0; i < sizeof(c); i++) {
x[i] = c & 0xFF;
c >>= 8;
}
#pragma unroll
for (unsigned int i = sizeof(c); i < F25519_SIZE; i++) {
x[i] = 0;
}
}
__device__ void f25519_copy(unsigned char* __restrict__ x, const unsigned char* __restrict__ a) {
#pragma unroll
for (int i = 0; i < F25519_SIZE; i++) {
for (int i = 0; i < 32; i++) {
x[i] = a[i];
}
}
__device__ void f25519_select(unsigned char* __restrict__ dst, const unsigned char* __restrict__ zero, const unsigned char* __restrict__ one, unsigned char cond) {
const unsigned char mask = 0 - cond;
const unsigned char mask = -cond;
#pragma unroll
for (int i = 0; i < F25519_SIZE; i++) {
dst[i] = zero[i] ^ (mask & (one[i] ^ zero[i]));
for (int i = 0; i < 32; i++) {
dst[i] = (zero[i] & ~mask) | (one[i] & mask);
}
}
__device__ void f25519_normalize(unsigned char* __restrict__ x) {
unsigned char minusp[F25519_SIZE];
unsigned char minusp[32];
unsigned short c = (x[31] >> 7) * 19;
x[31] &= 127;
#pragma unroll
for (int i = 0; i < F25519_SIZE; i++) {
#pragma unroll 32
for (int i = 0; i < 32; i++) {
c += x[i];
x[i] = (unsigned char)c;
c >>= 8;
}
c = 19;
#pragma unroll
for (int i = 0; i + 1 < F25519_SIZE; i++) {
for (int i = 0; i + 1 < 32; i++) {
c += x[i];
minusp[i] = (unsigned char)c;
c >>= 8;
}
c += x[F25519_SIZE - 1] - 128;
minusp[F25519_SIZE - 1] = (unsigned char)c;
c += x[32 - 1] - 128;
minusp[32 - 1] = (unsigned char)c;
f25519_select(x, minusp, x, (c >> 15) & 1);
}
__device__ void f25519_add(unsigned char* __restrict__ r, const unsigned char* __restrict__ a, const unsigned char* __restrict__ b) {
unsigned short c = 0;
#pragma unroll
for (int i = 0; i < F25519_SIZE; i++) {
for (int i = 0; i < 32; i++) {
c = (c >> 8) + ((unsigned short)a[i]) + ((unsigned short)b[i]);
r[i] = (unsigned char)c;
}
r[F25519_SIZE - 1] &= 127;
r[32 - 1] &= 127;
c = (c >> 7) * 19;
#pragma unroll
for (int i = 0; i < F25519_SIZE; i++) {
for (int i = 0; i < 32; i++) {
c += r[i];
r[i] = (unsigned char)c;
c >>= 8;
@ -64,7 +53,7 @@ __device__ void f25519_sub(unsigned char* __restrict__ r, const unsigned char* _
unsigned c = 218;
int i = 0;
#pragma unroll
for (i = 0; i + 1 < F25519_SIZE; i++) {
for (i = 0; i + 1 < 32; i++) {
c += 65280 + ((unsigned)a[i]) - ((unsigned)b[i]);
r[i] = (unsigned char)c;
c >>= 8;
@ -73,7 +62,7 @@ __device__ void f25519_sub(unsigned char* __restrict__ r, const unsigned char* _
r[i] = (unsigned char)(c & 127);
c = (c >> 7) * 19;
#pragma unroll
for (i = 0; i < F25519_SIZE; i++) {
for (i = 0; i < 32; i++) {
c += r[i];
r[i] = (unsigned char)c;
c >>= 8;
@ -83,7 +72,7 @@ __device__ void f25519_neg(unsigned char* __restrict__ r, const unsigned char* _
unsigned c = 218;
int i = 0;
#pragma unroll
for (i = 0; i + 1 < F25519_SIZE; i++) {
for (i = 0; i + 1 < 32; i++) {
c += 65280 - ((unsigned)a[i]);
r[i] = (unsigned char)c;
c >>= 8;
@ -92,7 +81,7 @@ __device__ void f25519_neg(unsigned char* __restrict__ r, const unsigned char* _
r[i] = (unsigned char)(c & 127);
c = (c >> 7) * 19;
#pragma unroll
for (i = 0; i < F25519_SIZE; i++) {
for (i = 0; i < 32; i++) {
c += r[i];
r[i] = (unsigned char)c;
c >>= 8;
@ -101,27 +90,27 @@ __device__ void f25519_neg(unsigned char* __restrict__ r, const unsigned char* _
__device__ void f25519_mul__distinct(unsigned char* __restrict__ r, const unsigned char* __restrict__ a, const unsigned char* __restrict__ b) {
unsigned c = 0;
#pragma unroll
for (int i = 0; i < F25519_SIZE; i++) {
for (int i = 0; i < 32; i++) {
c >>= 8;
for (int j = 0; j <= i; j++) {
c += ((unsigned)a[j]) * ((unsigned)b[i - j]);
}
for (int j = i + 1; j < F25519_SIZE; j++) {
c += ((unsigned)a[j]) * ((unsigned)b[F25519_SIZE + i - j]) * 38;
for (int j = i + 1; j < 32; j++) {
c += ((unsigned)a[j]) * ((unsigned)b[32 + i - j]) * 38;
}
r[i] = (unsigned char)c;
}
r[F25519_SIZE - 1] &= 127;
r[32 - 1] &= 127;
c = (c >> 7) * 19;
#pragma unroll
for (int i = 0; i < F25519_SIZE; i++) {
for (int i = 0; i < 32; i++) {
c += r[i];
r[i] = (unsigned char)c;
c >>= 8;
}
}
__device__ void f25519_inv__distinct(unsigned char* __restrict__ r, const unsigned char* __restrict__ x) {
unsigned char s[F25519_SIZE];
unsigned char s[32];
f25519_mul__distinct(s, x, x);
f25519_mul__distinct(r, s, x);
#pragma unroll

View File

@ -1,7 +1,5 @@
#ifndef __F25519_CUH
#define __F25519_CUH
#define F25519_SIZE 32
__device__ void f25519_load(unsigned char* __restrict__ x, unsigned int c);
__device__ void f25519_copy(unsigned char* __restrict__ x, const unsigned char* __restrict__ a);
__device__ void f25519_select(unsigned char* __restrict__ dst, const unsigned char* __restrict__ zero, const unsigned char* __restrict__ one, unsigned char cond);
__device__ void f25519_normalize(unsigned char* __restrict__ x);

View File

@ -26,7 +26,7 @@ __device__ ds46 getAddr(const unsigned char rawAddr[16]) noexcept {
addrStr.data[pos] = '\0';
return addrStr;
}
__device__ void getRawAddress(int lErase, Key32& InvertedPublicKey, Address& rawAddr) noexcept {
__device__ void getRawAddress(int lErase, Key32& InvertedPublicKey, Addr16& rawAddr) noexcept {
lErase++;
const int bitsToShift = lErase & 7;
const int start = lErase >> 3;

View File

@ -6,7 +6,7 @@ struct ds64 {
struct ds46 {
char data[46];
};
using Address = unsigned char[16];
using Addr16 = unsigned char[16];
using Key32 = unsigned char[32];
struct KeysBox32 {
Key32 PublicKey;
@ -14,6 +14,6 @@ struct KeysBox32 {
};
__device__ ds64 ktos(const unsigned char* key) noexcept;
__device__ ds46 getAddr(const unsigned char rawAddr[16]) noexcept;
__device__ void getRawAddress(int lErase, Key32& InvertedPublicKey, Address& rawAddr) noexcept;
__device__ void getRawAddress(int lErase, Key32& InvertedPublicKey, Addr16& rawAddr) noexcept;
__device__ void invertKey(const unsigned char* key, unsigned char* inverted);
#endif

View File

@ -6,65 +6,43 @@ __device__ __constant__ sha512_state sha512_initial_state = { {
0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL,
} };
__device__ __constant__ unsigned long round_k[80] = {
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL,
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL,
};
__device__ __forceinline__ unsigned long load64(const unsigned char* x) {
__device__ __forceinline__ unsigned long load64(const unsigned char* __restrict__ x) {
return ((unsigned long)x[0] << 56) | ((unsigned long)x[1] << 48) | ((unsigned long)x[2] << 40) | ((unsigned long)x[3] << 32)
| ((unsigned long)x[4] << 24) | ((unsigned long)x[5] << 16) | ((unsigned long)x[6] << 8) | ((unsigned long)x[7]);
}
__device__ __forceinline__ void store64(unsigned char* x, unsigned long v) {
x[0] = (unsigned char)(v >> 56);
x[1] = (unsigned char)(v >> 48);
x[2] = (unsigned char)(v >> 40);
x[3] = (unsigned char)(v >> 32);
x[4] = (unsigned char)(v >> 24);
x[5] = (unsigned char)(v >> 16);
x[6] = (unsigned char)(v >> 8);
x[7] = (unsigned char)(v);
__device__ __forceinline__ void store64(unsigned char* __restrict__ x, unsigned long v) {
unsigned char val = 56;
#pragma unroll 8
for (unsigned char i = 0; i < 8; i++) {
x[i] = (unsigned char)(v >> val);
val -= 8;
}
}
__device__ __forceinline__ unsigned long rot64(unsigned long x, int bits) {
return (x >> bits) | (x << (64 - bits));
}
__device__ __forceinline__ void sha512_block(sha512_state* s, const unsigned char* blk) {
__device__ __forceinline__ void sha512_block(sha512_state* __restrict__ s, const unsigned char* __restrict__ blk) {
unsigned long w[16];
#pragma unroll 16
for (int i = 0; i < 16; i++) {
@ -88,10 +66,8 @@ __device__ __forceinline__ void sha512_block(sha512_state* s, const unsigned cha
unsigned long s1 = rot64(w[idx14], 19) ^ rot64(w[idx14], 61) ^ (w[idx14] >> 6);
unsigned long S0 = rot64(a, 28) ^ rot64(a, 34) ^ rot64(a, 39);
unsigned long S1 = rot64(e, 14) ^ rot64(e, 18) ^ rot64(e, 41);
unsigned long ch = (e & f) ^ ((~e) & g);
unsigned long temp1 = h + S1 + ch + round_k[i] + w[idx];
unsigned long maj = (a & b) ^ (a & c) ^ (b & c);
unsigned long temp2 = S0 + maj;
unsigned long temp1 = h + S1 + ((e & f) ^ ((~e) & g)) + round_k[i] + w[idx];
unsigned long temp2 = S0 + ((a & b) ^ (a & c) ^ (b & c));
h = g;
g = f;
f = e;
@ -111,36 +87,30 @@ __device__ __forceinline__ void sha512_block(sha512_state* s, const unsigned cha
s->h[6] += g;
s->h[7] += h;
}
__device__ void sha512_final(sha512_state* s, const unsigned char* blk, unsigned long total_size) {
__device__ void sha512_final(sha512_state* s, const unsigned char* blk) {
unsigned char temp[128];
memset(temp, 0, sizeof(temp));
unsigned long last_size = total_size & (128 - 1);
if (last_size) {
unsigned long last_size = 32 & (128 - 1);
memcpy(temp, blk, last_size);
}
temp[last_size] = 0x80;
if (last_size > (128 - 9)) {
sha512_block(s, temp);
memset(temp, 0, sizeof(temp));
}
store64(temp + 128 - 8, total_size << 3);
store64(temp + 128 - 8, 256);
sha512_block(s, temp);
}
__device__ void sha512_get(const sha512_state* s, unsigned char* hash, unsigned int offset, unsigned int len) {
if (offset > 128) return;
if (len > 128 - offset) len = 128 - offset;
unsigned int i = offset >> 3;
unsigned int off = offset & 7;
if (off) {
__device__ void sha512_get(const sha512_state* s, unsigned char* hash) {
unsigned len = 64;
if (0 > 128) return;
if (len > 128) len = 128;
unsigned i = 0, c = 8;
unsigned char tmp[8];
store64(tmp, s->h[i]);
unsigned int c = 8 - off;
if (c > len) c = len;
memcpy(hash, tmp + off, c);
memcpy(hash, tmp, c);
len -= c;
hash += c;
i++;
}
while (len >= 8) {
store64(hash, s->h[i]);
hash += 8;
@ -153,6 +123,3 @@ __device__ void sha512_get(const sha512_state* s, unsigned char* hash, unsigned
memcpy(hash, tmp, len);
}
}
__device__ void sha512_init(struct sha512_state* s) {
memcpy(s, &sha512_initial_state, sizeof(*s));
}

View File

@ -9,7 +9,6 @@ __device__ __forceinline__ unsigned long load64(const unsigned char* x);
__device__ __forceinline__ void store64(unsigned char* x, unsigned long v);
__device__ __forceinline__ unsigned long rot64(unsigned long x, int bits);
__device__ __forceinline__ void sha512_block(sha512_state* s, const unsigned char* blk);
__device__ void sha512_final(sha512_state* s, const unsigned char* blk, unsigned long total_size);
__device__ void sha512_get(const sha512_state* s, unsigned char* hash, unsigned int offset, unsigned int len);
__device__ void sha512_init(sha512_state* s);
__device__ void sha512_final(sha512_state* s, const unsigned char* blk);
__device__ void sha512_get(const sha512_state* s, unsigned char* hash);
#endif

View File

@ -1,7 +1,6 @@
#include <stdio.h>
#include <cuda_runtime.h>
#include <curand_kernel.h>
#include <sstream>
#include <iostream>
#include <sha512.cuh>
#include <ed25519.cuh>
#include <edsign.cuh>
@ -83,7 +82,7 @@ __global__ void KeyGen(curandState* randStates) {
rmbytes(seed, &localState);
ed25519_keygen(keys.PrivateKey, keys.PublicKey, seed);
if (unsigned zeros = getZeros(keys.PublicKey); zeros > atomicMax((unsigned*)&d_high, zeros)) {
Address raw;
Addr16 raw;
Key32 inv;
invertKey(keys.PublicKey, inv);
getRawAddress(zeros, inv, raw);
@ -92,9 +91,12 @@ __global__ void KeyGen(curandState* randStates) {
}
}
int main(int argc, char* argv[]) {
int* d_result;
cudaMalloc((void**)&d_result, sizeof(int));
const int thPerBlock = 256;
int* d_result, mBpSM, h_high;
char** d_argv;
cudaDeviceProp prop;
curandState* rst;
cudaMalloc((void**)&d_result, sizeof(int));
cudaMalloc((void**)&d_argv, argc * sizeof(char*));
for (int i = 0; i < argc; i++) {
unsigned long len = strlen(argv[i]) + 1;
@ -104,24 +106,16 @@ int main(int argc, char* argv[]) {
cudaMemcpy(&d_argv[i], &d_str, sizeof(char*), cudaMemcpyHostToDevice);
}
args<<<1, 1>>>(d_argv, argc, d_result);
unsigned h_high;
cudaMemcpyFromSymbol(&h_high, d_high, sizeof(unsigned));
printf("High addresses (2%02x+)\n", h_high);
const int threadsPerBlock = 256;
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 0);
int mBpSM;
cudaOccupancyMaxActiveBlocksPerMultiprocessor(&mBpSM, KeyGen, threadsPerBlock, 0);
const int totalThreads = mBpSM * prop.multiProcessorCount * threadsPerBlock;
printf("SMs: %d\n", prop.multiProcessorCount);
printf("MaxBlocksPerSM: %d\n", mBpSM);
printf("totalThreads: %d\n", totalThreads);
printf("BlocksThreads: %d:%d\n", totalThreads / threadsPerBlock, threadsPerBlock);
curandState* rst;
cudaMalloc(&rst, totalThreads * sizeof(curandState));
initRand<<<totalThreads / threadsPerBlock, threadsPerBlock>>>(rst);
cudaDeviceSynchronize();
KeyGen<<<totalThreads / threadsPerBlock, threadsPerBlock>>>(rst);
cudaMemcpyFromSymbol(&h_high, d_high, sizeof(unsigned));
cudaGetDeviceProperties(&prop, 0);
cudaOccupancyMaxActiveBlocksPerMultiprocessor(&mBpSM, KeyGen, thPerBlock, 0);
const int totalTh = mBpSM * prop.multiProcessorCount * thPerBlock;
printf("High addrs: 2%02x+\nSMs: %d\nMaxBlocksPerSM: %d\nTotalTh: %d\nBlocksThreads: %d:%d\n", h_high, prop.multiProcessorCount, mBpSM, totalTh, totalTh / thPerBlock, thPerBlock);
cudaMalloc(&rst, totalTh * sizeof(curandState));
initRand<<<totalTh / thPerBlock, thPerBlock>>>(rst);
cudaDeviceSynchronize();
KeyGen<<<totalTh / thPerBlock, thPerBlock>>>(rst);
cudaDeviceSynchronize();
cudaFree(rst);
return 0;