aboutsummaryrefslogtreecommitdiff
path: root/test/ec/test_op.py
blob: a7f9cd0b77be1eb032b476592f5dcc5449fd6b4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from ast import parse
from unittest import TestCase

from parameterized import parameterized

from pyecsca.ec.mod import Mod
from pyecsca.ec.op import CodeOp, OpType


class OpTests(TestCase):

    @parameterized.expand([
        ("add", "x = a+b", "x = a+b", OpType.Add),
        ("sub", "x = a-b", "x = a-b", OpType.Sub),
        ("mul", "y = a*b", "y = a*b", OpType.Mult),
        ("div", "z = a/b", "z = a/b", OpType.Div),
        ("inv", "z = 1/b", "z = 1/b", OpType.Inv),
        ("pow", "b = a**d", "b = a^d", OpType.Pow),
        ("sqr", "b = a**2", "b = a^2", OpType.Sqr),
        ("id1", "b = 7", "b = 7", OpType.Id),
        ("id2", "b = a", "b = a", OpType.Id),
    ])
    def test_str(self, name, module, result, op_type):
        code = parse(module, mode="exec")
        op = CodeOp(code)
        self.assertEqual(str(op), result)
        self.assertEqual(op.operator, op_type)

    @parameterized.expand([
        ("add", "x = a+b", {"a": Mod(5, 21), "b": Mod(7, 21)}, Mod(12, 21)),
        ("sub", "x = a-b", {"a": Mod(7, 21), "b": Mod(5, 21)}, Mod(2, 21))
    ])
    def test_call(self, name, module, locals, result):
        code = parse(module, mode="exec")
        op = CodeOp(code)
        res = op(**locals)
        self.assertEqual(res, result)