Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go: Refactor for clarity and performance #34

Merged
merged 4 commits into from
Feb 22, 2023
Merged

go: Refactor for clarity and performance #34

merged 4 commits into from
Feb 22, 2023

Conversation

pjbgf
Copy link
Owner

@pjbgf pjbgf commented Feb 22, 2023

The core of the algorihm has been refactored for improved clarity. Magic numbers
were replaced by constants, and the code was reorganised to make it easier to
understand.

Previously the rehashing code for blocks were a collision was identified, had a
custom code with a duplicated logic from the core algorithm. This has now being
simplified, resulting in less code and a logic more aligned with what a goasm
implementation would look like.

When it was first implemented, all fields storing state needed for the collision
detection were kept in the digest struct. That was unnecessary, as such data
is only required during block processing, and therefore this commit removes them.

The storage of pre-step compression state has been optimised to use less memory.
The collision detection algorithm only need to access this data for specific
rounds of the SHA1 process which are linked to existing dvs, which at present
is for rounds: 0, 58 and 65.

If more dvs were to be added, targeting different rounds, those would need to
to be stored in memory as well. Note that the original algorithmn stores into
memory the data for the 80 rounds.

Likewise, during recompression, the original algorithm had additional steps to
account for dvs that did not make it into the final dvs set. Therefore, this has
been removed from the code.

The impact in sec/op and process B/s can be seen below:

goos: linux
goarch: amd64
pkg: github.com/pjbgf/sha1cd/test
cpu: Intel(R) Core(TM) i9-10885H CPU @ 2.40GHz
                                │       before        │                  after                     │
                                │       sec/op        │       sec/op         vs base               │
Hash8Bytes/sha1cd-16                    1245.0n ± ∞ ¹          387.6n ± ∞ ¹  -68.87% (p=0.008 n=5)
Hash8Bytes/sha1cd_cgo-16                 1.135µ ± ∞ ¹          1.061µ ± ∞ ¹        ~ (p=0.421 n=5)
Hash320Bytes/sha1cd-16                   6.140µ ± ∞ ¹          2.054µ ± ∞ ¹  -66.55% (p=0.008 n=5)
Hash320Bytes/sha1cd_cgo-16               2.208µ ± ∞ ¹          1.963µ ± ∞ ¹  -11.10% (p=0.008 n=5)
Hash1K/sha1cd-16                        16.787µ ± ∞ ¹          5.711µ ± ∞ ¹  -65.98% (p=0.008 n=5)
Hash1K/sha1cd_cgo-16                     4.110µ ± ∞ ¹          4.134µ ± ∞ ¹        ~ (p=0.690 n=5)
Hash8K/sha1cd-16                        126.02µ ± ∞ ¹          43.38µ ± ∞ ¹  -65.58% (p=0.008 n=5)
Hash8K/sha1cd_cgo-16                     23.95µ ± ∞ ¹          24.13µ ± ∞ ¹        ~ (p=0.548 n=5)
HashWithCollision/sha1cd-16                                    6.484µ ± ∞ ¹
HashWithCollision/sha1cd_cgo-16                                4.458µ ± ∞ ¹
geomean                                  147.3n                171.5n        -28.81%
¹ need >= 6 samples for confidence interval at level 0.95

                                │   before        │              after                 │
                                │       B/s       │      B/s        vs base                │
Hash8Bytes/sha1cd-16                6.123Mi ± ∞ ¹   19.684Mi ± ∞ ¹  +221.50% (p=0.008 n=5)
Hash8Bytes/sha1cd_cgo-16            6.723Mi ± ∞ ¹    7.191Mi ± ∞ ¹         ~ (p=0.421 n=5)
Hash320Bytes/sha1cd-16              49.70Mi ± ∞ ¹   148.55Mi ± ∞ ¹  +198.93% (p=0.008 n=5)
Hash320Bytes/sha1cd_cgo-16          138.2Mi ± ∞ ¹    155.5Mi ± ∞ ¹   +12.46% (p=0.008 n=5)
Hash1K/sha1cd-16                    58.17Mi ± ∞ ¹   170.99Mi ± ∞ ¹  +193.93% (p=0.008 n=5)
Hash1K/sha1cd_cgo-16                237.6Mi ± ∞ ¹    236.2Mi ± ∞ ¹         ~ (p=0.690 n=5)
Hash8K/sha1cd-16                    62.00Mi ± ∞ ¹   180.09Mi ± ∞ ¹  +190.48% (p=0.008 n=5)
Hash8K/sha1cd_cgo-16                326.2Mi ± ∞ ¹    323.7Mi ± ∞ ¹         ~ (p=0.548 n=5)
HashWithCollision/sha1cd-16                          94.14Mi ± ∞ ¹
HashWithCollision/sha1cd_cgo-16                      136.9Mi ± ∞ ¹
geomean                             103.3Mi          144.8Mi         +45.92%

Signed-off-by: Paulo Gomes <pjbgf@linux.com>
Optimises type selection for decreased memory consumption in goasm,
and for easier calculations for memory mapping.

Signed-off-by: Paulo Gomes <pjbgf@linux.com>
The core of the algorihm has been refactored for improved clarity. Magic numbers
were replaced by constants, and the code was reorganised to make it easier to
understand.

Previously the rehashing code for blocks were a collision was identified, had a
custom code with a duplicated logic from the core algorithm. This has now being
simplified, resulting in less code and a logic more aligned with what a goasm
implementation would look like.

When it was first implemented, all fields storing state needed for the collision
detection were kept in the digest struct. That was unnecessary, as such data
is only required during block processing, and therefore this commit removes them.

The storage of pre-step compression state has been optimised to use less memory.
The collision detection algorithm only need to access this data for specific
rounds of the SHA1 process which are linked to existing dvs, which at present
is for rounds: 0, 58 and 65.

If more dvs were to be added, targeting different rounds, those would need to
to be stored in memory as well. Note that the original algorithmn stores into
memory the data for the 80 rounds.

Signed-off-by: Paulo Gomes <pjbgf@linux.com>
Signed-off-by: Paulo Gomes <pjbgf@linux.com>
@pjbgf pjbgf merged commit 3b4a158 into main Feb 22, 2023
@pjbgf pjbgf deleted the generic-opts branch February 22, 2023 00:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant