Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 20b672e

Browse files
alexNgrsakridge
authored andcommitted
added implementation to poh_verify_many
1 parent cda1f2a commit 20b672e

File tree

11 files changed

+542
-18
lines changed

11 files changed

+542
-18
lines changed

src/Makefile

+18
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ V=debug
1313
include gpu-common.mk
1414

1515
ECC_TEST_BIN=cuda_ed25519_verify
16+
POH_VERIFY_MANY_TEST_BIN=cuda_poh_verify_many
1617
LIB=cuda-crypt
1718

19+
CL_POH_VERIFY_MANY_TEST_BIN=cl_poh_verify_many
1820
CL_ECC_TEST_BIN=cl_ed25519_verify
1921
CL_LIB=cl-crypt
2022

@@ -25,6 +27,8 @@ CUDA_SHA256_DIR:=cuda-sha256
2527

2628
CUDA_DIR ?= /usr/local/cuda
2729

30+
POH_VERIFY_TEST_DIR=poh-verify-test
31+
2832
CXX ?= g++
2933

3034
CFLAGS_COMMON:=-DENDIAN_NEUTRAL -DLTC_NO_ASM
@@ -39,11 +43,14 @@ CL_CFLAGS_debug:=$(CL_CFLAGS_common) -O0 -g
3943
CL_CFLAGS:=$(CL_CFLAGS_$V)
4044

4145
all: $(V)/$(ECC_TEST_BIN) \
46+
$(V)/$(POH_VERIFY_MANY_TEST_BIN) \
4247
$(V)/$(CL_ECC_TEST_BIN) \
48+
$(V)/$(CL_POH_VERIFY_MANY_TEST_BIN) \
4349
$(V)/lib$(LIB).so \
4450
$(V)/lib$(CL_LIB).so
4551

4652
ECC_DIR:=cuda-ecc-ed25519
53+
POH_VERIFY_MANY_DIR:=cuda-poh-verify
4754

4855
KEYPAIR_SRCS:=$(addprefix $(ECC_DIR)/,keypair.cu ed25519.h ge.h)
4956
$V/keypair.o: $(KEYPAIR_SRCS)
@@ -112,9 +119,16 @@ $V/cl_ecc_main.o: $(CL_ECC_DIR)/main.cpp $(ECC_DIR)/ed25519.h
112119
@mkdir -p $(@D)
113120
$(CXX) $(CL_CFLAGS) -pthread -I$(ECC_DIR) -c $< -o $@
114121

122+
$V/poh_many_main.o: $(POH_VERIFY_TEST_DIR)/main.cpp
123+
@mkdir -p $(@D)
124+
$(CXX) $(CL_CFLAGS) -pthread -I$(ECC_DIR) -c $< -o $@
125+
115126
$V/$(CL_ECC_TEST_BIN): $V/cl_ecc_main.o $V/lib$(CL_LIB).so
116127
$(CXX) $(CL_CFLAGS) -L$(CUDA_DIR)/lib64 -L$V -pthread $< -l$(CL_LIB) -lOpenCL -o $@
117128

129+
$V/$(CL_POH_VERIFY_MANY_TEST_BIN): $V/poh_many_main.o $V/lib$(CL_LIB).so
130+
$(CXX) $(CL_CFLAGS) -L$(CUDA_DIR)/lib64 -L$V -pthread $< -l$(CL_LIB) -lOpenCL -o $@
131+
118132
CPU_GPU_OBJS=$(addprefix $V/,verify.o poh_verify.o gpu_ctx.o sign.o seed.o keypair.o)
119133

120134
$V/crypt-dlink.o: $(CPU_GPU_OBJS)
@@ -130,6 +144,10 @@ $V/ecc_main.o: $(addprefix $(ECC_DIR)/,main.cu ed25519.h)
130144
$V/$(ECC_TEST_BIN): $V/ecc_main.o $V/lib$(LIB).so
131145
$(NVCC) $(CFLAGS) -L$V -l$(LIB) $< -o $@
132146

147+
$V/$(POH_VERIFY_MANY_TEST_BIN): $V/poh_many_main.o $V/lib$(LIB).so
148+
$(NVCC) $(CFLAGS) -L$V -l$(LIB) $< -o $@
149+
150+
133151
.PHONY:clean
134152
clean:
135153
rm -rf $V

src/cuda-poh-verify/poh_verify.cu

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ __global__ void poh_verify_kernel(uint8_t* hashes, uint64_t* num_hashes_arr, siz
2222
sha256_init(&sha_state);
2323
sha256_process(&sha_state, hash, SHA256_BLOCK_SIZE);
2424
sha256_done(&sha_state, hash);
25-
}
26-
25+
}
2726
memcpy(&hashes[idx * SHA256_BLOCK_SIZE], hash, SHA256_BLOCK_SIZE);
2827
}
2928

@@ -73,6 +72,11 @@ bool poh_init() {
7372
}
7473

