39 Commits
Author SHA1 Message Date
xtaci ad2550932b crypt: optimize encryption routines with bitwise operations and lock improvements
- Replace division and modulo operations with bitwise equivalents:
  - len(src) / 8  -> len(src) >> 3
  - len(src) / 16 -> len(src) >> 4
  - n / 8  -> n >> 3
  - n % 8  -> n & 7

- Improve lock handling in blockCrypt:
  - Split single mutex into separate encMu/decMu for encrypt/decrypt
  - Remove defer for Lock/Unlock to reduce overhead
  - Cache blockSize in struct to avoid repeated method calls

- Optimize Salsa20 and None ciphers:
  - Skip unnecessary copy when dst and src point to same memory
  - Add //go:nosplit directive to hot path functions

All encryption tests pass. Benchmark results show no regression with
zero memory allocations maintained (0 B/op, 0 allocs/op).
2026-01-25 22:10:17 +08:00
xtaci 8cdb131af5 Squashed commit of the following:
commit e51740f7e6
Author: xtaci <imap@live.com>
Date:   Sat Jan 24 22:11:58 2026 +0800

    optimize crypto using subtle/XORBytes

commit 071dc4a69d
Author: xtaci <imap@live.com>
Date:   Sat Jan 24 21:55:49 2026 +0800

    1. Fix incorrect RepeatSegs counter in parse_data() function
       - Previously, the repeat flag was never set to true when duplicate packets
         were detected, causing incorrect statistics reporting
       - Added missing else branch to properly mark duplicate segments
       - Impact: Correct tracking of repeated packet statistics

    2. Fix memory leak in FEC decoder auto-tune functionality
       - When FEC parameters were adjusted dynamically, the old shardSet map
         was discarded without recycling its buffer pool entries
       - Added proper cleanup loop to return old shard packets to buffer pool
         before creating new shardSet
       - Impact: Prevents memory growth during long-lived connections with
         dynamic FEC parameter adjustments

    Files modified:
    - kcp.go: Added else branch in parse_data() to set repeat=true for duplicates
    - fec.go: Added buffer cleanup loop before shardSet recreation in decode()

    All existing tests pass, including FEC encoding/decoding and lossy connection tests.
