#include #include #include __device__ __forceinline__ void expand_key(unsigned char* expanded, const unsigned char* secret) { struct sha512_state s; memcpy(&s, &sha512_initial_state, sizeof(s)); sha512_final(&s, secret); sha512_get(&s, expanded); expanded[0] &= 0xf8; expanded[31] &= 0x7f; expanded[31] |= 0x40; } __device__ __forceinline__ void pp(unsigned char* packed, const struct ed25519_pt* p) { unsigned char x[32], y[32]; ed25519_unproject(x, y, p); ed25519_pack(packed, x, y); } __device__ __forceinline__ void sm_pack(unsigned char* r, const unsigned char* k) { struct ed25519_pt p; ed25519_smult(&p, k); pp(r, &p); } __device__ __forceinline__ void edsign_sec_to_pub(unsigned char* pub, const unsigned char* secret) { unsigned char expanded[64]; expand_key(expanded, secret); sm_pack(pub, expanded); } __device__ __forceinline__ void compact_wipe(void* __restrict__ data) { unsigned char* p = (unsigned char*)data; unsigned long i = 0; #pragma unroll for (; i + 3 < 32; i += 4) { p[i] = 0; p[i + 1] = 0; p[i + 2] = 0; p[i + 3] = 0; } #pragma unroll for (; i < 32; i++) { p[i] = 0; } } __device__ void ed25519_keygen(unsigned char private_key[64], unsigned char public_key[32], unsigned char random_seed[32]) { 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); }