| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace Renci.SshNet.Security.Org.BouncyCastle.Math.Field |
| | | 4 | | { |
| | | 5 | | internal abstract class FiniteFields |
| | | 6 | | { |
| | 0 | 7 | | internal static readonly IFiniteField GF_2 = new PrimeField(BigInteger.ValueOf(2)); |
| | 0 | 8 | | internal static readonly IFiniteField GF_3 = new PrimeField(BigInteger.ValueOf(3)); |
| | | 9 | | |
| | | 10 | | public static IPolynomialExtensionField GetBinaryExtensionField(int[] exponents) |
| | 0 | 11 | | { |
| | 0 | 12 | | if (exponents[0] != 0) |
| | 0 | 13 | | { |
| | 0 | 14 | | throw new ArgumentException("Irreducible polynomials in GF(2) must have constant term", "exponents"); |
| | | 15 | | } |
| | 0 | 16 | | for (int i = 1; i < exponents.Length; ++i) |
| | 0 | 17 | | { |
| | 0 | 18 | | if (exponents[i] <= exponents[i - 1]) |
| | 0 | 19 | | { |
| | 0 | 20 | | throw new ArgumentException("Polynomial exponents must be montonically increasing", "exponents"); |
| | | 21 | | } |
| | 0 | 22 | | } |
| | | 23 | | |
| | 0 | 24 | | return new GenericPolynomialExtensionField(GF_2, new GF2Polynomial(exponents)); |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | // public static IPolynomialExtensionField GetTernaryExtensionField(Term[] terms) |
| | | 28 | | // { |
| | | 29 | | // return new GenericPolynomialExtensionField(GF_3, new GF3Polynomial(terms)); |
| | | 30 | | // } |
| | | 31 | | |
| | | 32 | | public static IFiniteField GetPrimeField(BigInteger characteristic) |
| | 3 | 33 | | { |
| | 3 | 34 | | int bitLength = characteristic.BitLength; |
| | 3 | 35 | | if (characteristic.SignValue <= 0 || bitLength < 2) |
| | 0 | 36 | | { |
| | 0 | 37 | | throw new ArgumentException("Must be >= 2", "characteristic"); |
| | | 38 | | } |
| | | 39 | | |
| | 3 | 40 | | if (bitLength < 3) |
| | 0 | 41 | | { |
| | 0 | 42 | | switch (characteristic.IntValue) |
| | | 43 | | { |
| | | 44 | | case 2: |
| | 0 | 45 | | return GF_2; |
| | | 46 | | case 3: |
| | 0 | 47 | | return GF_3; |
| | | 48 | | } |
| | 0 | 49 | | } |
| | | 50 | | |
| | 3 | 51 | | return new PrimeField(characteristic); |
| | 3 | 52 | | } |
| | | 53 | | } |
| | | 54 | | } |