/** * Multiplies a {@link NBitcoin.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) { var curve = (AbstractF2mCurve)p.Curve; sbyte a = (sbyte)curve.A.ToBigInteger().IntValue; AbstractF2mPoint[] pu; if ((preCompInfo == null) || !(preCompInfo is WTauNafPreCompInfo)) { pu = Tnaf.GetPreComp(p, a); var 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 var puNeg = new AbstractF2mPoint[pu.Length]; for (int i = 0; i < pu.Length; ++i) { puNeg[i] = (AbstractF2mPoint)pu[i].Negate(); } // q = infinity var 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); }
/** * Multiplies a {@link NBitcoin.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; }
/** * Multiplies a {@link NBitcoin.BouncyCastle.math.ec.F2mPoint F2mPoint} * 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 F2mPoint to multiply. * @param u The the WTNAF of <code>λ</code>.. * @return <code>λ * 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); WTauNafPreCompInfo pre = new WTauNafPreCompInfo(); pre.PreComp = pu; curve.SetPreCompInfo(p, PRECOMP_NAME, pre); } else { pu = ((WTauNafPreCompInfo)preCompInfo).PreComp; } // q = infinity F2mPoint q = (F2mPoint)curve.Infinity; for (int i = u.Length - 1; i >= 0; i--) { q = Tnaf.Tau(q); sbyte ui = u[i]; if (ui != 0) { if (ui > 0) { q = q.AddSimple(pu[ui]); } else { // u[i] < 0 q = q.SubtractSimple(pu[-ui]); } } } return(q); }
/** * Multiplies a {@link NBitcoin.BouncyCastle.math.ec.F2mPoint F2mPoint} * 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 F2mPoint to multiply. * @param u The the WTNAF of <code>λ</code>.. * @return <code>λ * 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); WTauNafPreCompInfo pre = new WTauNafPreCompInfo(); pre.PreComp = pu; curve.SetPreCompInfo(p, PRECOMP_NAME, pre); } else { pu = ((WTauNafPreCompInfo)preCompInfo).PreComp; } // q = infinity F2mPoint q = (F2mPoint)curve.Infinity; for (int i = u.Length - 1; i >= 0; i--) { q = Tnaf.Tau(q); sbyte ui = u[i]; if (ui != 0) { if (ui > 0) { q = q.AddSimple(pu[ui]); } else { // u[i] < 0 q = q.SubtractSimple(pu[-ui]); } } } return q; }