Skip to content

curve25519.c: reformat code to follow coding guidelines - #7750

Closed
mspncp wants to merge 10 commits into
openssl:masterfrom
mspncp:pr-reformat-curve25519-c
Closed

mspncp wants to merge 10 commits into
openssl:masterfrom
mspncp:pr-reformat-curve25519-c

Conversation

@mspncp

@mspncp mspncp commented Dec 3, 2018 •

Copy link
Copy Markdown
Contributor

Fixes #7698

I tried to reformat the code as gently as possible and to minimize unnessesary whitespace diffs. In particular I tried to preserve most of the tabular aligning in the formulas, even in case where the 80 column rule was violated, referring to the following exceptional rule in our coding guidelines:

Statements longer than 80 columns must be broken into sensible chunks, unless exceeding 80 columns significantly increases readability and does not hide information.

(In fact, the golden rule of all coding guidelines mandates that they must serve readability and not vice versa...)

There is another rule which I didn't follow blindly, because I did not want to disturb the perfect formatting of the formulas:

Do not unnecessarily use braces around a single statement

IMHO this should be a should, not a must anyway. But if you insist on this rule, I can add one more fixup.

Editorial Note

I split the reformatting into simple comprehensible steps and I recommend reviewing the changes commit-by-commit, before looking at the total change set. In the end all commits will be squashed into a single one with the following commit message:

curve25519.c: reformat code to follow coding guidelines

Fixes #7698

Update: the last commit will be kept separate from the others and not squashed

curve25519.c: improve formula alignment

@richsalz

richsalz commented Dec 3, 2018

Copy link
Copy Markdown
Contributor

It says "unnecessarily use braces" for a reason. :)

@mspncp
mspncp force-pushed the pr-reformat-curve25519-c branch from 7a498b7 to 19f6d1d Compare December 3, 2018 21:11

@richsalz richsalz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

partial comments.

Comment thread crypto/ec/curve25519.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line after the declaration. consider indenting the expression so the cast/in part lines up with the next two lines.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line after the declaration.

Thanks for the reminder. Needs to be fixed globally.

Comment thread crypto/ec/curve25519.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smae comments as above.

Comment thread crypto/ec/curve25519.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider putting space before the 4, 7, to make things line up

Comment thread crypto/ec/curve25519.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, consider spacing these lines out so they line up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, consider spacing these lines out so they line up.

I was already considering improving the alignments, like this one and the one you noted above. So with your endorsement I will probably not have to resist the temptation to do it any further ;-).

@mspncp mspncp changed the title curve25519.c: reformat code to follow coding guidelines WIP: curve25519.c: reformat code to follow coding guidelines Dec 3, 2018
@mspncp

mspncp commented Dec 3, 2018 •

Copy link
Copy Markdown
Contributor Author

I just added the WIP marker.

@mspncp
mspncp force-pushed the pr-reformat-curve25519-c branch 2 times, most recently from 67d1ba8 to fffac34 Compare December 3, 2018 22:00
@mspncp

mspncp commented Dec 3, 2018

Copy link
Copy Markdown
Contributor Author

Ok, that's enough for today. Please let me know whether the rigorous alignment of formulas in commit 43e0e6a8d5669d60c9f9b3a48fe96b5b57d3ba77 improves readablility or whether it should be reverted. (currently, the alignment is only partially done).

@richsalz

richsalz commented Dec 3, 2018 via email

Copy link
Copy Markdown
Contributor

@mspncp
mspncp force-pushed the pr-reformat-curve25519-c branch 2 times, most recently from 7ce806c to 2fe09bf Compare December 4, 2018 23:06
@mspncp

mspncp commented Dec 4, 2018

Copy link
Copy Markdown
Contributor Author

Out of WIP, ready for final review.

In the end all commits except for the last one will be squashed into a single one with the following commit message:

curve25519.c: reformat code to follow coding guidelines

Fixes #7698

The last commit is a little different, so it will be kept separate from the others:

curve25519.c: improve formula alignment

@mspncp mspncp changed the title WIP: curve25519.c: reformat code to follow coding guidelines curve25519.c: reformat code to follow coding guidelines Dec 4, 2018
@mspncp mspncp added branch: master Applies to master branch approval: review pending This pull request needs review by a committer branch: 1.1.1 Applies to OpenSSL_1_1_1-stable branch (EOL) labels Dec 4, 2018
Comment thread crypto/ec/curve25519.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be at least one space either side of each +?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, it should... however, I'd like to see an exception here, in the interest of keeping the matrix look and thereby avoiding broken lines if we can.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elsewhere we have similar expressions that do have the spaces. If we want to avoid the broken lines, I'd rather add the spaces and exceed the 80 character limit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I overlooked this one. Fixup follows.

Comment thread crypto/ec/curve25519.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't a6 be lined up with c6? Similar comment for lines below.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if an expression is broken into several lines, we prefer to have the lines broken before an operator rather than after, i.e. that pair of lines should be:

    s6 = c6 + a0 * b6 + a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1
        + a6 * b0;

