Module opshin.rewrite.rewrite_import_bls12_381

Expand source code
import ast
import typing
from _ast import ImportFrom, AST, Store, Assign, Name
from dataclasses import dataclass
from enum import Enum, auto

import uplc.ast

import pluthon as plt

from frozenlist2 import frozenlist

from ..typed_ast import *
from ..type_impls import (
    ClassType,
    InstanceType,
    ByteStringInstanceType,
    FunctionType,
    AtomicType,
    UnitType,
    IntegerType,
)
from ..util import CompilingNodeTransformer, force_params, OVar, OLambda

"""
Checks that there was an import of dataclass if there are any class definitions
"""


@dataclass(frozen=True, unsafe_hash=True)
class BLS12381G1ElementType(ClassType):
    def python_type(self):
        return "BLS12381G1Element"

    def cmp(self, op: ast.cmpop, o: "Type") -> plt.AST:
        if isinstance(op, ast.Eq) and isinstance(o, BLS12381G1ElementType):
            return plt.BuiltIn(uplc.ast.BuiltInFun.Bls12_381_G1_Equal)
        raise NotImplementedError(
            f"Comparison {op.__class__.__name__} not implemented for {self.python_type()} and {o.python_type()}"
        )

    def _binop_return_type(self, binop: ast.operator, other: "Type") -> "Type":
        if isinstance(other, InstanceType):
            other = other.typ
            if isinstance(other, BLS12381G1ElementType):
                if isinstance(binop, (ast.Add, ast.Sub)):
                    return BLS12381G1ElementType()
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return BLS12381G1ElementType()
        return super()._binop_return_type(binop, other)

    def _binop_bin_fun(self, binop: ast.operator, other: "TypedAST"):
        if isinstance(other.typ, InstanceType):
            other = other.typ.typ
            if isinstance(other, BLS12381G1ElementType):
                if isinstance(binop, ast.Add):
                    return plt.Bls12_381_G1_Add
                if isinstance(binop, ast.Sub):
                    return lambda x, y: plt.Bls12_381_G1_Add(x, plt.Bls12_381_G1_Neg(y))
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return lambda x, y: plt.Bls12_381_G1_ScalarMul(y, x)
        return super()._binop_bin_fun(binop, other)

    def _rbinop_return_type(self, binop: ast.operator, other: "Type") -> "Type":
        if isinstance(other, InstanceType):
            other = other.typ
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return BLS12381G1ElementType()
        return super()._rbinop_return_type(binop, other)

    def _rbinop_bin_fun(self, binop: ast.operator, other: "TypedAST"):
        if isinstance(other.typ, InstanceType):
            other = other.typ.typ
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return plt.Bls12_381_G1_ScalarMul
        return super()._rbinop_bin_fun(binop, other)

    def attribute_type(self, attr) -> "Type":
        if attr == "compress":
            return InstanceType(FunctionType([], ByteStringInstanceType))
        return super().attribute_type(attr)

    def attribute(self, attr) -> plt.AST:
        if attr == "compress":
            return OLambda(["x", "_"], plt.Bls12_381_G1_Compress(OVar("x")))
        return super().attribute(attr)

    def _unop_return_type(self, unop: ast.unaryop) -> "Type":
        if isinstance(unop, (ast.USub, ast.UAdd)):
            return BLS12381G1ElementType()
        return super()._unop_return_type(unop)

    def _unop_fun(self, unop: ast.unaryop):
        if isinstance(unop, ast.USub):
            return plt.Bls12_381_G1_Neg
        if isinstance(unop, ast.UAdd):
            return lambda x: x
        return super()._unop_fun(unop)

    def __ge__(self, other):
        return isinstance(other, BLS12381G1ElementType)


