public string SingGen(byte[] h, BigInteger d) { BigInteger alpha = BigInteger.Parse(SupportEDS.DecStringFromByteArray(h)); BigInteger e = alpha % this.curv.N; if (e == 0) { e = 1; } BigInteger k = new BigInteger(); EllipticCurve_Point C = new EllipticCurve_Point(); BigInteger r = new BigInteger(); BigInteger s = new BigInteger(); do { Random rnd = new Random(); do { k = Maths.RandBigInteger(Maths.Length(this.curv.N), rnd); }while ((k < 0) || (k > this.curv.N)); curv.Mult(k, this.curv.G, ref C); r = C.X % this.curv.N; s = ((r * d) + (k * e)) % this.curv.N; }while ((r == 0) || (s == 0)); int midl = Maths.Length(this.curv.N) / 4; string Rvector = SupportEDS.Add0PaddingToString(r.ToString("X"), midl); string Svector = SupportEDS.Add0PaddingToString(s.ToString("X"), midl); return(Rvector + Svector); }
public bool SingVer(byte[] H, string sing, EllipticCurve_Point Q) { int midl = Maths.Length(this.curv.N) / 4; string Rvector = sing.Substring(0, midl); string Svector = sing.Substring(midl, midl); BigInteger r = BigInteger.Parse(SupportEDS.DecStringFromHexString(Rvector)); BigInteger s = BigInteger.Parse(SupportEDS.DecStringFromHexString(Svector)); if ((r < 1) || (r > (this.curv.N - 1)) || (s < 1) || (s > (this.curv.N - 1))) { return(false); } BigInteger alpha = BigInteger.Parse(SupportEDS.DecStringFromByteArray(H)); BigInteger e = alpha % this.curv.N; if (e == 0) { e = 1; } BigInteger v = Maths.GetInverse(e, this.curv.N); BigInteger z1 = (s * v) % this.curv.N; BigInteger z2 = this.curv.N + ((-(r * v)) % this.curv.N); EllipticCurve_Point A = new EllipticCurve_Point(); EllipticCurve_Point B = new EllipticCurve_Point(); EllipticCurve_Point C = new EllipticCurve_Point(); curv.Mult(z1, this.curv.G, ref A); curv.Mult(z2, Q, ref B); curv.Sum(A, B, ref C); BigInteger R = C.X % this.curv.N; if (R == r) { return(true); } else { return(false); } }
public static BigInteger RandBigInteger(int len, Random rnd) { int b = len; int ost = b % 8; int b_count = (b / 8) + (ost == 0 ? 0 : 1); if (b_count == 0) { return(BigInteger.Zero); } byte ml = (byte)Math.Pow(2, (b - 1) % 8); for (int i = ((b - 1) % 8) - 1; i >= 0; i--) { ml += (byte)(Math.Pow(2, i) * rnd.Next(0, 2)); } byte[] arr = new byte[b_count]; arr[0] = ml; for (int i = 1; i < b_count; i++) { arr[i] = (byte)rnd.Next(0, 256); } return(BigInteger.Parse(SupportEDS.DecStringFromByteArray(arr))); }