3

Now I'm trying to figure out best method for iterating over bits in FPGA. I'm using some variation of fast powering algorithm, a.k.a exponentiation by squaring (more precisely it's doubling and add algorithm for elliptic curve mathematics). To implement it on hardware, I know I must use FSM which does iteration. My problem is how to properly "handle" moving from bit to bit. My first thought was to switch order of bytes, but when my k = 17 is 32bit, I must discard first 27 bits, so it's rather stupid idea. Another concept was with "moving" 0001000 pattern and bitwise & it with number, but it also requires to find first nonzero bit.

TL&DR Got for example k = 17 (32bits, so: 17x0 10001) and want to iterate 5 times (that means I start iteration on first "real" bit of number) knowing each bit I iterate over.

Language doesn't matter - I need only the algorithm, not solution in specific language. However, if it is easily done in Verilog, I wouldn't mind. :P

Kacper Banasik
  • 191
  • 3
  • 13
  • 1
    With a little googling you can find a bunch of papers using the search term "doubling and add algorithm elliptic curve VHDL" and optionally throwing in FSM. You'd be hard pressed not being able to find a description of the algorithm. A data dependency graph is a close to abstract as a block diagram. –  Jun 21 '14 at 06:56
  • Yeah, but almost all algorithms describe loop from "l-1 to 0", and completely don't know how to get bits count l of number. I guess logarithm is rather bad idea. :P – Kacper Banasik Jun 21 '14 at 14:41
  • You can always iterate 32 times and ignore 27 results. – Qiu Jun 21 '14 at 16:05
  • Yes, but this would result in 27 wasted cycles - and that's why I'm asking here. To learn something new and better! :) – Kacper Banasik Jun 21 '14 at 21:14
  • Check out this question on finding the first non-zero bit: http://stackoverflow.com/questions/24166295/verilog-bit-change-location/24169188#24169188 – Guy Jun 25 '14 at 13:51
  • In your TL-DR example, the second two-digit number may be meant to be a _27_. `start iteration on first "real" bit` is that to say from the most significant bit? Do you need to know its position/weight? If the number isn't zero, that bit will be one… and the first iteration should give ample time to figure out the bits to follow/the position/weight of the leading 1-bit. – greybeard Sep 22 '16 at 20:05

4 Answers4

0

Don not code for FPGA but still:

  1. rewrite algorithm to iterate number x from LSB to MSB
  2. then in each iteration bit shift x right by 1 bit
  3. stop if x==0.

this way you have bit-scan inside your main loop and do not need additional cycles for it.

  • x!=0 is done easily by ORing all its bits together

C++ code example:

DWORD x = ...;
for (; x != 0; x >>= 1) 
{
    //here is your iteration loop stuff like:
    if (DWORD(x & 1) !=0 ) ...;   
}
Simon Richter
  • 28,572
  • 1
  • 42
  • 64
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • @Martin Thompson what does added space after bullet number mean in markdown? (do not take this the wrong way I just want to know the difference so I use it right in the future) – Spektre Jun 23 '14 at 14:05
  • It turns it into a "real" numbered list (where the browser formats it as an
  • ), rather than one done "by hand". You can even put the same number on each line and the browsers numbering will carry on (see latest edit :)
  • – Martin Thompson Jun 23 '14 at 16:13