patterncppModerate
Custom integer class for calculating crypto algorithms
Viewed 0 times
algorithmscustomcalculatingcryptoforclassinteger
Problem
How can I improve upon this integer class that I wrote? I am/will be using this for some calculation intensive crypto algorithms, mainly POLY1305AES. I might even write RSA using this. I know that there are more efficient algorithms for at least the algebraic operators, but I will rewrite those when I learn them.
Right now, I am trying to find anything I might have missed while writing this.
Note that this code might not run if your compiler is too strict, like ideone.com's compiler (the errors are not caused by the lack of a
There are a lot of operators and I'm still missing many of them.
```
/*
integer.h
An integer type that does not have bit size restrictions
by Jason Lee @ calccrypto at yahoo dot com
NOTE: whether or not negative numbers will be implemented
is yet to be determined
*/
#ifndef INTEGER_H
#define INTEGER_H
#include
#include
#include
#include
// Checks platform bitsize using stdint.h
#ifndef BITS
#define BITS 32
#if INTPTR_MAX == INT64_MAX
#undef BITS
#define BITS 64
#endif
#endif
// //////////////////////////////////////
class integer{
private:
void clean(){
while ((value.begin() != value.end()) & !value.front())
value.pop_front();
}
integer twos_complement(){
return (~*this) + 1;
}
public:
std::deque value;
// Constructors
integer(){
value.clear();
}
template
integer(T rhs){
*this = rhs;
}
integer(std::deque val){
value = val;
clean();
}
integer(const integer & rhs){
value = rhs.value;
}
// RHS input args only
// Assignment Operator
Right now, I am trying to find anything I might have missed while writing this.
- Should I change my container?
- Are can any of the algorithms be improved?
- Is something running too many times?
- Anything I should do dynamically?
Note that this code might not run if your compiler is too strict, like ideone.com's compiler (the errors are not caused by the lack of a
main(); rather, it thinks some of the operators are ambiguous).There are a lot of operators and I'm still missing many of them.
```
/*
integer.h
An integer type that does not have bit size restrictions
by Jason Lee @ calccrypto at yahoo dot com
NOTE: whether or not negative numbers will be implemented
is yet to be determined
*/
#ifndef INTEGER_H
#define INTEGER_H
#include
#include
#include
#include
// Checks platform bitsize using stdint.h
#ifndef BITS
#define BITS 32
#if INTPTR_MAX == INT64_MAX
#undef BITS
#define BITS 64
#endif
#endif
// //////////////////////////////////////
class integer{
private:
void clean(){
while ((value.begin() != value.end()) & !value.front())
value.pop_front();
}
integer twos_complement(){
return (~*this) + 1;
}
public:
std::deque value;
// Constructors
integer(){
value.clear();
}
template
integer(T rhs){
*this = rhs;
}
integer(std::deque val){
value = val;
clean();
}
integer(const integer & rhs){
value = rhs.value;
}
// RHS input args only
// Assignment Operator
Solution
#define BITS 32
#if INTPTR_MAX == INT64_MAX
#undef BITS
#define BITS 64
#endif#define it, and optionally #undefing it is ugly. Use an else block with parallel #defines instead.integer(){
value.clear();
}value will be empty when the object is constructed, so there is no need to clear it.operator char(){
return (char) value.back();
}Converting to a
char by ignoring everything but one element strikes me as ill-advised.operator uint64_t(){
int out = 0, count = 0;
std::deque ::iterator i = value.end();
while ((*this > 0) && (count < 8))
out += *(i++);
return out;
}Do you really need to convert to so many different integer types? You should probably assert/throw an error if the value cannot be stored in the output format type.
integer operator|(integer rhs){
std::deque larger = value, smaller = rhs.value;You make unnecessary copies of the data here.
if (value.size() ::reverse_iterator i = smaller.rbegin(), j = larger.rbegin(); i != smaller.rend(); i++, j++)Why are you using a reverse iterator?
*j |= *i;
return integer(larger);
}The
& operator is very similar; they should be refactored to eliminate duplicate code.integer operator~(){
std::deque out = value;
std::deque ::reverse_iterator i = out.rbegin(), j = out.rend();
j--;i and j are not very helpful names.for(i = i; i != j; i++)
*i ^= 0xff;
// get highest bit
int8_t top = 8;
while (!((1 -1; top--)
*i ^= 1 << top;
return integer(out);
}Can't you just apply
~ to all of the elements? At any rate, some comments as to why you are doing what you are doing here would be useful.Code Snippets
#define BITS 32
#if INTPTR_MAX == INT64_MAX
#undef BITS
#define BITS 64
#endifinteger(){
value.clear();
}operator char(){
return (char) value.back();
}operator uint64_t(){
int out = 0, count = 0;
std::deque <uint8_t>::iterator i = value.end();
while ((*this > 0) && (count < 8))
out += *(i++);
return out;
}integer operator|(integer rhs){
std::deque <uint8_t> larger = value, smaller = rhs.value;Context
StackExchange Code Review Q#3212, answer score: 10
Revisions (0)
No revisions yet.