From d22c92ba0ab8cc8001ba00de151320fabaa76983 Mon Sep 17 00:00:00 2001 From: Ahmed Amr <43810060+AhmedAmrNabil@users.noreply.github.com> Date: Sun, 21 Apr 2024 18:31:16 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20MixCols=20and=20InvMixCols=20and=20?= =?UTF-8?q?7seg=20done=20(#1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * mixCols and InvMixCols and 7seg done * Delete InvMixColumns_tb.v.bak deleted unneeded file * Update mixColumns.v * Update InvMixColumns.v --- DisplayDecoder.v | 20 +++++++++++++++ InvMixColumns.v | 63 ++++++++++++++++++++++++++++++++++++++++++++++ InvMixColumns_tb.v | 21 ++++++++++++++++ mixColumns.v | 33 ++++++++++++++++++++++++ mixColumns_tb.v | 21 ++++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 DisplayDecoder.v create mode 100644 InvMixColumns.v create mode 100644 InvMixColumns_tb.v create mode 100644 mixColumns.v create mode 100644 mixColumns_tb.v diff --git a/DisplayDecoder.v b/DisplayDecoder.v new file mode 100644 index 0000000..504cfbb --- /dev/null +++ b/DisplayDecoder.v @@ -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 diff --git a/InvMixColumns.v b/InvMixColumns.v new file mode 100644 index 0000000..6d524be --- /dev/null +++ b/InvMixColumns.v @@ -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 diff --git a/InvMixColumns_tb.v b/InvMixColumns_tb.v new file mode 100644 index 0000000..3c7d532 --- /dev/null +++ b/InvMixColumns_tb.v @@ -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 \ No newline at end of file diff --git a/mixColumns.v b/mixColumns.v new file mode 100644 index 0000000..c539c86 --- /dev/null +++ b/mixColumns.v @@ -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 diff --git a/mixColumns_tb.v b/mixColumns_tb.v new file mode 100644 index 0000000..1c122ad --- /dev/null +++ b/mixColumns_tb.v @@ -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 \ No newline at end of file