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
use gategen::boolvar::*;
use gategen::gatesim::*;
use gategen::intvar::*;
use gatenative::{cpu_build_exec::*, *};
// generate circuit
fn mul_add_circuit() -> Circuit<u32> {
call32(|| {
let a = U16Var32::var();
let b = U16Var32::var();
let c = U16Var32::var();
let ra = &a * &b + &c;
let rb = &c * &a + &b;
let rc = &b * &c + &a;
let out = ra.concat(rb).concat(rc);
// Circuit has 48-bit input divided into:
// 0..16 - 'a' argument
// 16..32 - 'b' argument
// 32..48 - 'c' argument
out.to_translated_circuit(a.concat(b).concat(c).iter())
})
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create circuit.
let circuit = mul_add_circuit();
// Create builder.
let mut builder = CPUBuilder::new(None);
// Add circuit to builder.
builder.add_with_config(
"mul_add",
circuit,
CodeConfig::new()
// Sets single buffer
.single_buffer(true)
// Set inner loop - executes 10 times circuit passing input as previous output.
.inner_loop(Some(10)),
);
let mut execs = builder.build()?;
// Get input data transformer that converts 96-bit structure into 48-bit circuit input:
// 0 32-bit word - 'a', 1 32-bit word - 'b', 2 32-bit word - 'c'.
let mut it = execs[0].input_transformer(
96,
&((0..16).chain(32..48).chain(64..80).collect::<Vec<_>>()),
)?;
// Get output data transformer that converts 48-bit circuit input to 96-bit structure:
// 0 32-bit word - 'a', 1 32-bit word - 'b', 2 32-bit word - 'c'.
let mut ot = execs[0].output_transformer(
96,
&((0..16).chain(32..48).chain(64..80).collect::<Vec<_>>()),
)?;
let input = execs[0].new_data_from_vec(
(0..16384u32)
.map(|x| [(x + 3) & 0xffff, (x + 1489u32) & 0xffff, (5 * x) & 0xffff])
.flatten()
.collect::<Vec<_>>(),
);
// Transform input to internal form.
let mut data = it.transform(&input)?;
// Execute simulation with single buffer.
execs[0].execute_single(&mut data, 0)?;
// Transform output to 32-bit array.
let output = ot.transform(&data)?;
// Release output data holder - just get its data.
let output = output.release();
// Print that data (3 values per element)
for (i, v) in output.chunks(3).into_iter().enumerate() {
println!("{}: {} {} {}", i, v[0], v[1], v[2]);
}
Ok(())
}