/** * Multiplies a {@link org.bouncycastle.math.ec.AbstractF2mPoint AbstractF2mPoint} * by an element <code>λ</code> of <code><b>Z</b>[τ]</code> using * the <code>τ</code>-adic NAF (TNAF) method. * @param p The AbstractF2mPoint to multiply. * @param lambda The element <code>λ</code> of * <code><b>Z</b>[τ]</code> of which to compute the * <code>[τ]</code>-adic NAF. * @return <code>p</code> multiplied by <code>λ</code>. */ private AbstractF2mPoint MultiplyWTnaf(AbstractF2mPoint p, ZTauElement lambda, PreCompInfo preCompInfo, sbyte a, sbyte mu) { ZTauElement[] alpha = (a == 0) ? Tnaf.Alpha0 : 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.AbstractF2mPoint AbstractF2mPoint} * by an element <code>λ</code> of <code><b>Z</b>[τ]</code> * using the window <code>τ</code>-adic NAF (TNAF) method, given the * WTNAF of <code>λ</code>. * @param p The AbstractF2mPoint to multiply. * @param u The the WTNAF of <code>λ</code>.. * @return <code>λ * p</code> */ private static AbstractF2mPoint MultiplyFromWTnaf(AbstractF2mPoint p, sbyte[] u, PreCompInfo preCompInfo) { AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve; sbyte a = (sbyte)curve.A.ToBigInteger().IntValue; AbstractF2mPoint[] pu; if ((preCompInfo == null) || !(preCompInfo is WTauNafPreCompInfo)) { pu = Tnaf.GetPreComp(p, a); WTauNafPreCompInfo pre = new WTauNafPreCompInfo(); pre.PreComp = pu; curve.SetPreCompInfo(p, PRECOMP_NAME, pre); } else { pu = ((WTauNafPreCompInfo)preCompInfo).PreComp; } // TODO Include negations in precomp (optionally) and use from here AbstractF2mPoint[] puNeg = new AbstractF2mPoint[pu.Length]; for (int i = 0; i < pu.Length; ++i) { puNeg[i] = (AbstractF2mPoint)pu[i].Negate(); } // q = infinity AbstractF2mPoint q = (AbstractF2mPoint) p.Curve.Infinity; int tauCount = 0; for (int i = u.Length - 1; i >= 0; i--) { ++tauCount; int ui = u[i]; if (ui != 0) { q = q.TauPow(tauCount); tauCount = 0; ECPoint x = ui > 0 ? pu[ui >> 1] : puNeg[(-ui) >> 1]; q = (AbstractF2mPoint)q.Add(x); } } if (tauCount > 0) { q = q.TauPow(tauCount); } return q; }
/** * Does the precomputation for WTNAF multiplication. * @param p The <code>ECPoint</code> for which to do the precomputation. * @param a The parameter <code>a</code> of the elliptic curve. * @return The precomputation array for <code>p</code>. */ public static AbstractF2mPoint[] GetPreComp(AbstractF2mPoint p, sbyte a) { sbyte[][] alphaTnaf = (a == 0) ? Tnaf.Alpha0Tnaf : Tnaf.Alpha1Tnaf; AbstractF2mPoint[] pu = new AbstractF2mPoint[(uint)(alphaTnaf.Length + 1) >> 1]; pu[0] = p; uint precompLen = (uint)alphaTnaf.Length; for(uint i = 3; i < precompLen; i += 2) { pu[i >> 1] = Tnaf.MultiplyFromTnaf(p, alphaTnaf[i]); } p.Curve.NormalizeAll(pu); return pu; }
/** * Multiplies a {@link NBitcoin.BouncyCastle.math.ec.AbstractF2mPoint AbstractF2mPoint} * by an element <code>λ</code> of <code><b>Z</b>[τ]</code> * using the <code>τ</code>-adic NAF (TNAF) method, given the TNAF * of <code>λ</code>. * @param p The AbstractF2mPoint to Multiply. * @param u The the TNAF of <code>λ</code>.. * @return <code>λ * p</code> */ public static AbstractF2mPoint MultiplyFromTnaf(AbstractF2mPoint p, sbyte[] u) { ECCurve curve = p.Curve; AbstractF2mPoint q = (AbstractF2mPoint)curve.Infinity; AbstractF2mPoint pNeg = (AbstractF2mPoint)p.Negate(); int tauCount = 0; for(int i = u.Length - 1; i >= 0; i--) { ++tauCount; sbyte ui = u[i]; if(ui != 0) { q = q.TauPow(tauCount); tauCount = 0; ECPoint x = ui > 0 ? p : pNeg; q = (AbstractF2mPoint)q.Add(x); } } if(tauCount > 0) { q = q.TauPow(tauCount); } return q; }
/** * Multiplies a {@link NBitcoin.BouncyCastle.math.ec.AbstractF2mPoint AbstractF2mPoint} * by an element <code>λ</code> of <code><b>Z</b>[τ]</code> * using the <code>τ</code>-adic NAF (TNAF) method. * @param p The AbstractF2mPoint to Multiply. * @param lambda The element <code>λ</code> of * <code><b>Z</b>[τ]</code>. * @return <code>λ * p</code> */ public static AbstractF2mPoint MultiplyTnaf(AbstractF2mPoint p, ZTauElement lambda) { AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve; sbyte mu = GetMu(curve.A); sbyte[] u = TauAdicNaf(mu, lambda); AbstractF2mPoint q = MultiplyFromTnaf(p, u); return q; }
/** * Multiplies a {@link NBitcoin.BouncyCastle.math.ec.AbstractF2mPoint AbstractF2mPoint} * by a <code>BigInteger</code> using the reduced <code>τ</code>-adic * NAF (RTNAF) method. * @param p The AbstractF2mPoint to Multiply. * @param k The <code>BigInteger</code> by which to Multiply <code>p</code>. * @return <code>k * p</code> */ public static AbstractF2mPoint MultiplyRTnaf(AbstractF2mPoint p, BigInteger k) { AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve; int m = curve.FieldSize; int a = curve.A.ToBigInteger().IntValue; sbyte mu = GetMu(a); BigInteger[] s = curve.GetSi(); ZTauElement rho = PartModReduction(k, m, (sbyte)a, s, mu, (sbyte)10); return MultiplyTnaf(p, rho); }
/** * Applies the operation <code>τ()</code> to an * <code>AbstractF2mPoint</code>. * @param p The AbstractF2mPoint to which <code>τ()</code> is applied. * @return <code>τ(p)</code> */ public static AbstractF2mPoint Tau(AbstractF2mPoint p) { return p.Tau(); }
/** * Applies the operation <code>τ()</code> to an * <code>AbstractF2mPoint</code>. * @param p The AbstractF2mPoint to which <code>τ()</code> is applied. * @return <code>τ(p)</code> */ public static AbstractF2mPoint Tau(AbstractF2mPoint p) { return(p.Tau()); }