code wiki / (root) / nx_p384_point_test.nx

nx_p384_point_test.nx source

↩ module page · 64 lines · 2074 B

1// nx_p384_point_test.nx -- KAT for P-384 point operations. 2// 3// Verifies: 4// - G (base point) is on curve 5// - 2G via double + to_affine is on curve 6// - 2G computed via add(G, G) equals 2G via double(G) 7// - G + infinity == G 8// - G + (-G) == infinity (additive inverse: -G = (Gx, -Gy)) 9// - 3G via add(2G, G) is on curve 10// 11// expect_exit: 0 12// license_tier: ORIGINAL 13 14import "nx_syscalls.nx" 15import "nx_u384.nx" 16import "nx_p384_field.nx" 17import "nx_p384_point.nx" 18import "nx_p384_point_add.nx" 19 20func main() -> i64 { 21 let G: *P384Point = p384_point_alloc() 22 p384_point_load_g(G) 23 if p384_point_on_curve(G) != 1 { return 1 } 24 25 // Double G via the Jacobian doubling primitive, normalize to 26 // affine, verify on curve. 27 let G2: *P384Point = p384_point_alloc() 28 p384_point_double(G2, G) 29 p384_point_to_affine(G2) 30 if p384_point_on_curve(G2) != 1 { return 2 } 31 32 // Add G + G via point-add, normalize, compare to G2. 33 let G2_via_add: *P384Point = p384_point_alloc() 34 p384_point_add(G2_via_add, G, G) 35 if p384_point_eq(G2_via_add, G2) != 1 { return 3 } 36 37 // G + infinity == G 38 let inf_pt: *P384Point = p384_point_alloc() 39 let r: *P384Point = p384_point_alloc() 40 p384_point_add(r, G, inf_pt) 41 if p384_point_eq(r, G) != 1 { return 4 } 42 p384_point_add(r, inf_pt, G) 43 if p384_point_eq(r, G) != 1 { return 5 } 44 45 // G + (-G) == infinity. -G has the same X but negated Y. 46 let neg_G: *P384Point = p384_point_alloc() 47 u384_copy(neg_G.x, G.x) 48 p384_field_neg(neg_G.y, G.y) 49 p384_field_one(neg_G.z) 50 p384_point_add(r, G, neg_G) 51 if p384_point_is_infinity(r) != 1 { return 6 } 52 53 // 3G = add(2G_jacobian, G); on curve. Use the non-affine G2 54 // (snapshot the doubled-then-normalized one isn't required; 55 // add handles mixed Z). 56 let G2_jac: *P384Point = p384_point_alloc() 57 p384_point_double(G2_jac, G) 58 let G3: *P384Point = p384_point_alloc() 59 p384_point_add(G3, G2_jac, G) 60 p384_point_to_affine(G3) 61 if p384_point_on_curve(G3) != 1 { return 7 } 62 63 return 0 64}