I am trying to implement a protocol described in the paper Private Data Aggregation with Groups for Smart Grids in a Dynamic Setting using CRT in python.
In order to do this, I need to calculate the following value:
I know that since python 3.6, you can calculate a SHA3 value as follows:
import hashlib
hash_object = hashlib.sha3_512(b'value_to_encode')
hash_value = hash_object.hexdigest()
I was wondering you should solve this, since, as far as I know, a SHA-3 function returns a string and therefore cannot be calculated in a function with to the power of n.
What am I overlooking?
If we define a hash function $H: \{0, 1\}^* \rightarrow \{0, 1\}^n$, that is one that produces an $n$ bit output, we can always interpret the binary data $h$ that it outputs as an integer. The integer value of this digest is $\sum_{i=0}^n h_i 2^i$, in other words the digest is a base 2 representation of the integer.
In your case, since python has a notion of types, we need to take the binary string and convert it to an integer type. The builtin int
function can do this for us:
int(x=0) -> integer
int(x, base=10) -> integer
Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero.
If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by '+' or '-' and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
The hexdigest
call will return a hex string which is base 16, so you would want to do something like int_value = int(hash_value, 16)
.