Skip to content

Instantly share code, notes, and snippets.

@andreasnoack
Created November 19, 2014 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreasnoack/b2e86a5205b2390b2245 to your computer and use it in GitHub Desktop.
Save andreasnoack/b2e86a5205b2390b2245 to your computer and use it in GitHub Desktop.
module Random123
macro skein_mk_64(hi32, lo32)
return :($lo32 + (convert(Uint64, $hi32) << 32))
end
const SKEIN_KS_PARITY64 = @skein_mk_64(0x1BD11BDA, 0xA9FC1A22)
const SKEIN_KS_PARITY32 = 0x1BD11BDA
rotl(x::Uint64, N::Integer) = (x << (N & 63)) | (x >> ((64 - N) & 63))
const R_64x2_0_0 = 16
const R_64x2_1_0 = 42
const R_64x2_2_0 = 12
const R_64x2_3_0 = 31
const R_64x2_4_0 = 16
const R_64x2_5_0 = 32
const R_64x2_6_0 = 24
const R_64x2_7_0 = 21
const R_32x2_0_0 = 13
const R_32x2_1_0 = 15
const R_32x2_2_0 = 26
const R_32x2_3_0 = 6
const R_32x2_4_0 = 17
const R_32x2_5_0 = 29
const R_32x2_6_0 = 16
const R_32x2_7_0 = 24
for (sz, T) in ((:32, Uint32), (:64, Uint64))
@eval begin
# function threefly(Nrounds::Uint, inarg::NTuple{2,$T}, k::NTuple{2,$T})
# Nrounds <= 32 || error("Hej")
# ks2 = ($(symbol(string("SKEIN_KS_PARITY", sz))))::$T
# ks0 = k[1]::$T
# Xv0 = inarg[1]::$T
# ks2 $= k[1]::$T
# ks1 = k[2]::$T
# Xv1 = inarg[2]::$T
# ks2 $= k[2]::$T
function threefly(Nrounds::Uint, inarg1::$T, inarg2::$T, k1::$T, k2::$T)
@inbounds begin
# @assert Nrounds <= 32
ks2 = $(symbol(string("SKEIN_KS_PARITY", sz)))
ks0 = k1
Xv0 = inarg1
ks2 $= k1
ks1 = k2
Xv1 = inarg2
ks2 $= k2
# Insert initial key before round 0
Xv0 += ks0; Xv1 += ks1
if Nrounds > 0; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_0_0")))); Xv1 $= Xv0; end
if Nrounds > 1; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_1_0")))); Xv1 $= Xv0; end
if Nrounds > 2; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_2_0")))); Xv1 $= Xv0; end
if Nrounds > 3; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_3_0")))); Xv1 $= Xv0; end
if Nrounds > 3
# InjectKey(r=1)
Xv0 += ks1; Xv1 += ks2
Xv1 += 1; # Xv[1] += r
end
if Nrounds > 4; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_4_0")))); Xv1 $= Xv0; end
if Nrounds > 5; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_5_0")))); Xv1 $= Xv0; end
if Nrounds > 6; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_6_0")))); Xv1 $= Xv0; end
if Nrounds > 7; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_7_0")))); Xv1 $= Xv0; end
if Nrounds > 7
# InjectKey(r=2)
Xv0 += ks2; Xv1 += ks0;
Xv1 += 2;
end
if Nrounds > 8; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_0_0")))); Xv1 $= Xv0; end
if Nrounds > 9; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_1_0")))); Xv1 $= Xv0; end
if Nrounds > 10; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_2_0")))); Xv1 $= Xv0; end
if Nrounds > 11; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_3_0")))); Xv1 $= Xv0; end
if Nrounds > 11
# InjectKey(r=3)
Xv0 += ks0; Xv1 += ks1;
Xv1 += 3
end
if Nrounds > 12; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_4_0")))); Xv1 $= Xv0; end
if Nrounds > 13; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_5_0")))); Xv1 $= Xv0; end
if Nrounds > 14; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_6_0")))); Xv1 $= Xv0; end
if Nrounds > 15; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_7_0")))); Xv1 $= Xv0; end
if Nrounds > 15
# InjectKey(r=4)
Xv0 += ks1; Xv1 += ks2;
Xv1 += 4;
end
if Nrounds > 16; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_0_0")))); Xv1 $= Xv0; end
if Nrounds > 17; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_1_0")))); Xv1 $= Xv0; end
if Nrounds > 18; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_2_0")))); Xv1 $= Xv0; end
if Nrounds > 19; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_3_0")))); Xv1 $= Xv0; end
if Nrounds > 19
# InjectKey(r=5)
Xv0 += ks2; Xv1 += ks0
Xv1 += 5;
end
if Nrounds > 20; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_4_0")))); Xv1 $= Xv0; end
if Nrounds > 21; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_5_0")))); Xv1 $= Xv0; end
if Nrounds > 22; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_6_0")))); Xv1 $= Xv0; end
if Nrounds > 23; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_7_0")))); Xv1 $= Xv0; end
if Nrounds > 23
# InjectKey(r=6)
Xv0 += ks0; Xv1 += ks1;
Xv1 += 6;
end
if Nrounds > 24; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_0_0")))); Xv1 $= Xv0; end
if Nrounds > 25; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_1_0")))); Xv1 $= Xv0; end
if Nrounds > 26; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_2_0")))); Xv1 $= Xv0; end
if Nrounds > 27; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_3_0")))); Xv1 $= Xv0; end
if Nrounds > 27
# InjectKey(r=7)
Xv0 += ks1; Xv1 += ks2;
Xv1 += 7;
end
if Nrounds > 28; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_4_0")))); Xv1 $= Xv0; end
if Nrounds > 29; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_5_0")))); Xv1 $= Xv0; end
if Nrounds > 30; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_6_0")))); Xv1 $= Xv0; end
if Nrounds > 31; Xv0 += Xv1; Xv1 = rotl(Xv1, $(symbol(string("R_", sz, "x2_7_0")))); Xv1 $= Xv0; end
if Nrounds > 31
# InjectKey(r=8)
Xv0 += ks2; Xv1 += ks0;
Xv1 += 8;
end
return Xv0, Xv1
# return ks2
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment