-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext_euclidean.v
247 lines (211 loc) · 4.57 KB
/
ext_euclidean.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//extended euclidean algorithm
`timescale 1ns / 1ps
//Inverse
module ext_euclidean
#(
parameter WIDTH = 512
)(
input aclk, // Clock
input aresetn, // Asynchronous reset active low
input wire [WIDTH-1:0] Multiplicative_Num,
input wire [WIDTH-1:0] Modular,
output reg [WIDTH-1:0] Multiplicative_Inv_Mod,
output reg [WIDTH-1:0] Multiplicative_Inv_Num,
output reg Multiplicative_Inv_Done
//output reg error //if gcd of two numbers are != 1 ,output =1
);
//----------------------------------------------------------------
// Parameters.
//----------------------------------------------------------------
localparam STATE_INIT = 3'd0 ;
localparam STATE_RUN = 3'd1 ;
localparam STATE_NEXT = 3'd2 ;
//---------------------------------------------------------------------------------------------------------------------
// Internal wires and registers
//---------------------------------------------------------------------------------------------------------------------
reg [WIDTH-1:0] X_1;
reg [WIDTH-1:0] X_2;
reg [WIDTH-1:0] Y_1;
reg [WIDTH-1:0] Y_2;
reg [WIDTH-1:0] q,r,a,b,x,y;
reg start;
reg [2:0] state;
//----------------------------------------------------------------
// assignments for ports.
//----------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
// Implementation
//---------------------------------------------------------------------------------------------------------------------
/*always @*
begin : start_algo
start = 1'b1;
end // key_mem_read*/
//start
always @(Multiplicative_Num,Modular) begin
start = 1'b1;
Multiplicative_Inv_Done <= 1'b0;
end
always @(posedge aclk ) begin
if(!aresetn) begin
start <= 1'b0;
end else begin
case (state)
STATE_RUN :
begin
start <= 1'b0;
end
default : /* default */;
endcase
end
end
//FSM
always @(posedge aclk ) begin
if(!aresetn) begin
state <= STATE_INIT;
end else begin
case (state)
STATE_INIT :
begin
if (start) begin
state <= STATE_RUN;
end
end
STATE_RUN :
begin
if (b!=512'b0) begin
state <= STATE_NEXT;
end else begin
state <= STATE_INIT;
end
end
STATE_NEXT :
begin
state <= STATE_RUN;
end
/*STATE_NEXT_01 :
begin
end*/
default : /* default */;
endcase
end
end
//x,y
always @(posedge aclk ) begin
if(!aresetn) begin
x <= 512'd0;
y <= 512'd0;
end else begin
case (state)
STATE_INIT :
begin
x <= 512'd0;
y <= 512'd0;
end
STATE_NEXT :
begin
x <= X_2 - (q*X_1);
y <= Y_2 - (q*Y_1);
end
/*STATE_NEXT_01 :
begin
end*/
default : /* default */;
endcase
end
end
always @(posedge aclk ) begin
if(!aresetn) begin
a <= Modular;
b <= Multiplicative_Num;
X_1 <= 512'd0;
X_2 <= 512'd1;
Y_1 <= 512'd1;
Y_2 <= 512'd0;
end else begin
case (state)
STATE_INIT :
begin
a <= Modular;
b <= Multiplicative_Num;
X_1 <= 512'd0;
X_2 <= 512'd1;
Y_1 <= 512'd1;
Y_2 <= 512'd0;
end
default : /* default */;
endcase
end
end
always @(x,y) begin
a = b;
b = r;
X_2 = X_1;
X_1 = x;
Y_2 = Y_1;
Y_1 = y;
end
//q,r
always @(posedge aclk )
begin
if(!aresetn) begin
q <= 512'd0;
r <= 512'd0;
Multiplicative_Inv_Num <= 512'd0;
Multiplicative_Inv_Mod <= 512'd0;
end else begin
case (state)
STATE_RUN :
begin
if (b!=512'b0) begin
q <= a/b;
r <= a%b;
end else begin
//check for sign bit
if (Y_2[511]) begin //if number if negetive sign bit is one
Multiplicative_Inv_Num <= Modular+Y_2;
end else begin
Multiplicative_Inv_Num <= Y_2;
end
if (X_2[511]) begin //if number if negetive sign bit is one
Multiplicative_Inv_Mod <= Multiplicative_Num+X_2;
end else begin
Multiplicative_Inv_Mod <= X_2;
end
/* if (a) begin
error <= 1'b0;
end else begin
error <= 1'b1;
end*/
end
end
default : /* default */;
endcase
end
end
//done
always @(posedge aclk )
begin
if(!aresetn) begin
Multiplicative_Inv_Done <= 1'b0;
end else begin
case (state)
STATE_RUN :
begin
if (b!=512'b0) begin
Multiplicative_Inv_Done <= 1'b0;
end else begin
Multiplicative_Inv_Done <= 1'b1;
end
end
default : ;
endcase
end
end
/*always @(b) begin
if (b!=512'b0) begin
Multiplicative_Inv_Done <= 1'b0;
end else begin
Multiplicative_Inv_Done <= 1'b1;
end
end*/
endmodule //ext_euclidean