Skip to content

fix Calibrate edge cases, improve test coverage#31

Merged
elithrar merged 1 commit intomainfrom
code-review-fixes
Feb 10, 2026
Merged

fix Calibrate edge cases, improve test coverage#31
elithrar merged 1 commit intomainfrom
code-review-fixes

Conversation

@elithrar
Copy link
Owner

Code review surfaced several bugs in Calibrate and gaps in test coverage.

Calibrate fixes:

  • division-by-zero panic when int64(dur) / int64(p.P) truncates to 0 in the optimal-P calculation
  • P decrementing to 0 when constraints are unsatisfiable (timeout already exceeded at P=1) — now returns P=1 instead of ErrInvalidParams
  • negative memMiBytes not rejected
  • godoc now documents that R is always set to 8

GenerateFromPassword: validate params before consuming entropy from crypto/rand

Test fixes:

  • TestGenerateFromPassword now asserts that invalid params produce errors — 9 of 13 pass: false cases were dead code
  • TestKnownHash pins RFC 7914 Section 12 vector 3 (pleaseletmein/SodiumChloride/N=16384/r=8/p=1/dkLen=64) as a regression test
  • TestHashFormat validates the N$R$P$hexsalt$hexdk output structure
  • TestCheck directly exercises Params.Check() with boundary values (N=2, N=3, SaltLen=7/8, DKLen=15/16)
  • TestCost expanded to cover non-default params
  • TestCompareHashAndPassword covers nil/empty inputs
  • TestCalibrate timing tolerance widened (timeout/4 to 3*timeout) to reduce CI flakiness

Also bumps go.mod to go 1.21 to match the CI matrix, and gitignores coverage.out.

Base automatically changed from add-agents-md to main February 10, 2026 15:52
Address findings from code review:

- validate params before generating salt in GenerateFromPassword
- guard division-by-zero in Calibrate when durPerP truncates to 0
- prevent Calibrate from decrementing P to 0 when constraints are
  unsatisfiable (return P=1 instead of erroring)
- reject negative memMiBytes in Calibrate
- document that Calibrate always sets R=8

Test improvements:

- fix TestGenerateFromPassword: assert invalid params produce errors
  (9 of 13 test cases were previously dead code)
- add TestHashFormat: validate N$R$P$hexsalt$hexdk structure
- add TestKnownHash: RFC 7914 Section 12 vector 3 as regression pin
- add TestCheck: direct Params.Check() test with boundary values
- expand TestCost to cover non-default params
- add nil/empty input coverage to TestCompareHashAndPassword
- widen TestCalibrate timing tolerance to reduce CI flakiness

Also bump go.mod to go 1.21 (matches CI matrix) and gitignore
coverage.out.
@elithrar elithrar merged commit 3b9f1a2 into main Feb 10, 2026
5 checks passed
@elithrar elithrar deleted the code-review-fixes branch February 10, 2026 16:09
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