-
Notifications
You must be signed in to change notification settings - Fork 307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use squares with a width that is a power of two #331
Changes from all commits
40510a7
e5a7683
4fd8936
4aa3372
98c1791
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1381,9 +1381,9 @@ func (data *Data) ComputeShares() (NamespacedShares, int) { | |
msgShares := data.Messages.splitIntoShares() | ||
curLen := len(txShares) + len(intermRootsShares) + len(evidenceShares) + len(msgShares) | ||
|
||
// FIXME(ismail): this is not a power of two | ||
// see: https://github.com/lazyledger/lazyledger-specs/issues/80 and | ||
wantLen := getNextSquareNum(curLen) | ||
// find the number of shares needed to create a square that has a power of | ||
// two width | ||
wantLen := paddedLen(curLen) | ||
|
||
// ensure that the min square size is used | ||
if wantLen < minSharecount { | ||
|
@@ -1400,10 +1400,32 @@ func (data *Data) ComputeShares() (NamespacedShares, int) { | |
tailShares...), curLen | ||
} | ||
|
||
func getNextSquareNum(length int) int { | ||
width := int(math.Ceil(math.Sqrt(float64(length)))) | ||
// TODO(ismail): make width a power of two instead | ||
return width * width | ||
// paddedLen calculates the number of shares needed to make a power of 2 square | ||
// given the current number of shares | ||
func paddedLen(length int) int { | ||
width := uint32(math.Ceil(math.Sqrt(float64(length)))) | ||
width = nextHighestPowerOf2(width) | ||
return int(width * width) | ||
} | ||
|
||
// nextPowerOf2 returns the next highest power of 2 unless the input is a power | ||
// of two, in which case it returns the input | ||
func nextHighestPowerOf2(v uint32) uint32 { | ||
if v == 0 { | ||
return 0 | ||
} | ||
|
||
// find the next highest power using bit mashing | ||
v-- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like it might not behave properly if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently input of 0 results in 0 out, should we be expecting an output of 2? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah you're right, it does output It would still be nice to maybe have an input of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added the check and renamed to |
||
v |= v >> 1 | ||
v |= v >> 2 | ||
v |= v >> 4 | ||
v |= v >> 8 | ||
v |= v >> 16 | ||
v++ | ||
|
||
// return the next highest power | ||
return v | ||
} | ||
|
||
type Message struct { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat! I'm sure there is a way to do this without floats but let's think about this later. The above is certainly clearer than before.