|
| 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 | +} |
0 commit comments