However, @mattcaswell, I fear that perfect alignment of variables competes with our requiring 4 space indent of the broken lines.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fear that perfect alignment of variables competes with our requiring 4 space indent of the broken lines.

Is that a requirement??

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... in this hunk I didn't do any aligning (yet), I only shortened overly long lines, which went from 80 to 82 bytes by the reindentation. I kept the original indation of 4 bytes for followup lines. You can see this nicely in 9c8ffa6?diff=split.

I could align the followup lines, but then I would have to do this for the entire block. Also note that there are different types of lines, the ones starting with a carry

openssl/crypto/ec/curve25519.c

Lines 4989 to 4992 in 9c8ffa6

s6 = c6 + a0 * b6 + a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1 +
a6 * b0;
s7 = c7 + a0 * b7 + a1 * b6 + a2 * b5 + a3 * b4 + a4 * b3 + a5 * b2 +
a6 * b1 + a7 * b0;

and the ones without:

openssl/crypto/ec/curve25519.c

Lines 5005 to 5008 in 9c8ffa6

s14 = a3 * b11 + a4 * b10 + a5 * b9 + a6 * b8 + a7 * b7 + a8 * b6 +
a9 * b5 + a10 * b4 + a11 * b3;
s15 = a4 * b11 + a5 * b10 + a6 * b9 + a7 * b8 + a8 * b7 + a9 * b6 +
a10 * b5 + a11 * b4;

If I would indent 5 bytes instead of 4, the * operators would align nicely in the latter case, but in the former the * operators would align with + operators which does not make sense. That's why I left it unchanged. But If you think it is better that way, or if you have an alternative proposal, I'm open for suggestions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fear that perfect alignment of variables competes with our requiring 4 space indent of the broken lines.

Is that a requirement??

@levitte @mattcaswell hang on for a minute, I think I found a nice compromise...

@mspncp

mspncp commented Dec 5, 2018 •

Copy link
Copy Markdown
Contributor Author

I have two alternative variants to offer

  • Variant 1 (a92f069ca67c8a1ecd8f00c891755365061274dc) only aligns the operators.
  • Variant 2 (2a1e01a58846ae12049b7d28e05c023f25278759) builds on variant 1 but emphasizes 'multiplication before addition' by reducing the spaces around * operators while increasing spaces around + operators.

Yes, I know that variant 2 is far from all coding guidelines. But from a mathematical viewpoint it makes sense, because it emphasizes the the structure of the formula.

Edit: sorry, I had to swap the commit ids, they were in the wrong order

@mspncp

mspncp commented Dec 5, 2018

Copy link
Copy Markdown
Contributor Author

Needless to say that I prefer variant 2 ;-)

@mspncp

mspncp commented Dec 5, 2018 •

Copy link
Copy Markdown
Contributor Author

@mspncp

mspncp commented Dec 5, 2018 •

Copy link
Copy Markdown
Contributor Author

May I suggest right justifying the lhs of the * operations? I.e., taken from variant 1:

My last suggestion (I promise!): also with right justified lhs operands, but more compact (128 columns):

Variant 5

https://github.com/openssl/openssl/blob/86d13ed1a2acdfac17ef9d23c56d3867aa50d987/crypto/ec/curve25519.c#L5044-L5067

@mspncp

mspncp commented Dec 5, 2018

Copy link
Copy Markdown
Contributor Author

"To 3 or not to 3, that's the question" 🤣

(i.e variants 5 and 3 are my favorites, in that order)

@levitte

levitte commented Dec 6, 2018

Copy link
Copy Markdown
Member

Oh wait, so breaking those lines isn't an option any more?

@mspncp

mspncp commented Dec 6, 2018

Copy link
Copy Markdown
Contributor Author

All variants can be voted on. Variants 5 and 3 are my favorites, but I will follow your's and Matts advice.

@mspncp

mspncp commented Dec 6, 2018

Copy link
Copy Markdown
Contributor Author

Ah, ok. It looks like I did not fully understand your suggestion #7750 (comment).

@mattcaswell

Copy link
Copy Markdown
Member

Personally I prefer 4 in spite of the line length. But 5 would be acceptable.

@levitte

levitte commented Dec 6, 2018

Copy link
Copy Markdown
Member

I agree with @mattcaswell, of all variants, I like 4 best. Regarding breaking lines, I dunno what to say. Our coding style says 80 columns, but on the other hand, it seems we've already accepted exceptions to the rules in this file, so...

@mspncp

mspncp commented Dec 6, 2018 •

Copy link
Copy Markdown
Contributor Author

I agree with @mattcaswell, of all variants, I like 4 best.

That's ok for me, I'll choose variant 4.

Regarding breaking lines, I dunno what to say. Our coding style says 80 columns, but on the other hand, it seems we've already accepted exceptions to the rules in this file, so...

@richsalz made a very solominic comment w.r.t. this rule in #7750 (comment)

