forked from Fudmottin/sha-256
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord.h
More file actions
173 lines (142 loc) · 6.58 KB
/
Word.h
File metadata and controls
173 lines (142 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*******************************************************************************
* Word.h *
* Author: Fudmottin *
* *
* This software is provided 'as-is', without any express or implied warranty. *
* In no event will the authors be held liable for any damages arising from *
* the use of this software. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense and *
* or sell copies of the Software. *
* *
* The Word class is a simple lightweight wrapper for uint32_t. It is intended *
* to work like a uint32_t for cryptography. That said, not every possible *
* conversion to uint32_t is defined. Sometimes it will be necessary to use *
* the get() method to return the wrapped uint32_t value. In addition to *
* traditional get() and set() methods, assignment is supported. You can also *
* create a Word object using the constructor: Word w(42). *
* *
* Due to properties of Word object, two seemingly equal values may not *
* actually be equal unless their type is either Word or uint32_t. Size *
* matters! *
* *
* This file has been placed into The Public Domain *
* *
******************************************************************************/
#pragma once
#include <cstdint>
#include <iostream>
class Word {
private:
uint32_t w;
static constexpr uint32_t ONE = 1; // Named constant
public:
// Default constructor initializes internal representation (w) to zero.
Word() : w(0) {}
// Constructor that allows initialization with an unsigned int value.
Word(uint32_t val) : w(val) {}
// Copy constructor which copies value from another Word object into this one.
Word(const Word& other): w(other.w) {}
// Destructor - currently does nothing but included for completeness.
~Word() {}
// Getter and Setter methods
inline uint32_t get() const { return w; }
inline void set(unsigned int v) { w = static_cast<uint32_t>(v); }
// Bitwise rotate left and right operations ; use -std=c++20 option
inline Word rotl(int n) { return std::rotl(w, n); }
inline Word rotr(int n) { return std::rotr(w, n); }
// GetBit and SetBit methods
inline bool getbit(int n) const { return (w >> n) & ONE; }
inline void setbit(int n, bool b){
b ? w |= (ONE << n) : w &= ~(ONE << n);
}
// FlipBit method
inline void flipbit(int n) {setbit(n, ~getbit(n));}
// comparison operators
inline bool operator==(const Word& rhs){return this->w == rhs.w;}
inline bool operator==(const uint32_t rhs){return this->w == rhs;}
inline bool operator!=(const Word& rhs){return this->w != rhs.w;}
inline bool operator!=(const uint32_t rhs){return this->w != rhs;}
// logical operators
inline Word operator|(const Word& rhs){return this->w | rhs.w;}
inline Word operator|(const uint32_t rhs){return this->w | rhs;}
inline Word operator&(const Word& rhs){return this->w & rhs.w;}
inline Word operator&(const uint32_t rhs){return this->w & rhs;}
inline Word operator^(const Word& rhs){return this->w ^ rhs.w;}
inline Word operator^(const uint32_t rhs){return this->w ^ rhs;}
inline Word operator<<(const Word& rhs) { return w << rhs.w; }
inline Word operator<<(const uint32_t rhs) { return w << rhs; }
inline Word operator>>(const Word& rhs) { return w >> rhs.w; }
inline Word operator>>(const uint32_t rhs) { return w >> rhs; }
inline Word operator~() const { return Word(~w); }
// assignmennt operators
inline Word& operator=(const uint32_t val){
this->w = val;
return *this;
}
inline Word& operator=(const Word& other){
this->w = other.w;
return *this;
}
inline Word& operator|=(const uint32_t val){
this->w |= val;
return *this;
}
inline Word& operator|=(const Word& other){
this->w |= other.w;
return *this;
}
inline Word& operator&=(const uint32_t val){
this->w &= val;
return *this;
}
inline Word& operator&=(const Word& other){
this->w &= other.w;
return *this;
}
inline Word& operator^=(const uint32_t val){
this->w ^= val;
return *this;
}
inline Word& operator^=(const Word& other){
this->w ^= other.w;
return *this;
}
inline Word& operator<<=(const Word& rhs) {
w <<= rhs.w;
return *this;
}
inline Word& operator<<=(const uint32_t rhs) {
w <<= rhs;
return *this;
}
inline Word& operator>>=(const Word& other){
this->w >>= other.w;
return *this;
}
inline Word& operator>>=(const uint32_t val){
this->w >>= val;
return *this;
}
// + and - operators
inline Word operator+(const Word& rhs) { return this->w + rhs.w; }
inline Word operator+(const uint32_t rhs) { return this->w + rhs; }
inline Word operator-(const Word& rhs) { return this->w - rhs.w; }
inline Word operator-(const uint32_t rhs) { return this->w - rhs; }
// * and / operators
inline Word operator*(const Word& rhs) { return this->w * rhs.w; }
inline Word operator*(const uint32_t rhs) { return this->w * rhs; }
inline Word operator/(const Word& rhs) { return this->w / rhs.w; }
inline Word operator/(const uint32_t rhs) { return this->w / rhs; }
// % operator
inline Word operator%(const Word& rhs) { return this->w % rhs.w; }
inline Word operator%(const uint32_t rhs) { return this->w % rhs; }
// Output opperator for std::ostream
friend std::ostream& operator<<(std::ostream& os, const Word& obj) {
os << obj.w;
return os;
}
};