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

bernardladenthin / BitcoinAddressFinder / #312

06 May 2025 08:41PM UTC coverage: 67.925% (+0.4%) from 67.512%
#312

push

bernardladenthin
Refactor OpenCL I/O to use unified chunk layout and consistent endianness handling

0 of 10 new or added lines in 3 files covered. (0.0%)

2 existing lines in 2 files now uncovered.

1224 of 1802 relevant lines covered (67.92%)

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 net.ladenthin.bitcoinaddressfinder.opencl.OpenCLBuilder;
31
import net.ladenthin.bitcoinaddressfinder.opencl.OpenCLDevice;
32
import net.ladenthin.bitcoinaddressfinder.opencl.OpenCLDeviceSelection;
33
import net.ladenthin.bitcoinaddressfinder.opencl.OpenCLPlatform;
34
import net.ladenthin.bitcoinaddressfinder.opencl.OpenCLPlatformSelector;
35
import static org.jocl.CL.clBuildProgram;
36
import static org.jocl.CL.clCreateCommandQueueWithProperties;
37
import static org.jocl.CL.clCreateContext;
38
import static org.jocl.CL.clCreateKernel;
39
import static org.jocl.CL.clCreateProgramWithSource;
40
import static org.jocl.CL.clReleaseCommandQueue;
41
import static org.jocl.CL.clReleaseContext;
42
import org.jocl.cl_command_queue;
43
import org.jocl.cl_context;
44
import org.jocl.cl_context_properties;
45
import org.jocl.cl_device_id;
46
import org.jocl.cl_kernel;
47
import org.jocl.cl_program;
48
import org.jocl.cl_queue_properties;
49
import org.jocl.CL;
50
import static org.jocl.CL.clReleaseKernel;
51
import static org.jocl.CL.clReleaseProgram;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

55
public class OpenCLContext {
56

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

85
        resourceNames.add("copyfromhashcat/inc_ecc_secp256k1.h");
×
86
        resourceNames.add("copyfromhashcat/inc_ecc_secp256k1.cl");
×
87
        resourceNames.add("inc_ecc_secp256k1custom.cl");
×
88
        return resourceNames;
×
89
    }
90
    
91
    private final static String KERNEL_NAME = "generateKeysKernel_grid";
92
    private final static boolean EXCEPTIONS_ENABLED = true;
93
    
94
    private final CProducerOpenCL producerOpenCL;
95
    private final BitHelper bitHelper;
96

97
    private OpenCLDevice device;
98
    private cl_context context;
99
    private cl_command_queue commandQueue;
100
    private cl_program program;
101
    private cl_kernel kernel;
102
    private OpenClTask openClTask;
103
    private ByteBufferUtility byteBufferUtility;
104
    
105
    public OpenCLContext(CProducerOpenCL producerOpenCL, BitHelper bitHelper) {
×
106
        this.producerOpenCL = producerOpenCL;
×
107
        this.bitHelper = bitHelper;
×
108
        this.byteBufferUtility = new ByteBufferUtility(true);
×
109
    }
×
110
    
111
    public void init() throws IOException {
112
        
113
        // #################### general ####################
114
        
115
        // Enable exceptions and subsequently omit error checks in this sample
116
        CL.setExceptionsEnabled(EXCEPTIONS_ENABLED);
×
117
        
118
        List<OpenCLPlatform> platforms = new OpenCLBuilder().build();
×
119

120
        OpenCLDeviceSelection selection = OpenCLPlatformSelector.select(
×
121
            platforms,
122
            producerOpenCL.platformIndex,
123
            producerOpenCL.deviceType,
124
            producerOpenCL.deviceIndex
125
        );
126
        
127
        device = selection.getDevice();
×
128
        cl_context_properties contextProperties = selection.getContextProperties();
×
129
        cl_device_id[] cl_device_ids = new cl_device_id[]{device.device()};
×
130
        
131
        // Create a context for the selected device
132
        context = clCreateContext(contextProperties, 1, cl_device_ids, null, null, null);
×
133
        
134
        // Create a command-queue for the selected device
135
        cl_queue_properties properties = new cl_queue_properties();
×
136
        commandQueue = clCreateCommandQueueWithProperties(context, device.device(), properties, null);
×
137
        
138
        // #################### kernel specifix ####################
139
        
140
        String[] openCLPrograms = getOpenCLPrograms();
×
141
        // Create the program from the source code
142
        program = clCreateProgramWithSource(context, openCLPrograms.length, openCLPrograms, null, null);
×
143

144
        // Build the program
145
        clBuildProgram(program, 0, null, null, null, null);
×
146
        
147
        // Create the kernel
148
        kernel = clCreateKernel(program, KERNEL_NAME, null);
×
149
        
NEW
150
        openClTask = new OpenClTask(context, producerOpenCL, bitHelper, byteBufferUtility);
×
151
    }
×
152

153
    OpenClTask getOpenClTask() {
154
        return openClTask;
×
155
    }
156

157
    public void release() {
158
        openClTask.releaseCl();
×
159
        clReleaseKernel(kernel);
×
160
        clReleaseProgram(program);
×
161
        clReleaseCommandQueue(commandQueue);
×
162
        clReleaseContext(context);
×
163
    }
×
164

165
    public OpenCLGridResult createKeys(BigInteger privateKeyBase) {
166
        openClTask.setSrcPrivateKeyChunk(privateKeyBase);
×
167
        ByteBuffer dstByteBuffer = openClTask.executeKernel(kernel, commandQueue);
×
168

169
        OpenCLGridResult openCLGridResult = new OpenCLGridResult(privateKeyBase, bitHelper.convertBitsToSize(producerOpenCL.batchSizeInBits), dstByteBuffer);
×
170
        return openCLGridResult;
×
171
    }
172

173
    private static List<String> getResourceNamesContent(List<String> resourceNames) throws IOException {
174
        List<String> contents = new ArrayList<>();
×
175
        for (String resourceName : resourceNames) {
×
176
            URL url = Resources.getResource(resourceName);
×
177
            String content = Resources.toString(url, StandardCharsets.UTF_8);
×
178
            contents.add(content);
×
179
        }
×
180
        return contents;
×
181
    }
182
}
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