Skip to content

Commit

Permalink
review feedback: follow the spec and pad shares when generating the s…
Browse files Browse the repository at this point in the history
…hare commitment instead of not allowing chains
  • Loading branch information
evan-forbes committed Feb 19, 2021
1 parent bd70fa9 commit 96ffd13
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
25 changes: 22 additions & 3 deletions x/lazyledgerapp/types/payformessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ func (msg *SignedTransactionDataPayForMessage) GetSigners() []sdk.AccAddress {
// https://github.com/lazyledger/lazyledger-specs/blob/master/rationale/message_block_layout.md#non-interactive-default-rules
func CreateCommit(k uint64, namespace, message []byte) ([]byte, error) {
// break message into shares
shares := ChunkMessage(message)
shares := chunkMessage(message)

// add padding if necessary
shares = addSharePadding(shares)

// organize shares for merkle mountain range
heights := PowerOf2MountainRange(uint64(len(shares)), k)
Expand Down Expand Up @@ -233,8 +236,8 @@ func CreateCommit(k uint64, namespace, message []byte) ([]byte, error) {
return merkle.HashFromByteSlices(subTreeRoots), nil
}

// ChunkMessage breaks the message into 256 byte pieces
func ChunkMessage(message []byte) [][]byte {
// chunkMessage breaks the message into 256 byte pieces
func chunkMessage(message []byte) [][]byte {
var shares [][]byte
for i := 0; i < len(message); i += ShareSize {
end := i + ShareSize
Expand All @@ -246,6 +249,22 @@ func ChunkMessage(message []byte) [][]byte {
return shares
}

// addSharePadding will add padding to the last share if necessary
func addSharePadding(shares [][]byte) [][]byte {
if len(shares) == 0 {
return shares
}

// add padding to the last share if necessary
if len(shares[len(shares)-1]) != ShareSize {
padded := make([]byte, ShareSize)
copy(padded, shares[len(shares)-1])
shares[len(shares)-1] = padded
}

return shares
}

// PowerOf2MountainRange returns the heights of the subtrees for binary merkle
// mountian range
func PowerOf2MountainRange(l, k uint64) []uint64 {
Expand Down
27 changes: 27 additions & 0 deletions x/lazyledgerapp/types/payformessage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,30 @@ func TestGetCommitmentSignBytes(t *testing.T) {
assert.Equal(t, tt.expected, res)
}
}

func TestShareChunkingAndPadding(t *testing.T) {
type test struct {
input []byte
expect [][]byte
}
tests := []test{
{
input: []byte{1},
expect: [][]byte{append([]byte{1}, bytes.Repeat([]byte{0}, ShareSize-1)...)},
},
{
input: bytes.Repeat([]byte{1}, ShareSize),
expect: [][]byte{bytes.Repeat([]byte{1}, ShareSize)},
},
}
for _, tt := range tests {
shares := chunkMessage(tt.input)
shares = addSharePadding(shares)
for _, share := range shares {
if len(share) != ShareSize {
t.Errorf("invalid share length: got %d wanted core.ShareSize (%d)", len(share), ShareSize)
}
}
assert.Equal(t, tt.expect, shares)
}
}

0 comments on commit 96ffd13

Please sign in to comment.