forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashCashService.java
181 lines (158 loc) · 6.89 KB
/
HashCashService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.common.crypto;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.primitives.Longs;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiPredicate;
import lombok.extern.slf4j.Slf4j;
/**
* HashCash implementation for proof of work
* It doubles required work by difficulty increase (adding one leading zero).
*
* See https://www.hashcash.org/papers/hashcash.pdf
*/
@Slf4j
public class HashCashService {
// Default validations. Custom implementations might use tolerance.
private static final BiPredicate<byte[], byte[]> isChallengeValid = Arrays::equals;
private static final BiPredicate<Integer, Integer> isDifficultyValid = Integer::equals;
public static CompletableFuture<ProofOfWork> mint(byte[] payload,
byte[] challenge,
int difficulty) {
return HashCashService.mint(payload,
challenge,
difficulty,
HashCashService::testDifficulty);
}
public static boolean verify(ProofOfWork proofOfWork) {
return verify(proofOfWork,
proofOfWork.getChallenge(),
proofOfWork.getNumLeadingZeros());
}
public static boolean verify(ProofOfWork proofOfWork,
byte[] controlChallenge,
int controlDifficulty) {
return HashCashService.verify(proofOfWork,
controlChallenge,
controlDifficulty,
HashCashService::testDifficulty);
}
public static boolean verify(ProofOfWork proofOfWork,
byte[] controlChallenge,
int controlDifficulty,
BiPredicate<byte[], byte[]> challengeValidation,
BiPredicate<Integer, Integer> difficultyValidation) {
return HashCashService.verify(proofOfWork,
controlChallenge,
controlDifficulty,
challengeValidation,
difficultyValidation,
HashCashService::testDifficulty);
}
private static boolean testDifficulty(byte[] result, long difficulty) {
return HashCashService.numberOfLeadingZeros(result) > difficulty;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Generic
///////////////////////////////////////////////////////////////////////////////////////////
static CompletableFuture<ProofOfWork> mint(byte[] payload,
byte[] challenge,
int difficulty,
BiPredicate<byte[], Integer> testDifficulty) {
return CompletableFuture.supplyAsync(() -> {
long ts = System.currentTimeMillis();
byte[] result;
long counter = 0;
do {
result = toSha256Hash(payload, challenge, ++counter);
}
while (!testDifficulty.test(result, difficulty));
ProofOfWork proofOfWork = new ProofOfWork(payload, counter, challenge, difficulty, System.currentTimeMillis() - ts, 0);
log.info("Completed minting proofOfWork: {}", proofOfWork);
return proofOfWork;
});
}
static boolean verify(ProofOfWork proofOfWork,
byte[] controlChallenge,
int controlDifficulty,
BiPredicate<byte[], Integer> testDifficulty) {
return verify(proofOfWork,
controlChallenge,
controlDifficulty,
HashCashService.isChallengeValid,
HashCashService.isDifficultyValid,
testDifficulty);
}
static boolean verify(ProofOfWork proofOfWork,
byte[] controlChallenge,
int controlDifficulty,
BiPredicate<byte[], byte[]> challengeValidation,
BiPredicate<Integer, Integer> difficultyValidation,
BiPredicate<byte[], Integer> testDifficulty) {
return challengeValidation.test(proofOfWork.getChallenge(), controlChallenge) &&
difficultyValidation.test(proofOfWork.getNumLeadingZeros(), controlDifficulty) &&
verify(proofOfWork, testDifficulty);
}
private static boolean verify(ProofOfWork proofOfWork, BiPredicate<byte[], Integer> testDifficulty) {
byte[] hash = HashCashService.toSha256Hash(proofOfWork.getPayload(),
proofOfWork.getChallenge(),
proofOfWork.getCounter());
return testDifficulty.test(hash, proofOfWork.getNumLeadingZeros());
}
///////////////////////////////////////////////////////////////////////////////////////////
// Utils
///////////////////////////////////////////////////////////////////////////////////////////
public static byte[] getBytes(String value) {
return value.getBytes(StandardCharsets.UTF_8);
}
@VisibleForTesting
static int numberOfLeadingZeros(byte[] bytes) {
int numberOfLeadingZeros = 0;
for (int i = 0; i < bytes.length; i++) {
numberOfLeadingZeros += numberOfLeadingZeros(bytes[i]);
if (numberOfLeadingZeros < 8 * (i + 1)) {
break;
}
}
return numberOfLeadingZeros;
}
private static byte[] toSha256Hash(byte[] payload, byte[] challenge, long counter) {
byte[] preImage = org.bouncycastle.util.Arrays.concatenate(payload,
challenge,
Longs.toByteArray(counter));
return Hash.getSha256Hash(preImage);
}
// Borrowed from Integer.numberOfLeadingZeros and adjusted for byte
@VisibleForTesting
static int numberOfLeadingZeros(byte i) {
if (i <= 0)
return i == 0 ? 8 : 0;
int n = 7;
if (i >= 1 << 4) {
n -= 4;
i >>>= 4;
}
if (i >= 1 << 2) {
n -= 2;
i >>>= 2;
}
return n - (i >>> 1);
}
}