• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bernardladenthin / BitcoinAddressFinder / #305

19 Apr 2025 11:43PM UTC coverage: 68.442% (+0.9%) from 67.58%
#305

push

bernardladenthin
Refactor private key validation and serialization; extract logic to KeyUtility and ByteBufferUtility, improve tests

51 of 65 new or added lines in 9 files covered. (78.46%)

2 existing lines in 2 files now uncovered.

1195 of 1746 relevant lines covered (68.44%)

0.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/net/ladenthin/bitcoinaddressfinder/OpenCLContext.java
1
// @formatter:off
2
/**
3
 * Copyright 2020 Bernard Ladenthin bernard.ladenthin@gmail.com
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *    http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
// @formatter:on
19
package net.ladenthin.bitcoinaddressfinder;
20

21
import com.google.common.io.Resources;
22
import java.io.IOException;
23
import java.math.BigInteger;
24
import java.net.URL;
25
import java.nio.ByteBuffer;
26
import java.nio.charset.StandardCharsets;
27
import java.util.ArrayList;
28
import java.util.List;
29
import net.ladenthin.bitcoinaddressfinder.configuration.CProducerOpenCL;
30
import static org.jocl.CL.CL_CONTEXT_PLATFORM;
31
import static org.jocl.CL.clBuildProgram;
32
import static org.jocl.CL.clCreateCommandQueueWithProperties;
33
import static org.jocl.CL.clCreateContext;
34
import static org.jocl.CL.clCreateKernel;
35
import static org.jocl.CL.clCreateProgramWithSource;
36
import static org.jocl.CL.clGetDeviceIDs;
37
import static org.jocl.CL.clGetPlatformIDs;
38
import static org.jocl.CL.clReleaseCommandQueue;
39
import static org.jocl.CL.clReleaseContext;
40
import org.jocl.cl_command_queue;
41
import org.jocl.cl_context;
42
import org.jocl.cl_context_properties;
43
import org.jocl.cl_device_id;
44
import org.jocl.cl_kernel;
45
import org.jocl.cl_platform_id;
46
import org.jocl.cl_program;
47
import org.jocl.cl_queue_properties;
48
import org.jocl.CL;
49
import static org.jocl.CL.clReleaseKernel;
50
import static org.jocl.CL.clReleaseProgram;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

54
public class OpenCLContext {
55

56
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
×
57
    
58
    public String[] getOpenCLPrograms() throws IOException {
59
        List<String> resourceNamesContent = getResourceNamesContent(getResourceNames());
×
60
        List<String> resourceNamesContentWithReplacements = new ArrayList<>();
×
61
        for (String content : resourceNamesContent) {
×
62
            String contentWithReplacements = content;
×
63
            contentWithReplacements = contentWithReplacements.replaceAll("#include.*", "");
×
64
            resourceNamesContentWithReplacements.add(contentWithReplacements);
×
65
        }
×
66
        String[] openClPrograms = resourceNamesContentWithReplacements.toArray(new String[0]);
×
67
        return openClPrograms;
×
68
    }
69
    
70
    private List<String> getResourceNames() {
71
        List<String> resourceNames = new ArrayList<>();
×
72
        resourceNames.add("inc_defines.h");
×
73
        resourceNames.add("copyfromhashcat/inc_vendor.h");
×
74
        resourceNames.add("copyfromhashcat/inc_types.h");
×
75
        resourceNames.add("copyfromhashcat/inc_platform.h");
×
76
        resourceNames.add("copyfromhashcat/inc_platform.cl");
×
77
        resourceNames.add("copyfromhashcat/inc_common.h");
×
78
        resourceNames.add("copyfromhashcat/inc_common.cl");
×
79

80
        resourceNames.add("copyfromhashcat/inc_ecc_secp256k1.h");
×
81
        resourceNames.add("copyfromhashcat/inc_ecc_secp256k1.cl");
×
82
        resourceNames.add("inc_ecc_secp256k1custom.cl");
×
83
        return resourceNames;
×
84
    }
85
    
86
    private final static String KERNEL_NAME = "generateKeysKernel_grid";
87
    private final static boolean EXCEPTIONS_ENABLED = true;
88
    
89
    private final CProducerOpenCL producerOpenCL;
90
    private final BitHelper bitHelper;
91

92
    private cl_context_properties contextProperties;
93
    private cl_device_id device;
94
    private cl_context context;
95
    private cl_command_queue commandQueue;
96
    private cl_program program;
97
    private cl_kernel kernel;
98
    private OpenClTask openClTask;
99
    private ByteBufferUtility byteBufferUtility;
100
    
101
    public OpenCLContext(CProducerOpenCL producerOpenCL, BitHelper bitHelper) {
×
102
        this.producerOpenCL = producerOpenCL;
×
103
        this.bitHelper = bitHelper;
×
NEW
104
        this.byteBufferUtility = new ByteBufferUtility(true);
×
UNCOV
105
    }
×
106
    
107
    public void init() throws IOException {
108
        
109
        // #################### general ####################
110
        
111
        // Enable exceptions and subsequently omit error checks in this sample
112
        CL.setExceptionsEnabled(EXCEPTIONS_ENABLED);
×
113
        
114
        // Obtain the number of platforms
115
        int numPlatformsArray[] = new int[1];
×
116
        clGetPlatformIDs(0, null, numPlatformsArray);
×
117
        int numPlatforms = numPlatformsArray[0];
×
118
        
119
        // Obtain a platform ID
120
        cl_platform_id platforms[] = new cl_platform_id[numPlatforms];
×
121
        clGetPlatformIDs(platforms.length, platforms, null);
×
122
        cl_platform_id platform = platforms[producerOpenCL.platformIndex];
×
123
        
124
        // Initialize the context properties
125
        contextProperties = new cl_context_properties();
×
126
        contextProperties.addProperty(CL_CONTEXT_PLATFORM, platform);
×
127
        
128
        // Obtain the number of devices for the platform
129
        int numDevicesArray[] = new int[1];
×
130
        clGetDeviceIDs(platform, producerOpenCL.deviceType, 0, null, numDevicesArray);
×
131
        int numDevices = numDevicesArray[0];
×
132
        
133
        // Obtain a device ID 
134
        cl_device_id devices[] = new cl_device_id[numDevices];
×
135
        clGetDeviceIDs(platform, producerOpenCL.deviceType, numDevices, devices, null);
×
136
        device = devices[producerOpenCL.deviceIndex];
×
137
        cl_device_id[] cl_device_ids = new cl_device_id[]{device};
×
138
        
139
        // Create a context for the selected device
140
        context = clCreateContext(contextProperties, 1, cl_device_ids, null, null, null);
×
141
        
142
        // Create a command-queue for the selected device
143
        cl_queue_properties properties = new cl_queue_properties();
×
144
        commandQueue = clCreateCommandQueueWithProperties(context, device, properties, null);
×
145
        
146
        // #################### kernel specifix ####################
147
        
148
        String[] openCLPrograms = getOpenCLPrograms();
×
149
        // Create the program from the source code
150
        program = clCreateProgramWithSource(context, openCLPrograms.length, openCLPrograms, null, null);
×
151

152
        // Build the program
153
        clBuildProgram(program, 0, null, null, null, null);
×
154
        
155
        // Create the kernel
156
        kernel = clCreateKernel(program, KERNEL_NAME, null);
×
157
        
NEW
158
        openClTask = new OpenClTask(context, producerOpenCL, bitHelper, byteBufferUtility);
×
159
    }
×
160

161
    OpenClTask getOpenClTask() {
162
        return openClTask;
×
163
    }
164

165
    public void release() {
166
        openClTask.releaseCl();
×
167
        clReleaseKernel(kernel);
×
168
        clReleaseProgram(program);
×
169
        clReleaseCommandQueue(commandQueue);
×
170
        clReleaseContext(context);
×
171
    }
×
172

173
    public OpenCLGridResult createKeys(BigInteger privateKeyBase) {
174
        openClTask.setSrcPrivateKeyChunk(privateKeyBase);
×
175
        ByteBuffer dstByteBuffer = openClTask.executeKernel(kernel, commandQueue);
×
176

177
        OpenCLGridResult openCLGridResult = new OpenCLGridResult(privateKeyBase, bitHelper.convertBitsToSize(producerOpenCL.batchSizeInBits), dstByteBuffer);
×
178
        return openCLGridResult;
×
179
    }
180

181
    private static List<String> getResourceNamesContent(List<String> resourceNames) throws IOException {
182
        List<String> contents = new ArrayList<>();
×
183
        for (String resourceName : resourceNames) {
×
184
            URL url = Resources.getResource(resourceName);
×
185
            String content = Resources.toString(url, StandardCharsets.UTF_8);
×
186
            contents.add(content);
×
187
        }
×
188
        return contents;
×
189
    }
190
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc