0
$\begingroup$

This is wrt to implementation of the salsa20_word_specification function at https://cr.yp.to/salsa20.html

The 2nd for loop goes like this

 for (i = 20;i > 0;i -= 2) {
....
....
....
}

The value of i isn't used inside the for loop. So I am wondering why this loop starts with 20 & goes down to 0 decrementing 2 in each iteration?

The same thing could be done the regular way i.e.

for (i = 0; i < 10; ++i) {
...
}

Am I missing something here?

$\endgroup$
5
  • 2
    $\begingroup$ I think it is just the indication of this: "The entire series of modifications is a series of 10 identical double-rounds." It's right in the text above it. i counts the rounds. As a developer I'm more concerned that $i$ starts at 20, probably an obscure way of optimizing a check at 0 instead of 20... $\endgroup$ – Maarten Bodewes Feb 2 at 8:19
  • $\begingroup$ @MaartenBodewes - I don't understand - how does i count the rounds? for (i=0; i < 10; ++i) would count 10 rounds. Also how exactly is a check at 0 more optimized at compared to a check at 20? $\endgroup$ – user93353 Feb 2 at 8:25
  • 2
    $\begingroup$ It reads "double-rounds" right? So it performs 2 per iteration. As for comparison with zero, CPU's often carry a zero flag which can be used within a branching instruction. Saves an explicit compare. It's a bit useless to program that in manually though as an optimizing compiler would be smart enough, and in that case counting from 10 would also be more optimized, but yeah.... $\endgroup$ – Maarten Bodewes Feb 2 at 8:31
  • $\begingroup$ @MaartenBodewes - there are 2 other for loops in the same function. Both of them use for (i = 0; i < 16; ++i) - they could have been similarly written as for (i=16; i >0; --i) to use the same 0 optimization. I am wondering why this optimization is used only for one of the functions! $\endgroup$ – user93353 Feb 2 at 8:37
  • 2
    $\begingroup$ Dan and Tanja are heavily into optimization; the other for loops are basically copy procedures. No doubt a memory copy will go faster from low to high... In the end this is a why question, and we cannot look into the minds of the authors (we might hope that they see this anyway and want to give us a hint). $\endgroup$ – Maarten Bodewes Feb 2 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.