@mspncp
mspncp force-pushed the pr-reformat-curve25519-c branch from 86d13ed to fdbab61 Compare December 6, 2018 10:48
@mspncp

mspncp commented Dec 6, 2018

Copy link
Copy Markdown
Contributor Author

See fdbab61.

@mspncp

mspncp commented Dec 6, 2018

Copy link
Copy Markdown
Contributor Author

Here is a link to the current state of curve25519.c.

@mattcaswell mattcaswell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mattcaswell mattcaswell added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Dec 6, 2018
@mspncp

mspncp commented Dec 6, 2018

Copy link
Copy Markdown
Contributor Author

Matt, I have one final fixup: When taking a final look at curve25519.c, I noticed these constants

static const int64_t kBottom25Bits = 0x1ffffffLL;
static const int64_t kBottom26Bits = 0x3ffffffLL;
static const int64_t kTop39Bits = 0xfffffffffe000000LL;
static const int64_t kTop38Bits = 0xfffffffffc000000LL;

which inspired my to replace the hexadecimal numbers (which I converted from dezimal in fdbab61) by a symbolic constant, too.

Please confirm 2fefd39.

@mattcaswell mattcaswell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reconfirm

levitte pushed a commit that referenced this pull request Dec 6, 2018
levitte pushed a commit that referenced this pull request Dec 6, 2018
Reviewed-by: Matt Caswell <[email protected]>
(Merged from #7750)
levitte pushed a commit that referenced this pull request Dec 6, 2018
Fixes #7698

Reviewed-by: Matt Caswell <[email protected]>
(Merged from #7750)

(cherry picked from commit 3a17b9a)
levitte pushed a commit that referenced this pull request Dec 6, 2018
Reviewed-by: Matt Caswell <[email protected]>
(Merged from #7750)

(cherry picked from commit 425dde5)
@mspncp

mspncp commented Dec 6, 2018

Copy link
Copy Markdown
Contributor Author

Merged, thanks!

master:
425dde5 curve25519.c: improve formula alignment
3a17b9a curve25519.c: reformat code to follow coding guidelines

1.1.1:
9478b1a curve25519.c: improve formula alignment
6a1ed7a curve25519.c: reformat code to follow coding guidelines

(The cherry-pick succeeded without conflicts. My #7698 (comment) turned out to be fake news. It was just a layer 8 problem while using pick-to-branch.)

@mspncp mspncp closed this Dec 6, 2018
@mspncp
mspncp deleted the pr-reformat-curve25519-c branch December 6, 2018 21:36
@mspncp
mspncp restored the pr-reformat-curve25519-c branch December 6, 2018 21:48
@mspncp

mspncp commented Dec 6, 2018 •

Copy link
Copy Markdown
Contributor Author

For the record (and for fun) I restored the unsquashed and unmerged branch and rebased it onto 4ace4cc such that commit d4160bc94901 (the head of pr-reformat-curve25519-c) points to the same tree as commit 425dde5, and the same holds for commits 3a17b9a and 45b75f5a3d:

for c in 425dde5d75 3a17b9a46e d4160bc949 45b75f5a3d 
do
    echo "commit $c -> $(git rev-parse $c^{tree})"
done

commit 425dde5 -> df372e71aab600b40d3c0d883fba6df3a850392b
commit 3a17b9a -> be96cb2b7f7ef68c897105fd909d4078c492b801

commit d4160bc949 -> df372e71aab600b40d3c0d883fba6df3a850392b
commit 45b75f5a3d -> be96cb2b7f7ef68c897105fd909d4078c492b801

This proves the equivalence of the merged with the unsquashed branch and makes commit 3a17b9a decomposable:

master

425dde5 curve25519.c: improve formula alignment
3a17b9a curve25519.c: reformat code to follow coding guidelines

pr-reformat-curve25519-c

d4160bc949 curve25519.c: improve formula alignment
45b75f5a3d reformat curve25519.c: fix multiline comments
5858e2c58b reformat curve25519.c: insert missing blank lines after delcarations
fb018c059e reformat curve25519.c: reformat some non-standard array initializers
73a7b2e657 reformat curve25519.c: break long lines where it makes sense
19b532c731 reformat curve25519.c: fix opening braces of function bodies{
6069d79157 reformat curve25519.c: undo some unnecessary changes
685ff096c1 reformat curve25519.c: run emacs indent-region

@mspncp

mspncp commented Dec 6, 2018

Copy link
Copy Markdown
Contributor Author

Oh, interesting. The pull request has now become closed permanently, it can't be reopened and the commit view doesn't get updated anymore. That's probably because I force-pushed while it was closed. What a pity, but never mind, it's also something new I learned. Sorry if I created some noise on the notification area with my experiments ;-).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval: done This pull request has the required number of approvals branch: master Applies to master branch branch: 1.1.1 Applies to OpenSSL_1_1_1-stable branch (EOL)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

curve25519.c does not conform to the coding standards

4 participants