Skip to content

Add support for Zvksed extension #848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Supported RISC-V ISA features
- Zvbc extension for vector carryless multiplication, v1.0
- Zvkb extension for vector cryptography bit-manipulation, v1.0
- Zvknha and Zvknhb extensions for vector cryptography NIST Suite: Vector SHA-2 Secure Hash, v1.0
- Zvksed extension for vector cryptography ShangMi Suite: SM4 Block Cipher, v1.0
- Machine, Supervisor, and User modes
- Smcntrpmf extension for cycle and instret privilege mode filtering, v1.0
- Sscofpmf extension for Count Overflow and Mode-Based Filtering, v1.0
Expand Down
3 changes: 3 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@
"Zvknhb" : {
"supported" : true
},
"Zvksed" : {
"supported" : true
},
"Sscofpmf" : {
"supported" : true
},
Expand Down
1 change: 1 addition & 0 deletions model/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ foreach (xlen IN ITEMS 32 64)
"riscv_insts_zvbb.sail"
"riscv_insts_zvbc.sail"
"riscv_insts_zvknhab.sail"
"riscv_insts_zvksed.sail"
# Zimop and Zcmop should be at the end so they can be overridden by earlier extensions
"riscv_insts_zimop.sail"
"riscv_insts_zcmop.sail"
Expand Down
3 changes: 3 additions & 0 deletions model/riscv_extensions.sail
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ function clause hartSupports(Ext_Zvknha) = config extensions.Zvknha.supported

enum clause extension = Ext_Zvknhb
function clause hartSupports(Ext_Zvknhb) = config extensions.Zvknhb.supported
// ShangMi Suite: SM4 Block Cipher
enum clause extension = Ext_Zvksed
function clause hartSupports(Ext_Zvksed) = config extensions.Zvksed.supported

// Count Overflow and Mode-Based Filtering
enum clause extension = Ext_Sscofpmf
Expand Down
11 changes: 11 additions & 0 deletions model/riscv_insts_vext_utils.sail
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,17 @@ function write_velem_quad(vd, SEW, input, i) = {
write_single_element(SEW, 4 * i + j, vd, slice(input, j * SEW, SEW));
}

/* Extracts 4 consecutive vector elements starting from index 4*i and returns a vector */
val get_velem_quad_vec : forall 'n 'm 'p, 'n > 0 & 8 <= 'm <= 64 & 'p >= 0 & 4 * 'p + 3 < 'n. (vector('n, bits('m)), int('p)) -> vector(4, bits('m))
function get_velem_quad_vec(v, i) = [ v[4 * i + 3], v[4 * i + 2], v[4 * i + 1], v[4 * i] ]

/* Writes each of the 4 elements from the input vector to the vector register vd, starting at position 4 * i */
val write_velem_quad_vec : forall 'p 'n, 8 <= 'n <= 64 & 'p >= 0. (vregidx, int('n), vector(4, bits('n)), int('p)) -> unit
function write_velem_quad_vec(vd, SEW, input, i) = {
foreach(j from 0 to 3)
write_single_element(SEW, 4 * i + j, vd, input[j]);
}

/* Get the starting element index from csr vtype */
val get_start_element : unit -> result(nat, unit)
function get_start_element() = {
Expand Down
125 changes: 125 additions & 0 deletions model/riscv_insts_zvksed.sail
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*=======================================================================================*/
/* This Sail RISC-V architecture model, comprising all files and */
/* directories except where otherwise noted is subject the BSD */
/* two-clause license in the LICENSE file. */
/* */
/* SPDX-License-Identifier: BSD-2-Clause */
/*=======================================================================================*/

function clause currentlyEnabled(Ext_Zvksed) = hartSupports(Ext_Zvksed) & currentlyEnabled(Ext_V)

union clause ast = VSM4K_VI : (vregidx, bits(5), vregidx)

mapping clause encdec = VSM4K_VI(vs2, uimm, vd)
<-> 0b1000011 @ encdec_vreg(vs2) @ uimm @ 0b010 @ encdec_vreg(vd) @ 0b1110111
when currentlyEnabled(Ext_Zvksed) & get_sew() == 32 & zvk_check_encdec(128, 4)

function clause execute (VSM4K_VI(vs2, uimm, vd)) = {
let SEW = get_sew();
let LMUL_pow = get_lmul_pow();
let num_elem = get_num_elem(LMUL_pow, SEW);

assert(SEW == 32);

let vs2_val = read_vreg(num_elem, SEW, LMUL_pow, vs2);

let rnd = unsigned(uimm[2..0]);

let eg_len = (unsigned(vl) / 4);
let eg_start = (unsigned(vstart) / 4);

foreach (i from eg_start to (eg_len - 1)) {
assert(i * 4 + 3 < num_elem);

let rk_in : vector(4, bits(32)) = get_velem_quad_vec(vs2_val, i);
var rk_out : vector(4, bits(32)) = vector_init(zeros());

var B = rk_in[1] ^ rk_in[2] ^ rk_in[3] ^ zvk_sm4_sbox(4 * rnd);
var S = zvk_sm4_subword(B);
rk_out[0] = zvk_round_key(rk_in[0], S);

B = rk_in[2] ^ rk_in[3] ^ rk_out[0] ^ zvk_sm4_sbox(4 * rnd + 1);
S = zvk_sm4_subword(B);
rk_out[1] = zvk_round_key(rk_in[1], S);

B = rk_in[3] ^ rk_out[0] ^ rk_out[1] ^ zvk_sm4_sbox(4 * rnd + 2);
S = zvk_sm4_subword(B);
rk_out[2] = zvk_round_key(rk_in[2], S);

B = rk_out[0] ^ rk_out[1] ^ rk_out[2] ^ zvk_sm4_sbox(4 * rnd + 3);
S = zvk_sm4_subword(B);
rk_out[3] = zvk_round_key(rk_in[3], S);

write_velem_quad_vec(vd, SEW, rk_out, i);
};

set_vstart(zeros());
RETIRE_SUCCESS
}

mapping clause assembly = VSM4K_VI(vs2, uimm, vd)
<-> "vsm4k.vi" ^ spc() ^ vreg_name(vd) ^ sep() ^ vreg_name(vs2) ^ sep() ^ hex_bits_5(uimm)

union clause ast = ZVKSM4RTYPE : (zvkfunct6, vregidx, vregidx)

mapping clause encdec = ZVKSM4RTYPE(ZVK_VSM4RVV, vs2, vd)
<-> 0b1010001 @ encdec_vreg(vs2) @ 0b10000 @ 0b010 @ encdec_vreg(vd) @ 0b1110111
when currentlyEnabled(Ext_Zvksed) & get_sew() == 32 & zvk_check_encdec(128, 4)

mapping clause encdec = ZVKSM4RTYPE(ZVK_VSM4RVS, vs2, vd)
<-> 0b1010011 @ encdec_vreg(vs2) @ 0b10000 @ 0b010 @ encdec_vreg(vd) @ 0b1110111
when currentlyEnabled(Ext_Zvksed) & get_sew() == 32 & zvk_check_encdec(128, 4) & zvk_valid_reg_overlap(vs2, vd, get_lmul_pow())

function clause execute (ZVKSM4RTYPE(funct6, vs2, vd)) = {
let SEW = get_sew();
let LMUL_pow = get_lmul_pow();
let num_elem = get_num_elem(LMUL_pow, SEW);

assert(SEW == 32);

let vs2_val = read_vreg(num_elem, SEW, LMUL_pow, vs2);
let vd_val = read_vreg(num_elem, SEW, LMUL_pow, vd);

let eg_len = (unsigned(vl) / 4);
let eg_start = (unsigned(vstart) / 4);

foreach (i from eg_start to (eg_len - 1)) {
assert(i * 4 + 3 < num_elem);

let rk_in : vector(4, bits(32)) = if funct6 == ZVK_VSM4RVV
then get_velem_quad_vec(vs2_val, i)
else get_velem_quad_vec(vs2_val, 0);

let x_in : vector(4, bits(32)) = get_velem_quad_vec(vd_val, i);
var x_out : vector(4, bits(32)) = vector_init(zeros());

var B = x_in[1] ^ x_in[2] ^ x_in[3] ^ rk_in[0];
var S = zvk_sm4_subword(B);
x_out[0] = zvk_sm4_round(x_in[0], S);

B = x_in[2] ^ x_in[3] ^ x_out[0] ^ rk_in[1];
S = zvk_sm4_subword(B);
x_out[1] = zvk_sm4_round(x_in[1], S);

B = x_in[3] ^ x_out[0] ^ x_out[1] ^ rk_in[2];
S = zvk_sm4_subword(B);
x_out[2] = zvk_sm4_round(x_in[2], S);

B = x_out[0] ^ x_out[1] ^ x_out[2] ^ rk_in[3];
S = zvk_sm4_subword(B);
x_out[3] = zvk_sm4_round(x_in[3], S);

write_velem_quad_vec(vd, SEW, x_out, i);
};

set_vstart(zeros());
RETIRE_SUCCESS
}

mapping vsm4r_mnemonic : zvkfunct6 <-> string = {
ZVK_VSM4RVV <-> "vsm4r.vv",
ZVK_VSM4RVS <-> "vsm4r.vs",
}

mapping clause assembly = ZVKSM4RTYPE(funct6, vs2, vd)
<-> vsm4r_mnemonic(funct6) ^ spc() ^ vreg_name(vd) ^ sep() ^ vreg_name(vs2)
43 changes: 41 additions & 2 deletions model/riscv_zvk_utils.sail
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ function zvk_valid_reg_overlap(rs, rd, emul_pow) = {

function zvk_check_encdec(EGW: int, EGS: int) -> bool = (unsigned(vl) % EGS == 0) & (unsigned(vstart) % EGS == 0) & (2 ^ get_lmul_pow() * VLEN) >= EGW

enum zvkfunct6 = {ZVK_VSHA2CH, ZVK_VSHA2CL, ZVK_VSM4RVV, ZVK_VSM4RVS}

/*
* Utility functions for Zvknh[ab]
* ----------------------------------------------------------------------
*/

enum zvkfunct6 = {ZVK_VSHA2CH, ZVK_VSHA2CL}

function zvknhab_check_encdec(vs2: vregidx, vs1: vregidx, vd: vregidx) -> bool = {
let SEW = get_sew();
let LMUL_pow = get_lmul_pow();
Expand Down Expand Up @@ -66,3 +66,42 @@ function zvk_ch(x, y, z) = (x & y) ^ (~(x) & z)

val zvk_maj : forall 'n, 'n >= 0. (bits('n), bits('n), bits('n)) -> bits('n)
function zvk_maj(x, y, z) = (x & y) ^ (x & z) ^ (y & z)

/*
* Utility functions for Zvksed
* ----------------------------------------------------------------------
*/

val zvk_round_key : (bits(32), bits(32)) -> bits(32)
function zvk_round_key(X, S) = X ^ (S ^ (S <<< 13) ^ (S <<< 23))

val zvk_sm4_round : (bits(32), bits(32)) -> bits(32)
function zvk_sm4_round(X, S) = X ^ (S ^ (S <<< 2) ^ (S <<< 10) ^ (S <<< 18) ^ (S <<< 24))

// SM4 Constant Key (CK)
let zvksed_ck : vector(32, bits(32)) = [
0x00070E15, 0x1C232A31, 0x383F464D, 0x545B6269,
0x70777E85, 0x8C939AA1, 0xA8AFB6BD, 0xC4CBD2D9,
0xE0E7EEF5, 0xFC030A11, 0x181F262D, 0x343B4249,
0x50575E65, 0x6C737A81, 0x888F969D, 0xA4ABB2B9,
0xC0C7CED5, 0xDCE3EAF1, 0xF8FF060D, 0x141B2229,
0x30373E45, 0x4C535A61, 0x686F767D, 0x848B9299,
0xA0A7AEB5, 0xBCC3CAD1, 0xD8DFE6ED, 0xF4FB0209,
0x10171E25, 0x2C333A41, 0x484F565D, 0x646B7279
]

val zvksed_box_lookup : (bits(5), vector(32, bits(32))) -> bits(32)
function zvksed_box_lookup(x, table) = {
table[31 - unsigned(x)]
}

val zvk_sm4_sbox : (int) -> bits(32)
function zvk_sm4_sbox(x) = zvksed_box_lookup(to_bits(5, x), zvksed_ck)

val zvk_sm4_subword : bits(32) -> bits(32)
function zvk_sm4_subword(x) = {
sm4_sbox(x[31..24]) @
sm4_sbox(x[23..16]) @
sm4_sbox(x[15.. 8]) @
sm4_sbox(x[ 7.. 0])
}
Loading