Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: blake2b using memory #121

Merged
merged 63 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
2c92f07
initial commit
kevjue Nov 8, 2023
97bcf13
more changes
kevjue Nov 9, 2023
00602a4
some fixes
kevjue Nov 9, 2023
63a25f8
added logic for initial compress xors
kevjue Nov 9, 2023
9c72d2d
more compilation fixes
kevjue Nov 10, 2023
e7ddf4e
added in multiplicities (TODO, need to add in dummy multiplicities)
kevjue Nov 10, 2023
fd3232c
compilation fixes
kevjue Nov 13, 2023
510eb2a
more compilation fixes
kevjue Nov 13, 2023
73817f7
added in missing multiplicities
kevjue Nov 13, 2023
f6a1504
added builder object
kevjue Nov 13, 2023
4d08eec
added digest bits and indices
kevjue Nov 13, 2023
f009e92
added constraints for verifying digest
kevjue Nov 13, 2023
c9fbee5
fixed the m time
kevjue Nov 13, 2023
d09cc3f
added in t register
kevjue Nov 13, 2023
a7c6667
air test case
kevjue Nov 19, 2023
2bbdc0f
bug fixes
kevjue Nov 20, 2023
c7481b3
some fies
kevjue Nov 20, 2023
1ea352f
fixed v_final dummy reads
kevjue Nov 20, 2023
a614c71
more bug fixes
kevjue Nov 21, 2023
7210cb1
Merge branch 'main' into kevjue/blake2b_refactor
kevjue Nov 21, 2023
10e15dc
more bug fixes
kevjue Nov 22, 2023
1feaf9e
filled up 96 rows
kevjue Nov 23, 2023
7c575eb
fixed compress_iteration
kevjue Nov 23, 2023
dc07e58
got basic air writing working
kevjue Nov 24, 2023
17eb4c6
fixes
kevjue Nov 26, 2023
f5f1b65
fixes
kevjue Nov 26, 2023
2ba1110
filled in the dummy rows
kevjue Nov 28, 2023
9f754fb
added watch_memory instruction
kevjue Nov 28, 2023
9727f30
got correct trace for multiple messages
kevjue Nov 28, 2023
904ffba
const memory multiplicities fixed
kevjue Nov 28, 2023
9efd0fb
most multiplicities fixed
kevjue Nov 29, 2023
97d6c31
fixed v_final multiplicities
kevjue Nov 29, 2023
d553448
added bitregister builder not
kevjue Nov 29, 2023
6013926
almost there
kevjue Nov 30, 2023
4a9afe5
more multiplicities fixes
kevjue Nov 30, 2023
a4a323c
removed using ts for h
kevjue Nov 30, 2023
df66165
added memory trace output
kevjue Dec 1, 2023
bcb8038
replaced usage of set_to_expression to expression
kevjue Dec 5, 2023
0ab79db
added some memory trace debug messages
kevjue Dec 5, 2023
0e1a93a
some cleanup
kevjue Dec 5, 2023
0477167
enabled some timing info
kevjue Dec 5, 2023
dbb7654
fixed bug and more complicated test case
kevjue Dec 5, 2023
3bf7232
added some timing output
kevjue Dec 5, 2023
5d29aa3
enabled recursive verification
kevjue Dec 5, 2023
1932e0e
fixed test
kevjue Dec 5, 2023
27f19d9
verifying the expected digest
kevjue Dec 6, 2023
698638a
some cleanup and fixes
kevjue Dec 6, 2023
bb4cee3
fixed a bug in the test case
kevjue Dec 6, 2023
b366a24
store the digest
kevjue Dec 6, 2023
63f2ac9
cleanup and refactoring
kevjue Dec 6, 2023
2b1f178
clippy fixes
kevjue Dec 6, 2023
e00c76b
added consts
kevjue Dec 6, 2023
4f3644e
more cleanup
kevjue Dec 6, 2023
36bfb56
fix CI clippy issues
kevjue Dec 6, 2023
e64aba1
more fixing of CI clippy issues
kevjue Dec 6, 2023
aee91ad
more constants
kevjue Dec 6, 2023
c421a96
made IV public
kevjue Dec 6, 2023
f5126b1
added register file for blake2b
kevjue Dec 7, 2023
f4c034e
made blake2b builder function public
kevjue Dec 7, 2023
d87802e
output memory label at error
kevjue Dec 8, 2023
1aa3658
fix
kevjue Dec 8, 2023
3e44f3d
address pr comments
kevjue Dec 8, 2023
3080bbb
removed a debug line
kevjue Dec 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions curta/src/chip/ec/edwards/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ impl<L: AirParameters> AirBuilder<L> {
let y2 = q.y;

// x3_numerator = x1 * y2 + x2 * y1.
let x3_numerator = self.fp_inner_product(&vec![x1, x2], &vec![y2, y1]);
let x3_numerator = self.fp_inner_product(&[x1, x2], &[y2, y1]);

// y3_numerator = y1 * y2 + x1 * x2.
let y3_numerator = self.fp_inner_product(&vec![y1, x1], &vec![y2, x2]);
let y3_numerator = self.fp_inner_product(&[y1, x1], &[y2, x2]);

// f = x1 * x2 * y1 * y2.
let x1_mul_y1 = self.fp_mul(&x1, &y1);
Expand Down
4 changes: 2 additions & 2 deletions curta/src/chip/ec/edwards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ where
let y2 = q.y;

// x3_numerator = x1 * y2 + x2 * y1.
let x3_numerator = builder.fp_inner_product(&vec![x1, x2], &vec![y2, y1]);
let x3_numerator = builder.fp_inner_product(&[x1, x2], &[y2, y1]);

// y3_numerator = y1 * y2 + x1 * x2.
let y3_numerator = builder.fp_inner_product(&vec![y1, x1], &vec![y2, x2]);
let y3_numerator = builder.fp_inner_product(&[y1, x1], &[y2, x2]);

// f = x1 * x2 * y1 * y2.
let x1_mul_y1 = builder.fp_mul(&x1, &y1);
Expand Down
12 changes: 6 additions & 6 deletions curta/src/chip/field/inner_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ pub struct FpInnerProductInstruction<P: FieldParameters> {
impl<L: AirParameters> AirBuilder<L> {
pub fn fp_inner_product<P: FieldParameters>(
&mut self,
a: &Vec<FieldRegister<P>>,
b: &Vec<FieldRegister<P>>,
a: &[FieldRegister<P>],
b: &[FieldRegister<P>],
) -> FieldRegister<P>
where
L::Instruction: From<FpInnerProductInstruction<P>>,
Expand All @@ -62,8 +62,8 @@ impl<L: AirParameters> AirBuilder<L> {
}

let instr = FpInnerProductInstruction {
a: a.clone(),
b: b.clone(),
a: a.to_vec(),
b: b.to_vec(),
result,
carry,
witness_low,
Expand Down Expand Up @@ -259,13 +259,13 @@ mod tests {
let b_pub = builder.alloc_public::<Fp>();
let c_pub = builder.alloc_public::<Fp>();
let d_pub = builder.alloc_public::<Fp>();
let _ = builder.fp_inner_product(&vec![a_pub, b_pub], &vec![c_pub, d_pub]);
let _ = builder.fp_inner_product(&[a_pub, b_pub], &[c_pub, d_pub]);

let a = builder.alloc::<Fp>();
let b = builder.alloc::<Fp>();
let c = builder.alloc::<Fp>();
let d = builder.alloc::<Fp>();
let _ = builder.fp_inner_product(&vec![a, b], &vec![c, d]);
let _ = builder.fp_inner_product(&[a, b], &[c, d]);

let (air, trace_data) = builder.build();
let num_rows = 1 << 16;
Expand Down
8 changes: 4 additions & 4 deletions curta/src/chip/field/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ mod tests {

let mult = builder.constant(&F::from_canonical_usize(num_rows));

builder.store(&a_ptr, a_pub, &Time::zero(), Some(mult));
builder.store(&b_ptr, b_pub, &Time::zero(), Some(mult));
builder.store(&a_ptr, a_pub, &Time::zero(), Some(mult), None, None);
builder.store(&b_ptr, b_pub, &Time::zero(), Some(mult), None, None);

let a = builder.load(&a_ptr, &Time::zero());
let b = builder.load(&b_ptr, &Time::zero());
let a = builder.load(&a_ptr, &Time::zero(), None, None);
let b = builder.load(&b_ptr, &Time::zero(), None, None);
let _ = builder.add(a, b);

let (air, trace_data) = builder.build();
Expand Down
6 changes: 4 additions & 2 deletions curta/src/chip/hash/blake/blake2b/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ mod tests {

let _ = env_logger::builder().is_test(true).try_init();

let mut timing = TimingTree::new("Blake2b test", log::Level::Debug);
let mut timing = TimingTree::new("Blake2b test", log::Level::Info);

let mut builder = AirBuilder::<L>::new();
let clk = builder.clock();
Expand Down Expand Up @@ -1249,7 +1249,7 @@ mod tests {
}
}

timed!(timing, "Write the execusion trace", {
timed!(timing, log::Level::Info, "Write the execusion trace", {
byte_table.write_table_entries(&writer);
blake_gadget.write(
padded_messages,
Expand Down Expand Up @@ -1300,13 +1300,15 @@ mod tests {
// Generate proof and verify as a stark
timed!(
timing,
log::Level::Info,
"Stark proof and verify",
test_starky(&stark, &config, &generator, &public_inputs)
);

// Generate recursive proof
timed!(
timing,
log::Level::Info,
"Recursive proof generation and verification",
test_recursive_starky(stark, config, generator, &public_inputs)
);
Expand Down
4 changes: 2 additions & 2 deletions curta/src/chip/instruction/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub enum AirInstruction<F, I> {
Clock(ClockInstruction),
ProcessId(ProcessIdInstruction),
Filtered(ArithmeticExpression<F>, Arc<Self>),
Mem(MemoryInstruction),
Mem(MemoryInstruction<F>),
Watch(String, ArrayRegister<ElementRegister>),
}

Expand Down Expand Up @@ -143,7 +143,7 @@ impl<F, I> AirInstruction<F, I> {
AirInstruction::Filtered(filter, Arc::new(self))
}

pub fn mem(instruction: MemoryInstruction) -> Self {
pub fn mem(instruction: MemoryInstruction<F>) -> Self {
AirInstruction::Mem(instruction)
}

Expand Down
59 changes: 51 additions & 8 deletions curta/src/chip/memory/builder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use core::borrow::Borrow;

use super::get::GetInstruction;
use super::instruction::MemoryInstruction;
use super::instruction::{MemoryInstruction, MemoryOutput, MemorySliceIndex};
use super::pointer::slice::{RawSlice, Slice};
use super::pointer::Pointer;
use super::set::SetInstruction;
use super::time::Time;
use super::value::MemoryValue;
use super::watch::WatchInstruction;
use crate::chip::builder::AirBuilder;
use crate::chip::instruction::set::AirInstruction;
use crate::chip::register::cubic::CubicRegister;
Expand Down Expand Up @@ -39,7 +40,7 @@ impl<L: AirParameters> AirBuilder<L> {
let ptr = self.uninit();
let digest = value.compress(self, ptr.raw, time, &ptr.challenges);
self.input_to_memory_bus(digest, multiplicity);
self.unsafe_raw_write(&ptr, *value, multiplicity, true);
self.unsafe_raw_write(&ptr, *value, multiplicity, true, None);

ptr
}
Expand Down Expand Up @@ -77,7 +78,7 @@ impl<L: AirParameters> AirBuilder<L> {
let ptr = slice.get(i);
let digest = value.compress(self, ptr.raw, time, &ptr.challenges);
self.input_to_memory_bus(digest, multiplicity);
self.unsafe_raw_write(&ptr, *value, multiplicity, true);
self.unsafe_raw_write(&ptr, *value, multiplicity, true, None);
}
slice
}
Expand Down Expand Up @@ -122,16 +123,36 @@ impl<L: AirParameters> AirBuilder<L> {
}

/// Reads the value from the memory at location `ptr`.
pub fn get<V: MemoryValue>(&mut self, ptr: &Pointer<V>, last_write_ts: &Time<L::Field>) -> V {
let value = self.unsafe_raw_read(ptr);
pub fn get<V: MemoryValue>(
&mut self,
ptr: &Pointer<V>,
last_write_ts: &Time<L::Field>,
label: Option<String>,
index: Option<MemorySliceIndex>,
) -> V {
let memory_output = label.map(|label| MemoryOutput {
label,
index,
ts: (*last_write_ts).clone(),
});

let value = self.unsafe_raw_read(ptr, memory_output);
let read_digest = value.compress(self, ptr.raw, last_write_ts, &ptr.challenges);
self.output_from_memory_bus(read_digest);
value
}

fn unsafe_raw_read<V: MemoryValue>(&mut self, ptr: &Pointer<V>) -> V {
fn unsafe_raw_read<V: MemoryValue>(
&mut self,
ptr: &Pointer<V>,
memory_output: Option<MemoryOutput<L::Field>>,
) -> V {
let value = self.alloc::<V>();
let instr = MemoryInstruction::Get(GetInstruction::new(ptr.raw, *value.register()));
let instr = MemoryInstruction::Get(GetInstruction::new(
ptr.raw,
*value.register(),
memory_output,
));
self.register_air_instruction_internal(AirInstruction::mem(instr));
value
}
Expand All @@ -142,11 +163,13 @@ impl<L: AirParameters> AirBuilder<L> {
value: V,
multiplicity: Option<ElementRegister>,
global: bool,
memory_output: Option<MemoryOutput<L::Field>>,
) {
let instr = MemoryInstruction::Set(SetInstruction::new(
ptr.raw,
*value.register(),
multiplicity,
memory_output,
));
if global {
self.register_global_air_instruction_internal(AirInstruction::mem(instr))
Expand All @@ -167,6 +190,8 @@ impl<L: AirParameters> AirBuilder<L> {
value: V,
write_ts: &Time<L::Field>,
multiplicity: Option<ElementRegister>,
label: Option<String>,
index: Option<MemorySliceIndex>,
) {
if value.is_trace() {
if let Some(mult) = multiplicity {
Expand All @@ -175,6 +200,24 @@ impl<L: AirParameters> AirBuilder<L> {
}
let write_digest = value.compress(self, ptr.raw, write_ts, &ptr.challenges);
self.input_to_memory_bus(write_digest, multiplicity);
self.unsafe_raw_write(ptr, value, multiplicity, !write_digest.is_trace())

let memory_output = label.map(|label| MemoryOutput {
label,
index,
ts: (*write_ts).clone(),
});

self.unsafe_raw_write(
ptr,
value,
multiplicity,
!write_digest.is_trace(),
memory_output,
);
}

pub fn watch_memory<V: MemoryValue>(&mut self, ptr: &Pointer<V>, name: &str) {
let instr = MemoryInstruction::Watch(WatchInstruction::new(ptr.raw, name.to_string()));
self.register_air_instruction_internal(AirInstruction::mem(instr));
}
}
Loading
Loading