Skip to content

Commit 7f4b759

Browse files
author
Jakub Jaksik
committed
feat: init - modular and elliptic curve arithmetic operations
0 parents  commit 7f4b759

File tree

460 files changed

+128347
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

460 files changed

+128347
-0
lines changed

HDL/eddsa/eddsa.sv

Whitespace-only changes.

HDL/parameters_pkg.sv

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package parameters_pkg;
2+
parameter int DATA_WIDTH = 448;
3+
// p = 2^448 - 2^224 - 1
4+
parameter logic [DATA_WIDTH-1:0] MODULUS = 448'hFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
5+
parameter logic [DATA_WIDTH-1:0] MODULUS_INV = 448'hFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000000000000000000000000001;
6+
// R = 2^448
7+
parameter logic [DATA_WIDTH:0] R = 449'h10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
8+
parameter logic [DATA_WIDTH-1:0] R_MOD_P = 448'h100000000000000000000000000000000000000000000000000000001;
9+
parameter logic [DATA_WIDTH-1:0] R_INV = 448'hFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE00000000000000000000000000000000000000000000000000000001;
10+
parameter logic [DATA_WIDTH-1:0] R2_MOD_P = 448'h0000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000002;
11+
12+
// d = -39081
13+
// D = d*R Montgomery representation
14+
parameter logic [DATA_WIDTH-1:0] D = 448'hFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF6755FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF6756;
15+
endpackage
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Module: add
3+
4+
Description:
5+
This module performs staged addition of two large inputs (a and b) with a bit width specified by
6+
the parameter SIZE. The width of SIZE must be divisible by 2. The module is designed
7+
with a typical input size of 448 bits in mind.
8+
9+
Parameters:
10+
- SIZE: Bit width of the inputs, must be divisible by 2.
11+
12+
Inputs:
13+
- clk : Clock signal.
14+
- rst : Synchronous reset signal.
15+
- start : Initiates the addition process.
16+
- a, b : Input operands with width SIZE.
17+
18+
Outputs:
19+
- result : Result of a + b, with a width of SIZE + 1 to accommodate any carry.
20+
- done : Asserted high when the addition is complete.
21+
22+
Operation:
23+
- On `start`, the module begins adding `a` and `b` in 2 stages, each handling half of the bits.
24+
The FSM controls the addition process across IDLE and STAGE1 states, with the
25+
final result in `result` once `done` is asserted.
26+
*/
27+
28+
module add_2_parts #(
29+
parameter int SIZE = 224 // Default bit width, must be divisible by 2
30+
)(
31+
input logic clk,
32+
input logic rst,
33+
input logic start,
34+
input [SIZE-1:0] a,
35+
input [SIZE-1:0] b,
36+
output logic [SIZE:0] result,
37+
output logic done
38+
);
39+
typedef enum logic [1:0] {IDLE, STAGE1} state_t;
40+
state_t state = IDLE;
41+
42+
logic [SIZE/2-1:0] partial_sum1;
43+
logic carry;
44+
45+
always_ff@(posedge clk or posedge rst) begin
46+
if (rst) begin
47+
state <= IDLE;
48+
done <= 1;
49+
result <= 0;
50+
partial_sum1 <= 0;
51+
carry <= 0;
52+
end else begin
53+
case (state)
54+
IDLE: begin
55+
if (start) begin
56+
{carry, partial_sum1} <= a[SIZE/2-1:0] + b[SIZE/2-1:0];
57+
state <= STAGE1;
58+
done <= 0;
59+
end
60+
end
61+
STAGE1: begin
62+
result[SIZE:SIZE/2] <= a[SIZE-1:SIZE/2] + b[SIZE-1:SIZE/2] + carry;
63+
result[SIZE/2-1:0] <= partial_sum1;
64+
state <= IDLE;
65+
done <= 1;
66+
end
67+
endcase
68+
end
69+
end
70+
71+
endmodule
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Module: add
3+
4+
Description:
5+
This module performs staged addition of two large inputs (a and b) with a bit width specified by
6+
the parameter SIZE. The width of SIZE must be divisible by 4. The module is designed
7+
with a typical input size of 448 bits in mind.
8+
9+
Parameters:
10+
- SIZE: Bit width of the inputs, must be divisible by 4.
11+
12+
Inputs:
13+
- clk : Clock signal.
14+
- rst : Synchronous reset signal.
15+
- start : Initiates the addition process.
16+
- a, b : Input operands with width SIZE.
17+
18+
Outputs:
19+
- result : Result of a + b, with a width of SIZE + 1 to accommodate any carry.
20+
- done : Asserted high when the addition is complete.
21+
22+
Operation:
23+
- On `start`, the module begins adding `a` and `b` in 4 stages, each handling a quarter of the bits.
24+
The FSM controls the addition process across IDLE, STAGE1, STAGE2, and STAGE3 states, with the
25+
final result in `result` once `done` is asserted.
26+
*/
27+
28+
module add_4_parts #(
29+
parameter int SIZE = 896 // Default bit width, must be divisible by 4
30+
)(
31+
input logic clk,
32+
input logic rst,
33+
input logic start,
34+
input [SIZE-1:0] a,
35+
input [SIZE-1:0] b,
36+
output logic [SIZE:0] result,
37+
output logic done
38+
);
39+
typedef enum logic [1:0] {IDLE, STAGE1, STAGE2, STAGE3} state_t;
40+
state_t state = IDLE;
41+
42+
logic [SIZE/4-1:0] partial_sum1, partial_sum2, partial_sum3;
43+
logic carry;
44+
45+
always_ff@(posedge clk or posedge rst) begin
46+
if (rst) begin
47+
state <= IDLE;
48+
done <= 1;
49+
result <= 0;
50+
partial_sum1 <= 0;
51+
partial_sum2 <= 0;
52+
partial_sum3 <= 0;
53+
carry <= 0;
54+
end else begin
55+
case (state)
56+
IDLE: begin
57+
if (start) begin
58+
{carry, partial_sum1} <= a[SIZE/4-1:0] + b[SIZE/4-1:0];
59+
state <= STAGE1;
60+
done <= 0;
61+
end
62+
end
63+
STAGE1: begin
64+
{carry, partial_sum2} <= a[2*SIZE/4-1:SIZE/4] + b[2*SIZE/4-1:SIZE/4] + carry;
65+
state <= STAGE2;
66+
end
67+
STAGE2: begin
68+
{carry, partial_sum3} <= a[3*SIZE/4-1:2*SIZE/4] + b[3*SIZE/4-1:2*SIZE/4] + carry;
69+
state <= STAGE3;
70+
end
71+
STAGE3: begin
72+
result[SIZE:3*SIZE/4] <= a[4*SIZE/4-1:3*SIZE/4] + b[4*SIZE/4-1:3*SIZE/4] + carry;
73+
result[3*SIZE/4-1:0] <= {partial_sum3, partial_sum2, partial_sum1};
74+
state <= IDLE;
75+
done <= 1;
76+
end
77+
endcase
78+
end
79+
end
80+
81+
endmodule
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Module: add
3+
4+
Description:
5+
This module performs staged addition of two large inputs (a and b) with a bit width specified by
6+
the parameter SIZE. The width of SIZE must be divisible by 8. The module is designed
7+
with a typical input size of 448 bits in mind.
8+
9+
Parameters:
10+
- SIZE: Bit width of the inputs, must be divisible by 8.
11+
12+
Inputs:
13+
- clk : Clock signal.
14+
- rst : Synchronous reset signal.
15+
- start : Initiates the addition process.
16+
- a, b : Input operands with width SIZE.
17+
18+
Outputs:
19+
- result : Result of a + b, with a width of SIZE + 1 to accommodate any carry.
20+
- done : Asserted high when the addition is complete.
21+
22+
Operation:
23+
- On `start`, the module begins adding `a` and `b` in 8 stages, each handling an eighth of the bits.
24+
The FSM controls the addition process across IDLE, STAGE1 to STAGE7 states, with the
25+
final result in `result` once `done` is asserted.
26+
*/
27+
28+
module add_8_parts #(
29+
parameter int SIZE = 896 // Default bit width, must be divisible by 8
30+
)(
31+
input logic clk,
32+
input logic rst,
33+
input logic start,
34+
input [SIZE-1:0] a,
35+
input [SIZE-1:0] b,
36+
output logic [SIZE:0] result,
37+
output logic done
38+
);
39+
typedef enum logic [2:0] {IDLE, STAGE1, STAGE2, STAGE3, STAGE4, STAGE5, STAGE6, STAGE7} state_t;
40+
state_t state = IDLE;
41+
42+
logic [SIZE/8-1:0] partial_sum1, partial_sum2, partial_sum3, partial_sum4, partial_sum5, partial_sum6, partial_sum7;
43+
logic carry;
44+
45+
always_ff@(posedge clk or posedge rst) begin
46+
if (rst) begin
47+
state <= IDLE;
48+
done <= 1;
49+
result <= 0;
50+
partial_sum1 <= 0;
51+
partial_sum2 <= 0;
52+
partial_sum3 <= 0;
53+
partial_sum4 <= 0;
54+
partial_sum5 <= 0;
55+
partial_sum6 <= 0;
56+
partial_sum7 <= 0;
57+
carry <= 0;
58+
end else begin
59+
case (state)
60+
IDLE: begin
61+
if (start) begin
62+
{carry, partial_sum1} <= a[SIZE/8-1:0] + b[SIZE/8-1:0];
63+
state <= STAGE1;
64+
done <= 0;
65+
end
66+
end
67+
STAGE1: begin
68+
{carry, partial_sum2} <= a[2*SIZE/8-1:SIZE/8] + b[2*SIZE/8-1:SIZE/8] + carry;
69+
state <= STAGE2;
70+
end
71+
STAGE2: begin
72+
{carry, partial_sum3} <= a[3*SIZE/8-1:2*SIZE/8] + b[3*SIZE/8-1:2*SIZE/8] + carry;
73+
state <= STAGE3;
74+
end
75+
STAGE3: begin
76+
{carry, partial_sum4} <= a[4*SIZE/8-1:3*SIZE/8] + b[4*SIZE/8-1:3*SIZE/8] + carry;
77+
state <= STAGE4;
78+
end
79+
STAGE4: begin
80+
{carry, partial_sum5} <= a[5*SIZE/8-1:4*SIZE/8] + b[5*SIZE/8-1:4*SIZE/8] + carry;
81+
state <= STAGE5;
82+
end
83+
STAGE5: begin
84+
{carry, partial_sum6} <= a[6*SIZE/8-1:5*SIZE/8] + b[6*SIZE/8-1:5*SIZE/8] + carry;
85+
state <= STAGE6;
86+
end
87+
STAGE6: begin
88+
{carry, partial_sum7} <= a[7*SIZE/8-1:6*SIZE/8] + b[7*SIZE/8-1:6*SIZE/8] + carry;
89+
state <= STAGE7;
90+
end
91+
STAGE7: begin
92+
result[SIZE:7*SIZE/8] <= a[8*SIZE/8-1:7*SIZE/8] + b[8*SIZE/8-1:7*SIZE/8] + carry;
93+
result[7*SIZE/8-1:0] <= {partial_sum7, partial_sum6, partial_sum5, partial_sum4, partial_sum3, partial_sum2, partial_sum1};
94+
state <= IDLE;
95+
done <= 1;
96+
end
97+
endcase
98+
end
99+
end
100+
101+
endmodule

