Skip to content

Commit

Permalink
Avoid infinity appearing in lookup table for FixedPointCombMultiplier
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdettman committed May 8, 2017
1 parent ccc3d43 commit 05b83be
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected ECPoint multiplyPositive(ECPoint p, BigInteger k)
R = R.twicePlus(lookupTable[index]);
}

return R;
return R.add(info.getOffset());
}

protected int getWidthForCombSize(int combSize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
public class FixedPointPreCompInfo implements PreCompInfo
{
protected ECPoint offset = null;

/**
* Array holding the precomputed <code>ECPoint</code>s used for a fixed
* point multiplication.
Expand All @@ -18,6 +20,16 @@ public class FixedPointPreCompInfo implements PreCompInfo
*/
protected int width = -1;

public ECPoint getOffset()
{
return offset;
}

public void setOffset(ECPoint offset)
{
this.offset = offset;
}

public ECPoint[] getPreComp()
{
return preComp;
Expand Down
14 changes: 9 additions & 5 deletions core/src/main/java/org/bouncycastle/math/ec/FixedPointUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,21 @@ public static FixedPointPreCompInfo precompute(ECPoint p, int minWidth)
int bits = getCombSize(c);
int d = (bits + minWidth - 1) / minWidth;

ECPoint[] pow2Table = new ECPoint[minWidth];
ECPoint[] pow2Table = new ECPoint[minWidth + 1];
pow2Table[0] = p;
for (int i = 1; i < minWidth; ++i)
{
pow2Table[i] = pow2Table[i - 1].timesPow2(d);
}


// This will be the 'offset' value
pow2Table[minWidth] = pow2Table[0].subtract(pow2Table[1]);

c.normalizeAll(pow2Table);

lookupTable = new ECPoint[n];
lookupTable[0] = c.getInfinity();
lookupTable[0] = pow2Table[0];

for (int bit = minWidth - 1; bit >= 0; --bit)
{
ECPoint pow2 = pow2Table[bit];
Expand All @@ -60,6 +63,7 @@ public static FixedPointPreCompInfo precompute(ECPoint p, int minWidth)

c.normalizeAll(lookupTable);

info.setOffset(pow2Table[minWidth]);
info.setPreComp(lookupTable);
info.setWidth(minWidth);

Expand Down

0 comments on commit 05b83be

Please sign in to comment.