-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
83 lines (66 loc) · 1.92 KB
/
main.rs
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
use zktsim::boolean_circuit::{BooleanCircuit, BooleanCircuitInstance};
use zktsim::circuits::io_sat::{run_mock_prover, run_prover_kzg};
const K: u32 = 12;
const G: usize = 1 << (K - 1);
const W: usize = 1 << (K - 1);
fn u64_to_bits_le(x: u64, n: usize) -> Vec<bool> {
let mut v = Vec::new();
for i in 0..n {
v.push((x >> i) & 1 == 1);
}
v
}
fn bits_le_to_u64(v: &[bool]) -> u64 {
assert!(v.len() <= 64);
let mut x = 0;
for i in 0..v.len() {
if v[i] {
x |= 1 << i;
}
}
x
}
#[allow(dead_code)]
fn test_zktsim_cla() {
let ckt = BooleanCircuit::from_netlist("examples/cla_adder_6b.zkt").unwrap();
let a = u64_to_bits_le(31, 6);
let b = u64_to_bits_le(17, 6);
let c_in = u64_to_bits_le(0, 1);
let inputs = vec![a, b, c_in].concat();
let inst = BooleanCircuitInstance::from_ckt_and_inputs(ckt, &inputs);
let outputs = inst
.ckt
.outputs
.iter()
.map(|&i| inst.assn.wires[i as usize])
.collect::<Vec<_>>();
let s = bits_le_to_u64(&outputs[0..6]);
let c_out = bits_le_to_u64(&outputs[6..7]);
println!("s = {}", s);
println!("c_out = {}", c_out);
run_mock_prover::<K, G, W>(inst);
println!("zktsim cla works!");
}
fn test_zktsim_c6288() {
let ckt = BooleanCircuit::from_netlist("examples/c6288.zkt").unwrap();
let mut a = u64_to_bits_le(512, 16);
let mut b = u64_to_bits_le(2048, 16);
a.reverse();
b.reverse();
let inputs = vec![a, b].concat();
let inst = BooleanCircuitInstance::from_ckt_and_inputs(ckt, &inputs);
let mut outputs = inst
.ckt
.outputs
.iter()
.map(|&i| inst.assn.wires[i as usize])
.collect::<Vec<_>>();
outputs.reverse();
let p = bits_le_to_u64(&outputs);
println!("p = {}", p);
run_prover_kzg::<K, G, W>(inst);
println!("zktsim c6288 works!");
}
fn main() {
test_zktsim_c6288();
}