-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsroot0.rs
More file actions
67 lines (57 loc) · 1.89 KB
/
sroot0.rs
File metadata and controls
67 lines (57 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
halo2curves::bn256::Fr as Fp,
plonk::{Advice, Circuit, Column, ConstraintSystem, ErrorFront, Instance, Selector},
poly::Rotation,
};
#[derive(Debug, Default, Clone, Copy)]
pub struct SquareRootCircuit {
pub root: Value<Fp>,
}
#[derive(Clone, Debug)]
pub struct SquareRootConfig {
advice: Column<Advice>,
instance: Column<Instance>,
myselector: Selector,
}
impl Circuit<Fp> for SquareRootCircuit {
type Config = SquareRootConfig;
type FloorPlanner = SimpleFloorPlanner;
fn without_witnesses(&self) -> Self {
Self::default()
}
fn configure(meta: &mut ConstraintSystem<Fp>) -> Self::Config {
let advice = meta.advice_column();
let instance = meta.instance_column();
meta.enable_equality(advice);
meta.enable_equality(instance);
let myselector = meta.selector();
meta.create_gate("square", |meta| {
let s = meta.query_selector(myselector);
let root = meta.query_advice(advice, Rotation::cur());
let square = meta.query_advice(advice, Rotation::next());
vec![s * (root.clone() * root - square)]
});
SquareRootConfig {
advice,
instance,
myselector,
}
}
fn synthesize(
&self,
config: Self::Config,
mut layouter: impl Layouter<Fp>,
) -> Result<(), ErrorFront> {
let out = layouter.assign_region(
|| "main region",
|mut region| {
config.myselector.enable(&mut region, 0)?;
region.assign_advice(|| "a", config.advice, 0, || self.root)?;
region.assign_advice(|| "square", config.advice, 1, || self.root * self.root)
},
)?;
layouter.constrain_instance(out.cell(), config.instance, 0)?;
Ok(())
}
}