Skip to content

Commit 468210e

Browse files
committed
refactor: Move bmodexp cost magic numbers to constants and improve comments
1 parent 0e70540 commit 468210e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

data/transactions/logic/opcodes.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ const spOpcodesVersion = 11 // falcon_verify, sumhash512
8686
// Unlimited Global Storage opcodes
8787
const boxVersion = 8 // box_*
8888

89-
// CustomCost encapsulates a custom cost function and its documentation, applicable to opcodes like bmodexp where linearCost is inadequate by itself
89+
// CustomCost encapsulates a custom cost function and its documentation,
90+
// applicable to opcodes like bmodexp where linearCost is inadequate by itself
9091
type CustomCost struct {
9192
compute func(stack []stackValue, depth int) int
9293
docCost string
@@ -98,10 +99,19 @@ func bmodExpCostFunction(stack []stackValue, depth int) int {
9899
prev := last - depth - 1 // exp
99100
pprev := last - depth - 2 // base
100101

102+
// Empirically estimated cost function constants
103+
const (
104+
exponentFactor = 1.63 // Adjusts cost of base & mod multiplication in the modexp by squaring algorithm
105+
scalingFactor = 15 // Normalization factor
106+
baseCost = 200 // Minimum cost of bmodexp
107+
)
108+
101109
expLength := float64(len(stack[prev].Bytes))
102110
modLength := float64(len(stack[last].Bytes))
103111
baseLength := float64(len(stack[pprev].Bytes))
104-
cost := (math.Pow(math.Max(baseLength, modLength), 1.63) * expLength / 15) + 200
112+
113+
// Derived from the asymptotic time complexity of the exponentiation by squaring algorithm
114+
cost := (math.Pow(math.Max(baseLength, modLength), exponentFactor) * expLength / scalingFactor) + baseCost
105115

106116
return int(cost)
107117
}

0 commit comments

Comments
 (0)