HDL/primitives/arithmetic/mul.sv

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
module mul #(
2+
parameter int SIZE = 448
3+
)(
4+
input logic clk,
5+
input logic rst,
6+
input logic start,
7+
input [SIZE-1:0] a,
8+
input [SIZE-1:0] b,
9+
output logic [2*SIZE-1:0] result,
10+
output logic done
11+
);
12+
typedef enum logic [1:0] {IDLE, STAGE1, STAGE2, FINISH} state_t;
13+
state_t state = IDLE;
14+
15+
logic add_224_start, add_448_start, add_896_start;
16+
logic add_224_done, add_448_done, add_896_done;
17+
logic [SIZE/2-1:0] add_224_a, add_224_b,
18+
logic [SIZE/2:0] add_224_result;
19+
logic [SIZE-1:0] add_448_a, add_448_b,
20+
logic [SIZE:0] add_448_result;
21+
logic [2*SIZE-1:0] add_896_a, add_896_b,
22+
logic [2*SIZE:0] add_896_result;
23+
24+
logic [2*SIZE-1:0] accumulated_result,
25+
26+
add_2_parts #(SIZE/2) adder_224 (
27+
.clk(clk),
28+
.rst(rst),
29+
.start(add_224_start),
30+
.a(add_224_a),
31+
.b(add_224_b),
32+
.result(add_224_result),
33+
.done(add_224_done)
34+
);
35+
36+
add_4_parts #(SIZE) adder_448 (
37+
.clk(clk),
38+
.rst(rst),
39+
.start(add_448_start),
40+
.a(add_448_a),
41+
.b(add_448_b),
42+
.result(add_448_result),
43+
.done(add_448_done)
44+
);
45+
46+
add_8_parts #(2*SIZE) adder_896 (
47+
.clk(clk),
48+
.rst(rst),
49+
.start(add_896_start),
50+
.a(add_896_a),
51+
.b(add_896_b),
52+
.result(add_896_result),
53+
.done(add_896_done)
54+
);
55+
56+
always_ff @(posedge clk or posedge rst) begin
57+
if (rst) begin
58+
state <= IDLE;
59+
done <= 0;
60+
accumulated_result <= 0;
61+
add_224_start <= 0;
62+
add_448_start <= 0;
63+
add_896_start <= 0;
64+
end else begin
65+
case (state)
66+
IDLE: begin
67+
if (start) begin
68+
state <= STAGE1;
69+
done <= 0;
70+
end
71+
end
72+
STAGE1: begin
73+
74+
end
75+
STAGE2: begin
76+
77+
end
78+
FINISH: begin
79+
80+
end
81+
endcase
82+
end
83+
end
84+
endmodule
85+

0 commit comments

Comments
 (0)