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)'
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)
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.])
def gf2_supersingular_curve(): return GF2SupersingularCurve( p=Polynomial([1., 1., 0., 0., 1.]), a=pone(), b=pone(), c=pone(), )
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)
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()
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)
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.])
def pzero() -> Polynomial: return Polynomial(polyzero)
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
def pone() -> Polynomial: return Polynomial(polyone)
def test_parse_polynomial(polynomial_raw, expected_coefficients): assert parse_polynomial(polynomial_raw) == Polynomial( expected_coefficients)
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',
def zero(cls) -> Polynomial: return Polynomial(polyzero)
def modulus_polynomial(polynomial_coef: List[float]) -> Polynomial: polynomial = field.modulus(Polynomial(polynomial_coef)) return field.normalize_element(polynomial)
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.])
def test_polynomial__equals(): assert Polynomial(3) == Polynomial(3) assert Polynomial(3) != Polynomial(4) with pytest.raises(ValueError): assert Polynomial(3) == 3
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
def parse_int_polynomial(number_str: str) -> Polynomial: return Polynomial(_parse_int(number_str))
def test_polynomial__len(): assert len(Polynomial([1., 1., 0., 1.])) == 4 assert len(Polynomial([0., 0., 0., 0.])) == 0
def one(cls) -> Polynomial: return Polynomial(polyone)
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
def test_polynomial_formatter(polynomial_formatter): polynomial = Polynomial([0., 0., 1., 0., 1.]) assert polynomial_formatter.format(polynomial, {}) == '20'
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
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(
def test_polynomial__lshift(): p1 = Polynomial([0., 1., 1., 0., 1.]) p2 = Polynomial([0., 0., 0., 1., 1., 0., 1.]) assert p1 << 2 == p2
def test_polynomial__rshift(): p1 = Polynomial([0., 1., 1., 0., 1.]) p2 = Polynomial([1., 0., 1.]) assert p1 >> 2 == p2
[ (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.])), ), ], )