-
Notifications
You must be signed in to change notification settings - Fork 1
/
diffi_helman.v
101 lines (82 loc) · 2.12 KB
/
diffi_helman.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
`timescale 10ns/1ns
module diffi_helman(
input wire clk,
input wire key_change,
input wire reset,
input wire [3:0] secret_key,
input wire [63:0] partner_key,
input wire val_p,
output wire [63:0] my_key,
output wire val_my_key,
output wire [127:0] K,
output wire val_K
);
reg [3:0] count1;
reg [3:0] count2;
reg [63:0] g;
reg [63:0] p;
reg val_my_key_reg;
reg val_K_reg;
reg [63:0] K_reg;
reg [63:0] K_reg1;
reg [63:0] div2;
reg work;
reg [3:0] secret_key_reg;
assign K = {{64{1'b0}}, K_reg1};
assign val_K = val_K_reg;
assign my_key = g;
assign val_my_key = val_my_key_reg;
always @(posedge clk)
secret_key_reg <= key_change ? secret_key : secret_key_reg;
always @(posedge clk)
if (reset || val_K)
work <= 1'b0;
else if (key_change)
work <= 1;
else if (val_K)
work <= 1'b0;
always @(posedge clk)
if (reset || val_K)
g <= 64'd37;
else if (~(count1 == secret_key_reg ) & work)
g <= g * 64'd37;
always @(posedge clk)
if (reset || val_K)
count1 <= 4'b1;
else if (~(count1 == secret_key_reg) & work)
count1 <= count1 + 1'b1;
always @(posedge clk)
if (reset)
p <= 64'b100110001010000000110001;
always @(posedge clk)
if (reset || val_K)
val_my_key_reg <= 1'b0;
else if ((count1 == secret_key_reg) & work)
val_my_key_reg <= 1'b1;
else
val_my_key_reg <= 1'b0;
always @(posedge clk)
if (reset || val_K)
count2 <= 4'b0;
else if (~(count2 == secret_key_reg) & (val_p) & work)
count2 <= count2 + 1'b1;
always @(posedge clk)
if (reset || val_K)
K_reg <= 64'b1;
else if (~(count2 == secret_key_reg) & (val_p) & work)
K_reg <= K_reg * partner_key;
always @(posedge clk)
if (reset || val_K)
val_K_reg <= 1'b0;
else if ((count2 == secret_key_reg) && (K_reg > p) && (K_reg1 < p) & work)
val_K_reg <= 1'b1;
else if ((count2 == secret_key_reg) & work)
val_K_reg <= 1'b1;
else
val_K_reg <= 1'b0;
always @(posedge clk)
if ((count2 == secret_key_reg) && (K_reg > p) && work)
K_reg1 <= K_reg % p;
else if (count2 == secret_key_reg && work)
K_reg1 <= K_reg;
endmodule