diff --git a/merkle/coniks/coniks.go b/merkle/coniks/coniks.go index b6181db83a..043ab31fbb 100644 --- a/merkle/coniks/coniks.go +++ b/merkle/coniks/coniks.go @@ -94,15 +94,14 @@ func (m *hasher) BitLen() int { return m.Size() * 8 } -// leftmask contains bitmasks indexed such that the left x bits are set. -// 0 is special cased to 0xFF since 8 mod 8 is 0. -var leftmask = map[int]byte{ - 0: 0xFF, 1: 0x80, 2: 0xC0, 3: 0xE0, - 4: 0xF0, 5: 0xF8, 6: 0xFC, 7: 0xFE, -} +// leftmask contains bitmasks indexed such that the left x bits are set. It is +// indexed by byte position from 0-7 0 is special cased to 0xFF since 8 mod 8 +// is 0. leftmask is only used to mask the last byte. +var leftmask = [8]byte{0xFF, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE} // maskIndex returns index with only the left depth bits set. // index must be of size m.Size() and 0 <= depth <= m.BitLen(). +// e.g. func (m *hasher) maskIndex(index []byte, depth int) []byte { if got, want := len(index), m.Size(); got != want { panic(fmt.Sprintf("index len: %d, want %d", got, want)) diff --git a/merkle/coniks/coniks_test.go b/merkle/coniks/coniks_test.go index 1de0d54851..4924291e93 100644 --- a/merkle/coniks/coniks_test.go +++ b/merkle/coniks/coniks_test.go @@ -53,24 +53,30 @@ func TestVectors(t *testing.T) { } } -func TestFormatIndex(t *testing.T) { +func TestMaskIndex(t *testing.T) { h := &hasher{crypto.SHA1} // Use a shorter hash for shorter test vectors. for _, tc := range []struct { index []byte depth int want []byte }{ - {index: h2b("FF00000000000000000000000000000000000000"), depth: 0, want: h2b("0000000000000000000000000000000000000000")}, - {index: h2b("FF00000000000000000000000000000000000000"), depth: 1, want: h2b("8000000000000000000000000000000000000000")}, - {index: h2b("FF00000000000000000000000000000000000000"), depth: 2, want: h2b("C000000000000000000000000000000000000000")}, - {index: h2b("FF00000000000000000000000000000000000000"), depth: 3, want: h2b("E000000000000000000000000000000000000000")}, - {index: h2b("FF00000000000000000000000000000000000000"), depth: 4, want: h2b("F000000000000000000000000000000000000000")}, - {index: h2b("FF00000000000000000000000000000000000000"), depth: 5, want: h2b("F800000000000000000000000000000000000000")}, - {index: h2b("FF00000000000000000000000000000000000000"), depth: 6, want: h2b("FC00000000000000000000000000000000000000")}, - {index: h2b("FF00000000000000000000000000000000000000"), depth: 7, want: h2b("FE00000000000000000000000000000000000000")}, - {index: h2b("FF00000000000000000000000000000000000000"), depth: 8, want: h2b("FF00000000000000000000000000000000000000")}, - {index: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), depth: 160, want: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")}, + {index: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), depth: 0, want: h2b("0000000000000000000000000000000000000000")}, + {index: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), depth: 1, want: h2b("8000000000000000000000000000000000000000")}, + {index: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), depth: 2, want: h2b("C000000000000000000000000000000000000000")}, + {index: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), depth: 3, want: h2b("E000000000000000000000000000000000000000")}, + {index: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), depth: 4, want: h2b("F000000000000000000000000000000000000000")}, + {index: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), depth: 5, want: h2b("F800000000000000000000000000000000000000")}, + {index: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), depth: 6, want: h2b("FC00000000000000000000000000000000000000")}, + {index: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), depth: 7, want: h2b("FE00000000000000000000000000000000000000")}, + {index: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), depth: 8, want: h2b("FF00000000000000000000000000000000000000")}, + {index: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), depth: 9, want: h2b("FF80000000000000000000000000000000000000")}, + {index: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), depth: 10, want: h2b("FFC0000000000000000000000000000000000000")}, {index: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), depth: 159, want: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE")}, + {index: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), depth: 160, want: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")}, + {index: h2b("000102030405060708090A0B0C0D0E0F10111213"), depth: 1, want: h2b("0000000000000000000000000000000000000000")}, + {index: h2b("000102030405060708090A0B0C0D0E0F10111213"), depth: 17, want: h2b("0001000000000000000000000000000000000000")}, + {index: h2b("000102030405060708090A0B0C0D0E0F10111213"), depth: 159, want: h2b("000102030405060708090A0B0C0D0E0F10111212")}, + {index: h2b("000102030405060708090A0B0C0D0E0F10111213"), depth: 160, want: h2b("000102030405060708090A0B0C0D0E0F10111213")}, } { if got, want := h.maskIndex(tc.index, tc.depth), tc.want; !bytes.Equal(got, want) { t.Errorf("maskIndex(%x, %v): %x, want %x", tc.index, tc.depth, got, want) diff --git a/merkle/hstar2_test.go b/merkle/hstar2_test.go index d0198ba451..876a5794f2 100644 --- a/merkle/hstar2_test.go +++ b/merkle/hstar2_test.go @@ -211,13 +211,15 @@ func TestHStar2NegativeTreeLevelOffset(t *testing.T) { func TestPaddedBytes(t *testing.T) { size := 160 / 8 for _, tc := range []struct { - i int64 + i *big.Int want []byte }{ - {i: 1, want: h2b("0000000000000000000000000000000000000001")}, + {i: big.NewInt(0), want: h2b("0000000000000000000000000000000000000000")}, + {i: big.NewInt(1), want: h2b("0000000000000000000000000000000000000001")}, + {i: new(big.Int).SetBytes(h2b("00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0F")), want: h2b("00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0F")}, + {i: new(big.Int).SetBytes(h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0F")), want: h2b("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0F")}, } { - bigInt := new(big.Int).SetInt64(tc.i) - if got, want := PaddedBytes(bigInt, size), tc.want; !bytes.Equal(got, want) { + if got, want := PaddedBytes(tc.i, size), tc.want; !bytes.Equal(got, want) { t.Errorf("PaddedBytes(%d): %x, want %x", tc.i, got, want) } }