Skip to content

Commit

Permalink
fix missing constants
Browse files Browse the repository at this point in the history
  • Loading branch information
namnc committed Jun 27, 2024
1 parent ef9d9d6 commit 800e2d4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
59 changes: 48 additions & 11 deletions input/circuit.circom
Original file line number Diff line number Diff line change
@@ -1,20 +1,57 @@
pragma circom 2.1.0;
// from 0xZKML/zk-mnist

template componentA () {
signal input in[2][2];
signal output out;
pragma circom 2.0.0;

template Switcher() {
signal input sel;
signal input L;
signal input R;
signal output outL;
signal output outR;

out <== in[0][0] + in[0][1] + in[1][0] + in[1][1];
signal aux;

aux <== (R-L)*sel; // We create aux in order to have only one multiplication
outL <== aux + L;
outR <== -aux + R;
}

template componentB() {
signal input a_in[2][2];
template ArgMax (n) {
signal input in[n];
signal output out;

component a = componentA();
a.in <== a_in;
// assert (out < n);
signal gts[n]; // store comparators
component switchers[n+1]; // switcher for comparing maxs
component aswitchers[n+1]; // switcher for arg max

signal maxs[n+1];
signal amaxs[n+1];

out <== a.out;
maxs[0] <== in[0];
amaxs[0] <== 0;
for(var i = 0; i < n; i++) {
gts[i] <== in[i] > maxs[i]; // changed to 252 (maximum) for better compatibility
switchers[i+1] = Switcher();
aswitchers[i+1] = Switcher();

switchers[i+1].sel <== gts[i];
switchers[i+1].L <== maxs[i];
switchers[i+1].R <== in[i];

aswitchers[i+1].sel <== gts[i];
aswitchers[i+1].L <== amaxs[i];
aswitchers[i+1].R <== i;
amaxs[i+1] <== aswitchers[i+1].outL;
maxs[i+1] <== switchers[i+1].outL;
}

out <== amaxs[n];
}

component main = componentB();
component main = ArgMax(1);

/* INPUT = {
"in": ["2","3","1","5","4"],
"out": "3"
} */
2 changes: 1 addition & 1 deletion src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ impl Compiler {

if let Some(value) = signal.value {
constant_to_node_id_and_value
.insert(signal.name.clone(), (*node_id, value.to_string()));
.insert(format!("{}_{}",signal.name.clone(),signal_id), (*node_id, value.to_string()));
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ fn main() -> Result<(), ProgramError> {
let output_file_path = build_output(&output_dir, "circuit", "txt");
circuit.write_bristol(&mut File::create(output_file_path)?)?;

// let output_file_path_json = build_output(&output_dir, "circuit", "json");
// File::create(output_file_path_json)?.write_all(serde_json::to_string_pretty(&circuit)?.as_bytes())?;

let output_debug_path_json = build_output(&output_dir, "debug", "json");
File::create(output_debug_path_json)?.write_all(serde_json::to_string_pretty(&compiler)?.as_bytes())?;

let output_file_path = build_output(&output_dir, "circuit_info", "json");
File::create(output_file_path)?.write_all(to_string_pretty(&circuit.info)?.as_bytes())?;

Expand Down

0 comments on commit 800e2d4

Please sign in to comment.