2026-01-24 22:13:21 +08:00
xtaci f53a3373f8 optimize: modernize -fix -test . 2025-12-22 13:10:47 +08:00
xtaci 14d9473c32 optimize alloc check 2025-12-11 17:02:43 +08:00
xtaci 371609fe77 simplify alloc check 2025-12-11 16:50:09 +08:00
xtaci 7c0354dbf2 update panic message 2025-12-11 16:38:19 +08:00
xtaci 4f88cab989 make sure new slices are not created in Open&Seal 2025-12-11 16:30:59 +08:00
xtaci c73f280f8e rename aesgcm->aead 2025-12-10 22:09:26 +08:00
xtaci d255fb80d8 update NewAEADCrypt 2025-12-10 22:01:46 +08:00
xtaci c73962fe8f adjust AEAD function initialization 2025-12-10 20:41:36 +08:00
Coia Prant 752f25858e feat: support AEAD 2025-12-10 16:49:39 +08:00
Coia Prant 4af97a6395 chores: replace xorBytes to subtle (#321) 2025-12-10 08:54:07 +08:00
Coia Prant 26d9746211 fix: reuse BlockCrypt (#315) 2025-12-09 15:48:35 +08:00
Coia Prant a0cc9d98d6 refactor: merge same crypt struct (#314) 2025-12-09 15:26:42 +08:00
Meng Zhuo 5ba8221e58 chore: drop xorsimd dependency (#294) 2025-07-30 13:38:14 +08:00
xtaci 3040333ea6 adjust comments 2024-07-27 21:54:02 +08:00
xtaci 3653cc9fea add license to source codes 2024-07-25 21:35:37 +08:00
xtaci 3bedbae3ba remove variant cipher block size, cause variant blocksize can cap to
8/16 bytes
2020-02-09 14:46:43 +00:00
xtaci e0b4e0b2dc Squashed commit of the following:
commit 2553f5d81a269170f04fb518ba7a4bfd62bb0944
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Jan 3 19:49:16 2020 +0800

    update xorsimd

commit 0060649b4e5694d2e89c3572f4154d6307a40a27
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Jan 3 19:43:28 2020 +0800

    upgrade xorsimd
2020-01-03 19:50:23 +08:00
xtaci 99d42c4a84 use primitive methods for < 8 bytes 2020-01-03 16:01:03 +08:00
xtaci c6331af2fa Squashed commit of the following:
commit 68a8b437de6c6f2c93e868d5e4dda7712807b5cf
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Jan 3 15:48:21 2020 +0800

    add comments

commit 37e06370a13afacd7af5a28b97a0160d80d18579
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Jan 3 15:20:48 2020 +0800

    add comments

commit 7060ce363f93896e01cc2c3af2b79cbd55a5c7e1
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Jan 3 14:32:22 2020 +0800

    update
2020-01-03 15:48:50 +08:00
Temple3x 56f3bfc617 Update Lib: xor -> xorsimd, slight perf improvement (#158)
➜  kcp-go git:(refine/xor2) ✗ benchcmp old.txt new.txt
benchmark                        old ns/op     new ns/op     delta
BenchmarkAES128-8                2777          2581          -7.06%
BenchmarkAES192-8                3067          2874          -6.29%
BenchmarkAES256-8                3300          3201          -3.00%
BenchmarkCsprngNonceAES128-8     15.5          15.3          -1.29%

benchmark                        old MB/s     new MB/s     speedup
BenchmarkAES128-8                1080.19      1162.22      1.08x
BenchmarkAES192-8                978.28       1043.86      1.07x
BenchmarkAES256-8                909.06       937.27       1.03x
BenchmarkCsprngNonceAES128-8     1033.62      1046.87      1.01x

benchmark             old allocs     new allocs     delta
BenchmarkAES128-8     0              0              +0.00%
BenchmarkAES192-8     0              0              +0.00%
BenchmarkAES256-8     0              0              +0.00%

benchmark             old bytes     new bytes     delta
BenchmarkAES128-8     0             0             +0.00%
BenchmarkAES192-8     0             0             +0.00%
BenchmarkAES256-8     0             0             +0.00%
2020-01-03 12:15:35 +08:00
xtaci 497055bb7e simplify buffer creation in cryptos 2018-09-26 00:08:46 +08:00
xtaci 0b3dd1116c optimize the decrypting part as in encryption 2018-09-25 20:25:57 +08:00
xtaci 4c8c86725d remove the swapping in decrypting 2018-09-25 20:08:23 +08:00
xtaci a4bbf1e4b2 more optimizations on block encryption 2018-09-25 17:05:06 +08:00
xtaci b8c37e1a76 Loop unrolling in the core of CFB 2018-09-24 21:43:29 +08:00
xtaci f042b40cd3 add support SM4 block cipher 2017-10-09 22:55:47 +08:00
xtaci edee1a2ac6 use avx2 & sse2 optimized xor, estimated 3 times faster 2017-08-01 23:45:39 +08:00
xtaci 1ca4a76782 add a comment to BlockCrypt 2016-09-23 12:56:02 +08:00
xtaci e53ecdcee8 upd 2016-09-18 15:48:49 +08:00
xtaci a7d32a601e refactor 2016-09-18 15:39:28 +08:00
xtaci fdc17127b0 metalinter 2016-09-06 16:46:31 +08:00
xtaci 6cf1ab63e8 add salsa20 crypto 2016-08-20 14:52:47 +08:00
xtaci fbb6bf6788 pisces 2016-08-20 12:14:07 +08:00
xtaci be1bcbe05f add triple des encryption 2016-08-19 11:04:36 +08:00
xtaci c4508c6142 add cast5 encryption 2016-08-19 10:57:51 +08:00
xtaci c40d0f2d7c add blowfish crypt 2016-08-19 10:45:19 +08:00
xtaci 4976678721 lint 2016-06-29 22:18:39 +08:00