util.h (4060B)
1 /* 2 * util.h 3 * This file is part of lib-gpu-verify. 4 * 5 * lib-gpu-verify is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * lib-gpu-verify is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * Created by Cedric Zwahlen 16 * 17 */ 18 19 /* 20 * This file contains internal definitions that should not be exposed in the library 21 */ 22 23 #ifndef util_h 24 #define util_h 25 26 #include "universal.h" 27 28 #include <stdlib.h> 29 30 #include <errno.h> 31 #include <string.h> 32 #include <fcntl.h> 33 #include <sys/mman.h> 34 #include <sys/stat.h> 35 #include <stdbool.h> 36 #include <unistd.h> 37 38 #include <gcrypt.h> 39 #include <gmp.h> 40 41 #if __APPLE__ 42 #include <OpenCL/opencl.h> 43 #else 44 #include <CL/opencl.h> 45 #endif 46 47 #define NEED_LIBGCRYPT_VERSION "1.9.4" 48 49 typedef u_int64_t gpu_register; 50 typedef uint32_t DIGIT_T; 51 52 unsigned long gpuvt_estimate_pairs (void); 53 54 cl_platform_id select_platform (unsigned int offset, bool print_platforms); 55 56 cl_device_id select_device (cl_platform_id platform); 57 58 void 59 logger (const char *errinfo, 60 const void *private_info, 61 size_t cb, 62 void *user_data); 63 64 cl_context 65 create_compute_context (cl_device_id device_id); 66 67 cl_command_queue 68 create_command_queue (cl_device_id device_id, 69 cl_context context); 70 71 cl_program 72 compile_program (cl_device_id device_id, 73 cl_context context, 74 const char *sourcefile); 75 76 cl_kernel 77 create_kernel (cl_program program, 78 const char *name); 79 80 // Implementations of structures, their fields should be private 81 82 struct gpuv_public_key 83 { 84 85 86 char *n; 87 unsigned long len_n; 88 89 unsigned long e; 90 91 // calculated 92 93 char *ni; 94 unsigned long len_ni; 95 96 char *r_1; 97 unsigned long len_r_1; 98 99 int prepared; 100 101 }; 102 103 struct gpuv_signature_message 104 { 105 106 struct gpuv_public_key *pubkey; 107 108 char *m; 109 unsigned long len_m; 110 111 char *s; 112 unsigned long len_s; 113 114 // calculated 115 116 char *x; 117 unsigned long len_x; 118 119 char *M; 120 unsigned long len_M; 121 122 // unsigned long count; 123 int prepared; 124 }; 125 126 struct gpuv_batch 127 { 128 129 struct gpuv_signature_message *pairs; 130 unsigned long max_count; 131 unsigned long current; 132 133 // this is needed here, to make it easier to load it into the gpu 134 uint32_t *pk_indices; 135 unsigned long pk_max_count; 136 unsigned long pk_current; 137 138 // a list, containing indices of pubkeys 139 uint32_t *pk_list; 140 141 }; 142 143 struct gpuv_info 144 { 145 146 cl_platform_id platform; 147 cl_device_id device_id; 148 cl_context context; 149 cl_program program; 150 cl_kernel kernel; 151 152 enum GPUV_VARIANT variant; 153 154 int in_progress; 155 156 }; 157 158 struct gpuv_state 159 { 160 161 struct gpuv_info *info; 162 cl_command_queue queue; 163 164 // the callback the user defines 165 void (*cls)(void *, int, struct timespec, unsigned long, uint32_t *); // pointer to the results and how long the array is 166 void *arg; 167 168 int stale; // says if we can enqueue it or not (0 at init) 169 int ready; 170 171 // user provided 172 cl_mem n_mem; 173 cl_mem msg_mem; 174 cl_mem exp_mem; 175 cl_mem sig_mem; 176 unsigned long sig_count; 177 unsigned long pubkey_count; 178 179 // calculated 180 cl_mem x_mem; 181 cl_mem m_mem; 182 cl_mem ni_mem; 183 cl_mem res_mem; 184 cl_mem pks_indices; 185 186 // measuring times 187 struct timespec t; 188 struct timespec p; 189 190 int valid; // 0 in the beginning 191 192 uint32_t *results; 193 unsigned long results_len; 194 195 }; 196 197 198 void CL_CALLBACK callback_result (cl_event event, cl_int event_command_status, 199 void *user_data); 200 201 void CL_CALLBACK callback_kernel (cl_event event, cl_int event_command_status, 202 void *user_data); 203 204 void pk_to_mont (struct gpuv_public_key *pk); 205 void sig_msg_to_mont (struct gpuv_signature_message *sig_msg); 206 207 void cpu_verify (struct gpuv_batch *batch, struct gpuv_state *state); 208 209 #endif /* util_h */