Пример #1
0
def test_point_formatter(point_formatter):
    polynomial_point = Point(
        x=Polynomial([0., 0., 1.]),
        y=Polynomial([1., 1., 1.]),
    )
    assert point_formatter.format(polynomial_point, {}) == '(4, 7)'

    int_point = Point(x=14, y=27)
    assert point_formatter.format(int_point, {}) == '(14, 27)'
Пример #2
0
def test_task_runner_config_build__gf_curve(p, curve_args, expected_curve_cls):
    config = TaskRunnerConfig(
        field_type=FieldType.GF,
        field_args=[p],
        curve_args=curve_args,
        task_configs=[],
    )
    actual = config.build_runner()
    expected = expected_curve_cls(
        Polynomial([1., 1., 1.]),
        Polynomial(polyone),
        Polynomial(polyone),
        Polynomial(polyone),
    )
    assert_curves_equals(actual.curve, expected)
Пример #3
0
def test_gf2_field_modulus():
    """
    Тест над полем с характеристикой 2 над многочленом x^4 + x + 1
    """
    def modulus_polynomial(polynomial_coef: List[float]) -> Polynomial:
        polynomial = field.modulus(Polynomial(polynomial_coef))
        return field.normalize_element(polynomial)

    field = GF2PolynomialField(Polynomial([1., 1., 0., 0., 1.]))

    assert modulus_polynomial([.0, 1.]) == Polynomial([0., 1.])
    assert modulus_polynomial([0., 0., 0., 0., 1.]) == Polynomial([1., 1.])
    assert modulus_polynomial([*([0.] * 12),
                               1.]) == Polynomial([1., 1., 1., 1.])
    assert modulus_polynomial([*([0.] * 15), 1.]) == Polynomial([1.])
Пример #4
0
def gf2_supersingular_curve():
    return GF2SupersingularCurve(
        p=Polynomial([1., 1., 0., 0., 1.]),
        a=pone(),
        b=pone(),
        c=pone(),
    )
Пример #5
0
def test_gf2_field_invert(polynomial_coef, invert_polynomial_coef):
    """
    Тест над полем с характеристикой 2 над многочленом x^4 + x + 1
    """
    def assert_invert(poly_1: Polynomial, poly_2: Polynomial):
        actual = field.invert(poly_1)
        actual = field.normalize_element(actual)

        assert poly_2 == actual

    field = GF2PolynomialField(Polynomial([1., 1., 0., 0., 1.]))

    polynomial = Polynomial(polynomial_coef)
    invert_polynomial = Polynomial(invert_polynomial_coef)

    assert_invert(polynomial, invert_polynomial)
    assert_invert(invert_polynomial, polynomial)
Пример #6
0
def test_task_runner_config_build__gf_curve__unexpected_case():
    curve_args = SUPERSINGULAR_CURVE_ARGS.copy()
    curve_args[0] = Polynomial(polyone)
    config = TaskRunnerConfig(
        field_type=FieldType.GF,
        field_args=[2],
        curve_args=curve_args,
        task_configs=[],
    )
    with pytest.raises(ValueError):
        config.build_runner()
Пример #7
0
    def parse(cls, polynomial_raw: str) -> Polynomial:
        polynomial_raw = (polynomial_raw.replace(' ',
                                                 '').replace('-',
                                                             '+').strip(' +'))

        polynomial_powers = []
        for monomial_raw in polynomial_raw.split('+'):
            monomial_raw = monomial_raw.strip()

            if monomial_raw.isnumeric():
                scalar = parse_int(monomial_raw) % 2

                if scalar != 0:
                    polynomial_powers.append(0)

                continue

            match = cls.MONOMIAL_PATTERN.match(monomial_raw)

            if match is None:
                raise PolynomialParseError(monomial_raw, polynomial_raw)

            scalar = 1
            if match.group(1) is not None:
                scalar = parse_int(match.group(1)) % 2

            power = parse_int(match.group(2) or '1')

            if scalar != 0:
                polynomial_powers.append(power)

        if len(polynomial_powers) != 0:
            polynomial_array = [0] * (max(polynomial_powers) + 1)

            for power in polynomial_powers:
                polynomial_array[power] = 1

            return Polynomial(polynomial_array)

        return Polynomial(polyzero)