@dataclass(frozen=True, unsafe_hash=True)
class BLS12381G2ElementType(ClassType):
    def python_type(self):
        return "BLS12381G2Element"

    def cmp(self, op: ast.cmpop, o: "Type") -> plt.AST:
        if isinstance(op, ast.Eq) and isinstance(o, BLS12381G2ElementType):
            return plt.BuiltIn(uplc.ast.BuiltInFun.Bls12_381_G2_Equal)
        raise NotImplementedError(
            f"Comparison {op.__class__.__name__} not implemented for {self.python_type()} and {o.python_type()}"
        )

    def _binop_return_type(self, binop: ast.operator, other: "Type") -> "Type":
        if isinstance(other, InstanceType):
            other = other.typ
            if isinstance(other, BLS12381G2ElementType):
                if isinstance(binop, (ast.Add, ast.Sub)):
                    return BLS12381G2ElementType()
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return BLS12381G2ElementType()
        return super()._binop_return_type(binop, other)

    def _binop_bin_fun(self, binop: ast.operator, other: "TypedAST"):
        if isinstance(other.typ, InstanceType):
            other = other.typ.typ
            if isinstance(other, BLS12381G2ElementType):
                if isinstance(binop, ast.Add):
                    return plt.Bls12_381_G2_Add
                if isinstance(binop, ast.Sub):
                    return lambda x, y: plt.Bls12_381_G2_Add(x, plt.Bls12_381_G2_Neg(y))
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return lambda x, y: plt.Bls12_381_G2_ScalarMul(y, x)
        return super()._binop_bin_fun(binop, other)

    def _rbinop_return_type(self, binop: ast.operator, other: "Type") -> "Type":
        if isinstance(other, InstanceType):
            other = other.typ
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return BLS12381G2ElementType()
        return super()._rbinop_return_type(binop, other)

    def _rbinop_bin_fun(self, binop: ast.operator, other: "TypedAST"):
        if isinstance(other.typ, InstanceType):
            other = other.typ.typ
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return plt.Bls12_381_G2_ScalarMul
        return super()._rbinop_bin_fun(binop, other)

    def attribute_type(self, attr) -> "Type":
        if attr == "compress":
            return InstanceType(FunctionType([], ByteStringInstanceType))
        return super().attribute_type(attr)

    def attribute(self, attr) -> plt.AST:
        if attr == "compress":
            return OLambda(["x", "_"], plt.Bls12_381_G2_Compress(OVar("x")))
        return super().attribute(attr)

    def _unop_return_type(self, unop: ast.unaryop) -> "Type":
        if isinstance(unop, (ast.USub, ast.UAdd)):
            return BLS12381G2ElementType()
        return super()._unop_return_type(unop)

    def _unop_fun(self, unop: ast.unaryop):
        if isinstance(unop, ast.USub):
            return plt.Bls12_381_G2_Neg
        if isinstance(unop, ast.UAdd):
            return lambda x: x
        return super()._unop_fun(unop)

    def __ge__(self, other):
        return isinstance(other, BLS12381G2ElementType)


@dataclass(frozen=True, unsafe_hash=True)
class BLS12381MlresultType(ClassType):
    def python_type(self):
        return "BLS12381MillerLoopResult"

    def _binop_return_type(self, binop: ast.operator, other: "Type") -> "Type":
        if isinstance(other, InstanceType):
            other = other.typ
            if isinstance(other, BLS12381MlresultType):
                if isinstance(binop, ast.Mult):
                    return BLS12381MlresultType()
        return super()._binop_return_type(binop, other)

    def _binop_bin_fun(self, binop: ast.operator, other: "TypedAST"):
        if isinstance(other.typ, InstanceType):
            other = other.typ.typ
            if isinstance(other, BLS12381MlresultType):
                if isinstance(binop, ast.Mult):
                    return plt.Bls12_381_MulMlResult
        return super()._binop_bin_fun(binop, other)

    def __ge__(self, other):
        return isinstance(other, BLS12381MlresultType)


BLS12381G1ElementInstance = InstanceType(BLS12381G1ElementType())
BLS12381G2ElementInstance = InstanceType(BLS12381G2ElementType())
BLS12381MlresultInstance = InstanceType(BLS12381MlresultType())

BLS12_381_ENTRIES = {
    x.python_type(): x
    for x in (
        BLS12381G1ElementType(),
        BLS12381G2ElementType(),
        BLS12381MlresultType(),
    )
}


class RewriteImportBLS12381(CompilingNodeTransformer):
    step = "Resolving imports and usage of std.bls12_381"

    def visit_ImportFrom(self, node: ImportFrom) -> typing.Union[typing.List[AST], AST]:
        if node.module != "opshin.std.bls12_381":
            return node
        additional_assigns = []
        for n in node.names:
            imported_type = BLS12_381_ENTRIES.get(n.name)
            assert (
                imported_type is not None
            ), f"Unsupported type import from bls12_381 '{n.name}"
            imported_name = n.name if n.asname is None else n.asname
            additional_assigns.append(
                Assign(
                    targets=[Name(id=imported_name, ctx=Store())],
                    value=RawPlutoExpr(expr=plt.Unit(), typ=imported_type),
                )
            )
        return additional_assigns

