Skip to content

Fix divergent behavior in integer backends - #49

Merged
survived merged 14 commits into
mfrom
fix-divergent-behavior
Oct 21, 2026
Merged

survived merged 14 commits into
mfrom
fix-divergent-behavior

Conversation

@survived

@survived survived commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

I used Claude to analyze if both integer backends exhibit the same behavior, and it found methods that diverge.

PR fixes divergent behavior. Review commit-by-commit. See commit message. Note: in some places, I add panics into the code, but I believe it's justified (but ideally, we probably want to change methods to return None) (this was changed: methods return error if input is invalid)

Also see the changelog.

Previous impl went into infinite loop with negative numbers

Signed-off-by: Denis Varlakov <cordial.egg6523@fastmail.com>
rug backend used to allow whitespaces in the integer encoding when
provided to `Integer::from_str_radix`, even if the whitespace is in
the middle of the number.

This commit forbids any characters that are not ascii alphanum in the
integer encoding.

Signed-off-by: Denis Varlakov <cordial.egg6523@fastmail.com>
@survived
survived force-pushed the fix-divergent-behavior branch 2 times, most recently from 5cb7dd6 to 6f0ad9f Compare October 16, 2026 15:36
@survived
survived marked this pull request as ready for review October 16, 2026 15:38
@survived
survived requested a review from maurges October 16, 2026 15:38
Comment thread src/backend/rug.rs
Comment on lines +79 to +90
// rug accepts strings that have whitespaces in them, integer is parsed while
// ignoring whitespaces, even in the middle of the number. This behavior is
// different from num-bigint backend, and it is strange. We manually restrict
// strings to be ascii alphanum
if !s
.chars()
.enumerate()
.skip_while(|&(i, c)| i == 0 && (c == '+' || c == '-'))
.all(|(_, c)| c.is_ascii_alphanumeric())
{
return None;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure we actually care about this change. It's not like we want our backends to be exactly identical everywhere, just give the same numerical operations and serialize to the same format. If the deserialization of one is more permissive than another - that doesn't concern our usage

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It does concern our usage if we have signers with different backends running in the same cluster. In this case, malicious party may send a broadcast message which will cause one subset of parties to abort (cuz num-bigint doesn't accept whitespaces) and the other subset of signers will continue the protocol.

Comment thread src/backend/num_bigint.rs Outdated
@survived
survived force-pushed the fix-divergent-behavior branch from dcac262 to c86601c Compare October 16, 2026 20:00
Signed-off-by: Denis Varlakov <cordial.egg6523@fastmail.com>
Signed-off-by: Denis Varlakov <cordial.egg6523@fastmail.com>
Signed-off-by: Denis Varlakov <cordial.egg6523@fastmail.com>
Signed-off-by: Denis Varlakov <cordial.egg6523@fastmail.com>
@survived
survived marked this pull request as draft October 19, 2026 11:52
Signed-off-by: Denis Varlakov <cordial.egg6523@fastmail.com>
Signed-off-by: Denis Varlakov <cordial.egg6523@fastmail.com>
Signed-off-by: Denis Varlakov <cordial.egg6523@fastmail.com>
Signed-off-by: Denis Varlakov <cordial.egg6523@fastmail.com>
Current implementation panics if n == 0, and hangs in infinite loop if n == 1

Signed-off-by: Denis Varlakov <cordial.egg6523@fastmail.com>
Signed-off-by: Denis Varlakov <cordial.egg6523@fastmail.com>
@survived
survived force-pushed the fix-divergent-behavior branch from c86601c to 90dc666 Compare October 19, 2026 12:21
@survived
survived marked this pull request as ready for review October 19, 2026 12:25
@survived
survived requested a review from maurges October 19, 2026 12:25
Signed-off-by: Denis Varlakov <cordial.egg6523@fastmail.com>
Signed-off-by: Denis Varlakov <cordial.egg6523@fastmail.com>
@survived
survived merged commit f560838 into m Oct 21, 2026
17 checks passed
@survived
survived deleted the fix-divergent-behavior branch October 21, 2026 09:20
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.

2 participants