Re: Avoiding C++ Templates In Cipher Implementation
From: André Pönitz (poenitz_at_gmx.net)
Date: 06/11/03
- Next message: Ulrich Wurst: "Re: Is this a cipher?"
- Previous message: Malcolm Singh: "Re: timings for DSA, ECDSA, RSA"
- In reply to: Daniel James: "Re: Avoiding C++ Templates In Cipher Implementation"
- Next in thread: Daniel James: "Re: Avoiding C++ Templates In Cipher Implementation"
- Reply: Daniel James: "Re: Avoiding C++ Templates In Cipher Implementation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: 11 Jun 2003 09:51:02 GMT
In gnu.g++.help Daniel James <wastebasket@nospam.aaisp.org> wrote:
> You have various template classes (these are classes, not
> functions, right?) that are bound at compile time to
> specializations of the Cipher_RC6 template. What's the
> advantage of doing this over having separate classes called
> Cipher_RC6_128, Cipher_RC6_192, etc.?
Code re-use?
[And you could provide Cipher_RC6<N> which maps to the next better
algorithm etc and other 'cute' tricks]
> Either way, if an application needs to use RC6 with two
> different keylengths (we are talking about keylength, here,
> right?) you end up with two separate instantiations of the code
> in the executable.
Sure. But you have a decent chance that e.g. all loops are of fixed size
and the compiler can do a pretty good job on optimization on that.
> If, instead, you have a class Cipher_RC6 that takes a
> constructor argument (128, 192, etc.) that indicates the
> keylength, there will only be one copy of the code in the
> executable (with conditional branches to cope with the
> different handling of different keylengths).
But you would suddenly need dynamic data structure.
> That represents a considerable saving in program size for a very small
> (probably unmeasurable) increase in execution time.
First of all, I doubt the 'probably unmeasurable' as I've seen profiles of
other applications uniformly suggesting the contrary. Secondly, depending
on your code, execution time may matter.
>> Finally, as far as the argument of correctness goes, I could easily turn
>> the template arguments into run-time arguments, and the structure of the
>> code would hardly change.
>
> No, that's where problems start. If you need to use a cipher whose
> keylength you don't know until runtime you can't make use of the template
> resolution mechanism to select the correct cipher code at compile time
> (obviously).
>
> The following:
>
> int i = getKeyLength(); // from somewhere Cipher_RC6< 32, 20, i >
> cipher;
>
> won't compile, whereas:
>
> int i = getKeyLength(); // from somewhere Cipher_RC6 cipher(i);
>
> will.
And a toplevel
switch (getKeyLength()) {
case 128: Cipher_RC6<32, 20, 128>; break;
case 192: Cipher_RC6<32, 20, 192>; break;
}
certainly will compile, too.
Andre'
PS: F'up restricted to comp.lang.c++
-- Those who desire to give up Freedom in order to gain Security, will not have, nor do they deserve, either one. (T. Jefferson or B. Franklin or both...)
- Next message: Ulrich Wurst: "Re: Is this a cipher?"
- Previous message: Malcolm Singh: "Re: timings for DSA, ECDSA, RSA"
- In reply to: Daniel James: "Re: Avoiding C++ Templates In Cipher Implementation"
- Next in thread: Daniel James: "Re: Avoiding C++ Templates In Cipher Implementation"
- Reply: Daniel James: "Re: Avoiding C++ Templates In Cipher Implementation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|
|