-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsroot1.rs
More file actions
97 lines (84 loc) · 2.79 KB
/
sroot1.rs
File metadata and controls
97 lines (84 loc) · 2.79 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
halo2curves::bn256::Fr as Fp,
plonk::{
Advice, Circuit, Column, ConstraintSystem, ErrorFront, Instance, Selector, TableColumn,
},
poly::Rotation,
};
#[derive(Debug, Default, Clone, Copy)]
pub struct SquareRootCircuit {
pub root: Value<Fp>,
}
#[derive(Clone, Debug)]
pub struct SquareRootConfig {
input: Column<Advice>,
squared: Column<Advice>,
instance: Column<Instance>,
myselector: Selector,
lookup_table: TableColumn,
}
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 input = meta.advice_column();
let squared = meta.advice_column();
let instance = meta.instance_column();
meta.enable_equality(input);
meta.enable_equality(squared);
meta.enable_equality(instance);
let myselector = meta.selector();
meta.create_gate("square", |meta| {
let s = meta.query_selector(myselector);
let root = meta.query_advice(input, Rotation::cur());
let square = meta.query_advice(squared, Rotation::cur());
vec![s * (root.clone() * root - square)]
});
let lookup_table = meta.lookup_table_column();
meta.lookup("range_check_constraint", |meta| {
let value = meta.query_advice(input, Rotation::cur());
vec![(value, lookup_table)]
});
SquareRootConfig {
input,
squared,
instance,
myselector,
lookup_table,
}
}
fn synthesize(
&self,
config: Self::Config,
mut layouter: impl Layouter<Fp>,
) -> Result<(), ErrorFront> {
layouter.assign_table(
|| "assign lookup table",
|mut table| {
for (i, v) in (0..=10).into_iter().enumerate() {
table.assign_cell(
|| "assign cell in lookup table",
config.lookup_table,
i,
|| Value::known(Fp::from(v as u64)),
)?;
}
Ok(())
},
)?;
let out = layouter.assign_region(
|| "main region",
|mut region| {
config.myselector.enable(&mut region, 0)?;
region.assign_advice(|| "a", config.input, 0, || self.root)?;
region.assign_advice(|| "square", config.squared, 0, || self.root * self.root)
},
)?;
layouter.constrain_instance(out.cell(), config.instance, 0)?;
Ok(())
}
}