In order to emulate EC math, it seems we’ll need to implement every step in the secp256k1 group arithmetic. Roughly, that would equate to:
1. Finite‑Field Arithmetic Primitives - currently missing modular inverse, which might look something like this if we were to roll out our own implementation in Script
// Pseudocode in CashAssembly‑style
// Inputs: a, p (the prime)
// Outputs: inv = a⁻¹ mod p
// Maintains variables (a, b=p, x0=1, x1=0)
<E> OP_TOALTSTACK // E = p
<A> OP_TOALTSTACK // A = a
// x0=1, x1=0 on the stack as immediate constants
<1> <0>
// Loop until a == 1 or a == 0
OP_BEGIN
// if a == 0, fail (no inverse)
OP_DUP OP_0 OP_EQUALVERIFY
// q = b / a
OP_FROMALTSTACK OP_SWAP // bring b,a to top
OP_DUP OP_HASH256 // not needed—just illustrative
OP_DIV // q = b÷a
// (b, a) ← (a, b mod a)
OP_SWAP OP_MOD
// (x0, x1) ← (x1, x0 − q·x1)
OP_SWAP // bring x0,x1
OP_OVER OP_SWAP // stack: x0,x1|x1,x0
OP_OVER OP_MUL // compute q·x1
OP_SUB // x0 − q·x1
OP_SWAP // new x1,x0
// Continue looping while a > 1
OP_DUP OP_1 OP_GREATERTHAN OP_ENDIF // jump back if a>1
OP_UNTIL
// Now x1 holds the inverse; reduce mod p
2. Point Addition for 2 points (x1, y1), (x2, y2)
- Compute λ=(y2−y1)×inv(x2−x1)mod p\lambda = (y_2 - y_1)\times \text{inv}(x_2 - x_1)\mod pλ=(y2−y1)×inv(x2−x1)modp.
- Compute x3=λ2−x1−x2x_3 = \lambda^2 - x_1 - x_2x3=λ2−x1−x2.
- Compute y3=λ (x1−x3)−y1y_3 = \lambda,(x_1 - x_3) - y_1y3=λ(x1−x3)−y1.
Each step is multiple multiplies, subs, and an extra inverse.
3. Scalar Multiplication
// binary expansion of k
// result = ∞
// for each bit of k (most to least):
// result = ECADD(result, result) // point doubling
// if bit = 1: result = ECADD(result, P)
4. Curve‑Membership Checking
Before ever doing arithmetic, we must verify input points lie on
y2≡x3+7(modp)y^2 ≡ x^3 + 7 \pmod py2≡x3+7(modp): one OP_MUL
, one OP_SUB
, one OP_MOD
, two OP_DIV
5. Looping & Stack‑Management
We’ll need to juggle at least 8–12 “registers” of big‑ints on the stack, managing the OP_SWAP
/ OP_TOALTSTACK
choreography to keep the right values in place
I think with all these operations, we’ll easily hit the vm limit caps, increase unnecessary complexity and open DoS attack vectors with all these computations.