code wiki / (root) / nx_p384_scalar_mul_test.nx

nx_p384_scalar_mul_test.nx source

↩ module page · 48 lines · 1238 B

1// nx_p384_scalar_mul_test.nx -- KAT for scalar_mul. 2// 3// Verifies: 4// - 1 * G == G 5// - 2 * G == double(G) (via add) 6// - n * G == infinity (where n is the group order) 7// 8// expect_exit: 0 9// license_tier: ORIGINAL 10 11import "nx_syscalls.nx" 12import "nx_u384.nx" 13import "nx_p384_modn.nx" 14import "nx_p384_point.nx" 15import "nx_p384_point_add.nx" 16import "nx_p384_scalar_mul.nx" 17 18func main() -> i64 { 19 let G: *P384Point = p384_point_alloc() 20 p384_point_load_g(G) 21 22 let k: *i64 = u384_alloc() 23 24 // ---- Test A: 1 * G == G ---- 25 u384_one(k) 26 let r1: *P384Point = p384_point_alloc() 27 p384_scalar_mul(r1, k, G) 28 if p384_point_eq(r1, G) != 1 { return 1 } 29 30 // ---- Test B: 2 * G == double(G) ---- 31 u384_zero(k) 32 k[0] = 2 33 let r2: *P384Point = p384_point_alloc() 34 p384_scalar_mul(r2, k, G) 35 let G2: *P384Point = p384_point_alloc() 36 p384_point_double(G2, G) 37 if p384_point_eq(r2, G2) != 1 { return 2 } 38 39 // ---- Test C: n * G == infinity (Lagrange's theorem on the 40 // group of order n) ---- 41 let n: *i64 = u384_alloc() 42 p384_modn_load_n(n) 43 let r3: *P384Point = p384_point_alloc() 44 p384_scalar_mul(r3, n, G) 45 if p384_point_is_infinity(r3) != 1 { return 3 } 46 47 return 0 48}