-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathField.cs
39 lines (31 loc) · 1.28 KB
/
Field.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
namespace Drm_Mina;
using System.Numerics;
public class Field
{
public BigInteger Value { get; }
private static BigInteger Modulus { get; } =
BigInteger.Parse("28948022309329048855892746252171976963363056481941560715954676764349967630337");
public Field(BigInteger value)
{
Value = value % Modulus;
if (Value < 0) Value += Modulus;
}
public static Field operator +(Field a, Field b) => new Field(a.Value + b.Value);
public static Field operator -(Field a, Field b) => new Field(a.Value - b.Value);
public static Field operator *(Field a, Field b) => new Field(a.Value * b.Value);
public static Field operator /(Field a, Field b) => a * b.Inverse();
public Field Inverse() => new Field(BigInteger.ModPow(Value, Modulus - 2, Modulus));
public static Field Power(Field a, BigInteger exp) =>
new Field(BigInteger.ModPow(a.Value, exp, Modulus));
public static Field Dot(Field[] a, Field[] b)
{
if (a.Length != b.Length) throw new ArgumentException("Vectors must have the same length");
Field result = new Field(0);
for (int i = 0; i < a.Length; i++)
{
result += a[i] * b[i];
}
return result;
}
public override string ToString() => Value.ToString();
}