VHDL Synthesis, can`t make code be synthesizable - encryption

I have some code on VHDL language, this code male some encryption/decryption operation. Help me please make this code be synthesizable because Xilinx IDE told me that
line 82: Operator must have constant modulo operand.
This is my code. Maybe you have some wish for me how I can refactor it.
library ieee;
use ieee.std_logic_1164.all;
use ieee.STD_LOGIC_TEXTIO.all;
use ieee.STD_LOGIC_UNSIGNED.all;
use ieee.STD_LOGIC_SIGNED.all;
use ieee.NUMERIC_STD.all;
use ieee.NUMERIC_BIT.all;
use ieee.std_logic_arith.all;
use ieee.MATH_REAL.all;
use ieee.MATH_COMPLEX.all;
entity comp is
port(
clk : in STD_LOGIC;
word : in INTEGER;
n : inout INTEGER;
v1 : out INTEGER;
v2 : out INTEGER;
v3 : out INTEGER;
v4 : out INTEGER;
v5 : out INTEGER;
v6 : out INTEGER;
v7 : out INTEGER;
v8 : out INTEGER
);
end comp;
architecture arch of comp is
---- Signal declarations used on the diagram ----
signal g1 : INTEGER := 1;
signal g2 : INTEGER := 1;
signal g3 : INTEGER := 1;
signal k1 : INTEGER := 0;
signal k2 : INTEGER := 0;
signal k3 : INTEGER := 0;
signal m1 : INTEGER;
signal m2 : INTEGER;
signal m3 : INTEGER;
---signal n : INTEGER;
signal p1 : INTEGER;
signal p1_g1 : INTEGER;
signal p2 : INTEGER;
signal p2_g2 : INTEGER;
signal p3 : INTEGER;
signal p3_g3 : INTEGER;
signal sqrt1 : INTEGER;
signal sqrt2 : INTEGER;
signal sqrt3 : INTEGER;
signal w : INTEGER;
---signal word : INTEGER;
begin
---- Processes ----
read : process (clk)
begin
if clk'event and clk = '1' then
n <= p1 * p2 * p3;
w <= word * word MOD n;
sqrt1 <= w MOD p1;
sqrt2 <= w MOD p2;
sqrt3 <= w MOD p3;
if k1*k1 MOD p1 /= sqrt1 then
k1 <= k1 + 1;
else
g1 <= k1;
end if;
if k2*k2 MOD p2 /= sqrt2 then
k2 <= k2 + 1;
else
g2 <= k2;
end if;
if k3*k3 MOD p3 /= sqrt3 then
k3 <= k3 + 1;
else
g3 <= k3;
end if;
p1_g1 <= p1 - g1;
p2_g2 <= p2 - g2;
p3_g3 <= p3 - g3;
m1 <= n / p1;
m2 <= n / p2;
m3 <= n / p3;
v1 <= (-m1 * g1 + m2 * g2 + m3 * g3) MOD n;
v2 <= (-m1 * g1 + m2 * g2 + m3 * p3_g3) MOD n;
v3 <= (-m1 * g1 + m2 * p2_g2 + m3 * g3) MOD n;
v4 <= (-m1 * g1 + m2 * p2_g2 + m3 * p3_g3) MOD n;
v5 <= (-m1 * p1_g1 + m2 * g2 + m3 * g3) MOD n;
v6 <= (-m1 * p1_g1 + m2 * g2 + m3 * p3_g3) MOD n;
v7 <= (-m1 * p1_g1 + m2 * p2_g2 + m3 * g3) MOD n;
v8 <= (-m1 * p1_g1 + m2 * p2_g2 + m3 * p3_g3) MOD n;
end if;
end process;
end arch;

In VHDL/verilog MOD ,REM and DIVISION operators are not synthesizable. Division is only possible when the second operand is a power of 2.
even if you want to traverse your code using a loop to find the remainder you need to put a constant in the condition segment to get your code synthesized.

Related

A potential bug in the induction strategy of Frama-C 24.0

I am working on the following proof and the invariant result_val is proved with an induction strategy on i using begin as the base case.
The sup case is trying to prove true which holds trivially using Frama-C 24.0. But when I switch to 25.0, it tries to prove a seemingly more complicated condition, which looks closer to a correct inductive inference because it did the weakest precondition computation explicitly.
However, all SMT solvers I tried cannot prove the condition generated by Frama-C 25.0.
I am a bit worried about the correctness of version 24.0's result because using true as the inductive proof goal seems to be unlikely. Can anyone hint to me at what happened? Is that a bug in 24.0 or just some difference in the implementation?
#include <stdbool.h>
#define SIZE 1000
bool data[SIZE] ;
/*#
logic integer count(integer begin, integer end)=
begin >= end ? 0 : (data[begin]==true) ? count(begin+1, end)+1 : count(begin+1, end);
*/
/*#
requires SIZE > begin >= 0;
requires SIZE >= end >= 0;
requires begin <= end;
assigns \nothing;
ensures \result == count(begin, end);
*/
unsigned int occurrences_of(int begin, int end)
{
unsigned int result = 0;
/*#
loop invariant i_bound: begin <= i <= end;
loop invariant result_bound: 0 <= result <= i-begin;
loop invariant result_val: result == count(begin, i);
loop assigns i, result;
loop variant end-i;
*/
for (unsigned int i = begin; i < end; ++i){
result += (data[i] == true) ? 1 : 0;
}
return result;
}
Below is the result from Frama-c 24.0
Proof:
Goal Invariant 'result_val' (preserved) (Induction: proved)
+ Goal Induction (Base) (proved)
+ Goal Induction (Induction (sup)) (proved)
+ Goal Induction (Induction (inf)) (proved)
Qed.
--------------------------------------------------------------------------------
Goal Induction (Induction (sup)):
Prove: true.
Below is the result from Frama-c 25.0
--------------------------------------------------------------------------------
Proof:
Goal Invariant 'result_val' (preserved) (Induction: pending)
+ Goal Induction (Base) (proved)
+ Goal Induction (Induction (sup)) (pending)
+ Goal Induction (Induction (inf)) (proved)
End.
--------------------------------------------------------------------------------
Goal Induction (Induction (sup)):
Let x_0 = to_uint32(end#L1).
Let x_1 = to_uint32(tmp#L12).
Let x_2 = data#L1[i#L6].
Let x_3 = result#L6.
Let x_4 = result#L13.
Let x_5 = to_uint32(1 + i#L6).
Assume {
Have: begin#L1 < i#L6.
Have: i#L6 <= end#L1.
Have: i#L6 < x_0.
Have: 0 <= x_3.
Have: x_5 <= end#L1.
Have: begin#L1 <= x_5.
Have: (begin#L1 + x_3) <= i#L6.
Have: (begin#L1 + x_4) <= x_5.
Have: is_uint32(i#L6).
Have: is_bool(x_2).
Have: is_uint32(x_3).
Have: if (x_2 = 1) then (tmp#L12 = 1) else (tmp#L12 = 0).
Have: forall i_0 : Z. let x_6 = L_count(data#L1, begin#L1, i_0) in
let x_7 = to_uint32(1 + i_0) in let x_8 = to_uint32(x_1 + x_6) in
let x_9 = data#L1[i_0] in ((i_0 <= end#L1) -> ((begin#L1 <= i_0) ->
((i_0 < i#L6) -> ((i_0 < x_0) -> ((0 <= x_6) -> ((x_7 <= end#L1) ->
((begin#L1 <= x_7) -> (((begin#L1 + x_6) <= i_0) ->
(((begin#L1 + x_8) <= x_7) -> (is_uint32(i_0) -> (is_bool(x_9) ->
(is_uint32(x_6) ->
((if (x_9 = 1) then (tmp#L12 = 1) else (tmp#L12 = 0)) ->
(L_count(data#L1, begin#L1, x_7) = x_8)))))))))))))).
[...]
Stmt { L6: }
Stmt { tmp = tmp_0; }
Stmt { L12: result = x_4; }
Stmt { L13: }
}
Prove: L_count(data#L1, begin#L1, x_5) = x_4.
Goal id: typed_occurrences_of_loop_invariant_result_val_preserved
Short id: occurrences_of_loop_invariant_result_val_preserved
--------------------------------------------------------------------------------
Prover Alt-Ergo 2.4.2: Timeout (Qed:52ms) (10s).
A bug on the typing of the induction tactic was indeed fixed between Frama-C 24 and 25 (https://git.frama-c.com/pub/frama-c/-/commit/6058453cce2715f7dcf9027767559f95fb3b1679). And the symptom was indeed that the tactic could generate ill-typed formulas with true instead of a term.
Proving this example in not that easy. For two main reasons:
the function and the definition work in the opposite directions,
the definition does not have an optimal expression for reasoning.
However, one can write a lemma function to solve the problem:
#include <stdbool.h>
#define SIZE 1000
bool data[SIZE] ;
/*#
logic integer count(integer begin, integer end)=
begin >= end ? 0 : ((data[begin]==true) ? count(begin+1, end)+1 : count(begin+1, end));
*/
/*# ghost
/# requires begin < end ;
assigns \nothing ;
ensures count(begin, end) == ((data[end-1]==true) ? count(begin, end-1)+1 : count(begin, end-1));
#/
void lemma(bool* d, int begin, int end){
/# loop invariant begin <= i < end ;
loop invariant count(i, end) == ((data[end-1]==true) ? count(i, end-1)+1 : count(i, end-1));
loop assigns i ;
loop variant i - begin ;
#/
for(int i = end-1 ; i > begin ; i--);
}
*/
/*#
requires SIZE > begin >= 0;
requires SIZE >= end >= 0;
requires begin <= end;
assigns \nothing;
ensures \result == count(begin, end);
*/
unsigned int occurrences_of(int begin, int end)
{
unsigned int result = 0;
/*#
loop invariant i_bound: begin <= i <= end;
loop invariant result_bound: 0 <= result <= i-begin;
loop invariant result_val: result == count(begin, i);
loop assigns i, result;
loop variant end-i;
*/
for (unsigned int i = begin; i < end; ++i){
result += (data[i] == true) ? 1 : 0;
//# ghost lemma(data, begin, i+1);
}
return result;
}
I'd suggest to use the following definition:
/*#
logic integer count(integer begin, integer end)=
begin >= end ? 0 : ((data[end-1]==true) ? 1 : 0) + count(begin, end-1);
*/
It works in the same direction as the function and avoids the duplication of the term count(begin, end-1) which makes reasoning easier.

CPLEX give me this error " V23 has never been used"

A part of input is:
tuple copie_macchine {
int macchina;
int copia1;
int copia2;
int copia3;
}
{copie_macchine} copie = ...;
int macc [I1][J] = ...;
{int} s = {1,2,4,5,7,11,12,13,14,15,16,17,18,19};
macc = [[1, 10],
[1, 1],
[3, 3],
[0, 4]];
copie = {<3,3,4,0>,
<6,7,8,9>,
<8,11,12,0>,
<9,13,14,0>,
<10,15,16,0>,
<20,26,27,28>};
dvar boolean y[I][J][M];
and I write in Cplex this code: the algorithm assigns to the variable y 1 if the value macc [i] [j] is in the set s otherwise it must choose to assign the value 1 either to copy 1 or to copy 2 or to copy
forall (j in J)
forall (i in I1 : macc [i][j] in s)
forall (i1 in I : i1==i)
forall (m in M)
V22: y[i1][j][m] == 1;
forall (j in J)
forall (c in copie : c.copia3!=0)
forall (i in I1 : macc[i][j] == c.macchina)
forall (i1 in I : i1==i)
forall (m in M)
V23: y[i1][j][c.copia1] == 1 || y[i1][j][c.copia2] == 1 || y[i1][j][c.copia3] == 1;
but I have the error "V23 has never been used". How can I solve?
your line that contains V23 is never used.
Let me share a smaller example which replicates this warning:
dvar int+ x;
subject to
{
x<=10;
forall(i in 1..2:i>=5)
{
ct:(x==2) ;
}
}
gives
ct has never been used

Pascal error: illegal qualifier

The code below is supposed to create a 3x4 matrix and and print the sum of all numbers each row. However, upon compiling it, I keep getting the following errors:
jdoodle.pas(26,25) Error: Illegal qualifier
jdoodle.pas(33,32) Error: Illegal qualifier
jdoodle.pas(41,32) Error: Illegal qualifier
jdoodle.pas(48,24) Error: Illegal qualifier
jdoodle.pas(56,4) Fatal: There were 4 errors compiling module, stopping
Fatal: Compilation aborted.
The lines in question are:
line 26: readln (A[i, j]);
line 33: B[i] := B[i] + A[i, j]
line 41: C[J] := C [J] + A[i,j]
line 48: write (A[i, j]:5);
Could anyone clarify this for me please? The main problem is, that I don't really know what that error code means. I have noticed that 'A[i,j]' is in all lines that are mentioned in the errors, but I just can't find out what is wrong with it.Any help would be greatly appreciated!
And here is my complete code:
Program Matrix (input, output);
const
ZEILENMAX = 3;
SPALTENMAX = 4;
type
tZeile = 1..ZEILENMAX;
tSpalte = 1..SPALTENMAX;
tMatrix = array[tZeile] of integer;
tZeilensumme = array [tZeile] of integer;
tSpaltensumme = array [tSpalte] of integer;
var
A : tMatrix;
B : tZeilensumme;
C : tSpaltensumme;
i : tZeile;
j : tSpalte;
begin
for i := 1 to ZEILENMAX do
for j := 1 to SPALTENMAX do
readln (A[i, j]);
for i := 1 to ZEILENMAX do
begin
B[i] := 0;
for j := 1 to SPALTENMAX do
B[i] := B[i] + A[i, j]
end;
for j := 1 to SPALTENMAX do
begin
C[j] := 0;
for i := 1 to Zeilenmax do
C[J] := C [J] + A[i,j]
end;
writeln;
for i := 1 to ZEILENMAX do
begin
for j := 1 to SPALTENMAX do
write (A[i, j]:5);
writeln (B[i]:10)
end;
writeln;
for j:= 1 to SPALTENMAX do
write (C[j]:5);
writeln
end.
Let us transcript the following piece of code:
const
ZEILENMAX = 3;
type
tZeile = 1..ZEILENMAX;
tMatrix = array[tZeile] of integer;
var
A : tMatrix;
it is equal to:
type
tZeile = 1..3;
tMatrix = array[tZeile] of integer;
var
A : tMatrix;
and this is similar to:
type
tMatrix = array[1..3] of integer;
var
A : tMatrix;
Members of array A can be accessed by a single index:
A[1] := 0; but not A[1,1] := 1 or alike.

3d line-intersection code not working properly

I created this piece of code to get the intersection of two 3d line-segments.
Unfortunately the result of this code is inaccurate, the intersection-point is not always on both lines.
I am confused and unsure what I'm doing wrong.
Here is my code:
--dir = direction
--p1,p2 = represents the line
function GetIntersection(dirStart, dirEnd, p1, p2)
local s1_x, s1_y, s2_x, s2_y = dirEnd.x - dirStart.x, dirEnd.z - dirStart.z, p2.x - p1.x, p2.z - p1.z
local div = (-s2_x * s1_y) + (s1_x * s2_y)
if div == 0 then return nil end
local s = (-s1_y * (dirStart.x - p1.x) + s1_x * (dirStart.z - p1.z)) / div
local t = ( s2_x * (dirStart.z - p1.z) - s2_y * (dirStart.x - p1.x)) / div
if (s >= 0 and s <= 1 and t >= 0 and t <= 1) and (Vector(dirStart.x + (t * s1_x), 0, dirStart.z + (t * s1_y)) or nil) then
local v = Vector(dirStart.x + (t * s1_x),0,dirStart.z + (t * s1_y))
return v
end
end
This is example of Delphi code to find a distance between two skew lines in 3D. For your purposes it is necessary to check that result if small enough value (intersection does exist), check that s and t parameters are in range 0..1, then
calculate point using parameter s
Math of this approach is described in 'the shortest line...' section of Paul Bourke page
VecDiff if vector difference function, Dot id scalar product function
function LineLineDistance(const L0, L1: TLine3D; var s, t: Double): Double;
var
u: TPoint3D;
a, b, c, d, e, det, invdet:Double;
begin
u := VecDiff(L1.Base, L0.Base);
a := Dot(L0.Direction, L0.Direction);
b := Dot(L0.Direction, L1.Direction);
c := Dot(L1.Direction, L1.Direction);
d := Dot(L0.Direction, u);
e := Dot(L1.Direction, u);
det := a * c - b * b;
if det < eps then
Result := -1
else begin
invdet := 1 / det;
s := invdet * (b * e - c * d);
t := invdet * (a * e - b * d);
Result := Distance(PointAtParam(L0, s), PointAtParam(L1, t));
end;
end;
As far as I can tell your code is good. I've implemented this in javascript at https://jsfiddle.net/SalixAlba/kkrc9kcf/
and it seems to work for all the cases I can think of.
The only changes I've done is to change things to work in javascript rather than lua. The final condition was commented out
function GetIntersection(dirStart, dirEnd, p1, p2) {
var s1_x = dirEnd.x - dirStart.x;
var s1_y = dirEnd.z - dirStart.z;
var s2_x = p2.x - p1.x;
var s2_y = p2.z - p1.z;
var div = (-s2_x * s1_y) + (s1_x * s2_y);
if (div == 0)
return new Vector(0,0);
var s = (-s1_y * (dirStart.x - p1.x) + s1_x * (dirStart.z - p1.z)) / div;
var t = ( s2_x * (dirStart.z - p1.z) - s2_y * (dirStart.x - p1.x)) / div;
if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
//and (Vector(dirStart.x + (t * s1_x), 0, dirStart.z + (t * s1_y)) or nil) then
var v = new Vector(
dirStart.x + (t * s1_x),
dirStart.z + (t * s1_y));
return v;
}
return new Vector(0,0);
}
Mathmatically it makes sense. If A,B and C,D are your two lines. Let s1 = B-A, s2 = C-D. A point of the line AB is given by A + t s1 and a point on the line CD is given by C + s s2. For an intersection we require
A + t s1 = C + s s2
or
(A-C) + t s1 = s s2
You two formula for s, t are found by taking the 2D cross product with each of the vectors s1 and s2
(A-C)^s1 + t s1^s1 = s s2^s1
(A-C)^s2 + t s1^s2 = s s2^s2
recalling s1^s1=s2^s2=0 and s2^s1= - s1^s2 we get
(A-C)^s1 = s s2^s1
(A-C)^s2 + t s1^s2 = 0
which can be solved to get s and t. This matches your equations.

Why we use CORDIC gain?

I'm studying the cordic. And I found the cordic gain. K=0.607XXX.
From CORDIC, K_i = cos(tan^-1(2^i)).
As I know the K is approched 0.607xxx.when I is going to infinity
this value come up with from all K multiplying.
I understand the reason of exist each k. But I am curioused Where does it used ? Why we use that value K=0.607xx?
The scale factor for the rotation mode of the circular variant of CORDIC can easily be established from first principles. The idea behind CORDIC is to take a point on the unit circle and rotate it, in steps, through the angle u whose sine and cosine we want to determine.
To that end we define a set of incremental angles a0, ..., an-1, such that ak = atan(0.5k). We sum these incremental angles appropriately into a partial sum of angles sk, such than sn ~= u. Let yk = cos(sk) and xk = sin(sk). If in a given step k we rotate by ak, we have
yk+1 = cos (sk+1) = cos (sk + ak)
xk+1 = sin (sk+1) = sin (sk + ak)
we can compute xk+1 and yk+1 from xk and yk as follows:
yk+1 = yk * cos (ak) - xk * sin (ak)
xk+1 = xk * cos (ak) + yk * sin (ak)
Considering that we may both add and subtract ak, and that tan(ak) = sin(ak)/cos(ak), we get:
yk+1 = cos (ak) * (yk ∓ xk * tan(ak)) = cos (sk+1)
xk+1 = cos (ak) * (xk ± yk * tan(ak)) = sin (sk+1)
To simplify this computation, we can leave out the multiplication with cos(ak) in every step, which gives us our CORDIC iteration scheme:
yk+1 = y ∓ xk * tan(ak)
xk+1 = x ± yk * tan(ak)
Because of our choice of ak, the multiplications with tan(ak) turn into simple right shifts if we compute in fixed-point arithmetic. Because we left off the factors cos(ak), we wind up with
yn ~= cos(u) * (1 / (cos (a0) * cos (a1) * ... * cos (an))
xn ~= sin(u) * (1 / (cos (a0) * cos (a1) * ... * cos (an))
The factor f = cos (a0) * cos (a1) * ... * cos (an) is 0.607..., as already noted. We incorporate it into the computation by setting the starting values
y0 = f * cos(0) = f
x0 = f * sin(0) = 0
Here is C code that shows the entire computation in action, using 16-bit fixed-point arithmetic. Input angles are scaled such that 360 degrees correspond to 216, while sine and cosine outputs are scaled such that 1 corresponds to 215.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* round (atand (0.5**i) * 65536/360) */
static const short a[15] =
{
0x2000, 0x12e4, 0x09fb, 0x0511,
0x028b, 0x0146, 0x00a3, 0x0051,
0x0029, 0x0014, 0x000a, 0x0005,
0x0003, 0x0001, 0x0001
};
#define swap(a,b){a=a^b; b=b^a; a=a^b;}
void cordic (unsigned short u, short *s, short *c)
{
short x, y, oldx, oldy, q;
int i;
x = 0;
y = 0x4dba; /* 0.60725 */
oldx = x;
oldy = y;
q = u >> 14; /* quadrant */
u = u & 0x3fff; /* reduced angle */
u = -(short)u;
i = 0;
do {
if ((short)u < 0) {
x = x + oldy;
y = y - oldx;
u = u + a[i];
} else {
x = x - oldy;
y = y + oldx;
u = u - a[i];
}
oldx = x;
oldy = y;
i++;
/* right shift of signed negative number implementation defined in C */
oldx = (oldx < 0) ? (-((-oldx) >> i)) : (oldx >> i);
oldy = (oldy < 0) ? (-((-oldy) >> i)) : (oldy >> i);
} while (i < 15);
for (i = 0; i < q; i++) {
swap (x, y);
y = -y;
}
*s = x;
*c = y;
}
int main (void)
{
float angle;
unsigned short u;
short s, c;
printf ("angle in degrees [0,360): ");
scanf ("%f", &angle);
u = (unsigned short)(angle * 65536.0f / 360.0f + 0.5f);
cordic (u, &s, &c);
printf ("sin = % f (ref: % f) cos = % f (ref: % f)\n",
s/32768.0f, sinf(angle/360*2*3.14159265f),
c/32768.0f, cosf(angle/360*2*3.14159265f));
return EXIT_SUCCESS;
}

Resources