Classes

class BLS12381G1ElementType

BLS12381G1ElementType()

Expand source code
@dataclass(frozen=True, unsafe_hash=True)
class BLS12381G1ElementType(ClassType):
    def python_type(self):
        return "BLS12381G1Element"

    def cmp(self, op: ast.cmpop, o: "Type") -> plt.AST:
        if isinstance(op, ast.Eq) and isinstance(o, BLS12381G1ElementType):
            return plt.BuiltIn(uplc.ast.BuiltInFun.Bls12_381_G1_Equal)
        raise NotImplementedError(
            f"Comparison {op.__class__.__name__} not implemented for {self.python_type()} and {o.python_type()}"
        )

    def _binop_return_type(self, binop: ast.operator, other: "Type") -> "Type":
        if isinstance(other, InstanceType):
            other = other.typ
            if isinstance(other, BLS12381G1ElementType):
                if isinstance(binop, (ast.Add, ast.Sub)):
                    return BLS12381G1ElementType()
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return BLS12381G1ElementType()
        return super()._binop_return_type(binop, other)

    def _binop_bin_fun(self, binop: ast.operator, other: "TypedAST"):
        if isinstance(other.typ, InstanceType):
            other = other.typ.typ
            if isinstance(other, BLS12381G1ElementType):
                if isinstance(binop, ast.Add):
                    return plt.Bls12_381_G1_Add
                if isinstance(binop, ast.Sub):
                    return lambda x, y: plt.Bls12_381_G1_Add(x, plt.Bls12_381_G1_Neg(y))
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return lambda x, y: plt.Bls12_381_G1_ScalarMul(y, x)
        return super()._binop_bin_fun(binop, other)

    def _rbinop_return_type(self, binop: ast.operator, other: "Type") -> "Type":
        if isinstance(other, InstanceType):
            other = other.typ
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return BLS12381G1ElementType()
        return super()._rbinop_return_type(binop, other)

    def _rbinop_bin_fun(self, binop: ast.operator, other: "TypedAST"):
        if isinstance(other.typ, InstanceType):
            other = other.typ.typ
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return plt.Bls12_381_G1_ScalarMul
        return super()._rbinop_bin_fun(binop, other)

    def attribute_type(self, attr) -> "Type":
        if attr == "compress":
            return InstanceType(FunctionType([], ByteStringInstanceType))
        return super().attribute_type(attr)

    def attribute(self, attr) -> plt.AST:
        if attr == "compress":
            return OLambda(["x", "_"], plt.Bls12_381_G1_Compress(OVar("x")))
        return super().attribute(attr)

    def _unop_return_type(self, unop: ast.unaryop) -> "Type":
        if isinstance(unop, (ast.USub, ast.UAdd)):
            return BLS12381G1ElementType()
        return super()._unop_return_type(unop)

    def _unop_fun(self, unop: ast.unaryop):
        if isinstance(unop, ast.USub):
            return plt.Bls12_381_G1_Neg
        if isinstance(unop, ast.UAdd):
            return lambda x: x
        return super()._unop_fun(unop)

    def __ge__(self, other):
        return isinstance(other, BLS12381G1ElementType)

Ancestors

Methods

def attribute(self, attr) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.attribute

The attributes of this class. Needs to be a lambda that expects as first argument the object itself

Expand source code
def attribute(self, attr) -> plt.AST:
    if attr == "compress":
        return OLambda(["x", "_"], plt.Bls12_381_G1_Compress(OVar("x")))
    return super().attribute(attr)
def attribute_type(self, attr) ‑> Type

Inherited from: ClassType.attribute_type

The types of the named attributes of this class

Expand source code
def attribute_type(self, attr) -> "Type":
    if attr == "compress":
        return InstanceType(FunctionType([], ByteStringInstanceType))
    return super().attribute_type(attr)
def binop(self, binop: ast.operator, other: TypedAST) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.binop

Implements a binary operation between self and other

def binop_type(self, binop: ast.operator, other: Type) ‑> Type

Inherited from: ClassType.binop_type

Type of a binary operation between self and other.

def cmp(self, op: ast.cmpop, o: Type) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.cmp

The implementation of comparing this type to type o via operator op. Returns a lambda that expects as first argument the object itself and as second …

Expand source code
def cmp(self, op: ast.cmpop, o: "Type") -> plt.AST:
    if isinstance(op, ast.Eq) and isinstance(o, BLS12381G1ElementType):
        return plt.BuiltIn(uplc.ast.BuiltInFun.Bls12_381_G1_Equal)
    raise NotImplementedError(
        f"Comparison {op.__class__.__name__} not implemented for {self.python_type()} and {o.python_type()}"
    )
def constr(self) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.constr

The constructor for this class

def constr_type(self) ‑> InstanceType

Inherited from: ClassType.constr_type

The type of the constructor for this class

def copy_only_attributes(self) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.copy_only_attributes

Pluthon function that returns a copy of only the attributes of the object This can only be called for UnionType and RecordType, as such the input data …

def pluthon_type(self, skip_constructor: bool = False) ‑> str

Inherited from: ClassType.pluthon_type

Returns a representation of the type in pluthon.

def python_type(self)

Inherited from: ClassType.python_type

Returns a representation of the type in python.

Expand source code
def python_type(self):
    return "BLS12381G1Element"
def rbinop(self, binop: ast.operator, other: TypedAST) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.rbinop

Implements the reverse binary operation between self and other (other self)

def rbinop_type(self, binop: ast.operator, other: Type) ‑> Type

Inherited from: ClassType.rbinop_type

Type of the reversed binary operation between self and other (other self)

def stringify(self, recursive: bool = False) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.stringify

Returns a stringified version of the object …

def unop(self, unop: ast.unaryop) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.unop

Implements a unary operation on self

def unop_type(self, unop: ast.unaryop) ‑> Type

Inherited from: ClassType.unop_type

Type of a unary operation on self.

class BLS12381G2ElementType

BLS12381G2ElementType()

Expand source code
@dataclass(frozen=True, unsafe_hash=True)
class BLS12381G2ElementType(ClassType):
    def python_type(self):
        return "BLS12381G2Element"

    def cmp(self, op: ast.cmpop, o: "Type") -> plt.AST:
        if isinstance(op, ast.Eq) and isinstance(o, BLS12381G2ElementType):
            return plt.BuiltIn(uplc.ast.BuiltInFun.Bls12_381_G2_Equal)
        raise NotImplementedError(
            f"Comparison {op.__class__.__name__} not implemented for {self.python_type()} and {o.python_type()}"
        )

    def _binop_return_type(self, binop: ast.operator, other: "Type") -> "Type":
        if isinstance(other, InstanceType):
            other = other.typ
            if isinstance(other, BLS12381G2ElementType):
                if isinstance(binop, (ast.Add, ast.Sub)):
                    return BLS12381G2ElementType()
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return BLS12381G2ElementType()
        return super()._binop_return_type(binop, other)

    def _binop_bin_fun(self, binop: ast.operator, other: "TypedAST"):
        if isinstance(other.typ, InstanceType):
            other = other.typ.typ
            if isinstance(other, BLS12381G2ElementType):
                if isinstance(binop, ast.Add):
                    return plt.Bls12_381_G2_Add
                if isinstance(binop, ast.Sub):
                    return lambda x, y: plt.Bls12_381_G2_Add(x, plt.Bls12_381_G2_Neg(y))
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return lambda x, y: plt.Bls12_381_G2_ScalarMul(y, x)
        return super()._binop_bin_fun(binop, other)

    def _rbinop_return_type(self, binop: ast.operator, other: "Type") -> "Type":
        if isinstance(other, InstanceType):
            other = other.typ
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return BLS12381G2ElementType()
        return super()._rbinop_return_type(binop, other)

    def _rbinop_bin_fun(self, binop: ast.operator, other: "TypedAST"):
        if isinstance(other.typ, InstanceType):
            other = other.typ.typ
            if isinstance(other, IntegerType):
                if isinstance(binop, ast.Mult):
                    return plt.Bls12_381_G2_ScalarMul
        return super()._rbinop_bin_fun(binop, other)

    def attribute_type(self, attr) -> "Type":
        if attr == "compress":
            return InstanceType(FunctionType([], ByteStringInstanceType))
        return super().attribute_type(attr)

    def attribute(self, attr) -> plt.AST:
        if attr == "compress":
            return OLambda(["x", "_"], plt.Bls12_381_G2_Compress(OVar("x")))
        return super().attribute(attr)

    def _unop_return_type(self, unop: ast.unaryop) -> "Type":
        if isinstance(unop, (ast.USub, ast.UAdd)):
            return BLS12381G2ElementType()
        return super()._unop_return_type(unop)

    def _unop_fun(self, unop: ast.unaryop):
        if isinstance(unop, ast.USub):
            return plt.Bls12_381_G2_Neg
        if isinstance(unop, ast.UAdd):
            return lambda x: x
        return super()._unop_fun(unop)

    def __ge__(self, other):
        return isinstance(other, BLS12381G2ElementType)

