Pythonで有限体

ecc.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class FieldElement:
     
    def __init__(self, num, prime):
        if num >= prime or num < 0:
            error = 'Num {} not in field range 0 to {}'.format(num, prime - 1)
            raise ValueError(error)
        self.num = num
        self.prime = prime
 
    def __repr__(self):
        return 'FieldElement_{}({})'.format(self.prime, self.num)
 
    def __eq__(self, other):
        if other is None:
            return False
        return self.num == other.num and self.prime == other.prime

main.py

1
2
3
4
5
from ecc import FieldElement
a = FieldElement(7, 13)
b = FieldElement(6, 13)
print(a==b)
print(a==a)