make d2h et al. constant-time

This commit is contained in:
Jethro Grassie 2019-11-08 01:26:17 -05:00
parent 81da5246e4
commit d8fc8d8868
No known key found for this signature in database
GPG key ID: DE8ED755616565BB

View file

@ -31,6 +31,7 @@
#include "misc_log_ex.h" #include "misc_log_ex.h"
#include "cryptonote_config.h" #include "cryptonote_config.h"
#include "rctTypes.h" #include "rctTypes.h"
#include "int-util.h"
using namespace crypto; using namespace crypto;
using namespace std; using namespace std;
@ -118,40 +119,22 @@ namespace rct {
//uint long long to 32 byte key //uint long long to 32 byte key
void d2h(key & amounth, const xmr_amount in) { void d2h(key & amounth, const xmr_amount in) {
sc_0(amounth.bytes); sc_0(amounth.bytes);
xmr_amount val = in; memcpy_swap64le(amounth.bytes, &in, 1);
int i = 0;
while (val != 0) {
amounth[i] = (unsigned char)(val & 0xFF);
i++;
val /= (xmr_amount)256;
}
} }
//uint long long to 32 byte key //uint long long to 32 byte key
key d2h(const xmr_amount in) { key d2h(const xmr_amount in) {
key amounth; key amounth;
sc_0(amounth.bytes); d2h(amounth, in);
xmr_amount val = in;
int i = 0;
while (val != 0) {
amounth[i] = (unsigned char)(val & 0xFF);
i++;
val /= (xmr_amount)256;
}
return amounth; return amounth;
} }
//uint long long to int[64] //uint long long to int[64]
void d2b(bits amountb, xmr_amount val) { void d2b(bits amountb, xmr_amount val) {
int i = 0; int i = 0;
while (val != 0) {
amountb[i] = val & 1;
i++;
val >>= 1;
}
while (i < 64) { while (i < 64) {
amountb[i] = 0; amountb[i++] = val & 1;
i++; val >>= 1;
} }
} }
@ -172,16 +155,11 @@ namespace rct {
int val = 0, i = 0, j = 0; int val = 0, i = 0, j = 0;
for (j = 0; j < 8; j++) { for (j = 0; j < 8; j++) {
val = (unsigned char)test.bytes[j]; val = (unsigned char)test.bytes[j];
i = 8 * j; i = 0;
while (val != 0) { while (i < 8) {
amountb2[i] = val & 1; amountb2[j*8+i++] = val & 1;
i++;
val >>= 1; val >>= 1;
} }
while (i < 8 * (j + 1)) {
amountb2[i] = 0;
i++;
}
} }
} }