Skip to content

Add validation and tests for discriminant_size_bits parameter#286

Merged
emlowe merged 5 commits into
mainfrom
add-discriminant-validation-tests
Dec 18, 2025
Merged

Add validation and tests for discriminant_size_bits parameter#286
emlowe merged 5 commits into
mainfrom
add-discriminant-validation-tests

Conversation

@hoffmang9
Copy link
Copy Markdown
Member

@hoffmang9 hoffmang9 commented Dec 14, 2025

Fixes #282

  • Add validation to CreateDiscriminant to check for:
    • Positive values (rejects -1, 0)
    • Upper bound of 16384 (rejects 16400)
    • Multiple of 8 requirement
    • Non-empty seed
  • Add comprehensive tests for edge cases:
    • test_discriminant_size_bits_negative: -1 should fail
    • test_discriminant_size_bits_zero: 0 should fail
    • test_discriminant_size_bits_too_large: 16400 should fail
    • test_discriminant_size_bits_valid: 1024 should succeed
  • Fix Python binding to ensure exceptions are properly handled

Note

Add strict input validation to CreateDiscriminant and introduce tests covering invalid and valid discriminant_size_bits cases.

  • Core:
    • Strengthen CreateDiscriminant in src/create_discriminant.h with input validation:
      • Require positive length, cap at 16384, enforce multiple-of-8, and non-empty seed; throw std::invalid_argument on violations.
  • Tests:
    • Add tests/test_discriminant_validation.py with cases for negative, zero, overly large, and valid discriminant_size_bits.

Written by Cursor Bugbot for commit f5abd89. This will update automatically on new commits. Configure here.

@hoffmang9
Copy link
Copy Markdown
Member Author

@BugBot review

@hoffmang9 hoffmang9 force-pushed the add-discriminant-validation-tests branch from 55151a6 to c84381e Compare December 14, 2025 20:30
- Add validation to CreateDiscriminant to check for:
  * Positive values (rejects -1, 0)
  * Upper bound of 16384 (rejects 16400)
  * Multiple of 8 requirement
  * Non-empty seed
- Add comprehensive tests for edge cases:
  * test_discriminant_size_bits_negative: -1 should fail
  * test_discriminant_size_bits_zero: 0 should fail
  * test_discriminant_size_bits_too_large: 16400 should fail
  * test_discriminant_size_bits_valid: 1024 should succeed
- Fix Python binding to ensure exceptions are properly handled
@hoffmang9 hoffmang9 force-pushed the add-discriminant-validation-tests branch from c84381e to 21fa8dd Compare December 14, 2025 20:34
@cmmarslender cmmarslender reopened this Dec 14, 2025
@cmmarslender cmmarslender reopened this Dec 14, 2025
@hoffmang9
Copy link
Copy Markdown
Member Author

cursor review

@cursor
Copy link
Copy Markdown

cursor Bot commented Dec 14, 2025

🚨 Bugbot couldn't run

Bugbot is not enabled for your user on this team.

Ask your team administrator to increase your team's hard limit for Bugbot seats or add you to the allowlist in the Cursor dashboard.

@hoffmang9
Copy link
Copy Markdown
Member Author

cursor review

@cursor
Copy link
Copy Markdown

cursor Bot commented Dec 14, 2025

🚨 Bugbot couldn't run

Bugbot is not enabled for your user on this team.

Ask your team administrator to increase your team's hard limit for Bugbot seats or add you to the allowlist in the Cursor dashboard.

@cmmarslender
Copy link
Copy Markdown
Member

cursor review

@cursor
Copy link
Copy Markdown

cursor Bot commented Dec 14, 2025

🚨 Bugbot couldn't run

Bugbot is not enabled for your user on this team.

Ask your team administrator to increase your team's hard limit for Bugbot seats or add you to the allowlist in the Cursor dashboard.

@hoffmang9
Copy link
Copy Markdown
Member Author

cursor review

@cursor
Copy link
Copy Markdown

cursor Bot commented Dec 14, 2025

