diff --git a/prover/scripts/e2e.rs b/prover/scripts/e2e.rs index f6cd434aa2..379e95d0e9 100644 --- a/prover/scripts/e2e.rs +++ b/prover/scripts/e2e.rs @@ -92,6 +92,6 @@ pub fn main() { println!( "{:?}", - String::from_utf8(hex::encode(proof.encoded_proof)).unwrap() + String::from_utf8(hex::encode(proof.encoded_proof())).unwrap() ); } diff --git a/prover/src/verify.rs b/prover/src/verify.rs index ed47cee3d3..9b9af6fa64 100644 --- a/prover/src/verify.rs +++ b/prover/src/verify.rs @@ -400,13 +400,14 @@ impl SP1Prover { ) -> Result<()> { let prover = PlonkBn254Prover::new(); - let vkey_hash = BigUint::from_str(&proof.public_inputs[0])?; - let committed_values_digest = BigUint::from_str(&proof.public_inputs[1])?; + let public_inputs = proof.public_inputs(); + let vkey_hash = BigUint::from_str(&public_inputs[0])?; + let committed_values_digest = BigUint::from_str(&public_inputs[1])?; // Verify the proof with the corresponding public inputs. prover.verify(proof, &vkey_hash, &committed_values_digest, build_dir); - verify_plonk_bn254_public_inputs(vk, public_values, &proof.public_inputs)?; + verify_plonk_bn254_public_inputs(vk, public_values, &public_inputs)?; Ok(()) } diff --git a/recursion/gnark-ffi/src/plonk_bn254.rs b/recursion/gnark-ffi/src/plonk_bn254.rs index 2b7c071f6c..8bf9037e62 100644 --- a/recursion/gnark-ffi/src/plonk_bn254.rs +++ b/recursion/gnark-ffi/src/plonk_bn254.rs @@ -24,10 +24,42 @@ pub struct PlonkBn254Prover; /// A zero-knowledge proof generated by the PLONK protocol with a Base64 encoded gnark PLONK proof. #[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct PlonkBn254Proof { - pub public_inputs: [String; 2], - pub encoded_proof: String, - pub raw_proof: String, - pub plonk_vkey_hash: [u8; 32], + pub(crate) public_inputs: [String; 2], + pub(crate) encoded_proof: String, + pub(crate) raw_proof: String, + pub(crate) plonk_vkey_hash: [u8; 32], +} + +impl PlonkBn254Proof { + pub fn new( + public_inputs: [String; 2], + encoded_proof: String, + raw_proof: String, + plonk_vkey_hash: [u8; 32], + ) -> Self { + Self { + public_inputs, + encoded_proof, + raw_proof, + plonk_vkey_hash, + } + } + + pub fn public_inputs(&self) -> [String; 2] { + self.public_inputs.clone() + } + + pub fn encoded_proof(&self) -> String { + self.encoded_proof.clone() + } + + pub fn raw_proof(&self) -> String { + self.raw_proof.clone() + } + + pub fn plonk_vkey_hash(&self) -> [u8; 32] { + self.plonk_vkey_hash + } } impl PlonkBn254Prover { diff --git a/sdk/src/proof.rs b/sdk/src/proof.rs index 86d8939f74..a60295138c 100644 --- a/sdk/src/proof.rs +++ b/sdk/src/proof.rs @@ -43,7 +43,7 @@ impl SP1ProofWithPublicValues { /// Returns the raw proof as a string. pub fn raw(&self) -> String { match &self.proof { - SP1Proof::Plonk(plonk) => plonk.raw_proof.clone(), + SP1Proof::Plonk(plonk) => plonk.raw_proof().clone(), _ => unimplemented!(), } } @@ -53,11 +53,11 @@ impl SP1ProofWithPublicValues { pub fn bytes(&self) -> Vec { match &self.proof { SP1Proof::Plonk(plonk_proof) => { - let mut bytes = Vec::with_capacity(4 + plonk_proof.encoded_proof.len()); - bytes.extend_from_slice(&plonk_proof.plonk_vkey_hash[..4]); - bytes.extend_from_slice( - &hex::decode(&plonk_proof.encoded_proof).expect("Invalid Plonk proof"), - ); + let encoded_proof = plonk_proof.encoded_proof(); + let vkey_hash = plonk_proof.plonk_vkey_hash(); + let mut bytes = Vec::with_capacity(4 + encoded_proof.len()); + bytes.extend_from_slice(&vkey_hash[..4]); + bytes.extend_from_slice(encoded_proof.as_bytes()); bytes } _ => unimplemented!("only Plonk proofs are verifiable onchain"), diff --git a/sdk/src/provers/mock.rs b/sdk/src/provers/mock.rs index 893643ba6b..8c845107c3 100644 --- a/sdk/src/provers/mock.rs +++ b/sdk/src/provers/mock.rs @@ -94,15 +94,15 @@ impl Prover for MockProver { SP1ProofKind::Plonk => { let (public_values, _) = self.prover.execute(&pk.elf, &stdin, context)?; Ok(SP1ProofWithPublicValues { - proof: SP1Proof::Plonk(PlonkBn254Proof { - public_inputs: [ + proof: SP1Proof::Plonk(PlonkBn254Proof::new( + [ pk.vk.hash_bn254().as_canonical_biguint().to_string(), public_values.hash().to_string(), ], - encoded_proof: "".to_string(), - raw_proof: "".to_string(), - plonk_vkey_hash: [0; 32], - }), + "".to_string(), + "".to_string(), + [0; 32], + )), stdin, public_values, sp1_version: self.version().to_string(), @@ -117,10 +117,12 @@ impl Prover for MockProver { vkey: &SP1VerifyingKey, ) -> Result<(), SP1VerificationError> { match &bundle.proof { - SP1Proof::Plonk(PlonkBn254Proof { public_inputs, .. }) => { - verify_plonk_bn254_public_inputs(vkey, &bundle.public_values, public_inputs) - .map_err(SP1VerificationError::Plonk) - } + SP1Proof::Plonk(plonk) => verify_plonk_bn254_public_inputs( + vkey, + &bundle.public_values, + &plonk.public_inputs(), + ) + .map_err(SP1VerificationError::Plonk), _ => Ok(()), } }