Skip to content

Commit

Permalink
feat: adapt for MMAP export in Besu
Browse files Browse the repository at this point in the history
  • Loading branch information
delehef committed Nov 17, 2023
1 parent 1660b55 commit c0b4192
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 40 deletions.
34 changes: 24 additions & 10 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub fn make<S1: AsRef<str>, S2: AsRef<str>>(
})?
}

let constraints = maybe_bail(
let mut constraints = maybe_bail(
asts.iter()
.flat_map(|(name, ast)| generator::pass(ast, ctx.clone(), name, settings))
.collect(),
Expand Down Expand Up @@ -179,15 +179,29 @@ pub fn make<S1: AsRef<str>, S2: AsRef<str>>(
let id = columns.insert_column(column)?;
match k {
Kind::Atomic | Kind::Phantom => (),
Kind::Computed(e) => computations
.insert(
&id,
Computation::Composite {
target: id.clone(),
exp: *e.clone(),
},
)
.map(|_| ())?,
Kind::Computed(e) => {
computations
.insert(
&id,
Computation::Composite {
target: id.clone(),
exp: *e.clone(),
},
)
.map(|_| ())?;
constraints.push(Constraint::Vanishes {
handle: Handle::new(
&handle.as_handle().module,
format!("prove-{}", handle.as_handle().name),
),
domain: None,
expr: Box::new(
Intrinsic::Sub
.call(&[Node::column().handle(id).build(), *e.clone()])
.unwrap(),
),
})
}
}
}
Expression::ExoColumn {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ impl Magma {
self.m.bit_size()
}

pub fn byte_size(&self) -> usize {
(self.m.bit_size() + 7) / 8
}

pub fn invert(&self) -> Magma {
Magma {
m: match self.m {
Expand Down
19 changes: 17 additions & 2 deletions src/exporters/besu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct BesuColumn {
updater: String,
tupe: String,
register: String,
putter: String,
reg_id: usize,
}
#[derive(Serialize)]
Expand All @@ -37,6 +38,7 @@ struct BesuRegister {
tupe: String,
id: usize,
zero_value: String,
bytes_width: i16,
}
#[derive(Serialize)]
struct BesuConstant {
Expand Down Expand Up @@ -120,14 +122,19 @@ pub fn render(cs: &ConstraintSet, package: &str, output_path: Option<&String>) -
.iter()
.enumerate()
.map(|(i, r)| {
let corset_name = reg_to_string(r, i);
let corset_name = format!(
"{}.{}",
r.handle.as_ref().unwrap().module,
r.handle.as_ref().unwrap().name
);
let java_name = reg_to_string(r, i).to_case(Case::Camel);
BesuRegister {
corset_name,
java_name,
tupe: magma_to_java_type(r.magma),
id: i,
zero_value: magma_to_java_zero(r.magma),
bytes_width: r.magma.byte_size() as i16,
}
})
.sorted_by_key(|f| f.java_name.clone())
Expand All @@ -141,13 +148,21 @@ pub fn render(cs: &ConstraintSet, package: &str, output_path: Option<&String>) -
let r = c.register.unwrap();
let register = reg_to_string(&cs.columns.registers[r], r).to_case(Case::Camel);
Some(BesuColumn {
corset_name: c.handle.name.to_string(),
corset_name: c.handle.to_string(),
java_name: c.handle.name.to_case(Case::Camel),
appender: handle_to_appender(&c.handle),
updater: handle_to_updater(&c.handle),
tupe: magma_to_java_type(c.t),
register,
reg_id: r,
putter: match c.t.rm() {
RawMagma::Binary => "put((byte) (b ? 1 : 0))",
RawMagma::Nibble => "put(b.toByte())",
RawMagma::Byte => "put(b.toByte())",
RawMagma::Native => "put(b.toByteArray())",
_ => unreachable!(),
}
.to_string(),
})
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion src/exporters/besu_module_trace.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright ConsenSys Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand Down
64 changes: 37 additions & 27 deletions src/exporters/besu_trace_columns.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright ConsenSys Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand All @@ -15,14 +15,16 @@

package net.consensys.linea.zktracer.module.{{ module }};

import com.fasterxml.jackson.annotation.JsonProperty;
import net.consensys.linea.zktracer.types.UnsignedByte;

import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import net.consensys.linea.zktracer.ColumnHeader;
import net.consensys.linea.zktracer.types.UnsignedByte;

/**
* WARNING: This code is generated automatically.
* Any modifications to this code may be overwritten and could lead to unexpected behavior.
Expand All @@ -40,26 +42,45 @@ public int size() {
return this.{{ registers.0.java_name }}.size();
}

public static List<ColumnHeader> headers(int size) {
return List.of(
{{#each registers}}
new ColumnHeader("{{ this.corset_name }}", {{ this.bytes_width }}, size){{ #unless @last}},{{/unless}}
{{/each}}
);
}



static class TraceBuilder {
private final BitSet filled = new BitSet();
private int currentLine = 0;

{{#each registers}}
@JsonProperty("{{ this.corset_name }}")
private final List<{{ this.tupe }}> {{ this.java_name }};
private MappedByteBuffer {{ this.java_name}};
{{/each}}

private TraceBuilder(int length) {
{{#each registers}}
this.{{ this.java_name }} = new ArrayList<>(length);
{{/each}}
}

public int size() {
if (!filled.isEmpty()) {
throw new RuntimeException("Cannot measure a trace with a non-validated row.");
}

return this.{{ registers.0.java_name }}.size();
return this.currentLine;
}

public void setBuffers(List<MappedByteBuffer> buffers) {
{{ #each registers }}
this.{{ java_name }} = buffers.get({{ @index }});
{{ /each }}
}

public void releaseBuffers() {
{{ #each registers }}
this.{{ java_name }} = null;
{{ /each }}
}

{{#each columns}}
Expand All @@ -70,48 +91,37 @@ public int size() {
filled.set({{ this.reg_id }});
}

{{ this.register }}.add(b);
{{ this.register }}.{{ this.putter }};

return this;
}
{{#unless @last}}

{{/unless}}
{{/each}}

public TraceBuilder validateRow() {
{{#each registers}}
if (!filled.get({{ this.id }})) {
throw new IllegalStateException("{{ this.corset_name }} has not been filled");
}

{{/each}}

filled.clear();
this.currentLine++;

return this;
}

public TraceBuilder fillAndValidateRow() {
{{#each registers}}
if (!filled.get({{ this.id }})) {
{{ this.java_name }}.add({{ this.zero_value }});
this.filled.set({{ this.id }});
}
{{/each}}
filled.clear();
this.currentLine++;

return this.validateRow();
return this;
}

public Trace build() {
if (!filled.isEmpty()) {
throw new IllegalStateException("Cannot build trace with a non-validated row.");
}

return new Trace(
{{#each registers}}
{{ this.java_name }}{{#unless @last}},{{/unless}}{{#if @last}});{{/if}}
{{/each}}
return null;
}
}
}

0 comments on commit c0b4192

Please sign in to comment.