Skip to content

Commit

Permalink
VerifyBatch direct hint
Browse files Browse the repository at this point in the history
  • Loading branch information
nyunyunyunyu committed Feb 1, 2025
1 parent 74610b9 commit cfb6eb2
Show file tree
Hide file tree
Showing 18 changed files with 236 additions and 198 deletions.
4 changes: 3 additions & 1 deletion extensions/native/circuit/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ impl<F: PrimeField32> VmExtension<F> for Native {
offline_memory.clone(),
Poseidon2Config::default(),
VerifyBatchBus(builder.new_bus_idx()),
builder.streams().clone(),
);
inventory.add_executor(
poseidon2_chip,
Expand Down Expand Up @@ -334,7 +335,8 @@ pub(crate) mod phantom {
};
let id = streams.hint_space.len();
streams.hint_space.push(payload);
streams.hint_stream.clear();
// Hint stream should have already been consumed.
debug_assert!(streams.hint_stream.is_empty());
streams.hint_stream.push_back(F::from_canonical_usize(id));
Ok(())
}
Expand Down
63 changes: 11 additions & 52 deletions extensions/native/circuit/src/poseidon2/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl<AB: InteractionBuilder, const SBOX_REGISTERS: usize> Air<AB>
} else {
builder.when(inside_row)
};
// everthing above oks
// everything above oks

// update state for normal cell
when_inside_row_not_last
Expand Down Expand Up @@ -302,27 +302,23 @@ impl<AB: InteractionBuilder, const SBOX_REGISTERS: usize> Air<AB>
dim_register,
opened_register,
opened_length_register,
sibling_register,
sibling_id,
index_register,
commit_register,
final_opened_index,
height,
opened_length,
dim_base_pointer,
sibling_base_pointer,
index_base_pointer,
dim_base_pointer_read,
opened_base_pointer_read,
opened_length_read,
sibling_base_pointer_read,
index_base_pointer_read,
commit_pointer_read,
proof_index,
read_initial_height_or_root_is_on_right,
read_final_height_or_sibling_array_start,
read_final_height,
root_is_on_right,
sibling_array_start,
reads,
commit_pointer,
commit_read,
} = top_level_specific;
Expand All @@ -348,7 +344,7 @@ impl<AB: InteractionBuilder, const SBOX_REGISTERS: usize> Air<AB>
dim_register,
opened_register,
opened_length_register,
sibling_register,
sibling_id,
index_register,
commit_register,
opened_element_size_inv,
Expand Down Expand Up @@ -382,27 +378,19 @@ impl<AB: InteractionBuilder, const SBOX_REGISTERS: usize> Air<AB>
&opened_length_read,
)
.eval(builder, end_top_level);
self.memory_bridge
.read(
MemoryAddress::new(self.address_space, sibling_register),
[sibling_base_pointer],
very_first_timestamp + AB::F::from_canonical_usize(3),
&sibling_base_pointer_read,
)
.eval(builder, end_top_level);
self.memory_bridge
.read(
MemoryAddress::new(self.address_space, index_register),
[index_base_pointer],
very_first_timestamp + AB::F::from_canonical_usize(4),
very_first_timestamp + AB::F::from_canonical_usize(3),
&index_base_pointer_read,
)
.eval(builder, end_top_level);
self.memory_bridge
.read(
MemoryAddress::new(self.address_space, commit_register),
[commit_pointer],
very_first_timestamp + AB::F::from_canonical_usize(5),
very_first_timestamp + AB::F::from_canonical_usize(4),
&commit_pointer_read,
)
.eval(builder, end_top_level);
Expand All @@ -411,7 +399,7 @@ impl<AB: InteractionBuilder, const SBOX_REGISTERS: usize> Air<AB>
.read(
MemoryAddress::new(self.address_space, commit_pointer),
left_output,
very_first_timestamp + AB::F::from_canonical_usize(6),
very_first_timestamp + AB::F::from_canonical_usize(5),
&commit_read,
)
.eval(builder, end_top_level);
Expand All @@ -423,10 +411,6 @@ impl<AB: InteractionBuilder, const SBOX_REGISTERS: usize> Air<AB>
.assert_eq(next_top_level_specific.dim_base_pointer, dim_base_pointer);

when_top_level_not_end.assert_eq(next.opened_base_pointer, opened_base_pointer);
when_top_level_not_end.assert_eq(
next_top_level_specific.sibling_base_pointer,
sibling_base_pointer,
);
when_top_level_not_end.assert_eq(
next_top_level_specific.index_base_pointer,
index_base_pointer,
Expand Down Expand Up @@ -516,7 +500,7 @@ impl<AB: InteractionBuilder, const SBOX_REGISTERS: usize> Air<AB>
MemoryAddress::new(self.address_space, dim_base_pointer + final_opened_index),
[height],
end_timestamp - AB::F::ONE,
&read_final_height_or_sibling_array_start,
&read_final_height,
)
.eval(builder, incorporate_row);

