Skip to content

Commit

Permalink
✨ MixCols and InvMixCols and 7seg done (#1)
Browse files Browse the repository at this point in the history
* mixCols and InvMixCols and 7seg done

* Delete InvMixColumns_tb.v.bak

deleted unneeded file

* Update mixColumns.v

* Update InvMixColumns.v
  • Loading branch information
AhmedAmrNabil authored Apr 21, 2024
1 parent 5efc772 commit d22c92b
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
20 changes: 20 additions & 0 deletions DisplayDecoder.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module DisplayDecoder(in,display7);
input [3:0]in;
output [6:0]display7;

always @(in)
case(in)
0 : display7 = 7'b0000001;
1 : display7 = 7'b1001111;
2 : display7 = 7'b0010010;
3 : display7 = 7'b0000110;
4 : display7 = 7'b1001100;
5 : display7 = 7'b0100100;
6 : display7 = 7'b0100000;
7 : display7 = 7'b0001111;
8 : display7 = 7'b0000000;
9 : display7 = 7'b0000100;
default : display7 = 7'b1111111;
endcase

endmodule
63 changes: 63 additions & 0 deletions InvMixColumns.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module InvMixColumns (stateIn,stateOut);
input[127:0] stateIn;
output[127:0] stateOut;

//mul2 function that multiply x by 2^n and fixes the overflow
function [7:0] mul2(input [7:0]in,input integer n);
integer i;
begin
for(i = 0; i < n; i = i + 1)begin
if(in[7] == 1) in = (in << 1) ^ 8'h1B;
else in = in << 1;
end
mul2 = in;
end
endfunction

function [7:0] mb0e; //Multiply by 0e
input [7:0] x;
begin
mb0e = mul2(x,3) ^ mul2(x,2)^ mul2(x,1);
end
endfunction

function [7:0] mb0d; //Multiply by 0d
input [7:0] x;
begin
mb0d = mul2(x,3) ^ mul2(x,2) ^ x;
end
endfunction

function [7:0] mb0b; //Multiply by 0b
input [7:0] x;
begin
mb0b = mul2(x,3) ^ mul2(x,1) ^ x;
end
endfunction

function [7:0] mb09; //Multiply by 09
input [7:0] x;
begin
mb09 = mul2(x,3) ^ x;
end
endfunction

genvar i;
generate
for(i = 0; i < 4; i = i + 1)begin: InvMixColumnsLoop
//state[0,c] = 0e*state[0,c] + 0b*state[1,c] + 0d*state[2,c] + 09*state[3,c]
assign stateOut[32*i+24+:8] = mb0e(stateIn[32*i+24+:8]) ^ mb0b(stateIn[32*i+16+:8]) ^ mb0d(stateIn[32*i+8 +:8]) ^ mb09(stateIn[32*i +:8]);

//state[1,c] = 0e*state[1,c] + 0b* state[2,c] + 0d*state[3,c] + 09*state[0,c]
assign stateOut[32*i+16+:8] = mb0e(stateIn[32*i+16+:8]) ^ mb0b(stateIn[32*i+8 +:8]) ^ mb0d(stateIn[32*i +:8]) ^ mb09(stateIn[32*i+24+:8]);

//state[2,c] = 0e*state[2,c] + 0b* state[3,c] + 0d*state[0,c] + 09*state[1,c]
assign stateOut[32*i+8 +:8] = mb0e(stateIn[32*i+8 +:8]) ^ mb0b(stateIn[32*i +:8]) ^ mb0d(stateIn[32*i+24+:8]) ^ mb09(stateIn[32*i+16+:8]);

//state[3,c] = 0e*state[3,c] + 0b* state[0,c] + 0d*state[1,c] + 09*state[2,c]
assign stateOut[32*i +:8] = mb0e(stateIn[32*i +:8]) ^ mb0b(stateIn[32*i+24+:8]) ^ mb0d(stateIn[32*i+16+:8]) ^ mb09(stateIn[32*i+8 +:8]);
end
endgenerate


endmodule
21 changes: 21 additions & 0 deletions InvMixColumns_tb.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module InvMixColumns_tb();

reg [127:0] stateIn;
wire [127:0] stateOut;

InvMixColumns mix(stateIn,stateOut);

initial begin
stateIn = 128'hbd6e7c3df2b5779e0b61216e8b10b689;
$monitor("expected: 4773b91ff72f354361cb018ea1e6cf2c actual: %h\n",stateOut);
#2
stateIn = 128'hfde3bad205e5d0d73547964ef1fe37f1;
$monitor("expected: 2d7e86a339d9393ee6570a1101904e16 actual: %h\n",stateOut);
#2
stateIn = 128'hd1876c0f79c4300ab45594add66ff41f;
$monitor("expected: 39daee38f4f1a82aaf432410c36d45b9 actual: %h\n",stateOut);


end

endmodule
33 changes: 33 additions & 0 deletions mixColumns.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

module MixColumns (stateIn,stateOut);
input[127:0] stateIn;
output[127:0] stateOut;



//xtime function that multiply x by 2 and fixes the overflow
function [7:0] xtime;
input [7:0]in;
if(in[7] == 1) xtime = (in << 1) ^ 8'h1B;
else xtime = in << 1;
endfunction

genvar i;
generate
for(i = 0; i < 4; i = i + 1)begin: mixColumnsLoop
//state[0,c] = 2*state[0,c] + (2 * state[1,c] + state[1,c]) + state[2,c] + state[3,c]
assign stateOut[32*i+24+:8] = xtime(stateIn[32*i+24+:8]) ^ (xtime(stateIn[32*i+16+:8]) ^ stateIn[32*i+16+:8]) ^ stateIn[32*i+8 +:8] ^ stateIn[32*i +:8];

//state[1,c] = 2*state[1,c] + (2 * state[2,c] + state[2,c]) + state[3,c] + state[0,c]
assign stateOut[32*i+16+:8] = xtime(stateIn[32*i+16+:8]) ^ (xtime(stateIn[32*i+8 +:8]) ^ stateIn[32*i+8 +:8]) ^ stateIn[32*i +:8] ^ stateIn[32*i+24+:8];

//state[2,c] = 2*state[2,c] + (2 * state[3,c] + state[3,c]) + state[0,c] + state[1,c]
assign stateOut[32*i+8 +:8] = xtime(stateIn[32*i+8 +:8]) ^ (xtime(stateIn[32*i +:8]) ^ stateIn[32*i +:8]) ^ stateIn[32*i+24+:8] ^ stateIn[32*i+16+:8];

//state[3,c] = 2*state[3,c] + (2 * state[0,c] + state[0,c]) + state[1,c] + state[2,c]
assign stateOut[32*i +:8] = xtime(stateIn[32*i +:8]) ^ (xtime(stateIn[32*i+24+:8]) ^ stateIn[32*i+24+:8]) ^ stateIn[32*i+16+:8] ^ stateIn[32*i+8 +:8];
end
endgenerate


endmodule
21 changes: 21 additions & 0 deletions mixColumns_tb.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module MixColumns_tb();

reg [127:0] stateIn;
wire [127:0] stateOut;

MixColumns mix(stateIn,stateOut);

initial begin
stateIn = 128'h6353e08c0960e104cd70b751bacad0e7;
$monitor("expected: 5f72641557f5bc92f7be3b291db9f91a actual: %h\n",stateOut);
#2
stateIn = 128'h84e1dd691a41d76f792d389783fbac70;
$monitor("expected: 9f487f794f955f662afc86abd7f1ab29 actual: %h\n",stateOut);
#2
stateIn = 128'h1fb5430ef0accf64aa370cde3d77792c;
$monitor("expected: b7a53ecbbf9d75a0c40efc79b674cc11 actual: %h\n",stateOut);


end

endmodule

0 comments on commit d22c92b

Please sign in to comment.