yggm/libs/edsign.cuh

37 lines
1.3 KiB
Plaintext
Raw Normal View History

2025-03-13 19:43:54 +05:00
#pragma once
2025-03-13 04:09:27 +05:00
#include <ed25519.cuh>
#include <sha512.cuh>
2025-03-13 19:43:54 +05:00
__device__ __forceinline__ void expand_key(unsigned char* expanded, const unsigned char* secret) {
2025-03-13 04:09:27 +05:00
struct sha512_state s;
sha512_init(&s);
2025-03-13 19:43:54 +05:00
sha512_final(&s, secret, 32);
sha512_get(&s, expanded, 0, 64);
2025-03-13 04:09:27 +05:00
ed25519_prepare(expanded);
}
2025-03-13 19:43:54 +05:00
__device__ __forceinline__ void pp(unsigned char* packed, const struct ed25519_pt* p) {
unsigned char x[F25519_SIZE], y[F25519_SIZE];
2025-03-13 04:09:27 +05:00
ed25519_unproject(x, y, p);
ed25519_pack(packed, x, y);
}
2025-03-13 19:43:54 +05:00
__device__ __forceinline__ void sm_pack(unsigned char* r, const unsigned char* k) {
2025-03-13 04:09:27 +05:00
struct ed25519_pt p;
ed25519_smult(&p, &ed25519_base, k);
pp(r, &p);
}
2025-03-13 19:43:54 +05:00
__device__ __forceinline__ void edsign_sec_to_pub(unsigned char* pub, const unsigned char* secret) {
unsigned char expanded[64];
2025-03-13 04:09:27 +05:00
expand_key(expanded, secret);
sm_pack(pub, expanded);
2025-03-14 19:55:09 +05:00
}
__device__ void compact_wipe(void* data, unsigned long length) {
volatile unsigned char* p = (volatile unsigned char*)data;
while (length--) {
*p++ = 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, 32);
2025-03-13 19:45:21 +05:00
}