kmackay / micro-ecc

ECDH and ECDSA for 8-bit, 32-bit, and 64-bit processors.
BSD 2-Clause "Simplified" License
1.25k stars 458 forks source link

Adding support for other short Weierstraß prime curves with a!=-3 #159

Open lisa2code opened 4 years ago

lisa2code commented 4 years ago

Such as the popular BrainpoolP256r1 curve common in the EU.

When one looks at "uECC_curve-specific.inc", it is trivial to add a new struct:

static const struct uECC_Curve_t curve_brainpoolP256r1 = { ... };

which contains the curve parameters (except for a), and also add a new function for "x_side" which uses the a!=-3 value for alternative curves.

/ Computes result = x^3 + ax + b. result must not overlap x. / static void x_side_brainpoolP256r1(uint32_t result, const uint32_t x, uECC_Curve curve) { static const uint32_t curve_a[uECC_MAX_WORDS] = { BYTES_TO_WORDS_8(D9, B5, 30, F3, 44, 4B, 4A, E9), BYTES_TO_WORDS_8(6C, 5C, DC, 26, C1, 55, 80, FB), BYTES_TO_WORDS_8(E7, FF, 7A, 41, 30, 75, F6, EE), BYTES_TO_WORDS_8(57, 30, 2C, FC, 75, 09, 5A, 7D) }; int8_t num_words = curve->num_words; uECC_vli_modSquare_fast(result, x, curve); / r = x^2 / uECC_vli_modAdd(result, result, curve_a, curve->p, num_words); / r = x^2 + a / uECC_vli_modMult_fast(result, result, x, curve); / r = x^3 + ax / uECC_vli_modAdd(result, result, curve->b, curve->p, num_words); / r = x^3 + ax + b / }

But what about the "double_jacobian_default(...)" function, would this work for curves with a!=-3?

Thanks.

kmackay commented 3 years ago

You would use the general Jacobian doubling method, ie double_jacobian_secp256k1().