Commit d1cca320 authored by gerd's avatar gerd

Logical operations differ in their speed because of Ocaml's

integer representation. lor and land are a bit faster than lxor.
Because of this I reduced the number of lxor operations.


git-svn-id: https://godirepo.camlcity.org/svn/lib-cryptgps/trunk@6 b101cce4-44db-0310-b718-db4b2d8d2e05
parent b0357fc7
(* $Id: crypt_des.ml,v 1.4 1999/06/17 16:51:33 gerd Exp $
(* $Id: crypt_des.ml,v 1.5 1999/06/17 19:41:10 gerd Exp $
* ----------------------------------------------------------------------
*
*)
......@@ -491,8 +491,8 @@ module Cryptsystem : Cryptsystem_64.T =
{ data : string;
k64 : value64;
k56 : value64;
k_enc : value48 array;
k_dec : value48 array;
k_enc : (int * int * int * int) array;
k_dec : (int * int * int * int) array;
(* k_enc: encryption keys for 16 rounds;
* k_dec: decryption keys for 16 rounds
......@@ -518,7 +518,7 @@ module Cryptsystem : Cryptsystem_64.T =
let rec do_rounds i blast0 blast1 last0 last1 =
if i < 16 then
let k48_0, k48_1 = k.(i) in
let k48_0_sh12, k48_0_sh0, k48_1_sh12, k48_1_sh0 = k.(i) in
let l32_0 = blast0 in
let l32_1 = blast1 in
let r32_0 = last0 in
......@@ -526,33 +526,41 @@ module Cryptsystem : Cryptsystem_64.T =
(* ---------------- begin -------------------- *)
(* OLD: let (r48_0, r48_1) = do_perm48 xperm (r32_0,r32_1) in *)
(* The permutation can be expressed by logical operations directly: *)
let a0 = (r32_1 land 1) lsl 23 in
let a1 = (r32_0 land 0xf800) lsl 7 in
let a2 = (r32_0 land 0x1f80) lsl 5 in
let a0 = (r32_1 land 1) lsl 11 in
let a1 = (r32_0 land 0xf800) lsr 5 in
let a2 = (r32_0 land 0x1f80) lsr 7 in
let a3 = (r32_0 land 0x01f8) lsl 3 in
let a4 = (r32_0 land 0x001f) lsl 1 in
let a5 = r32_1 lsr 15 in
let b0 = (r32_0 land 1) lsl 23 in
let b1 = (r32_1 land 0xf800) lsl 7 in
let b2 = (r32_1 land 0x1f80) lsl 5 in
let b0 = (r32_0 land 1) lsl 11 in
let b1 = (r32_1 land 0xf800) lsr 5 in
let b2 = (r32_1 land 0x1f80) lsr 7 in
let b3 = (r32_1 land 0x01f8) lsl 3 in
let b4 = (r32_1 land 0x001f) lsl 1 in
let b5 = r32_0 lsr 15 in
let r48_0 = a0 lor a1 lor a2 lor a3 lor a4 lor a5 in
let r48_1 = b0 lor b1 lor b2 lor b3 lor b4 lor b5 in
(* ------------------ end -------------------- *)
let r'48_0 = k48_0 lxor r48_0 in
let r'48_1 = k48_1 lxor r48_1 in
let x00,x01 = s1.( r'48_0 lsr 18 ) in
let x10,x11 = s2.( (r'48_0 lsr 12) land 63 ) in
let x20,x21 = s3.( (r'48_0 lsr 6) land 63 ) in
let x30,x31 = s4.( r'48_0 land 63 ) in
let x40,x41 = s5.( r'48_1 lsr 18 ) in
let x50,x51 = s6.( (r'48_1 lsr 12) land 63 ) in
let x60,x61 = s7.( (r'48_1 lsr 6) land 63 ) in
let x70,x71 = s8.( r'48_1 land 63 ) in
let z0 = (a0 lor a1 lor a2) lxor k48_0_sh12 in
let z1 = (a3 lor a4 lor a5) lxor k48_0_sh0 in
let z2 = (b0 lor b1 lor b2) lxor k48_1_sh12 in
let z3 = (b3 lor b4 lor b5) lxor k48_1_sh0 in
(*
assert ((z0 lsr 12) = 0);
assert ((z1 lsr 12) = 0);
assert ((z2 lsr 12) = 0);
assert ((z3 lsr 12) = 0);
*)
let x00,x01 = s1.( z0 lsr 6 ) in
let x10,x11 = s2.( z0 land 63) in
let x20,x21 = s3.( z1 lsr 6) in
let x30,x31 = s4.( z1 land 63) in
let x40,x41 = s5.( z2 lsr 6 ) in
let x50,x51 = s6.( z2 land 63 ) in
let x60,x61 = s7.( z3 lsr 6 ) in
let x70,x71 = s8.( z3 land 63 ) in
let p0 =x00 lor x10 lor x20 lor x30 lor x40 lor x50 lor x60 lor x70 in
let p1 =x01 lor x11 lor x21 lor x31 lor x41 lor x51 lor x61 lor x71 in
......@@ -660,8 +668,8 @@ module Cryptsystem : Cryptsystem_64.T =
(x' land 0xfffffff) lor (x' lsr 28)
in
let k = ref k56 in
let k_enc = Array.create 16 (0,0) in
let k_dec = Array.create 16 (0,0) in
let k_enc = Array.create 16 (0,0,0,0) in
let k_dec = Array.create 16 (0,0,0,0) in
for n = 0 to 15 do
let (k0,k1,k2,k3) = !k in
let k_left = (k0 lsl 16) lor k1 in (* k_left: 28 bits *)
......@@ -674,11 +682,15 @@ module Cryptsystem : Cryptsystem_64.T =
k_right' lsr 12,
(k_right' land 0xfff) lsl 4 );
let (c0,c1,c2,c3) = do_perm64 cperm !k in
let k48 =
let k48_0, k48_1 =
( (c0 lsl 16) lor c1,
(c2 lsl 16) lor c3 ) in
k_enc.(n) <- k48;
k_dec.(15 - n) <- k48
let k48_0_sh12 = (k48_0 lsr 12) land 0xfff in
let k48_0_sh0 = k48_0 land 0xfff in
let k48_1_sh12 = (k48_1 lsr 12) land 0xfff in
let k48_1_sh0 = k48_1 land 0xfff in
k_enc.(n) <- k48_0_sh12, k48_0_sh0, k48_1_sh12, k48_1_sh0;
k_dec.(15 - n) <- k_enc.(n)
done;
{ data = key;
......@@ -773,6 +785,11 @@ module Cryptmodes = Cryptmodes_64.Make_modes(Cryptsystem)
* history:
*
* $Log: crypt_des.ml,v $
* Revision 1.5 1999/06/17 19:41:10 gerd
* Logical operations differ in their speed because of Ocaml's
* integer representation. lor and land are a bit faster than lxor.
* Because of this I reduced the number of lxor operations.
*
* Revision 1.4 1999/06/17 16:51:33 gerd
* The 'xperm' permutation is done by bit-shifting instead of
* an array lookup.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment