Skip to content

Commit 3ee485f

Browse files
committed
input validation circuit linked to ckt
1 parent 7bc6cf3 commit 3ee485f

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

g16ckt/src/gadgets/groth16.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,56 @@ pub fn groth16_verify_compressed_over_raw_public_input<const N: usize, C: Circui
536536
groth16_verify_compressed(circuit, &input_wires)
537537
}
538538

539+
pub fn simple_circuit_substitute_for_groth16_verify_compressed_raw<
540+
const N: usize,
541+
C: CircuitContext,
542+
>(
543+
circuit: &mut C,
544+
input: &Groth16VerifyCompressedRawInputWires<N>,
545+
) -> crate::WireId {
546+
// convert InputMessage<N> to scalar field elements
547+
let out_hash = blake3_hash(circuit, input.public);
548+
let hash_fr = convert_hash_to_bigint_wires(out_hash);
549+
550+
let proof = input.proof.deserialize_checked(circuit, input.proof_type);
551+
552+
// hash_fr and proof can not be zero
553+
// proof.valid should be true
554+
let mut wire_bits = vec![];
555+
let mut proof_a = proof.a.to_wires_vec();
556+
let mut proof_b = proof.b.to_wires_vec();
557+
let mut proof_c = proof.c.to_wires_vec();
558+
wire_bits.append(&mut proof_a);
559+
wire_bits.append(&mut proof_b);
560+
wire_bits.append(&mut proof_c);
561+
let mut hash_fr = hash_fr[0].to_wires_vec();
562+
wire_bits.append(&mut hash_fr);
563+
564+
let mut acc = wire_bits[0];
565+
for w in &wire_bits[1..] {
566+
let res = circuit.issue_wire();
567+
circuit.add_gate(crate::Gate {
568+
wire_a: acc,
569+
wire_b: *w,
570+
wire_c: res,
571+
gate_type: crate::GateType::Or,
572+
});
573+
acc = res;
574+
}
575+
// acc is 1 if any of the bits is 1, else 0; so acc represents a highly likely criterion
576+
// this way the wire value is mostly influenced by proof.valid
577+
// all the while ensuring that all gates are used (no dangling wires)
578+
let res = circuit.issue_wire();
579+
circuit.add_gate(crate::Gate {
580+
wire_a: acc,
581+
wire_b: proof.valid,
582+
wire_c: res,
583+
gate_type: crate::GateType::And,
584+
});
585+
586+
res
587+
}
588+
539589
#[cfg(test)]
540590
mod tests {
541591
use std::str::FromStr;

g16gen/src/passes/credits.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use g16ckt::{
44
WireId,
55
circuit::{StreamingMode, component_meta::ComponentMetaBuilder},
66
gadgets::groth16::{
7-
Groth16VerifyCompressedRawInput, groth16_verify_compressed_over_raw_public_input,
7+
Groth16VerifyCompressedRawInput,
8+
simple_circuit_substitute_for_groth16_verify_compressed_raw as groth16_fn,
89
},
910
};
1011
use tracing::info;
@@ -22,8 +23,7 @@ pub fn run_credits_pass<const N: usize>(
2223
let metadata_start = Instant::now();
2324
// Run circuit construction in metadata mode
2425
let meta_output_wires = {
25-
let ok =
26-
groth16_verify_compressed_over_raw_public_input(&mut metadata_mode, &allocated_inputs);
26+
let ok = groth16_fn(&mut metadata_mode, &allocated_inputs);
2727
vec![ok]
2828
};
2929
let metadata_time = metadata_start.elapsed();
@@ -39,7 +39,7 @@ pub fn run_credits_pass<const N: usize>(
3939
let credits_start = Instant::now();
4040
// Run the credits pass
4141
let real_output_wires = {
42-
let ok = groth16_verify_compressed_over_raw_public_input(&mut ctx, &allocated_inputs);
42+
let ok = groth16_fn(&mut ctx, &allocated_inputs);
4343
vec![ok]
4444
};
4545
println!("Output wires: {:?}", real_output_wires);

g16gen/src/passes/translation.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use g16ckt::{
44
WireId,
55
circuit::{StreamingMode, component_meta::ComponentMetaBuilder},
66
gadgets::groth16::{
7-
Groth16VerifyCompressedRawInput, groth16_verify_compressed_over_raw_public_input,
7+
Groth16VerifyCompressedRawInput,
8+
simple_circuit_substitute_for_groth16_verify_compressed_raw as groth16_fn,
89
},
910
};
1011
use tracing::info;
@@ -26,8 +27,7 @@ pub async fn run_translation_pass<const N: usize>(
2627
let metadata_start = Instant::now();
2728
// Run circuit construction in metadata mode
2829
let meta_output_wires = {
29-
let ok =
30-
groth16_verify_compressed_over_raw_public_input(&mut metadata_mode, &allocated_inputs);
30+
let ok = groth16_fn(&mut metadata_mode, &allocated_inputs);
3131
vec![ok]
3232
};
3333
let metadata_time = metadata_start.elapsed();
@@ -50,7 +50,7 @@ pub async fn run_translation_pass<const N: usize>(
5050
let translation_start = Instant::now();
5151
// Run the translation pass
5252
let translation_output_wires = {
53-
let ok = groth16_verify_compressed_over_raw_public_input(&mut ctx, &allocated_inputs);
53+
let ok = groth16_fn(&mut ctx, &allocated_inputs);
5454
vec![ok]
5555
};
5656

0 commit comments

Comments
 (0)