Velocity Reviews > VHDL > Squaring of a binary number

# Squaring of a binary number

rickman
Guest
Posts: n/a

 07-21-2013
On 7/20/2013 7:57 PM, lokesh kumar wrote:
> Yes, I am also thinking there must be some ways to know about the number of loops. We can calculate small numbers like 5-bit or 6-bit by using our hand. But it is very hard to calculate for 163-bit.
>
> In your last message you told that, 3 loops are used to reduced. But it is not correct. After the first loop, you are getting two zeros in the beginning. So we do not need to apply the reduction polynomial value.So the 2nd loop is not needed. We can directly consider 1111101 and XOR with 100101 to get the 5-bit number.

I think you didn't look at my example carefully enough. If a = 11111
then c = 101010101 which is 9 bits. The IP is used once which removes
the one in the msb and as you say, the next bit is a zero which is not
changed. So we get two bits in the first step. But that still leaves
us with a seven bit number and each further application of the IP only
removes a single bit from the resulting value of c.

> http://www-brs.ub.ruhr-uni-bochum.de...deepS/diss.pdf
>
> I am quite not sure if you can access this link.

Yes, I downloaded it and can open it, just not in Acrobat, I have an
older copy which won't open this file, I have to use SumatraPDF reader.

> If you are unable, then please google "elliptic curve cryptography for constrained devices" and check out the first link. On page number 132 (figure 8..2), the multiplier circuit for 163 bit is given. The reduction polynomial for 163 bit is, F(x) = x^163 + x^7 + x^6 + x^3 +1

I think you mean figure 8.1 on page 132? That is what I am seeing.

> So if you closely take a look on the circuit, then you can see that almost all the parts are similar. But there are 3 extra XOR gates are connected. The first extra XOR gate is connected after C2 register ( it is for x^3 which is a part of irreducible polynomial). The second extra XOR gate is connected after C5 register ( for x^6 which is a part of irreducible polynomial) and the third extra XOR gate is connected after C6 register ( for x^7 term in the polynomial). The output of C162 is directly connected to the XOR gate before C0. Because we do not need to add XOR gates for x^162 and 1.
>
> Similarly for a 5-bit number, the reduction polynomial is F(x) = x^5 + x^2 + 1
> So in this case we need to connect only one extra XOR gate after C1 (or before C0). And the output from c4 is directly connected to the XOR gate before C0.
>
> In this case the reduction polynomial is attached. So the result if we take both A and B are of 5-bit numbers, then we will get the 5-bit number as an output.
> This is the basic operation for the multiplier.
>
> Now please look at the squaring circuit on page 133 (figure 8.3). I am unable to understand the circuit. I am not sure if the reduction polynomial is attached here. Please have a look on it and let me know if you understand.

I assume you mean figure 8.2 on page 133.

Sorry, I can follow your directions above, but I understand this less
than you do. However, I think there is a lot not shown in Figure 8.2 on
page 133. I guess most, if not each output bit has at least one XOR
gate. I am guessing that they have manually "unrolled" the loop of the
calculation and it distills to a series of XOR gates on each output. In
fact, I seem to recall doing exactly that with a CRC computation once.
We had to calculate CRC on some 32 bits of input at a time. I took the
single bit calculation and applied it 32 times algebraically to produce
a fairly messy, but accurate result. We can do that in VHDL without
manually calculating the coefficients. We just apply the IP ANDed with
the corresponding bit of the temp variable and the calculations will be
implemented.

So if you want a fully combinatorial circuit like they are describing in
the squaring circuit of Figure 8.2 I will write a loop of combinatorial
logic. I can only think the complication of the code you provided where
they copy other higher order bits into the "zero" bits of c must be a
way to simplify the circuit and save the logic of the zero bits.
Although things like zero inputs are often optimized automatically in
FPGA design tools making this unnecessary.

--

Rick

rickman
Guest
Posts: n/a

 07-21-2013
On 7/20/2013 7:57 PM, lokesh kumar wrote:
>
> Now please look at the squaring circuit on page 133 (figure 8.3). I am unable to understand the circuit. I am not sure if the reduction polynomial is attached here. Please have a look on it and let me know if you understand.

I almost forgot. To test the circuit will require that some examples be
provided with solutions. Can you come up with enough examples that
testing with those will give you confidence the circuit works correctly?

--

Rick

lokesh kumar
Guest
Posts: n/a

 07-21-2013
On Sunday, July 21, 2013 8:55:35 AM UTC+5:30, rickman wrote:
> On 7/20/2013 7:57 PM, lokesh kumar wrote:
>
> >

>
> > Now please look at the squaring circuit on page 133 (figure 8.3). I am unable to understand the circuit. I am not sure if the reduction polynomialis attached here. Please have a look on it and let me know if you understand.

>
>
>
> I almost forgot. To test the circuit will require that some examples be
>
> provided with solutions. Can you come up with enough examples that
>
> testing with those will give you confidence the circuit works correctly?
>
>
>
> --
>
>
>
> Rick

I think we can start with a small number like a 5-bit number. It would be easy to check.But for 163-bit, it is very difficult to check the result.So if we get an appropriate result for 5-bit then we can design 163-bit by using the same logic.As per your last message, if you can design a full combinatorial circuit for a 5-bit number by using the squaring circuit of figure 8..2, then we can check the results with several examples.If the circuit works fine then we can proceed for the implementation of 163-bit.

We can take the reduction polynomial as F(x)= x^5 + x^2 + 1 for a 5-bit number.

---
Lokesh

lokesh kumar
Guest
Posts: n/a

 07-22-2013
Hi Rick,

Finally I got the logic properly. And as per your comment I am sending you some more examples. Please have a look on the logic.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Example (Part-1)
----------------
Suppose we consider a 5-bit number, A = 11010
If we do the square of the number then,

