<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=windows-1252">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 10/12/2015 18:33, Viktor Dukhovni
      wrote:<br>
    </div>
    <blockquote class=" cite"
      id="mid_20151210173317_GL11836_mournblade_imrryr_org"
      cite="mid:20151210173317.GL11836@mournblade.imrryr.org"
      type="cite">
      <pre wrap="">On Thu, Dec 10, 2015 at 04:55:29AM -0700, Jayalakshmi bhat wrote:

</pre>
      <blockquote class=" cite" id="Cite_3140110" type="cite">
        <pre wrap="">static inline unsigned int constant_time_msb(unsigned int a) {
-  return 0 - (a >> (sizeof(a) * 8 - 1));
+ return (((unsigned)((int)(a) >> (sizeof(int) * 8 - 1))));
}
</pre>
      </blockquote>
      <pre wrap="">The replacement is not right.  This function is supposed to return
0xfffffff for inputs with the high bit set, and 0x0000000 for inputs
with the high bit not set.  Could you try:

    static inline unsigned int constant_time_msb(unsigned int a) {
      return 0 - (a >> ((int)(sizeof(a) * 8 - 1)));
    }

Just in case the compiler is promoting "a" to the (larger?) size
of sizeof(a), which would cause an unsigned "a" to get a zero MSB,
while a signed "a" would be promoted "correctly".
</pre>
    </blockquote>
    <tt>Look again, he is casting a to signed, then doing an <br>
      arithmetic right shift to extend the msb (sign bit) <br>
      to the rest of the word.  This works on 3 conditions:</tt><tt><br>
    </tt><tt><br>
    </tt><tt>1. The platform is actually using twos complement.</tt><tt><br>
    </tt><tt>2. The signed right shift function invoked by the C <br>
        compiler is a sign-preserving ("arithmetic") shift.</tt><tt><br>
    </tt><tt>3. The compiler wasn't written by a fanatic who put <br>
        the "right shift of negative signed values is <br>
        undefined" rule above common sense.</tt><tt><br>
    </tt><br>
    <br>
    <pre class="moz-signature" cols="72">Enjoy

Jakob
-- 
Jakob Bohm, CIO, Partner, WiseMo A/S.  <a class="moz-txt-link-freetext" href="https://www.wisemo.com">https://www.wisemo.com</a>
Transformervej 29, 2860 Søborg, Denmark.  Direct +45 31 13 16 10
This public discussion message is non-binding and may contain errors.
WiseMo - Remote Service Management for PCs, Phones and Embedded </pre>
  </body>
</html>