Skip to content

[Bugfix]: utils::sqrt no longer maps negative values to 1#175

Open
PROWLERx15 wants to merge 3 commits intoLFDT-Lockness:cggmp24/mfrom
PROWLERx15:fix-return-value-utils-sqrt
Open

[Bugfix]: utils::sqrt no longer maps negative values to 1#175
PROWLERx15 wants to merge 3 commits intoLFDT-Lockness:cggmp24/mfrom
PROWLERx15:fix-return-value-utils-sqrt

Conversation

@PROWLERx15
Copy link
Copy Markdown

Fixes #57

utils::sqrt used to return 1 when Integer::sqrt_ref() was None (negative input). That hid bad values and made ZK failures harder to trace and debug.

sqrt now returns Option<Integer> and delegates to sqrt_ref(). Call sites in aux key refresh use .expect() for our own modulus and treat None as a fault when verifying another party’s decommitment.

Tests updated accordingly.

Signed-off-by: PROWLERx15 <prowlerx15@gmail.com>
Signed-off-by: PROWLERx15 <prowlerx15@gmail.com>
Comment thread cggmp24/src/key_refresh/aux_only.rs Outdated
epsilon: L::EPSILON,
};
let n_sqrt = utils::sqrt(&N);
let n_sqrt = utils::sqrt(&N).expect("Own modulus N is always positive");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We ban the use of unwrap and expect in our code (except in tests). Here you should instead create a new variant of the key_refresh::Bug enum and return it

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

noted. i've replaced it with Bug::NegativeModulus

Comment thread cggmp24/src/utils.rs Outdated
Comment on lines 150 to 152
pub fn sqrt(x: &Integer) -> Option<Integer> {
x.sqrt_ref()
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This function is currently just a piece of tech debt - it's a remnant from the time when our big integer backend didn't have the sqrt method. You should remove it completely and use sqrt_ref directly. Obviously the test below is also unnecessary anymore

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

i've removed utils::sqrt entirely and replaced it with sqrt_ref(). also removed the test_sqrt since it is no longer needed

Signed-off-by: PROWLERx15 <prowlerx15@gmail.com>
@PROWLERx15 PROWLERx15 requested a review from maurges April 26, 2026 05:29
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.

utils::sqrt should probably return an error for negative values

2 participants