AxA = 11010
11010
---------
00000
11010
00000
11010
11010
---------------
C= 101000100 (9-bit) (XOR operation is performed to add in this case, NOT the binary addition )

But we can make it a 10-bit number by adding a zero as MSB.
Hence we can write, C= 0101000100 (Now its a 10-bit number and the value is not changed)

Now if we can obtain the same value of C, only by adding adding zeros between all the bits of A.
In the example, A = 11010
So after adding zeros between all the bits, then we will get

1 0 1 0 0 0 1 0 0
| | | |

It is the value of C that we calculated earlier. Its of 9-bit.
But we can add one extra zero at the MSB to make it 0101000100 ( Now it is of 10-bits)

Or we can directly write

0 1 0 1 0 0 0 1 0 0
| | | | |
Like this we do not need to multiply A, 2 times if we need to calculate thesquare.
We can simply add zeros between all the bits of A to get the square.
--------------------------------------------------------------------------------
Step-1 (For design)

So in general lets take

A = a4a3a2a1a0 ( 5-bit number)

If we take the square of the number, then we will get

C = c9 c8 c7 c6 c5 c4 c3 c2 c1 c0 ( it is of 10 bit)

C = 0 a4 0 a3 0 a2 0 a1 0 a0 ( This is how zeros are added and value of C is calculate)

-----------------------------------------------------------------------------------------------
Example (part-2)
----------------
Now our main aim is to reduce the 10-bit result to 5-bit.
For that purpose, we use reduction polynomial (it is a standard value)

The reduction polynomial for a 5-bit number is : F(x) = x^5 + x^2 + 1

We can write the reduction polynomial as 100101 in binary form.
To reduce,

0101000100 (10-bit number, that we obtained)
10101 (reduction polynomial)
---------------
0000010100 (XOR operation)

Hence the first five MSB are zero, then if we do not consider these bits. Then it is a 5-bit number. It is our final result
--------------------------------------------------------------------------------------------------------
Step-2 (For design)

The value of reduction polynomial is = 100101

We have already got the 10-bit result.
We have to do the XOR operation of the 10-bit result with the reduction polynomial in the same way (as shown is the example)

For that purpose, we need to see if the MSB of the 10-bit result is "1" or not. If it is "1" then we will do the XOR operation with the reduction polynomial.
If the MSB of 10-bit number is zero, then we need to check the second MSB of it. If the second MSB is "1" then we need to do the XOR operation there.

We need to do this loop till we obtain the 5-bit result.

Now this is the final result.
--------------------------------------------------------------------------------
I have explained you the logic with an example. I have started with an example. I have used example (1st part) to show the result after squaring. And in Step-1, I have described the general logic. In example (part-2) , I haveshown the reduction of the result to 5-bit. And in Step-2, I have providedthe general logic.
So basically we need to follow both step-1 and step-2.

Now I hope everything is clear. Please help me to design the code for it now. Hope this time we will be able to do it.

Many Thanks!
Lokesh

lokesh kumar
Guest
Posts: n/a

 07-22-2013
Example (Part-1)
----------------
Suppose we consider a 5-bit number, A = 11010
If we do the square of the number then,

AxA = 11010
11010
---------
00000
11010
00000
11010
11010
---------------
C= 101000100 (9-bit) (XOR operation is performed to add in this case, NOT the binary addition )

But we can make it a 10-bit number by adding a zero as MSB.
Hence we can write, C= 0101000100 (Now its a 10-bit number and the value is not changed)

Now if we can obtain the same value of C, only by adding adding zeros between all the bits of A.
In the example, A = 11010
So after adding zeros between all the bits, then we will get

1 0 1 0 0 0 1 0 0
| | | |

It is the value of C that we calculated earlier. Its of 9-bit.
But we can add one extra zero at the MSB to make it 0101000100 ( Now it is of 10-bits)

Like this we do not need to multiply A, 2 times if we need to calculate the square.
We can simply add zeros between all the bits of A to get the square.
--------------------------------------------------------------------------------
Step-1 (For design)

So in general lets take

A = a4a3a2a1a0 ( 5-bit number)

If we take the square of the number, then we will get

C = c9 c8 c7 c6 c5 c4 c3 c2 c1 c0 ( it is of 10 bit)

C = 0 a4 0 a3 0 a2 0 a1 0 a0 ( This is how zeros are added and value of C is calculate)

-----------------------------------------------------------------------------------------------
Example (part-2)
----------------
Now our main aim is to reduce the 10-bit result to 5-bit.
For that purpose, we use reduction polynomial (it is a standard value)

The reduction polynomial for a 5-bit number is : F(x) = x^5 + x^2 + 1

We can write the reduction polynomial as 100101 in binary form.
To reduce,

0101000100 (10-bit number, that we obtained)
10101 (reduction polynomial)
---------------
0000010100 (XOR operation)

Hence the first five MSB are zero, then if we do not consider these bits. Then it is a 5-bit number. It is our final result
--------------------------------------------------------------------------------------------------------
Step-2 (For design)

The value of reduction polynomial is = 100101

We have already got the 10-bit result.
We have to do the XOR operation of the 10-bit result with the reduction polynomial in the same way (as shown is the example)

For that purpose, we need to see if the MSB of the 10-bit result is "1" or not. If it is "1" then we will do the XOR operation with the reduction polynomial.
If the MSB of 10-bit number is zero, then we need to check the second MSB of it. If the second MSB is "1" then we need to do the XOR operation there.

We need to do this loop till we obtain the 5-bit result.

Now this is the final result. How can I design the code? Please help me to implement.

Many Thanks!
Lokesh