diff options
author | Oren Novotny <oren@novotny.org> | 2014-08-26 17:39:02 -0400 |
---|---|---|
committer | Oren Novotny <oren@novotny.org> | 2014-08-26 17:39:02 -0400 |
commit | 6dbc12162af086bbcfbd583dcaa8144d049c7fcc (patch) | |
tree | 3cf9443720ad508eb5ff615130d57fc118cb7145 /crypto/src/math/field/FiniteFields.cs | |
parent | rename Crypto dir to crypto to match bc-git (diff) | |
parent | Rework the nonce-random initialisation and avoid GenerateSeed (diff) | |
download | BouncyCastle.NET-ed25519-6dbc12162af086bbcfbd583dcaa8144d049c7fcc.tar.xz |
Merge in bc-git to this repo
Diffstat (limited to 'crypto/src/math/field/FiniteFields.cs')
-rw-r--r-- | crypto/src/math/field/FiniteFields.cs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/crypto/src/math/field/FiniteFields.cs b/crypto/src/math/field/FiniteFields.cs new file mode 100644 index 000000000..7b84569fe --- /dev/null +++ b/crypto/src/math/field/FiniteFields.cs @@ -0,0 +1,54 @@ +using System; + +namespace Org.BouncyCastle.Math.Field +{ + public abstract class FiniteFields + { + internal static readonly IFiniteField GF_2 = new PrimeField(BigInteger.ValueOf(2)); + internal static readonly IFiniteField GF_3 = new PrimeField(BigInteger.ValueOf(3)); + + public static IPolynomialExtensionField GetBinaryExtensionField(int[] exponents) + { + if (exponents[0] != 0) + { + throw new ArgumentException("Irreducible polynomials in GF(2) must have constant term", "exponents"); + } + for (int i = 1; i < exponents.Length; ++i) + { + if (exponents[i] <= exponents[i - 1]) + { + throw new ArgumentException("Polynomial exponents must be montonically increasing", "exponents"); + } + } + + return new GenericPolynomialExtensionField(GF_2, new GF2Polynomial(exponents)); + } + + // public static IPolynomialExtensionField GetTernaryExtensionField(Term[] terms) + // { + // return new GenericPolynomialExtensionField(GF_3, new GF3Polynomial(terms)); + // } + + public static IFiniteField GetPrimeField(BigInteger characteristic) + { + int bitLength = characteristic.BitLength; + if (characteristic.SignValue <= 0 || bitLength < 2) + { + throw new ArgumentException("Must be >= 2", "characteristic"); + } + + if (bitLength < 3) + { + switch (characteristic.IntValue) + { + case 2: + return GF_2; + case 3: + return GF_3; + } + } + + return new PrimeField(characteristic); + } + } +} |