ringct: optimization/cleanup of hash functions

This commit is contained in:
Shen Noether 2016-07-09 20:04:23 +01:00 committed by moneromooo-monero
parent 4fd01f2bee
commit dbb5f2d6a3
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
3 changed files with 45 additions and 22 deletions

View file

@ -423,6 +423,31 @@ namespace rct {
return rv; return rv;
} }
//cn_fast_hash for a key-vector of arbitrary length
//this is useful since you take a number of keys
//put them in the key vector and it concatenates them
//and then hashes them
key cn_fast_hash(const keyV &keys) {
size_t l = keys.size();
vector<unsigned char> m(l * 32);
size_t i, j;
for (i = 0 ; i < l ; i++) {
for (j = 0 ; j < 32 ; j++) {
m[i * 32 + j] = keys[i][j];
}
}
key rv;
cn_fast_hash(rv, &m[0], 32 * l);
//dp(rv);
return rv;
}
key hash_to_scalar(const keyV &keys) {
key rv = cn_fast_hash(keys);
sc_reduce32(rv.bytes);
return rv;
}
key hashToPointSimple(const key & hh) { key hashToPointSimple(const key & hh) {
key pointk; key pointk;
ge_p1p1 point2; ge_p1p1 point2;

View file

@ -151,6 +151,9 @@ namespace rct {
key hash_to_scalar128(const void * in); key hash_to_scalar128(const void * in);
key cn_fast_hash(ctkeyV PC); key cn_fast_hash(ctkeyV PC);
key hash_to_scalar(ctkeyV PC); key hash_to_scalar(ctkeyV PC);
//for mg sigs
key cn_fast_hash(const keyV &keys);
key hash_to_scalar(const keyV &keys);
//returns hashToPoint as described in https://github.com/ShenNoether/ge_fromfe_writeup //returns hashToPoint as described in https://github.com/ShenNoether/ge_fromfe_writeup
key hashToPointSimple(const key &in); key hashToPointSimple(const key &in);

View file

@ -169,22 +169,21 @@ namespace rct {
keyV alpha(rows); keyV alpha(rows);
keyV aG(rows); keyV aG(rows);
keyV aHP(rows); keyV aHP(rows);
key m2hash; keyV toHash(1 + 3 * rows);
unsigned char m2[128]; toHash[0] = message;
memcpy(m2, message.bytes, 32);
DP("here1"); DP("here1");
for (i = 0; i < rows; i++) { for (i = 0; i < rows; i++) {
skpkGen(alpha[i], aG[i]); //need to save alphas for later.. skpkGen(alpha[i], aG[i]); //need to save alphas for later..
Hi = hashToPoint(pk[index][i]); Hi = hashToPoint(pk[index][i]);
aHP[i] = scalarmultKey(Hi, alpha[i]); aHP[i] = scalarmultKey(Hi, alpha[i]);
memcpy(m2+32, pk[index][i].bytes, 32); toHash[3 * i + 1] = pk[index][i];
memcpy(m2 + 64, aG[i].bytes, 32); toHash[3 * i + 2] = aG[i];
memcpy(m2 + 96, aHP[i].bytes, 32); toHash[3 * i + 3] = aHP[i];
rv.II[i] = scalarmultKey(Hi, xx[i]); rv.II[i] = scalarmultKey(Hi, xx[i]);
precomp(Ip[i].k, rv.II[i]); precomp(Ip[i].k, rv.II[i]);
m2hash = hash_to_scalar128(m2);
sc_add(c_old.bytes, c_old.bytes, m2hash.bytes);
} }
c_old = hash_to_scalar(toHash);
i = (index + 1) % cols; i = (index + 1) % cols;
if (i == 0) { if (i == 0) {
@ -198,12 +197,11 @@ namespace rct {
addKeys2(L, rv.ss[i][j], c_old, pk[i][j]); addKeys2(L, rv.ss[i][j], c_old, pk[i][j]);
hashToPoint(Hi, pk[i][j]); hashToPoint(Hi, pk[i][j]);
addKeys3(R, rv.ss[i][j], Hi, c_old, Ip[j].k); addKeys3(R, rv.ss[i][j], Hi, c_old, Ip[j].k);
memcpy(m2+32, pk[i][j].bytes, 32); toHash[3 * j + 1] = pk[i][j];
memcpy(m2 + 64, L.bytes, 32); toHash[3 * j + 2] = L;
memcpy(m2 + 96, R.bytes, 32); toHash[3 * j + 3] = R;
m2hash = hash_to_scalar128(m2);
sc_add(c.bytes, c.bytes, m2hash.bytes);
} }
c = hash_to_scalar(toHash);
copy(c_old, c); copy(c_old, c);
i = (i + 1) % cols; i = (i + 1) % cols;
@ -248,10 +246,8 @@ namespace rct {
for (i= 0 ; i< rows ; i++) { for (i= 0 ; i< rows ; i++) {
precomp(Ip[i].k, II[i]); precomp(Ip[i].k, II[i]);
} }
unsigned char m2[128]; keyV toHash(1 + 3 * rows);
memcpy(m2, message.bytes, 32); toHash[0] = message;
key m2hash;
i = 0; i = 0;
while (i < cols) { while (i < cols) {
sc_0(c.bytes); sc_0(c.bytes);
@ -259,12 +255,11 @@ namespace rct {
addKeys2(L, rv.ss[i][j], c_old, pk[i][j]); addKeys2(L, rv.ss[i][j], c_old, pk[i][j]);
hashToPoint(Hi, pk[i][j]); hashToPoint(Hi, pk[i][j]);
addKeys3(R, rv.ss[i][j], Hi, c_old, Ip[j].k); addKeys3(R, rv.ss[i][j], Hi, c_old, Ip[j].k);
memcpy(m2 + 32, pk[i][j].bytes, 32); toHash[3 * j + 1] = pk[i][j];
memcpy(m2 + 64, L.bytes, 32); toHash[3 * j + 2] = L;
memcpy(m2 + 96, R.bytes, 32); toHash[3 * j + 3] = R;
m2hash = hash_to_scalar128(m2);
sc_add(c.bytes, c.bytes, m2hash.bytes);
} }
c = hash_to_scalar(toHash);
copy(c_old, c); copy(c_old, c);
i = (i + 1); i = (i + 1);
} }