Skip to content

Commit

Permalink
qualifying and using skencode error as assertion
Browse files Browse the repository at this point in the history
msg interface fsm doesn't unconditionally move on held last msg
  • Loading branch information
Nitsirks committed Jan 3, 2025
1 parent a734774 commit 802d230
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 38 deletions.
44 changes: 20 additions & 24 deletions src/mldsa_top/rtl/mldsa_ctrl.sv
Original file line number Diff line number Diff line change
Expand Up @@ -873,26 +873,22 @@ always_comb mldsa_privkey_lock = '0;
if (!rst_b) begin
msg_data <= '0;
end else begin
if (msg_hold) begin
msg_data <= msg_data;
end else begin
unique case (sampler_src) inside
MLDSA_SEED_ID: msg_data <= msg_done ? {48'b0,sampler_imm} : {seed_reg[{sampler_src_offset[1:0],1'b1}],seed_reg[{sampler_src_offset[1:0],1'b0}]};
MLDSA_RHO_ID: msg_data <= msg_done ? {48'b0,sampler_imm} : rho_reg[sampler_src_offset[1:0]];
MLDSA_RHO_P_ID: msg_data <= msg_done ? {48'b0,sampler_imm} : rho_p_reg[sampler_src_offset[2:0]];
MLDSA_TR_ID: msg_data <= privatekey_reg.enc.tr[sampler_src_offset[2:0]];
MLDSA_MSG_ID: msg_data <= {msg_p_reg[{sampler_src_offset[3:0],1'b1}],msg_p_reg[{sampler_src_offset[3:0],1'b0}]};
MLDSA_K_ID: msg_data <= privatekey_reg.enc.K[sampler_src_offset[1:0]];
MLDSA_MU_ID: msg_data <= mu_reg[sampler_src_offset[2:0]];
MLDSA_SIGN_RND_ID: msg_data <= {sign_rnd_reg[{sampler_src_offset[1:0],1'b1}],sign_rnd_reg[{sampler_src_offset[1:0],1'b0}]};
MLDSA_RHO_P_KAPPA_ID: msg_data <= msg_done ? {48'b0,(kappa_reg + sampler_imm[2:0])} : rho_p_reg[sampler_src_offset[2:0]];
MLDSA_SIG_C_REG_ID: msg_data <= {signature_reg.enc.c[{sampler_src_offset[2:0],1'b1}], signature_reg.enc.c[{sampler_src_offset[2:0],1'b0}]};
MLDSA_PK_REG_ID: msg_data <= {publickey_reg.enc.rho[{sampler_src_offset[1:0],1'b1}],publickey_reg.enc.rho[{sampler_src_offset[1:0],1'b0}]};
MLDSA_ENTROPY_ID: msg_data <= lfsr_entropy_reg[sampler_src_offset[2:0]];
MLDSA_CNT_ID: msg_data <= counter_reg;
default: msg_data <= '0;
endcase
end
unique case (sampler_src) inside
MLDSA_SEED_ID: msg_data <= msg_done ? {48'b0,sampler_imm} : {seed_reg[{sampler_src_offset[1:0],1'b1}],seed_reg[{sampler_src_offset[1:0],1'b0}]};
MLDSA_RHO_ID: msg_data <= msg_done ? {48'b0,sampler_imm} : rho_reg[sampler_src_offset[1:0]];
MLDSA_RHO_P_ID: msg_data <= msg_done ? {48'b0,sampler_imm} : rho_p_reg[sampler_src_offset[2:0]];
MLDSA_TR_ID: msg_data <= privatekey_reg.enc.tr[sampler_src_offset[2:0]];
MLDSA_MSG_ID: msg_data <= {msg_p_reg[{sampler_src_offset[3:0],1'b1}],msg_p_reg[{sampler_src_offset[3:0],1'b0}]};
MLDSA_K_ID: msg_data <= privatekey_reg.enc.K[sampler_src_offset[1:0]];
MLDSA_MU_ID: msg_data <= mu_reg[sampler_src_offset[2:0]];
MLDSA_SIGN_RND_ID: msg_data <= {sign_rnd_reg[{sampler_src_offset[1:0],1'b1}],sign_rnd_reg[{sampler_src_offset[1:0],1'b0}]};
MLDSA_RHO_P_KAPPA_ID: msg_data <= msg_done ? {48'b0,(kappa_reg + sampler_imm[2:0])} : rho_p_reg[sampler_src_offset[2:0]];
MLDSA_SIG_C_REG_ID: msg_data <= {signature_reg.enc.c[{sampler_src_offset[2:0],1'b1}], signature_reg.enc.c[{sampler_src_offset[2:0],1'b0}]};
MLDSA_PK_REG_ID: msg_data <= {publickey_reg.enc.rho[{sampler_src_offset[1:0],1'b1}],publickey_reg.enc.rho[{sampler_src_offset[1:0],1'b0}]};
MLDSA_ENTROPY_ID: msg_data <= lfsr_entropy_reg[sampler_src_offset[2:0]];
MLDSA_CNT_ID: msg_data <= counter_reg;
default: msg_data <= '0;
endcase
end
end

Expand Down Expand Up @@ -1233,7 +1229,7 @@ always_comb msg_hold = msg_valid_o & ~msg_rdy_i;

//Done when msg count is equal to length
//length is in bytes - compare against MSB from strobe width gets us the length in msg interface chunks
always_comb msg_done = ~msg_hold & (msg_cnt >= prim_instr.length[MLDSA_OPR_WIDTH-1:$clog2(MsgStrbW)]);
always_comb msg_done = (msg_cnt >= prim_instr.length[MLDSA_OPR_WIDTH-1:$clog2(MsgStrbW)]);

always_ff @(posedge clk or negedge rst_b) begin
if (!rst_b) begin
Expand All @@ -1247,8 +1243,8 @@ always_ff @(posedge clk or negedge rst_b) begin
msg_strobe_o <= 0;
end
else begin
msg_cnt <= msg_done ? 'd0 :
msg_hold ? msg_cnt :
msg_cnt <= msg_hold ? msg_cnt :
msg_done ? 'd0 :
msg_valid ? msg_cnt + 'd1 : msg_cnt;
msg_valid_o <= msg_hold ? msg_valid_o : msg_valid;
msg_strobe_o <= msg_hold ? msg_strobe_o :
Expand Down Expand Up @@ -1299,7 +1295,7 @@ always_comb begin : primary_ctrl_fsm_out_combo
msg_valid = 1;
sampler_src = prim_instr.operand1;
sampler_imm = prim_instr.imm;
if (msg_done) begin
if (msg_done & ~msg_hold) begin
if (prim_instr.opcode.sampler_en) ctrl_fsm_ns = MLDSA_CTRL_FUNC_START;
else ctrl_fsm_ns = MLDSA_CTRL_MSG_WAIT;
end
Expand Down
1 change: 0 additions & 1 deletion src/mldsa_top/rtl/mldsa_top.sv
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,6 @@ skencode_inst

.skencode_enable(skencode_enable),
.skencode_done(skencode_done),
.skencode_error(),

.keymem_a_wr_req(skencode_keymem_if),
.keymem_a_wr_data(skencode_wr_data),
Expand Down
19 changes: 6 additions & 13 deletions src/sk_encode/rtl/skencode.sv
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ module skencode
output mem_if_t mem_b_rd_req,
output logic [AHB_DATA_WIDTH-1:0] keymem_a_wr_data,

output logic skencode_error,
output logic skencode_done
);

Expand All @@ -80,7 +79,7 @@ module skencode


logic [2:0] main_state, next_main_state, write_state, next_write_state;
logic error_flag, asserted_error_flag;
logic error_flag;
logic [7:0][2:0] encoded_coeffs;
logic [7:0] encoding_error;
logic [23:0] one_encoding_string;
Expand All @@ -92,10 +91,6 @@ module skencode

assign one_encoding_string = {encoded_coeffs[7],encoded_coeffs[6],encoded_coeffs[5],encoded_coeffs[4],
encoded_coeffs[3],encoded_coeffs[2],encoded_coeffs[1],encoded_coeffs[0]};

//FIXME enable this later?
assign error_flag = 0;//encoding_error[0] | encoding_error[1] | encoding_error[2] | encoding_error[3] |
//encoding_error[4] | encoding_error[5] | encoding_error[6] | encoding_error[7];


// State Machine: Updates main_state and write_state based on current conditions and next states.
Expand All @@ -109,7 +104,7 @@ module skencode
write_state <= IDLE;
end
else begin
if (skencode_error) begin
if (error_flag) begin
main_state <= IDLE;
write_state <= IDLE;
end
Expand Down Expand Up @@ -241,7 +236,7 @@ module skencode
// Locks destination and source addresses when skencode_enable is active.
// Manages write_buffer and producer_selector based on the main state machine.
// Issues read requests to memory and increments num_mem_operands during appropriate states.
// Signals completion (skencode_done) and checks for errors (skencode_error) based on operation progress.
// Signals completion (skencode_done) based on operation progress.
always_ff @(posedge clk or negedge reset_n) begin
if (!reset_n) begin
write_buffer <= 'h0;
Expand All @@ -250,7 +245,6 @@ module skencode
locked_dest_addr <= 'h0;
locked_src_addr <= 'h0;
skencode_done <= 'h0;
skencode_error <= 'h0;
mem_a_rd_req <= '{rd_wr_en: RW_IDLE, addr: '0};
mem_b_rd_req <= '{rd_wr_en: RW_IDLE, addr: '0};
end
Expand All @@ -261,7 +255,6 @@ module skencode
locked_dest_addr <= 'h0;
locked_src_addr <= 'h0;
skencode_done <= 'h0;
skencode_error <= 'h0;
mem_a_rd_req <= '{rd_wr_en: RW_IDLE, addr: '0};
mem_b_rd_req <= '{rd_wr_en: RW_IDLE, addr: '0};
end
Expand Down Expand Up @@ -300,9 +293,6 @@ module skencode
else begin
skencode_done <= 'h0;
end

if (skencode_error == 1'b0 && main_state != IDLE)
skencode_error <= error_flag;
end
end

Expand Down Expand Up @@ -396,5 +386,8 @@ module skencode
end
end : encoding

assign error_flag = (|encoding_error) & main_state inside {READ_ENC_and_CONSUME, ENC_and_CONSUME, CONSUME, CONSUME_LAST};

`ABR_ASSERT_NEVER(SKENCODE_ERROR_FLAG, error_flag, clk, !reset_n)

endmodule

0 comments on commit 802d230

Please sign in to comment.