Expand All @@ -543,17 +527,6 @@ impl<AB: InteractionBuilder, const SBOX_REGISTERS: usize> Air<AB>
&read_initial_height_or_root_is_on_right,
)
.eval(builder, incorporate_sibling);
self.memory_bridge
.read(
MemoryAddress::new(
self.address_space,
sibling_base_pointer + (proof_index * AB::F::TWO),
),
[sibling_array_start],
timestamp_after_initial_reads.clone() + AB::F::ONE,
&read_final_height_or_sibling_array_start,
)
.eval(builder, incorporate_sibling);

for i in 0..CHUNK {
builder
Expand All @@ -564,25 +537,11 @@ impl<AB: InteractionBuilder, const SBOX_REGISTERS: usize> Air<AB>
.when(next.incorporate_sibling)
.when(AB::Expr::ONE - next_top_level_specific.root_is_on_right)
.assert_eq(next_left_input[i], left_output[i]);

self.memory_bridge
.read(
MemoryAddress::new(
self.address_space,
sibling_array_start + AB::F::from_canonical_usize(i),
),
[(root_is_on_right * left_input[i])
+ ((AB::Expr::ONE - root_is_on_right) * right_input[i])],
timestamp_after_initial_reads.clone() + AB::F::from_canonical_usize(2 + i),
&reads[i],
)
.eval(builder, incorporate_sibling);
}

builder.when(incorporate_sibling).assert_eq(
end_timestamp,
timestamp_after_initial_reads + AB::F::from_canonical_usize(2 + CHUNK),
);
builder
.when(incorporate_sibling)
.assert_eq(end_timestamp, timestamp_after_initial_reads + AB::F::ONE);

//// simple permute

Expand Down
49 changes: 17 additions & 32 deletions extensions/native/circuit/src/poseidon2/chip.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use std::sync::{Arc, Mutex};
use std::{
array,
sync::{Arc, Mutex},
};

use openvm_circuit::{
arch::{ExecutionBridge, ExecutionError, ExecutionState, InstructionExecutor, SystemPort},
arch::{
ExecutionBridge, ExecutionError, ExecutionState, InstructionExecutor, Streams, SystemPort,
},
system::memory::{MemoryController, OfflineMemory, RecordId},
};
use openvm_instructions::{instruction::Instruction, program::DEFAULT_PC_STEP, LocalOpcode};
Expand Down Expand Up @@ -31,14 +36,12 @@ pub struct VerifyBatchRecord<F: Field> {
pub dim_base_pointer: F,
pub opened_base_pointer: F,
pub opened_length: usize,
pub sibling_base_pointer: F,
pub index_base_pointer: F,
pub commit_pointer: F,

pub dim_base_pointer_read: RecordId,
pub opened_base_pointer_read: RecordId,
pub opened_length_read: RecordId,
pub sibling_base_pointer_read: RecordId,
pub index_base_pointer_read: RecordId,
pub commit_pointer_read: RecordId,

Expand All @@ -65,10 +68,8 @@ pub struct TopLevelRecord<F: Field> {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound = "F: Field")]
pub struct IncorporateSiblingRecord<F: Field> {
pub read_sibling_array_start: RecordId,
pub read_root_is_on_right: RecordId,
pub root_is_on_right: bool,
pub reads: [RecordId; CHUNK],
pub p2_input: [F; 2 * CHUNK],
}

Expand Down Expand Up @@ -132,6 +133,7 @@ pub struct NativePoseidon2Chip<F: Field, const SBOX_REGISTERS: usize> {
pub(super) height: usize,
pub(super) offline_memory: Arc<Mutex<OfflineMemory<F>>>,
pub(super) subchip: Poseidon2SubChip<F, SBOX_REGISTERS>,
pub(super) streams: Arc<Mutex<Streams<F>>>,
}

impl<F: PrimeField32, const SBOX_REGISTERS: usize> Stateful<Vec<u8>>
Expand Down Expand Up @@ -163,6 +165,7 @@ impl<F: PrimeField32, const SBOX_REGISTERS: usize> NativePoseidon2Chip<F, SBOX_R
offline_memory: Arc<Mutex<OfflineMemory<F>>>,
poseidon2_config: Poseidon2Config<F>,
verify_batch_bus: VerifyBatchBus,
streams: Arc<Mutex<Streams<F>>>,
) -> Self {
let air = NativePoseidon2Air {
execution_bridge: ExecutionBridge::new(port.execution_bus, port.program_bus),
Expand All @@ -177,6 +180,7 @@ impl<F: PrimeField32, const SBOX_REGISTERS: usize> NativePoseidon2Chip<F, SBOX_R
height: 0,
offline_memory,
subchip: Poseidon2SubChip::new(poseidon2_config.constants),
streams,
}
}

Expand All @@ -188,7 +192,7 @@ impl<F: PrimeField32, const SBOX_REGISTERS: usize> NativePoseidon2Chip<F, SBOX_R
}
}

pub(super) const NUM_INITIAL_READS: usize = 7;
pub(super) const NUM_INITIAL_READS: usize = 6;
pub(super) const NUM_SIMPLE_ACCESSES: u32 = 7;

