Skip to content

Commit

Permalink
more test vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbelvin committed Jun 27, 2017
1 parent 751cf04 commit 63928d6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
11 changes: 5 additions & 6 deletions merkle/coniks/coniks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
28 changes: 17 additions & 11 deletions merkle/coniks/coniks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions merkle/hstar2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 63928d6

Please sign in to comment.