diff --git a/cerberus.go b/cerberus.go index 866e349..f9836f5 100644 --- a/cerberus.go +++ b/cerberus.go @@ -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 } diff --git a/cerberus_test.go b/cerberus_test.go index fc65297..9265476 100644 --- a/cerberus_test.go +++ b/cerberus_test.go @@ -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) {