diff --git a/bls/globals.go b/bls/globals.go index dad60de..1ecf8a5 100644 --- a/bls/globals.go +++ b/bls/globals.go @@ -70,7 +70,7 @@ func initGlobals() { } func IsPowerOfTwo(v uint64) bool { - return v&(v-1) == 0 + return v != 0 && v&(v-1) == 0 } func EvalPolyAtUnoptimized(dst *Fr, coeffs []Fr, x *Fr) { diff --git a/bls/globals_test.go b/bls/globals_test.go new file mode 100644 index 0000000..1763116 --- /dev/null +++ b/bls/globals_test.go @@ -0,0 +1,48 @@ +package bls + +import ( + "testing" +) + +func TestIsPowerOfTwo(t *testing.T) { + testCases := []struct { + name string + input uint64 + expected bool + }{ + { + name: "0 is not a power of 2", + input: 0, + expected: false, + }, + { + name: "2^0 = 1", + input: 1, + expected: true, + }, + { + name: "2^1 = 2", + input: 2, + expected: true, + }, + { + name: "3 is not a power of 2", + input: 3, + expected: false, + }, + { + name: "2^2 = 4", + input: 4, + expected: true, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := IsPowerOfTwo(tc.input) + if result != tc.expected { + t.Fatalf("IsPowerOfTwo(%d) = %v; want %v", tc.input, result, tc.expected) + } + }) + } +}