Skip to content

Commit faa6ce3

Browse files
Add mroe Verify() checks to points
1 parent e87c069 commit faa6ce3

File tree

3 files changed

+96
-12
lines changed

3 files changed

+96
-12
lines changed

Src/Autarkysoft.Bitcoin/Cryptography/EllipticCurve/Point.cs

+34-5
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,9 @@ public static bool IsFracOnCurve(in UInt256_10x26 xn, in UInt256_10x26 xd)
444444
/// <returns></returns>
445445
public bool IsValidVar()
446446
{
447+
#if DEBUG
448+
Verify();
449+
#endif
447450
if (isInfinity)
448451
{
449452
return false;
@@ -462,9 +465,16 @@ public bool IsValidVar()
462465
/// <returns>-P</returns>
463466
public Point Negate()
464467
{
468+
#if DEBUG
469+
Verify();
470+
#endif
465471
UInt256_10x26 yNorm = y.NormalizeWeak();
466472
UInt256_10x26 yNeg = yNorm.Negate(1);
467-
return new Point(x, yNeg, isInfinity);
473+
Point result = new Point(x, yNeg, isInfinity);
474+
#if DEBUG
475+
result.Verify();
476+
#endif
477+
return result;
468478
}
469479

470480
/// <summary>
@@ -476,8 +486,8 @@ public Point MulLambda()
476486
#if DEBUG
477487
Verify();
478488
#endif
479-
var rx = x.Multiply(UInt256_10x26.Beta);
480-
var r = new Point(rx, y, isInfinity);
489+
UInt256_10x26 rx = x.Multiply(UInt256_10x26.Beta);
490+
Point r = new Point(rx, y, isInfinity);
481491
#if DEBUG
482492
r.Verify();
483493
#endif
@@ -516,21 +526,33 @@ public Span<byte> ToByteArray(bool compressed)
516526
internal Point ToPointZInv(in UInt256_10x26 zi)
517527
{
518528
#if DEBUG
529+
Verify();
519530
zi.Verify();
520531
Debug.Assert(!isInfinity);
521532
#endif
522533
UInt256_10x26 zi2 = zi.Sqr();
523534
UInt256_10x26 zi3 = zi2 * zi;
524535
UInt256_10x26 rx = x * zi2;
525536
UInt256_10x26 ry = y * zi3;
526-
return new Point(rx, ry, isInfinity);
537+
Point result = new Point(rx, ry, isInfinity);
538+
#if DEBUG
539+
result.Verify();
540+
#endif
541+
return result;
527542
}
528543

529544

530545
/// <summary>
531546
/// Converts this instance in affine coordinates to point in jacobian coordinates
532547
/// </summary>
533-
public PointJacobian ToPointJacobian() => new PointJacobian(x, y, UInt256_10x26.One, isInfinity);
548+
public PointJacobian ToPointJacobian()
549+
{
550+
PointJacobian result = new PointJacobian(x, y, UInt256_10x26.One, isInfinity);
551+
#if DEBUG
552+
result.Verify();
553+
#endif
554+
return result;
555+
}
534556

535557
/// <summary>
536558
/// Converts this instance to a <see cref="PointStorage"/>.
@@ -539,7 +561,10 @@ internal Point ToPointZInv(in UInt256_10x26 zi)
539561
/// <returns>Result</returns>
540562
public PointStorage ToStorage()
541563
{
564+
#if DEBUG
565+
Verify();
542566
Debug.Assert(!isInfinity);
567+
#endif
543568
return new PointStorage(x, y);
544569
}
545570

@@ -552,6 +577,10 @@ public PointStorage ToStorage()
552577
/// <returns>True if the two points are equal; otherwise false.</returns>
553578
public bool EqualsVar(in Point other)
554579
{
580+
#if DEBUG
581+
Verify();
582+
other.Verify();
583+
#endif
555584
if (isInfinity != other.isInfinity)
556585
{
557586
return false;

Src/Autarkysoft.Bitcoin/Cryptography/EllipticCurve/PointJacobian.cs

+54-6
Original file line numberDiff line numberDiff line change
@@ -543,13 +543,21 @@ public PointJacobian DoubleVar(out UInt256_10x26 rzr)
543543
[MethodImpl(MethodImplOptions.AggressiveInlining)]
544544
public static PointJacobian CMov(in PointJacobian r, in PointJacobian a, uint flag)
545545
{
546+
#if DEBUG
547+
r.Verify();
548+
a.Verify();
549+
#endif
546550
UInt256_10x26 rx = UInt256_10x26.CMov(r.x, a.x, flag);
547551
UInt256_10x26 ry = UInt256_10x26.CMov(r.y, a.y, flag);
548552
UInt256_10x26 rz = UInt256_10x26.CMov(r.z, a.z, flag);
549553
// TODO: can the following be simplified?
550554
bool inf = r.isInfinity ^ (r.isInfinity ^ a.isInfinity) & (flag == 1);
551555

552-
return new PointJacobian(rx, ry, rz, inf);
556+
PointJacobian result = new PointJacobian(rx, ry, rz, inf);
557+
#if DEBUG
558+
result.Verify();
559+
#endif
560+
return result;
553561
}
554562

555563
/// <summary>
@@ -560,6 +568,7 @@ public static PointJacobian CMov(in PointJacobian r, in PointJacobian a, uint fl
560568
public PointJacobian Rescale(in UInt256_10x26 s)
561569
{
562570
#if DEBUG
571+
Verify();
563572
s.Verify();
564573
Debug.Assert(!s.IsZeroNormalizedVar());
565574
#endif
@@ -570,7 +579,11 @@ public PointJacobian Rescale(in UInt256_10x26 s)
570579
ry = y.Multiply(s); // r->y *= s^3
571580
UInt256_10x26 rz = z.Multiply(s); // r->z *= s
572581

573-
return new PointJacobian(rx, ry, rz, isInfinity);
582+
PointJacobian result = new PointJacobian(rx, ry, rz, isInfinity);
583+
#if DEBUG
584+
result.Verify();
585+
#endif
586+
return result;
574587
}
575588

576589

@@ -580,9 +593,16 @@ public PointJacobian Rescale(in UInt256_10x26 s)
580593
/// <returns>-P</returns>
581594
public PointJacobian Negate()
582595
{
596+
#if DEBUG
597+
Verify();
598+
#endif
583599
UInt256_10x26 yNorm = y.NormalizeWeak();
584600
UInt256_10x26 yNeg = yNorm.Negate(1);
585-
return new PointJacobian(x, yNeg, z, isInfinity);
601+
PointJacobian result = new PointJacobian(x, yNeg, z, isInfinity);
602+
#if DEBUG
603+
result.Verify();
604+
#endif
605+
return result;
586606
}
587607

588608

@@ -595,12 +615,19 @@ public PointJacobian Negate()
595615
/// <returns>Result</returns>
596616
public Point ToPoint()
597617
{
618+
#if DEBUG
619+
Verify();
620+
#endif
598621
UInt256_10x26 rz = z.Inverse();
599622
UInt256_10x26 z2 = rz.Sqr();
600623
UInt256_10x26 z3 = rz * z2;
601624
UInt256_10x26 rx = x * z2;
602625
UInt256_10x26 ry = y * z3;
603-
return new Point(rx, ry, isInfinity);
626+
Point result = new Point(rx, ry, isInfinity);
627+
#if DEBUG
628+
result.Verify();
629+
#endif
630+
return result;
604631
}
605632

606633
/// <summary>
@@ -612,6 +639,9 @@ public Point ToPoint()
612639
/// <returns>Result</returns>
613640
public Point ToPointVar()
614641
{
642+
#if DEBUG
643+
Verify();
644+
#endif
615645
if (isInfinity)
616646
{
617647
return Point.Infinity;
@@ -622,21 +652,30 @@ public Point ToPointVar()
622652
UInt256_10x26 z3 = rz * z2;
623653
UInt256_10x26 rx = x * z2;
624654
UInt256_10x26 ry = y * z3;
625-
return new Point(rx, ry, isInfinity);
655+
Point result = new Point(rx, ry, isInfinity);
656+
#if DEBUG
657+
result.Verify();
658+
#endif
659+
return result;
626660
}
627661

628662

629663
internal Point ToPointZInv(in UInt256_10x26 zi)
630664
{
631665
#if DEBUG
666+
Verify();
632667
zi.Verify();
633668
Debug.Assert(!isInfinity);
634669
#endif
635670
UInt256_10x26 zi2 = zi.Sqr();
636671
UInt256_10x26 zi3 = zi2 * zi;
637672
UInt256_10x26 rx = x * zi2;
638673
UInt256_10x26 ry = y * zi3;
639-
return new Point(rx, ry, isInfinity);
674+
Point result = new Point(rx, ry, isInfinity);
675+
#if DEBUG
676+
result.Verify();
677+
#endif
678+
return result;
640679
}
641680

642681

@@ -647,6 +686,10 @@ internal Point ToPointZInv(in UInt256_10x26 zi)
647686
/// <returns>True if the two points are equal; otherwise false.</returns>
648687
public bool EqualsVar(in PointJacobian other)
649688
{
689+
#if DEBUG
690+
Verify();
691+
other.Verify();
692+
#endif
650693
PointJacobian tmp = Negate();
651694
tmp = tmp.AddVar(other, out _);
652695
return tmp.isInfinity;
@@ -659,6 +702,10 @@ public bool EqualsVar(in PointJacobian other)
659702
/// <returns>True if the two points are equal; otherwise false.</returns>
660703
public bool EqualsVar(in Point other)
661704
{
705+
#if DEBUG
706+
Verify();
707+
other.Verify();
708+
#endif
662709
PointJacobian tmp = Negate();
663710
tmp = tmp.AddVar(other, out _);
664711
return tmp.isInfinity;
@@ -675,6 +722,7 @@ public bool EqualsVar(in Point other)
675722
public bool EqualsVar(in UInt256_10x26 x)
676723
{
677724
#if DEBUG
725+
Verify();
678726
x.Verify();
679727
Debug.Assert(!isInfinity);
680728
#endif

Src/Autarkysoft.Bitcoin/Cryptography/EllipticCurve/PointStorage.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ public PointStorage(in UInt256_8x32 x32, in UInt256_8x32 y32)
4545
/// Converts this instance to a <see cref="Point"/>.
4646
/// </summary>
4747
/// <returns>Result</returns>
48-
public Point ToPoint() => new Point(x.ToUInt256_10x26(), y.ToUInt256_10x26(), false);
48+
public Point ToPoint()
49+
{
50+
Point result = new Point(x.ToUInt256_10x26(), y.ToUInt256_10x26(), false);
51+
#if DEBUG
52+
result.Verify();
53+
#endif
54+
return result;
55+
}
4956

5057

5158
/// <summary>

0 commit comments

Comments
 (0)