mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-02 22:03:07 +00:00
range check example
This commit is contained in:
parent
af3fa1426e
commit
d6bb5d5dee
@ -16,8 +16,15 @@ A good starting point for how to use Plonky2 for simple applications is the incl
|
||||
|
||||
* [`factorial`](plonky2/examples/factorial.rs): Proving knowledge of 100!
|
||||
* [`fibonacci`](plonky2/examples/fibonacci.rs): Proving knowledge of the hundredth Fibonacci number
|
||||
* [`range_check`](plonky2/examples/range_check.rs): Proving that a field element is in a given range
|
||||
* [`square_root`](plonky2/examples/square_root.rs): Proving knowledge of the square root of a given field element
|
||||
|
||||
To run an example, use
|
||||
|
||||
```sh
|
||||
cargo run --example <example_name>
|
||||
```
|
||||
|
||||
|
||||
## Building
|
||||
|
||||
|
||||
38
plonky2/examples/range_check.rs
Normal file
38
plonky2/examples/range_check.rs
Normal file
@ -0,0 +1,38 @@
|
||||
#![allow(clippy::upper_case_acronyms)]
|
||||
|
||||
use anyhow::Result;
|
||||
use plonky2::field::types::Field;
|
||||
use plonky2::iop::witness::{PartialWitness, WitnessWrite};
|
||||
use plonky2::plonk::circuit_builder::CircuitBuilder;
|
||||
use plonky2::plonk::circuit_data::CircuitConfig;
|
||||
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
|
||||
|
||||
/// An example of using Plonky2 to prove that a given value lies in a given range.
|
||||
fn main() -> Result<()> {
|
||||
const D: usize = 2;
|
||||
type C = PoseidonGoldilocksConfig;
|
||||
type F = <C as GenericConfig<D>>::F;
|
||||
|
||||
let config = CircuitConfig::standard_recursion_config();
|
||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||
|
||||
// The secret value.
|
||||
let value = builder.add_virtual_target();
|
||||
builder.register_public_input(value);
|
||||
|
||||
let log_max = 6;
|
||||
builder.range_check(value, log_max);
|
||||
|
||||
let mut pw = PartialWitness::new();
|
||||
pw.set_target(value, F::from_canonical_usize(42));
|
||||
|
||||
let data = builder.build::<C>();
|
||||
let proof = data.prove(pw)?;
|
||||
|
||||
println!(
|
||||
"Value {} is less than 2^{}",
|
||||
proof.public_inputs[0], log_max,
|
||||
);
|
||||
|
||||
data.verify(proof)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user