-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldElement.cs
94 lines (75 loc) · 2.35 KB
/
FieldElement.cs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Numerics;
namespace LeutgebAes.EllipticCurve
{
public class FieldElement
{
private BigInteger x, q;
public FieldElement(BigInteger q, BigInteger x)
{
this.x = x;
this.q = q;
}
public BigInteger ToBigInteger()
{
return this.x;
}
public BigInteger Value
{
get { return this.x; }
}
public static implicit operator BigInteger(FieldElement f)
{
return f.Value;
}
public static bool operator ==(FieldElement a, FieldElement b)
{
if ((object)b != null ^ (object)a != null)
return false;
return a.x == b.x && a.q == b.q;
}
public static bool operator !=(FieldElement a, FieldElement b)
{
return a.x != b.x || a.q != b.q;
}
public static FieldElement operator ~(FieldElement o)
{
return new FieldElement(o.q, Tools.SafeMod(~o.x + BigInteger.One, o.q));
}
public static FieldElement operator +(FieldElement a, FieldElement b)
{
return new FieldElement(a.q, (a.x + b.x) % a.q);
}
public static FieldElement operator -(FieldElement a, FieldElement b) {
return new FieldElement(a.q, (a.x - b.x) % a.q);
}
public static FieldElement operator *(FieldElement a, FieldElement b) {
return new FieldElement(a.q, (a.x * b.x) % a.q);
}
public static FieldElement operator /(FieldElement a, FieldElement b) {
return new FieldElement(a.q, (a.x * b.ModularInverse()) % a.q);
}
private BigInteger ModularInverse()
{
return Tools.ModularInverse(x, q);
}
public static FieldElement Dummy
{
get { return new FieldElement(BigInteger.Zero, BigInteger.Zero); }
}
public override string ToString()
{
return x.ToString();
}
public override int GetHashCode()
{
return x.GetHashCode() ^ q.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj.GetType() != this.GetType())
return false;
return this == (FieldElement)obj;
}
}
}