# micro-ecc **Repository Path**: esp-submodules/micro-ecc ## Basic Information - **Project Name**: micro-ecc - **Description**: synchronize micro-ecc - **Primary Language**: Unknown - **License**: BSD-2-Clause - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-10 - **Last Updated**: 2026-12-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README micro-ecc ========== A small and fast ECDH and ECDSA implementation for 8-bit, 16-bit, 32-bit, and 64-bit processors. The static version of micro-ecc (ie, where the curve was selected at compile-time) can be found in the "static" branch. Features -------- * Side-channel hardened: constant-time scalar multiplication with mandatory RNG blinding by default (`uECC_CONSTANT_TIME=0`), or full constant-time execution without RNG blinding dependence when built with `-DuECC_CONSTANT_TIME=1` (see Compilation Notes). * Written in C, with optional inline assembly for AVR, ARM, Thumb, MSP430, MIPS (including PIC32M), PIC18, 16-bit PIC (PIC24 and dsPIC), dsPIC33A, RISC-V (RV32IM and RV64IM), and x86-64 (baseline and BMI2/ADX) platforms. * Supports 8, 16, 32, and 64-bit architectures. * Small code size. * No dynamic memory allocation. * Support for 8 short Weierstrass curves: secp160r1, secp192r1, secp224r1, secp256r1, secp256k1, plus opt-in secp384r1, brainpoolP256r1, and brainpoolP384r1 (RFC 5639), plus opt-in **X25519** (Curve25519 ECDH, RFC 7748) and **Ed25519** (Edwards-curve signatures, RFC 8032, with built-in or pluggable SHA-512, optional `Ed25519ctx`/`Ed25519ph`, and RFC 7748 Ed25519→X25519 key conversion when both are enabled) — opt-in curves are disabled by default; enable with `-DuECC_SUPPORTS_=1` (e.g. `-DuECC_SUPPORTS_x25519=1`, `-DuECC_SUPPORTS_ed25519=1`) or CMake (`-DUECC_SUPPORTS_SECP384R1=ON`, `-DUECC_SUPPORTS_BRAINPOOLP256R1=ON`, `-DUECC_SUPPORTS_BRAINPOOLP384R1=ON`, `-DUECC_SUPPORTS_X25519=ON`, `-DUECC_SUPPORTS_ED25519=ON`). X25519 and Ed25519 can also be built standalone with all Weierstrass curves set to `0`. * BSD 2-clause license. Usage Notes ----------- ### Point Representation ### Compressed points are represented in the standard format as defined in http://www.secg.org/sec1-v2.pdf (`curve_size + 1` bytes starting with `0x02` or `0x03`); uncompressed points are represented in standard format, but without the `0x04` prefix. All functions except `uECC_decompress()` only accept uncompressed points, and `uECC_decompress()` only accepts compressed points (validating the `0x02`/`0x03` prefix, `x < p`, and curve membership, returning `1` on success or zeroing `public_key` and returning `0` on failure); use `uECC_compress()` and `uECC_decompress()` to convert between compressed and uncompressed point representations. `uECC_shared_secret()` and `uECC_verify*()` also validate uncompressed public keys internally (and `uECC_shared_secret()` zeroes `secret` on error). For Ed25519, `uECC_ed25519_valid_public_key()` and `uECC_ed25519*_verify()` reject small-order torsion keys (`[8]A == O`, and `[8]R == O` on commitments in verification); `uECC_ed25519_public_key_to_x25519()` additionally enforces strict prime-order subgroup membership (`order(A) == L`, rejecting mixed-order points as well). Private keys are represented in the standard format (`ceil(num_n_bits / 8)` bytes: 21 bytes for `secp160r1`, matching curve size for all other curves). Note that `secp160r1` ECDSA signatures are 40 bytes (`20` bytes each for `r` and `s`, retrying if `s >= 2^160`; prepend a `0x00` byte to each component when interoperating with 42-byte `ceil(161/8)` libraries). Optional ECDSA recoverable signing and public key recovery (`uECC_sign_recoverable()`, `uECC_sign_deterministic_recoverable()`, and `uECC_recover_public_key()`) and low-S signature canonicalization (`uECC_normalize_signature()`) can be enabled independently with `-DuECC_SUPPORT_RECOVER_PUBKEY=1` / `-DuECC_SUPPORT_NORMALIZE_SIGNATURE=1` (or CMake `-DUECC_SUPPORT_RECOVER_PUBKEY=ON` / `-DUECC_SUPPORT_NORMALIZE_SIGNATURE=ON`). Set `-DuECC_SUPPORTS_ECDSA=0` (CMake `-DUECC_SUPPORTS_ECDSA=OFF`) to exclude all ECDSA functions and ECDSA curve-table ROM when only ECDH is needed. Optional built-in SHA-256 and SHA-512 support can be enabled with `-DuECC_SUPPORTS_SHA256=1` and `-DuECC_SUPPORTS_SHA512=1` (or CMake `-DUECC_SUPPORTS_SHA256=ON` / `-DUECC_SUPPORTS_SHA512=ON`; shares the internal SHA-512 core when `uECC_SUPPORTS_ed25519=1` is also enabled). Each provides a `uECC_HashContext` adapter (`uECC_SHA256_HashContext_init()` / `uECC_SHA512_HashContext_init()`) for `uECC_sign_deterministic()` or general hashing, plus raw-message deterministic ECDSA helpers (`uECC_sign_sha256()`, `uECC_verify_sha256()`, `uECC_sign_sha512()`, `uECC_verify_sha512()`, and `uECC_sign_recoverable_sha*()` / `uECC_recover_public_key_sha*()` when `uECC_SUPPORT_RECOVER_PUBKEY=1`) that hash the message internally without setting up a hash context. Both are disabled by default (`0 B` overhead). Low-level point arithmetic (`uECC_point_add()` and `uECC_point_mult()`) is exposed in `uECC_vli.h` when `-DuECC_ENABLE_VLI_API=1`. The edge-case scalars / private keys `1`, `n - 2`, and `n - 1` (where `n` is the curve order) are not supported by the Co-Z Montgomery ladder path and will cause `uECC_compute_public_key()`, `uECC_shared_secret()`, or `uECC_point_mult()` to fail depending on the optimization level and algorithm selected (specifically when both `uECC_FIXED_BASE_COMB == 0` and `uECC_MULT_WINDOW == 0` for `uECC_compute_public_key()`, or when `uECC_MULT_WINDOW == 0` for `uECC_shared_secret()` and `uECC_point_mult()`). `uECC_make_key()` and `uECC_sign()` retry automatically if an unsupported scalar is generated. ### Using the Code ### You can integrate micro-ecc in whichever way best fits your build system: * **Direct source inclusion:** Copy (or submodule/symlink) the micro-ecc files into your project, compile `uECC.c`, and `#include "uECC.h"`. * **CMake:** Add the repository via `add_subdirectory()` or `FetchContent` and link against `micro-ecc::micro-ecc`. Compile-time options are exposed as CMake cache variables (e.g. `UECC_OPTIMIZATION_LEVEL`, `UECC_CONSTANT_TIME`, `UECC_SUPPORTS_SECP256R1`, `UECC_EXTRA_DEFINES`): ```bash cmake -B build -DUECC_OPTIMIZATION_LEVEL=3 -DUECC_CONSTANT_TIME=ON cmake --build build ctest --test-dir build --output-on-failure ``` * **Make:** Build `build/libuECC.a` or run the standalone C tests directly: ```bash make # builds build/libuECC.a make test # builds & runs standalone C tests make CC=arm-none-eabi-gcc AR=arm-none-eabi-ar \ CFLAGS="-mcpu=cortex-m4 -mthumb -Os" UECC_OPTIMIZATION_LEVEL=3 ``` * **Arduino:** Use the Library Manager to download micro-ecc (**Sketch**=>**Include Library**=>**Manage Libraries**), then include it via **Sketch**=>**Import Library**. See `uECC.h` for API documentation, [OPTIMIZATION.md](OPTIMIZATION.md) for full per-architecture benchmarks and fine-grained tuning flags, and [test/README.md](test/README.md) for the cross-architecture verification scripts, benchmarks, and simulators. ### Optimization Levels ### Set `-DuECC_OPTIMIZATION_LEVEL=<0..6>` (default **`2`**) to trade Flash/ROM size — and stack/RAM at levels 4–6 — for speed. Levels `1` through `6` monotonically increase code size and overall speed on every supported architecture, and automatically specialize when a single Weierstrass curve is enabled (using direct curve dispatch and compile-time curve dimensions, while keeping the `uECC.h` API unchanged). * **0 — Minimal pure C:** Smallest portable code, no assembly or fast curve reductions. Very slow; only intended for extreme Flash constraints or debugging (not supported with `uECC_CONSTANT_TIME=1`). * **1 — Smallest practical build (lowest RAM):** Enables curve-specific fast reductions and compact looped assembly with no lookup tables. * **2 — Default *(recommended starting point, lowest RAM)*:** Best size-to-speed sweet spot. Enables fast core assembly with zero precomputed point tables and peak RAM at or below level 1 on almost all targets. * **3 — Fast key generation & signing (low RAM):** Adds a compact precomputed generator table across all architectures, roughly doubling to tripling key generation and signing speed with only ~0–110 B of extra RAM. * **4 — Balanced mid-high speed (low-to-medium RAM):** Expands the generator table and enables faster modular inversion / unrolled field arithmetic (or faster verification on 8/16-bit targets). * **5 — High speed (medium RAM):** Larger generator table and fast low-stack signature verification (plus unrolled multiplication for multi-curve 8/16-bit builds). * **6 — Maximum speed (higher RAM):** Adds windowed ECDH acceleration, full unrolled squaring, the largest default generator table in single-curve builds, and automatic GLV acceleration when `secp256k1` is the only enabled Weierstrass curve. #### Size & Speed Overview — Single Curve (`secp256r1`) #### Code size (`KB`, with `%` growth relative to **L1**) and overall speedup (geometric mean across ECDH, KeyGen, Sign, and Verify relative to **L1**), measured with `uECC_CONSTANT_TIME=0`. See [OPTIMIZATION.md](OPTIMIZATION.md) for raw cycle/instruction counts, per-operation timings, and `L0` baselines. | Architecture | L1 (Baseline) | L2 *(default)* | L3 | L4 | L5 | L6 *(max)* | Peak RAM (L1 -> L6) | | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | | **ARM Cortex-M4** | 5.7 KB
(784 B RAM) | 6.4 KB (`+13%`)
**6.3x** faster | 7.8 KB (`+38%`)
**9.6x** faster | 9.5 KB (`+68%`)
**12.4x** faster | 11.2 KB (`+99%`)
**14.0x** faster | 14.6 KB (`+159%`)
**15.5x** faster | 784 -> 1,312 B | | **ARM Cortex-M3** | 5.7 KB
(784 B RAM) | 7.0 KB (`+24%`)
**3.6x** faster | 8.4 KB (`+49%`)
**5.8x** faster | 10.5 KB (`+85%`)
**7.5x** faster | 12.4 KB (`+119%`)
**8.7x** faster | 15.2 KB (`+169%`)
**9.7x** faster | 784 -> 1,308 B | | **ARM Cortex-M0+** | 5.8 KB
(776 B RAM) | 6.2 KB (`+8%`)
**1.4x** faster | 7.5 KB (`+30%`)
**2.3x** faster | 9.3 KB (`+61%`)
**3.9x** faster | 11.8 KB (`+105%`)
**4.6x** faster | 14.5 KB (`+152%`)
**5.3x** faster | 776 -> 1,408 B | | **AVR (ATmega2560)** | 11.1 KB
(914 B RAM) | 14.1 KB (`+27%`)
**1.4x** faster | 16.4 KB (`+48%`)
**2.3x** faster | 18.8 KB (`+69%`)
**5.3x** faster | 23.4 KB (`+111%`)
**6.8x** faster | 30.5 KB (`+176%`)
**8.3x** faster | 914 -> 1,499 B | | **MSP430 (MPY32)** | 7.3 KB
(664 B RAM) | 8.3 KB (`+14%`)
**2.4x** faster | 10.0 KB (`+37%`)
**4.0x** faster | 13.0 KB (`+79%`)
**5.6x** faster | 16.1 KB (`+122%`)
**6.3x** faster | 20.4 KB (`+182%`)
**7.4x** faster | 664 -> 1,240 B | | **MSP430 (SW mult)** | 7.7 KB
(664 B RAM) | 8.5 KB (`+11%`)
**1.4x** faster | 10.3 KB (`+34%`)
**2.4x** faster | 12.5 KB (`+62%`)
**4.0x** faster | 16.6 KB (`+115%`)
**5.1x** faster | 19.5 KB (`+153%`)
**5.9x** faster | 664 -> 1,330 B | | **PIC18 (XC8)** | 15.6 KB
(1,282 B RAM) | 16.5 KB (`+6%`)
**2.1x** faster | 19.3 KB (`+24%`)
**3.6x** faster | 25.0 KB (`+61%`)
**5.9x** faster | 32.0 KB (`+106%`)
**7.9x** faster | 35.7 KB (`+129%`)
**8.8x** faster | 1,282 -> 2,147 B | | **16-bit PIC (PIC24/dsPIC)** | 10.8 KB
(1,056 B RAM) | 11.5 KB (`+6%`)
**1.7x** faster | 13.6 KB (`+25%`)
**2.9x** faster | 19.1 KB (`+76%`)
**4.6x** faster | 25.4 KB (`+134%`)
**6.0x** faster | 31.0 KB (`+186%`)
**6.9x** faster | 1,056 -> 1,586 B | | **dsPIC33A (XC-DSC)** | 14.6 KB
(1,256 B RAM) | 15.5 KB (`+6%`)
**1.6x** faster | 17.9 KB (`+23%`)
**2.6x** faster | 20.0 KB (`+37%`)
**3.1x** faster | 22.4 KB (`+53%`)
**3.6x** faster | 25.7 KB (`+76%`)
**4.1x** faster | 1,256 -> 1,784 B | | **MIPS32 (PIC32, XC32)** | 17.7 KB
(888 B RAM) | 18.9 KB (`+7%`)
**1.7x** faster | 21.8 KB (`+23%`)
**3.2x** faster | 24.9 KB (`+41%`)
**4.1x** faster | 27.4 KB (`+55%`)
**4.6x** faster | 31.9 KB (`+80%`)
**5.3x** faster | 888 -> 1,512 B | | **RISC-V (RV32IMAC)** | 8.8 KB
(740 B RAM) | 10.0 KB (`+14%`)
**1.6x** faster | 11.5 KB (`+31%`)
**2.6x** faster | 14.2 KB (`+62%`)
**3.2x** faster | 16.1 KB (`+83%`)
**3.7x** faster | 19.0 KB (`+117%`)
**4.3x** faster | 740 -> 1,296 B | | **RISC-V (RV64IMAC)** | 8.8 KB
(920 B RAM) | 9.8 KB (`+11%`)
**1.3x** faster | 11.4 KB (`+29%`)
**2.2x** faster | 13.6 KB (`+55%`)
**2.8x** faster | 15.5 KB (`+76%`)
**3.2x** faster | 18.4 KB (`+110%`)
**3.6x** faster | 920 -> 1,320 B | | **x86-64 (`gcc -O2`)** | 18.6 KB
(1,120 B RAM) | 20.0 KB (`+8%`)
**1.1x** faster | 22.4 KB (`+20%`)
**1.8x** faster | 24.2 KB (`+30%`)
**2.2x** faster | 26.6 KB (`+43%`)
**2.5x** faster | 29.2 KB (`+57%`)
**2.7x** faster | 1,120 -> 1,740 B |
All 5 Curves Enabled (secp160r1, secp192r1, secp224r1, secp256r1, secp256k1) — Size & Speed vs. L1 | Architecture | L1 (Baseline) | L2 *(default)* | L3 | L4 | L5 | L6 *(max)* | Peak RAM (L1 -> L6) | | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | | **ARM Cortex-M4** | 9.2 KB | 11.9 KB (`+30%`)
**7.3x** | 15.7 KB (`+72%`)
**11.9x** | 19.9 KB (`+118%`)
**15.6x** | 22.6 KB (`+147%`)
**16.9x** | 28.6 KB (`+213%`)
**18.8x** | 824 -> 1,376 B | | **ARM Cortex-M3** | 9.2 KB | 12.9 KB (`+41%`)
**4.6x** | 16.2 KB (`+77%`)
**7.4x** | 20.5 KB (`+123%`)
**9.6x** | 22.6 KB (`+147%`)
**10.6x** | 28.4 KB (`+210%`)
**11.7x** | 824 -> 1,416 B | | **ARM Cortex-M0+** | 9.3 KB | 11.4 KB (`+22%`)
**1.7x** | 14.7 KB (`+58%`)
**2.8x** | 19.5 KB (`+109%`)
**4.2x** | 23.9 KB (`+157%`)
**5.1x** | 30.0 KB (`+222%`)
**6.0x** | 812 -> 1,484 B | | **AVR (ATmega2560)** | 17.2 KB | 21.9 KB (`+27%`)
**1.4x** | 26.2 KB (`+52%`)
**2.5x** | 37.9 KB (`+120%`)
**5.0x** | 49.0 KB (`+184%`)
**6.8x** | 62.4 KB (`+262%`)
**8.1x** | 1,690 -> 2,213 B | | **MSP430 (MPY32)** | 12.1 KB | 14.5 KB (`+20%`)
**2.7x** | 18.0 KB (`+48%`)
**4.5x** | 20.7 KB (`+70%`)
**5.3x** | 26.8 KB (`+120%`)
**6.2x** | 41.1 KB (`+238%`)
**7.7x** | 698 -> 1,276 B | | **MSP430 (SW mult)** | 12.4 KB | 14.8 KB (`+20%`)
**1.4x** | 18.5 KB (`+49%`)
**2.5x** | 21.1 KB (`+70%`)
**2.9x** | 28.9 KB (`+133%`)
**4.2x** | 43.4 KB (`+250%`)
**5.7x** | 696 -> 1,366 B | | **PIC18 (XC8)** | 21.7 KB | 23.8 KB (`+10%`)
**2.1x** | 28.7 KB (`+32%`)
**3.6x** | 32.2 KB (`+48%`)
**4.2x** | 48.9 KB (`+126%`)
**6.2x** | 67.7 KB (`+213%`)
**8.3x** | 1,443 -> 2,154 B | | **16-bit PIC (PIC24/dsPIC)** | 18.1 KB | 20.2 KB (`+12%`)
**1.7x** | 25.4 KB (`+41%`)
**2.9x** | 29.3 KB (`+62%`)
**3.4x** | 41.7 KB (`+131%`)
**4.8x** | 62.1 KB (`+244%`)
**6.5x** | 1,080 -> 1,622 B | | **dsPIC33A (XC-DSC)** | 25.1 KB | 27.7 KB (`+10%`)
**1.5x** | 32.8 KB (`+31%`)
**2.5x** | 36.5 KB (`+46%`)
**3.0x** | 43.0 KB (`+72%`)
**3.4x** | 48.0 KB (`+92%`)
**3.8x** | 1,228 -> 1,856 B | | **MIPS32 (PIC32, XC32)** | 22.4 KB | 26.9 KB (`+20%`)
**1.7x** | 32.1 KB (`+44%`)
**3.1x** | 38.2 KB (`+71%`)
**3.8x** | 46.5 KB (`+108%`)
**4.5x** | 52.5 KB (`+135%`)
**5.0x** | 896 -> 1,704 B | | **RISC-V (RV32IMAC)** | 14.0 KB | 15.4 KB (`+10%`)
**1.5x** | 19.8 KB (`+42%`)
**2.6x** | 24.9 KB (`+78%`)
**3.2x** | 31.1 KB (`+122%`)
**3.7x** | 32.8 KB (`+135%`)
**4.0x** | 800 -> 1,344 B | | **RISC-V (RV64IMAC)** | 13.6 KB | 14.3 KB (`+5%`)
**1.2x** | 19.6 KB (`+45%`)
**2.1x** | 25.0 KB (`+84%`)
**2.7x** | 31.7 KB (`+133%`)
**3.1x** | 33.7 KB (`+148%`)
**3.3x** | 1,032 -> 1,448 B | | **x86-64 (`gcc -O2`)** | 26.8 KB | 30.3 KB (`+13%`)
**1.1x** | 35.1 KB (`+31%`)
**1.9x** | 38.5 KB (`+44%`)
**2.2x** | 47.2 KB (`+76%`)
**2.6x** | 48.5 KB (`+81%`)
**2.7x** | 2,024 -> 2,712 B | *Note:* `KB` = `bytes / 1024`. On 16-bit PIC (XC16), code size is reported in 24-bit instruction-byte units from the linker map.
For opt-in **X25519** and **Ed25519** (`L0`–`L6`) size, RAM, and speed tables across all architectures, see [OPTIMIZATION.md](OPTIMIZATION.md) (§6–§7). ### Opt-In Optimizations ### The options below are **not enabled by default** across general builds (either because they trade significantly more RAM/Flash than the main ladder or only help a single curve in multi-curve builds). Define them with `-D