-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_mult_struct.vhd
More file actions
156 lines (130 loc) · 3.99 KB
/
matrix_mult_struct.vhd
File metadata and controls
156 lines (130 loc) · 3.99 KB
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
-- entity name: matrix_mult_struct
-- Stephen Carter - 260500858
-- Copyright (C) 2016 Stephen Carter
-- Version 1.0
-- Author: Stephen Carter; stephen.carter@mail.mcgill.ca
-- Date: Jan. 10, 2016
-- This circuit performs matrix multiplication of a 1x3 vector and a 3x3 matrix(key_loader)
-- This circuit is a structural implementation of matrix_mult
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all; -- arithmetic operators
use ieee.std_logic_unsigned.all; -- Treat vectors as unsigned
LIBRARY lpm;
USE lpm.lpm_components.all; -- used to peform required calculations
entity matrix_mult_struct is
port( p1 : in std_logic_vector(3 downto 0); -- inputs of 1x3 vector (plaintext)
p2 : in std_logic_vector(3 downto 0);
p3 : in std_logic_vector(3 downto 0);
k11 : in std_logic_vector(3 downto 0); -- inputs of 3x3 key_loader (encryption key)
k12 : in std_logic_vector(3 downto 0);
k13 : in std_logic_vector(3 downto 0);
k21 : in std_logic_vector(3 downto 0);
k22 : in std_logic_vector(3 downto 0);
k23 : in std_logic_vector(3 downto 0);
k31 : in std_logic_vector(3 downto 0);
k32 : in std_logic_vector(3 downto 0);
k33 : in std_logic_vector(3 downto 0);
clk : in std_logic; -- clock for synch
c1 : out std_logic_vector(3 downto 0); -- output vector (ciphertext)
c2 : out std_logic_vector(3 downto 0);
c3 : out std_logic_vector(3 downto 0)
);
end matrix_mult_struct;
architecture structural of matrix_mult_struct is
Signal p1_temp,p2_temp,p3_temp : std_logic_vector(3 downto 0); -- temp signals for the input (registers)
Signal c1_temp,c2_temp,c3_temp : std_logic_vector(3 downto 0); -- temp signals for the output (registers)
component vector_mult
Port( p1 : in std_logic_vector(3 downto 0); -- inputs of 1x3 vector (plaintext)
p2 : in std_logic_vector(3 downto 0);
p3 : in std_logic_vector(3 downto 0);
k1x : in std_logic_vector(3 downto 0); -- inputs of keyloader column (encryption key)
k2x : in std_logic_vector(3 downto 0);
k3x : in std_logic_vector(3 downto 0);
clk : in std_logic; -- clock for synch
cx : out std_logic_vector(3 downto 0) -- output (ciphertext)
);
end component;
begin
p1_reg : lpm_ff -- stores p1 in register
GENERIC MAP (
lpm_width => 4,
lpm_fftype => "DFF"
)
PORT MAP (
data => p1,
clock => clk,
q => p1_temp
);
p2_reg : lpm_ff -- stores p2 in register
GENERIC MAP (
lpm_width => 4,
lpm_fftype => "DFF"
)
PORT MAP (
data => p2,
clock => clk,
q => p2_temp
);
p3_reg : lpm_ff -- stores p3 in register
GENERIC MAP (
lpm_width => 4,
lpm_fftype => "DFF"
)
PORT MAP (
data => p3,
clock => clk,
q => p3_temp
);
c1_reg : lpm_ff -- stores c1 in register
GENERIC MAP (
lpm_width => 4,
lpm_fftype => "DFF"
)
PORT MAP (
data => c1_temp,
clock => clk,
q => c1
);
c2_reg : lpm_ff -- stores c2 in register
GENERIC MAP (
lpm_width => 4,
lpm_fftype => "DFF"
)
PORT MAP (
data => c2_temp,
clock => clk,
q => c2
);
c3_reg : lpm_ff -- stores c3 in register
GENERIC MAP (
lpm_width => 4,
lpm_fftype => "DFF"
)
PORT MAP (
data => c3_temp,
clock => clk,
q => c3
);
-- performs calculations for first column of keyloader
inst1 : vector_mult PORT MAP(p1 => p1_temp,p2 => p2_temp, p3 => p3_temp,
k1x => k11,
k2x => k21,
k3x => k31,
clk => clk,
cx => c1_temp);
-- performs calculations for second column of keyloader
inst2 : vector_mult PORT MAP(p1 => p1_temp,p2 => p2_temp, p3 => p3_temp,
k1x => k12,
k2x => k22,
k3x => k32,
clk => clk,
cx => c2_temp);
-- performs calculations for third column of keyloader
inst3 : vector_mult PORT MAP(p1 => p1_temp,p2 => p2_temp, p3 => p3_temp,
k1x => k13,
k2x => k23,
k3x => k33,
clk => clk,
cx => c3_temp);
end structural;