Skip to content

better isSubtreeValid - #7

Open
phbnf wants to merge 1 commit into
mainfrom
IsSubtreeValid
Open

better isSubtreeValid#7
phbnf wants to merge 1 commit into
mainfrom
IsSubtreeValid

Conversation

@phbnf

@phbnf phbnf commented Jun 12, 2026

Copy link
Copy Markdown
Owner

No description provided.

@phbnf

phbnf commented Jun 12, 2026

Copy link
Copy Markdown
Owner Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors subtree validation logic in the proof package, shifting validation checks into isSubtreeValid and optimizing the check using bitwise operations. It also updates the fuzz tests in the testonly package to use this validation. However, the changes introduce a compilation error because isSubtreeValid is unexported in the proof package but called as proof.IsSubtreeValid in the testonly package. The review comments correctly point out this issue and suggest exporting the function by renaming it to IsSubtreeValid and updating its references.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread proof/proof.go
func SubtreeInclusion(index, start, end uint64) (Nodes, error) {
if start >= end {
return Nodes{}, fmt.Errorf("start %d greater than or equal to end %d", start, end)
if err := isSubtreeValid(start, end); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

Rename isSubtreeValid to IsSubtreeValid to match the exported name, as it needs to be exported for use in the testonly package.

Suggested change
if err := isSubtreeValid(start, end); err != nil {
if err := IsSubtreeValid(start, end); err != nil {

Comment thread proof/proof.go
// - all the subtree nodes
// - no extra node to the left of the subtree
// - potentially extra nodes to the right of the subtree
func isSubtreeValid(start, end uint64) error {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

Rename isSubtreeValid to IsSubtreeValid to export it. This is required because the testonly package (which is in a different directory/package) attempts to call proof.IsSubtreeValid in testonly/tree_fuzz_test.go. Note that you will also need to update the reference in proof/verify.go (line 68), which is not currently part of this PR's diff.

Suggested change
func isSubtreeValid(start, end uint64) error {
func IsSubtreeValid(start, end uint64) error {

Comment thread testonly/tree_fuzz_test.go Outdated
}
t.Logf("index=%d, start=%d, end=%d", index, start, end)
if start >= end {
if err := proof.IsSubtreeValid(start, end); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

This call to proof.IsSubtreeValid will fail to compile because isSubtreeValid is currently unexported in the proof package. Please export it by renaming it to IsSubtreeValid in proof/proof.go and updating its references.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors subtree validation logic in the proof package and integrates it into the fuzz tests. Specifically, it moves the start >= end check into isSubtreeValid, optimizes the power-of-two alignment check using bitwise operations, and replaces manual checks in tree_fuzz_test.go with the validation function. However, the reviewer identified a compilation error: the fuzz test attempts to call proof.IsSubtreeValid, but the function is currently unexported as isSubtreeValid in the proof package. The reviewer recommends exporting the function and updating its references to resolve this issue.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread testonly/tree_fuzz_test.go Outdated
}
t.Logf("index=%d, start=%d, end=%d", index, start, end)
if start >= end {
if err := proof.IsSubtreeValid(start, end); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

The function proof.IsSubtreeValid is not exported in the proof package (it is defined as isSubtreeValid with a lowercase i). Since testonly is a separate package, this will cause a compilation error. To resolve this, isSubtreeValid should be exported as IsSubtreeValid in the proof package.

Comment thread proof/proof.go
// - all the subtree nodes
// - no extra node to the left of the subtree
// - potentially extra nodes to the right of the subtree
func isSubtreeValid(start, end uint64) error {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

Rename isSubtreeValid to IsSubtreeValid to export it, allowing external packages like testonly to use it. Note that you will also need to update the reference in proof/verify.go (line 68) which is not part of this PR's diff.

Suggested change
func isSubtreeValid(start, end uint64) error {
func IsSubtreeValid(start, end uint64) error {

Comment thread proof/proof.go
func SubtreeInclusion(index, start, end uint64) (Nodes, error) {
if start >= end {
return Nodes{}, fmt.Errorf("start %d greater than or equal to end %d", start, end)
if err := isSubtreeValid(start, end); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

Update the internal call to use the newly exported IsSubtreeValid function.

Suggested change
if err := isSubtreeValid(start, end); err != nil {
if err := IsSubtreeValid(start, end); err != nil {

@phbnf

phbnf commented Jun 12, 2026

Copy link
Copy Markdown
Owner Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request exports and refactors the subtree validation logic into IsSubtreeValid, optimizing the check with a bitwise operation and simplifying validation across the codebase, including tests and fuzzing. The review feedback suggests aligning the validation order in VerifySubtreeInclusion with SubtreeInclusion for consistency, removing a trailing space in an error message, and correcting a grammatical typo in the bitCeil documentation.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread proof/verify.go Outdated
Comment on lines 65 to 70
if index < start || index >= end {
return fmt.Errorf("index %d out of bounds for subtree [%d, %d)", index, start, end)
}
if err := isSubtreeValid(start, end); err != nil {
if err := IsSubtreeValid(start, end); err != nil {
return fmt.Errorf("subtree invalid: %v", err)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

To be consistent with SubtreeInclusion in proof.go, we should validate the subtree bounds using IsSubtreeValid before checking if the index is out of bounds.

Additionally, please note that the check if start >= end at lines 62-64 becomes redundant because IsSubtreeValid already performs this check, so it should be removed from the beginning of the function.

Suggested change
if index < start || index >= end {
return fmt.Errorf("index %d out of bounds for subtree [%d, %d)", index, start, end)
}
if err := isSubtreeValid(start, end); err != nil {
if err := IsSubtreeValid(start, end); err != nil {
return fmt.Errorf("subtree invalid: %v", err)
}
if err := IsSubtreeValid(start, end); err != nil {
return fmt.Errorf("subtree invalid: %v", err)
}
if index < start || index >= end {
return fmt.Errorf("index %d out of bounds for subtree [%d, %d)", index, start, end)
}

Comment thread proof/proof.go
Comment on lines +239 to 241
if l > uint64(1)<<63 {
return fmt.Errorf("start %d must be 0 when subtree length %d > 1<<63. ", start, l)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

There is a trailing space at the end of the error message string. It should be removed for clean formatting.

Suggested change
if l > uint64(1)<<63 {
return fmt.Errorf("start %d must be 0 when subtree length %d > 1<<63. ", start, l)
}
if l > uint64(1)<<63 {
return fmt.Errorf("start %d must be 0 when subtree length %d > 1<<63.", start, l)
}

Comment thread proof/proof.go Outdated
}

// bitCeil returns the smallest power of 2 larger than n.
// bitCeil returns the smallest power of 2 larger or equal than n.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The phrase 'larger or equal than n' is grammatically incorrect. It should be 'greater than or equal to n' or 'larger than or equal to n'.

Suggested change
// bitCeil returns the smallest power of 2 larger or equal than n.
// bitCeil returns the smallest power of 2 greater than or equal to n.

@phbnf

phbnf commented Jun 12, 2026

Copy link
Copy Markdown
Owner Author

/gemini review

@phbnf

phbnf commented Jun 12, 2026

Copy link
Copy Markdown
Owner Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors subtree validation by exporting the IsSubtreeValid function, consolidating boundary checks inside it, and optimizing the validation logic using bitwise operations. It also updates callers and tests to use this exported function. The review feedback suggests a minor improvement to conform to Go style guidelines by removing a trailing period from an error message.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread proof/proof.go

// special-case large subtree to avoid panic
if l > uint64(1)<<63 {
return fmt.Errorf("start %d must be 0 when subtree length %d > 1<<63", start, l)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

According to the Go style guide, error strings should not end with punctuation or be capitalized, as they are typically wrapped or printed following other context. Consider removing the trailing period.

Suggested change
return fmt.Errorf("start %d must be 0 when subtree length %d > 1<<63", start, l)
return fmt.Errorf("start %d must be 0 when subtree length %d > 1<<63", start, l)

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors subtree validation by exporting isSubtreeValid to IsSubtreeValid and consolidating validation checks across SubtreeInclusion, VerifySubtreeInclusion, and fuzz tests. It also optimizes the validation logic by replacing a modulo operation with a bitwise AND check (start&(bc-1) != 0) and cleans up unused imports in the fuzz tests. There are no review comments, and the changes look solid, so I have no further feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@phbnf
phbnf force-pushed the IsSubtreeValid branch 2 times, most recently from a47d0fd to d14cc61 Compare June 12, 2026 11:53
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