Skip to content

Conversation

@apoelstra
Copy link
Collaborator

The div_mod_128_64 jet has preconditions and shortcuts its operation if these are not met. To benchmark correctly we need to sample according to those preconditions.

@apoelstra apoelstra force-pushed the 2025-11/benchmark-divmod branch 2 times, most recently from f7adad0 to 0f6cf69 Compare November 21, 2025 22:25
@apoelstra apoelstra marked this pull request as ready for review November 21, 2025 22:41
Copy link
Collaborator Author

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

On 0f6cf69 successfully ran local tests

@apoelstra
Copy link
Collaborator Author

The new distribution is consistently slower than the uniform one. But all results are in a 40-60ns range; I see maybe a 10% slowdown rather than the 2x (or maybe 4x) that I would naively expect.

But I added printfs to the C code to confirm that with the new distribution we are no longer hitting the "shortcut" case, while with the uniform distribution we are. So this is working. I think this is just a case where the marshalling cost is overwhelming the actual computation cost.


// To implement this, we sample two random 64-bit strings, with one of them
// having its high bit forced to 1. The higher of the two (which will always
// have its high bit 1) is ah, and the other one is b.
Copy link
Contributor

Choose a reason for hiding this comment

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

The higher of the two is b.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

@apoelstra apoelstra force-pushed the 2025-11/benchmark-divmod branch from 0f6cf69 to 9dc53e8 Compare November 21, 2025 22:59
}
unreachable!("if we get here, two uniform 63-bit samples were exactly equal")
} else {
panic!("invalid distribution {} for BIP 340 signature", dist)
Copy link
Contributor

Choose a reason for hiding this comment

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

Bad message.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

@apoelstra apoelstra force-pushed the 2025-11/benchmark-divmod branch from 9dc53e8 to 8cb7e03 Compare November 21, 2025 23:02
@apoelstra
Copy link
Collaborator Author

cc @canndrew

}

fn distribution_name(&self, dist: usize) -> String {
if dist == 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't seem to match the implementation of sample below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

lol oops, I had crippled sample to help with printf debugging. Good find!

@canndrew
Copy link
Collaborator

LGTM other than the implementation of distribution_name.

@apoelstra apoelstra force-pushed the 2025-11/benchmark-divmod branch from 8cb7e03 to 888115c Compare November 25, 2025 18:27
@apoelstra
Copy link
Collaborator Author

@canndrew can you re-review the change?

@canndrew
Copy link
Collaborator

ACK 888115c

@apoelstra apoelstra merged commit b80452a into BlockstreamResearch:master Nov 26, 2025
24 checks passed
@apoelstra apoelstra deleted the 2025-11/benchmark-divmod branch November 26, 2025 16:13
apoelstra added a commit to apoelstra/rust-simplicity that referenced this pull request Nov 26, 2025
888115c benches: fix divmod128_64 distribution (Andrew Poelstra)

Pull request description:

  The `div_mod_128_64` jet has preconditions and shortcuts its operation if these are not met. To benchmark correctly we need to sample according to those preconditions.


ACKs for top commit:
  canndrew:
    ACK 888115c


Tree-SHA512: a9ddc32def17427bf2f28b4d8d41246bac1b1c71e97e8e3615797bf2e97ed728c5163427bd099a71d57004febebe2c9ba8768e4fe62722077a1f9b55b6e69778
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.

3 participants