Skip to content

Commit

Permalink
Rename PC -> Bytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
moodlezoup committed Dec 4, 2023
1 parent 4d32731 commit 296a8be
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 60 deletions.
2 changes: 1 addition & 1 deletion jolt-core/src/jolt/trace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ark_ff::PrimeField;

use super::{
instruction::JoltInstruction,
vm::{pc::ELFRow, read_write_memory::MemoryOp},
vm::{bytecode::ELFRow, read_write_memory::MemoryOp},
};

trait JoltProvableTrace {
Expand Down
2 changes: 1 addition & 1 deletion jolt-core/src/jolt/trace/rv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::jolt::instruction::srl::SRLInstruction;
use crate::jolt::instruction::xor::XORInstruction;
use crate::jolt::instruction::JoltInstruction;
use crate::jolt::instruction::{add::ADDInstruction, sub::SUBInstruction};
use crate::jolt::vm::{pc::ELFRow, rv32i_vm::RV32I};
use crate::jolt::vm::{bytecode::ELFRow, rv32i_vm::RV32I};
use common::{constants::REGISTER_COUNT, RV32InstructionFormat, RV32IM};

use super::JoltProvableTrace;
Expand Down
97 changes: 51 additions & 46 deletions jolt-core/src/jolt/vm/pc.rs → jolt-core/src/jolt/vm/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl<F: PrimeField> FiveTuplePoly<F> {
}
}

