Skip to content

Commit

Permalink
Merge pull request #19 from itzmeanjan/reset-hasher-state
Browse files Browse the repository at this point in the history
Reset API for hasher state; Test using google-test
  • Loading branch information
itzmeanjan committed Jul 16, 2023
2 parents a90d0af + e7154f3 commit e529767
Show file tree
Hide file tree
Showing 19 changed files with 694 additions and 643 deletions.
17 changes: 16 additions & 1 deletion .github/workflows/test_ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test SHA3 using CI
name: Test SHA3 Hash and Extendable Output Functions using CI

on:
push:
Expand All @@ -17,6 +17,21 @@ jobs:
run: |
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 9
- name: Get CMake
run: sudo apt-get install cmake
- name: Setup Google-Test
run: |
pushd ~
git clone https://github.com/google/googletest.git -b v1.13.0
pushd googletest
mkdir build
pushd build
cmake .. -DBUILD_GMOCK=OFF
make
sudo make install
popd
popd
popd
- name: Execute Tests
run: make
- name: Cleanup
Expand Down
25 changes: 22 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,29 @@ I_FLAGS = -I ./include

all: test

tests/a.out: tests/main.cpp include/*.hpp include/tests/*.hpp
$(CXX) $(CXX_FLAGS) $(WARN_FLAGS) $(OPT_FLAGS) $(I_FLAGS) $< -o $@
tests/test_sha3_224.o: tests/test_sha3_224.cpp include/*.hpp
$(CXX) $(CXX_FLAGS) $(WARN_FLAGS) $(OPT_FLAGS) $(I_FLAGS) -c $< -o $@

test: tests/a.out
tests/test_sha3_256.o: tests/test_sha3_256.cpp include/*.hpp
$(CXX) $(CXX_FLAGS) $(WARN_FLAGS) $(OPT_FLAGS) $(I_FLAGS) -c $< -o $@

tests/test_sha3_384.o: tests/test_sha3_384.cpp include/*.hpp
$(CXX) $(CXX_FLAGS) $(WARN_FLAGS) $(OPT_FLAGS) $(I_FLAGS) -c $< -o $@

tests/test_sha3_512.o: tests/test_sha3_512.cpp include/*.hpp
$(CXX) $(CXX_FLAGS) $(WARN_FLAGS) $(OPT_FLAGS) $(I_FLAGS) -c $< -o $@

tests/test_shake128.o: tests/test_shake128.cpp include/*.hpp
$(CXX) $(CXX_FLAGS) $(WARN_FLAGS) $(OPT_FLAGS) $(I_FLAGS) -c $< -o $@

tests/test_shake256.o: tests/test_shake256.cpp include/*.hpp
$(CXX) $(CXX_FLAGS) $(WARN_FLAGS) $(OPT_FLAGS) $(I_FLAGS) -c $< -o $@

tests/test.out: tests/test_sha3_224.o tests/test_sha3_256.o tests/test_sha3_384.o tests/test_sha3_512.o \
tests/test_shake128.o tests/test_shake256.o
$(CXX) $(OPT_FLAGS) $^ -lgtest -lgtest_main -o $@

test: tests/test.out
./$<

clean:
Expand Down
50 changes: 39 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ $ cmake --version
cmake version 3.25.1
```

- For testing SHA3 algorithms, you need to globally install `google-test` library and headers. Follow [this](https://github.com/google/googletest/tree/main/googletest#standalone-cmake-project) guide if you haven't installed it yet.
- For benchmarking SHA3 algorithms, targeting CPU systems, `google-benchmark` library and headers are required to be installed system-wide. Follow [this](https://github.com/google/benchmark#installation) guide if you don't have it installed yet.
- If you are on a machine running GNU/Linux kernel and you want to obtain following (see list below), when benchmarking SHA3 algorithms, you should consider building `google-benchmark` library yourself with libPFM support, following [this](https://gist.github.com/itzmeanjan/05dc3e946f635d00c5e0b21aae6203a7) step-by-step guide. Find more about libPFM @ https://perfmon2.sourceforge.net.
1) CPU cycle count.
Expand All @@ -69,21 +70,48 @@ For ensuring that SHA3 hash function and extendable output function implementati
I also test correctness of

- Incremental message absorption property of SHA3 hash functions and Xofs.
- Output squeezing property of SHA3 Xofs.
- Incremental output squeezing property of SHA3 Xofs.

Issue following command for running all the test cases.

```bash
make

[test] SHA3-{224,256,384,512} incremental absorption
[test] SHAKE{128,256} incremental absorption and squeezing
[test] SHA3-224 K(nown) A(nswer) T(ests)
[test] SHA3-256 K(nown) A(nswer) T(ests)
[test] SHA3-384 K(nown) A(nswer) T(ests)
[test] SHA3-512 K(nown) A(nswer) T(ests)
[test] Shake128 Xof K(nown) A(nswer) T(ests)
[test] Shake256 Xof K(nown) A(nswer) T(ests)
$ make -j8

[==========] Running 12 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 8 tests from Sha3Hashing
[ RUN ] Sha3Hashing.Sha3_224IncrementalAbsorption
[ OK ] Sha3Hashing.Sha3_224IncrementalAbsorption (1 ms)
[ RUN ] Sha3Hashing.Sha3_224KnownAnswerTests
[ OK ] Sha3Hashing.Sha3_224KnownAnswerTests (2 ms)
[ RUN ] Sha3Hashing.Sha3_256IncrementalAbsorption
[ OK ] Sha3Hashing.Sha3_256IncrementalAbsorption (1 ms)
[ RUN ] Sha3Hashing.Sha3_256KnownAnswerTests
[ OK ] Sha3Hashing.Sha3_256KnownAnswerTests (2 ms)
[ RUN ] Sha3Hashing.Sha3_384IncrementalAbsorption
[ OK ] Sha3Hashing.Sha3_384IncrementalAbsorption (1 ms)
[ RUN ] Sha3Hashing.Sha3_384KnownAnswerTests
[ OK ] Sha3Hashing.Sha3_384KnownAnswerTests (2 ms)
[ RUN ] Sha3Hashing.Sha3_512IncrementalAbsorption
[ OK ] Sha3Hashing.Sha3_512IncrementalAbsorption (2 ms)
[ RUN ] Sha3Hashing.Sha3_512KnownAnswerTests
[ OK ] Sha3Hashing.Sha3_512KnownAnswerTests (3 ms)
[----------] 8 tests from Sha3Hashing (17 ms total)

[----------] 4 tests from Sha3Xof
[ RUN ] Sha3Xof.Shake128IncrementalAbsorptionAndSqueezing
[ OK ] Sha3Xof.Shake128IncrementalAbsorptionAndSqueezing (971 ms)
[ RUN ] Sha3Xof.Shake128KnownAnswerTests
[ OK ] Sha3Xof.Shake128KnownAnswerTests (2 ms)
[ RUN ] Sha3Xof.Shake256IncrementalAbsorptionAndSqueezing
[ OK ] Sha3Xof.Shake256IncrementalAbsorptionAndSqueezing (1060 ms)
[ RUN ] Sha3Xof.Shake256KnownAnswerTests
[ OK ] Sha3Xof.Shake256KnownAnswerTests (2 ms)
[----------] 4 tests from Sha3Xof (2038 ms total)

[----------] Global test environment tear-down
[==========] 12 tests from 2 test suites ran. (2055 ms total)
[ PASSED ] 12 tests.
```

## Benchmarking
Expand Down
4 changes: 4 additions & 0 deletions include/sha3_224.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ struct sha3_224
squeezed = true;
}
}

// Reset the internal state of the SHA3-224 hasher, now it can again be used
// for another absorb->finalize->squeeze cycle.
inline void reset() { std::memset(this, 0, sizeof(*this)); }
};

}
4 changes: 4 additions & 0 deletions include/sha3_256.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ struct sha3_256
squeezed = true;
}
}

// Reset the internal state of the SHA3-256 hasher, now it can again be used
// for another absorb->finalize->squeeze cycle.
inline void reset() { std::memset(this, 0, sizeof(*this)); }
};

}
4 changes: 4 additions & 0 deletions include/sha3_384.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ struct sha3_384
squeezed = true;
}
}

// Reset the internal state of the SHA3-384 hasher, now it can again be used
// for another absorb->finalize->squeeze cycle.
inline void reset() { std::memset(this, 0, sizeof(*this)); }
};

}
4 changes: 4 additions & 0 deletions include/sha3_512.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ struct sha3_512
squeezed = true;
}
}

// Reset the internal state of the SHA3-512 hasher, now it can again be used
// for another absorb->finalize->squeeze cycle.
inline void reset() { std::memset(this, 0, sizeof(*this)); }
};

}
4 changes: 4 additions & 0 deletions include/shake128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ struct shake128
sponge::squeeze<RATE>(state, squeezable, dig, dlen);
}
}

// Reset the internal state of the Shake128-Xof hasher, now it can again be
// used for another absorb->finalize->squeeze cycle.
inline void reset() { std::memset(this, 0, sizeof(*this)); }
};

}
4 changes: 4 additions & 0 deletions include/shake256.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ struct shake256
sponge::squeeze<RATE>(state, squeezable, dig, dlen);
}
}

// Reset the internal state of the Shake256-Xof hasher, now it can again be
// used for another absorb->finalize->squeeze cycle.
inline void reset() { std::memset(this, 0, sizeof(*this)); }
};

}
Loading

0 comments on commit e529767

Please sign in to comment.