I am wondering what the space and time complexity of SHA-2 is. I tried looking around, didn't really get a straight forward answer. Can anyone help me out? Thanks very much!
1 Answers
For example, let's consider SHA-256.
Based on the description of the algorithm given here, main steps are:
- Initialise
h1,..,h8
(first32
bits of the fractional parts of the square roots of the first8
primes2..19
). - Initialise
k1,...,k64
(first32
bits of the fractional parts of the cube roots of the first64
primes2..311
).
Since their number is fixed, then the time complexity and space complexity needed for their initialisation is constant.
Break the message into
512
- bit parts and for each512
- bit part do the following:3.1 Calculate a
1536
- bit number in48
iterations by using a constant number of bitwise and arithmetic operations. Several temp variables may be used in between.3.2 Set additional
8
variablesa1,...,a8
equal toh1,...,h8
3.3 Keep updating
a1,...,a8
in a loop64
times by using a fixed number of variables and performing the fixed number of bitwise and arithmetic operations.3.4 Add
a1,...,a8
toh1,...,h8
Concatenate
h1,...,h8
.
As seen above, the time and space complexity of 1) and 2) is O(1). Then, for each isolated step from 4.1) to 4.4) they're also fixed. The only thing which is not constant is a size of the input message, and so the time complexity for 4) as a whole is O(n). The space complexity is O(1) because there's always a fixed number of temp variables used, and a 1536
- bit variable used in 3) can be re-used for each iteration.
Other SHA-2 based algorithms like SHA-512 or SHA-384 have the same complexity since they're different mostly by the number of iterations in 3.3) or word sizes.
Thereof, time complexity is O(n) and space complexity is O(1).

- 14,139
- 4
- 35
- 65
-
Please, can you provide any citable resource for this? – Udo E. Jul 16 '23 at 11:53