-
Notifications
You must be signed in to change notification settings - Fork 16
/
blob_share_commitment_rules.go
44 lines (36 loc) · 1.32 KB
/
blob_share_commitment_rules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package share
import (
math "math"
"golang.org/x/exp/constraints"
)
// BlobMinSquareSize returns the minimum square size that can contain shareCount
// number of shares.
func BlobMinSquareSize(shareCount int) int {
return RoundUpPowerOfTwo(int(math.Ceil(math.Sqrt(float64(shareCount)))))
}
// SubTreeWidth determines the maximum number of leaves per subtree in the share
// commitment over a given blob. The input should be the total number of shares
// used by that blob. The reasoning behind this algorithm is discussed in depth
// in ADR013
// (celestia-app/docs/architecture/adr-013-non-interative-default-rules-for-zero-padding).
func SubTreeWidth(shareCount, subtreeRootThreshold int) int {
// per ADR013, we use a predetermined threshold to determine width of sub
// trees used to create share commitments
s := (shareCount / subtreeRootThreshold)
// round up if the width is not an exact multiple of the threshold
if shareCount%subtreeRootThreshold != 0 {
s++
}
// use a power of two equal to or larger than the multiple of the subtree
// root threshold
s = RoundUpPowerOfTwo(s)
// use the minimum of the subtree width and the min square size, this
// gurarantees that a valid value is returned
return min(s, BlobMinSquareSize(shareCount))
}
func min[T constraints.Integer](i, j T) T {
if i < j {
return i
}
return j
}