mirror of
https://gitgud.io/yats/cerberus.git
synced 2026-06-17 10:05:18 -04:00
Further simplify bitmasking lol
This commit is contained in:
+6
-5
@@ -43,7 +43,7 @@ func checkZeros(diff uint32, hash []byte) bool {
|
|||||||
rem = diff % 8 // Remainder after dividing diff (given in bits) to bytes.
|
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.
|
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)
|
mask uint8 = ^((1 << (8 - rem)) - 1)
|
||||||
// Example (rem = 3): mask = ^((1 << (8 - 3)) - 1)
|
// Example (rem = 3): mask = ^((1 << (8 - 3)) - 1)
|
||||||
// = ^(00100000 - 1)
|
// = ^(00100000 - 1)
|
||||||
@@ -62,11 +62,12 @@ func checkZeros(diff uint32, hash []byte) bool {
|
|||||||
return false
|
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
|
return hash[nbytes]&mask == 0x0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -118,6 +118,11 @@ func TestCheckZeros(t *testing.T) {
|
|||||||
t.Error(errBadZeroCheck{d, h})
|
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).
|
// This should fail (i.e. be false).
|
||||||
d, h = uint32(3), []byte{33, 130, 222, 88}
|
d, h = uint32(3), []byte{33, 130, 222, 88}
|
||||||
if checkZeros(d, h) {
|
if checkZeros(d, h) {
|
||||||
|
|||||||
Reference in New Issue
Block a user