Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof-of-concept: make grp argument of ecp_mul const #7970

Draft
wants to merge 12 commits into
base: development
Choose a base branch
from

Commits on Jul 20, 2023

  1. Weierstrass curves always have a static table

    When FIXED_POINT_OPTIM == 1, all Weierstrass curves (those that use comb
    multiplication) have a static table for multiples of G defined in
    ecp_curve.c, and there's not reason we'd want one of them not to.
    
    So, the code that handles the case where we don't have one, and then
    fall back to computing it storing it in grp->T, is effectively dead
    since 3.0.
    
    Actually it becomes undead when adding a new curve, before it has a
    static table: scripts/ecc_comb_table.py relies on this fallback kicking
    in so that we can then dump the T that was computed on the fly, stick
    the data in ecp_curves.c, then the fallback code becomes dead again.
    
    Having extra complexity in the library just for supporting a development
    script is rarely a good idea, especially when that complexity involves
    memory management, and is the only thing that prevents us from making
    the grp argument of ecp_mul const (which in turns forces us to play
    tricks to ensure thread safety when an ECC PK context is shared across
    threads).
    
    So, this commit is the first in a series that will clarify how the table
    of pre-computed points is used and managed.
    
    Right now, it breaks scripts/ecc_comb_table.py, but the script will be
    fixed later (by making it capable of computing the table on its own,
    rather than relying on undead code in the library for that).
    
    Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
    mpg committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    63a188a View commit details
    Browse the repository at this point in the history
  2. Remove obsolete code when picking window size

    This is a remnant of the era where we supported arbitrary curves, and
    took advantage of that by using some small curves in some tests.
    
    Now we only support standard curves, which all have nbits >= 192 so this
    code was effectively dead.
    
    Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
    mpg committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    9aa5d65 View commit details
    Browse the repository at this point in the history
  3. Exclude T from builds that won't use it

    This ensures we don't waste code handling T when it's not used.
    
    Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
    mpg committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    8368c6d View commit details
    Browse the repository at this point in the history
  4. Clarify ownership of the table

    Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
    mpg committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    c57b1a5 View commit details
    Browse the repository at this point in the history
  5. Improve comments and a variable's name

    Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
    mpg committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    9537ad4 View commit details
    Browse the repository at this point in the history
  6. We don't need T_size in the group structure

    It was only useful for freeing all the points in the table, but we never
    want to do that as the table is static now.
    
    Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
    mpg committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    238cc63 View commit details
    Browse the repository at this point in the history
  7. Remove unneeded macro in ecp_curves.c

    All the points in the table can be stored with Z freed, because Z is
    never going to be accessed. I'm not sure why we introduced the Z1
    macros, but we should never have needed it. It was probably a miss in
    reviewing the PR that added static tables.
    
    Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
    mpg committed Jul 20, 2023
    Configuration menu
    Copy the full SHA
    f712c70 View commit details
    Browse the repository at this point in the history

Commits on Jul 21, 2023

  1. Add guide to adding a new curve in ecp_curves.c

    Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
    mpg committed Jul 21, 2023
    Configuration menu
    Copy the full SHA
    fc45634 View commit details
    Browse the repository at this point in the history
  2. Replace scripts/ecp_comb_table.py with new program

    The old script was no longer working once we removed the legacy code
    that updated grp->T in ecp_mul_comb() (which was necessary to simplify
    the code an in the future support making grp const.) Also, I didn't like
    that it had quite a lot of C embedded in a python script.
    
    Since the new program needs access to an MBEDTLS_STATIC_TESTABLE
    function, and programs can't directly access internal header, take an
    indirect route via a "test" helper, that programs can access. (This is a
    bit of hack as non-test code lives in test, but I think it's OK.) Large
    parts of the "test" helper are adapted from the code previously embedded
    in the python script.
    
    The new program was manually tested by running on all short Weierstrass
    curves (1-8 and 10-12) and checking the output is byte for byte
    identical to the current content of ecp_curves.c.
    
    Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
    mpg committed Jul 21, 2023
    Configuration menu
    Copy the full SHA
    72ad325 View commit details
    Browse the repository at this point in the history
  3. Remove unused fields from ecp_group

    Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
    mpg committed Jul 21, 2023
    Configuration menu
    Copy the full SHA
    78db480 View commit details
    Browse the repository at this point in the history
  4. Rename field in ecp_group

    Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
    mpg committed Jul 21, 2023
    Configuration menu
    Copy the full SHA
    0a5043c View commit details
    Browse the repository at this point in the history
  5. Make the group const when it should be

    Follow-up: simplify PK accordingly (no need for a copy any more)
    
    Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
    mpg committed Jul 21, 2023
    Configuration menu
    Copy the full SHA
    18f526d View commit details
    Browse the repository at this point in the history