Skip to content

Commit 58febbc

Browse files
committed
temp: Debugging
1 parent 140acf2 commit 58febbc

File tree

3 files changed

+233
-6
lines changed

3 files changed

+233
-6
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package field_p256r1
2+
3+
import "core:encoding/hex"
4+
import "core:testing"
5+
6+
G_X : string : "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296"
7+
G_Y : string : "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5"
8+
9+
@(test)
10+
basic_math :: proc(t: ^testing.T) {
11+
fe_x, fe_y: Montgomery_Domain_Field_Element
12+
b, _ := hex.decode(transmute([]byte)(G_X), context.temp_allocator)
13+
14+
ok := fe_from_bytes(&fe_x, b)
15+
testing.expect(t, ok == true)
16+
17+
g_x := Montgomery_Domain_Field_Element{
18+
8784043285714375740,
19+
8483257759279461889,
20+
8789745728267363600,
21+
1770019616739251654,
22+
}
23+
testing.expectf(t, fe_equal(&g_x, &fe_x) == 1, "g_x: %v, fe: %v", g_x, fe_x)
24+
25+
b, _ = hex.decode(transmute([]byte)(G_Y), context.temp_allocator)
26+
ok = fe_from_bytes(&fe_y, b)
27+
testing.expect(t, ok == true)
28+
29+
g_y := Montgomery_Domain_Field_Element{
30+
15992936863339206154,
31+
10037038012062884956,
32+
15197544864945402661,
33+
9615747158586711429,
34+
}
35+
testing.expectf(t, fe_equal(&g_y, &fe_y) == 1, "g_y: %v, fe: %v")
36+
37+
// 55df5d5850f47bad82149139979369fe498a9022a412b5e0bedd2cfc21c3ed91
38+
y_sq: Montgomery_Domain_Field_Element
39+
fe_square(&y_sq, &fe_y)
40+
41+
fe_to_bytes(b, &y_sq)
42+
s := (string)(hex.encode(b, context.temp_allocator))
43+
testing.expectf(t, false, "%s", s)
44+
45+
fe_mul(&y_sq, &fe_y, &fe_y)
46+
fe_to_bytes(b, &y_sq)
47+
s = (string)(hex.encode(b, context.temp_allocator))
48+
testing.expectf(t, false, "%s", s)
49+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package _weierstrass
2+
3+
import "core:encoding/hex"
4+
import "core:testing"
5+
6+
import "core:fmt"
7+
8+
@(private = "file")
9+
G_X :: "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296"
10+
@(private = "file")
11+
G_Y :: "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5"
12+
13+
@(private = "file")
14+
G_UNCOMPRESSED :: "04" + G_X + G_Y
15+
16+
@(test)
17+
test_p256_a :: proc(t: ^testing.T) {
18+
a_str := "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc"
19+
20+
fe, a_fe: Field_Element_p256r1
21+
fe_a(&fe)
22+
fe_a(&a_fe)
23+
24+
b: [32]byte
25+
fe_bytes(b[:], &fe)
26+
27+
s := (string)(hex.encode(b[:], context.temp_allocator))
28+
29+
testing.expect(t, s == a_str)
30+
31+
fe_zero(&fe)
32+
fe_set_bytes(&fe, b[:])
33+
34+
testing.expect(t, fe_equal(&fe, &a_fe) == 1)
35+
}
36+
37+
@(test)
38+
test_p256_b :: proc(t: ^testing.T) {
39+
b_str := "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b"
40+
41+
fe, b_fe: Field_Element_p256r1
42+
fe_b(&fe)
43+
fe_b(&b_fe)
44+
45+
b: [32]byte
46+
fe_bytes(b[:], &fe)
47+
48+
s := (string)(hex.encode(b[:], context.temp_allocator))
49+
50+
testing.expect(t, s == b_str)
51+
52+
fe_zero(&fe)
53+
fe_set_bytes(&fe, b[:])
54+
55+
testing.expect(t, fe_equal(&fe, &b_fe) == 1)
56+
}
57+
58+
@(test)
59+
test_p256_g_x :: proc(t: ^testing.T) {
60+
fe, x_fe: Field_Element_p256r1
61+
fe_gen_x(&fe)
62+
fe_gen_x(&x_fe)
63+
64+
b: [32]byte
65+
fe_bytes(b[:], &fe)
66+
67+
s := (string)(hex.encode(b[:], context.temp_allocator))
68+
testing.expect(t, s == G_X)
69+
70+
fe_zero(&fe)
71+
fe_set_bytes(&fe, b[:])
72+
73+
testing.expect(t, fe_equal(&fe, &x_fe) == 1)
74+
}
75+
76+
@(test)
77+
test_p256_g_y :: proc(t: ^testing.T) {
78+
fe, y_fe: Field_Element_p256r1
79+
fe_gen_y(&fe)
80+
fe_gen_y(&y_fe)
81+
82+
b: [32]byte
83+
fe_bytes(b[:], &fe)
84+
85+
s := (string)(hex.encode(b[:], context.temp_allocator))
86+
testing.expect(t, s == G_Y)
87+
88+
fe_zero(&fe)
89+
fe_set_bytes(&fe, b[:])
90+
91+
testing.expect(t, fe_equal(&fe, &y_fe) == 1)
92+
}
93+
94+
@(test)
95+
test_p256_scalarmul :: proc(t: ^testing.T) {
96+
p, q, r: Point_p256r1
97+
sc: Scalar_p256r1
98+
b: [1]byte
99+
tmp: [32]byte
100+
101+
pt_generator(&p)
102+
pt_identity(&q)
103+
pt_identity(&r)
104+
105+
for i in 0..<256 {
106+
b[0] = byte(i)
107+
_ = sc_set_bytes(&sc, b[:])
108+
109+
sc_bytes(tmp[:], &sc)
110+
s := string(hex.encode(tmp[:], context.temp_allocator))
111+
112+
if i != 0 {
113+
pt_add(&q, &q, &p)
114+
}
115+
pt_scalar_mul(&r, &p, &sc)
116+
pt_rescale(&q, &q)
117+
pt_rescale(&r, &r)
118+
119+
testing.expectf(t, pt_equal(&q, &r) == 1, "sc: %s, %v %v", s, q, r)
120+
}
121+
}
122+
123+
@(test)
124+
test_p256_is_on_curve :: proc(t: ^testing.T) {
125+
x, y, lhs, rhs: Field_Element_p256r1
126+
tmp: [32]byte
127+
128+
fe_gen_x(&x)
129+
fe_gen_y(&y)
130+
131+
set_yy_candidate(&rhs, &x)
132+
fe_square(&lhs, &y)
133+
134+
fe_bytes(tmp[:], &rhs)
135+
rhs_s := string(hex.encode(tmp[:], context.temp_allocator))
136+
137+
fe_bytes(tmp[:], &lhs)
138+
lhs_s := string(hex.encode(tmp[:], context.temp_allocator))
139+
testing.expectf(t, lhs_s == rhs_s, "lhs: %s rhs: %s", lhs_s, rhs_s)
140+
}
141+
142+
@(test)
143+
test_p256_s11n_sec_identity ::proc(t: ^testing.T) {
144+
p: Point_p256r1
145+
146+
pt_generator(&p)
147+
ok := pt_set_sec_bytes(&p, []byte{0x00})
148+
testing.expect(t, ok)
149+
testing.expectf(t, pt_is_identity(&p) == 1, "%v", p)
150+
151+
b := []byte{0xff}
152+
ok = pt_sec_bytes(b, &p, true)
153+
testing.expect(t, ok)
154+
testing.expect(t, b[0] == 0x00)
155+
156+
b = []byte{0xff}
157+
ok = pt_sec_bytes(b, &p, false)
158+
testing.expect(t, ok)
159+
testing.expect(t, b[0] == 0x00)
160+
}
161+
162+
@(test)
163+
test_p256_s11n_sec_generator ::proc(t: ^testing.T) {
164+
p, g: Point_p256r1
165+
166+
pt_generator(&g)
167+
pt_identity(&p)
168+
169+
b: [65]byte
170+
ok := pt_sec_bytes(b[:], &g, false)
171+
testing.expect(t, ok)
172+
s := (string)(hex.encode(b[:], context.temp_allocator))
173+
testing.expectf(t, s == G_UNCOMPRESSED, "g: %v bytes: %v, %v", g, G_UNCOMPRESSED, s)
174+
175+
ok = pt_set_sec_bytes(&p, b[:])
176+
testing.expectf(t, ok, "%s", s)
177+
testing.expect(t, pt_equal(&g, &p) == 1)
178+
}

core/crypto/_weierstrass/point.odin

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,15 +533,15 @@ is_on_curve :: proc "contextless" (x, y: ^$T) -> bool {
533533
@(private)
534534
set_yy_candidate :: proc "contextless" (maybe_yy, x: ^$T) {
535535
// RHS: x^3 + ax + b
536-
tmp: T
536+
a_x, b_fe: T
537537

538538
fe_square(maybe_yy, x)
539539
fe_mul(maybe_yy, maybe_yy, x)
540540

541-
fe_a(&tmp)
542-
fe_mul(&tmp, &tmp, x)
543-
fe_add(maybe_yy, maybe_yy, &tmp)
541+
fe_a(&a_x)
542+
fe_mul(&a_x, &a_x, x)
543+
fe_add(maybe_yy, maybe_yy, &a_x)
544544

545-
fe_b(&tmp)
546-
fe_add(maybe_yy, maybe_yy, &tmp)
545+
fe_b(&b_fe)
546+
fe_add(maybe_yy, maybe_yy, &b_fe)
547547
}

0 commit comments

Comments
 (0)