Zanehpn commited on
Commit
bdaddc3
·
verified ·
1 Parent(s): 8b908b8

Add files using upload-large-folder tool

Browse files
Files changed (20) hide show
  1. snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/config/compile.yml +49 -0
  2. snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/config/norm_check.vf +9 -0
  3. snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/config/norm_check_pkg.vf +3 -0
  4. snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/config/norm_check_tb.vf +11 -0
  5. snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/rtl/norm_check.sv +53 -0
  6. snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/rtl/norm_check_ctrl.sv +159 -0
  7. snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/rtl/norm_check_defines_pkg.sv +34 -0
  8. snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/rtl/norm_check_top.sv +135 -0
  9. snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/stimulus/tests/directed/norm_check_normal_test.yml +21 -0
  10. snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/tb/norm_check_tb.sv +482 -0
  11. snapshots/chipsalliance_adams-bridge_pr66/src/rej_sampler/stimulus/tests/directed/rej_rand_test.yml +17 -0
  12. snapshots/chipsalliance_adams-bridge_pr66/src/rej_sampler/tb/rej_sampler.py +106 -0
  13. snapshots/chipsalliance_adams-bridge_pr66/src/rej_sampler/tb/rej_sampler_tb.sv +439 -0
  14. snapshots/chipsalliance_adams-bridge_pr66/src/sk_encode/config/compile.yml +37 -0
  15. snapshots/chipsalliance_adams-bridge_pr66/src/sk_encode/config/design_lint/skencode/sglint_waivers +0 -0
  16. snapshots/chipsalliance_adams-bridge_pr66/src/sk_encode/config/skencode.vf +36 -0
  17. snapshots/chipsalliance_adams-bridge_pr66/src/sk_encode/config/skencode_tb.vf +38 -0
  18. snapshots/chipsalliance_adams-bridge_pr66/src/sk_encode/rtl/skencode.sv +400 -0
  19. snapshots/chipsalliance_adams-bridge_pr66/src/sk_encode/tb/pack_eta.py +86 -0
  20. snapshots/chipsalliance_adams-bridge_pr66/src/sk_encode/tb/skencode_tb.sv +382 -0
snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/config/compile.yml ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (C) Microsoft Corporation. All rights reserved.
2
+
3
+ # Go here for details on compile.yml syntax:
4
+ # https://dev.azure.com/ms-tsd/Ether/_git/sim-tools?path=%2FSourceFileSpec.docx&version=GBmaster
5
+
6
+ ---
7
+ provides: [norm_check_pkg]
8
+ schema_version: 2.4.0
9
+ requires: []
10
+ targets:
11
+ rtl:
12
+ directories: [$COMPILE_ROOT/rtl]
13
+ files:
14
+ - $COMPILE_ROOT/rtl/norm_check_defines_pkg.sv
15
+ tb:
16
+ directories: [$COMPILE_ROOT/rtl]
17
+ files:
18
+ - $COMPILE_ROOT/rtl/norm_check_defines_pkg.sv
19
+ ---
20
+ provides: [norm_check]
21
+ schema_version: 2.4.0
22
+ requires:
23
+ - norm_check_pkg
24
+ - mldsa_defines
25
+ targets:
26
+ rtl:
27
+ directories: [$COMPILE_ROOT/rtl]
28
+ files:
29
+ - $COMPILE_ROOT/rtl/norm_check.sv
30
+ - $COMPILE_ROOT/rtl/norm_check_ctrl.sv
31
+ - $COMPILE_ROOT/rtl/norm_check_top.sv
32
+ tops: [norm_check]
33
+ rtl_lint:
34
+ directories: []
35
+ waiver_files:
36
+ - $MSFT_REPO_ROOT/src/norm_check/config/design_lint/norm_check/sglint_waivers
37
+ ---
38
+ provides: [norm_check_tb]
39
+ schema_version: 2.4.0
40
+ requires:
41
+ - norm_check
42
+ - norm_check_pkg
43
+ - mldsa_defines
44
+ targets:
45
+ rtl:
46
+ directories: [$COMPILE_ROOT/tb]
47
+ files:
48
+ - $COMPILE_ROOT/tb/norm_check_tb.sv
49
+ tops: [norm_check_tb]
snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/config/norm_check.vf ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ +incdir+${ADAMSBRIDGE_ROOT}/src/norm_check/rtl
2
+ +incdir+${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl
3
+ ${ADAMSBRIDGE_ROOT}/src/norm_check/rtl/norm_check_defines_pkg.sv
4
+ ${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl/mldsa_config_defines.svh
5
+ ${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl/mldsa_params_pkg.sv
6
+ ${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl/mldsa_reg_pkg.sv
7
+ ${ADAMSBRIDGE_ROOT}/src/norm_check/rtl/norm_check.sv
8
+ ${ADAMSBRIDGE_ROOT}/src/norm_check/rtl/norm_check_ctrl.sv
9
+ ${ADAMSBRIDGE_ROOT}/src/norm_check/rtl/norm_check_top.sv
snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/config/norm_check_pkg.vf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ +incdir+${ADAMSBRIDGE_ROOT}/src/norm_check/rtl
2
+ ${ADAMSBRIDGE_ROOT}/src/norm_check/rtl/norm_check_defines_pkg.sv
3
+ ${ADAMSBRIDGE_ROOT}/src/norm_check/rtl/norm_check_defines_pkg.sv
snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/config/norm_check_tb.vf ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ +incdir+${ADAMSBRIDGE_ROOT}/src/norm_check/rtl
2
+ +incdir+${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl
3
+ +incdir+${ADAMSBRIDGE_ROOT}/src/norm_check/tb
4
+ ${ADAMSBRIDGE_ROOT}/src/norm_check/rtl/norm_check_defines_pkg.sv
5
+ ${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl/mldsa_config_defines.svh
6
+ ${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl/mldsa_params_pkg.sv
7
+ ${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl/mldsa_reg_pkg.sv
8
+ ${ADAMSBRIDGE_ROOT}/src/norm_check/rtl/norm_check.sv
9
+ ${ADAMSBRIDGE_ROOT}/src/norm_check/rtl/norm_check_ctrl.sv
10
+ ${ADAMSBRIDGE_ROOT}/src/norm_check/rtl/norm_check_top.sv
11
+ ${ADAMSBRIDGE_ROOT}/src/norm_check/tb/norm_check_tb.sv
snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/rtl/norm_check.sv ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //======================================================================
15
+ //
16
+ // norm_check.sv
17
+ // --------
18
+ // Performs: invalid = (coeff > bound) && (coeff < q-bound)
19
+
20
+ module norm_check
21
+ import norm_check_defines_pkg::*;
22
+ import mldsa_params_pkg::*;
23
+ #(
24
+ parameter MLDSA_Q = 8380417,
25
+ parameter GAMMA1 = 2**19,
26
+ // parameter MLDSA_GAMMA2 = (MLDSA_Q-1)/32,
27
+ parameter BETA = 120,
28
+ parameter GAMMA1_MINUS_BETA = GAMMA1 - BETA,
29
+ parameter GAMMA2_MINUS_BETA = MLDSA_GAMMA2 - BETA
30
+ )
31
+ (
32
+ input wire enable,
33
+ input chk_norm_mode_t mode,
34
+ input wire [REG_SIZE-2:0] opa_i,
35
+ output logic invalid
36
+ );
37
+
38
+ logic [REG_SIZE-2:0] bound;
39
+ logic [REG_SIZE-2:0] q_minus_bound;
40
+
41
+ always_comb begin
42
+ case(mode)
43
+ z_bound: bound = GAMMA1_MINUS_BETA;
44
+ r0_bound: bound = GAMMA2_MINUS_BETA;
45
+ ct0_bound: bound = MLDSA_GAMMA2;
46
+ default: bound = 'h0;
47
+ endcase
48
+
49
+ q_minus_bound = MLDSA_Q - bound;
50
+ end
51
+
52
+ always_comb invalid = enable & (opa_i >= bound) & (opa_i <= q_minus_bound);
53
+ endmodule
snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/rtl/norm_check_ctrl.sv ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //======================================================================
15
+ //
16
+ // norm_check_ctrl.sv
17
+ // --------
18
+ // Manages memory accesses for check norm module. Two reads are done
19
+ // to perform 8 coeff checks per cycle. First port reads 0, 2, 4, etc, while second
20
+ // port reads 1, 3, 5, etc
21
+ // Assumes that one enable triggers check for all polynomials of chosen vector and
22
+ // assumes all polynomials of the vector are stored in continuous addr space in memory
23
+
24
+ module norm_check_ctrl
25
+ import mldsa_params_pkg::*;
26
+ import norm_check_defines_pkg::*;
27
+ #(
28
+ parameter MLDSA_N = 256,
29
+ parameter MLDSA_L = 7,
30
+ parameter MLDSA_K = 8
31
+ )
32
+ (
33
+ input wire clk,
34
+ input wire reset_n,
35
+ input wire zeroize,
36
+
37
+ input wire norm_check_enable,
38
+ input chk_norm_mode_t mode,
39
+
40
+ input wire [5:0] randomness,
41
+
42
+ input wire [MLDSA_MEM_ADDR_WIDTH-1:0] mem_base_addr,
43
+ output mem_if_t mem_rd_req,
44
+ output logic check_enable,
45
+ output logic norm_check_done
46
+ );
47
+
48
+
49
+ chk_read_state_e read_fsm_state_ps, read_fsm_state_ns;
50
+ logic [MLDSA_MEM_ADDR_WIDTH-1:0] mem_rd_addr, locked_based_addr;
51
+
52
+ //Flags
53
+ logic incr_rd_addr;
54
+ logic last_poly_last_addr;
55
+ logic norm_check_busy;
56
+
57
+ logic [4:0] latched_out_randomness;
58
+ logic latched_in_randomness;
59
+ logic [4:0] increment_addr;
60
+ logic [6:0] neutral_cnt;
61
+
62
+ always_ff @(posedge clk or negedge reset_n) begin
63
+ if (!reset_n) begin
64
+ latched_out_randomness <= 'h0;
65
+ latched_in_randomness <= 'h0;
66
+ increment_addr <= 'h0;
67
+ mem_rd_addr <= 'h0;
68
+ neutral_cnt <= 'h0;
69
+ locked_based_addr <= 'h0;
70
+ end
71
+ else if (zeroize) begin
72
+ latched_out_randomness <= 'h0;
73
+ latched_in_randomness <= 'h0;
74
+ increment_addr <= 'h0;
75
+ mem_rd_addr <= 'h0;
76
+ neutral_cnt <= 'h0;
77
+ locked_based_addr <= 'h0;
78
+ end
79
+ else begin
80
+ if (norm_check_enable) begin
81
+ latched_out_randomness <= randomness[5:1];
82
+ latched_in_randomness <= randomness[0];
83
+ increment_addr <= randomness[5:1];
84
+ mem_rd_addr <= {{(MLDSA_MEM_ADDR_WIDTH-6){1'b0}}, randomness};
85
+ neutral_cnt <= 'h0;
86
+ locked_based_addr <= mem_base_addr;
87
+ end
88
+ else if (incr_rd_addr) begin
89
+ latched_in_randomness <= latched_in_randomness;
90
+ latched_out_randomness <= latched_out_randomness;
91
+ increment_addr <= increment_addr;
92
+ mem_rd_addr <= {mem_rd_addr[MLDSA_MEM_ADDR_WIDTH-1:6], increment_addr, latched_in_randomness};
93
+ neutral_cnt <= neutral_cnt + 'h1;
94
+ end
95
+ else if (~incr_rd_addr) begin
96
+ latched_in_randomness <= randomness[0];
97
+ latched_out_randomness <= latched_out_randomness;
98
+ increment_addr <= increment_addr + 'h1;
99
+ mem_rd_addr <= {mem_rd_addr[MLDSA_MEM_ADDR_WIDTH-1:1], ~latched_in_randomness};
100
+ neutral_cnt <= neutral_cnt + 'h1;
101
+ end
102
+ end
103
+ end
104
+
105
+ //Addr assignment
106
+ always_comb begin
107
+ mem_rd_req.addr = mem_rd_addr+locked_based_addr;
108
+
109
+ mem_rd_req.rd_wr_en = ((read_fsm_state_ps == CHK_RD_MEM) | (read_fsm_state_ps == CHK_WAIT)) ? RW_READ : RW_IDLE;
110
+ end
111
+
112
+ //Last addr flag
113
+ always_comb last_poly_last_addr = (neutral_cnt == ((MLDSA_N/4))-1);
114
+
115
+ //Ctrl flags
116
+ always_comb begin
117
+ norm_check_busy = (read_fsm_state_ps != CHK_IDLE);
118
+ end
119
+
120
+ //FSM
121
+ always_ff @(posedge clk or negedge reset_n) begin
122
+ if (!reset_n)
123
+ read_fsm_state_ps <= CHK_IDLE;
124
+ else if (zeroize)
125
+ read_fsm_state_ps <= CHK_IDLE;
126
+ else
127
+ read_fsm_state_ps <= read_fsm_state_ns;
128
+ end
129
+
130
+ always_comb begin
131
+ read_fsm_state_ns = read_fsm_state_ps;
132
+ incr_rd_addr = 'b0;
133
+ norm_check_done = 0;
134
+
135
+ case(read_fsm_state_ps)
136
+ CHK_IDLE: begin
137
+ read_fsm_state_ns = norm_check_enable ? CHK_RD_MEM : CHK_IDLE;
138
+ end
139
+ CHK_RD_MEM: begin
140
+ read_fsm_state_ns = last_poly_last_addr ? CHK_DONE : CHK_WAIT;
141
+ incr_rd_addr = 'b0;
142
+ end
143
+ CHK_WAIT: begin
144
+ read_fsm_state_ns = last_poly_last_addr ? CHK_DONE : CHK_RD_MEM;
145
+ incr_rd_addr = 'b1;
146
+ end
147
+ CHK_DONE: begin
148
+ read_fsm_state_ns = CHK_IDLE;
149
+ norm_check_done = 'b1;
150
+ end
151
+ default begin
152
+ read_fsm_state_ns = CHK_IDLE;
153
+ end
154
+ endcase
155
+ end
156
+
157
+ always_comb check_enable = (read_fsm_state_ps == CHK_RD_MEM) | (read_fsm_state_ps == CHK_WAIT);
158
+
159
+ endmodule
snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/rtl/norm_check_defines_pkg.sv ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //======================================================================
15
+ //
16
+ // norm_check_defines_pkg.sv
17
+ // --------
18
+ // Validity norm check parameters for the digital signature algorithm (DSA).
19
+ //
20
+ //
21
+ //======================================================================
22
+
23
+ `ifndef ABR_NORM_CHECK_DEFINES
24
+ `define ABR_NORM_CHECK_DEFINES
25
+
26
+ package norm_check_defines_pkg;
27
+
28
+ parameter z_bound = 2'h0, r0_bound = 2'h1, ct0_bound = 2'h2;
29
+ typedef logic [1:0] chk_norm_mode_t;
30
+
31
+ typedef enum logic [1:0] {CHK_IDLE, CHK_RD_MEM, CHK_WAIT, CHK_DONE} chk_read_state_e;
32
+ endpackage
33
+
34
+ `endif
snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/rtl/norm_check_top.sv ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //======================================================================
15
+ //
16
+ // norm_check_top.sv
17
+ // --------
18
+ // Performs the 3 validity checks (z, r0, ct0) needed for signing algorithm based on mode selection
19
+ // Generates one final invalid bit that is accumulated across all polynomials of the chosen vector
20
+ // Since this module only has to read from memory, two addresses are read per cycle to increase bandwidth
21
+ // and reduce latency, i.e. 8 coeffs are processed per cycle, and 8 check units are instantiated
22
+
23
+ // Once the norm_check_done is asserted, the invalid flag needs to be latched at the top level
24
+ // for future use in signing algo. Assuming it takes 1 cycle to latch the value, ready is asserted 1 cycle later
25
+ // to let HLC know that norm_check operation is done for that vector and next one can be queued
26
+ // TODO: see if this needs any changes
27
+
28
+ // TODO: embed z and r0 checks in decompose?
29
+ // TODO: need a restart input (other than zeroize)
30
+
31
+ module norm_check_top
32
+ import mldsa_params_pkg::*;
33
+ import norm_check_defines_pkg::*;
34
+ #(
35
+ parameter MLDSA_N = 256
36
+ )
37
+ (
38
+ input wire clk,
39
+ input wire reset_n,
40
+ input wire zeroize,
41
+
42
+ input wire norm_check_enable,
43
+ input chk_norm_mode_t mode,
44
+
45
+ input wire shuffling_enable,
46
+ input wire [5:0] randomness,
47
+
48
+ input wire [MLDSA_MEM_ADDR_WIDTH-1:0] mem_base_addr,
49
+ output mem_if_t mem_rd_req,
50
+ input [4*REG_SIZE-1:0] mem_rd_data,
51
+ output logic invalid,
52
+ output logic norm_check_ready,
53
+ output logic norm_check_done
54
+ );
55
+
56
+ logic [3:0] check_a_invalid;
57
+ logic check_enable, check_enable_reg;
58
+ logic norm_check_done_int;
59
+ logic latched_shuffling_enable;
60
+ logic randomness_enable;
61
+ logic [5:0] controller_randomness;
62
+
63
+
64
+ assign randomness_enable = latched_shuffling_enable | shuffling_enable;
65
+ assign controller_randomness = randomness_enable ? randomness: '0;
66
+
67
+ generate
68
+ for (genvar i = 0; i < 4; i++) begin
69
+ norm_check check_inst (
70
+ .enable(check_enable_reg),
71
+ .mode(mode),
72
+ .opa_i(mem_rd_data[(REG_SIZE-2)+(i*REG_SIZE):i*REG_SIZE]),
73
+ .invalid(check_a_invalid[i])
74
+ );
75
+ end
76
+ endgenerate
77
+
78
+ always_ff @(posedge clk or negedge reset_n) begin
79
+ if (!reset_n)
80
+ norm_check_done <= 'b0;
81
+ else if (zeroize)
82
+ norm_check_done <= 'b0;
83
+ else
84
+ norm_check_done <= norm_check_done_int;
85
+ end
86
+
87
+ always_ff @(posedge clk or negedge reset_n) begin
88
+ if (!reset_n)
89
+ invalid <= 'b0;
90
+ else if (zeroize | norm_check_done)
91
+ invalid <= 'b0;
92
+ else
93
+ invalid <= invalid | (|check_a_invalid);// | (|check_b_invalid);
94
+ end
95
+
96
+ //Delay flops
97
+ //Give one cycle for HLC to capture invalid flag
98
+ always_ff @(posedge clk or negedge reset_n) begin
99
+ if (!reset_n) begin
100
+ norm_check_ready <= 'b0;
101
+ check_enable_reg <= 'b0;
102
+ latched_shuffling_enable <= 'b0;
103
+ end
104
+ else if (zeroize) begin
105
+ norm_check_ready <= 'b0;
106
+ check_enable_reg <= 'b0;
107
+ latched_shuffling_enable <= 'b0;
108
+ end
109
+ else begin
110
+ norm_check_ready <= norm_check_done;
111
+ check_enable_reg <= check_enable;
112
+ if (norm_check_enable)
113
+ latched_shuffling_enable <= shuffling_enable;
114
+ else if (norm_check_done)
115
+ latched_shuffling_enable <= 'b0;
116
+ end
117
+
118
+ end
119
+
120
+ norm_check_ctrl
121
+ norm_check_ctrl_inst (
122
+ .clk(clk),
123
+ .reset_n(reset_n),
124
+ .zeroize(zeroize),
125
+ .norm_check_enable(norm_check_enable),
126
+ .randomness(controller_randomness),
127
+ .mode(mode),
128
+ .mem_base_addr(mem_base_addr),
129
+ .mem_rd_req(mem_rd_req),
130
+ .norm_check_done(norm_check_done_int),
131
+ .check_enable(check_enable)
132
+ );
133
+
134
+
135
+ endmodule
snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/stimulus/tests/directed/norm_check_normal_test.yml ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ ---
16
+ # plusargs:
17
+ # - +ECC_TEST_VECTOR_FILE='${CALIPTRA_ROOT}/src/ecc/tb/test_vectors/ecc_drbg_mbedtls.hex'
18
+ # - +ECC_TEST='ECC_normal_test'
19
+
20
+ testname: norm_check_normal_test
21
+ seed: 1
snapshots/chipsalliance_adams-bridge_pr66/src/norm_check/tb/norm_check_tb.sv ADDED
@@ -0,0 +1,482 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ //======================================================================
16
+ //
17
+ // norm_check_tb.sv
18
+ // --------
19
+ //
20
+ //
21
+ //
22
+ //======================================================================
23
+
24
+ `default_nettype none
25
+
26
+ module norm_check_tb
27
+ import mldsa_params_pkg::*;
28
+ import norm_check_defines_pkg::*;
29
+ #(
30
+ parameter NUM_WR = 4,
31
+ parameter NUM_RD = 4,
32
+ parameter BUFFER_DATA_W = 8
33
+ )
34
+ ();
35
+
36
+ parameter CLK_HALF_PERIOD = 5;
37
+ parameter CLK_PERIOD = 2 * CLK_HALF_PERIOD;
38
+
39
+ parameter MLDSA_Q = 8380417;
40
+ parameter MLDSA_ETA = 2;
41
+ parameter MLDSA_TAU = 60;
42
+ parameter MLDSA_GAMMA1 = 2**19;
43
+ parameter MLDSA_GAMMA2 = (MLDSA_Q -1)/32;
44
+ parameter MLDSA_BETA = MLDSA_TAU * MLDSA_ETA;
45
+
46
+ parameter Z_BOUND = MLDSA_GAMMA1 - MLDSA_BETA;
47
+ parameter R0_BOUND = MLDSA_GAMMA2 - MLDSA_BETA;
48
+ parameter CT0_BOUND = MLDSA_GAMMA2;
49
+
50
+ //----------------------------------------------------------------
51
+ // Register and Wire declarations.
52
+ //----------------------------------------------------------------
53
+ reg clk_tb;
54
+ reg reset_n_tb;
55
+ reg zeroize_tb;
56
+ reg en_tb;
57
+ reg shuffling_enable_tb;
58
+ reg [5:0] randomness_tb;
59
+ reg [MLDSA_MEM_ADDR_WIDTH-1:0] src_base_tb;
60
+ wire invalid_tb;
61
+ wire norm_check_done_tb;
62
+ chk_norm_mode_t mode_tb;
63
+
64
+ parameter NUM_OF_COEFF = 256 * 7; // Adjust as per your design
65
+ parameter NUM_OF_MEM_WORDS = NUM_OF_COEFF / 4;
66
+ reg [3:0][REG_SIZE-1:0] input_mem [0:NUM_OF_MEM_WORDS-1];
67
+
68
+ mem_if_t mem_rd_req_tb;
69
+ reg [3:0][REG_SIZE-1:0] mem_rd_data_tb;
70
+
71
+ int mem_word_index;
72
+
73
+ generate
74
+ genvar k;
75
+ for (k = 0; k < 4; k = k +1) begin : mem_blocks
76
+ normcheck_dual_port_memory #(
77
+ .ADDR_WIDTH(MLDSA_MEM_ADDR_WIDTH),
78
+ .DATA_WIDTH(REG_SIZE),
79
+ .DEPTH(NUM_OF_MEM_WORDS)
80
+ )
81
+ input_memory (
82
+ .clk(clk_tb),
83
+ .addr_a(mem_rd_req_tb.addr),
84
+ .data_in_a(24'h0),
85
+ .we_a(1'b0),
86
+ .data_out_a(mem_rd_data_tb[k]),
87
+ .addr_b('0), // Unused
88
+ .data_in_b(24'h0),
89
+ .we_b(1'b0),
90
+ .data_out_b()
91
+ );
92
+ end
93
+ endgenerate
94
+
95
+ norm_check_top dut(
96
+ .clk(clk_tb),
97
+ .reset_n(reset_n_tb),
98
+ .zeroize(zeroize_tb),
99
+ .norm_check_enable(en_tb),
100
+ .shuffling_enable(shuffling_enable_tb),
101
+ .randomness(randomness_tb),
102
+ .mode(mode_tb),
103
+ .mem_base_addr(src_base_tb),
104
+ .mem_rd_req(mem_rd_req_tb),
105
+ .mem_rd_data(mem_rd_data_tb),
106
+ .invalid(invalid_tb),
107
+ .norm_check_ready(),
108
+ .norm_check_done(norm_check_done_tb)
109
+ );
110
+
111
+ //----------------------------------------------------------------
112
+ // clk_gen
113
+ //----------------------------------------------------------------
114
+ always
115
+ begin : clk_gen
116
+ #CLK_HALF_PERIOD;
117
+ clk_tb = !clk_tb;
118
+ end // clk_gen
119
+
120
+ task init_sim;
121
+ begin
122
+ $display("Start of init\n");
123
+ clk_tb = 0;
124
+ reset_n_tb = 0;
125
+ zeroize_tb = 0;
126
+ en_tb = 0;
127
+ src_base_tb = 'h0;
128
+ shuffling_enable_tb = '0;
129
+ end
130
+ endtask
131
+
132
+ //----------------------------------------------------------------
133
+ // reset_dut()
134
+ //----------------------------------------------------------------
135
+ task reset_dut;
136
+ begin
137
+ $display("*** Toggle reset.");
138
+ reset_n_tb = 0;
139
+
140
+ #(2 * CLK_PERIOD);
141
+ reset_n_tb = 1;
142
+ #(2 * CLK_PERIOD);
143
+
144
+ $display("End of reset");
145
+ end
146
+ endtask // reset_dut
147
+
148
+ task overwrite_memory_content(input reg [MLDSA_MEM_ADDR_WIDTH-1:0] src_base_addr);
149
+ int i;
150
+ for (i = 0; i < 64; i = i + 1) begin
151
+ mem_blocks[0].input_memory.mem[i+src_base_addr] = input_mem[i][0];
152
+ mem_blocks[1].input_memory.mem[i+src_base_addr] = input_mem[i][1];
153
+ mem_blocks[2].input_memory.mem[i+src_base_addr] = input_mem[i][2];
154
+ mem_blocks[3].input_memory.mem[i+src_base_addr] = input_mem[i][3];
155
+ end
156
+ endtask
157
+
158
+ task z_bound_norm_check_test;
159
+ // Z_BOUND Test
160
+ $display("Z bound mode\n");
161
+ mode_tb = z_bound;
162
+ src_base_tb = 'h0;
163
+
164
+ mem_word_index = 0;
165
+ for (int i = 0; i < 64; i++) begin
166
+ input_mem[mem_word_index][0] = REG_SIZE'($urandom_range(0,Z_BOUND-1));
167
+ input_mem[mem_word_index][1] = REG_SIZE'($urandom_range(MLDSA_Q-Z_BOUND, MLDSA_Q-1));
168
+ input_mem[mem_word_index][2] = REG_SIZE'($urandom_range(0,Z_BOUND));
169
+ input_mem[mem_word_index][3] = REG_SIZE'($urandom_range(0,Z_BOUND));
170
+ mem_word_index = mem_word_index + 1;
171
+ end
172
+ overwrite_memory_content(src_base_tb);
173
+ @(negedge clk_tb);
174
+ en_tb = 1'b1;
175
+ @(posedge clk_tb);
176
+ @(negedge clk_tb);
177
+ en_tb = 1'b0;
178
+
179
+ repeat (2) @(posedge clk_tb);
180
+
181
+ wait (norm_check_done_tb);
182
+ if (invalid_tb) $error("Expected z check to be valid, received invalid\n");
183
+ else $display("Norm check for z is successful\n");
184
+ endtask
185
+
186
+ task r0_bound_norm_check_test;
187
+ //R0_BOUND Test
188
+ $display("r0 bound mode\n");
189
+ mode_tb = r0_bound;
190
+ src_base_tb = 'h40;
191
+
192
+ mem_word_index = 0;
193
+ for (int i = 0; i < 64; i++) begin
194
+ input_mem[mem_word_index][0] = REG_SIZE'($urandom_range(MLDSA_Q-R0_BOUND, MLDSA_Q-1));
195
+ input_mem[mem_word_index][1] = REG_SIZE'($urandom_range(MLDSA_Q-R0_BOUND, MLDSA_Q-1));
196
+ input_mem[mem_word_index][2] = REG_SIZE'($urandom_range(MLDSA_Q-R0_BOUND, MLDSA_Q-1));
197
+ input_mem[mem_word_index][3] = REG_SIZE'($urandom_range(MLDSA_Q-R0_BOUND, MLDSA_Q-1));
198
+ mem_word_index = mem_word_index + 1;
199
+ end
200
+ overwrite_memory_content(src_base_tb);
201
+ @(negedge clk_tb);
202
+ en_tb = 1'b1;
203
+ @(posedge clk_tb);
204
+ @(negedge clk_tb);
205
+ en_tb = 1'b0;
206
+ repeat (2) @(posedge clk_tb);
207
+
208
+ wait (norm_check_done_tb);
209
+ if (invalid_tb) $error("Expected r0 check to be valid, received invalid\n");
210
+ else $display("Norm check for r0 is successful\n");
211
+
212
+ endtask
213
+
214
+ task ct0_bound_norm_check_test;
215
+ // CT0_BOUND Test
216
+ $display("ct0 bound mode\n");
217
+ mode_tb = ct0_bound;
218
+ src_base_tb = 'h80;
219
+
220
+ mem_word_index = '0;
221
+ for (int i = 0; i < 64; i++) begin
222
+ input_mem[mem_word_index][0] = REG_SIZE'($urandom_range(0,CT0_BOUND-1));
223
+ input_mem[mem_word_index][1] = REG_SIZE'($urandom_range(0,CT0_BOUND-1));
224
+ input_mem[mem_word_index][2] = REG_SIZE'($urandom_range(0,CT0_BOUND-1));
225
+ input_mem[mem_word_index][3] = REG_SIZE'($urandom_range(0,CT0_BOUND-1));
226
+ mem_word_index = mem_word_index + 1;
227
+ end
228
+ overwrite_memory_content(src_base_tb);
229
+ @(negedge clk_tb);
230
+ en_tb = 1'b1;
231
+ @(posedge clk_tb);
232
+ @(negedge clk_tb);
233
+ en_tb = 1'b0;
234
+ repeat (2) @(posedge clk_tb);
235
+
236
+ wait (norm_check_done_tb);
237
+ if (invalid_tb) $error("Expected r0 check to be valid, received invalid\n");
238
+ else $display("Norm check for r0 is successful\n");
239
+
240
+ endtask
241
+
242
+ task rejected_ct0_bound_norm_check_test;
243
+ // Rejected Test
244
+ $display("Rejected test for ct0 bound mode\n");
245
+ mode_tb = ct0_bound;
246
+ src_base_tb = 'hC0;
247
+
248
+ mem_word_index = 0;
249
+ for (int poly = 0; poly < 8; poly++) begin
250
+ $display("Starting poly %0d", poly);
251
+ for (int i = 0; i < 32; i++) begin
252
+ input_mem[mem_word_index][0] = REG_SIZE'($urandom_range(0,CT0_BOUND-1));
253
+ input_mem[mem_word_index][1] = REG_SIZE'($urandom_range(0,CT0_BOUND-1));
254
+ input_mem[mem_word_index][2] = REG_SIZE'($urandom_range(0,CT0_BOUND-1));
255
+ input_mem[mem_word_index][3] = REG_SIZE'($urandom_range(CT0_BOUND,MLDSA_Q-1));
256
+ mem_word_index = mem_word_index + 1;
257
+ end
258
+ end
259
+ overwrite_memory_content(src_base_tb);
260
+ @(negedge clk_tb);
261
+ en_tb = 1'b1;
262
+ @(posedge clk_tb);
263
+ @(negedge clk_tb);
264
+ en_tb = 1'b0;
265
+ repeat (2) @(posedge clk_tb);
266
+
267
+ @(posedge clk_tb);
268
+ wait (norm_check_done_tb);
269
+ if (invalid_tb) $display("Norm check for ct0 is rejected as expected\n");
270
+ else $error("Expected ct0 check to be invalid, received valid\n");
271
+
272
+ endtask
273
+
274
+ task rejected_z_bound_norm_check_test;
275
+ // Z_BOUND Test
276
+ $display("Z bound mode\n");
277
+ mode_tb = z_bound;
278
+ src_base_tb = 'h0;
279
+
280
+ mem_word_index = 0;
281
+ for (int i = 0; i < 64; i++) begin
282
+ input_mem[mem_word_index][0] = REG_SIZE'($urandom_range(0,Z_BOUND-1));
283
+ input_mem[mem_word_index][1] = REG_SIZE'($urandom_range(MLDSA_Q-Z_BOUND, MLDSA_Q-1));
284
+ input_mem[mem_word_index][2] = REG_SIZE'($urandom_range(0,Z_BOUND));
285
+ if (i==63) begin
286
+ input_mem[mem_word_index][3] = REG_SIZE'($urandom_range(Z_BOUND,MLDSA_Q-Z_BOUND));
287
+ end
288
+ else begin
289
+ input_mem[mem_word_index][3] = REG_SIZE'($urandom_range(0,Z_BOUND));
290
+ end
291
+ mem_word_index = mem_word_index + 1;
292
+ end
293
+ overwrite_memory_content(src_base_tb);
294
+ @(negedge clk_tb);
295
+ en_tb = 1'b1;
296
+ @(posedge clk_tb);
297
+ @(negedge clk_tb);
298
+ en_tb = 1'b0;
299
+
300
+ repeat (2) @(posedge clk_tb);
301
+
302
+ wait (norm_check_done_tb);
303
+ if (invalid_tb) $display("Norm check for z is rejected as expected\n");
304
+ else $error("Expected z check to be invalid, received valid\n");
305
+ endtask
306
+
307
+ task rejected_r0_bound_norm_check_test;
308
+ //R0_BOUND Test
309
+ $display("r0 bound mode\n");
310
+ mode_tb = r0_bound;
311
+ src_base_tb = 'h40;
312
+
313
+ mem_word_index = 0;
314
+ for (int i = 0; i < 64; i++) begin
315
+ if (i==0)
316
+ input_mem[mem_word_index][0] = REG_SIZE'($urandom_range(0, MLDSA_Q-R0_BOUND-1));
317
+ else
318
+ input_mem[mem_word_index][0] = REG_SIZE'($urandom_range(MLDSA_Q-R0_BOUND, MLDSA_Q-1));
319
+ input_mem[mem_word_index][1] = REG_SIZE'($urandom_range(MLDSA_Q-R0_BOUND, MLDSA_Q-1));
320
+ input_mem[mem_word_index][2] = REG_SIZE'($urandom_range(MLDSA_Q-R0_BOUND, MLDSA_Q-1));
321
+ input_mem[mem_word_index][3] = REG_SIZE'($urandom_range(MLDSA_Q-R0_BOUND, MLDSA_Q-1));
322
+ mem_word_index = mem_word_index + 1;
323
+ end
324
+ overwrite_memory_content(src_base_tb);
325
+ @(negedge clk_tb);
326
+ en_tb = 1'b1;
327
+ @(posedge clk_tb);
328
+ @(negedge clk_tb);
329
+ en_tb = 1'b0;
330
+ repeat (2) @(posedge clk_tb);
331
+
332
+ wait (norm_check_done_tb);
333
+ if (invalid_tb) $display("Norm check for r0 is rejected as expected\n");
334
+ else $error("Expected r0 check to be invalid, received valid\n");
335
+
336
+ endtask
337
+
338
+ task randomized_rejected_r0_bound_norm_check_test;
339
+ //R0_BOUND Test
340
+ int inner_index;
341
+ mode_tb = r0_bound;
342
+ src_base_tb = $urandom_range(0, 127);
343
+ repeat (3) @(posedge clk_tb);
344
+
345
+ mem_word_index = 0;
346
+ for (int i = 0; i < 64; i++) begin
347
+ input_mem[mem_word_index][0] = REG_SIZE'($urandom_range(MLDSA_Q-R0_BOUND, MLDSA_Q-1));
348
+ input_mem[mem_word_index][1] = REG_SIZE'($urandom_range(MLDSA_Q-R0_BOUND, MLDSA_Q-1));
349
+ input_mem[mem_word_index][2] = REG_SIZE'($urandom_range(MLDSA_Q-R0_BOUND, MLDSA_Q-1));
350
+ input_mem[mem_word_index][3] = REG_SIZE'($urandom_range(MLDSA_Q-R0_BOUND, MLDSA_Q-1));
351
+ mem_word_index = mem_word_index + 1;
352
+ end
353
+ mem_word_index = $urandom_range(0, 63);
354
+ inner_index = $urandom_range(0, 3);
355
+ input_mem[mem_word_index][inner_index] = REG_SIZE'($urandom_range(R0_BOUND, MLDSA_Q-R0_BOUND-1));
356
+
357
+ overwrite_memory_content(src_base_tb);
358
+ @(negedge clk_tb);
359
+ en_tb = 1'b1;
360
+ @(posedge clk_tb);
361
+ @(negedge clk_tb);
362
+ en_tb = 1'b0;
363
+ repeat (2) @(posedge clk_tb);
364
+
365
+ wait (norm_check_done_tb);
366
+ if (!invalid_tb) begin
367
+ $display("The incorrect range (%x) is inserted in %d and %d\n",input_mem[mem_word_index][inner_index], mem_word_index, inner_index );
368
+ $error("Expected r0 check to be invalid, received valid at %t\n", $time);
369
+ end
370
+
371
+ endtask
372
+
373
+ task randomized_ct0_bound_norm_check_test;
374
+ // CT0_BOUND Test
375
+ mode_tb = ct0_bound;
376
+ src_base_tb = $urandom_range(0, 127);
377
+ repeat (3) @(posedge clk_tb);
378
+
379
+ mem_word_index = '0;
380
+ for (int i = 0; i < 64; i++) begin
381
+ input_mem[mem_word_index][0] = REG_SIZE'($urandom_range(0,CT0_BOUND-1));
382
+ input_mem[mem_word_index][1] = REG_SIZE'($urandom_range(0,CT0_BOUND-1));
383
+ input_mem[mem_word_index][2] = REG_SIZE'($urandom_range(0,CT0_BOUND-1));
384
+ input_mem[mem_word_index][3] = REG_SIZE'($urandom_range(0,CT0_BOUND-1));
385
+ mem_word_index = mem_word_index + 1;
386
+ end
387
+ overwrite_memory_content(src_base_tb);
388
+ @(negedge clk_tb);
389
+ en_tb = 1'b1;
390
+ @(posedge clk_tb);
391
+ @(negedge clk_tb);
392
+ en_tb = 1'b0;
393
+ repeat (2) @(posedge clk_tb);
394
+
395
+ wait (norm_check_done_tb);
396
+ if (invalid_tb) $error("Expected r0 check to be valid, received invalid\n");
397
+
398
+ endtask
399
+
400
+ task norm_check_test;
401
+ $display("Starting check norm ctrl test\n");
402
+ z_bound_norm_check_test();
403
+ repeat (2) @(posedge clk_tb);
404
+ r0_bound_norm_check_test();
405
+ repeat (2) @(posedge clk_tb);
406
+ ct0_bound_norm_check_test();
407
+ repeat (2) @(posedge clk_tb);
408
+ rejected_ct0_bound_norm_check_test();
409
+ repeat (2) @(posedge clk_tb);
410
+ rejected_z_bound_norm_check_test();
411
+ repeat (2) @(posedge clk_tb);
412
+ rejected_r0_bound_norm_check_test();
413
+ repeat (10000) randomized_rejected_r0_bound_norm_check_test();
414
+ repeat (10000) randomized_ct0_bound_norm_check_test();
415
+ $display("Ctrl test done\n");
416
+ endtask
417
+
418
+ initial begin
419
+ fork
420
+ begin
421
+ init_sim();
422
+ reset_dut();
423
+ shuffling_enable_tb = '0;
424
+ norm_check_test();
425
+ shuffling_enable_tb = '1;
426
+ norm_check_test();
427
+ end
428
+ begin
429
+ while(1) begin
430
+ @(posedge clk_tb);
431
+ randomness_tb = $urandom_range(0,64);
432
+ end
433
+ end
434
+ join_any
435
+ $finish;
436
+ end
437
+
438
+ endmodule
439
+
440
+ // Memory module definition (normcheck_dual_port_memory)
441
+ module normcheck_dual_port_memory
442
+ #(
443
+ parameter ADDR_WIDTH = 15,
444
+ parameter DATA_WIDTH = 32,
445
+ parameter DEPTH = 256
446
+ )
447
+ (
448
+ input wire clk,
449
+
450
+ // Port A
451
+ input wire [ADDR_WIDTH-1:0] addr_a,
452
+ input wire [DATA_WIDTH-1:0] data_in_a,
453
+ input wire we_a, // Write enable for port A
454
+ output reg [DATA_WIDTH-1:0] data_out_a,
455
+
456
+ // Port B (Unused in this testbench)
457
+ input wire [ADDR_WIDTH-1:0] addr_b,
458
+ input wire [DATA_WIDTH-1:0] data_in_b,
459
+ input wire we_b, // Write enable for port B
460
+ output reg [DATA_WIDTH-1:0] data_out_b
461
+ );
462
+
463
+ // Memory array
464
+ reg [DATA_WIDTH-1:0] mem [0:DEPTH-1];
465
+
466
+ // Port A logic
467
+ always @(posedge clk) begin
468
+ if (we_a) begin
469
+ mem[addr_a] <= data_in_a;
470
+ end
471
+ data_out_a <= mem[addr_a];
472
+ end
473
+
474
+ // Port B logic (Unused)
475
+ always @(posedge clk) begin
476
+ if (we_b) begin
477
+ mem[addr_b] <= data_in_b;
478
+ end
479
+ data_out_b <= mem[addr_b];
480
+ end
481
+
482
+ endmodule
snapshots/chipsalliance_adams-bridge_pr66/src/rej_sampler/stimulus/tests/directed/rej_rand_test.yml ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ ---
16
+ testname: rej_rand_test
17
+ seed: 1 #${PLAYBOOK_RANDOM_SEED}
snapshots/chipsalliance_adams-bridge_pr66/src/rej_sampler/tb/rej_sampler.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ import sys
16
+ import os
17
+ import binascii
18
+ from hashlib import shake_128, shake_256
19
+
20
+ DILITHIUM_Q = 8380417 # 2**23 - 2**13 + 1
21
+ DILITHIUM_N = 256
22
+ DILITHIUM_LOGN = 8
23
+ DILITHIUM_ROOT_OF_UNITY = 1753
24
+
25
+ ## Category 5 parameters:
26
+ DILITHIUM_ETA = 2
27
+ DILITHIUM_ETA_BOUND = 15
28
+ DILITHIUM_TAU = 60
29
+ DILITHIUM_GAMMA1 = 2**19
30
+
31
+
32
+ class Shake():
33
+ def __init__(self, algorithm, block_length):
34
+ self.algorithm = algorithm
35
+ self.block_length = block_length
36
+ self.read_blocks = 0
37
+ self.read_data = b""
38
+
39
+ def absorb(self, input_bytes):
40
+ self.read_blocks = 0
41
+ self.read_data = b""
42
+ self.xof = self.algorithm(input_bytes)
43
+
44
+ def digest(self, input_bytes, length):
45
+ return self.algorithm(input_bytes).digest(length)
46
+
47
+ def get_n_blocks(self, n):
48
+ byte_count = self.block_length * (self.read_blocks + n)
49
+ xof_data = self.xof.digest(byte_count)
50
+ self.read_blocks += n
51
+ self.read_data = xof_data[-self.block_length*n:]
52
+
53
+ def read(self, n):
54
+ if n > len(self.read_data):
55
+ self.get_n_blocks(5*n)
56
+ send = self.read_data[:n]
57
+ self.read_data = self.read_data[n:]
58
+ return send
59
+
60
+ Shake128 = Shake(shake_128, 168)
61
+ Shake256 = Shake(shake_256, 136)
62
+
63
+ def rejection_q(seed):
64
+ global DILITHIUM_Q
65
+ global DILITHIUM_N
66
+ global buff_arr
67
+
68
+ def rejection_sample(xof):
69
+ global buff_arr
70
+ while True:
71
+ if (len(buff_arr) == 0):
72
+ log_file.write("squeezing\n")
73
+ buff_bytes = xof.read(168)
74
+ buff_arr = bytearray(buff_bytes)
75
+ buff_int = int.from_bytes(buff_bytes, "little")
76
+ input_vectors.write(f"{buff_int:0336x}")
77
+ j_bytes = buff_arr[0:3]
78
+ for i in range (3):
79
+ buff_arr.pop(0)
80
+ j = int.from_bytes(j_bytes, "little")
81
+ j &= 0x7FFFFF
82
+ if j < DILITHIUM_Q:
83
+ log_file.write("ack: " + f"{j:06x}" + "\n")
84
+ return j
85
+ log_file.write("rej: " + f"{j:06x}" + "\n")
86
+
87
+ # Initialise the XOF
88
+ Shake128.absorb(seed)
89
+ buff_arr = bytearray()
90
+ coeffs = [rejection_sample(Shake128) for _ in range(DILITHIUM_N)]
91
+ input_vectors.write("\n")
92
+ log_file.write("\n")
93
+ return coeffs
94
+
95
+ #open the input vector file for reading
96
+ input_seeds = open("input_seeds.txt", "r")
97
+ input_vectors = open("input_vectors.txt", "w")
98
+ exp_results = open("exp_results.txt", "w")
99
+ log_file = open("rej_sampler.log", "w")
100
+ Lines = input_seeds.readlines()
101
+
102
+ for line in Lines:
103
+ Coeffs = rejection_q( bytes(line.strip(),'utf-8') )
104
+ for coeff in Coeffs:
105
+ exp_results.write(f"{coeff:06x}"+" ")
106
+ exp_results.write("\n")
snapshots/chipsalliance_adams-bridge_pr66/src/rej_sampler/tb/rej_sampler_tb.sv ADDED
@@ -0,0 +1,439 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ //======================================================================
16
+ //
17
+ // rej_sampler_tb.sv
18
+ // --------
19
+ //
20
+ //======================================================================
21
+
22
+ import "DPI-C" function string getenv(input string env_name);
23
+
24
+ module rej_sampler_tb
25
+ import mldsa_sampler_pkg::*;
26
+ import mldsa_params_pkg::*;
27
+ (
28
+ `ifdef VERILATOR
29
+ input bit clk_tb
30
+ `endif
31
+ );
32
+
33
+ parameter REJ_NUM_SAMPLERS = 5;
34
+ parameter REJ_SAMPLE_W = 24;
35
+ parameter REJ_VLD_SAMPLES = 4;
36
+ parameter PISO_BUFFER_W = 1440;
37
+ parameter PISO_INPUT_RATE = 1344;
38
+ parameter PISO_OUTPUT_RATE = 120;
39
+ parameter MLDSA_Q = 8380417;
40
+ parameter MLDSA_Q_WIDTH = $clog2(MLDSA_Q) + 1;
41
+ parameter MLDSA_N = 256;
42
+
43
+
44
+ `ifndef VERILATOR
45
+ int MAX_CYCLES;
46
+ int VEC_CNT;
47
+
48
+ initial begin
49
+ // To use this from the command line, add "+MAX_CYCLES=<value>"
50
+ // to override the sim timeout
51
+ if ($value$plusargs("MAX_CYCLES=%d", MAX_CYCLES)) begin
52
+ $info("Received argument +MAX_CYCLES, with value %d", MAX_CYCLES);
53
+ end else begin
54
+ MAX_CYCLES = 20_0000;
55
+ $info("No argument provided for MAX_CYCLES, defaulting to %d", MAX_CYCLES);
56
+ end
57
+ if ($value$plusargs("VEC_CNT=%d", VEC_CNT)) begin
58
+ $info("Received argument +VEC_CNT, with value %d", VEC_CNT);
59
+ end else begin
60
+ VEC_CNT = 10;
61
+ $info("No argument provided for VEC_CNT, defaulting to %d", VEC_CNT);
62
+ end
63
+ end
64
+ `else
65
+ parameter MAX_CYCLES = 20_0000;
66
+ `endif
67
+
68
+ parameter DEBUG = 0;
69
+
70
+ parameter CLK_HALF_PERIOD = 5;
71
+
72
+ //input data to piso
73
+ logic data_valid_i;
74
+ logic data_hold_o;
75
+ logic [PISO_INPUT_RATE-1:0] data_i;
76
+
77
+ //input data
78
+ logic piso_valid;
79
+ logic piso_hold;
80
+ logic [REJ_NUM_SAMPLERS-1:0][REJ_SAMPLE_W-1:0] piso_data;
81
+
82
+ //output data
83
+ logic data_valid_o;
84
+ logic [REJ_VLD_SAMPLES-1:0][MLDSA_Q_WIDTH-1:0] data_o;
85
+
86
+ logic zeroize;
87
+
88
+ //----------------------------------------------------------------
89
+ // Register and Wire declarations.
90
+ //----------------------------------------------------------------
91
+ reg [31 : 0] cycle_ctr;
92
+ reg [31 : 0] error_ctr;
93
+ reg [31 : 0] vld_coeff_ctr;
94
+ reg [31 : 0] tc_ctr;
95
+
96
+ `ifndef VERILATOR
97
+ reg clk_tb;
98
+ `endif
99
+ reg clk_i;
100
+ reg reset_n_tb;
101
+ reg rst_ni;
102
+ reg [31 : 0] read_data;
103
+
104
+ assign clk_i = clk_tb;
105
+ assign rst_ni = reset_n_tb;
106
+
107
+ logic [MLDSA_Q_WIDTH-1:0] exp_result;
108
+ logic [MLDSA_Q_WIDTH-1:0] expected_results[$]; // queue of results
109
+
110
+ string seed_filename, vector_filename, exp_res_filename;
111
+ assign exp_res_filename = "exp_results.txt";
112
+ assign seed_filename = "input_seeds.txt";
113
+ assign vector_filename = "input_vectors.txt";
114
+
115
+ int fd_r_exp;
116
+
117
+ //----------------------------------------------------------------
118
+ // Device Under Test.
119
+ //----------------------------------------------------------------
120
+ abr_piso #(
121
+ .PISO_BUFFER_W(PISO_BUFFER_W),
122
+ .PISO_INPUT_RATE(PISO_INPUT_RATE),
123
+ .PISO_OUTPUT_RATE(PISO_OUTPUT_RATE)
124
+ ) piso_i (
125
+ .clk(clk_i),
126
+ .rst_b(rst_ni),
127
+ .zeroize(zeroize),
128
+ .valid_i(data_valid_i),
129
+ .hold_o(data_hold_o),
130
+ .data_i(data_i),
131
+ .valid_o(piso_valid),
132
+ .hold_i(piso_hold),
133
+ .data_o(piso_data)
134
+ );
135
+
136
+ rej_sampler_ctrl#(
137
+ .REJ_NUM_SAMPLERS(REJ_NUM_SAMPLERS),
138
+ .REJ_SAMPLE_W(REJ_SAMPLE_W),
139
+ .REJ_VLD_SAMPLES(REJ_VLD_SAMPLES),
140
+ .REJ_VALUE(MLDSA_Q)
141
+ ) dut (
142
+ .clk(clk_i),
143
+ .rst_b(rst_ni),
144
+ .zeroize(zeroize),
145
+ //input data
146
+ .data_valid_i(piso_valid),
147
+ .data_hold_o(piso_hold),
148
+ .data_i(piso_data),
149
+
150
+ //output data
151
+ .data_valid_o(data_valid_o),
152
+ .data_o(data_o)
153
+ );
154
+
155
+ //----------------------------------------------------------------
156
+ // clk_gen
157
+ //
158
+ // Clock generator process.
159
+ //----------------------------------------------------------------
160
+ `ifndef VERILATOR
161
+ always
162
+ begin : clk_gen
163
+ #CLK_HALF_PERIOD
164
+ clk_tb = !clk_tb;
165
+ end // clk_gen
166
+ `endif
167
+
168
+ //----------------------------------------------------------------
169
+ // sys_monitor
170
+ //
171
+ // Generates a cycle counter and displays information about
172
+ // the dut as needed.
173
+ //----------------------------------------------------------------
174
+ always @(posedge clk_tb) begin : sys_monitor
175
+ cycle_ctr = (!reset_n_tb) ? 32'h0 : cycle_ctr + 1;
176
+
177
+ // Test timeout monitor
178
+ if(cycle_ctr == MAX_CYCLES) begin
179
+ $error("Hit max cycle count (%0d) .. stopping",cycle_ctr);
180
+ $finish;
181
+ end
182
+ end
183
+
184
+ //----------------------------------------------------------------
185
+ // reset_dut()
186
+ //
187
+ // Toggles reset to force the DUT into a well defined state.
188
+ //----------------------------------------------------------------
189
+ task reset_dut;
190
+ begin
191
+ $display("*** Toggle reset.");
192
+ reset_n_tb = 0;
193
+
194
+ repeat (2) @(posedge clk_tb);
195
+ reset_n_tb = 1;
196
+
197
+ repeat (2) @(posedge clk_tb);
198
+
199
+ $display("");
200
+ end
201
+ endtask // reset_dut
202
+
203
+ //----------------------------------------------------------------
204
+ // init_sim()
205
+ //
206
+ // Initialize all counters and testbed functionality as well
207
+ // as setting the DUT inputs to defined values.
208
+ //----------------------------------------------------------------
209
+ task init_sim;
210
+ begin
211
+ error_ctr = '0;
212
+ tc_ctr = '0;
213
+ vld_coeff_ctr = '0;
214
+ `ifndef VERILATOR
215
+ clk_tb = 0;
216
+ `endif
217
+ reset_n_tb = 0;
218
+
219
+ zeroize = 0;
220
+ data_valid_i = '0;
221
+ data_i = '0;
222
+
223
+ end
224
+ endtask // init_dut
225
+
226
+ //----------------------------------------------------------------
227
+ // display_test_result()
228
+ //
229
+ // Display the accumulated test results.
230
+ //----------------------------------------------------------------
231
+ task display_test_result;
232
+ begin
233
+ if (error_ctr == 0)
234
+ begin
235
+ $display("*** All %02d test cases completed successfully.", tc_ctr);
236
+ $display("* TESTCASE PASSED");
237
+ end
238
+ else
239
+ begin
240
+ $display("*** %02d test cases completed.", tc_ctr);
241
+ $display("*** %02d errors detected during testing.", error_ctr);
242
+ $display("* TESTCASE FAILED");
243
+ end
244
+ end
245
+ endtask // display_test_result
246
+
247
+ //----------------------------------------------------------------
248
+ // Scoreboard
249
+ //----------------------------------------------------------------
250
+ initial begin
251
+ forever begin
252
+ @(posedge clk_tb)
253
+ if (data_valid_o) begin
254
+ if (expected_results.size() >= 4) begin
255
+ for (int i = 0; i < REJ_VLD_SAMPLES; i++) begin
256
+ vld_coeff_ctr += 1; //increment vld coeff ctr
257
+ if (expected_results[0] != data_o[i]) begin
258
+ //ERROR
259
+ $display("[%t] Expected results mismatch for output index %d", $time, i);
260
+ $display("[%t] Actual: %x", $time,data_o[i]);
261
+ $display("[%t] Expected: %x", $time,expected_results[0]);
262
+ error_ctr += 1;
263
+ end
264
+ expected_results.pop_front();
265
+ end
266
+ end else begin
267
+ //ERROR
268
+ $display("[%t] Not enough valid samples in the queue", $time);
269
+ error_ctr += 1;
270
+ end
271
+ end
272
+ end
273
+
274
+ end
275
+
276
+ initial begin
277
+ forever begin
278
+ @(posedge clk_tb)
279
+ if (vld_coeff_ctr == 256) begin
280
+ zeroize <= 1;
281
+ @(posedge clk_tb)
282
+ zeroize <= 0;
283
+ end
284
+ end
285
+ end
286
+
287
+ initial begin
288
+ forever begin
289
+ @(posedge zeroize)
290
+ vld_coeff_ctr = '0;
291
+ expected_results = {};
292
+ end
293
+ end
294
+
295
+ //----------------------------------------------------------------
296
+ // write_single_msg()
297
+ //
298
+ // Write the given msg to the DUT using the DUT interface.
299
+ //----------------------------------------------------------------
300
+ task write_single_msg(logic [PISO_INPUT_RATE-1:0] rand_samples);
301
+ begin
302
+ data_valid_i <= 1;
303
+ data_i <= rand_samples;
304
+
305
+ //don't like this, but solves the timing issue
306
+ @(posedge clk_tb);
307
+ //Make sure no hold
308
+ //drop this transaction if zeroize is set
309
+ while ((data_hold_o == 1) & (zeroize == 0)) @(posedge clk_tb);
310
+
311
+ //@(posedge clk_tb);
312
+ data_valid_i <= 0;
313
+ data_i <= 'x;
314
+
315
+ end
316
+ endtask // write_single_msg
317
+
318
+ //----------------------------------------------------------------
319
+ // gen_rand_test_vectors()
320
+ //----------------------------------------------------------------
321
+ task gen_rand_test_vectors;
322
+ int fd_w;
323
+
324
+ fd_w = $fopen(seed_filename, "a");
325
+ if (fd_w == 0) $error("Cannot open file %s for writing", seed_filename);
326
+
327
+ for (int i = 0; i < VEC_CNT; i++) begin
328
+ //Write random_ eeds
329
+ $fwrite(fd_w, "%0d \n", $urandom());
330
+ end
331
+ $fclose(fd_w);
332
+ //generate input vectors and expected results
333
+ $system($sformatf("python rej_sampler.py"));
334
+
335
+
336
+ //open expected results files
337
+ fd_r_exp = $fopen(exp_res_filename, "r");
338
+ if (fd_r_exp == 0) begin
339
+ $error("Cannot open file %s for reading", exp_res_filename);
340
+ error_ctr++;
341
+ end
342
+
343
+ endtask
344
+
345
+
346
+ //----------------------------------------------------------------
347
+ // run_rand_test()
348
+ //----------------------------------------------------------------
349
+ parameter PISO_INPUT_CHARS = PISO_INPUT_RATE/4;
350
+ parameter PISO_INPUT_DWORDS = PISO_INPUT_RATE/32;
351
+ task run_rand_test;
352
+ int fd_r_inp;
353
+ string line_read;
354
+ string line_substr;
355
+ int num_states;
356
+ logic [PISO_INPUT_DWORDS-1:0][31:0] test_data;
357
+
358
+ //open input vectors
359
+ fd_r_inp = $fopen(vector_filename, "r");
360
+ if (fd_r_inp == 0) begin
361
+ $error("Cannot open file %s for reading", vector_filename);
362
+ error_ctr++;
363
+ end
364
+
365
+ //open expected results files
366
+ fd_r_exp = $fopen(exp_res_filename, "r");
367
+ if (fd_r_exp == 0) begin
368
+ $error("Cannot open file %s for reading", exp_res_filename);
369
+ error_ctr++;
370
+ end
371
+
372
+ for (int iter = 0; iter < VEC_CNT; iter++) begin
373
+ //get the next exp result
374
+ if (!($fgets(line_read, fd_r_exp))) begin
375
+ $error("Failed to read a new line");
376
+ error_ctr++;
377
+ end
378
+ for (int res = 0; res < MLDSA_N; res++) begin
379
+ exp_result = line_read.substr(res*7, res*7 + 7-2).atohex();
380
+ expected_results.push_back(exp_result);
381
+ end
382
+
383
+ //get the next input vector
384
+ if (!($fgets(line_read, fd_r_inp))) begin
385
+ $error("Failed to read a new line");
386
+ error_ctr++;
387
+ end
388
+
389
+ num_states = (line_read.len()/PISO_INPUT_CHARS);
390
+ //$display("Vector Length: %d", line_read.len());
391
+ //$display("Number of states: %d", num_states);
392
+ for (int state = 0; state < num_states; state++) begin
393
+ line_substr = line_read.substr(state*PISO_INPUT_CHARS , (state*PISO_INPUT_CHARS)+PISO_INPUT_CHARS-1);
394
+ //$display("State: %s", line_substr);
395
+ for (int dword = 0; dword < PISO_INPUT_DWORDS; dword++) begin
396
+ test_data[dword] = line_substr.substr((PISO_INPUT_CHARS - dword*8 - 8), (PISO_INPUT_CHARS - dword*8 - 1)).atohex();
397
+ //$display("Dword: %d - %h", dword, test_data[dword]);
398
+ end
399
+ write_single_msg(test_data);
400
+ end
401
+
402
+ wait (zeroize == 1);
403
+ wait (zeroize == 0);
404
+
405
+ tc_ctr++;
406
+
407
+ end
408
+
409
+ $fclose(fd_r_inp);
410
+
411
+ endtask // run_hw_if_test
412
+
413
+ //----------------------------------------------------------------
414
+ // The main test functionality.
415
+ //----------------------------------------------------------------
416
+
417
+ initial
418
+ begin : main
419
+ $write("PLAYBOOK_RANDOM_SEED = %s\n", getenv("PLAYBOOK_RANDOM_SEED"));
420
+ $display(" -- Testbench for REJ_SAMPLER started --");
421
+
422
+ init_sim();
423
+ reset_dut();
424
+
425
+ gen_rand_test_vectors();
426
+ run_rand_test();
427
+
428
+ @(posedge clk_tb);
429
+ display_test_result();
430
+
431
+ $display(" -- Testbench for REJ_SAMPLER done. --");
432
+ $finish;
433
+ end // main
434
+
435
+ endmodule // rej_sampler_tb
436
+
437
+ //======================================================================
438
+ // EOF rej_sampler_tb.sv
439
+ //======================================================================
snapshots/chipsalliance_adams-bridge_pr66/src/sk_encode/config/compile.yml ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (C) Microsoft Corporation. All rights reserved.
2
+
3
+ # Go here for details on compile.yml syntax:
4
+ # https://dev.azure.com/ms-tsd/Ether/_git/sim-tools?path=%2FSourceFileSpec.docx&version=GBmaster
5
+
6
+ ---
7
+ provides: [skencode]
8
+ schema_version: 2.4.0
9
+ requires:
10
+ - mldsa_defines
11
+ - skdecode_pkg
12
+ - abr_libs
13
+ targets:
14
+ rtl:
15
+ directories: [$COMPILE_ROOT/rtl]
16
+ files:
17
+ - $COMPILE_ROOT/rtl/skencode.sv
18
+
19
+ tops: [skencode]
20
+ rtl_lint:
21
+ directories: []
22
+ waiver_files:
23
+ - $MSFT_REPO_ROOT/src/sk_decode/config/design_lint/skencode/sglint_waivers
24
+ ---
25
+ provides: [skencode_tb]
26
+ schema_version: 2.4.0
27
+ requires:
28
+ - skencode
29
+ - skdecode_pkg
30
+ targets:
31
+ rtl:
32
+ directories: [$COMPILE_ROOT/tb]
33
+ files:
34
+ - $COMPILE_ROOT/tb/skencode_tb.sv
35
+ tops: [skencode_tb]
36
+ sim:
37
+ pre_exec: 'echo "[PRE-EXEC] Copying s1 bytes hex to $PWD" && cp $COMPILE_ROOT/tb/pack_eta.py .'
snapshots/chipsalliance_adams-bridge_pr66/src/sk_encode/config/design_lint/skencode/sglint_waivers ADDED
File without changes
snapshots/chipsalliance_adams-bridge_pr66/src/sk_encode/config/skencode.vf ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ +incdir+${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl
2
+ +incdir+${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl
3
+ +incdir+${ADAMSBRIDGE_ROOT}/src/sk_decode/rtl
4
+ +incdir+${ADAMSBRIDGE_ROOT}/src/sk_encode/rtl
5
+ ${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl/mldsa_config_defines.svh
6
+ ${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl/mldsa_params_pkg.sv
7
+ ${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl/mldsa_reg_pkg.sv
8
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_sva.svh
9
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_macros.svh
10
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_1r1w_ram.sv
11
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_1r1w_be_ram.sv
12
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_1r1w_512x4_ram.sv
13
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_ram_regout.sv
14
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_icg.sv
15
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_2ff_sync.sv
16
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_piso_4.sv
17
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_piso.sv
18
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_sample_buffer.sv
19
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_ahb_defines_pkg.sv
20
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_ahb_slv_sif.sv
21
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_AND.sv
22
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_delay_masked_shares.sv
23
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_add_sub_mod_Boolean.sv
24
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_MUX.sv
25
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_N_bit_Boolean_sub.sv
26
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_full_adder.sv
27
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_A2B_conv.sv
28
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_N_bit_Boolean_adder.sv
29
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_B2A_conv.sv
30
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_N_bit_mult.sv
31
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_N_bit_mult_two_share.sv
32
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_adder.sv
33
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_add_sub_mod.sv
34
+ ${ADAMSBRIDGE_ROOT}/src/sk_decode/rtl/skdecode_defines_pkg.sv
35
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_N_bit_Arith_adder.sv
36
+ ${ADAMSBRIDGE_ROOT}/src/sk_encode/rtl/skencode.sv
snapshots/chipsalliance_adams-bridge_pr66/src/sk_encode/config/skencode_tb.vf ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ +incdir+${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl
2
+ +incdir+${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl
3
+ +incdir+${ADAMSBRIDGE_ROOT}/src/sk_decode/rtl
4
+ +incdir+${ADAMSBRIDGE_ROOT}/src/sk_encode/rtl
5
+ +incdir+${ADAMSBRIDGE_ROOT}/src/sk_encode/tb
6
+ ${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl/mldsa_config_defines.svh
7
+ ${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl/mldsa_params_pkg.sv
8
+ ${ADAMSBRIDGE_ROOT}/src/mldsa_top/rtl/mldsa_reg_pkg.sv
9
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_sva.svh
10
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_macros.svh
11
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_1r1w_ram.sv
12
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_1r1w_be_ram.sv
13
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_1r1w_512x4_ram.sv
14
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_ram_regout.sv
15
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_icg.sv
16
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_2ff_sync.sv
17
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_piso_4.sv
18
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_piso.sv
19
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_sample_buffer.sv
20
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_ahb_defines_pkg.sv
21
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_ahb_slv_sif.sv
22
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_AND.sv
23
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_delay_masked_shares.sv
24
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_add_sub_mod_Boolean.sv
25
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_MUX.sv
26
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_N_bit_Boolean_sub.sv
27
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_full_adder.sv
28
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_A2B_conv.sv
29
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_N_bit_Boolean_adder.sv
30
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_B2A_conv.sv
31
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_N_bit_mult.sv
32
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_N_bit_mult_two_share.sv
33
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_adder.sv
34
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_add_sub_mod.sv
35
+ ${ADAMSBRIDGE_ROOT}/src/sk_decode/rtl/skdecode_defines_pkg.sv
36
+ ${ADAMSBRIDGE_ROOT}/src/abr_libs/rtl/abr_masked_N_bit_Arith_adder.sv
37
+ ${ADAMSBRIDGE_ROOT}/src/sk_encode/rtl/skencode.sv
38
+ ${ADAMSBRIDGE_ROOT}/src/sk_encode/tb/skencode_tb.sv
snapshots/chipsalliance_adams-bridge_pr66/src/sk_encode/rtl/skencode.sv ADDED
@@ -0,0 +1,400 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //======================================================================
15
+ //
16
+ // skencode.sv
17
+ // --------
18
+ // The skencode module performs the encoding of secret key components into a packed format.
19
+ // This module supports the MLDSA scheme, encoding sk inputs into s1, s2 polynomials.
20
+ // The output coefficients are packed into 24-bit format.
21
+ // The design supports dual memory writes per cycle, processing 8 values per cycle for s1 and s2 encoding.
22
+ // The module operates under the control of a single enable pulse, triggering the encoding process for all polynomials.
23
+ // Upon completion, control is transferred back to the host. If any invalid input is detected, the module raises
24
+ // an error flag, halting the operation. Error handling and notifications are left to the host's implementation.
25
+ // 8 instances of the s1s2 encoder can operate simultaneously, each handling 24 bits of data per cycle.
26
+ //
27
+ //======================================================================
28
+
29
+ module skencode
30
+ import mldsa_params_pkg::*;
31
+ import skdecode_defines_pkg::*;
32
+ #(
33
+ parameter MEM_ADDR_WIDTH = 15,
34
+ parameter MLDSA_Q = 23'd8380417,
35
+ parameter MLDSA_L = 'h7,
36
+ parameter MLDSA_K = 'h8,
37
+ parameter MLDSA_N = 'd256,
38
+ parameter REG_SIZE = 24,
39
+ parameter AHB_DATA_WIDTH = 32
40
+ )
41
+ (
42
+ input wire clk,
43
+ input wire reset_n,
44
+ input wire zeroize,
45
+
46
+ input wire skencode_enable,
47
+ input wire [MEM_ADDR_WIDTH-1:0] dest_base_addr,
48
+ input wire [MEM_ADDR_WIDTH-1:0] src_base_addr,
49
+ input wire [3:0][REG_SIZE-1:0] mem_a_rd_data,
50
+ input wire [3:0][REG_SIZE-1:0] mem_b_rd_data,
51
+
52
+ output mem_if_t keymem_a_wr_req,
53
+ output mem_if_t mem_a_rd_req,
54
+ output mem_if_t mem_b_rd_req,
55
+ output logic [AHB_DATA_WIDTH-1:0] keymem_a_wr_data,
56
+
57
+ output logic skencode_error,
58
+ output logic skencode_done
59
+ );
60
+
61
+ localparam THE_LAST_ADDR = ((MLDSA_K * MLDSA_N)/4)+((MLDSA_L * MLDSA_N)/4)-1;
62
+ localparam THE_LAST_API = ((MLDSA_K +MLDSA_L)*MLDSA_N*3)/32;
63
+
64
+ // Main State Machine States
65
+ // TODO: Define the state names in a pakcage.
66
+ localparam IDLE = 3'b000,
67
+ READ = 3'b001,
68
+ READ_and_ENC = 3'b010,
69
+ READ_ENC_and_CONSUME = 3'b011,
70
+ ENC_and_CONSUME = 3'b100,
71
+ CONSUME = 3'b101,
72
+ CONSUME_LAST = 3'b110,
73
+ DONE = 3'b111;
74
+
75
+ // Main State Machine States
76
+ localparam WAIT_BUFFER = 3'b001,
77
+ WRITE = 3'b010,
78
+ STALL = 3'b011,
79
+ GET_LAST = 3'b100;
80
+
81
+
82
+ logic [2:0] main_state, next_main_state, write_state, next_write_state;
83
+ logic error_flag, asserted_error_flag;
84
+ logic [7:0][2:0] encoded_coeffs;
85
+ logic [7:0] encoding_error;
86
+ logic [23:0] one_encoding_string;
87
+ logic [95:0] buffer;
88
+ logic [1:0] producer_selector, consumer_selector;
89
+ logic write_buffer;
90
+ logic [31:0] num_mem_operands, num_api_operands; // encoded each four coeff will increment these by one
91
+ logic [MEM_ADDR_WIDTH-1:0] locked_dest_addr, locked_src_addr; // this ensures that addresses are captured when the block is enabled
92
+
93
+ assign one_encoding_string = {encoded_coeffs[7],encoded_coeffs[6],encoded_coeffs[5],encoded_coeffs[4],
94
+ encoded_coeffs[3],encoded_coeffs[2],encoded_coeffs[1],encoded_coeffs[0]};
95
+
96
+ //FIXME enable this later?
97
+ assign error_flag = 0;//encoding_error[0] | encoding_error[1] | encoding_error[2] | encoding_error[3] |
98
+ //encoding_error[4] | encoding_error[5] | encoding_error[6] | encoding_error[7];
99
+
100
+
101
+ // State Machine: Updates main_state and write_state based on current conditions and next states.
102
+ always_ff @(posedge clk or negedge reset_n) begin
103
+ if (!reset_n) begin
104
+ main_state <= IDLE;
105
+ write_state <= IDLE;
106
+ end
107
+ else if (zeroize) begin
108
+ main_state <= IDLE;
109
+ write_state <= IDLE;
110
+ end
111
+ else begin
112
+ if (skencode_error) begin
113
+ main_state <= IDLE;
114
+ write_state <= IDLE;
115
+ end
116
+ else begin
117
+ main_state <= next_main_state;
118
+ write_state <= next_write_state;
119
+ end
120
+ end
121
+ end
122
+
123
+ // Determines next state for main_state based on current main_state and conditions.
124
+ always_comb begin
125
+ case (main_state)
126
+ IDLE: begin
127
+ if (skencode_enable)
128
+ next_main_state = READ;
129
+ else
130
+ next_main_state = IDLE;
131
+ end
132
+ READ: begin
133
+ next_main_state = READ_and_ENC;
134
+ end
135
+ READ_and_ENC: begin
136
+ next_main_state = READ_ENC_and_CONSUME;
137
+ end
138
+ READ_ENC_and_CONSUME: begin
139
+ if ((num_mem_operands+1) == THE_LAST_ADDR) begin
140
+ next_main_state = ENC_and_CONSUME;
141
+ end
142
+ else begin
143
+ next_main_state = READ_ENC_and_CONSUME;
144
+ end
145
+ end
146
+ ENC_and_CONSUME: begin
147
+ next_main_state = CONSUME;
148
+ end
149
+ CONSUME: begin
150
+ next_main_state = CONSUME_LAST;
151
+ end
152
+ CONSUME_LAST: begin
153
+ next_main_state = DONE;
154
+ end
155
+ DONE: begin
156
+ next_main_state = IDLE;
157
+ end
158
+ default: begin
159
+ next_main_state = IDLE;
160
+ end
161
+ endcase
162
+ end
163
+
164
+ // Determines next state for write_state based on current write_state and conditions.
165
+ always_comb begin
166
+ case (write_state)
167
+ IDLE: begin
168
+ if (main_state == READ_ENC_and_CONSUME)
169
+ next_write_state = WAIT_BUFFER;
170
+ else
171
+ next_write_state = IDLE;
172
+ end
173
+ WAIT_BUFFER: begin
174
+ if (producer_selector == 'h1)
175
+ next_write_state = WRITE;
176
+ else
177
+ next_write_state = WAIT_BUFFER;
178
+ end
179
+ WRITE: begin
180
+ if (consumer_selector == 'h1)
181
+ next_write_state = STALL;
182
+ else
183
+ next_write_state = WRITE;
184
+ end
185
+ STALL: begin
186
+ if (num_api_operands == THE_LAST_API) begin
187
+ next_write_state = GET_LAST;
188
+ end
189
+ else begin
190
+ next_write_state = WAIT_BUFFER;
191
+ end
192
+ end
193
+ GET_LAST: begin
194
+ next_write_state = DONE;
195
+ end
196
+ DONE: begin
197
+ next_write_state = IDLE;
198
+ end
199
+ default: begin
200
+ next_write_state = IDLE;
201
+ end
202
+ endcase
203
+ end
204
+
205
+ // Write request generation
206
+ always_ff @(posedge clk or negedge reset_n) begin
207
+ if (!reset_n) begin
208
+ keymem_a_wr_req <= '{rd_wr_en: RW_IDLE, addr: '0};
209
+ num_api_operands <= 'h0;
210
+ consumer_selector <= 'h0;
211
+ end
212
+ else if (zeroize) begin
213
+ keymem_a_wr_req <= '{rd_wr_en: RW_IDLE, addr: '0};
214
+ num_api_operands <= 'h0;
215
+ consumer_selector <= 'h0;
216
+ end
217
+ else begin
218
+ if (write_state == WRITE)
219
+ consumer_selector <= consumer_selector + 'h1;
220
+ else
221
+ consumer_selector <= 'h0;
222
+
223
+ if (write_state == IDLE ) begin
224
+ num_api_operands <= 'h0;
225
+ end
226
+ else if (write_state == WRITE || (write_state == WAIT_BUFFER && producer_selector == 'h1)) begin
227
+ num_api_operands <= num_api_operands +'h1;
228
+ end
229
+
230
+ if (write_state == WRITE || (write_state == WAIT_BUFFER && producer_selector == 'h1)) begin
231
+ keymem_a_wr_req <= '{rd_wr_en: RW_WRITE, addr: locked_dest_addr + num_api_operands};
232
+ end
233
+ else begin
234
+ keymem_a_wr_req <= '{rd_wr_en: RW_IDLE, addr: '0};
235
+ end
236
+ end
237
+ end
238
+
239
+ // Controls and updates the state of various internal signals and registers based on the state machine and input signals.
240
+ // Resets or initializes all signals execpt for the write operation related logics.
241
+ // Locks destination and source addresses when skencode_enable is active.
242
+ // Manages write_buffer and producer_selector based on the main state machine.
243
+ // Issues read requests to memory and increments num_mem_operands during appropriate states.
244
+ // Signals completion (skencode_done) and checks for errors (skencode_error) based on operation progress.
245
+ always_ff @(posedge clk or negedge reset_n) begin
246
+ if (!reset_n) begin
247
+ write_buffer <= 'h0;
248
+ producer_selector <= 'h0;
249
+ num_mem_operands <= 'h0;
250
+ locked_dest_addr <= 'h0;
251
+ locked_src_addr <= 'h0;
252
+ skencode_done <= 'h0;
253
+ skencode_error <= 'h0;
254
+ mem_a_rd_req <= '{rd_wr_en: RW_IDLE, addr: '0};
255
+ mem_b_rd_req <= '{rd_wr_en: RW_IDLE, addr: '0};
256
+ end
257
+ else if (zeroize) begin
258
+ write_buffer <= 'h0;
259
+ producer_selector <= 'h0;
260
+ num_mem_operands <= 'h0;
261
+ locked_dest_addr <= 'h0;
262
+ locked_src_addr <= 'h0;
263
+ skencode_done <= 'h0;
264
+ skencode_error <= 'h0;
265
+ mem_a_rd_req <= '{rd_wr_en: RW_IDLE, addr: '0};
266
+ mem_b_rd_req <= '{rd_wr_en: RW_IDLE, addr: '0};
267
+ end
268
+ else begin
269
+ if (skencode_enable) begin
270
+ locked_dest_addr <= dest_base_addr;
271
+ locked_src_addr <= src_base_addr;
272
+ end
273
+
274
+ if (main_state == READ_and_ENC || main_state == READ_ENC_and_CONSUME || main_state == ENC_and_CONSUME) begin
275
+ write_buffer <= 'h1;
276
+ end
277
+ else begin
278
+ write_buffer <= 'h0;
279
+ end
280
+
281
+ if (main_state == READ_ENC_and_CONSUME || main_state == ENC_and_CONSUME)
282
+ producer_selector <= producer_selector + 1'b1;
283
+ else
284
+ producer_selector <= 'h0;
285
+
286
+ if (main_state == READ || main_state == READ_and_ENC || main_state == READ_ENC_and_CONSUME) begin
287
+ mem_a_rd_req <= '{rd_wr_en: RW_READ, addr: locked_src_addr + num_mem_operands};
288
+ mem_b_rd_req <= '{rd_wr_en: RW_READ, addr: locked_src_addr + num_mem_operands + 1};
289
+ num_mem_operands <= num_mem_operands +2'h2;
290
+ end
291
+ else begin
292
+ mem_a_rd_req <= '{rd_wr_en: RW_IDLE, addr: '0};
293
+ mem_b_rd_req <= '{rd_wr_en: RW_IDLE, addr: '0};
294
+ num_mem_operands <= 'h0;
295
+ end
296
+
297
+ if (main_state == DONE) begin
298
+ skencode_done <= 'h1;
299
+ end
300
+ else begin
301
+ skencode_done <= 'h0;
302
+ end
303
+
304
+ if (skencode_error == 1'b0 && main_state != IDLE)
305
+ skencode_error <= error_flag;
306
+ end
307
+ end
308
+
309
+ // Manages the 96-bit buffer register based on the write_buffer signal and producer_selector state.
310
+ // Accumulates 24-bit encoded strings into the 96-bit buffer, preparing data for further processing or output.
311
+ always_ff @(posedge clk or negedge reset_n) begin
312
+ if (!reset_n) begin
313
+ buffer <= 'h0;
314
+ end
315
+ else if (zeroize) begin
316
+ buffer <= 'h0;
317
+ end
318
+ else begin
319
+ if (write_buffer) begin
320
+ if (producer_selector == 2'h0)
321
+ buffer[23:0] <= one_encoding_string;
322
+ else if (producer_selector == 2'h1)
323
+ buffer[47:24] <= one_encoding_string;
324
+ else if (producer_selector == 2'h2)
325
+ buffer[71:48] <= one_encoding_string;
326
+ else
327
+ buffer[95:72] <= one_encoding_string;
328
+ end
329
+ end
330
+ end
331
+
332
+ // Assigns the appropriate 32-bit portion of the buffer to keymem_a_wr_data based on consumer_selector.
333
+ always_comb begin : API_org
334
+ case(consumer_selector)
335
+ 2'h0: keymem_a_wr_data = buffer[31:0];
336
+ 2'h1: keymem_a_wr_data = buffer[63:32];
337
+ 2'h2: keymem_a_wr_data = buffer[95:64];
338
+ 2'h3: keymem_a_wr_data = 'h0; // Ignored case
339
+ endcase
340
+ end : API_org
341
+
342
+ always_comb begin : encoding
343
+ for (int i = 0; i < 4; i++) begin
344
+ unique case(mem_a_rd_data[i])
345
+ 'h0: begin
346
+ encoded_coeffs[i] = 'h2;
347
+ encoding_error[i] = 'h0;
348
+ end
349
+ 'h1: begin
350
+ encoded_coeffs[i] = 'h1;
351
+ encoding_error[i] = 'h0;
352
+ end
353
+ 'h2: begin
354
+ encoded_coeffs[i] = 'h0;
355
+ encoding_error[i] = 'h0;
356
+ end
357
+ MLDSA_Q-1: begin
358
+ encoded_coeffs[i] = 'h3;
359
+ encoding_error[i] = 'h0;
360
+ end
361
+ MLDSA_Q-2: begin
362
+ encoded_coeffs[i] = 'h4;
363
+ encoding_error[i] = 'h0;
364
+ end
365
+ default: begin
366
+ encoded_coeffs[i] = 'h0;
367
+ encoding_error[i] = 'h1;
368
+ end
369
+ endcase
370
+ unique case(mem_b_rd_data[i])
371
+ 'h0: begin
372
+ encoded_coeffs[i+4] = 'h2;
373
+ encoding_error[i+4] = 'h0;
374
+ end
375
+ 'h1: begin
376
+ encoded_coeffs[i+4] = 'h1;
377
+ encoding_error[i+4] = 'h0;
378
+ end
379
+ 'h2: begin
380
+ encoded_coeffs[i+4] = 'h0;
381
+ encoding_error[i+4] = 'h0;
382
+ end
383
+ MLDSA_Q-1: begin
384
+ encoded_coeffs[i+4] = 'h3;
385
+ encoding_error[i+4] = 'h0;
386
+ end
387
+ MLDSA_Q-2: begin
388
+ encoded_coeffs[i+4] = 'h4;
389
+ encoding_error[i+4] = 'h0;
390
+ end
391
+ default: begin
392
+ encoded_coeffs[i+4] = 'h0;
393
+ encoding_error[i+4] = 'h1;
394
+ end
395
+ endcase
396
+ end
397
+ end : encoding
398
+
399
+
400
+ endmodule
snapshots/chipsalliance_adams-bridge_pr66/src/sk_encode/tb/pack_eta.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ import random
16
+
17
+ # Constants
18
+ Q = 8380417
19
+ VALID_COEFFICIENTS = [Q - 2, Q - 1, 0, 1, 2]
20
+ INVALID_COEFFICIENTS = [Q + 1, -1, Q + 2, 123456] # Example invalid values
21
+ K = 8
22
+ L = 7
23
+ N = 256
24
+ TOTAL_COEFFICIENTS = (K + L) * N
25
+
26
+ def map_coefficient(coef):
27
+ """Map the input coefficient to the corresponding 3-bit value."""
28
+ mapping = {
29
+ Q - 2: 4,
30
+ Q - 1: 3,
31
+ 0: 2,
32
+ 1: 1,
33
+ 2: 0
34
+ }
35
+ return mapping.get(coef, None)
36
+
37
+ def generate_random_coefficients(num_coeffs, include_invalid=False):
38
+ """Generate a list of random coefficients, including invalid ones if specified."""
39
+ coefficients = []
40
+ for _ in range(num_coeffs):
41
+ if include_invalid and random.random() < 0.2:
42
+ # 20% chance to pick an invalid coefficient
43
+ coefficients.append(random.choice(INVALID_COEFFICIENTS))
44
+ else:
45
+ # 80% chance to pick a valid coefficient
46
+ coefficients.append(random.choice(VALID_COEFFICIENTS))
47
+ return coefficients
48
+
49
+ def generate_files(input_coefficients, input_file, output_file):
50
+ """Generate files for the input coefficients and their expected outputs."""
51
+ output_lines = []
52
+
53
+ # Write input coefficients to input file
54
+ with open(input_file, 'w') as f_input:
55
+ for coef in input_coefficients:
56
+ f_input.write(f"{coef:X}\n")
57
+
58
+ # Process coefficients for expected output
59
+ for i in range(0, len(input_coefficients), 32):
60
+ current_line = 0
61
+ for j in range(32):
62
+ if i + j < len(input_coefficients):
63
+ coef = input_coefficients[i + j]
64
+ mapped_value = map_coefficient(coef)
65
+ if mapped_value is not None:
66
+ # Shift the current line to the right by 3 and add the mapped value to the left
67
+ current_line = (mapped_value << (j * 3)) | current_line
68
+
69
+ if current_line is not None:
70
+ # Convert the line to a 96-bit hex string
71
+ final_hex = f"{current_line:024X}"
72
+ # Add the line to output lines
73
+ output_lines.append(final_hex)
74
+
75
+ # Write expected output lines to the output file
76
+ with open(output_file, 'w') as f_output:
77
+ for line in output_lines:
78
+ f_output.write(f"{line}\n")
79
+
80
+ # Generate normal and error test vectors
81
+ normal_coefficients = generate_random_coefficients(TOTAL_COEFFICIENTS)
82
+ error_coefficients = generate_random_coefficients(TOTAL_COEFFICIENTS, include_invalid=True)
83
+
84
+ # Output the files with specified names
85
+ generate_files(normal_coefficients, 'input_sk.hex', 'output_encoded_sk.hex')
86
+ generate_files(error_coefficients, 'error_input_sk.hex', 'error_output_encoded_sk.hex')
snapshots/chipsalliance_adams-bridge_pr66/src/sk_encode/tb/skencode_tb.sv ADDED
@@ -0,0 +1,382 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ //======================================================================
16
+ //
17
+ // skencode_tb.sv
18
+ // ---------------
19
+ // Testbench for skencode module
20
+ //======================================================================
21
+
22
+ `default_nettype none
23
+
24
+ module skencode_tb
25
+ import mldsa_params_pkg::*;
26
+ import skdecode_defines_pkg::*;
27
+ #(
28
+ parameter REG_SIZE = 24,
29
+ parameter MEM_ADDR_WIDTH = MLDSA_MEM_ADDR_WIDTH,
30
+ parameter MLDSA_Q = 23'd8380417,
31
+ parameter AHB_DATA_WIDTH = 32,
32
+ parameter MLDSA_L = 'h7,
33
+ parameter MLDSA_K = 'h8
34
+ )
35
+ ();
36
+ localparam NUM_OF_COEFF = MLDSA_N * (MLDSA_L + MLDSA_K);
37
+ parameter CLK_HALF_PERIOD = 5;
38
+ parameter CLK_PERIOD = 2 * CLK_HALF_PERIOD;
39
+
40
+ //----------------------------------------------------------------
41
+ // Register and Wire declarations.
42
+ //----------------------------------------------------------------
43
+ reg clk_tb;
44
+ reg reset_n_tb;
45
+ reg zeroize_tb;
46
+ reg skencode_enable_tb;
47
+ reg [MEM_ADDR_WIDTH-1:0] src_base_addr_tb;
48
+ reg [MEM_ADDR_WIDTH-1:0] dest_base_addr_tb;
49
+ wire [3:0][REG_SIZE-1:0] mem_a_rd_data_tb;
50
+ wire [3:0][REG_SIZE-1:0] mem_b_rd_data_tb;
51
+ wire skencode_done_tb;
52
+ mem_if_t mem_a_rd_req_tb;
53
+ mem_if_t mem_b_rd_req_tb;
54
+ wire [AHB_DATA_WIDTH-1:0] keymem_a_wr_data_tb;
55
+ mem_if_t keymem_a_wr_req_tb;
56
+ wire skencode_error_tb;
57
+
58
+ reg [23:0] input_mem [0:NUM_OF_COEFF-1];
59
+ reg [AHB_DATA_WIDTH*3-1:0] expected_output_mem [0:NUM_OF_COEFF/32-1];
60
+ reg [AHB_DATA_WIDTH*3-1:0] actual_output_mem [0:NUM_OF_COEFF/32-1];
61
+
62
+ skencode #(
63
+ .MEM_ADDR_WIDTH(MEM_ADDR_WIDTH),
64
+ .MLDSA_Q(MLDSA_Q),
65
+ .REG_SIZE(REG_SIZE),
66
+ .AHB_DATA_WIDTH(AHB_DATA_WIDTH)
67
+ ) dut (
68
+ .clk(clk_tb),
69
+ .reset_n(reset_n_tb),
70
+ .zeroize(zeroize_tb),
71
+ .skencode_enable(skencode_enable_tb),
72
+ .src_base_addr(src_base_addr_tb),
73
+ .dest_base_addr(dest_base_addr_tb),
74
+ .mem_a_rd_req(mem_a_rd_req_tb),
75
+ .mem_b_rd_req(mem_b_rd_req_tb),
76
+ .mem_a_rd_data(mem_a_rd_data_tb),
77
+ .mem_b_rd_data(mem_b_rd_data_tb),
78
+ .keymem_a_wr_req(keymem_a_wr_req_tb),
79
+ .keymem_a_wr_data(keymem_a_wr_data_tb),
80
+ .skencode_done(skencode_done_tb),
81
+ .skencode_error(skencode_error_tb)
82
+ );
83
+
84
+ wire [AHB_DATA_WIDTH-1:0] dummy_mem_out0, dummy_mem_out1;
85
+
86
+ genvar k;
87
+ generate
88
+ for (k = 0; k < 4; k = k +1) begin : mem_blocks
89
+ dual_port_memory #(
90
+ .ADDR_WIDTH(MEM_ADDR_WIDTH),
91
+ .DATA_WIDTH(REG_SIZE),
92
+ .DEPTH(NUM_OF_COEFF)
93
+ )
94
+ input_memory (
95
+ .clk(clk_tb),
96
+ .addr_a(mem_a_rd_req_tb.addr),
97
+ .data_in_a(24'h0),
98
+ .we_a(1'b0),
99
+ .data_out_a(mem_a_rd_data_tb[k]),
100
+ .addr_b(mem_b_rd_req_tb.addr),
101
+ .data_in_b(24'h0),
102
+ .we_b(1'b0),
103
+ .data_out_b(mem_b_rd_data_tb[k])
104
+ );
105
+ end : mem_blocks
106
+ endgenerate
107
+
108
+ dual_port_memory #(
109
+ .ADDR_WIDTH(MEM_ADDR_WIDTH),
110
+ .DATA_WIDTH(AHB_DATA_WIDTH),
111
+ .DEPTH(NUM_OF_COEFF)
112
+ )
113
+ output_memory (
114
+ .clk(clk_tb),
115
+ .addr_a(keymem_a_wr_req_tb.addr),
116
+ .data_in_a(keymem_a_wr_data_tb),
117
+ .we_a((keymem_a_wr_req_tb.rd_wr_en == RW_WRITE)),
118
+ .data_out_a(dummy_mem_out0),
119
+ .addr_b(15'b0),
120
+ .data_in_b(32'b0),
121
+ .we_b(1'b0),
122
+ .data_out_b(dummy_mem_out1)
123
+ );
124
+
125
+ //----------------------------------------------------------------
126
+ // clk_gen
127
+ //
128
+ // Always running clock generator process.
129
+ //----------------------------------------------------------------
130
+ always
131
+ begin : clk_gen
132
+ #CLK_HALF_PERIOD;
133
+ clk_tb = !clk_tb;
134
+ end // clk_gen
135
+
136
+ task init_sim;
137
+ begin
138
+ $display("Start of init\n");
139
+ clk_tb = 0;
140
+ reset_n_tb = 0;
141
+ zeroize_tb = 0;
142
+ skencode_enable_tb = 0;
143
+ src_base_addr_tb = 0;
144
+ dest_base_addr_tb = 0;
145
+ end
146
+ endtask
147
+
148
+ //----------------------------------------------------------------
149
+ // reset_dut()
150
+ //
151
+ // Toggle reset to put the DUT into a well known state.
152
+ //----------------------------------------------------------------
153
+ task reset_dut;
154
+ begin
155
+ $display("*** Toggle reset.");
156
+ reset_n_tb = 0;
157
+ #(3 * CLK_PERIOD);
158
+ reset_n_tb = 1;
159
+ $display("End of reset");
160
+ end
161
+ endtask // reset_dut
162
+
163
+ task overwrite_memory_content(reg [MEM_ADDR_WIDTH-1:0] src_base_addr);
164
+ int i, j;
165
+
166
+ // Overwrite input memory with the values from input_mem
167
+ for (i = 0; i < (NUM_OF_COEFF/4); i = i + 1) begin
168
+ mem_blocks[0].input_memory.mem[i+src_base_addr] = input_mem[4*i + 0];
169
+ mem_blocks[1].input_memory.mem[i+src_base_addr] = input_mem[4*i + 1];
170
+ mem_blocks[2].input_memory.mem[i+src_base_addr] = input_mem[4*i + 2];
171
+ mem_blocks[3].input_memory.mem[i+src_base_addr] = input_mem[4*i + 3];
172
+ end
173
+ endtask
174
+
175
+ task read_memory_content(input reg [MEM_ADDR_WIDTH-1:0] dest_base_addr);
176
+ int i, j;
177
+ // Read output memory into the actual_output_mem array
178
+ for (i = 0; i < (NUM_OF_COEFF)/32; i = i + 1) begin
179
+ actual_output_mem[i][31:0] = output_memory.mem[3*i+0+dest_base_addr];
180
+ actual_output_mem[i][63:32] = output_memory.mem[3*i+1+dest_base_addr];
181
+ actual_output_mem[i][95:64] = output_memory.mem[3*i+2+dest_base_addr];
182
+ end
183
+ endtask
184
+
185
+ task read_test_vectors(input reg [MEM_ADDR_WIDTH-1:0] src_base_addr);
186
+ string input_file = "input_sk.hex";
187
+ string output_file = "output_encoded_sk.hex";
188
+ integer file, ret;
189
+ int i;
190
+ string line;
191
+
192
+ // Read input file
193
+ file = $fopen(input_file, "r");
194
+ if (file == 0) $error("Cannot open %s for reading\n", input_file);
195
+ i = 0;
196
+ while (!$feof(file)) begin
197
+ if($fgets(line,file)) begin
198
+ ret = $sscanf(line, "%h", input_mem[i]);
199
+ i = i + 1;
200
+ end
201
+ end
202
+ $fclose(file);
203
+ overwrite_memory_content(src_base_addr);
204
+
205
+ // Read expected output file
206
+ file = $fopen(output_file, "r");
207
+ if (file == 0) $error("Cannot open %s for reading\n", output_file);
208
+ i = 0;
209
+ while (!$feof(file)) begin
210
+ if($fgets(line,file)) begin
211
+ ret = $sscanf(line, "%h", expected_output_mem[i]);
212
+ i = i + 1;
213
+ end
214
+ end
215
+ $fclose(file);
216
+ endtask
217
+
218
+
219
+ task read_error_test_vectors(input reg [MEM_ADDR_WIDTH-1:0] src_base_addr);
220
+ string input_file = "error_input_sk.hex";
221
+ string output_file = "error_output_encoded_sk.hex";
222
+ integer file, ret;
223
+ int i;
224
+ string line;
225
+
226
+ // Read input file
227
+ file = $fopen(input_file, "r");
228
+ if (file == 0) $error("Cannot open %s for reading\n", input_file);
229
+ i = 0;
230
+ while (!$feof(file)) begin
231
+ if($fgets(line,file)) begin
232
+ ret = $sscanf(line, "%h", input_mem[i]);
233
+ i = i + 1;
234
+ end
235
+ end
236
+ $fclose(file);
237
+ overwrite_memory_content(src_base_addr);
238
+
239
+ // Read expected output file
240
+ file = $fopen(output_file, "r");
241
+ if (file == 0) $error("Cannot open %s for reading\n", output_file);
242
+ i = 0;
243
+ while (!$feof(file)) begin
244
+ if($fgets(line,file)) begin
245
+ ret = $sscanf(line, "%h", expected_output_mem[i]);
246
+ i = i + 1;
247
+ end
248
+ end
249
+ $fclose(file);
250
+ endtask
251
+
252
+ task skencode_test(input reg [MEM_ADDR_WIDTH-1:0] dest_base_addr, input reg [MEM_ADDR_WIDTH-1:0] src_base_addr);
253
+ int i, j;
254
+ $display("Starting skencode test\n");
255
+ read_test_vectors(src_base_addr);
256
+ @(posedge clk_tb);
257
+ skencode_enable_tb = 1;
258
+ src_base_addr_tb = src_base_addr;
259
+ dest_base_addr_tb = dest_base_addr;
260
+ @(posedge clk_tb);
261
+ skencode_enable_tb = 0;
262
+ src_base_addr_tb = 0;
263
+ dest_base_addr_tb = 0;
264
+
265
+ $display("Waiting for skencode to complete\n");
266
+ wait (skencode_done_tb);
267
+ read_memory_content(dest_base_addr);
268
+
269
+ $display("Checking output data\n");
270
+ for (i = 0; i < NUM_OF_COEFF; i = i + 1) begin
271
+ if (actual_output_mem[i] !== expected_output_mem[i]) begin
272
+ $display("Error: Output mismatch at index %0d. Expected: %h, Got: %h", i, expected_output_mem[i], actual_output_mem[i]);
273
+ end
274
+ end
275
+ $display("Test completed\n");
276
+ endtask
277
+
278
+
279
+ task skencode_error_test(input reg [MEM_ADDR_WIDTH-1:0] dest_base_addr, input reg [MEM_ADDR_WIDTH-1:0] src_base_addr);
280
+ int i, j;
281
+ $display("Starting skencode error test\n");
282
+ read_error_test_vectors(src_base_addr);
283
+ @(posedge clk_tb);
284
+ skencode_enable_tb = 1;
285
+ src_base_addr_tb = src_base_addr;
286
+ dest_base_addr_tb = dest_base_addr;
287
+ @(posedge clk_tb);
288
+ skencode_enable_tb = 0;
289
+ src_base_addr_tb = 0;
290
+ dest_base_addr_tb = 0;
291
+
292
+ $display("Waiting for skencode to complete\n");
293
+ wait (skencode_done_tb | skencode_error_tb);
294
+ if (skencode_done_tb) begin
295
+ $display("Error: Error flag should have been asserted...");
296
+ end
297
+ else begin
298
+ @(posedge clk_tb);
299
+ zeroize_tb = 1;
300
+ @(posedge clk_tb);
301
+ zeroize_tb = 0;
302
+ repeat(3) @(posedge clk_tb);
303
+ $display("Starting skencode test again\n");
304
+ read_test_vectors(src_base_addr);
305
+ @(posedge clk_tb);
306
+ skencode_enable_tb = 1;
307
+ src_base_addr_tb = src_base_addr;
308
+ dest_base_addr_tb = dest_base_addr;
309
+ @(posedge clk_tb);
310
+ skencode_enable_tb = 0;
311
+ src_base_addr_tb = 0;
312
+ dest_base_addr_tb = 0;
313
+
314
+ $display("Waiting for skencode to complete\n");
315
+ wait (skencode_done_tb);
316
+ read_memory_content(dest_base_addr);
317
+
318
+ $display("Checking output data\n");
319
+ for (i = 0; i < NUM_OF_COEFF; i = i + 1) begin
320
+ if (actual_output_mem[i] !== expected_output_mem[i]) begin
321
+ $display("Error: Output mismatch at index %0d. Expected: %h, Got: %h", i, expected_output_mem[i], actual_output_mem[i]);
322
+ end
323
+ end
324
+ $display("Test completed\n");
325
+ end
326
+ endtask
327
+
328
+ initial begin
329
+ $system($sformatf("python3 pack_eta.py"));
330
+ init_sim();
331
+ reset_dut();
332
+ $display("Reading test vectors from hex files\n");
333
+ skencode_test('h0, 'h0);
334
+ skencode_test('h40, 'h20);
335
+ skencode_error_test('h0, 'h0);
336
+ $finish;
337
+ end
338
+
339
+ endmodule
340
+
341
+ module dual_port_memory
342
+ #(
343
+ parameter ADDR_WIDTH = 15,
344
+ parameter DATA_WIDTH = 32,
345
+ parameter DEPTH = 256
346
+ )
347
+ (
348
+ input wire clk,
349
+
350
+ // Port A
351
+ input wire [ADDR_WIDTH-1:0] addr_a,
352
+ input wire [DATA_WIDTH-1:0] data_in_a,
353
+ input wire we_a, // Write enable for port A
354
+ output reg [DATA_WIDTH-1:0] data_out_a,
355
+
356
+ // Port B
357
+ input wire [ADDR_WIDTH-1:0] addr_b,
358
+ input wire [DATA_WIDTH-1:0] data_in_b,
359
+ input wire we_b, // Write enable for port B
360
+ output reg [DATA_WIDTH-1:0] data_out_b
361
+ );
362
+
363
+ // Memory array
364
+ reg [DATA_WIDTH-1:0] mem [0:DEPTH-1];
365
+
366
+ // Port A logic
367
+ always @(posedge clk) begin
368
+ if (we_a) begin
369
+ mem[addr_a] <= data_in_a;
370
+ end
371
+ data_out_a <= mem[addr_a];
372
+ end
373
+
374
+ // Port B logic
375
+ always @(posedge clk) begin
376
+ if (we_b) begin
377
+ mem[addr_b] <= data_in_b;
378
+ end
379
+ data_out_b <= mem[addr_b];
380
+ end
381
+
382
+ endmodule