Tcl Library Source Code

View Ticket
Login
Ticket UUID: 358a88716e32b452115b8556208fd2ee42be8496
Title: slight performance improvement in CBC mode
Type: Patch Version:
Submitter: anonymous Created on: 2014-09-18 13:13:29
Subsystem: aes Assigned To: aku
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2014-10-20 20:38:54
Resolution: Accepted Closed By: aku
    Closed on: 2014-09-23 00:19:51
Description:
This patch modifies aes' state to keep the IV as a list of integers instead of their binary representation.

This spares one [binary scan] per block (see ::aes::EncryptBlock and ::aes::DecryptBlock). The cost is an additional [binary scan] in ::aes::Init and ::aes::Reset, but since these functions are called much more rarely than [En|De]cryptBlock, it's worth doing: my tests have shown a gain of ~ 3% in speed.

Also, the last chunk fixes the comment block for ::aes::Decrypt.

[moved the inlined patch into a proper attachment]
User Comments: aku added on 2014-10-20 20:38:54:

Merged with revision [845ab17352], after bumping the version to 1.2.1. Done.


aku added on 2014-10-18 00:12:12:
Config for both variants:
- Tclsh 8.5.7
- Repeat 10 (= run 11 times (1+ 10 repeats))
- Collate min (take best time over all runs)

Gains about 1-3% it looks like.
Col 1 = aes/trunk
Col 2 = aes/modernize
Normalized to column 1.
To check against 8.5.16 and 8.6

+----+-----------------------------+------+------+
|    | INTERP                      |    1 |    2 |
+----+-----------------------------+------+------+
|  1 | AES-128 CBC keyschedule     | 1.00 | 1.00 |
|  2 | AES-128 ECB decryption      | 1.00 | 1.01 |
|  3 | AES-128 ECB decryption core | 1.00 | 1.00 |
|  4 | AES-128 ECB encryption      | 1.00 | 0.99 |
|  5 | AES-128 ECB encryption core | 1.00 | 0.97 |
|  6 | AES-128 ECB keyschedule     | 1.00 | 1.00 |
|  7 | AES-192 CBC keyschedule     | 1.00 | 1.01 |
|  8 | AES-192 ECB decryption      | 1.00 | 1.01 |
|  9 | AES-192 ECB decryption core | 1.00 | 1.00 |
| 10 | AES-192 ECB encryption      | 1.00 | 0.98 |
| 11 | AES-192 ECB encryption core | 1.00 | 0.98 |
| 12 | AES-192 ECB keyschedule     | 1.00 | 1.00 |
| 13 | AES-256 CBC keyschedule     | 1.00 | 1.00 |
| 14 | AES-256 ECB decryption      | 1.00 | 1.00 |
| 15 | AES-256 ECB decryption core | 1.00 | 1.01 |
| 16 | AES-256 ECB encryption      | 1.00 | 0.99 |
| 17 | AES-256 ECB encryption core | 1.00 | 0.98 |
| 18 | AES-256 ECB keyschedule     | 1.00 | 0.99 |
+----+-----------------------------+------+------+

aku added on 2014-10-17 22:45:19:

The aes-modernize patch uses 'lmap'. That is a Tcl 8.6 command, and not available for 8.5. Six places.

But we have lassign and can unroll the loops (4 iterations is small enough). Note, this makes use of our knowledge about the size of the list, i.e. that it always has 4 elements.

And that fixes the test breakage I saw. Can't say if that gives us perf gains or not.

Did you use the aes.bench to benchmark things ?

For the moment I put the patch + mods on branch "aes-modernize" so that I can benchmark it versus the original using aes.bench.

Commit [5903319520] Pushed.


aku added on 2014-10-17 22:18:36:
Reopening to handle the aes-modernize patch.
Would have been better in a new ticket.

aku added on 2014-09-23 00:19:51:
Patch applied, revision [4b3d8bca51].
Committed.
Pushed.

Version 1.2, requiring Tcl 8.5.

aku added on 2014-09-22 23:38:28:
Actually, given that "aes" is currently assuming Tcl 8.2 it is overdue to be moved to a modern revision of the core.

I will apply this patch, bump the package to version 1.2 and declare that to require Tcl 8.5.

At that point please feel free to add more 8.5-isms, if sensible ({*}, lassign, in/ni operators, whatever).

anonymous (claiming to be [email protected]) added on 2014-09-18 13:36:46:
I just realized that 8.4 doesn't have the "u" flag char. If compatibility with 8.4 is to be kept, please disregard this ticket.

Attachments: