Skip to content

Instantly share code, notes, and snippets.

@CraZySacX
Last active August 29, 2015 14:01
Show Gist options
  • Save CraZySacX/eccc3e755db4ffe33976 to your computer and use it in GitHub Desktop.
Save CraZySacX/eccc3e755db4ffe33976 to your computer and use it in GitHub Desktop.
Which is preferable from a functional perspective?
(defn- quarter-subround
"### quarter-subround
A quarter subround.
Represents a vector of two values:
[w = (x + y mod 2<sup>32</sup>)
((w ^ z) <<< shift)]"
{:added "0.2.0"}
[[x y z] shift]
(let [w (+modw x y)]
[w (<<< (bit-xor w z) shift)]))
(defn- quarter-round
"### quarter-round
quarterround function as defined in [ChaCha Spec][chacha]"
{:added "0.2.0"}
[a b c d]
(let [[a d] (quarter-subround [a b d] 16)
[c b] (quarter-subround [c d b] 12)
[a d] (quarter-subround [a b d] 8)
[c b] (quarter-subround [c d b] 7)]
[a b c d]))
(defn- quarter-subround
"### quarter-subround
A quarter subround.
Represents a vector of two values:
[w = (x + y mod 2<sup>32</sup>)
((w ^ z) <<< shift)]"
{:added "0.2.0"}
[[a b c d] shift]
(cond
(or (= shift 16) (= shift 8)) (let [a (+modw a b)]
[a b c (<<< (bit-xor a d) shift)])
(or (= shift 12) (= shift 7)) (let [c (+modw c d)]
[a (<<< (bit-xor c b) shift) c d])))
(defn- quarter-round
"### quarter-round
quarterround function as defined in [ChaCha Spec][chacha]"
{:added "0.2.0"}
[a b c d]
(vec (reduce quarter-subround [a b c d] [16 12 8 7])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment