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

SecP256K1FieldElement

using Org.BouncyCastle.Math.Raw; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; using System; namespace Org.BouncyCastle.Math.EC.Custom.Sec { internal class SecP256K1FieldElement : AbstractFpFieldElement { public static readonly BigInteger Q = new BigInteger(1, Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F")); protected internal readonly uint[] x; public override bool IsZero => Nat256.IsZero(x); public override bool IsOne => Nat256.IsOne(x); public override string FieldName => "SecP256K1Field"; public override int FieldSize => Q.BitLength; public SecP256K1FieldElement(BigInteger x) { if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0) throw new ArgumentException("value invalid for SecP256K1FieldElement", "x"); this.x = SecP256K1Field.FromBigInteger(x); } public SecP256K1FieldElement() { x = Nat256.Create(); } protected internal SecP256K1FieldElement(uint[] x) { this.x = x; } public override bool TestBitZero() { return Nat256.GetBit(x, 0) == 1; } public override BigInteger ToBigInteger() { return Nat256.ToBigInteger(x); } public override ECFieldElement Add(ECFieldElement b) { uint[] z = Nat256.Create(); SecP256K1Field.Add(x, ((SecP256K1FieldElement)b).x, z); return new SecP256K1FieldElement(z); } public override ECFieldElement AddOne() { uint[] z = Nat256.Create(); SecP256K1Field.AddOne(x, z); return new SecP256K1FieldElement(z); } public override ECFieldElement Subtract(ECFieldElement b) { uint[] z = Nat256.Create(); SecP256K1Field.Subtract(x, ((SecP256K1FieldElement)b).x, z); return new SecP256K1FieldElement(z); } public override ECFieldElement Multiply(ECFieldElement b) { uint[] z = Nat256.Create(); SecP256K1Field.Multiply(x, ((SecP256K1FieldElement)b).x, z); return new SecP256K1FieldElement(z); } public override ECFieldElement Divide(ECFieldElement b) { uint[] z = Nat256.Create(); SecP256K1Field.Inv(((SecP256K1FieldElement)b).x, z); SecP256K1Field.Multiply(z, x, z); return new SecP256K1FieldElement(z); } public override ECFieldElement Negate() { uint[] z = Nat256.Create(); SecP256K1Field.Negate(x, z); return new SecP256K1FieldElement(z); } public override ECFieldElement Square() { uint[] z = Nat256.Create(); SecP256K1Field.Square(x, z); return new SecP256K1FieldElement(z); } public override ECFieldElement Invert() { uint[] z = Nat256.Create(); SecP256K1Field.Inv(x, z); return new SecP256K1FieldElement(z); } public override ECFieldElement Sqrt() { uint[] array = x; if (Nat256.IsZero(array) || Nat256.IsOne(array)) return this; uint[] tt = Nat256.CreateExt(); uint[] array2 = Nat256.Create(); SecP256K1Field.Square(array, array2, tt); SecP256K1Field.Multiply(array2, array, array2, tt); uint[] array3 = Nat256.Create(); SecP256K1Field.Square(array2, array3, tt); SecP256K1Field.Multiply(array3, array, array3, tt); uint[] array4 = Nat256.Create(); SecP256K1Field.SquareN(array3, 3, array4, tt); SecP256K1Field.Multiply(array4, array3, array4, tt); uint[] array5 = array4; SecP256K1Field.SquareN(array4, 3, array5, tt); SecP256K1Field.Multiply(array5, array3, array5, tt); uint[] array6 = array5; SecP256K1Field.SquareN(array5, 2, array6, tt); SecP256K1Field.Multiply(array6, array2, array6, tt); uint[] array7 = Nat256.Create(); SecP256K1Field.SquareN(array6, 11, array7, tt); SecP256K1Field.Multiply(array7, array6, array7, tt); uint[] array8 = array6; SecP256K1Field.SquareN(array7, 22, array8, tt); SecP256K1Field.Multiply(array8, array7, array8, tt); uint[] array9 = Nat256.Create(); SecP256K1Field.SquareN(array8, 44, array9, tt); SecP256K1Field.Multiply(array9, array8, array9, tt); uint[] z = Nat256.Create(); SecP256K1Field.SquareN(array9, 88, z, tt); SecP256K1Field.Multiply(z, array9, z, tt); uint[] z2 = array9; SecP256K1Field.SquareN(z, 44, z2, tt); SecP256K1Field.Multiply(z2, array8, z2, tt); uint[] array10 = array8; SecP256K1Field.SquareN(z2, 3, array10, tt); SecP256K1Field.Multiply(array10, array3, array10, tt); uint[] z3 = array10; SecP256K1Field.SquareN(z3, 23, z3, tt); SecP256K1Field.Multiply(z3, array7, z3, tt); SecP256K1Field.SquareN(z3, 6, z3, tt); SecP256K1Field.Multiply(z3, array2, z3, tt); SecP256K1Field.SquareN(z3, 2, z3, tt); uint[] array11 = array2; SecP256K1Field.Square(z3, array11, tt); if (!Nat256.Eq(array, array11)) return null; return new SecP256K1FieldElement(z3); } public override bool Equals(object obj) { return Equals(obj as SecP256K1FieldElement); } public override bool Equals(ECFieldElement other) { return Equals(other as SecP256K1FieldElement); } public virtual bool Equals(SecP256K1FieldElement other) { if (this == other) return true; if (other == null) return false; return Nat256.Eq(x, other.x); } public override int GetHashCode() { return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 8); } } }