public Cypher GetCypher(string message) { BigInteger x = GeneralFunctions.H1hash(ID, prim); BigInteger y = x.Pow(3).Add(new BigInteger("7", 10)).Pow(2).ModInverse(prim); FpFieldElement x_Qid = new FpFieldElement(E.Q, x); FpFieldElement y_Qid = new FpFieldElement(E.Q, y); FpPoint Qid = new FpPoint(E, x_Qid, y_Qid); int r = 0; do { Random rnd = new Random(); r = rnd.Next(1, int.MaxValue - 1); } while (r == 0); FpPoint rP = (FpPoint)P.Multiply(new BigInteger(r.ToString(), 10)); BigInteger gid = GeneralFunctions.Pair(Qid, Ppub, k, prim); gid = gid.ModPow(new BigInteger(r.ToString(), 10), prim); char[] M = message.ToCharArray(); char[] cArray = new char[M.Length]; char[] hash = GeneralFunctions.H2hash(gid, prim).ToCharArray(); for (int i = 0; i < message.Length; i++) { cArray[i] = (char)(M[i] ^ hash[i % hash.Length]); } string c = new String(cArray); return new Cypher { U = rP, V = c }; }
public virtual bool Equals(FpFieldElement other) { if (q.Equals(other.q)) { return(base.Equals(other)); } return(false); }
/** * Decode a point on this curve from its ASN.1 encoding. The different * encodings are taken account of, including point compression for * <code>F<sub>p</sub><code> (X9.62 s 4.2.1 pg 17). * @return The decoded point. */ public override ECPoint DecodePoint( byte[] encoded) { ECPoint p = null; switch (encoded[0]) { // compressed case 0x02: case 0x03: int ytilde = encoded[0] & 1; byte[] i = new byte[encoded.Length - 1]; Array.Copy(encoded, 1, i, 0, i.Length); ECFieldElement x = new FpFieldElement(this.q, new BigInteger(1, i)); ECFieldElement alpha = x.Multiply(x.Square()).Add(x.Multiply(a).Add(b)); ECFieldElement beta = alpha.Sqrt(); // // if we can't find a sqrt we haven't got a point on the // curve - run! // if (beta == null) { throw new ArithmeticException("Invalid point compression"); } BigInteger betaValue = beta.ToBigInteger(); int bit0 = betaValue.TestBit(0) ? 1 : 0; if (bit0 != ytilde) { // Use the other root beta = new FpFieldElement(q, q.Subtract(betaValue)); } p = new FpPoint(this, x, beta, true); break; case 0x04: byte[] xEnc = new byte[(encoded.Length - 1) / 2]; byte[] yEnc = new byte[(encoded.Length - 1) / 2]; Array.Copy(encoded, 1, xEnc, 0, xEnc.Length); Array.Copy(encoded, xEnc.Length + 1, yEnc, 0, yEnc.Length); p = new FpPoint(this, new FpFieldElement(this.q, new BigInteger(1, xEnc)), new FpFieldElement(this.q, new BigInteger(1, yEnc))); break; default: throw new FormatException("Invalid point encoding " + encoded[0]); } return(p); }
public override bool Equals(object obj) { if (obj == this) { return(true); } FpFieldElement fpFieldElement = obj as FpFieldElement; return(fpFieldElement != null && this.Equals(fpFieldElement)); }
public FpCurve(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigInteger cofactor) : base(q) { this.m_q = q; this.m_r = FpFieldElement.CalculateResidue(q); this.m_infinity = new FpPoint(this, null, null); base.m_a = this.FromBigInteger(a); base.m_b = this.FromBigInteger(b); base.m_order = order; base.m_cofactor = cofactor; base.m_coord = 4; }
/** * Creates the points on the curve with literature values. */ internal static void createPoints() { for (int i = 0; i < pointSource.Length / 2; i++) { FpFieldElement x = new FpFieldElement(q, new BigInteger( pointSource[2 * i].ToString())); FpFieldElement y = new FpFieldElement(q, new BigInteger( pointSource[2 * i + 1].ToString())); p[i] = new FpPoint(curve, x, y); } }
public FpCurve(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigInteger cofactor) : base(FiniteFields.GetPrimeField(q)) { this.m_q = q; this.m_r = FpFieldElement.CalculateResidue(q); this.m_infinity = new FpPoint(this, null, null); this.m_a = FromBigInteger(a); this.m_b = FromBigInteger(b); this.m_order = order; this.m_cofactor = cofactor; this.m_coord = FP_DEFAULT_COORDS; }
public override bool Equals(object obj) { if (obj == this) { return(true); } FpFieldElement fpFieldElement = obj as FpFieldElement; if (fpFieldElement == null) { return(false); } return(Equals(fpFieldElement)); }
public override bool Equals(object obj) { if (obj == this) { return(true); } FpFieldElement other = obj as FpFieldElement; if (other == null) { return(false); } return(this.Equals(other)); }
/** * Decode a point on this curve from its ASN.1 encoding. The different * encodings are taken account of, including point compression for * <code>F<sub>p</sub></code> (X9.62 s 4.2.1 pg 17). * @return The decoded point. */ public override ECPoint DecodePoint( byte[] encoded) { ECPoint p = null; switch (encoded[0]) { // compressed case 0x02: case 0x03: int ytilde = encoded[0] & 1; byte[] i = new byte[encoded.Length - 1]; Array.Copy(encoded, 1, i, 0, i.Length); ECFieldElement x = new FpFieldElement(this.q, new BigInteger(1, i)); ECFieldElement alpha = x.Multiply(x.Square()).Add(x.Multiply(a).Add(b)); ECFieldElement beta = alpha.Sqrt(); // // if we can't find a sqrt we haven't got a point on the // curve - run! // if (beta == null) throw new ArithmeticException("Invalid point compression"); BigInteger betaValue = beta.ToBigInteger(); int bit0 = betaValue.TestBit(0) ? 1 : 0; if (bit0 != ytilde) { // Use the other root beta = new FpFieldElement(q, q.Subtract(betaValue)); } p = new FpPoint(this, x, beta, true); break; case 0x04: byte[] xEnc = new byte[(encoded.Length - 1) / 2]; byte[] yEnc = new byte[(encoded.Length - 1) / 2]; Array.Copy(encoded, 1, xEnc, 0, xEnc.Length); Array.Copy(encoded, xEnc.Length + 1, yEnc, 0, yEnc.Length); p = new FpPoint(this, new FpFieldElement(this.q, new BigInteger(1, xEnc)), new FpFieldElement(this.q, new BigInteger(1, yEnc))); break; default: throw new FormatException("Invalid point encoding " + encoded[0]); } return p; }
protected bool Equals( FpFieldElement other) { return(q.Equals(other.q) && base.Equals(other)); }
// D.1.4 91 /** * return a sqrt root - the routine verifies that the calculation * returns the right value - if none exists it returns null. */ public override ECFieldElement Sqrt() { if (!q.TestBit(0)) { throw Platform.CreateNotImplementedException("even value of q"); } // p mod 4 == 3 if (q.TestBit(1)) { // TODO Can this be optimised (inline the Square?) // z = g^(u+1) + p, p = 4u + 3 ECFieldElement z = new FpFieldElement(q, x.ModPow(q.ShiftRight(2).Add(BigInteger.One), q)); return(z.Square().Equals(this) ? z : null); } // p mod 4 == 1 BigInteger qMinusOne = q.Subtract(BigInteger.One); BigInteger legendreExponent = qMinusOne.ShiftRight(1); if (!(x.ModPow(legendreExponent, q).Equals(BigInteger.One))) { return(null); } BigInteger u = qMinusOne.ShiftRight(2); BigInteger k = u.ShiftLeft(1).Add(BigInteger.One); BigInteger Q = this.x; BigInteger fourQ = Q.ShiftLeft(2).Mod(q); BigInteger U, V; do { Random rand = new Random(); BigInteger P; do { P = new BigInteger(q.BitLength, rand); }while (P.CompareTo(q) >= 0 || !(P.Multiply(P).Subtract(fourQ).ModPow(legendreExponent, q).Equals(qMinusOne))); BigInteger[] result = fastLucasSequence(q, P, Q, k); U = result[0]; V = result[1]; if (V.Multiply(V).Mod(q).Equals(fourQ)) { // Integer division by 2, mod q if (V.TestBit(0)) { V = V.Add(q); } V = V.ShiftRight(1); Debug.Assert(V.Multiply(V).Mod(q).Equals(x)); return(new FpFieldElement(q, V)); } }while (U.Equals(BigInteger.One) || U.Equals(qMinusOne)); return(null); // BigInteger qMinusOne = q.Subtract(BigInteger.One); // // BigInteger legendreExponent = qMinusOne.ShiftRight(1); // if (!(x.ModPow(legendreExponent, q).Equals(BigInteger.One))) // return null; // // Random rand = new Random(); // BigInteger fourX = x.ShiftLeft(2); // // BigInteger r; // do // { // r = new BigInteger(q.BitLength, rand); // } // while (r.CompareTo(q) >= 0 // || !(r.Multiply(r).Subtract(fourX).ModPow(legendreExponent, q).Equals(qMinusOne))); // // BigInteger n1 = qMinusOne.ShiftRight(2); // BigInteger n2 = n1.Add(BigInteger.One); // // BigInteger wOne = WOne(r, x, q); // BigInteger wSum = W(n1, wOne, q).Add(W(n2, wOne, q)).Mod(q); // BigInteger twoR = r.ShiftLeft(1); // // BigInteger root = twoR.ModPow(q.Subtract(BigInteger.Two), q) // .Multiply(x).Mod(q) // .Multiply(wSum).Mod(q); // // return new FpFieldElement(q, root); }
public FpFieldElement(BigInteger q, BigInteger x) : this(q, FpFieldElement.CalculateResidue(q), x) { }
private FiniteFieldElement(FpFieldElement innerField) { _field = innerField; }
public virtual bool Equals( FpFieldElement other) { return q.Equals(other.q) && base.Equals(other); }
// D.1.4 91 /** * return a sqrt root - the routine verifies that the calculation * returns the right value - if none exists it returns null. */ public override ECFieldElement Sqrt() { if (!q.TestBit(0)) throw Platform.CreateNotImplementedException("even value of q"); // p mod 4 == 3 if (q.TestBit(1)) { // TODO Can this be optimised (inline the Square?) // z = g^(u+1) + p, p = 4u + 3 ECFieldElement z = new FpFieldElement(q, x.ModPow(q.ShiftRight(2).Add(BigInteger.One), q)); return this.Equals(z.Square()) ? z : null; } // p mod 4 == 1 BigInteger qMinusOne = q.Subtract(BigInteger.One); BigInteger legendreExponent = qMinusOne.ShiftRight(1); if (!(x.ModPow(legendreExponent, q).Equals(BigInteger.One))) return null; BigInteger u = qMinusOne.ShiftRight(2); BigInteger k = u.ShiftLeft(1).Add(BigInteger.One); BigInteger Q = this.x; BigInteger fourQ = Q.ShiftLeft(2).Mod(q); BigInteger U, V; do { Random rand = new Random(); BigInteger P; do { P = new BigInteger(q.BitLength, rand); } while (P.CompareTo(q) >= 0 || !(P.Multiply(P).Subtract(fourQ).ModPow(legendreExponent, q).Equals(qMinusOne))); BigInteger[] result = fastLucasSequence(q, P, Q, k); U = result[0]; V = result[1]; if (V.Multiply(V).Mod(q).Equals(fourQ)) { // Integer division by 2, mod q if (V.TestBit(0)) { V = V.Add(q); } V = V.ShiftRight(1); Debug.Assert(V.Multiply(V).Mod(q).Equals(x)); return new FpFieldElement(q, V); } } while (U.Equals(BigInteger.One) || U.Equals(qMinusOne)); return null; // BigInteger qMinusOne = q.Subtract(BigInteger.One); // // BigInteger legendreExponent = qMinusOne.ShiftRight(1); // if (!(x.ModPow(legendreExponent, q).Equals(BigInteger.One))) // return null; // // Random rand = new Random(); // BigInteger fourX = x.ShiftLeft(2); // // BigInteger r; // do // { // r = new BigInteger(q.BitLength, rand); // } // while (r.CompareTo(q) >= 0 // || !(r.Multiply(r).Subtract(fourX).ModPow(legendreExponent, q).Equals(qMinusOne))); // // BigInteger n1 = qMinusOne.ShiftRight(2); // BigInteger n2 = n1.Add(BigInteger.One); // // BigInteger wOne = WOne(r, x, q); // BigInteger wSum = W(n1, wOne, q).Add(W(n2, wOne, q)).Mod(q); // BigInteger twoR = r.ShiftLeft(1); // // BigInteger root = twoR.ModPow(q.Subtract(BigInteger.Two), q) // .Multiply(x).Mod(q) // .Multiply(wSum).Mod(q); // // return new FpFieldElement(q, root); }
protected override X9ECParameters CreateParameters() { // p = 2^224 - 2^96 + 1 BigInteger secp224r1P = new BigInteger( "26959946667150639794667015087019630673557916260026308143510066298881"); // a = -3, b = b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4 ECCurve secp224r1Curve = new FpCurve(secp224r1P, new BigInteger("26959946667150639794667015087019630673557916260026308143510066298878"), new BigInteger("b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4", 16)); // x = b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21 ECFieldElement secp224r1x = new FpFieldElement( secp224r1P, new BigInteger("b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21", 16)); // y = bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34 ECFieldElement secp224r1y = new FpFieldElement( secp224r1P, new BigInteger("bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34", 16)); ECPoint secp224r1BasePoint = new FpPoint( secp224r1Curve, secp224r1x, secp224r1y, false); BigInteger secp224r1n = new BigInteger("26959946667150639794667015087019625940457807714424391721682722368061"); BigInteger secp224r1h = new BigInteger("1"); //static readonly byte[] secp224r1Seed = (Hex.decode("bd71344799d5c7fcdc45b59fa3b9ab8f6a948bc5")); byte[] secp224r1Seed = null; return new X9ECParameters( secp224r1Curve, secp224r1BasePoint, secp224r1n, secp224r1h, secp224r1Seed); }
public virtual bool Equals(FpFieldElement other) => (this.q.Equals(other.q) && base.Equals((ECFieldElement)other));
public FiniteFieldElement Multiply(BigInteger x) { var xInnerField = new FpFieldElement(this._field.Q, x.ToBouncyCastleBigInteger()); var xAsFiniteFieldElement = new FiniteFieldElement(xInnerField); return Multiply(xAsFiniteFieldElement); }
public FpPoint Exctract(string ID, bool decrypt = false) { if (decrypt) { string sStr = File.ReadAllText("mk"); s = int.Parse(sStr); } // y^2 = x^3 + 117050x^2 + x BigInteger x = GeneralFunctions.H1hash(ID, p); BigInteger y = x.Pow(3).Add(x.Pow(2).Multiply(new BigInteger("117050", 10))).Add(x).Pow(2).ModInverse(p); FpFieldElement x_Qid = new FpFieldElement(q, x); FpFieldElement y_Qid = new FpFieldElement(q, y); FpPoint Qid = new FpPoint(E, x_Qid, y_Qid); FpPoint d_id = (FpPoint)Qid.Multiply(new BigInteger(s.ToString(), 10)); // privatni ključ return d_id; }
protected override X9ECParameters CreateParameters() { // p = 2^256 - 2^224 + 2^192 + 2^96 - 1 BigInteger secp256r1P = new BigInteger( "115792089210356248762697446949407573530086143415290314195533631308867097853951"); // a = -3, b = 5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b ECCurve secp256r1Curve = new FpCurve(secp256r1P, new BigInteger("115792089210356248762697446949407573530086143415290314195533631308867097853948"), new BigInteger("5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", 16)); // x = 6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296 ECFieldElement secp256r1x = new FpFieldElement( secp256r1P, new BigInteger("6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", 16)); // y = 4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5 ECFieldElement secp256r1y = new FpFieldElement( secp256r1P, new BigInteger("4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", 16)); ECPoint secp256r1BasePoint = new FpPoint( secp256r1Curve, secp256r1x, secp256r1y, false); BigInteger secp256r1n = new BigInteger("115792089210356248762697446949407573529996955224135760342422259061068512044369"); BigInteger secp256r1h = new BigInteger("1"); //static readonly byte[] secp256r1Seed = (Hex.decode("c49d360886e704936a6678e1139d26b7819f7e90")); byte[] secp256r1Seed = null; return new X9ECParameters( secp256r1Curve, secp256r1BasePoint, secp256r1n, secp256r1h, secp256r1Seed); }
public FiniteFieldElement(BigInteger value, BigInteger prime) { Org.BouncyCastle.Math.BigInteger bouncy_q = prime.ToBouncyCastleBigInteger(); Org.BouncyCastle.Math.BigInteger bouncy_x = value.ToBouncyCastleBigInteger(); _field = new FpFieldElement(bouncy_q, bouncy_x); }
protected bool Equals( FpFieldElement other) { return q.Equals(other.q) && base.Equals(other); }
protected override X9ECParameters CreateParameters() { // p = 2^384 - 2^128 - 2^96 + 2^32 - 1 BigInteger secp384r1P = new BigInteger( "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF", 16); // a, b ECCurve secp384r1Curve = new FpCurve(secp384r1P, new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC", 16), new BigInteger("B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF", 16)); // x ECFieldElement secp384r1x = new FpFieldElement( secp384r1P, new BigInteger("AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7", 16)); // y ECFieldElement secp384r1y = new FpFieldElement( secp384r1P, new BigInteger("3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F", 16)); ECPoint secp384r1BasePoint = new FpPoint( secp384r1Curve, secp384r1x, secp384r1y, false); BigInteger secp384r1n = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973", 16); BigInteger secp384r1h = BigInteger.One; //static readonly byte[] secp384r1Seed = (Hex.decode("A335926AA319A27A1D00896A6773A4827ACDAC73")); byte[] secp384r1Seed = null; return new X9ECParameters( secp384r1Curve, secp384r1BasePoint, secp384r1n, secp384r1h, secp384r1Seed); }
public virtual bool Equals( FpFieldElement other) { return(q.Equals(other.q) && base.Equals(other)); }
protected override X9ECParameters CreateParameters() { // p = 2^521 - 1 BigInteger secp521r1P = new BigInteger( "6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151"); // a = -3, b = 051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00 ECCurve secp521r1Curve = new FpCurve(secp521r1P, new BigInteger("6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057148"), new BigInteger("051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00", 16)); // x = c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66 ECFieldElement secp521r1x = new FpFieldElement( secp521r1P, new BigInteger("c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66", 16)); // y = 11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650 ECFieldElement secp521r1y = new FpFieldElement( secp521r1P, new BigInteger("11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650", 16)); ECPoint secp521r1BasePoint = new FpPoint( secp521r1Curve, secp521r1x, secp521r1y, false); BigInteger secp521r1n = new BigInteger("6864797660130609714981900799081393217269435300143305409394463459185543183397655394245057746333217197532963996371363321113864768612440380340372808892707005449"); BigInteger secp521r1h = new BigInteger("1"); //static readonly byte[] secp521r1Seed = (Hex.decode("d09e8800291cb85396cc6717393284aaa0da64ba")); byte[] secp521r1Seed = null; return new X9ECParameters( secp521r1Curve, secp521r1BasePoint, secp521r1n, secp521r1h, secp521r1Seed); }