🚨 Bugbot couldn't run

Bugbot is not enabled for your user on this team.

Ask your team administrator to increase your team's hard limit for Bugbot seats or add you to the allowlist in the Cursor dashboard.

@cmmarslender
Copy link
Copy Markdown
Member

cursor review

Comment thread src/create_discriminant.h Outdated
@hoffmang9
Copy link
Copy Markdown
Member Author

cursor review

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no bugs!


Comment thread src/create_discriminant.h Outdated
@hoffmang9
Copy link
Copy Markdown
Member Author

cursor review

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no bugs!


@grgalex
Copy link
Copy Markdown

grgalex commented Dec 16, 2025

@hoffmang9 @emlowe Sorry for interjecting, just wanted to add a few thoughts:

I understand that the only downstream client of chiavdf is the chia-blockchain package itself, i.e., both packages are part of the same whole.

Since we are adding these sanity checks in the hot path, they will introduce some runtime overhead.

If this overhead is non-negligible, and since we control the calling locations of chiavdf.create_discriminant, it could make sense to instead turn these checks into API guidelines instead (e.g., "this function expects values of length that are > 0") and/or selectively check values at the calling locations instead of in the function's implementation.

@hoffmang9
Copy link
Copy Markdown
Member Author

I generated a speed-test.py:

import secrets
import chiavdf
import time
import statistics

# Parameters
d_challenge = secrets.token_bytes(10)
d_size_bits = 1024
iterations = 1000

# Store timing results
timings = []

print(f"Running {iterations} iterations...")
for i in range(iterations):
    start_time = time.perf_counter()
    discriminant = chiavdf.create_discriminant(d_challenge, d_size_bits)
    end_time = time.perf_counter()
    
    elapsed = end_time - start_time
    timings.append(elapsed)
    
    # Progress indicator
    if (i + 1) % 100 == 0:
        print(f"Completed {i + 1} iterations...")

# Calculate statistics
mean_time = statistics.mean(timings)
median_time = statistics.median(timings)
min_time = min(timings)
max_time = max(timings)
stdev_time = statistics.stdev(timings)

print(f"\nResults:")
print(f"Mean time:   {mean_time*1000:.4f} ms")
print(f"Median time: {median_time*1000:.4f} ms")
print(f"Min time:    {min_time*1000:.4f} ms")
print(f"Max time:    {max_time*1000:.4f} ms")
print(f"Std Dev:     {stdev_time*1000:.4f} ms")
print(f"Total time:  {sum(timings):.2f} seconds")

Example results for this branch:

Results:
Mean time:   2.6593 ms
Median time: 2.6148 ms
Min time:    2.6018 ms
Max time:    4.6369 ms
Std Dev:     0.1748 ms
Total time:  2.66 seconds

Example results for main at 2ac6d810d28363b5dba7f96061006c6550441c4b

Results:
Mean time:   3.0062 ms
Median time: 2.9567 ms
Min time:    2.9351 ms
Max time:    5.1029 ms
Std Dev:     0.2035 ms   
Total time:  3.01 seconds

This was on an older Intel Mac to try to get the background noise to a minimum - MacBook Pro 13-inch, Four Thunderbolt 3 Ports, 3.1 GHz Dual-Core Intel Core i5, 8GB 2133MHz LPDDR3 running Ventura 13.7.8

After running a lot of iterations, I really don't see any difference in run times of chiavdf.create_discriminant() with the added input validations. It seems to be totally dominated by CPU scheduling and system load.

If you have a better way to validate this, I'm open to an additional approach.

@emlowe
Copy link
Copy Markdown
Contributor

emlowe commented Dec 18, 2025

Yes, these checks have negligible runtime impact.

@emlowe emlowe merged commit 712d2e4 into main Dec 18, 2025
90 of 96 checks passed
@emlowe emlowe deleted the add-discriminant-validation-tests branch December 18, 2025 23:15
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.

GMP overflow crash via create_discriminant function with discriminant_size_bits=0

4 participants