-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add+enable service to mint/verify Equihash proofs of work
Add an abstract base class, 'ProofOfWorkService', for the existing PoW implementation 'HashCashService' and a new 'EquihashProofOfWorkService' PoW implementation based on Equihash-90-5 (which has 72 byte solutions & 5-10 MB peak memory usage). Since the current 'ProofOfWork' protobuf object only provides a 64-bit counter field to hold the puzzle solution (as that is all Hashcash requires), repurpose the 'payload' field to hold the Equihash puzzle solution bytes, with the 'challenge' field equal to the puzzle seed: the SHA256 hash of the offerId & makerAddress. Use a difficulty scale factor of 3e-5 (derived from benchmarking) to try to make the average Hashcash & Equihash puzzle solution times roughly equal for any given log-difficulty/numLeadingZeros integer chosen in the filter. NOTE: An empty enabled-version-list in the filter defaults to Hashcash (= version 0) only. The new Equihash-90-5 PoW scheme is version 1.
- Loading branch information
Showing
6 changed files
with
215 additions
and
29 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
common/src/main/java/bisq/common/crypto/EquihashProofOfWorkService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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.primitives.Longs; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
@Slf4j | ||
public class EquihashProofOfWorkService extends ProofOfWorkService { | ||
/** Rough cost of one Hashcash iteration compared to solving an Equihash-90-5 puzzle of unit difficulty. */ | ||
private static final double DIFFICULTY_SCALE_FACTOR = 3.0e-5; | ||
|
||
EquihashProofOfWorkService(int version) { | ||
super(version); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<ProofOfWork> mint(String itemId, byte[] challenge, int log2Difficulty) { | ||
double difficulty = adjustedDifficulty(log2Difficulty); | ||
log.info("Got adjusted difficulty: {}", difficulty); | ||
|
||
return CompletableFuture.supplyAsync(() -> { | ||
long ts = System.currentTimeMillis(); | ||
byte[] solution = new Equihash(90, 5, difficulty).puzzle(challenge).findSolution().serialize(); | ||
long counter = Longs.fromByteArray(Arrays.copyOf(solution, 8)); | ||
var proofOfWork = new ProofOfWork(solution, counter, challenge, log2Difficulty, | ||
System.currentTimeMillis() - ts, getVersion()); | ||
log.info("Completed minting proofOfWork: {}", proofOfWork); | ||
return proofOfWork; | ||
}); | ||
} | ||
|
||
@Override | ||
public byte[] getChallenge(String itemId, String ownerId) { | ||
checkArgument(!StringUtils.contains(itemId, '\0')); | ||
checkArgument(!StringUtils.contains(ownerId, '\0')); | ||
return Hash.getSha256Hash(checkNotNull(itemId) + "\0" + checkNotNull(ownerId)); | ||
} | ||
|
||
@Override | ||
boolean verify(ProofOfWork proofOfWork) { | ||
double difficulty = adjustedDifficulty(proofOfWork.getNumLeadingZeros()); | ||
|
||
var puzzle = new Equihash(90, 5, difficulty).puzzle(proofOfWork.getChallenge()); | ||
return puzzle.deserializeSolution(proofOfWork.getPayload()).verify(); | ||
} | ||
|
||
private static double adjustedDifficulty(int log2Difficulty) { | ||
return Equihash.adjustDifficulty(Math.scalb(DIFFICULTY_SCALE_FACTOR, log2Difficulty), | ||
Equihash.EQUIHASH_n_5_MEAN_SOLUTION_COUNT_PER_NONCE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
common/src/main/java/bisq/common/crypto/ProofOfWorkService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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.base.Preconditions; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.BiPredicate; | ||
|
||
import lombok.Getter; | ||
|
||
public abstract class ProofOfWorkService { | ||
private static class InstanceHolder { | ||
private static final ProofOfWorkService[] INSTANCES = { | ||
new HashCashService(), | ||
new EquihashProofOfWorkService(1) | ||
}; | ||
} | ||
|
||
public static Optional<ProofOfWorkService> forVersion(int version) { | ||
return version >= 0 && version < InstanceHolder.INSTANCES.length ? | ||
Optional.of(InstanceHolder.INSTANCES[version]) : Optional.empty(); | ||
} | ||
|
||
@Getter | ||
private final int version; | ||
|
||
ProofOfWorkService(int version) { | ||
this.version = version; | ||
} | ||
|
||
public abstract CompletableFuture<ProofOfWork> mint(String itemId, byte[] challenge, int log2Difficulty); | ||
|
||
public abstract byte[] getChallenge(String itemId, String ownerId); | ||
|
||
abstract boolean verify(ProofOfWork proofOfWork); | ||
|
||
public CompletableFuture<ProofOfWork> mint(String itemId, String ownerId, int log2Difficulty) { | ||
return mint(itemId, getChallenge(itemId, ownerId), log2Difficulty); | ||
} | ||
|
||
public boolean verify(ProofOfWork proofOfWork, | ||
String itemId, | ||
String ownerId, | ||
int controlLog2Difficulty, | ||
BiPredicate<byte[], byte[]> challengeValidation, | ||
BiPredicate<Integer, Integer> difficultyValidation) { | ||
|
||
Preconditions.checkArgument(proofOfWork.getVersion() == version); | ||
|
||
byte[] controlChallenge = getChallenge(itemId, ownerId); | ||
return challengeValidation.test(proofOfWork.getChallenge(), controlChallenge) && | ||
difficultyValidation.test(proofOfWork.getNumLeadingZeros(), controlLog2Difficulty) && | ||
verify(proofOfWork); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters