Skip to content
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

Add support for the Zvkned extension #234

Open
wants to merge 18 commits into
base: vector-dev
Choose a base branch
from

Conversation

charmitro
Copy link
Contributor

@charmitro charmitro commented Mar 23, 2023

Implements the Zvkned (NIST Suite: Vector AES Block Cipher) extension, as of version Draft: 20230303
.

The following instructions are included:

  • vaesef.[vv,vs]
  • vaesem.[vv,vs]
  • vaesdf.[vv,vs]
  • vaesdm.[vv,vs]
  • vaeskf1.vi
  • vaeskf2.vi
  • vaesz.vs

All instructions were tested with VLEN=128 and results were compared with QEMU results of each instruction.

Current revision is rebased with the latest changes of vector-dev branch.

Note
It's better to merge all of Zvk extension support PRs in the created order, as this is going to create conflicts and it would be easier to resolve them.

XinlaiWan and others added 3 commits March 22, 2023 09:01
…iscv#191)

* V extension general framework and configuration setting instructions

* Update model/riscv_insts_vext_utils.sail

fix a typo

Co-authored-by: Nicolas Brunie <nibrunie@gmail.com>
Signed-off-by: BrighterW <xinlai.w@rioslab.org>

* Update model/riscv_insts_vext_vset.sail

* Revisions after Nov 22 meeting

* Update effect matching for functions in riscv_vlen.sail

* Fix code formatting issues

* Update model/riscv_insts_vext_utils.sail

Co-authored-by: Jessica Clarke <jrtc27@jrtc27.com>
Signed-off-by: Xinlai Wan <xinlai.w@rioslab.org>

* Fix coding style issues

* Update vset instructions

Signed-off-by: BrighterW <xinlai.w@rioslab.org>
Signed-off-by: Xinlai Wan <xinlai.w@rioslab.org>
Co-authored-by: Nicolas Brunie <nibrunie@gmail.com>
Co-authored-by: Jessica Clarke <jrtc27@jrtc27.com>
* Add vector load / store instructions

* Modify the implementation of SEW, LMUL, VLEN and avoid real numbers in the code

* Update vstart setting in vector load / store instructions

* Remove unnecessary assert statements in vector instructions

* Fix bugs in vleff instructions and revise coding styles

* Add guards for vector encdec clauses, Avoid redundant memory access after vector load/store failure
* Add vector arithmetic & mask instructions

* Update vector EEW and EMUL checking function

* Add vector instruction illegal check functions

* Adjust code formatting for vector instruction illegal check functions

Merge approved by team at tech-golden-model meeting on 2023-03-14.
@charmitro
Copy link
Contributor Author

charmitro commented Mar 24, 2023

One note, the latest revision of vector-dev branch fails to build, therefore this PR , #235 & #236 are failing to build also since they are synchronized.

@charmitro
Copy link
Contributor Author

charmitro commented Apr 18, 2023

Updated the PR:

  • Correction on iterating based on the specification
  • Correctly Load & Store from/to vector registers

This update is adjusting the implementation to match the Spike implenentation and as a follow-up, ACT Signatures matching.

* Add vector floating-point instructions

* Update vector floating-point conversion instructions

* Update copyright headers for vector extension code

---------

Co-authored-by: xwan <xinlai.wan@rivai.ai>
@charmitro
Copy link
Contributor Author

Rebased from vector-dev branch.

model/riscv_insts_zvkned.sail Outdated Show resolved Hide resolved
Comment on lines +12 to +14
function zvk_check_elements(VLEN, num_elem, LMUL, SEW) = {
((unsigned(vl)%num_elem) != 0) | ((unsigned(vstart)%num_elem) != 0) | (LMUL*VLEN) < (num_elem*SEW)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SAIL newbie question: the vector crypto spec distinguishes illegal cases ((LMUL*VLEN) < (num_elem*SEW)) and reserved cases (((unsigned(vl)%num_elem) != 0) | ((unsigned(vstart)%num_elem) != 0)) (which can legally raise an illegal exception or not). Does this difference is required in the sail model ?

model/riscv_insts_zvkned.sail Show resolved Hide resolved
@allenjbaum
Copy link
Collaborator

allenjbaum commented May 25, 2023 via email

Comment on lines +66 to +68
vd_state : bits(128) = undefined;
vs2_key : bits(128) = undefined;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to define both vd_state and vs2_key within the scope of the foreach since they are only used in the loop body and their live range does not exceed one iteration ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK., good catch!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are reserved encodings for the .vs form (for example when vs2 scalar element groups overlaps the vd register group), where would such constraint appear in the SAIL code ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm handling he .vs && .vv forms conditionally using the suffix of each instruction.

 if suffix == "vv" then { ... } else { ... }

But I might misunderstood this one.


val zvk_check_elements : (int, int, int, int) -> bool
function zvk_check_elements(VLEN, num_elem, LMUL, SEW) = {
((unsigned(vl)%num_elem) != 0) | ((unsigned(vstart)%num_elem) != 0) | (LMUL*VLEN) < (num_elem*SEW)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the last term looks suspicious, is it trying to implement EGW <= VLEN * LMUL / EGS <= VLMAX ?
If so I do not think this is correct, for example I don't understand how the EGS / EGW is injected. Could a comment be added to explain which checks this function is supposed to perform ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is implementing the following found in the specification, Version v0.4.2, 03 March, 2023.

if( ((vl%EGS)<>0) | ((vstart%EGS)<>0) | (LMUL*VLEN < EGW))  then {
      handle_illegal();  // illegal instruction exception
      RETIRE_SUCCESS
    } else {
    . . . 

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is EGW derived from num_elen * SEW ?

Copy link
Contributor Author

@charmitro charmitro May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the Vector Crypto Spec we have,

• Element Group Width (EGW) - total number of bits in an element group 
• Effective Element Width (EEW) - number of bits in each element
• Element Group Size (EGS) - number of elements in an element group
For all of the vector crypto instructions in this specification, EEW=SEW.

So here we're doing EGS * EEW, given the above:

  • num_elem = EGS
  • SEW = EEW

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but I am still missing something, it looks like num_elem comes from get_num_elem and I do not get how EGS appears here:

$ git grep -A8 "fun.*get_num_elem" *.sail
model/riscv_vext_regs.sail:function get_num_elem(LMUL_pow, SEW) = {
model/riscv_vext_regs.sail-  let VLEN = int_power(2, get_vlen_pow());
model/riscv_vext_regs.sail-  let LMUL_pow_reg = if LMUL_pow < 0 then 0 else LMUL_pow;
model/riscv_vext_regs.sail-  /* Ignore lmul < 1 so that the entire vreg is read, allowing all masking to
model/riscv_vext_regs.sail-   * be handled in init_masked_result */
model/riscv_vext_regs.sail-  let num_elem = int_power(2, LMUL_pow_reg) * VLEN / SEW;
model/riscv_vext_regs.sail-  assert(num_elem > 0);
model/riscv_vext_regs.sail-  num_elem
model/riscv_vext_regs.sail-}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll analyse this SAIL internal first and reach back. I'm positive get_num_elem returns the correct value, but a double-check is not a problem.

@github-actions
Copy link

github-actions bot commented May 30, 2023

Unit Test Results

712 tests  ±0   712 ✔️ ±0   0s ⏱️ ±0s
    6 suites ±0       0 💤 ±0 
    1 files   ±0       0 ±0 

Results for commit c4858f3. ± Comparison against base commit 5872908.

♻️ This comment has been updated with latest results.

@nibrunieAtSi5
Copy link

@charmitro , when I tried to build this branch, I get the following error:


Type error:
model/riscv_insts_vext_utils.sail:422.23-32:
422 |    16 => (xf[15..15], xf[14..0]),
    |                       ^-------^
    | Tried performing type coercion from bitvector(((14 - 0) + 1), dec) to bitvector(('n * 1), dec) on subrange_bits(xf, 14, 0)
    | Coercion failed because:
    | Failed to prove constraint: ((14 - 0) + 1) == ('n * 1)
make: *** [Makefile:315: generated_definitions/lem/RV64/riscv.lem] Error 1

Do you know if this is a known issue ?

@charmitro
Copy link
Contributor Author

@charmitro , when I tried to build this branch, I get the following error:


Type error:
model/riscv_insts_vext_utils.sail:422.23-32:
422 |    16 => (xf[15..15], xf[14..0]),
    |                       ^-------^
    | Tried performing type coercion from bitvector(((14 - 0) + 1), dec) to bitvector(('n * 1), dec) on subrange_bits(xf, 14, 0)
    | Coercion failed because:
    | Failed to prove constraint: ((14 - 0) + 1) == ('n * 1)
make: *** [Makefile:315: generated_definitions/lem/RV64/riscv.lem] Error 1

Do you know if this is a known issue ?

I can't seem to reproduce it on my side. Could it be a problem in the upstream vector-dev branch(I believe at some point it was force-pushed, try resetting it(?)), but on my forked vector-dev branch everything seems fine.

* Add vector mask and reduction instructions

* Fix register overlap check in vector mask instructions

---------

Co-authored-by: xwan <xinlai.wan@rivai.ai>
XinlaiWan and others added 9 commits July 10, 2023 20:12
To support the implementation of Zvkned extension in SAIL, this
creates the necessary infrastructure(i.e., a file to hold it, and the
extension macro), preparing the tree for the Zvkned implementation.

Signed-off-by: Charalampos Mitrodimas <charalampos.mitrodimas@vrull.eu>
All of the instructions that were introduced in the Zvk*
extensions, use a common logic that can be implemented in
functions in order to reuse them when needed and avoid
duplicate code.

Signed-off-by: Charalampos Mitrodimas <charalampos.mitrodimas@vrull.eu>
The "vaesef.vv|vs" instruction performs the final-round
encryption of the AES block cipher.

The "SubBytes" and "ShiftRows" steps are applied to each round state
element group from "vd". This is then XORed with the corresponding round
key element group in "vs2"(vector-vector form) or the scalar element group
in "vs2"(vector-scalar form).

Signed-off-by: Charalampos Mitrodimas <charalampos.mitrodimas@vrull.eu>
The "vaesem.vv|vs" instruction performs the middle-round
encryption of the AES block cipher.

The "SubBytes", "ShiftRows" and "MixColumns" steps are applied to each
round state element group from "vd". This is then XORed with the
corresponding round key element group in "vs2"(vector-vector form) or
the scalar element group in "vs2"(vector-scalar form).

Signed-off-by: Charalampos Mitrodimas <charalampos.mitrodimas@vrull.eu>
The "vaesdf.vv|vs" instruction performs a final-round
decryption of the AES block cipher.

The "InvShiftRows" and "InvSubBytes" steps are applied to each round state
element group from "vd". This is then XORed with the corresponding round
key element group in "vs2"(vector-vector form) or the scalar element group
in "vs2"(vector-scalar form).

Signed-off-by: Charalampos Mitrodimas <charalampos.mitrodimas@vrull.eu>
The "vaesdm.vv|vs" instruction performs the middle-round
decryption of the AES block cipher.

The "InvShiftRows" and "InvSubBytes" steps are applied to each round state
element group from "vd". This is then XORed with the corresponding round
key element group in "vs2"(vector-vector form) or the scalar element group
in "vs2"(vector-scalar form).

Signed-off-by: Charalampos Mitrodimas <charalampos.mitrodimas@vrull.eu>
The "vaeskf1.vi" instruction performs a single round
of the forward AES-128 KeySchedule generation.

The next round key is generated word by word from the current round
key element group in "vs2" and the immediately previous word of the
round key. The least significant word is generated using the most
significant word of the current round key as well as a round constant
which is selected by the round number.

The round number(ranges 1-10), comes from imm[3:0]; imm[4] is ignored.
We project out-of-range immediates onto in-range values by inverting
imm[3].

Signed-off-by: Charalampos Mitrodimas <charalampos.mitrodimas@vrull.eu>
Charalampos Mitrodimas added 4 commits July 11, 2023 09:15
The "vaeskf2.vi" instruction performs a single round
of the forward AES-256 KeySchedule generation.

The next round key is generated word by word from the previous round
key element group in "vd" and the immediately previous word of the round
key. The least significant word of the next round key is generated by
applying a function to the most significant word of the current round
key and then XORing the result with the round constant. The round
number is used to select the round constant as well as the function.

The round number(ranges 2-14), comes from imm[3:0]; imm[4] is ignored.
We project out-of-range immediates into in-range values by inverting
imm[3].

Signed-off-by: Charalampos Mitrodimas <charalampos.mitrodimas@vrull.eu>
The "vaesz.vs" instruction performs a round zero
encryption/decryption

This instruction is only available in ".vs" form.
The new round state output of each element group is produced
by XORing the round key "vs2" with each element group of "vd".

Signed-off-by: Charalampos Mitrodimas <charalampos.mitrodimas@vrull.eu>
Signed-off-by: Charalampos Mitrodimas <charalampos.mitrodimas@vrull.eu>
…decode_rcon

Signed-off-by: Charalampos Mitrodimas <charalampos.mitrodimas@vrull.eu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants