Skip to content

Commit

Permalink
Add mroe Verify() checks to points
Browse files Browse the repository at this point in the history
  • Loading branch information
Coding-Enthusiast committed Jan 1, 2024
1 parent e87c069 commit faa6ce3
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 12 deletions.
39 changes: 34 additions & 5 deletions Src/Autarkysoft.Bitcoin/Cryptography/EllipticCurve/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ public static bool IsFracOnCurve(in UInt256_10x26 xn, in UInt256_10x26 xd)
/// <returns></returns>
public bool IsValidVar()
{
#if DEBUG
Verify();
#endif
if (isInfinity)
{
return false;
Expand All @@ -462,9 +465,16 @@ public bool IsValidVar()
/// <returns>-P</returns>
public Point Negate()
{
#if DEBUG
Verify();
#endif
UInt256_10x26 yNorm = y.NormalizeWeak();
UInt256_10x26 yNeg = yNorm.Negate(1);
return new Point(x, yNeg, isInfinity);
Point result = new Point(x, yNeg, isInfinity);
#if DEBUG
result.Verify();
#endif
return result;
}

/// <summary>
Expand All @@ -476,8 +486,8 @@ public Point MulLambda()
#if DEBUG
Verify();
#endif
var rx = x.Multiply(UInt256_10x26.Beta);
var r = new Point(rx, y, isInfinity);
UInt256_10x26 rx = x.Multiply(UInt256_10x26.Beta);
Point r = new Point(rx, y, isInfinity);
#if DEBUG
r.Verify();
#endif
Expand Down Expand Up @@ -516,21 +526,33 @@ public Span<byte> ToByteArray(bool compressed)
internal Point ToPointZInv(in UInt256_10x26 zi)
{
#if DEBUG
Verify();
zi.Verify();
Debug.Assert(!isInfinity);
#endif
UInt256_10x26 zi2 = zi.Sqr();
UInt256_10x26 zi3 = zi2 * zi;
UInt256_10x26 rx = x * zi2;
UInt256_10x26 ry = y * zi3;
return new Point(rx, ry, isInfinity);
Point result = new Point(rx, ry, isInfinity);
#if DEBUG
result.Verify();
#endif
return result;
}


/// <summary>
/// Converts this instance in affine coordinates to point in jacobian coordinates
/// </summary>
public PointJacobian ToPointJacobian() => new PointJacobian(x, y, UInt256_10x26.One, isInfinity);
public PointJacobian ToPointJacobian()
{
PointJacobian result = new PointJacobian(x, y, UInt256_10x26.One, isInfinity);
#if DEBUG
result.Verify();
#endif
return result;
}

/// <summary>
/// Converts this instance to a <see cref="PointStorage"/>.
Expand All @@ -539,7 +561,10 @@ internal Point ToPointZInv(in UInt256_10x26 zi)
/// <returns>Result</returns>
public PointStorage ToStorage()
{
#if DEBUG
Verify();
Debug.Assert(!isInfinity);
#endif
return new PointStorage(x, y);
}

Expand All @@ -552,6 +577,10 @@ public PointStorage ToStorage()
/// <returns>True if the two points are equal; otherwise false.</returns>
public bool EqualsVar(in Point other)
{
#if DEBUG
Verify();
other.Verify();
#endif
if (isInfinity != other.isInfinity)
{
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,21 @@ public PointJacobian DoubleVar(out UInt256_10x26 rzr)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PointJacobian CMov(in PointJacobian r, in PointJacobian a, uint flag)
{
#if DEBUG
r.Verify();
a.Verify();
#endif
UInt256_10x26 rx = UInt256_10x26.CMov(r.x, a.x, flag);
UInt256_10x26 ry = UInt256_10x26.CMov(r.y, a.y, flag);
UInt256_10x26 rz = UInt256_10x26.CMov(r.z, a.z, flag);
// TODO: can the following be simplified?
bool inf = r.isInfinity ^ (r.isInfinity ^ a.isInfinity) & (flag == 1);

return new PointJacobian(rx, ry, rz, inf);
PointJacobian result = new PointJacobian(rx, ry, rz, inf);
#if DEBUG
result.Verify();
#endif
return result;
}

/// <summary>
Expand All @@ -560,6 +568,7 @@ public static PointJacobian CMov(in PointJacobian r, in PointJacobian a, uint fl
public PointJacobian Rescale(in UInt256_10x26 s)
{
#if DEBUG
Verify();
s.Verify();
Debug.Assert(!s.IsZeroNormalizedVar());
#endif
Expand All @@ -570,7 +579,11 @@ public PointJacobian Rescale(in UInt256_10x26 s)
ry = y.Multiply(s); // r->y *= s^3
UInt256_10x26 rz = z.Multiply(s); // r->z *= s

return new PointJacobian(rx, ry, rz, isInfinity);
PointJacobian result = new PointJacobian(rx, ry, rz, isInfinity);
#if DEBUG
result.Verify();
#endif
return result;
}


Expand All @@ -580,9 +593,16 @@ public PointJacobian Rescale(in UInt256_10x26 s)
/// <returns>-P</returns>
public PointJacobian Negate()
{
#if DEBUG
Verify();
#endif
UInt256_10x26 yNorm = y.NormalizeWeak();
UInt256_10x26 yNeg = yNorm.Negate(1);
return new PointJacobian(x, yNeg, z, isInfinity);
PointJacobian result = new PointJacobian(x, yNeg, z, isInfinity);
#if DEBUG
result.Verify();
#endif
return result;
}


Expand All @@ -595,12 +615,19 @@ public PointJacobian Negate()
/// <returns>Result</returns>
public Point ToPoint()
{
#if DEBUG
Verify();
#endif
UInt256_10x26 rz = z.Inverse();
UInt256_10x26 z2 = rz.Sqr();
UInt256_10x26 z3 = rz * z2;
UInt256_10x26 rx = x * z2;
UInt256_10x26 ry = y * z3;
return new Point(rx, ry, isInfinity);
Point result = new Point(rx, ry, isInfinity);
#if DEBUG
result.Verify();
#endif
return result;
}

/// <summary>
Expand All @@ -612,6 +639,9 @@ public Point ToPoint()
/// <returns>Result</returns>
public Point ToPointVar()
{
#if DEBUG
Verify();
#endif
if (isInfinity)
{
return Point.Infinity;
Expand All @@ -622,21 +652,30 @@ public Point ToPointVar()
UInt256_10x26 z3 = rz * z2;
UInt256_10x26 rx = x * z2;
UInt256_10x26 ry = y * z3;
return new Point(rx, ry, isInfinity);
Point result = new Point(rx, ry, isInfinity);
#if DEBUG
result.Verify();
#endif
return result;
}


internal Point ToPointZInv(in UInt256_10x26 zi)
{
#if DEBUG
Verify();
zi.Verify();
Debug.Assert(!isInfinity);
#endif
UInt256_10x26 zi2 = zi.Sqr();
UInt256_10x26 zi3 = zi2 * zi;
UInt256_10x26 rx = x * zi2;
UInt256_10x26 ry = y * zi3;
return new Point(rx, ry, isInfinity);
Point result = new Point(rx, ry, isInfinity);
#if DEBUG
result.Verify();
#endif
return result;
}


Expand All @@ -647,6 +686,10 @@ internal Point ToPointZInv(in UInt256_10x26 zi)
/// <returns>True if the two points are equal; otherwise false.</returns>
public bool EqualsVar(in PointJacobian other)
{
#if DEBUG
Verify();
other.Verify();
#endif
PointJacobian tmp = Negate();
tmp = tmp.AddVar(other, out _);
return tmp.isInfinity;
Expand All @@ -659,6 +702,10 @@ public bool EqualsVar(in PointJacobian other)
/// <returns>True if the two points are equal; otherwise false.</returns>
public bool EqualsVar(in Point other)
{
#if DEBUG
Verify();
other.Verify();
#endif
PointJacobian tmp = Negate();
tmp = tmp.AddVar(other, out _);
return tmp.isInfinity;
Expand All @@ -675,6 +722,7 @@ public bool EqualsVar(in Point other)
public bool EqualsVar(in UInt256_10x26 x)
{
#if DEBUG
Verify();
x.Verify();
Debug.Assert(!isInfinity);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ public PointStorage(in UInt256_8x32 x32, in UInt256_8x32 y32)
/// Converts this instance to a <see cref="Point"/>.
/// </summary>
/// <returns>Result</returns>
public Point ToPoint() => new Point(x.ToUInt256_10x26(), y.ToUInt256_10x26(), false);
public Point ToPoint()
{
Point result = new Point(x.ToUInt256_10x26(), y.ToUInt256_10x26(), false);
#if DEBUG
result.Verify();
#endif
return result;
}


/// <summary>
Expand Down

0 comments on commit faa6ce3

Please sign in to comment.