Ancestors

Methods

def attribute(self, attr) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.attribute

The attributes of this class. Needs to be a lambda that expects as first argument the object itself

Expand source code
def attribute(self, attr) -> plt.AST:
    if attr == "compress":
        return OLambda(["x", "_"], plt.Bls12_381_G2_Compress(OVar("x")))
    return super().attribute(attr)
def attribute_type(self, attr) ‑> Type

Inherited from: ClassType.attribute_type

The types of the named attributes of this class

Expand source code
def attribute_type(self, attr) -> "Type":
    if attr == "compress":
        return InstanceType(FunctionType([], ByteStringInstanceType))
    return super().attribute_type(attr)
def binop(self, binop: ast.operator, other: TypedAST) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.binop

Implements a binary operation between self and other

def binop_type(self, binop: ast.operator, other: Type) ‑> Type

Inherited from: ClassType.binop_type

Type of a binary operation between self and other.

def cmp(self, op: ast.cmpop, o: Type) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.cmp

The implementation of comparing this type to type o via operator op. Returns a lambda that expects as first argument the object itself and as second …

Expand source code
def cmp(self, op: ast.cmpop, o: "Type") -> plt.AST:
    if isinstance(op, ast.Eq) and isinstance(o, BLS12381G2ElementType):
        return plt.BuiltIn(uplc.ast.BuiltInFun.Bls12_381_G2_Equal)
    raise NotImplementedError(
        f"Comparison {op.__class__.__name__} not implemented for {self.python_type()} and {o.python_type()}"
    )
def constr(self) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.constr

The constructor for this class

def constr_type(self) ‑> InstanceType

Inherited from: ClassType.constr_type

The type of the constructor for this class

def copy_only_attributes(self) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.copy_only_attributes

Pluthon function that returns a copy of only the attributes of the object This can only be called for UnionType and RecordType, as such the input data …

def pluthon_type(self, skip_constructor: bool = False) ‑> str

Inherited from: ClassType.pluthon_type

Returns a representation of the type in pluthon.

def python_type(self)

Inherited from: ClassType.python_type

Returns a representation of the type in python.

Expand source code
def python_type(self):
    return "BLS12381G2Element"
def rbinop(self, binop: ast.operator, other: TypedAST) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.rbinop

Implements the reverse binary operation between self and other (other self)

def rbinop_type(self, binop: ast.operator, other: Type) ‑> Type

Inherited from: ClassType.rbinop_type

Type of the reversed binary operation between self and other (other self)

def stringify(self, recursive: bool = False) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.stringify

Returns a stringified version of the object …

def unop(self, unop: ast.unaryop) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.unop

Implements a unary operation on self

def unop_type(self, unop: ast.unaryop) ‑> Type

Inherited from: ClassType.unop_type

Type of a unary operation on self.

class BLS12381MlresultType

BLS12381MlresultType()

Expand source code
@dataclass(frozen=True, unsafe_hash=True)
class BLS12381MlresultType(ClassType):
    def python_type(self):
        return "BLS12381MillerLoopResult"

    def _binop_return_type(self, binop: ast.operator, other: "Type") -> "Type":
        if isinstance(other, InstanceType):
            other = other.typ
            if isinstance(other, BLS12381MlresultType):
                if isinstance(binop, ast.Mult):
                    return BLS12381MlresultType()
        return super()._binop_return_type(binop, other)

    def _binop_bin_fun(self, binop: ast.operator, other: "TypedAST"):
        if isinstance(other.typ, InstanceType):
            other = other.typ.typ
            if isinstance(other, BLS12381MlresultType):
                if isinstance(binop, ast.Mult):
                    return plt.Bls12_381_MulMlResult
        return super()._binop_bin_fun(binop, other)

    def __ge__(self, other):
        return isinstance(other, BLS12381MlresultType)

Ancestors

Methods

def attribute(self, attr) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.attribute

The attributes of this class. Needs to be a lambda that expects as first argument the object itself

def attribute_type(self, attr) ‑> Type

Inherited from: ClassType.attribute_type

The types of the named attributes of this class

def binop(self, binop: ast.operator, other: TypedAST) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.binop

