Skip to content

Instantly share code, notes, and snippets.

@dchest
Last active December 12, 2015 07:58
Show Gist options
  • Select an option

  • Save dchest/4740510 to your computer and use it in GitHub Desktop.

Select an option

Save dchest/4740510 to your computer and use it in GitHub Desktop.
Comparison to rc4_amd64.s on Core 2 Duo
benchmark old ns/op new ns/op delta
BenchmarkRC4_128 820 604 -26.34%
BenchmarkRC4_1K 6420 4444 -30.78%
BenchmarkRC4_8K 50373 35335 -29.85%
benchmark old MB/s new MB/s speedup
BenchmarkRC4_128 156.00 211.80 1.36x
BenchmarkRC4_1K 159.49 230.40 1.44x
BenchmarkRC4_8K 160.72 229.12 1.43x
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build amd64
package rc4
import "unsafe"
// XORKeyStream sets dst to the result of XORing src with the key stream.
// Dst and src may be the same slice but otherwise should not overlap.
func (c *Cipher) XORKeyStream(dst, src []byte) {
// Go unsafe version.
if len(dst) < len(src) {
panic("rc4: dst is shorter than src")
}
sp := uintptr(unsafe.Pointer(&src[0]))
dp := uintptr(unsafe.Pointer(&dst[0]))
send := sp + uintptr(len(src))
i, j := c.i + 1, c.j
s := &c.s
a := s[i]
for sp < send {
j += a
b := s[j]
s[i], s[j] = b, a
*(*byte)(unsafe.Pointer(dp)) = s[a+b] ^ *(*byte)(unsafe.Pointer(sp))
sp++
dp++
i += 1
a = s[i]
}
c.i, c.j = i-1, j
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment