@@ -86,7 +86,8 @@ const spOpcodesVersion = 11 // falcon_verify, sumhash512
86
86
// Unlimited Global Storage opcodes
87
87
const boxVersion = 8 // box_*
88
88
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
90
91
type CustomCost struct {
91
92
compute func (stack []stackValue , depth int ) int
92
93
docCost string
@@ -98,10 +99,19 @@ func bmodExpCostFunction(stack []stackValue, depth int) int {
98
99
prev := last - depth - 1 // exp
99
100
pprev := last - depth - 2 // base
100
101
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
+
101
109
expLength := float64 (len (stack [prev ].Bytes ))
102
110
modLength := float64 (len (stack [last ].Bytes ))
103
111
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
105
115
106
116
return int (cost )
107
117
}
0 commit comments