Skip to content

Commit

Permalink
Merged PR 118985: fixed mont mult bug in last reduction found by FPV
Browse files Browse the repository at this point in the history
- fixed mont mult bug in last reduction found by FPV
- fixed last mult reduction coverpoint
- added montgomery_tb for corner cases and updated vf files

Related work items: #522088
  • Loading branch information
mojtaba-bisheh authored and Anjana Parthasarathy committed Aug 14, 2023
1 parent 4fdf169 commit 64a9993
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 20 deletions.
14 changes: 14 additions & 0 deletions src/ecc/config/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ targets:
- $COMPILE_ROOT/tb/ecc_top_tb.sv
tops: [ecc_top_tb]
---
provides: [ecc_montgomerymultiplier_tb]
schema_version: 2.4.0
requires:
- ecc_top
targets:
tb:
directories:
- $COMPILE_ROOT/tb
files:
- $COMPILE_ROOT/tb/ecc_montgomerymultiplier_tb.sv
tops: [ecc_montgomerymultiplier_tb]
sim:
pre_exec: 'echo "[PRE-EXEC] Copying Mont multiplication test vector to $PWD" && cp $COMPILE_ROOT/tb/test_vectors/mm_test_vectors*.hex .'
---
provides: [ecc_coverage]
schema_version: 2.4.0
requires:
Expand Down
80 changes: 80 additions & 0 deletions src/ecc/config/ecc_montgomerymultiplier_tb.vf
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
+incdir+${CALIPTRA_ROOT}/src/integration/rtl
+incdir+${CALIPTRA_ROOT}/src/libs/rtl
+incdir+${CALIPTRA_ROOT}/src/keyvault/rtl
+incdir+${CALIPTRA_ROOT}/src/pcrvault/rtl
+incdir+${CALIPTRA_ROOT}/src/ecc/tb
+incdir+${CALIPTRA_ROOT}/src/sha512/rtl
+incdir+${CALIPTRA_ROOT}/src/sha512_masked/rtl
+incdir+${CALIPTRA_ROOT}/src/hmac/rtl
+incdir+${CALIPTRA_ROOT}/src/hmac_drbg/rtl
+incdir+${CALIPTRA_ROOT}/src/ecc/rtl
${CALIPTRA_ROOT}/src/integration/rtl/config_defines.svh
${CALIPTRA_ROOT}/src/integration/rtl/caliptra_reg_defines.svh
${CALIPTRA_ROOT}/src/libs/rtl/caliptra_sva.svh
${CALIPTRA_ROOT}/src/libs/rtl/caliptra_macros.svh
${CALIPTRA_ROOT}/src/libs/rtl/caliptra_sram.sv
${CALIPTRA_ROOT}/src/libs/rtl/ahb_defines_pkg.sv
${CALIPTRA_ROOT}/src/libs/rtl/caliptra_ahb_srom.sv
${CALIPTRA_ROOT}/src/libs/rtl/apb_slv_sif.sv
${CALIPTRA_ROOT}/src/libs/rtl/ahb_slv_sif.sv
${CALIPTRA_ROOT}/src/libs/rtl/caliptra_icg.sv
${CALIPTRA_ROOT}/src/libs/rtl/clk_gate.sv
${CALIPTRA_ROOT}/src/keyvault/rtl/kv_defines_pkg.sv
${CALIPTRA_ROOT}/src/keyvault/rtl/kv_macros.svh
${CALIPTRA_ROOT}/src/pcrvault/rtl/pv_defines_pkg.sv
${CALIPTRA_ROOT}/src/pcrvault/rtl/pv_macros.svh
${CALIPTRA_ROOT}/src/pcrvault/rtl/pv_gen_hash.sv
${CALIPTRA_ROOT}/src/ecc/tb/ecc_montgomerymultiplier_tb.sv
${CALIPTRA_ROOT}/src/libs/rtl/ahb_to_reg_adapter.sv
${CALIPTRA_ROOT}/src/keyvault/rtl/kv_reg_pkg.sv
${CALIPTRA_ROOT}/src/keyvault/rtl/kv_reg.sv
${CALIPTRA_ROOT}/src/keyvault/rtl/kv.sv
${CALIPTRA_ROOT}/src/keyvault/rtl/kv_fsm.sv
${CALIPTRA_ROOT}/src/keyvault/rtl/kv_read_client.sv
${CALIPTRA_ROOT}/src/keyvault/rtl/kv_write_client.sv
${CALIPTRA_ROOT}/src/pcrvault/rtl/pv_reg_pkg.sv
${CALIPTRA_ROOT}/src/pcrvault/rtl/pv_reg.sv
${CALIPTRA_ROOT}/src/pcrvault/rtl/pv.sv
${CALIPTRA_ROOT}/src/sha512/rtl/sha512_reg_pkg.sv
${CALIPTRA_ROOT}/src/sha512/rtl/sha512_params_pkg.sv
${CALIPTRA_ROOT}/src/sha512/rtl/sha512_ctrl.sv
${CALIPTRA_ROOT}/src/sha512/rtl/sha512.sv
${CALIPTRA_ROOT}/src/sha512/rtl/sha512_core.v
${CALIPTRA_ROOT}/src/sha512/rtl/sha512_h_constants.v
${CALIPTRA_ROOT}/src/sha512/rtl/sha512_k_constants.v
${CALIPTRA_ROOT}/src/sha512/rtl/sha512_w_mem.v
${CALIPTRA_ROOT}/src/sha512/rtl/sha512_reg.sv
${CALIPTRA_ROOT}/src/sha512_masked/rtl/sha512_masked_defines_pkg.sv
${CALIPTRA_ROOT}/src/sha512_masked/rtl/sha512_masked_core.sv
${CALIPTRA_ROOT}/src/sha512_masked/rtl/sha512_masked_lfsr.sv
${CALIPTRA_ROOT}/src/hmac/rtl/hmac_param_pkg.sv
${CALIPTRA_ROOT}/src/hmac/rtl/hmac_reg_pkg.sv
${CALIPTRA_ROOT}/src/hmac/rtl/hmac_ctrl.sv
${CALIPTRA_ROOT}/src/hmac/rtl/hmac.sv
${CALIPTRA_ROOT}/src/hmac/rtl/hmac_core.v
${CALIPTRA_ROOT}/src/hmac/rtl/hmac_reg.sv
${CALIPTRA_ROOT}/src/hmac_drbg/rtl/hmac_drbg.sv
${CALIPTRA_ROOT}/src/hmac_drbg/rtl/hmac_drbg_lfsr.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_reg_pkg.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_defines_pkg.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_params_pkg.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_dsa_uop_pkg.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_pm_uop_pkg.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_top.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_reg.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_dsa_ctrl.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_dsa_sequencer.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_scalar_blinding.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_hmac_drbg_interface.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_arith_unit.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_pm_ctrl.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_pm_sequencer.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_ram_tdp_file.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_fau.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_montgomerymultiplier.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_pe_first.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_pe.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_pe_final.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_mult_dsp.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_add_sub_mod_alter.sv
${CALIPTRA_ROOT}/src/ecc/rtl/ecc_adder.sv
17 changes: 5 additions & 12 deletions src/ecc/coverage/ecc_top_cov_if.sv
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ interface ecc_top_cov_if
logic add_res_greater_than_prime;
logic add_res_greater_than_384_bit;

