/// <summary> /// Performs the scalar multiplication of <paramref name="N"/>*<paramref name="P"/>. /// </summary> /// <param name="N">Scalar, in binary, little-endian form.</param> /// <param name="P">Point</param> /// <param name="Normalize">If normalized output is expected.</param> /// <returns><paramref name="N"/>*<paramref name="P"/></returns> public override PointOnCurve ScalarMultiplication(byte[] N, PointOnCurve P, bool Normalize) { PointOnCurve Result = base.ScalarMultiplication(N, P, Normalize); if (Normalize) { Result.Normalize(this); } return(Result); }
/// <summary> /// Verifies a signature of <paramref name="Data"/> made by the EdDSA algorithm. /// </summary> /// <param name="Data">Payload to sign.</param> /// <param name="PublicKey">Public Key of the entity that generated the signature.</param> /// <param name="HashFunction">Hash function to use.</param> /// <param name="Curve">Elliptic curve</param> /// <param name="Signature">Signature</param> /// <returns>If the signature is valid.</returns> public static bool Verify(byte[] Data, byte[] PublicKey, HashFunction HashFunction, EdwardsCurveBase Curve, byte[] Signature) { try { int ScalarBytes = Signature.Length; if ((ScalarBytes & 1) != 0) { return(false); } ScalarBytes >>= 1; byte[] R = new byte[ScalarBytes]; Array.Copy(Signature, 0, R, 0, ScalarBytes); PointOnCurve r = Decode(R, Curve); byte[] S = new byte[ScalarBytes]; Array.Copy(Signature, ScalarBytes, S, 0, ScalarBytes); BigInteger s = EllipticCurve.ToInt(S); if (s >= Curve.Order) { return(false); } int c = Data.Length; byte[] Bin = new byte[(ScalarBytes << 1) + c]; // dom2(F, C) = blank string Array.Copy(R, 0, Bin, 0, ScalarBytes); Array.Copy(PublicKey, 0, Bin, ScalarBytes, ScalarBytes); Array.Copy(Data, 0, Bin, ScalarBytes << 1, c); // PH(M)=M byte[] h = HashFunction(Bin); BigInteger k = BigInteger.Remainder(EllipticCurve.ToInt(h), Curve.Order); PointOnCurve P1 = Curve.ScalarMultiplication(s, Curve.BasePoint, false); PointOnCurve P2 = Curve.ScalarMultiplication(k, Curve.Decode(PublicKey), false); Curve.AddTo(ref P2, r); P1.Normalize(Curve); P2.Normalize(Curve); return(P1.Equals(P2)); } catch (ArgumentException) { return(false); } }