Skip to content

Commit 276fde9

Browse files
committed
Type checks and integer shift checked
1 parent 6c4fdc5 commit 276fde9

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

ciphers/uint128_t.hpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -637,19 +637,20 @@ class uint128_t {
637637
uint128_t operator<<(const T p) {
638638
if (!p) {
639639
return uint128_t(f, s);
640-
}
641-
if (p >= 64 && p <= 128) {
640+
} else if (p >= 64 && p <= 128) {
642641
return uint128_t((this->s << (p - 64)), 0);
642+
} else if (p < 64 && p > 0) {
643+
return uint128_t((this->f << p) + ((this->s >> (64 - p))),
644+
this->s << p);
643645
}
644-
return uint128_t((this->f << p) + ((this->s >> (64 - p))),
645-
this->s << p);
646+
return uint128_t(0);
646647
}
647648

648649
template <typename T, typename = typename std::enable_if<
649650
std::is_integral<T>::value, T>::type>
650651
uint128_t &operator<<=(const T p) {
651652
if (p) {
652-
if (p >= 64) {
653+
if (p >= 64 && p <= 128) {
653654
this->f = (this->s << (p - 64));
654655
this->s = 0;
655656
} else {
@@ -665,12 +666,13 @@ class uint128_t {
665666
uint128_t operator>>(const T p) {
666667
if (!p) {
667668
return uint128_t(this->f, this->s);
668-
}
669-
if (p >= 64) {
669+
} else if (p >= 64 && p <= 128) {
670670
return uint128_t(0, (this->f >> (p - 64)));
671+
} else if (p < 64 && p > 0) {
672+
return uint128_t((this->f >> p),
673+
(this->s >> p) + (this->f << (64 - p)));
671674
}
672-
return uint128_t((this->f >> p),
673-
(this->s >> p) + (this->f << (64 - p)));
675+
return uint128_t(0);
674676
}
675677

676678
template <typename T, typename = typename std::enable_if<

ciphers/uint256_t.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,7 @@ class uint256_t {
597597
uint256_t operator<<(const T &p) {
598598
if (!p) {
599599
return uint256_t(this->f, this->s);
600-
}
601-
if (p >= 128) {
600+
} else if (p >= 128) {
602601
return uint256_t((this->s << (p - 128)), uint128_t(0));
603602
}
604603
return uint256_t((this->f << p) + (this->s >> (128 - p)),
@@ -625,8 +624,7 @@ class uint256_t {
625624
uint256_t operator>>(const T &p) {
626625
if (!p) {
627626
return uint256_t(this->f, this->s);
628-
}
629-
if (p >= 128) {
627+
} else if (p >= 128) {
630628
return uint256_t(uint128_t(0), (this->f >> (p - 128)));
631629
}
632630
return uint256_t((this->f >> p),

0 commit comments

Comments
 (0)