-
Notifications
You must be signed in to change notification settings - Fork 0
add: zk plonky2 for sudoku #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe recent update introduces a significant enhancement by setting the Rust toolchain to Changes
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review Status
Actionable comments generated: 2
Configuration used: CodeRabbit UI
Files ignored due to path filters (2)
Cargo.tomlis excluded by:!**/*.tomlzk-plonky2-sudoku/Cargo.tomlis excluded by:!**/*.toml
Files selected for processing (2)
- rust-toolchain (1 hunks)
- zk-plonky2-sudoku/src/lib.rs (1 hunks)
Files skipped from review due to trivial changes (1)
- rust-toolchain
Additional comments: 1
zk-plonky2-sudoku/src/lib.rs (1)
- 1-8: The imports from
plonky2are correctly specified, ensuring that all necessary modules for building and verifying circuits are available. This is crucial for the functionality of thecircuit_builderfunction.
zk-plonky2-sudoku/src/lib.rs
Outdated
| pub fn circuit_builder() { | ||
| const D: usize = 2; | ||
| type C = PoseidonGoldilocksConfig; | ||
| type F = <C as GenericConfig<D>>::F; | ||
|
|
||
| let cfg = CircuitConfig::standard_recursion_config(); | ||
| let mut builder = CircuitBuilder::<F, D>::new(cfg); | ||
|
|
||
| let initial = builder.add_virtual_target(); | ||
| let mut cur_target = initial; | ||
|
|
||
| for i in 1..101 { | ||
| let i_target = builder.constant(F::from_canonical_u32(i)); | ||
| let n_plus_i = builder.add(initial, i_target); | ||
| cur_target = builder.mul(cur_target, n_plus_i); | ||
| } | ||
|
|
||
| builder.register_public_input(initial); | ||
|
|
||
| let mut pw = PartialWitness::new(); | ||
| pw.set_target(initial, F::ONE); | ||
|
|
||
| let data = builder.build::<C>(); | ||
| let proof = data.prove(pw).unwrap(); | ||
|
|
||
| println!( | ||
| "Factorial starting at {} is {}", | ||
| proof.public_inputs[0], proof.public_inputs[1] | ||
| ); | ||
|
|
||
| data.verify(proof).unwrap() | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The circuit_builder function demonstrates a practical application of zk-SNARKs by setting up a circuit to calculate factorials. However, there are several areas that could be improved or need attention:
-
Error Handling (Lines 34, 41): The use of
.unwrap()for handling the result ofproveandverifymethods is risky. In production code, it's better to handle errors gracefully to avoid panics. Consider usingmatchorif letto handle potential errors. -
Public Input Registration (Line 28): It's good practice to document why certain values are registered as public inputs, especially in the context of zk-SNARKs where the distinction between public and private inputs is crucial.
-
Efficiency (Lines 22-26): The loop calculates factorials in a straightforward manner. While this is logically correct, consider if there's a more efficient way to perform this calculation, especially in the context of zk-SNARKs where circuit size and complexity can significantly impact performance.
-
Print Statement (Lines 36-39): While the print statement is useful for debugging or demonstration purposes, it's generally not advisable to have such statements in production code, especially in cryptographic contexts where outputs might be sensitive.
-
Verification (Line 41): The result of
data.verify(proof)is unwrapped but not used. It would be beneficial to either use this result in a meaningful way or at least check that the verification succeeds and handle the case where it does not.
Consider addressing these points to improve error handling, efficiency, and clarity of the code.
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn ensure_facts() { | ||
| circuit_builder() | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test ensure_facts simply calls the circuit_builder function without any assertions. This does not effectively test the functionality of the circuit_builder or the correctness of the circuit setup. Consider adding assertions to verify the expected outcomes, such as checking if the proof is valid or if the public inputs match expected values.
Improve the test by adding meaningful assertions to verify the functionality and correctness of the circuit_builder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review Status
Actionable comments generated: 0
Configuration used: CodeRabbit UI
Files ignored due to path filters (1)
zk-plonky2-sudoku/Cargo.tomlis excluded by:!**/*.toml
Files selected for processing (1)
- zk-plonky2-sudoku/src/lib.rs (1 hunks)
Additional comments: 2
zk-plonky2-sudoku/src/lib.rs (2)
- 15-41: The
circuit_builderfunction constructs a Plonk circuit for verifying Sudoku constraints, which is a crucial part of integrating zk-SNARKs into the Sudoku application. A few points to consider:
Documentation: It would be beneficial to add more detailed comments explaining the logic behind the constraint checks (lines 30-34). This would help future maintainers understand the purpose and functionality of the circuit.
Error Handling: Currently, the function returns a
Result<()>, but there are no operations within the function that could result in an error. If future modifications to the function introduce error-prone operations, ensure proper error handling is implemented to avoid unwrapping errors directly.Debugging Code: The
println!statement on line 39 seems to be for debugging purposes. Consider removing it or guarding it with a feature flag or debug configuration to avoid cluttering the output in production environments.Overall, the function is well-structured and serves its purpose. Enhancing documentation and considering these points would further improve the code quality.
- 79-82: The test
ensure_factscurrently only calls thecircuit_builderfunction without verifying its functionality or correctness. To make this test more effective, consider adding assertions that check specific outcomes of thecircuit_builderfunction. For example, you could verify that the circuit is constructed as expected or that certain constraints are correctly applied. This would ensure that the function not only runs without errors but also behaves as intended.
Summary by CodeRabbit
nightly.