Пример #8
0
def test_polynomial__sub():
    p1 = Polynomial([0., 1., 1.])
    p2 = Polynomial([0., 0., 1., 1., 1.])

    assert p1 - p2 == Polynomial([0., 1., 0., 1., 1.])
Пример #9
0
def pzero() -> Polynomial:
    return Polynomial(polyzero)
Пример #10
0
def test_polynomial__floordiv():
    p1 = Polynomial([0., 1., 1., 0., 1.])
    p2 = Polynomial([1., 0., 1.])
    p3 = Polynomial([0., 0., 1.])

    assert p1 // p2 == p3
Пример #11
0
def pone() -> Polynomial:
    return Polynomial(polyone)
def test_parse_polynomial(polynomial_raw, expected_coefficients):
    assert parse_polynomial(polynomial_raw) == Polynomial(
        expected_coefficients)
Пример #13
0
                scalar=3,
            ),
            TaskConfig(
                task_type=TaskType.MUL,
                points=(Point(2, 1), ),
                scalar=2,
            ),
        ],
    )
    assert actual == expected


@pytest.mark.parametrize(
    'irreducible_polynomial, expected_field_args',
    [
        ('x^2+x+1', [Polynomial([1., 1., 1.])]),
        ('m: 2', [2]),
    ],
)
def test_parser__parse_gf__irreducible_polynomial(irreducible_polynomial: str,
                                                  expected_field_args: list):
    input_lines = [
        'GF(2^m)',
        irreducible_polynomial,
        '1',
        '2 ',
        ' 3',
        '   4',
        '5',
        'a ( 1 , 2) (3, 4  )',
        'm (3, 4) 3',
Пример #14
0
 def zero(cls) -> Polynomial:
     return Polynomial(polyzero)
Пример #15
0
 def modulus_polynomial(polynomial_coef: List[float]) -> Polynomial:
     polynomial = field.modulus(Polynomial(polynomial_coef))
     return field.normalize_element(polynomial)
Пример #16
0
def test_polynomial__add():
    p1 = Polynomial([0., 1., 1.])
    p2 = Polynomial([0., 0., 1., 1., 1.])

    assert p1 + p2 == Polynomial([0., 1., 0., 1., 1.])
Пример #17
0
def test_polynomial__equals():
    assert Polynomial(3) == Polynomial(3)
    assert Polynomial(3) != Polynomial(4)

    with pytest.raises(ValueError):
        assert Polynomial(3) == 3
Пример #18
0
def test_parser__parse_gf__irreducible_polynomial(irreducible_polynomial: str,
                                                  expected_field_args: list):
    input_lines = [
        'GF(2^m)',
        irreducible_polynomial,
        '1',
        '2 ',
        ' 3',
        '   4',
        '5',
        'a ( 1 , 2) (3, 4  )',
        'm (3, 4) 3',
        'm 2 (2, 1)',
    ]

    parser = Parser()
    actual = parser.parse(iter(input_lines))
    expected = TaskRunnerConfig(
        field_type=FieldType.GF,
        field_args=expected_field_args,
        curve_args=[
            Polynomial([1.]),
            Polynomial([0., 1.]),
            Polynomial([1., 1.]),
            Polynomial([0., 0., 1.]),
            Polynomial([1., 0., 1.]),
        ],
        task_configs=[
            TaskConfig(
                task_type=TaskType.ADD,
                points=(
                    Point(Polynomial([1.]), Polynomial([0., 1.])),
                    Point(Polynomial([1., 1.]), Polynomial([0., 0., 1.])),
                ),
            ),
            TaskConfig(
                task_type=TaskType.MUL,
                points=(Point(
                    Polynomial([1., 1.]),
                    Polynomial([0., 0., 1.]),
                ), ),
                scalar=3,
            ),
            TaskConfig(
                task_type=TaskType.MUL,
                points=(Point(
                    Polynomial([0., 1.]),
                    Polynomial([1.]),
                ), ),
                scalar=2,
            ),
        ],
    )
    assert actual == expected
Пример #19
0
def parse_int_polynomial(number_str: str) -> Polynomial:
    return Polynomial(_parse_int(number_str))
Пример #20
0
def test_polynomial__len():
    assert len(Polynomial([1., 1., 0., 1.])) == 4
    assert len(Polynomial([0., 0., 0., 0.])) == 0
Пример #21
0
 def one(cls) -> Polynomial:
     return Polynomial(polyone)
Пример #22
0
def test_polynomial__mod():
    p1 = Polynomial([0., 1., 0., 1., 0., 1.])
    p2 = Polynomial([1., 0., 1., 0., 0., 1.])
    p3 = Polynomial([1., 1., 1., 1.])

    assert p1 % p2 == p3
Пример #23
0
def test_polynomial_formatter(polynomial_formatter):
    polynomial = Polynomial([0., 0., 1., 0., 1.])
    assert polynomial_formatter.format(polynomial, {}) == '20'
Пример #24
0
def test_polynomial__constructor():
    assert Polynomial(3).bits == 3
    assert Polynomial([1, 1]).bits == 3
    assert Polynomial([1., 1.]).bits == 3
    assert Polynomial([0., 1., 0., 1.]).bits == 10
Пример #25
0
import pytest

from src.elliptic.elliptic import Curve
from src.elliptic.elliptic import GF2NotSupersingularCurve
from src.elliptic.elliptic import GF2SupersingularCurve
from src.elliptic.elliptic import ZpCurve
from src.polynomial.polynomial import Polynomial
from src.polynomial.polynomial import polyone
from src.polynomial.polynomial import polyzero
from src.task import FieldType
from src.task import TaskRunnerConfig

NOT_SUPERSINGULAR_CURVE_ARGS = [
    Polynomial(polyone),
    Polynomial(polyzero),
    Polynomial(polyone),
    Polynomial(polyzero),
    Polynomial(polyone),
]

SUPERSINGULAR_CURVE_ARGS = [
    Polynomial(polyzero),
    Polynomial(polyone),
    Polynomial(polyzero),
    Polynomial(polyone),
    Polynomial(polyone),
]


def test_task_runner_config_build__zp_curve():
    config = TaskRunnerConfig(
Пример #26
0
def test_polynomial__lshift():
    p1 = Polynomial([0., 1., 1., 0., 1.])
    p2 = Polynomial([0., 0., 0., 1., 1., 0., 1.])

    assert p1 << 2 == p2
Пример #27
0
def test_polynomial__rshift():
    p1 = Polynomial([0., 1., 1., 0., 1.])
    p2 = Polynomial([1., 0., 1.])

    assert p1 >> 2 == p2
Пример #28
0
    [
        (Point(None, None), 2, Point(None, None)),
        (Point(1, 2), 2, Point(2, 2)),
    ],
)
def test_zp_curve__mul(zp_curve, first_point, scalar, result):
    assert zp_curve.mul(first_point, scalar) == result


@pytest.mark.parametrize(
    'first_point, second_point, result',
    [
        (Point(None, None), Point(pone(), pone()), Point(pone(), pone())),
        (Point(pone(), pone()), Point(None, None), Point(pone(), pone())),
        (
            Point(Polynomial([0., 0., 1.]), Polynomial([0., 1.])),
            Point(Polynomial([0., 0., 1.]), Polynomial([0., 1., 1.])),
            Point.infinity(),
        ),
        (
            Point(Polynomial([0., 1., 1.]), pone()),
            Point(Polynomial([0., 1., 1.]), pone()),
            Point(pone(), Polynomial([1., 1., 1.])),
        ),
        (
            Point(pone(), Polynomial([0., 1., 1.])),
            Point(Polynomial([0., 1., 1.]), Polynomial([1., 1., 1.])),
            Point(Polynomial([1., 1., 1.]), Polynomial([0., 1., 1.])),
        ),
    ],
)