ecp_test.c 4.32 KB
 davxy committed Dec 11, 2016 1 2 3 ``````#include "test.h" #include `````` Davide Galassi committed May 11, 2019 4 5 6 7 8 ``````/* * y^2 = x^3 + 2x + 2 (mod 17) * * Generator point = (5, 1) * Generator order = 19 `````` Davide Galassi committed Apr 28, 2019 9 `````` */ `````` Davide Galassi committed May 15, 2019 10 ``````static void simple_curve_init(cry_ecp_grp *ec) `````` davxy committed Dec 11, 2016 11 ``````{ `````` Davide Galassi committed Apr 28, 2019 12 13 14 15 16 17 18 `````` cry_mpi_init_int(&ec->a, 2); cry_mpi_init_int(&ec->b, 2); cry_mpi_init_int(&ec->p, 17); cry_mpi_init_int(&ec->n, 19); cry_mpi_init_int(&ec->g.x, 5); cry_mpi_init_int(&ec->g.y, 1); cry_mpi_init_int(&ec->g.z, 1); `````` davxy committed Dec 11, 2016 19 20 ``````} `````` Davide Galassi committed Apr 29, 2019 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 ``````/* Check that a point is on the curve */ static void point_check(const cry_ecp *p, const cry_ecp_grp *grp) { /* Check thay y^2 = x^3 + ax + b (mod p) */ cry_mpi v, t; if (cry_ecp_is_zero(p)) return; cry_mpi_init_list(&v, &t, NULL); cry_mpi_sqr(&t, &p->x); /* x^2 */ cry_mpi_mul(&t, &t, &p->x); /* t = x^3 */ cry_mpi_mul(&v, &grp->a, &p->x); /* v = ax */ cry_mpi_add(&v, &v, &grp->b); /* v = ax + b */ cry_mpi_add(&v, &v, &t); /* v = x^3 + ax + b */ cry_mpi_sqr(&t, &p->y); /* t = y^2 */ cry_mpi_sub(&v, &v, &t); cry_mpi_mod(&v, &v, &grp->p); ASSERT(cry_mpi_is_zero(&v)); cry_mpi_clear_list(&v, &t, NULL); } `````` Davide Galassi committed May 11, 2019 42 ``````static void add_test(void) `````` davxy committed Dec 11, 2016 43 ``````{ `````` Davide Galassi committed May 15, 2019 44 `````` cry_ecp_grp grp; `````` Davide Galassi committed Apr 28, 2019 45 `````` cry_ecp p; `````` Davide Galassi committed Apr 29, 2019 46 `````` int i = 0; `````` Davide Galassi committed Apr 28, 2019 47 `````` `````` Davide Galassi committed May 15, 2019 48 `````` simple_curve_init(&grp); `````` Davide Galassi committed Apr 28, 2019 49 `````` cry_ecp_init(&p); `````` Davide Galassi committed Apr 28, 2019 50 `````` do { `````` Davide Galassi committed Jun 07, 2019 51 `````` //TRACE("n = %d\n", i); `````` Davide Galassi committed May 11, 2019 52 53 54 `````` //cry_mpi_print(&p.x, 10); //cry_mpi_print(&p.y, 10); //cry_mpi_print(&p.z, 10); `````` Davide Galassi committed May 15, 2019 55 `````` point_check(&p, &grp); `````` Davide Galassi committed Jun 07, 2019 56 `````` //TRACE("--------------------\n"); `````` Davide Galassi committed May 15, 2019 57 `````` cry_ecp_add(&p, &p, &grp.g, &grp); `````` Davide Galassi committed Apr 28, 2019 58 `````` i++; `````` Davide Galassi committed May 11, 2019 59 `````` } while (!cry_ecp_is_zero(&p)); `````` Davide Galassi committed May 15, 2019 60 61 `````` cry_ecp_grp_clear(&grp); cry_ecp_clear(&p); `````` Davide Galassi committed Apr 29, 2019 62 63 ``````} `````` Davide Galassi committed May 11, 2019 64 ``````static void mul_test(void) `````` Davide Galassi committed Apr 29, 2019 65 ``````{ `````` Davide Galassi committed May 15, 2019 66 `````` cry_ecp_grp grp; `````` Davide Galassi committed Apr 29, 2019 67 68 69 `````` cry_ecp p; cry_mpi v; `````` Davide Galassi committed May 15, 2019 70 `````` simple_curve_init(&grp); `````` Davide Galassi committed Apr 29, 2019 71 72 `````` cry_ecp_init_int(&p, 9, 16); /* p = 5g = (9, 16) */ cry_mpi_init_int(&v, 2); `````` Davide Galassi committed May 15, 2019 73 74 75 76 77 78 79 80 `````` point_check(&p, &grp); cry_ecp_mul(&p, &p, &v, &grp); /* 2p = 10g = (7,11) */ point_check(&p, &grp); cry_ecp_grp_clear(&grp); cry_ecp_clear(&p); cry_mpi_clear(&v); `````` davxy committed Dec 11, 2016 81 ``````} `````` Davide Galassi committed May 11, 2019 82 `````` `````` Davide Galassi committed May 11, 2019 83 ``````static void secp192r1_load_test(void) `````` Davide Galassi committed May 11, 2019 84 ``````{ `````` Davide Galassi committed May 11, 2019 85 `````` int res; `````` Davide Galassi committed May 11, 2019 86 87 `````` cry_ecp_grp grp; `````` Davide Galassi committed May 11, 2019 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 `````` res = cry_ecp_grp_load(&grp, CRY_ECP_GRP_SECP192R1); ASSERT(res == 0); } static void secp224r1_load_test(void) { int res; cry_ecp_grp grp; res = cry_ecp_grp_load(&grp, CRY_ECP_GRP_SECP224R1); ASSERT(res == 0); } static void secp256r1_load_test(void) { int res; cry_ecp_grp grp; res = cry_ecp_grp_load(&grp, CRY_ECP_GRP_SECP256R1); ASSERT(res == 0); } static void secp384r1_load_test(void) { int res; cry_ecp_grp grp; res = cry_ecp_grp_load(&grp, CRY_ECP_GRP_SECP384R1); ASSERT(res == 0); } static void secp521r1_load_test(void) { int res; cry_ecp_grp grp; res = cry_ecp_grp_load(&grp, CRY_ECP_GRP_SECP521R1); ASSERT(res == 0); } static void secp192k1_load_test(void) { int res; cry_ecp_grp grp; res = cry_ecp_grp_load(&grp, CRY_ECP_GRP_SECP192K1); ASSERT(res == 0); } static void secp224k1_load_test(void) { int res; cry_ecp_grp grp; res = cry_ecp_grp_load(&grp, CRY_ECP_GRP_SECP224K1); ASSERT(res == 0); } static void secp256k1_load_test(void) { int res; cry_ecp_grp grp; res = cry_ecp_grp_load(&grp, CRY_ECP_GRP_SECP256K1); ASSERT(res == 0); } static void bp256r1_load_test(void) { int res; cry_ecp_grp grp; res = cry_ecp_grp_load(&grp, CRY_ECP_GRP_BP256R1); ASSERT(res == 0); } static void bp384r1_load_test(void) { int res; cry_ecp_grp grp; res = cry_ecp_grp_load(&grp, CRY_ECP_GRP_BP384R1); ASSERT(res == 0); } static void bp512r1_load_test(void) { int res; cry_ecp_grp grp; res = cry_ecp_grp_load(&grp, CRY_ECP_GRP_BP512R1); ASSERT(res == 0); } static void bad_curve_load_test(void) { int res; cry_ecp_grp grp; res = cry_ecp_grp_load(&grp, 0xFFFF); ASSERT(res != 0); } static void curve_params_load(void) { RUN(secp192r1_load_test); RUN(secp224r1_load_test); RUN(secp256r1_load_test); RUN(secp384r1_load_test); RUN(secp521r1_load_test); RUN(secp192k1_load_test); RUN(secp224k1_load_test); RUN(secp256k1_load_test); RUN(bp256r1_load_test); RUN(bp384r1_load_test); RUN(bp512r1_load_test); RUN(bad_curve_load_test); `````` Davide Galassi committed May 11, 2019 217 218 219 220 ``````} void ecp_test(void) { `````` Davide Galassi committed Jun 07, 2019 221 `````` TRACE("* ECP load curves parameters\n"); `````` Davide Galassi committed May 11, 2019 222 223 224 `````` curve_params_load(); add_test(); mul_test(); `````` Davide Galassi committed May 11, 2019 225 ``}``