FiniteFields.cs 1.8 KB

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