Simplify bitmask creation

Separate clearnet and onion tests
This commit is contained in:
y a t s
2026-06-03 12:30:57 -04:00
parent 66e6f74a4b
commit 840a63f727
4 changed files with 53 additions and 46 deletions
+7 -12
View File
@@ -13,8 +13,7 @@ import (
type Challenge struct {
Salt string // Challenge salt from server.
Diff uint32 // Difficulty level.
Steps int8 // Each step consists of a Challenge and a Solution. More than 1 may be required.
// Stored as a signed int to get past underflow issues later on.
Steps uint8 // Each step consists of a Challenge and a Solution. More than 1 may be required.
host *url.URL
}
@@ -44,7 +43,12 @@ func checkZeros(diff uint32, hash []byte) bool {
rem = diff % 8 // Remainder after dividing diff (given in bits) to bytes.
nbytes = (diff - rem) / 8 // Amount of 0x0 bytes we can divide difficulty bits into.
mask uint8 // Mask to check remaining bits.
// Bitmask by setting a 1 bit (on LHS) for each remaining bit to check.
mask uint8 = ^((1 << (8 - rem)) - 1)
// Example (rem = 3): mask = ^((1 << (8 - 3)) - 1)
// = ^(00100000 - 1)
// = ^(00011111)
// = 11100000
)
// Check bounds for the loops found below.
@@ -63,15 +67,6 @@ func checkZeros(diff uint32, hash []byte) bool {
return true
}
// Create bitmask by setting a bit to 1 for each remaining bit to check.
// The mask is built from right-to-left and then shifted to the LHS of the octet.
for range rem {
mask <<= 1
mask += 1
}
// Shift 1s we just added to the LHS of the octet.
mask <<= 8 - rem
return hash[nbytes]&mask == 0x0
}