diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-01-22 11:40:05 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-01-22 11:40:05 +0700 |
commit | 73cb18129e632b44ddae7f9c589fd9c17c77d3ca (patch) | |
tree | cbe867e5538f2a9d37f3eadb49a4104a64261971 /crypto/src/asn1/x9/X9FieldID.cs | |
parent | Implement TwicePlus optimization in Fp curves (diff) | |
download | BouncyCastle.NET-ed25519-73cb18129e632b44ddae7f9c589fd9c17c77d3ca.tar.xz |
Use new Math.Field classes in EC curves, and avoid casting in client code
Diffstat (limited to 'crypto/src/asn1/x9/X9FieldID.cs')
-rw-r--r-- | crypto/src/asn1/x9/X9FieldID.cs | 144 |
1 files changed, 83 insertions, 61 deletions
diff --git a/crypto/src/asn1/x9/X9FieldID.cs b/crypto/src/asn1/x9/X9FieldID.cs index c51cc4df2..58823a285 100644 --- a/crypto/src/asn1/x9/X9FieldID.cs +++ b/crypto/src/asn1/x9/X9FieldID.cs @@ -1,3 +1,5 @@ +using System; + using Org.BouncyCastle.Math; namespace Org.BouncyCastle.Asn1.X9 @@ -12,80 +14,100 @@ namespace Org.BouncyCastle.Asn1.X9 private readonly DerObjectIdentifier id; private readonly Asn1Object parameters; - /** - * Constructor for elliptic curves over prime fields - * <code>F<sub>2</sub></code>. - * @param primeP The prime <code>p</code> defining the prime field. - */ - public X9FieldID( - BigInteger primeP) - { - this.id = X9ObjectIdentifiers.PrimeField; - this.parameters = new DerInteger(primeP); - } + /** + * Constructor for elliptic curves over prime fields + * <code>F<sub>2</sub></code>. + * @param primeP The prime <code>p</code> defining the prime field. + */ + public X9FieldID( + BigInteger primeP) + { + this.id = X9ObjectIdentifiers.PrimeField; + this.parameters = new DerInteger(primeP); + } - /** - * Constructor for elliptic curves over binary fields - * <code>F<sub>2<sup>m</sup></sub></code>. - * @param m The exponent <code>m</code> of - * <code>F<sub>2<sup>m</sup></sub></code>. - * @param k1 The integer <code>k1</code> where <code>x<sup>m</sup> + - * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code> - * represents the reduction polynomial <code>f(z)</code>. - * @param k2 The integer <code>k2</code> where <code>x<sup>m</sup> + - * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code> - * represents the reduction polynomial <code>f(z)</code>. - * @param k3 The integer <code>k3</code> where <code>x<sup>m</sup> + - * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code> - * represents the reduction polynomial <code>f(z)</code>.. - */ - public X9FieldID( - int m, - int k1, - int k2, - int k3) - { - this.id = X9ObjectIdentifiers.CharacteristicTwoField; + /** + * Constructor for elliptic curves over binary fields + * <code>F<sub>2<sup>m</sup></sub></code>. + * @param m The exponent <code>m</code> of + * <code>F<sub>2<sup>m</sup></sub></code>. + * @param k1 The integer <code>k1</code> where <code>x<sup>m</sup> + + * x<sup>k1</sup> + 1</code> + * represents the reduction polynomial <code>f(z)</code>. + */ + public X9FieldID(int m, int k1) + : this(m, k1, 0, 0) + { + } - Asn1EncodableVector fieldIdParams = new Asn1EncodableVector(new DerInteger(m)); + /** + * Constructor for elliptic curves over binary fields + * <code>F<sub>2<sup>m</sup></sub></code>. + * @param m The exponent <code>m</code> of + * <code>F<sub>2<sup>m</sup></sub></code>. + * @param k1 The integer <code>k1</code> where <code>x<sup>m</sup> + + * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code> + * represents the reduction polynomial <code>f(z)</code>. + * @param k2 The integer <code>k2</code> where <code>x<sup>m</sup> + + * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code> + * represents the reduction polynomial <code>f(z)</code>. + * @param k3 The integer <code>k3</code> where <code>x<sup>m</sup> + + * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code> + * represents the reduction polynomial <code>f(z)</code>.. + */ + public X9FieldID( + int m, + int k1, + int k2, + int k3) + { + this.id = X9ObjectIdentifiers.CharacteristicTwoField; + + Asn1EncodableVector fieldIdParams = new Asn1EncodableVector(new DerInteger(m)); - if (k2 == 0) - { - fieldIdParams.Add( - X9ObjectIdentifiers.TPBasis, - new DerInteger(k1)); - } - else - { - fieldIdParams.Add( - X9ObjectIdentifiers.PPBasis, - new DerSequence( - new DerInteger(k1), - new DerInteger(k2), - new DerInteger(k3))); - } + if (k2 == 0) + { + if (k3 != 0) + throw new ArgumentException("inconsistent k values"); - this.parameters = new DerSequence(fieldIdParams); - } + fieldIdParams.Add( + X9ObjectIdentifiers.TPBasis, + new DerInteger(k1)); + } + else + { + if (k2 <= k1 || k3 <= k2) + throw new ArgumentException("inconsistent k values"); - internal X9FieldID( - Asn1Sequence seq) - { - this.id = (DerObjectIdentifier) seq[0]; - this.parameters = (Asn1Object) seq[1]; - } + fieldIdParams.Add( + X9ObjectIdentifiers.PPBasis, + new DerSequence( + new DerInteger(k1), + new DerInteger(k2), + new DerInteger(k3))); + } + + this.parameters = new DerSequence(fieldIdParams); + } + + internal X9FieldID( + Asn1Sequence seq) + { + this.id = (DerObjectIdentifier) seq[0]; + this.parameters = (Asn1Object) seq[1]; + } - public DerObjectIdentifier Identifier + public DerObjectIdentifier Identifier { get { return id; } } - public Asn1Object Parameters + public Asn1Object Parameters { get { return parameters; } } - /** + /** * Produce a Der encoding of the following structure. * <pre> * FieldID ::= Sequence { @@ -96,7 +118,7 @@ namespace Org.BouncyCastle.Asn1.X9 */ public override Asn1Object ToAsn1Object() { - return new DerSequence(id, parameters); + return new DerSequence(id, parameters); } } } |