Skip to content

feat(puzzle): generalize key_range#30

Merged
oritwoen merged 2 commits into
mainfrom
feat/generalize-key-range
Dec 31, 2025
Merged

feat(puzzle): generalize key_range#30
oritwoen merged 2 commits into
mainfrom
feat/generalize-key-range

Conversation

@oritwoen
Copy link
Copy Markdown
Owner

Summary

Move key_range() and key_range_big() from b1000 module to Puzzle struct, making them available for any puzzle with a known bit range.

Closes #20

Changes

  • Add Puzzle::key_range() returning Option<RangeInclusive<u128>> (bits 1-128)
  • Add Puzzle::key_range_big() returning Option<(BigUint, BigUint)> (bits 1-256)
  • Remove b1000::key_range() and b1000::key_range_big() (breaking change)
  • Add build-time validation: bits field must match private_key for solved puzzles
  • Update CLI cmd_range() and print_puzzle_detail_table() to use new methods
  • Add comprehensive tests for new methods

Breaking Change

// Before
let range = b1000::key_range(66).unwrap();

// After
let p = b1000::get(66).unwrap();
let range = p.key_range().unwrap();

Testing

  • 40/40 tests pass
  • Clippy clean
  • CLI verified: boha range 66, boha show b1000/66

Co-Authored-By: Aei aei@oad.earth

- Add Puzzle::key_range() for bits 1-128 (u128)
- Add Puzzle::key_range_big() for bits 1-256 (BigUint)
- Remove b1000::key_range() and b1000::key_range_big()
- Add build-time validation: bits must match private_key
- Update CLI to use new Puzzle methods
- Add tests for new methods

Closes #20
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Dec 31, 2025

Greptile Summary

Moved key_range() and key_range_big() methods from b1000 module to Puzzle struct, making key range calculation available for any puzzle with a known bits field.

Key Changes:

  • Added Puzzle::key_range() returning Option<RangeInclusive<u128>> for bits 1-128
  • Added Puzzle::key_range_big() returning Option<(BigUint, BigUint)> for bits 1-256
  • Removed b1000::key_range() and b1000::key_range_big() (breaking change)
  • Added build-time validation to verify bits field matches actual private_key bit length for solved puzzles
  • Updated CLI commands (cmd_range, print_puzzle_detail_table) to use new Puzzle methods
  • Added comprehensive test coverage for new methods including edge cases

Issues Found:

  • Minor formatting bug in build.rs:118 where error message duplicates puzzle.bits instead of showing derived_bits

Confidence Score: 4/5

  • Safe to merge with one minor syntax fix in error message formatting
  • Well-tested refactoring with comprehensive test coverage (40/40 tests passing). The only issue is a minor formatting bug in an assertion message that doesn't affect functionality but should be fixed for debuggability
  • build.rs needs the error message fix before merge

Important Files Changed

Filename Overview
build.rs Added private_key validation with bits field; minor formatting issue in error message
src/puzzle.rs Moved key_range methods from b1000 to Puzzle struct, clean implementation
src/cli.rs Updated to use Puzzle::key_range_big() method; improved error handling in cmd_range

Sequence Diagram

sequenceDiagram
    participant User
    participant CLI as CLI (cmd_range/show)
    participant B1000 as b1000::get()
    participant Puzzle as Puzzle struct
    participant KeyRange as key_range_big()
    
    User->>CLI: Request puzzle info
    CLI->>B1000: get(puzzle_number)
    B1000-->>CLI: Return &Puzzle
    CLI->>Puzzle: Access bits field
    Puzzle-->>CLI: Some(bits)
    CLI->>KeyRange: p.key_range_big()
    KeyRange->>KeyRange: Check bits.is_some()
    KeyRange->>KeyRange: Validate 1..=256 range
    KeyRange->>KeyRange: Calculate BigUint range
    KeyRange-->>CLI: Some((start, end))
    CLI-->>User: Display range with address
Loading

Copy link
Copy Markdown

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment thread build.rs
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 8 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="src/puzzle.rs">

<violation number="1" location="src/puzzle.rs:143">
P1: Overflow when `bits == 128`: shifting `1u128 &lt;&lt; 128` is undefined behavior (panic in debug, wrap in release). Handle the edge case explicitly since `u128::MAX` is the correct value for a 128-bit range.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread src/puzzle.rs Outdated
When bits == 128, the expression (1u128 << 128) overflows.
Use u128::MAX directly for this edge case.

Add test coverage for the bits == 128 boundary condition.
@oritwoen oritwoen self-assigned this Dec 31, 2025
@oritwoen oritwoen merged commit 84be99a into main Dec 31, 2025
3 checks passed
@oritwoen oritwoen deleted the feat/generalize-key-range branch December 31, 2025 17:37
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.

Generalize bits and key_range to all puzzles

1 participant