Skip to content

Commit

Permalink
crypto.sha1, crypto.sha256, crypto.sha3, crypto.sha512: improve perfo…
Browse files Browse the repository at this point in the history
…rmance for non prod builds, by tagging the block_generic functions with `@[direct_array_access]`
  • Loading branch information
spytheman committed Sep 11, 2024
1 parent 3ee2732 commit 6e13b02
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions vlib/crypto/sha1/sha1block_generic.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const _k1 = 0x6ED9EBA1
const _k2 = u32(0x8F1BBCDC)
const _k3 = u32(0xCA62C1D6)

@[direct_array_access]
fn block_generic(mut dig Digest, p_ []u8) {
unsafe {
mut p := p_
Expand Down
1 change: 1 addition & 0 deletions vlib/crypto/sha256/sha256block_generic.v
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const _k = [
0xc67178f2,
]

@[direct_array_access]
fn block_generic(mut dig Digest, p_ []u8) {
unsafe {
mut p := p_
Expand Down
13 changes: 8 additions & 5 deletions vlib/crypto/sha3/sha3_state_generic.v
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mut:
// 5 x 5 array.
//
// The state is not modified by this method.
@[direct_array_access]
fn (s State) to_bytes() []u8 {
mut byte_array := []u8{len: 200, cap: 200}
mut index := 0
Expand All @@ -54,6 +55,7 @@ fn (s State) to_bytes() []u8 {
// that the input byte array is 200 bytes long.
//
// All 25 u64 values are set from the input byte array.
@[direct_array_access]
fn (mut s State) from_bytes(byte_array []u8) {
mut index := 0

Expand All @@ -79,6 +81,7 @@ fn (mut s State) from_bytes(byte_array []u8) {
//
// The rate must be less than the size of the state
// in bytes.
@[direct_array_access]
fn (mut s State) xor_bytes(byte_array []u8, rate int) {
mut index := 0

Expand Down Expand Up @@ -158,7 +161,7 @@ fn (mut s State) rnd(round_index int) {
// on all the bite in the lane with a single 64-bit operation. And, since
// we represent a lane as a u64 value, we can reduce the state to a 2
// dimensional array of u64 values.
@[inline]
@[direct_array_access; inline]
fn (mut s State) theta() {
// calculate the 5 intermediate C values
mut c := [5]Lane{init: 0}
Expand Down Expand Up @@ -208,7 +211,7 @@ const rho_offsets = [[int(0), 36, 3, 41, 18], [int(1), 44, 10, 45, 2],
// The effect of step 3a is to rotate a 64-bit lane by the amount calculated by
// (((t + 1) * (t + 2)) / 2) % 64. In order to save time, these rotation values,
// called offsets, can be calculated ahead of time.
@[inline]
@[direct_array_access; inline]
fn (mut s State) rho() {
for x in 0 .. 5 {
for y in 0 .. 5 {
Expand All @@ -226,7 +229,7 @@ fn (mut s State) rho() {
//
// For this function, we will need to have a temporary version of the state for
// holding the rearranged lanes.
@[inline]
@[direct_array_access; inline]
fn (mut s State) pi() {
mut a_prime := [5][5]Lane{}
for x in 0 .. 5 {
Expand Down Expand Up @@ -255,7 +258,7 @@ fn (mut s State) pi() {
//
// For this function, we will need to have a temporary version of the state for
// holding the changed lanes.
@[inline]
@[direct_array_access; inline]
fn (mut s State) chi() {
mut a_prime := [5][5]Lane{}
for x in 0 .. 5 {
Expand Down Expand Up @@ -296,7 +299,7 @@ const iota_round_constants = [u64(0x0000000000000001), 0x0000000000008082, 0x800
// precomputed values are indexed by the round which is being applied. For sha3,
// the number of rounds is 24 so we just need to precompute the 24 valuse needed
// to xor with lane 0, 0.
@[inline]
@[direct_array_access; inline]
fn (mut s State) iota(round_index int) {
s.a[0][0] ^= iota_round_constants[round_index]
}
Expand Down
1 change: 1 addition & 0 deletions vlib/crypto/sha512/sha512block_generic.v
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const _k = [
]
// vfmt on

@[direct_array_access]
fn block_generic(mut dig Digest, p_ []u8) {
unsafe {
mut p := p_
Expand Down

0 comments on commit 6e13b02

Please sign in to comment.