Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into tamir/audit_fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirhemo committed Dec 21, 2023
2 parents d65db0c + c4dd8dd commit 70da762
Show file tree
Hide file tree
Showing 29 changed files with 2,603 additions and 84 deletions.
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

0 comments on commit 70da762

Please sign in to comment.