Implements a binary operation between self and other

def binop_type(self, binop: ast.operator, other: Type) ‑> Type

Inherited from: ClassType.binop_type

Type of a binary operation between self and other.

def cmp(self, op: ast.cmpop, o: Type) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.cmp

The implementation of comparing this type to type o via operator op. Returns a lambda that expects as first argument the object itself and as second …

def constr(self) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.constr

The constructor for this class

def constr_type(self) ‑> InstanceType

Inherited from: ClassType.constr_type

The type of the constructor for this class

def copy_only_attributes(self) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.copy_only_attributes

Pluthon function that returns a copy of only the attributes of the object This can only be called for UnionType and RecordType, as such the input data …

def pluthon_type(self, skip_constructor: bool = False) ‑> str

Inherited from: ClassType.pluthon_type

Returns a representation of the type in pluthon.

def python_type(self)

Inherited from: ClassType.python_type

Returns a representation of the type in python.

Expand source code
def python_type(self):
    return "BLS12381MillerLoopResult"
def rbinop(self, binop: ast.operator, other: TypedAST) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.rbinop

Implements the reverse binary operation between self and other (other self)

def rbinop_type(self, binop: ast.operator, other: Type) ‑> Type

Inherited from: ClassType.rbinop_type

Type of the reversed binary operation between self and other (other self)

def stringify(self, recursive: bool = False) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.stringify

Returns a stringified version of the object …

def unop(self, unop: ast.unaryop) ‑> pluthon.pluthon_ast.AST

Inherited from: ClassType.unop

Implements a unary operation on self

def unop_type(self, unop: ast.unaryop) ‑> Type

Inherited from: ClassType.unop_type

Type of a unary operation on self.

class RewriteImportBLS12381

A :class:NodeVisitor subclass that walks the abstract syntax tree and allows modification of nodes.

The NodeTransformer will walk the AST and use the return value of the visitor methods to replace or remove the old node. If the return value of the visitor method is None, the node will be removed from its location, otherwise it is replaced with the return value. The return value may be the original node in which case no replacement takes place.

Here is an example transformer that rewrites all occurrences of name lookups (foo) to data['foo']::

class RewriteName(NodeTransformer):

   def visit_Name(self, node):
       return Subscript(
           value=Name(id='data', ctx=Load()),
           slice=Constant(value=node.id),
           ctx=node.ctx
       )

Keep in mind that if the node you're operating on has child nodes you must either transform the child nodes yourself or call the :meth:generic_visit method for the node first.

For nodes that were part of a collection of statements (that applies to all statement nodes), the visitor may also return a list of nodes rather than just a single node.

Usually you use the transformer like this::

node = YourTransformer().visit(node)

Expand source code
class RewriteImportBLS12381(CompilingNodeTransformer):
    step = "Resolving imports and usage of std.bls12_381"

    def visit_ImportFrom(self, node: ImportFrom) -> typing.Union[typing.List[AST], AST]:
        if node.module != "opshin.std.bls12_381":
            return node
        additional_assigns = []
        for n in node.names:
            imported_type = BLS12_381_ENTRIES.get(n.name)
            assert (
                imported_type is not None
            ), f"Unsupported type import from bls12_381 '{n.name}"
            imported_name = n.name if n.asname is None else n.asname
            additional_assigns.append(
                Assign(
                    targets=[Name(id=imported_name, ctx=Store())],
                    value=RawPlutoExpr(expr=plt.Unit(), typ=imported_type),
                )
            )
        return additional_assigns

Ancestors

Class variables

var step

Methods

def visit(self, node)

Inherited from: CompilingNodeTransformer.visit

Visit a node.

def visit_ImportFrom(self, node: ast.ImportFrom) ‑> List[ast.AST] | ast.AST
Expand source code
def visit_ImportFrom(self, node: ImportFrom) -> typing.Union[typing.List[AST], AST]:
    if node.module != "opshin.std.bls12_381":
        return node
    additional_assigns = []
    for n in node.names:
        imported_type = BLS12_381_ENTRIES.get(n.name)
        assert (
            imported_type is not None
        ), f"Unsupported type import from bls12_381 '{n.name}"
        imported_name = n.name if n.asname is None else n.asname
        additional_assigns.append(
            Assign(
                targets=[Name(id=imported_name, ctx=Store())],
                value=RawPlutoExpr(expr=plt.Unit(), typ=imported_type),
            )
        )
    return additional_assigns