debug_helpers_64.go raw
1 //go:build !js && !wasm && !tinygo && !wasm32
2
3 package p256k1
4
5 import "fmt"
6
7 // GetGeneratorXY returns the generator point coordinates as byte slices.
8 func GetGeneratorXY() (x, y []byte) {
9 gx := make([]byte, 32)
10 gy := make([]byte, 32)
11 g := Generator
12 g.x.normalize()
13 g.y.normalize()
14 g.x.getB32(gx)
15 g.y.getB32(gy)
16 return gx, gy
17 }
18
19 // TestFieldSetGetB32 tests the setB32→getB32 round-trip.
20 func TestFieldSetGetB32(in []byte) []byte {
21 var fe FieldElement
22 fe.setB32(in)
23 out := make([]byte, 32)
24 fe.getB32(out)
25 return out
26 }
27
28 // DumpFieldLimbs prints the internal limb representation of a field element.
29 func DumpFieldLimbs(b []byte) {
30 var fe FieldElement
31 fe.setB32(b)
32 fmt.Printf(" 5x52 limbs: [")
33 for i := 0; i < 5; i++ {
34 if i > 0 {
35 fmt.Print(", ")
36 }
37 fmt.Printf("0x%013x", fe.n[i])
38 }
39 fmt.Printf("]\n")
40 }
41
42 // TestFieldMul multiplies two field elements given as 32-byte big-endian.
43 func TestFieldMul(a, b []byte) []byte {
44 var fa, fb, fc FieldElement
45 fa.setB32(a)
46 fb.setB32(b)
47 fc.mul(&fa, &fb)
48 out := make([]byte, 32)
49 fc.getB32(out)
50 return out
51 }
52
53 // TestFieldSqr squares a field element given as 32-byte big-endian.
54 func TestFieldSqr(a []byte) []byte {
55 var fa, fc FieldElement
56 fa.setB32(a)
57 fc.sqr(&fa)
58 out := make([]byte, 32)
59 fc.getB32(out)
60 return out
61 }
62
63 // TestFieldInvMul computes inv(a)*a — should return 1.
64 func TestFieldInvMul(a []byte) []byte {
65 var fa, inv, result FieldElement
66 fa.setB32(a)
67 inv.inv(&fa)
68 result.mul(&inv, &fa)
69 out := make([]byte, 32)
70 result.getB32(out)
71 return out
72 }
73
74 // TestFieldSqrt computes sqrt(a) and returns (result, ok).
75 func TestFieldSqrt(a []byte) ([]byte, bool) {
76 var fa, fr FieldElement
77 fa.setB32(a)
78 ok := fr.sqrt(&fa)
79 out := make([]byte, 32)
80 fr.getB32(out)
81 return out, ok
82 }
83
84 // TestFieldAdd computes a + b mod p.
85 func TestFieldAdd(a, b []byte) []byte {
86 var fa, fb FieldElement
87 fa.setB32(a)
88 fb.setB32(b)
89 fa.add(&fb)
90 out := make([]byte, 32)
91 fa.getB32(out)
92 return out
93 }
94
95 // TestJacobianRoundTrip converts affine→Jacobian(z=1)→affine.
96 func TestJacobianRoundTrip(xb, yb []byte) ([]byte, []byte) {
97 var pt GroupElementAffine
98 pt.x.setB32(xb)
99 pt.y.setB32(yb)
100 pt.infinity = false
101
102 var jac GroupElementJacobian
103 jac.setGE(&pt)
104
105 var aff GroupElementAffine
106 aff.setGEJ(&jac)
107 aff.x.normalize()
108 aff.y.normalize()
109
110 ox := make([]byte, 32)
111 oy := make([]byte, 32)
112 aff.x.getB32(ox)
113 aff.y.getB32(oy)
114 return ox, oy
115 }
116
117 // TestPointDouble doubles an affine point and returns the result as affine.
118 func TestPointDouble(xb, yb []byte) ([]byte, []byte) {
119 var pt GroupElementAffine
120 pt.x.setB32(xb)
121 pt.y.setB32(yb)
122 pt.infinity = false
123
124 var jac GroupElementJacobian
125 jac.setGE(&pt)
126
127 var doubled GroupElementJacobian
128 doubled.double(&jac)
129
130 var aff GroupElementAffine
131 aff.setGEJ(&doubled)
132 aff.x.normalize()
133 aff.y.normalize()
134
135 ox := make([]byte, 32)
136 oy := make([]byte, 32)
137 aff.x.getB32(ox)
138 aff.y.getB32(oy)
139 return ox, oy
140 }
141