Skip to content

Commit 0a98ea8

Browse files
committed
fanout_ctr as u32
1 parent 0834f30 commit 0a98ea8

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

g16gen/src/cache.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ const FANOUT_FILE: &str = "fanout.cache";
99
const OUTPUT_WIRES_FILE: &str = "outputs.cache";
1010

1111
/// Try to load cached fanout and output wires from files
12-
pub fn try_load_cache() -> Option<(Vec<u16>, Vec<WireId>)> {
12+
pub fn try_load_cache() -> Option<(Vec<u32>, Vec<WireId>)> {
1313
let fanout = load_fanout()?;
1414
let output_wires = load_output_wires()?;
1515
Some((fanout, output_wires))
1616
}
1717

1818
/// Load fanout from cache file
19-
fn load_fanout() -> Option<Vec<u16>> {
19+
fn load_fanout() -> Option<Vec<u32>> {
2020
let file = OpenOptions::new().read(true).open(FANOUT_FILE).ok()?;
2121
let mut reader = BufReader::new(file);
2222
let mut fanout = Vec::new();
2323

2424
loop {
25-
let mut buf = [0u8; 2];
25+
let mut buf = [0u8; 4];
2626
if reader.read_exact(&mut buf).is_err() {
2727
break;
2828
}
29-
fanout.push(u16::from_le_bytes(buf));
29+
fanout.push(u32::from_le_bytes(buf));
3030
}
3131

3232
Some(fanout)
@@ -50,7 +50,7 @@ fn load_output_wires() -> Option<Vec<WireId>> {
5050
}
5151

5252
/// Save fanout to cache file
53-
pub fn save_fanout(fanout: &[u16]) -> std::io::Result<()> {
53+
pub fn save_fanout(fanout: &[u32]) -> std::io::Result<()> {
5454
let file = OpenOptions::new()
5555
.write(true)
5656
.create(true)
@@ -82,7 +82,7 @@ pub fn save_output_wires(output_wires: &[WireId]) -> std::io::Result<()> {
8282
}
8383

8484
/// Save both credits and output wires to cache files
85-
pub fn save_cache(credits: &[u16], output_wires: &[WireId]) -> std::io::Result<()> {
85+
pub fn save_cache(credits: &[u32], output_wires: &[WireId]) -> std::io::Result<()> {
8686
save_fanout(credits)?;
8787
save_output_wires(output_wires)?;
8888
Ok(())

g16gen/src/modes/fanout_ctr.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use indicatif::ProgressBar;
77

88
#[derive(Debug)]
99
pub struct FanoutCounter {
10-
fanout: Option<Vec<u16>>, // Original -> Normalized IDs
10+
fanout: Option<Vec<u32>>, // Original -> Normalized IDs
1111
next_normalized_id: u64,
1212
primary_inputs: usize,
1313
biggest_fanout_seen: usize,
@@ -43,7 +43,7 @@ impl CircuitMode for FanoutCounter {
4343
fn evaluate_gate(&mut self, gate: &SourceGate) {
4444
self.spinner.inc(1);
4545

46-
let resize = |fanout: &mut Vec<u16>, max_wire_produced: usize| {
46+
let resize = |fanout: &mut Vec<u32>, max_wire_produced: usize| {
4747
if max_wire_produced >= fanout.len() {
4848
fanout.resize(max_wire_produced + 1, 0);
4949
}
@@ -188,18 +188,23 @@ impl FanoutCounter {
188188
id
189189
}
190190

191-
fn wire_used(&mut self, wire_id: WireId) -> u16 {
191+
// fn top_n(mut v: Vec<u16>, n: usize) -> Vec<u16> {
192+
// v.sort_unstable_by(|a, b| b.cmp(a)); // descending
193+
// v.truncate(n);
194+
// v
195+
// }
196+
197+
fn wire_used(&mut self, wire_id: WireId) -> u32 {
192198
let wire_id = wire_id.0;
193199
if (0..self.primary_inputs + 2).contains(&wire_id) {
194200
return 0;
195201
}
196202
let fanout = self.fanout.as_mut().unwrap();
197-
198203
fanout[wire_id] += 1;
199204
fanout[wire_id]
200205
}
201206

202-
pub fn finish(&mut self) -> (Vec<u16>, usize) {
207+
pub fn finish(&mut self) -> (Vec<u32>, usize) {
203208
let fanout = self.fanout.take().unwrap();
204209
(fanout, self.biggest_fanout_seen)
205210
}

g16gen/src/modes/translate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use kanal::{Sender, bounded_async};
1818
use monoio::{FusionDriver, RuntimeBuilder, select};
1919

2020
pub struct TranslationMode {
21-
creds: Vec<u16>,
21+
creds: Vec<u32>,
2222
next_normalized_id: u64,
2323

2424
// Constants
@@ -72,7 +72,7 @@ impl CircuitMode for TranslationMode {
7272

7373
impl TranslationMode {
7474
pub async fn new(
75-
creds: Vec<u16>,
75+
creds: Vec<u32>,
7676
path: &str,
7777
primary_inputs: u64,
7878
outputs: Vec<WireId>,
@@ -151,7 +151,7 @@ impl TranslationMode {
151151
in1: in1.to_u64(),
152152
in2: in2.to_u64(),
153153
out: out.to_u64(),
154-
credits: self.creds[out.to_u64() as usize] as u32,
154+
credits: self.creds[out.to_u64() as usize],
155155
gate_type,
156156
};
157157
loop {

g16gen/src/passes/credits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::modes::fanout_ctr::FanoutCounter;
1414
pub fn run_credits_pass(
1515
inputs: &Groth16VerifyCompressedInput,
1616
primary_input_count: usize,
17-
) -> (Vec<u16>, Vec<WireId>) {
17+
) -> (Vec<u32>, Vec<WireId>) {
1818
let (allocated_inputs, root_meta) = ComponentMetaBuilder::new_with_input(inputs);
1919
let mut metadata_mode = StreamingMode::<FanoutCounter>::MetadataPass(root_meta);
2020

g16gen/src/passes/translation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const OUTPUT_FILE: &str = "g16.ckt";
1616
pub async fn run_translation_pass(
1717
inputs: &Groth16VerifyCompressedInput,
1818
primary_input_count: usize,
19-
credits: Vec<u16>,
19+
credits: Vec<u32>,
2020
output_wires: Vec<WireId>,
2121
) {
2222
let (allocated_inputs, root_meta) = ComponentMetaBuilder::new_with_input(inputs);

0 commit comments

Comments
 (0)