<jupyter_start><jupyter_text>Ladder and DifferentialAddition formula unrolling<jupyter_code>from pyecsca.ec.point import Point, InfinityPoint
from pyecsca.ec.mod import Mod, SymbolicMod
from pyecsca.ec.model import ShortWeierstrassModel, EdwardsModel, MontgomeryModel
from pyecsca.ec.coordinates import AffineCoordinateModel
from pyecsca.ec.context import local, DefaultContext
from pyecsca.ec.formula import LadderFormula, DifferentialAdditionFormula
from pyecsca.misc.cfg import getconfig
from sympy import symbols, Mod as SympyMod
import warnings
def remove_mod(expression):
    if isinstance(expression, SympyMod):
        return remove_mod(expression.args[0])
    if not expression.args:
        return expression
    args = []
    for arg in expression.args:
        if isinstance(arg, SympyMod):
            arg = remove_mod(arg.args[0])
        else:
            arg = remove_mod(arg)
        args.append(arg)
    return expression.func(*args)<jupyter_output><empty_output><jupyter_text>Short Weierstrass<jupyter_code>model = ShortWeierstrassModel()
coords = model.coordinates["xz"]
acoords = AffineCoordinateModel(model)
xd, zd, xp, zp, xq, zq = symbols("X1 Z1 X2 Z2 X3 Z3")
a, b = symbols("a b")
p = 53  # This number is unused, but pyecsca needs some prime to be the modulus even for symbolic exec
getconfig().ec.unsatisfied_formula_assumption_action = "warning"
PmQ = Point(coords, X=SymbolicMod(xd, p), Z=SymbolicMod(zd, p))
P = Point(coords, X=SymbolicMod(xp, p), Z=SymbolicMod(zp, p))
Q = Point(coords, X=SymbolicMod(xq, p), Z=SymbolicMod(zq, p))
for name, formula in coords.formulas.items():
    if not isinstance(formula, (LadderFormula, DifferentialAdditionFormula)):
        continue
    print(f"--- {name} ---")
    with warnings.catch_warnings(record=True) as w:
        with local(DefaultContext()) as ctx:
            res = formula(p, PmQ, P, Q, a=SymbolicMod(a, p), b=SymbolicMod(b, p))[0]
        action, _ = ctx.actions.get_by_index([0])
        for name, values in action.intermediates.items():
            for value in values:
                if name in formula.outputs:  # Remove this test if you want the output of all intermediates.
                    print(f"{name} =", remove_mod(value.value.x))
    if w:
        print(w[0].message)
    print()<jupyter_output><empty_output><jupyter_text>Edwards<jupyter_code>model = EdwardsModel()
coords = model.coordinates["yz"]
acoords = AffineCoordinateModel(model)
yd, zd, yp, zp, yq, zq = symbols("Y1 Z1 Y2 Z2 Y3 Z3")
c, r, d = symbols("c r d")
p = 53  # This number is unused, but pyecsca needs some prime to be the modulus even for symbolic exec
getconfig().ec.unsatisfied_formula_assumption_action = "warning"
PmQ = Point(coords, Y=SymbolicMod(yd, p), Z=SymbolicMod(zd, p))
P = Point(coords, Y=SymbolicMod(yp, p), Z=SymbolicMod(zp, p))
Q = Point(coords, Y=SymbolicMod(yq, p), Z=SymbolicMod(zq, p))
for name, formula in coords.formulas.items():
    if not isinstance(formula, (LadderFormula)):
        continue
    print(f"--- {name} ---")
    with warnings.catch_warnings(record=True) as w:
        with local(DefaultContext()) as ctx:
            res = formula(p, PmQ, P, Q, c=SymbolicMod(c, p), r=SymbolicMod(r, p), d=SymbolicMod(d, p))[0]
        action, _ = ctx.actions.get_by_index([0])
        for name, values in action.intermediates.items():
            for value in values:
                if name in formula.outputs:  # Remove this test if you want the output of all intermediates.
                    print(f"{name} =", remove_mod(value.value.x))
    if w:
        print(w[0].message)
    print()<jupyter_output><empty_output><jupyter_text>Montgomery<jupyter_code>model = MontgomeryModel()
coords = model.coordinates["xz"]
acoords = AffineCoordinateModel(model)
xd, zd, xp, zp, xq, zq = symbols("X1 Z1 X2 Z2 X3 Z3")
a, b = symbols("a b")
p = 53  # This number is unused, but pyecsca needs some prime to be the modulus even for symbolic exec
getconfig().ec.unsatisfied_formula_assumption_action = "warning"
PmQ = Point(coords, X=SymbolicMod(xd, p), Z=SymbolicMod(zd, p))
P = Point(coords, X=SymbolicMod(xp, p), Z=SymbolicMod(zp, p))
Q = Point(coords, X=SymbolicMod(xq, p), Z=SymbolicMod(zq, p))
for name, formula in coords.formulas.items():
    if not isinstance(formula, (LadderFormula, DifferentialAdditionFormula)):
        continue
    print(f"--- {name} ---")
    with warnings.catch_warnings(record=True) as w:
        with local(DefaultContext()) as ctx:
            res = formula(p, PmQ, P, Q, a=SymbolicMod(a, p), b=SymbolicMod(b, p))[0]
        action, _ = ctx.actions.get_by_index([0])
        for name, values in action.intermediates.items():
            for value in values:
                if name in formula.outputs:  # Remove this test if you want the output of all intermediates.
                    print(f"{name} =", remove_mod(value.value.x))
    if w:
        print(w[0].message)
    print()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1231237322515213, 'fraction_numerical': 0.007099391480730223, 'mean_word_length': 3.7734753146176185, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '617171', 'license': 'MIT', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1690, 'n_words': 445, 'path': 'unrolling/ladd_unroll.ipynb', 'repo_name': 'crocs-muni/formula-for-disaster', 'size': '4930'}