public PreCompInfo Precompute(PreCompInfo existing) { if (existing is WTauNafPreCompInfo) { return(existing); } WTauNafPreCompInfo result = new WTauNafPreCompInfo(); result.PreComp = Tnaf.GetPreComp(m_p, m_a); return(result); }
/** * 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> */ static AbstractF2mPoint MultiplyFromWTnaf(AbstractF2mPoint p, sbyte[] u, PreCompInfo preCompInfo) { var curve = (AbstractF2mCurve)p.Curve; var 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 (var i = 0; i < pu.Length; ++i) { puNeg[i] = (AbstractF2mPoint)pu[i].Negate(); } // q = infinity var q = (AbstractF2mPoint)p.Curve.Infinity; var tauCount = 0; for (var 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); }
private static AbstractF2mPoint MultiplyFromWTnaf(AbstractF2mPoint p, sbyte[] u, PreCompInfo preCompInfo) { AbstractF2mPoint[] preComp; AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve; sbyte intValue = (sbyte)curve.A.ToBigInteger().IntValue; if ((preCompInfo == null) || !(preCompInfo is WTauNafPreCompInfo)) { preComp = Tnaf.GetPreComp(p, intValue); WTauNafPreCompInfo info = new WTauNafPreCompInfo { PreComp = preComp }; curve.SetPreCompInfo(p, PRECOMP_NAME, info); } else { preComp = ((WTauNafPreCompInfo)preCompInfo).PreComp; } AbstractF2mPoint[] pointArray2 = new AbstractF2mPoint[preComp.Length]; for (int i = 0; i < preComp.Length; i++) { pointArray2[i] = (AbstractF2mPoint)preComp[i].Negate(); } AbstractF2mPoint infinity = (AbstractF2mPoint)p.Curve.Infinity; int pow = 0; for (int j = u.Length - 1; j >= 0; j--) { pow++; int num5 = u[j]; if (num5 != 0) { infinity = infinity.TauPow(pow); pow = 0; ECPoint b = (num5 <= 0) ? pointArray2[-num5 >> 1] : preComp[num5 >> 1]; infinity = (AbstractF2mPoint)infinity.Add(b); } } if (pow > 0) { infinity = infinity.TauPow(pow); } return(infinity); }
private static AbstractF2mPoint MultiplyFromWTnaf(AbstractF2mPoint p, sbyte[] u, PreCompInfo preCompInfo) { AbstractF2mCurve abstractF2mCurve = (AbstractF2mCurve)p.Curve; sbyte a = (sbyte)abstractF2mCurve.A.ToBigInteger().IntValue; AbstractF2mPoint[] preComp; if (preCompInfo == null || !(preCompInfo is WTauNafPreCompInfo)) { preComp = Tnaf.GetPreComp(p, a); WTauNafPreCompInfo wTauNafPreCompInfo = new WTauNafPreCompInfo(); wTauNafPreCompInfo.PreComp = preComp; abstractF2mCurve.SetPreCompInfo(p, PRECOMP_NAME, wTauNafPreCompInfo); } else { preComp = ((WTauNafPreCompInfo)preCompInfo).PreComp; } AbstractF2mPoint[] array = new AbstractF2mPoint[preComp.Length]; for (int i = 0; i < preComp.Length; i++) { array[i] = (AbstractF2mPoint)preComp[i].Negate(); } AbstractF2mPoint abstractF2mPoint = (AbstractF2mPoint)p.Curve.Infinity; int num = 0; for (int num2 = u.Length - 1; num2 >= 0; num2--) { num++; int num3 = u[num2]; if (num3 != 0) { abstractF2mPoint = abstractF2mPoint.TauPow(num); num = 0; ECPoint b = ((num3 > 0) ? preComp[num3 >> 1] : array[-num3 >> 1]); abstractF2mPoint = (AbstractF2mPoint)abstractF2mPoint.Add(b); } } if (num > 0) { abstractF2mPoint = abstractF2mPoint.TauPow(num); } return(abstractF2mPoint); }
/** * Multiplies a {@link org.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 org.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, IPreCompInfo 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.PreCompInfo = 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); }