logic PE_UNITS;
logic mult_ready;
logic mult_last_reduction;
logic mult_final_subtraction;

assign mod_p_q = ecc_top.ecc_dsa_ctrl_i.ecc_arith_unit_i.mod_p_q;
Expand All @@ -66,17 +67,9 @@ interface ecc_top_cov_if
assign add_res_greater_than_prime = ((add_cout0 == 1'b0) & (add_res0 >= ecc_top.ecc_dsa_ctrl_i.ecc_arith_unit_i.ecc_fau_i.prime_i));
assign add_res_greater_than_384_bit = (add_cout0 == 1'b1);

assign PE_UNITS = ecc_top.ecc_dsa_ctrl_i.ecc_arith_unit_i.ecc_fau_i.i_MULTIPLIER.PE_UNITS;
always_ff @(posedge clk) begin
if (!reset_n) begin
mult_final_subtraction <= '0;
end
else if (ecc_top.ecc_dsa_ctrl_i.ecc_arith_unit_i.ecc_fau_i.i_MULTIPLIER.ready_o) begin
mult_final_subtraction <= ecc_top.ecc_dsa_ctrl_i.ecc_arith_unit_i.ecc_fau_i.i_MULTIPLIER.sub_b_o[2*(PE_UNITS+1)];
end
end


assign mult_ready = ecc_top.ecc_dsa_ctrl_i.ecc_arith_unit_i.ecc_fau_i.i_MULTIPLIER.ready_o;
assign mult_last_reduction = ecc_top.ecc_dsa_ctrl_i.ecc_arith_unit_i.ecc_fau_i.i_MULTIPLIER.last_reduction;
assign mult_final_subtraction = mult_ready & mult_last_reduction;

assign ecc_cmd = ecc_top.ecc_dsa_ctrl_i.cmd_reg;
assign pcr_sign_mode = ecc_top.ecc_dsa_ctrl_i.pcr_sign_mode;
Expand Down
15 changes: 12 additions & 3 deletions src/ecc/rtl/ecc_montgomerymultiplier.sv
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ module ecc_montgomerymultiplier #(
logic [RADIX-1 : 0] last_s_reg;
logic [FULL_REG_SIZE-1:0] p_internal;
logic [FULL_REG_SIZE-1:0] p_subtracted_internal;
logic last_reduction;

//----------------------------------------------------------------
// Processing elements (PEs)
Expand Down Expand Up @@ -291,8 +292,15 @@ module ecc_montgomerymultiplier #(
always_comb sub_b_i[t1] = 1;
else
always_comb sub_b_i[t1] = sub_b_o[t1 - 1];

always_comb sub_b_o[t1] = sub_res[t1][RADIX];

always_ff @(posedge clk or negedge reset_n) begin
if (~reset_n)
sub_b_o[t1] <= '0;
else if (zeroize)
sub_b_o[t1] <= '0;
else
sub_b_o[t1] <= sub_res[t1][RADIX];
end
end
endgenerate

Expand Down Expand Up @@ -337,7 +345,8 @@ module ecc_montgomerymultiplier #(
end
endgenerate

assign p_o = (sub_b_o[2*(PE_UNITS+1)])? p_subtracted_internal[REG_SIZE-1:0] : p_internal[REG_SIZE-1:0];
always_comb last_reduction = (sub_b_o[2*(PE_UNITS+1)]);
assign p_o = (last_reduction)? p_subtracted_internal[REG_SIZE-1:0] : p_internal[REG_SIZE-1:0];

// Determines when results are ready based on S_NUM
always_ff @(posedge clk or negedge reset_n) begin
Expand Down
2 changes: 1 addition & 1 deletion src/ecc/rtl/ecc_pm_ctrl.sv
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module ecc_pm_ctrl
//----------------------------------------------------------------
// Internal constant and parameter definitions.
//----------------------------------------------------------------
localparam [7 : 0] MULT_DELAY = 8'd38; //39 -1;
localparam [7 : 0] MULT_DELAY = 8'd39; //40 -1;
localparam [7 : 0] ADD_DELAY = 8'd1; // 2 -1;

localparam [9 : 0] Secp384_SCA_MONT_COUNT = REG_SIZE[9 : 0] + RND_SIZE[9 : 0];
Expand Down
10 changes: 6 additions & 4 deletions src/ecc/tb/ecc_montgomerymultiplier_tb.sv
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
//======================================================================

module ecc_montgomerymultiplier_tb #(
parameter OPERAND_WIDTH = 384,
parameter WORD_WIDTH = 32,
parameter TEST_VECTOR_NUM = 102
parameter OPERAND_WIDTH = 16,
parameter WORD_WIDTH = 4,
parameter TEST_VECTOR_NUM = 5
)
();

Expand Down Expand Up @@ -91,6 +91,7 @@ module ecc_montgomerymultiplier_tb #(
mm_dut (
.clk (clk_tb),
.reset_n (reset_n_tb),
.zeroize (1'b0),

.start_i (start_i_tb),
.opa_i (opa_i_tb),
Expand Down Expand Up @@ -317,7 +318,8 @@ module ecc_montgomerymultiplier_tb #(
$display(" -= Testbench for mm started =-");
$display(" ==============================\n");

fname = $sformatf("/home/mojtabab/workspace_aha_poc/ws1/Caliptra/src/ecc/tb/test_vectors/mm_test_vectors_%0d_key_%0d_word_%0d.hex", TEST_VECTOR_NUM, OPERAND_WIDTH, WORD_WIDTH);
fname = $sformatf("mm_test_vectors_%0d_key_%0d_word_%0d.hex", TEST_VECTOR_NUM, OPERAND_WIDTH, WORD_WIDTH);

read_test_vectors(fname);

init_sim();
Expand Down
30 changes: 30 additions & 0 deletions src/ecc/tb/test_vectors/mm_test_vectors_5_key_16_word_4.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
0013
0d79
fffb
0d
0000
0
0015
00ff
fff1
0f
b00c
1
0026
e509
fffb
0d
0000
2
c15f
5445
fff1
0f
0006
3
bfff
2fff
fff1
0f
002f
4

0 comments on commit 64a9993

Please sign in to comment.