ringct: use memcpy/memset instead of handwritten loop where appropriate

This commit is contained in:
moneromooo-monero 2016-08-27 21:36:32 +01:00
parent 5d38206fcc
commit fd11271eea
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3

View file

@ -39,52 +39,38 @@ namespace rct {
//Creates a zero scalar //Creates a zero scalar
void zero(key &zero) { void zero(key &zero) {
int i = 0; memset(&zero, 0, 32);
for (i = 0; i < 32; i++) {
zero[i] = (unsigned char)(0x00);
}
} }
//Creates a zero scalar //Creates a zero scalar
key zero() { key zero() {
return{ {0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 } }; static const key z = { {0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 } };
return z;
} }
//Creates a zero elliptic curve point //Creates a zero elliptic curve point
void identity(key &Id) { void identity(key &Id) {
int i = 0;
Id[0] = (unsigned char)(0x01); Id[0] = (unsigned char)(0x01);
for (i = 1; i < 32; i++) { memset(Id.bytes+1, 0, 31);
Id[i] = (unsigned char)(0x00);
}
} }
//Creates a zero elliptic curve point //Creates a zero elliptic curve point
key identity() { key identity() {
key Id; key Id;
int i = 0;
Id[0] = (unsigned char)(0x01); Id[0] = (unsigned char)(0x01);
for (i = 1; i < 32; i++) { memset(Id.bytes+1, 0, 31);
Id[i] = (unsigned char)(0x00);
}
return Id; return Id;
} }
//copies a scalar or point //copies a scalar or point
void copy(key &AA, const key &A) { void copy(key &AA, const key &A) {
int i = 0; memcpy(&AA, &A, 32);
for (i = 0; i < 32; i++) {
AA[i] = A.bytes[i];
}
} }
//copies a scalar or point //copies a scalar or point
key copy(const key &A) { key copy(const key &A) {
int i = 0;
key AA; key AA;
for (i = 0; i < 32; i++) { memcpy(&AA, &A, 32);
AA[i] = A.bytes[i];
}
return AA; return AA;
} }
@ -387,10 +373,8 @@ namespace rct {
size_t i = 0, j = 0; size_t i = 0, j = 0;
vector<char> m(l * 64); vector<char> m(l * 64);
for (i = 0 ; i < l ; i++) { for (i = 0 ; i < l ; i++) {
for (j = 0 ; j < 32 ; j++) { memcpy(&m[i * 64], &PC[i].dest, 32);
m[i * 64 + j] = PC[i].dest[j]; memcpy(&m[i * 64 + 32], &PC[i].mask, 32);
m[i * 64 + 32 + j] = PC[i].mask[j];
}
} }
cn_fast_hash(rv, &m[0], 64*l); cn_fast_hash(rv, &m[0], 64*l);
return rv; return rv;
@ -409,11 +393,9 @@ namespace rct {
key cn_fast_hash(const keyV &keys) { key cn_fast_hash(const keyV &keys) {
size_t l = keys.size(); size_t l = keys.size();
vector<unsigned char> m(l * 32); vector<unsigned char> m(l * 32);
size_t i, j; size_t i;
for (i = 0 ; i < l ; i++) { for (i = 0 ; i < l ; i++) {
for (j = 0 ; j < 32 ; j++) { memcpy(&m[i * 32], keys[i].bytes, 32);
m[i * 32 + j] = keys[i][j];
}
} }
key rv; key rv;
cn_fast_hash(rv, &m[0], 32 * l); cn_fast_hash(rv, &m[0], 32 * l);