Further simplify bitmasking lol

This commit is contained in:
y a t s
2026-06-04 11:19:13 -04:00
parent 840a63f727
commit 1deed12e7b
2 changed files with 11 additions and 5 deletions
+6 -5
View File
@@ -43,7 +43,7 @@ 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.
// Bitmask by setting a 1 bit (on LHS) for each remaining bit to check.
// Create 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)
@@ -62,11 +62,12 @@ func checkZeros(diff uint32, hash []byte) bool {
return false
}
}
// If we don't have any more bits to check, return.
if rem == 0 {
return true
}
// For rem = 0: mask = ^((1 << 8) - 1)
// = ^(00000000 - 1) [note the uint8 wraparound]
// = ^(11111111) [note the uint8 wraparound again]
// = 00000000
// Cool, right?
return hash[nbytes]&mask == 0x0
}
+5
View File
@@ -118,6 +118,11 @@ func TestCheckZeros(t *testing.T) {
t.Error(errBadZeroCheck{d, h})
}
d, h = uint32(16), []byte{0, 0, 255, 128, 42}
if !checkZeros(d, h) {
t.Error(errBadZeroCheck{d, h})
}
// This should fail (i.e. be false).
d, h = uint32(3), []byte{33, 130, 222, 88}
if checkZeros(d, h) {