FiniteFields.cs 1.9 KB

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