pub struct PCPolys<F: PrimeField, G: CurveGroup<ScalarField = F>> {
pub struct BytecodePolynomials<F: PrimeField, G: CurveGroup<ScalarField = F>> {
_group: PhantomData<G>,
/// MLE of read/write addresses. For offline memory checking, each read is paired with a "virtual" write,
/// so the read addresses and write addresses are the same.
Expand All @@ -156,7 +156,7 @@ pub struct PCPolys<F: PrimeField, G: CurveGroup<ScalarField = F>> {
t_final: DensePolynomial<F>,
}

impl<F: PrimeField, G: CurveGroup<ScalarField = F>> PCPolys<F, G> {
impl<F: PrimeField, G: CurveGroup<ScalarField = F>> BytecodePolynomials<F, G> {
pub fn new_program(mut program: Vec<ELFRow>, mut trace: Vec<ELFRow>) -> Self {
Self::validate_program(&program, &trace);
Self::preprocess(&mut program, &mut trace);
Expand All @@ -168,7 +168,7 @@ impl<F: PrimeField, G: CurveGroup<ScalarField = F>> PCPolys<F, G> {
let num_ops = trace.len().next_power_of_two();
let code_size = program.len().next_power_of_two();

println!("PCPolys::new_program num_ops {num_ops} code_size {code_size}");
println!("BytecodePolynomials::new_program num_ops {num_ops} code_size {code_size}");

let mut a_read_write_usize: Vec<usize> = vec![0; num_ops];
let mut read_cts: Vec<usize> = vec![0; num_ops];
Expand Down Expand Up @@ -241,7 +241,7 @@ impl<F: PrimeField, G: CurveGroup<ScalarField = F>> PCPolys<F, G> {
}
}

pub struct BatchedPCPolys<F: PrimeField> {
pub struct BatchedBytecodePolynomials<F: PrimeField> {
/// Contains:
/// - a_read_write, t_read, v_read_write
combined_read_write: DensePolynomial<F>,
Expand All @@ -251,8 +251,8 @@ pub struct BatchedPCPolys<F: PrimeField> {
combined_init_final: DensePolynomial<F>,
}

pub struct ProgramCommitment<G: CurveGroup> {
generators: PCCommitmentGenerators<G>,
pub struct BytecodeCommitment<G: CurveGroup> {
generators: BytecodeCommitmentGenerators<G>,
/// Combined commitment for:
/// - a_read_write, t_read, v_read_write
pub read_write_commitments: CombinedTableCommitment<G>,
Expand All @@ -262,18 +262,18 @@ pub struct ProgramCommitment<G: CurveGroup> {
pub init_final_commitments: CombinedTableCommitment<G>,
}

pub struct PCCommitmentGenerators<G: CurveGroup> {
pub struct BytecodeCommitmentGenerators<G: CurveGroup> {
pub gens_read_write: PolyCommitmentGens<G>,
pub gens_init_final: PolyCommitmentGens<G>,
}

impl<F, G> BatchablePolynomials for PCPolys<F, G>
impl<F, G> BatchablePolynomials for BytecodePolynomials<F, G>
where
F: PrimeField,
G: CurveGroup<ScalarField = F>,
{
type BatchedPolynomials = BatchedPCPolys<F>;
type Commitment = ProgramCommitment<G>;
type BatchedPolynomials = BatchedBytecodePolynomials<F>;
type Commitment = BytecodeCommitment<G>;

fn batch(&self) -> Self::BatchedPolynomials {
let combined_read_write = DensePolynomial::merge(&vec![
Expand Down Expand Up @@ -303,12 +303,12 @@ where
fn commit(batched_polys: &Self::BatchedPolynomials) -> Self::Commitment {
let (gens_read_write, read_write_commitments) = batched_polys
.combined_read_write
.combined_commit(b"BatchedPCPolys.read_write");
.combined_commit(b"BatchedBytecodePolynomials.read_write");
let (gens_init_final, init_final_commitments) = batched_polys
.combined_init_final
.combined_commit(b"BatchedPCPolys.init_final");
.combined_commit(b"BatchedBytecodePolynomials.init_final");

let generators = PCCommitmentGenerators {
let generators = BytecodeCommitmentGenerators {
gens_read_write,
gens_init_final,
};
Expand All @@ -321,13 +321,13 @@ where
}
}

impl<F, G> MemoryCheckingProver<F, G, PCPolys<F, G>> for PCPolys<F, G>
impl<F, G> MemoryCheckingProver<F, G, BytecodePolynomials<F, G>> for BytecodePolynomials<F, G>
where
F: PrimeField,
G: CurveGroup<ScalarField = F>,
{
type ReadWriteOpenings = PCReadWriteOpenings<F, G>;
type InitFinalOpenings = PCInitFinalOpenings<F, G>;
type ReadWriteOpenings = BytecodeReadWriteOpenings<F, G>;
type InitFinalOpenings = BytecodeInitFinalOpenings<F, G>;

// [a, opcode, rd, rs1, rs2, imm, t]
type MemoryTuple = [F; 7];
Expand All @@ -344,14 +344,14 @@ where

fn read_leaves(
&self,
polynomials: &PCPolys<F, G>,
polynomials: &BytecodePolynomials<F, G>,
gamma: &F,
tau: &F,
) -> Vec<DensePolynomial<F>> {
let num_ops = polynomials.a_read_write.len();
let read_fingerprints = (0..num_ops)
.map(|i| {
<Self as MemoryCheckingProver<F, G, PCPolys<F, G>>>::fingerprint(
<Self as MemoryCheckingProver<F, G, BytecodePolynomials<F, G>>>::fingerprint(
&[
polynomials.a_read_write[i],
polynomials.v_read_write.opcode[i],
Expand All @@ -370,14 +370,14 @@ where
}
fn write_leaves(
&self,
polynomials: &PCPolys<F, G>,
polynomials: &BytecodePolynomials<F, G>,
gamma: &F,
tau: &F,
) -> Vec<DensePolynomial<F>> {
let num_ops = polynomials.a_read_write.len();
let read_fingerprints = (0..num_ops)
.map(|i| {
<Self as MemoryCheckingProver<F, G, PCPolys<F, G>>>::fingerprint(
<Self as MemoryCheckingProver<F, G, BytecodePolynomials<F, G>>>::fingerprint(
&[
polynomials.a_read_write[i],
polynomials.v_read_write.opcode[i],
Expand All @@ -396,14 +396,14 @@ where
}
fn init_leaves(
&self,
polynomials: &PCPolys<F, G>,
polynomials: &BytecodePolynomials<F, G>,
gamma: &F,
tau: &F,
) -> Vec<DensePolynomial<F>> {
let memory_size = polynomials.v_init_final.opcode.len();
let init_fingerprints = (0..memory_size)
.map(|i| {
<Self as MemoryCheckingProver<F, G, PCPolys<F, G>>>::fingerprint(
<Self as MemoryCheckingProver<F, G, BytecodePolynomials<F, G>>>::fingerprint(
&[
F::from(i as u64),
polynomials.v_init_final.opcode[i],
Expand All @@ -422,14 +422,14 @@ where
}
fn final_leaves(
&self,
polynomials: &PCPolys<F, G>,
polynomials: &BytecodePolynomials<F, G>,
gamma: &F,
tau: &F,
) -> Vec<DensePolynomial<F>> {
let memory_size = polynomials.v_init_final.opcode.len();
let init_fingerprints = (0..memory_size)
.map(|i| {
<Self as MemoryCheckingProver<F, G, PCPolys<F, G>>>::fingerprint(
<Self as MemoryCheckingProver<F, G, BytecodePolynomials<F, G>>>::fingerprint(
&[
F::from(i as u64),
polynomials.v_init_final.opcode[i],
Expand All @@ -452,7 +452,7 @@ where
}
}

impl<F, G> MemoryCheckingVerifier<F, G, PCPolys<F, G>> for PCPolys<F, G>
impl<F, G> MemoryCheckingVerifier<F, G, BytecodePolynomials<F, G>> for BytecodePolynomials<F, G>
where
F: PrimeField,
G: CurveGroup<ScalarField = F>,
Expand Down Expand Up @@ -508,7 +508,7 @@ where
}
}

pub struct PCReadWriteOpenings<F, G>
pub struct BytecodeReadWriteOpenings<F, G>
where
F: PrimeField,
G: CurveGroup<ScalarField = F>,
Expand All @@ -523,14 +523,14 @@ where
read_write_opening_proof: CombinedTableEvalProof<G>,
}

impl<F, G> StructuredOpeningProof<F, G, PCPolys<F, G>> for PCReadWriteOpenings<F, G>
impl<F, G> StructuredOpeningProof<F, G, BytecodePolynomials<F, G>> for BytecodeReadWriteOpenings<F, G>
where
F: PrimeField,
G: CurveGroup<ScalarField = F>,
{
type Openings = (F, Vec<F>, F);

fn open(polynomials: &PCPolys<F, G>, opening_point: &Vec<F>) -> Self::Openings {
fn open(polynomials: &BytecodePolynomials<F, G>, opening_point: &Vec<F>) -> Self::Openings {
(
polynomials.a_read_write.evaluate(&opening_point),
polynomials.v_read_write.evaluate(&opening_point),
Expand All @@ -539,8 +539,8 @@ where
}

fn prove_openings(
polynomials: &BatchedPCPolys<F>,
commitment: &ProgramCommitment<G>,
polynomials: &BatchedBytecodePolynomials<F>,
commitment: &BytecodeCommitment<G>,
opening_point: &Vec<F>,
openings: (F, Vec<F>, F),
transcript: &mut Transcript,
Expand Down Expand Up @@ -573,7 +573,7 @@ where

fn verify_openings(
&self,
commitment: &ProgramCommitment<G>,
commitment: &BytecodeCommitment<G>,
opening_point: &Vec<F>,
transcript: &mut Transcript,
) -> Result<(), ProofVerifyError> {
Expand All @@ -593,7 +593,7 @@ where
}
}

pub struct PCInitFinalOpenings<F, G>
pub struct BytecodeInitFinalOpenings<F, G>
where
F: PrimeField,
G: CurveGroup<ScalarField = F>,
Expand All @@ -608,23 +608,23 @@ where
init_final_opening_proof: CombinedTableEvalProof<G>,
}

impl<F, G> StructuredOpeningProof<F, G, PCPolys<F, G>> for PCInitFinalOpenings<F, G>
impl<F, G> StructuredOpeningProof<F, G, BytecodePolynomials<F, G>> for BytecodeInitFinalOpenings<F, G>
where
F: PrimeField,
G: CurveGroup<ScalarField = F>,
{
type Openings = (Vec<F>, F);

fn open(polynomials: &PCPolys<F, G>, opening_point: &Vec<F>) -> Self::Openings {
fn open(polynomials: &BytecodePolynomials<F, G>, opening_point: &Vec<F>) -> Self::Openings {
(
polynomials.v_init_final.evaluate(&opening_point),
polynomials.t_final.evaluate(&opening_point),
)
}

fn prove_openings(
polynomials: &BatchedPCPolys<F>,
commitment: &ProgramCommitment<G>,
polynomials: &BatchedBytecodePolynomials<F>,
commitment: &BytecodeCommitment<G>,
opening_point: &Vec<F>,
openings: Self::Openings,
transcript: &mut Transcript,
Expand Down Expand Up @@ -654,7 +654,7 @@ where

fn verify_openings(
&self,
commitment: &ProgramCommitment<G>,
commitment: &BytecodeCommitment<G>,
opening_point: &Vec<F>,
transcript: &mut Transcript,
) -> Result<(), ProofVerifyError> {
Expand Down Expand Up @@ -716,7 +716,8 @@ mod tests {
ELFRow::new(3, 16u64, 16u64, 16u64, 16u64, 16u64),
ELFRow::new(2, 8u64, 8u64, 8u64, 8u64, 8u64),
];
let polys: PCPolys<Fr, EdwardsProjective> = PCPolys::new_program(program, trace);
let polys: BytecodePolynomials<Fr, EdwardsProjective> =
BytecodePolynomials::new_program(program, trace);

let (gamma, tau) = (&Fr::from(100), &Fr::from(35));
let init_leaves: Vec<DensePolynomial<Fr>> = polys.init_leaves(&polys, gamma, tau);
Expand Down Expand Up @@ -747,13 +748,14 @@ mod tests {
ELFRow::new(3, 16u64, 16u64, 16u64, 16u64, 16u64),
ELFRow::new(2, 8u64, 8u64, 8u64, 8u64, 8u64),
];
let polys: PCPolys<Fr, EdwardsProjective> = PCPolys::new_program(program, trace);
let polys: BytecodePolynomials<Fr, EdwardsProjective> =
BytecodePolynomials::new_program(program, trace);

let mut transcript = Transcript::new(b"test_transcript");
let mut random_tape = RandomTape::new(b"test_tape");

let batched_polys = polys.batch();
let commitments = PCPolys::commit(&batched_polys);
let commitments = BytecodePolynomials::commit(&batched_polys);
let proof = polys.prove_memory_checking(
&polys,
&batched_polys,
Expand All @@ -763,7 +765,7 @@ mod tests {
);

let mut transcript = Transcript::new(b"test_transcript");
PCPolys::verify_memory_checking(proof, &commitments, &mut transcript)
BytecodePolynomials::verify_memory_checking(proof, &commitments, &mut transcript)
.expect("proof should verify");
}

Expand All @@ -782,9 +784,10 @@ mod tests {
ELFRow::new(4, 32u64, 32u64, 32u64, 32u64, 32u64),
];

let polys: PCPolys<Fr, EdwardsProjective> = PCPolys::new_program(program, trace);
let polys: BytecodePolynomials<Fr, EdwardsProjective> =
BytecodePolynomials::new_program(program, trace);
let batch = polys.batch();
let commitments = PCPolys::commit(&batch);
let commitments = BytecodePolynomials::commit(&batch);

let mut transcript = Transcript::new(b"test_transcript");
let mut random_tape = RandomTape::new(b"test_tape");
Expand All @@ -798,7 +801,7 @@ mod tests {
);

let mut transcript = Transcript::new(b"test_transcript");
PCPolys::verify_memory_checking(proof, &commitments, &mut transcript)
BytecodePolynomials::verify_memory_checking(proof, &commitments, &mut transcript)
.expect("should verify");
}

Expand All @@ -817,7 +820,8 @@ mod tests {
ELFRow::new(2, 8u64, 8u64, 8u64, 8u64, 8u64),
ELFRow::new(5, 0u64, 0u64, 0u64, 0u64, 0u64), // no_op: shouldn't exist in pgoram
];
let _polys: PCPolys<Fr, EdwardsProjective> = PCPolys::new_program(program, trace);
let _polys: BytecodePolynomials<Fr, EdwardsProjective> =
BytecodePolynomials::new_program(program, trace);
}

#[test]
Expand All @@ -833,6 +837,7 @@ mod tests {
ELFRow::new(3, 16u64, 16u64, 16u64, 16u64, 16u64),
ELFRow::new(2, 8u64, 8u64, 8u64, 8u64, 8u64),
];
let _polys: PCPolys<Fr, EdwardsProjective> = PCPolys::new_program(program, trace);
let _polys: BytecodePolynomials<Fr, EdwardsProjective> =
BytecodePolynomials::new_program(program, trace);
}
}
Loading

0 comments on commit 296a8be

Please sign in to comment.