Adding control instructions for bls12-381 coprocessor

This commit is contained in:
bsdevlin 2019-08-21 14:09:25 +08:00
parent d437d3a165
commit 0bee1e1cd3
2 changed files with 95 additions and 9 deletions

View File

@ -155,6 +155,9 @@ package bls12_381_pkg;
typedef enum logic [7:0] {
NOOP_WAIT = 8'h0,
COPY_REG = 8'h1,
JUMP = 8'h2,
JUMP_IF_EQ = 8'h4,
JUMP_NONZERO_SUB= 8'h5,
SEND_INTERRUPT = 8'h6,
SUB_ELEMENT = 8'h10,
@ -618,7 +621,7 @@ package bls12_381_pkg;
R.x = Q.x;
R.y = Q.y;
R.z = 1;
for (int i = ATE_X_START-1; i >= 0; i--) begin
f_sq = fe12_sqr(f); // Full multiplication
miller_double_step(R, P, lv_d);
@ -630,7 +633,7 @@ package bls12_381_pkg;
end
endtask
// This uses the miller loop functions to do a point multiplication
task miller_loop_point_mult(input fp2_af_point_t Q, input fe_t k, output fp2_jb_point_t R);
fe12_t f;
@ -642,7 +645,7 @@ package bls12_381_pkg;
R.x = FE2_zero;
R.y = FE2_zero;
R.z = FE2_one;
for (int i = $bits(fe_t)-1; i >= 0; i--) begin
if (~found_one) begin
found_one |= k[i];
@ -824,7 +827,7 @@ package bls12_381_pkg;
function fe12_t fe12_fmap(input fe12_t a, input int pow);
fe6_t t0, t1;
t0 = a[0];
t1 = a[1]; // 0.
t1 = a[1]; // 0.
t0 = fe6_fmap(t0, pow); // 1. [0]
t1 = fe6_fmap(t1, pow); // 2. [0]
t1[0] = fe2_mul(t1[0], FROBENIUS_COEFF_FQ12_C1[pow % 12]); // 3. [2]
@ -937,11 +940,11 @@ package bls12_381_pkg;
$display("y:(c1:0x%h, c0:0x%h)", p.y[1], p.y[0]);
$display("z:(c1:0x%h, c0:0x%h)", p.z[1], p.z[0]);
endtask
task print_af_point(af_point_t p);
$display("x:(0x%h)", p.x);
$display("y:(0x%h)", p.y);
endtask
endtask
task print_fp2_af_point(fp2_af_point_t p);
$display("x:(c1:0x%h, c0:0x%h)", p.x[1], p.x[0]);

View File

@ -154,7 +154,7 @@ always_ff @ (posedge i_clk) begin
pair_i_val <= 0;
pair_i_g1 <= 0;
pair_i_g2 <= 0;
pair_mode <= 0;
pair_key <= 0;
mult_pt_if.rdy <= 0;
@ -199,6 +199,18 @@ always_ff @ (posedge i_clk) begin
// Wait in this state
get_next_inst();
end
JUMP: begin
last_inst_cnt <= last_inst_cnt;
task_jump();
end
JUMP_IF_EQ: begin
last_inst_cnt <= last_inst_cnt;
task_jump_if_eq();
end
JUMP_NONZERO_SUB: begin
last_inst_cnt <= last_inst_cnt;
task_jump_nonzero_sub();
end
COPY_REG: begin
last_inst_cnt <= last_inst_cnt;
task_copy_reg();
@ -302,7 +314,7 @@ bls12_381_pairing_wrapper (
.i_mode ( pair_mode ),
.i_key ( pair_key ),
.o_fe12_if ( pair_o_res_if ),
.o_p_jb_if ( mult_pt_if ),
.o_p_jb_if ( mult_pt_if ),
.o_mul_fe_if ( mul_in_if[0] ),
.i_mul_fe_if ( mul_out_if[0] ),
.o_inv_fe2_if ( inv_fe2_i_if ),
@ -640,6 +652,77 @@ task task_copy_reg();
endcase
endtask
task task_jump();
case(cnt)
0: begin
inst_ram_sys_if.a <= curr_inst.a;
inst_ram_read[0] <= 1;
cnt <= cnt + 1;
end
1: begin
get_next_inst();
end
endcase
endtask
task task_jump_if_eq();
case(cnt)
0: begin
data_ram_sys_if.a <= curr_inst.b;
data_ram_read[0] <= 1;
cnt <= cnt + 1;
end
1: begin
if (data_ram_read[READ_CYCLE]) begin
data_ram_sys_if.a <= curr_inst.c;
new_data <= curr_data;
data_ram_read[0] <= 1;
cnt <= cnt + 1;
end
end
2: begin
if (data_ram_read[READ_CYCLE]) begin
if (new_data.dat[63:0] == curr_data.dat[63:0])
inst_ram_sys_if.a <= curr_inst.a;
else
inst_ram_sys_if.a <= inst_ram_sys_if.a + 1;
inst_ram_read[0] <= 1;
cnt <= cnt + 1;
end
end
3: begin
get_next_inst();
end
endcase
endtask
task task_jump_nonzero_sub();
case(cnt)
0: begin
data_ram_sys_if.a <= curr_inst.b;
data_ram_read[0] <= 1;
cnt <= cnt + 1;
end
1: begin
if (data_ram_read[READ_CYCLE]) begin
if (curr_data.dat[63:0] != 0) begin
inst_ram_sys_if.a <= curr_inst.a;
new_data.pt <= curr_data.pt;
new_data.dat[63:0] <= curr_data.dat[63:0] - 1;
data_ram_sys_if.we <= 1;
end else begin
inst_ram_sys_if.a <= inst_ram_sys_if.a + 1;
end
inst_ram_read[0] <= 1;
cnt <= cnt + 1;
end
end
2: begin
get_next_inst();
end
endcase
endtask
task task_inv_element();
case(cnt)
0: begin
@ -762,7 +845,7 @@ task task_point_mult();
pair_mode <= 0;
get_next_inst();
end
endcase
endcase
endtask
task task_fp_fpoint_mult();