A Galois field is finite set of elements and two operations + (addition) and × (multiplication), with the following properties:
Closure: if a and b are in the set, a+b and a×b are also in the set.
Additive identity: a+0=a
Multiplicative identity: a×1=a
Additive inverse: a+(−a)=0
Multiplicative inverse: a×a1=1
Field size is the number of elements in the set.
Elliptic curves that are defined over a finite field with a prime field size have interesting properties and are key to building of elliptic curve cryptographic protocols.
Let's define a PrimeGaloisField class that contains the intrinsic property of a finite field, prime. We also define a membership rule for a value in a given finite field, by overriding the __contains__ method.
@dataclassclassPrimeGaloisField: prime:intdef__contains__(self,field_element:"FieldElement")->bool:# called whenever you do: <FieldElement> in <PrimeGaloisField>return0<= field_element.value <self.prime
Let's also define a FieldElement class to make sure all mathematical operations are contained within a given PrimeGaloisField.
All parameters in an elliptic curve equation are actually elements in a given prime Galois field. This includes a,b,x, andy.