Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Fix Serialization and ABI Compatibility Update grand_product_ex… #558

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions jolt-evm-verifier/script/src/bin/grand_product_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,75 @@ use ark_std::test_rng;
use jolt_core::utils::transcript::{KeccakTranscript, Transcript};

fn get_proof_data(batched_circuit: &mut BatchedDenseGrandProduct<Fr>) {
// Initialize a transcript for the proof generation process
let mut transcript: KeccakTranscript = KeccakTranscript::new(b"test_transcript");

// Generate the proof and prover randomness
let (proof, r_prover) =
<BatchedDenseGrandProduct<Fr> as BatchedGrandProduct<
Fr,
HyperKZG<Bn254, KeccakTranscript>,
KeccakTranscript,
>>::prove_grand_product(batched_circuit, None, &mut transcript, None);

// Retrieve the claimed outputs from the circuit
let claims = <BatchedDenseGrandProduct<Fr> as BatchedGrandProduct<
Fr,
HyperKZG<Bn254, KeccakTranscript>,
KeccakTranscript,
>>::claimed_outputs(batched_circuit);

// encoding the proof into abi
// Serialize the proof into bytes
let mut proof_bytes = vec![];
proof
.serialize_uncompressed(&mut proof_bytes)
.expect("Failed to serialize proof");

sol!(struct SolProductProofAndClaims{
GrandProductProof encoded_proof;
uint256[] claims;
uint256[] r_prover;
// Define a Solidity-compatible struct for ABI encoding
sol!(struct SolProductProofAndClaims {
bytes encoded_proof; // Use `bytes` for the serialized proof
uint256[] claims; // Array of claims as uint256
uint256[] r_prover; // Array of prover randomness as uint256
});

// Convert the prover randomness and claims to U256
let r_prover = r_prover.iter().map(fr_to_uint256).collect::<Vec<_>>();

let claims = claims.iter().map(fr_to_uint256).collect::<Vec<_>>();

// Create the struct instance for ABI encoding
let proof_plus_results = SolProductProofAndClaims {
encoded_proof: proof.into(),
encoded_proof: proof_bytes, // Use the serialized proof bytes
claims,
r_prover,
};

// Encode the struct into ABI and print it as a hex string
print!(
"{}",
hex::encode(SolProductProofAndClaims::abi_encode(&proof_plus_results))
);
}

// Helper function to convert a field element (`Fr`) to `U256`
fn fr_to_uint256(c: &Fr) -> U256 {
let mut serialize = vec![];
let _ = c.serialize_uncompressed(&mut serialize);
c.serialize_uncompressed(&mut serialize)
.expect("Serialization failed");
U256::from_le_slice(&serialize)
}

fn main() {
// Collect command-line arguments (unused in this example)
let _: Vec<_> = env::args().collect();

//initial test taken from https://github.com/a16z/jolt/blob/d5147f8d27bb4961f3d648b872b45ff99af860c0/jolt-core/src/subprotocols/grand_product.rs
const LAYER_SIZE: usize = 1 << 8;
const BATCH_SIZE: usize = 4;
// Define constants for the circuit
const LAYER_SIZE: usize = 1 << 8; // Layer size = 256
const BATCH_SIZE: usize = 4; // Batch size = 4

// Initialize a random number generator
let mut rng = test_rng();

// Generate random leaves for the circuit
let leaves: Vec<Vec<Fr>> = std::iter::repeat_with(|| {
std::iter::repeat_with(|| Fr::random(&mut rng))
.take(LAYER_SIZE)
Expand All @@ -73,11 +91,13 @@ fn main() {
.take(BATCH_SIZE)
.collect();

// Construct the batched grand product circuit
let mut batched_circuit = <BatchedDenseGrandProduct<Fr> as BatchedGrandProduct<
Fr,
HyperKZG<Bn254, KeccakTranscript>,
KeccakTranscript,
>>::construct((leaves.concat(), BATCH_SIZE));

// Generate and print the proof data
get_proof_data(&mut batched_circuit);
}