From dbb5f2d6a3d34c0fa899fd26313873cc69dbad9d Mon Sep 17 00:00:00 2001 From: Shen Noether Date: Sat, 9 Jul 2016 20:04:23 +0100 Subject: [PATCH] ringct: optimization/cleanup of hash functions --- src/ringct/rctOps.cpp | 25 +++++++++++++++++++++++++ src/ringct/rctOps.h | 3 +++ src/ringct/rctSigs.cpp | 39 +++++++++++++++++---------------------- 3 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/ringct/rctOps.cpp b/src/ringct/rctOps.cpp index 3fa870fd0..c18ee6f2b 100644 --- a/src/ringct/rctOps.cpp +++ b/src/ringct/rctOps.cpp @@ -423,6 +423,31 @@ namespace rct { 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 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 pointk; ge_p1p1 point2; diff --git a/src/ringct/rctOps.h b/src/ringct/rctOps.h index ad6c520da..6438a1f03 100644 --- a/src/ringct/rctOps.h +++ b/src/ringct/rctOps.h @@ -151,6 +151,9 @@ namespace rct { key hash_to_scalar128(const void * in); key cn_fast_hash(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 key hashToPointSimple(const key &in); diff --git a/src/ringct/rctSigs.cpp b/src/ringct/rctSigs.cpp index c9e34ddb6..7fcb8e158 100644 --- a/src/ringct/rctSigs.cpp +++ b/src/ringct/rctSigs.cpp @@ -169,22 +169,21 @@ namespace rct { keyV alpha(rows); keyV aG(rows); keyV aHP(rows); - key m2hash; - unsigned char m2[128]; - memcpy(m2, message.bytes, 32); + keyV toHash(1 + 3 * rows); + toHash[0] = message; DP("here1"); for (i = 0; i < rows; i++) { skpkGen(alpha[i], aG[i]); //need to save alphas for later.. Hi = hashToPoint(pk[index][i]); aHP[i] = scalarmultKey(Hi, alpha[i]); - memcpy(m2+32, pk[index][i].bytes, 32); - memcpy(m2 + 64, aG[i].bytes, 32); - memcpy(m2 + 96, aHP[i].bytes, 32); + toHash[3 * i + 1] = pk[index][i]; + toHash[3 * i + 2] = aG[i]; + toHash[3 * i + 3] = aHP[i]; rv.II[i] = scalarmultKey(Hi, xx[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; if (i == 0) { @@ -198,12 +197,11 @@ namespace rct { addKeys2(L, rv.ss[i][j], c_old, pk[i][j]); hashToPoint(Hi, pk[i][j]); addKeys3(R, rv.ss[i][j], Hi, c_old, Ip[j].k); - memcpy(m2+32, pk[i][j].bytes, 32); - memcpy(m2 + 64, L.bytes, 32); - memcpy(m2 + 96, R.bytes, 32); - m2hash = hash_to_scalar128(m2); - sc_add(c.bytes, c.bytes, m2hash.bytes); + toHash[3 * j + 1] = pk[i][j]; + toHash[3 * j + 2] = L; + toHash[3 * j + 3] = R; } + c = hash_to_scalar(toHash); copy(c_old, c); i = (i + 1) % cols; @@ -248,10 +246,8 @@ namespace rct { for (i= 0 ; i< rows ; i++) { precomp(Ip[i].k, II[i]); } - unsigned char m2[128]; - memcpy(m2, message.bytes, 32); - - key m2hash; + keyV toHash(1 + 3 * rows); + toHash[0] = message; i = 0; while (i < cols) { sc_0(c.bytes); @@ -259,12 +255,11 @@ namespace rct { addKeys2(L, rv.ss[i][j], c_old, pk[i][j]); hashToPoint(Hi, pk[i][j]); addKeys3(R, rv.ss[i][j], Hi, c_old, Ip[j].k); - memcpy(m2 + 32, pk[i][j].bytes, 32); - memcpy(m2 + 64, L.bytes, 32); - memcpy(m2 + 96, R.bytes, 32); - m2hash = hash_to_scalar128(m2); - sc_add(c.bytes, c.bytes, m2hash.bytes); + toHash[3 * j + 1] = pk[i][j]; + toHash[3 * j + 2] = L; + toHash[3 * j + 3] = R; } + c = hash_to_scalar(toHash); copy(c_old, c); i = (i + 1); }