TL;DR:
Theoretical Limit: Assuming a gas cost limit of $30M$ we have the following theoretical bounds:
$$\text{BLen} \leq 75894, \quad \text{MLen} \leq 75894, \quad \text{ELen} \leq 720M + 32,$$
but there are other things to consider (e.g., see the next comment).
Practical Limit: Assuming a maximum restriction of 2MB in memory size and that the input lengths should be balanced, i.e., that $x := \text{BLen} = \text{MLen} = \text{ELen}$, we have the following practical bounds:
$$\text{BLen}, \text{MLen}, \text{ELen} \leq 1818.$$
Use Case Limit: After taking a look at projects using the modexp precompiled, we have decided to restrict the length to:
$$\text{BLen}, \text{MLen}, \text{ELen} = 32,$$
being able to handle up to 8192-bit RSA exponentiations.
Introduction
This issue aims to obtain the maximum length of the inputs (base, modulus and exponent) for which we can compute the ModExp precompiled on top of. My reasoning is as follows.
Following the formulas in EIP-2565 that define the gast cost of the ModExp precompile, we have that:
$$\text{GC}(E,\text{Blen},\text{Elen},\text{Mlen}) := \max \left( 200, \left\lfloor \frac{\text{MC}(\text{Blen},\text{Mlen})·\text{IC}(\text{Elen},\text{E})}{3} \right\rfloor \right)$$
where:
$$\text{MC}(\text{Blen},\text{Mlen}) := \left\lceil \frac{\max(\text{BLen},\text{MLen})}{8} \right\rceil ^2,$$
and
-
$\text{IC}(\text{ELen},\text{E}) := 1$ if $\text{ELen} \leq 32$ and $\text{E} = 0$ or $\text{E} = 1$;
-
$\text{IC}(\text{ELen},\text{E}) := \log_2(\text{E})$ if $\text{ELen} \leq 32$ and $\text{E} \neq 0$; and
-
$\text{IC}(\text{ELen},\text{E}) := \log_2(\text{E} \pmod{2^{256}}) + 8·(\text{ELen} - 32)$ if $\text{ELen} > 32$.
Right now, in our zkEVM we have $\text{GC} \leq 30M$ and based on this limit we are going to compute the maximum positive integer that each $\text{BLen},\text{ELen},\text{MLen}$ can take. As we will see, theses maximums are reached on worst cases and/or edge cases scenarios.
Expanding the above formula for the gas (and assuming sufficiently large inputs), we can express the gas bound as $$\text{GC} \leq \text{MC}(\text{Blen},\text{Mlen})·\text{IC}(\text{Elen},\text{E}) \leq 90M.$$
and expanding we obtain:
$$\text{GC} \leq \left( \frac{\max(\text{BLen},\text{MLen})}{8} \right) ^2·\text{IC}(\text{Elen},\text{E}) \leq 90M \quad \Longrightarrow \quad \text{GC} \leq \max(\text{BLen},\text{MLen})^2·\text{IC}(\text{Elen},\text{E}) \leq 5760M.$$
BLen and MLen
Let's focus our attention to $\text{BLen}$. To obtain its maximum, we need $\text{IC}(\text{Elen},\text{E})$ to be as low as possible, so take $E$ equal to either $0$ or $1$ (which correspond to the two "trivial" cases for ModExp). In both cases we have $\text{IC}(\text{Elen},\text{E}) = 1$ and taking a sufficiently large $\text{BLen}$ we can conclude that $\text{BLen}^2 \leq 5760M$ and therefore $\text{BLen} \leq \sqrt{5760M} \approx 75894$. That is, we should be able to handle a base $B$ of at most $2372$ chunks of $256$ bits. The exact same reasoning works for $\text{MLen}$.
ELen
To obtain the maximum for $\text{ELen}$, we should set $\max(\text{BLen},\text{MLen})^2$ as low as possible and we can make it equal to $1$ by taking any (possibly non-trivial) base and modulus satisfying $\text{BLen} = \text{MLen} = 1$. Since $\text{ELen} > 32$ we can express the initial inequality as $\log_2(\text{E} \pmod{2^{256}}) + 8·(\text{ELen} - 32) \leq 5760M.$
Now, by taking $E$ to be a multiple of $2^{256}$ we make zero the first summand of the previous expression, leaving as with the inequality $8·(\text{ELen} - 32) \leq 5760M$ and therefore $\text{ELen} \leq 720M + 32$. This is a number that is represented with at most $22.500.001$ chunks of $256$ bits.
TL;DR:$30M$ we have the following theoretical bounds:
$$\text{BLen} \leq 75894, \quad \text{MLen} \leq 75894, \quad \text{ELen} \leq 720M + 32,$$
Theoretical Limit: Assuming a gas cost limit of
but there are other things to consider (e.g., see the next comment).
Practical Limit: Assuming a maximum restriction of 2MB in memory size and that the input lengths should be balanced, i.e., that$x := \text{BLen} = \text{MLen} = \text{ELen}$ , we have the following practical bounds:
$$\text{BLen}, \text{MLen}, \text{ELen} \leq 1818.$$
Use Case Limit: After taking a look at projects using the modexp precompiled, we have decided to restrict the length to:
$$\text{BLen}, \text{MLen}, \text{ELen} = 32,$$
being able to handle up to 8192-bit RSA exponentiations.
Introduction
This issue aims to obtain the maximum length of the inputs (base, modulus and exponent) for which we can compute the ModExp precompiled on top of. My reasoning is as follows.
Following the formulas in EIP-2565 that define the gast cost of the ModExp precompile, we have that:
$$\text{GC}(E,\text{Blen},\text{Elen},\text{Mlen}) := \max \left( 200, \left\lfloor \frac{\text{MC}(\text{Blen},\text{Mlen})·\text{IC}(\text{Elen},\text{E})}{3} \right\rfloor \right)$$
$$\text{MC}(\text{Blen},\text{Mlen}) := \left\lceil \frac{\max(\text{BLen},\text{MLen})}{8} \right\rceil ^2,$$
where:
and
Right now, in our zkEVM we have$\text{GC} \leq 30M$ and based on this limit we are going to compute the maximum positive integer that each $\text{BLen},\text{ELen},\text{MLen}$ can take. As we will see, theses maximums are reached on worst cases and/or edge cases scenarios.
Expanding the above formula for the gas (and assuming sufficiently large inputs), we can express the gas bound as$$\text{GC} \leq \text{MC}(\text{Blen},\text{Mlen})·\text{IC}(\text{Elen},\text{E}) \leq 90M.$$
$$\text{GC} \leq \left( \frac{\max(\text{BLen},\text{MLen})}{8} \right) ^2·\text{IC}(\text{Elen},\text{E}) \leq 90M \quad \Longrightarrow \quad \text{GC} \leq \max(\text{BLen},\text{MLen})^2·\text{IC}(\text{Elen},\text{E}) \leq 5760M.$$
and expanding we obtain:
BLen and MLen
Let's focus our attention to$\text{BLen}$ . To obtain its maximum, we need $\text{IC}(\text{Elen},\text{E})$ to be as low as possible, so take $E$ equal to either $0$ or $1$ (which correspond to the two "trivial" cases for ModExp). In both cases we have $\text{IC}(\text{Elen},\text{E}) = 1$ and taking a sufficiently large $\text{BLen}$ we can conclude that $\text{BLen}^2 \leq 5760M$ and therefore $\text{BLen} \leq \sqrt{5760M} \approx 75894$ . That is, we should be able to handle a base $B$ of at most $2372$ chunks of $256$ bits. The exact same reasoning works for $\text{MLen}$ .
ELen
To obtain the maximum for$\text{ELen}$ , we should set $\max(\text{BLen},\text{MLen})^2$ as low as possible and we can make it equal to $1$ by taking any (possibly non-trivial) base and modulus satisfying $\text{BLen} = \text{MLen} = 1$ . Since $\text{ELen} > 32$ we can express the initial inequality as $\log_2(\text{E} \pmod{2^{256}}) + 8·(\text{ELen} - 32) \leq 5760M.$
Now, by taking$E$ to be a multiple of $2^{256}$ we make zero the first summand of the previous expression, leaving as with the inequality $8·(\text{ELen} - 32) \leq 5760M$ and therefore $\text{ELen} \leq 720M + 32$ . This is a number that is represented with at most $22.500.001$ chunks of $256$ bits.