summary refs log tree commit diff
path: root/crypto/src/math/ec/multiplier/WTauNafMultiplier.cs
blob: f1a6057701457262f19f0c8f364105aaf7faf8bc (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;

using Org.BouncyCastle.Math.EC.Abc;

namespace Org.BouncyCastle.Math.EC.Multiplier
{
	/**
	* Class implementing the WTNAF (Window
	* <code>&#964;</code>-adic Non-Adjacent Form) algorithm.
	*/
	internal class WTauNafMultiplier
		: ECMultiplier
	{
		/**
		* Multiplies a {@link org.bouncycastle.math.ec.F2mPoint F2mPoint}
		* by <code>k</code> using the reduced <code>&#964;</code>-adic NAF (RTNAF)
		* method.
		* @param p The F2mPoint to multiply.
		* @param k The integer by which to multiply <code>k</code>.
		* @return <code>p</code> multiplied by <code>k</code>.
		*/
		public ECPoint Multiply(ECPoint point, BigInteger k, PreCompInfo preCompInfo)
		{
			if (!(point is F2mPoint))
				throw new ArgumentException("Only F2mPoint can be used in WTauNafMultiplier");

			F2mPoint p = (F2mPoint)point;

			F2mCurve curve = (F2mCurve) p.Curve;
			int m = curve.M;
			sbyte a = (sbyte) curve.A.ToBigInteger().IntValue;
			sbyte mu = curve.GetMu();
			BigInteger[] s = curve.GetSi();

			ZTauElement rho = Tnaf.PartModReduction(k, m, a, s, mu, (sbyte)10);

			return MultiplyWTnaf(p, rho, preCompInfo, a, mu);
		}

		/**
		* Multiplies a {@link org.bouncycastle.math.ec.F2mPoint F2mPoint}
		* by an element <code>&#955;</code> of <code><b>Z</b>[&#964;]</code> using
		* the <code>&#964;</code>-adic NAF (TNAF) method.
		* @param p The F2mPoint to multiply.
		* @param lambda The element <code>&#955;</code> of
		* <code><b>Z</b>[&#964;]</code> of which to compute the
		* <code>[&#964;]</code>-adic NAF.
		* @return <code>p</code> multiplied by <code>&#955;</code>.
		*/
		private F2mPoint MultiplyWTnaf(F2mPoint p, ZTauElement lambda,
			PreCompInfo preCompInfo, sbyte a, sbyte mu)
		{
			ZTauElement[] alpha;
			if (a == 0)
			{
				alpha = Tnaf.Alpha0;
			}
			else
			{
				// a == 1
				alpha = Tnaf.Alpha1;
			}

			BigInteger tw = Tnaf.GetTw(mu, Tnaf.Width);

			sbyte[]u = Tnaf.TauAdicWNaf(mu, lambda, Tnaf.Width,
				BigInteger.ValueOf(Tnaf.Pow2Width), tw, alpha);

			return MultiplyFromWTnaf(p, u, preCompInfo);
		}
	    
		/**
		* Multiplies a {@link org.bouncycastle.math.ec.F2mPoint F2mPoint}
		* by an element <code>&#955;</code> of <code><b>Z</b>[&#964;]</code>
		* using the window <code>&#964;</code>-adic NAF (TNAF) method, given the
		* WTNAF of <code>&#955;</code>.
		* @param p The F2mPoint to multiply.
		* @param u The the WTNAF of <code>&#955;</code>..
		* @return <code>&#955; * p</code>
		*/
		private static F2mPoint MultiplyFromWTnaf(F2mPoint p, sbyte[] u,
			PreCompInfo preCompInfo)
		{
			F2mCurve curve = (F2mCurve)p.Curve;
			sbyte a = (sbyte) curve.A.ToBigInteger().IntValue;

			F2mPoint[] pu;
			if ((preCompInfo == null) || !(preCompInfo is WTauNafPreCompInfo))
			{
				pu = Tnaf.GetPreComp(p, a);
				p.SetPreCompInfo(new WTauNafPreCompInfo(pu));
			}
			else
			{
				pu = ((WTauNafPreCompInfo)preCompInfo).GetPreComp();
			}

			// q = infinity
			F2mPoint q = (F2mPoint) p.Curve.Infinity;
			for (int i = u.Length - 1; i >= 0; i--)
			{
				q = Tnaf.Tau(q);
				if (u[i] != 0)
				{
					if (u[i] > 0)
					{
						q = q.AddSimple(pu[u[i]]);
					}
					else
					{
						// u[i] < 0
						q = q.SubtractSimple(pu[-u[i]]);
					}
				}
			}

			return q;
		}
	}
}