<PackageReference Include="BouncyCastle.Cryptography" Version="2.6.1" />

X9Curve

public class X9Curve : Asn1Encodable
using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; using Org.BouncyCastle.Math.Field; using System; namespace Org.BouncyCastle.Asn1.X9 { public class X9Curve : Asn1Encodable { private readonly ECCurve m_curve; private readonly DerBitString m_seed; private readonly DerObjectIdentifier m_fieldType; public ECCurve Curve => m_curve; public DerBitString Seed => m_seed; public X9Curve(ECCurve curve) : this(curve, (DerBitString)null) { } public X9Curve(ECCurve curve, byte[] seed) : this(curve, DerBitString.FromContentsOptional(seed)) { } public X9Curve(ECCurve curve, DerBitString seed) { if (curve == null) throw new ArgumentNullException("curve"); m_curve = curve; m_seed = seed; IFiniteField field = curve.Field; if (ECAlgorithms.IsFpField(field)) m_fieldType = X9ObjectIdentifiers.PrimeField; else { if (!ECAlgorithms.IsF2mField(field)) throw new ArgumentException("This type of ECCurve is not implemented"); m_fieldType = X9ObjectIdentifiers.CharacteristicTwoField; } } public X9Curve(X9FieldID fieldID, BigInteger order, BigInteger cofactor, Asn1Sequence seq) { if (fieldID == null) throw new ArgumentNullException("fieldID"); if (seq == null) throw new ArgumentNullException("seq"); m_fieldType = fieldID.FieldType; if (X9ObjectIdentifiers.PrimeField.Equals(m_fieldType)) { BigInteger value = DerInteger.GetInstance(fieldID.Parameters).Value; BigInteger a = new BigInteger(1, Asn1OctetString.GetInstance(seq[0]).GetOctets()); BigInteger b = new BigInteger(1, Asn1OctetString.GetInstance(seq[1]).GetOctets()); m_curve = new FpCurve(value, a, b, order, cofactor); } else { if (!X9ObjectIdentifiers.CharacteristicTwoField.Equals(m_fieldType)) throw new ArgumentException("This type of ECCurve is not implemented"); Asn1Sequence instance = Asn1Sequence.GetInstance(fieldID.Parameters); int intValueExact = DerInteger.GetInstance(instance[0]).IntValueExact; DerObjectIdentifier instance2 = DerObjectIdentifier.GetInstance(instance[1]); int intValueExact2; int k; int k2; if (X9ObjectIdentifiers.TPBasis.Equals(instance2)) { intValueExact2 = DerInteger.GetInstance(instance[2]).IntValueExact; k = 0; k2 = 0; } else { if (!X9ObjectIdentifiers.PPBasis.Equals(instance2)) throw new ArgumentException("This CharacteristicTwoField representation is not implemented"); Asn1Sequence instance3 = Asn1Sequence.GetInstance(instance[2]); intValueExact2 = DerInteger.GetInstance(instance3[0]).IntValueExact; k = DerInteger.GetInstance(instance3[1]).IntValueExact; k2 = DerInteger.GetInstance(instance3[2]).IntValueExact; } BigInteger a2 = new BigInteger(1, Asn1OctetString.GetInstance(seq[0]).GetOctets()); BigInteger b2 = new BigInteger(1, Asn1OctetString.GetInstance(seq[1]).GetOctets()); m_curve = new F2mCurve(intValueExact, intValueExact2, k, k2, a2, b2, order, cofactor); } if (seq.Count == 3) m_seed = DerBitString.GetInstance(seq[2]); } public byte[] GetSeed() { return m_seed?.GetBytes(); } public override Asn1Object ToAsn1Object() { X9FieldElement x9FieldElement = new X9FieldElement(m_curve.A); X9FieldElement x9FieldElement2 = new X9FieldElement(m_curve.B); if (m_seed != null) return new DerSequence(x9FieldElement, x9FieldElement2, m_seed); return new DerSequence(x9FieldElement, x9FieldElement2); } } }