7574
extern "C" {
75+
76+
void poh_verify_many_set_verbose(bool val) {
77+
g_verbose = val;
78+
}
79+
7680
int poh_verify_many(uint8_t* hashes,
7781
const uint64_t* num_hashes_arr,
7882
size_t num_elems,
@@ -132,7 +136,6 @@ int poh_verify_many(uint8_t* hashes,
132136
CUDA_CHK(cudaPeekAtLastError());
133137

134138
CUDA_CHK(cudaMemcpyAsync(hashes, cur_ctx->hashes, hashes_size, cudaMemcpyDeviceToHost, stream));
135-
136139
CUDA_CHK(cudaStreamSynchronize(stream));
137140

138141
pthread_mutex_unlock(&cur_ctx->mutex);

src/opencl-ecc-ed25519/verify.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,55 @@ int cuda_host_unregister(void* ptr)
200200
{
201201
return 0;
202202
}
203+
204+
static int
205+
get_checked_scalar(unsigned char* scalar, const unsigned char* signature) {
206+
// Check if top 4-bits are clear
207+
// then scalar is reduced.
208+
// if ((signature[31] & 0xf0) == 0) {
209+
// for (int i = 0; i < 32; i++) {
210+
// scalar[i] = signature[i];
211+
// }
212+
// return 0;
213+
// }
214+
215+
// if ((signature[31] >> 7) != 0) {
216+
// return 1;
217+
// }
218+
219+
// scalar32_reduce(scalar);
220+
// if (!consttime_equal(scalar, signature)) {
221+
// return 1;
222+
// }
223+
fprintf(stderr, "get_checked_scalar not implemented.\n");
224+
exit(1);
225+
return 0;
226+
227+
}
228+
229+
int ed25519_get_checked_scalar(unsigned char* out_scalar, const unsigned char* in_scalar) {
230+
return get_checked_scalar(out_scalar, in_scalar);
231+
}
232+
233+
// Return 0=success if ge unpacks and is not small order
234+
static int
235+
check_packed_ge_small_order(const unsigned char* packed_group_element) {
236+
// ge_p3 signature_R;
237+
238+
// fail if ge does not unpack
239+
// if (0 != ge_frombytes_negate_vartime(&signature_R, packed_group_element)) {
240+
// return 1;
241+
// }
242+
243+
// // fail if ge is small order
244+
// if (0 != ge_is_small_order(&signature_R)) {
245+
// return 1;
246+
// }
247+
fprintf(stderr, "check_packed_ge_small_order not implemented.\n");
248+
exit(1);
249+
return 0;
250+
}
251+
252+
int ed25519_check_packed_ge_small_order(const unsigned char* packed_group_element) {
253+
return check_packed_ge_small_order(packed_group_element);
254+
}

src/opencl-platform/cl_common.h

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ using namespace std;
1616
#define __host__
1717
#endif
1818

19+
/* this is to silent warnings about opencl version (without this Werror flag will prevent compiling)*/
20+
#ifndef CL_TARGET_OPENCL_VERSION
21+
#define CL_TARGET_OPENCL_VERSION 120
22+
#endif
23+
1924
extern bool g_verbose;
2025

2126
#define LOG(...) if (g_verbose) { printf(__VA_ARGS__); }

src/opencl-platform/cl_init_platform.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,12 @@ bool cl_check_init(void) {
290290

291291
/* select device based on cli arguments */
292292
string tmpAttrData = attr_data;
293-
294-
// always select last device of type GPU
295-
device = device_list[dev];
293+
294+
// always select first device of first platform
295+
if((dev == 0) && (platf == 0)) {
296+
device = device_list[dev];
297+
cout << "<----- SELECTED";
298+
}
296299

297300
delete[] attr_data;
298301
cout << endl;

src/opencl-platform/kernels_verify.h

+8-9
Original file line numberDiff line numberDiff line change
@@ -604,15 +604,14 @@ static __constant ulong32 K[64] = {
604604
};
605605
#endif
606606

607-
/* Various logical functions */
608607
#define Ch(x,y,z) (z ^ (x & (y ^ z)))
609608
#define Maj(x,y,z) (((x | y) & z) | (x & y))
610-
#define S(x, n) ROR64c((x),(n))
611-
#define R(x, n) (((((ulong)x) & ((ulong)0xFFFFFFFFFFFFFFFFUL))) >> ((ulong)n))
612-
#define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
613-
#define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
614-
#define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
615-
#define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
609+
#define S(x, n) RORc((x),(n))
610+
#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
611+
#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
612+
#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
613+
#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
614+
#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
616615
#ifndef MIN
617616
#define MIN(x, y) ( ((x)<(y))?(x):(y) )
618617
#endif
@@ -642,7 +641,7 @@ static int sha256_compress(hash_state * md, const unsigned char *buf)
642641

643642
/* fill W[16..63] */
644643
for (i = 16; i < 64; i++) {
645-
W[i] += Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
644+
W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
646645
}
647646

648647
/* Compress */
@@ -738,6 +737,7 @@ static int sha256_compress(hash_state * md, const unsigned char *buf)
738737
for (i = 0; i < 8; i++) {
739738
md->sha256.state[i] = md->sha256.state[i] + S[i];
740739
}
740+
741741
return CRYPT_OK;
742742
}
743743

@@ -4266,7 +4266,6 @@ __kernel void poh_verify_kernel(__global uint8_t* hashes,
42664266
for(int i = 0; i < SHA256_BLOCK_SIZE; i++) {
42674267
hash[i] = hashes[idx * SHA256_BLOCK_SIZE + i];
42684268
}
4269-
42704269

42714270
for (size_t i = 0; i < num_hashes_arr[idx]; i++) {
42724271
hash_state sha_state;

0 commit comments

Comments
 (0)