summary refs log tree commit diff
path: root/crypto/src/math/ec/multiplier/AbstractECMultiplier.cs
blob: c2580c852f91120484a98247f9fcf201d2845035 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace Org.BouncyCastle.Math.EC.Multiplier
{
    public abstract class AbstractECMultiplier
        : ECMultiplier
    {
        public virtual ECPoint Multiply(ECPoint p, BigInteger k)
        {
            int sign = k.SignValue;
            if (sign == 0 || p.IsInfinity)
                return p.Curve.Infinity;

            ECPoint positive = MultiplyPositive(p, k.Abs());
            ECPoint result = sign > 0 ? positive : positive.Negate();

            /*
             * Although the various multipliers ought not to produce invalid output under normal
             * circumstances, a final check here is advised to guard against fault attacks.
             */
            return CheckResult(result);
        }

        protected abstract ECPoint MultiplyPositive(ECPoint p, BigInteger k);

        protected virtual ECPoint CheckResult(ECPoint p)
        {
            return ECAlgorithms.ImplCheckResult(p);
        }
    }
}