impl<F: PrimeField32, const SBOX_REGISTERS: usize> InstructionExecutor<F>
Expand Down Expand Up @@ -283,7 +287,7 @@ impl<F: PrimeField32, const SBOX_REGISTERS: usize> InstructionExecutor<F>
a: dim_register,
b: opened_register,
c: opened_length_register,
d: sibling_register,
d: sibling_id_ptr,
e: index_register,
f: commit_register,
g: opened_element_size_inv,
Expand All @@ -296,14 +300,13 @@ impl<F: PrimeField32, const SBOX_REGISTERS: usize> InstructionExecutor<F>
opened_element_size += F::ONE;
}

let sibling_id = memory.unsafe_read_cell(address_space, sibling_id_ptr);
let (dim_base_pointer_read, dim_base_pointer) =
memory.read_cell(address_space, dim_register);
let (opened_base_pointer_read, opened_base_pointer) =
memory.read_cell(address_space, opened_register);
let (opened_length_read, opened_length) =
memory.read_cell(address_space, opened_length_register);
let (sibling_base_pointer_read, sibling_base_pointer) =
memory.read_cell(address_space, sibling_register);
let (index_base_pointer_read, index_base_pointer) =
memory.read_cell(address_space, index_register);
let (commit_pointer_read, commit_pointer) =
Expand All @@ -321,6 +324,9 @@ impl<F: PrimeField32, const SBOX_REGISTERS: usize> InstructionExecutor<F>
let mut top_level = vec![];

let mut root = [F::ZERO; CHUNK];
let streams = self.streams.lock().unwrap();
let sibling_id_val = sibling_id.as_canonical_u32() as usize;
let sibling = &streams.hint_space[sibling_id_val];

while height >= 1 {
let incorporate_row = if opened_index < opened_length
Expand Down Expand Up @@ -452,24 +458,7 @@ impl<F: PrimeField32, const SBOX_REGISTERS: usize> InstructionExecutor<F>
index_base_pointer + F::from_canonical_usize(proof_index),
);
let root_is_on_right = root_is_on_right == F::ONE;

let (read_sibling_array_start, sibling_array_start) = memory.read_cell(
address_space,
sibling_base_pointer + F::from_canonical_usize(2 * proof_index),
);
let sibling_array_start = sibling_array_start.as_canonical_u32() as usize;

let mut sibling = [F::ZERO; CHUNK];
let mut reads = vec![];
for (i, sibling_elem) in sibling.iter_mut().enumerate().take(CHUNK) {
let (read, value) = memory.read_cell(
address_space,
F::from_canonical_usize(sibling_array_start + i),
);
*sibling_elem = value;
reads.push(read);
}

let sibling = array::from_fn(|i| sibling[proof_index * CHUNK + i]);
let (p2_input, new_root) = if root_is_on_right {
self.compress(sibling, root)
} else {
Expand All @@ -479,10 +468,8 @@ impl<F: PrimeField32, const SBOX_REGISTERS: usize> InstructionExecutor<F>

self.height += 1;
Some(IncorporateSiblingRecord {
read_sibling_array_start,
read_root_is_on_right,
root_is_on_right,
reads: std::array::from_fn(|i| reads[i]),
p2_input,
})
};
Expand All @@ -505,13 +492,11 @@ impl<F: PrimeField32, const SBOX_REGISTERS: usize> InstructionExecutor<F>
dim_base_pointer,
opened_base_pointer,
opened_length,
sibling_base_pointer,
index_base_pointer,
commit_pointer,
dim_base_pointer_read,
opened_base_pointer_read,
opened_length_read,
sibling_base_pointer_read,
index_base_pointer_read,
commit_pointer_read,
commit_read,
Expand Down
9 changes: 2 additions & 7 deletions extensions/native/circuit/src/poseidon2/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub struct TopLevelSpecificCols<T> {
pub dim_register: T,
pub opened_register: T,
pub opened_length_register: T,
pub sibling_register: T,
pub sibling_id: T,
pub index_register: T,
pub commit_register: T,

Expand All @@ -67,26 +67,21 @@ pub struct TopLevelSpecificCols<T> {
pub opened_length: T,

pub dim_base_pointer: T,
pub sibling_base_pointer: T,
pub index_base_pointer: T,

pub dim_base_pointer_read: MemoryReadAuxCols<T>,
pub opened_base_pointer_read: MemoryReadAuxCols<T>,
pub opened_length_read: MemoryReadAuxCols<T>,
pub sibling_base_pointer_read: MemoryReadAuxCols<T>,
pub index_base_pointer_read: MemoryReadAuxCols<T>,
pub commit_pointer_read: MemoryReadAuxCols<T>,

pub proof_index: T,

pub read_initial_height_or_root_is_on_right: MemoryReadAuxCols<T>,
pub read_final_height_or_sibling_array_start: MemoryReadAuxCols<T>,
pub read_final_height: MemoryReadAuxCols<T>,

// incorporate sibling only
pub root_is_on_right: T,
pub sibling_array_start: T,
pub reads: [MemoryReadAuxCols<T>; CHUNK],

pub commit_pointer: T,
pub commit_read: MemoryReadAuxCols<T>,
}
Expand Down
Loading

0 comments on commit cfb6eb2

Please sign in to comment.