Skip to content

Commit

Permalink
refactor: deprecate Challenges::count()
Browse files Browse the repository at this point in the history
Use associated constant `Challenges::COUNT` instead.
  • Loading branch information
jan-ferdinand committed Mar 14, 2024
1 parent 0d1d35e commit df783a0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
5 changes: 2 additions & 3 deletions triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl Stark {

prof_start!(maybe_profiler, "Fiat-Shamir", "hash");
proof_stream.enqueue(ProofItem::MerkleRoot(base_merkle_tree.root()));
let challenges = proof_stream.sample_scalars(Challenges::num_challenges_to_sample());
let challenges = proof_stream.sample_scalars(Challenges::SAMPLE_COUNT);
let challenges = Challenges::new(challenges, claim);
prof_stop!(maybe_profiler, "Fiat-Shamir");

Expand Down Expand Up @@ -711,8 +711,7 @@ impl Stark {

prof_start!(maybe_profiler, "Fiat-Shamir 1", "hash");
let base_merkle_tree_root = proof_stream.dequeue()?.try_into_merkle_root()?;
let extension_challenge_weights =
proof_stream.sample_scalars(Challenges::num_challenges_to_sample());
let extension_challenge_weights = proof_stream.sample_scalars(Challenges::SAMPLE_COUNT);
let challenges = Challenges::new(extension_challenge_weights, claim);
let extension_tree_merkle_root = proof_stream.dequeue()?.try_into_merkle_root()?;
// Sample weights for quotient codeword, which is a part of the combination codeword.
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl TasmConstraintEvaluationMemoryLayout {
MemoryRegion::new(self.curr_ext_row_ptr, NUM_EXT_COLUMNS),
MemoryRegion::new(self.next_base_row_ptr, NUM_BASE_COLUMNS),
MemoryRegion::new(self.next_ext_row_ptr, NUM_EXT_COLUMNS),
MemoryRegion::new(self.challenges_ptr, challenges::Challenges::count()),
MemoryRegion::new(self.challenges_ptr, challenges::Challenges::COUNT),
];
Box::new(all_regions)
}
Expand Down
28 changes: 17 additions & 11 deletions triton-vm/src/table/challenges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,12 @@ impl ChallengeId {
/// convenience methods.
#[derive(Debug, Clone, Arbitrary)]
pub struct Challenges {
pub challenges: [XFieldElement; Self::count()],
pub challenges: [XFieldElement; Self::COUNT],
}

impl Challenges {
/// The total number of challenges used in Triton VM.
pub const fn count() -> usize {
ChallengeId::COUNT
}
pub const COUNT: usize = ChallengeId::COUNT;

/// The number of weights to sample using the Fiat-Shamir heuristic. This number is lower
/// than the number of challenges because several challenges are not sampled, but computed
Expand All @@ -238,15 +236,23 @@ impl Challenges {
/// lookup table and the sampled indeterminate [`LookupTablePublicIndeterminate`].
/// - The [`CompressedProgramDigest`] is computed from the program to be executed and the
/// sampled indeterminate [`CompressProgramDigestIndeterminate`].
// When modifying this, be sure to add to the compile-time assertions in the
// `#[test] const fn compile_time_index_assertions() { … }`
// at the end of this file.
pub const SAMPLE_COUNT: usize = Self::COUNT - 4;

#[deprecated(since = "0.39.0", note = "Use `Self::COUNT` instead")]
pub const fn count() -> usize {
Self::COUNT
}

#[deprecated(since = "0.39.0", note = "Use `Self::SAMPLE_COUNT` instead")]
pub const fn num_challenges_to_sample() -> usize {
// When modifying this, be sure to add to the compile-time assertions in the
// `#[test] const fn compile_time_index_assertions() { … }`
// at the end of this file.
Self::count() - 4
Self::SAMPLE_COUNT
}

pub fn new(mut challenges: Vec<XFieldElement>, claim: &Claim) -> Self {
assert_eq!(Self::num_challenges_to_sample(), challenges.len());
assert_eq!(Self::SAMPLE_COUNT, challenges.len());

let compressed_digest = EvalArg::compute_terminal(
&claim.program_digest.values(),
Expand All @@ -273,7 +279,7 @@ impl Challenges {
challenges.insert(StandardOutputTerminal.index(), output_terminal);
challenges.insert(LookupTablePublicTerminal.index(), lookup_terminal);
challenges.insert(CompressedProgramDigest.index(), compressed_digest);
assert_eq!(Self::count(), challenges.len());
assert_eq!(Self::COUNT, challenges.len());
let challenges = challenges.try_into().unwrap();

Self { challenges }
Expand Down Expand Up @@ -319,7 +325,7 @@ pub(crate) mod tests {
/// Stand-in challenges for use in tests. For non-interactive STARKs, use the
/// Fiat-Shamir heuristic to derive the actual challenges.
pub fn placeholder(claim: &Claim) -> Self {
let stand_in_challenges = (1..=Self::num_challenges_to_sample())
let stand_in_challenges = (1..=Self::SAMPLE_COUNT)
.map(|i| xfe!([42, i as u64, 24]))
.collect();
Self::new(stand_in_challenges, claim)
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/table/constraint_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ mod tests {
println!("seed: {seed}");

let dummy_claim = Claim::default();
let challenges: [XFieldElement; Challenges::num_challenges_to_sample()] = rng.gen();
let challenges: [XFieldElement; Challenges::SAMPLE_COUNT] = rng.gen();
let challenges = challenges.to_vec();
let challenges = Challenges::new(challenges, &dummy_claim);

Expand Down Expand Up @@ -1967,7 +1967,7 @@ mod tests {

// Use the Schwartz-Zippel lemma to check no two substitution rules are equal.
let dummy_claim = Claim::default();
let challenges: [XFieldElement; Challenges::num_challenges_to_sample()] = rng.gen();
let challenges: [XFieldElement; Challenges::SAMPLE_COUNT] = rng.gen();
let challenges = challenges.to_vec();
let challenges = Challenges::new(challenges, &dummy_claim);

Expand Down

0 comments on commit df783a0

Please sign in to comment.