Skip to content

Commit b20ee94

Browse files
authored
recursion: reorganize crate (#157)
* recursion: reorganize crate * cleanup * cleanup doc * cleanup doc
1 parent eea4d24 commit b20ee94

30 files changed

+1527
-1446
lines changed

recursion/src/circuit_challenger.rs renamed to recursion/src/challenger/circuit.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
//! Circuit-based challenger implementation.
2-
//!
3-
//! This module provides a concrete implementation of `RecursiveChallenger`
4-
//! that uses the non-primitive `HashAbsorb` / `HashSqueeze` operations within the circuit.
52
63
use alloc::vec::Vec;
74

@@ -10,9 +7,9 @@ use p3_circuit::ops::HashOps;
107
use p3_field::Field;
118

129
use crate::Target;
13-
use crate::recursive_challenger::RecursiveChallenger;
10+
use crate::traits::RecursiveChallenger;
1411

15-
/// Concrete challenger implementation for Fiat-Shamir operations.
12+
/// Concrete challenger implementation for Fiat-Shamir operations in circuits.
1613
pub struct CircuitChallenger<const RATE: usize> {
1714
/// Buffer of field elements waiting to be absorbed
1815
absorb_buffer: Vec<Target>,
@@ -21,8 +18,8 @@ pub struct CircuitChallenger<const RATE: usize> {
2118
}
2219

2320
impl<const RATE: usize> CircuitChallenger<RATE> {
24-
/// Create a new circuit challenger.
25-
pub fn new() -> Self {
21+
/// Create a new circuit challenger with empty state.
22+
pub const fn new() -> Self {
2623
Self {
2724
absorb_buffer: Vec::new(),
2825
buffer_flushed: true,
@@ -40,6 +37,7 @@ impl<const RATE: usize> CircuitChallenger<RATE> {
4037
let reset = self.buffer_flushed;
4138

4239
// TODO: How do we want to handle padding?
40+
// Process buffer in chunks of RATE
4341
for chunk in self.absorb_buffer.chunks(RATE) {
4442
let _ = circuit.add_hash_absorb(chunk, reset);
4543
}
@@ -62,6 +60,7 @@ impl<F: Field, const RATE: usize> RecursiveChallenger<F> for CircuitChallenger<R
6260
}
6361

6462
fn sample(&mut self, circuit: &mut CircuitBuilder<F>) -> Target {
63+
// Flush any pending observations
6564
self.flush_absorb(circuit);
6665

6766
// TODO: We should be calling `add_hash_squeeze` but we may want to wait
@@ -122,4 +121,21 @@ mod tests {
122121
let challenges = challenger.sample_vec(&mut circuit, 3);
123122
assert_eq!(challenges.len(), 3);
124123
}
124+
125+
#[test]
126+
fn test_circuit_challenger_clear() {
127+
let mut circuit = CircuitBuilder::<BabyBear>::new();
128+
let mut challenger = CircuitChallenger::<DEFAULT_CHALLENGER_RATE>::new();
129+
130+
let val = circuit.add_const(BabyBear::ONE);
131+
RecursiveChallenger::<BabyBear>::observe(&mut challenger, &mut circuit, val);
132+
133+
assert!(!challenger.buffer_flushed);
134+
assert_eq!(challenger.absorb_buffer.len(), 1);
135+
136+
RecursiveChallenger::<BabyBear>::clear(&mut challenger);
137+
138+
assert!(challenger.buffer_flushed);
139+
assert!(challenger.absorb_buffer.is_empty());
140+
}
125141
}

recursion/src/challenger/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//! Challenger implementations for recursive Fiat-Shamir transformations.
2+
3+
mod circuit;
4+
5+
pub use circuit::CircuitChallenger;

0 commit comments

Comments
 (0)