|
| 1 | +use crate::{common::*, ProvingKey, QuadraticArithmeticProgram}; |
| 2 | +use lambdaworks_math::errors::DeserializationError; |
| 3 | +use lambdaworks_math::traits::{Deserializable, Serializable}; |
| 4 | +use lambdaworks_math::{cyclic_group::IsGroup, msm::pippenger::msm}; |
| 5 | +use std::mem::size_of; |
| 6 | + |
| 7 | +pub struct Proof { |
| 8 | + pub pi1: G1Point, |
| 9 | + pub pi2: G2Point, |
| 10 | + pub pi3: G1Point, |
| 11 | +} |
| 12 | + |
| 13 | +impl Proof { |
| 14 | + pub fn serialize(&self) -> Vec<u8> { |
| 15 | + let mut bytes: Vec<u8> = Vec::new(); |
| 16 | + [ |
| 17 | + Self::serialize_commitment(&self.pi1), |
| 18 | + Self::serialize_commitment(&self.pi2), |
| 19 | + Self::serialize_commitment(&self.pi3), |
| 20 | + ] |
| 21 | + .iter() |
| 22 | + .for_each(|serialized| { |
| 23 | + bytes.extend_from_slice(&(serialized.len() as u32).to_be_bytes()); |
| 24 | + bytes.extend_from_slice(serialized); |
| 25 | + }); |
| 26 | + bytes |
| 27 | + } |
| 28 | + |
| 29 | + pub fn deserialize(bytes: &[u8]) -> Result<Self, DeserializationError> |
| 30 | + where |
| 31 | + Self: Sized, |
| 32 | + { |
| 33 | + let (offset, pi1) = Self::deserialize_commitment::<G1Point>(bytes, 0)?; |
| 34 | + let (offset, pi2) = Self::deserialize_commitment::<G2Point>(bytes, offset)?; |
| 35 | + let (_, pi3) = Self::deserialize_commitment::<G1Point>(bytes, offset)?; |
| 36 | + Ok(Self { pi1, pi2, pi3 }) |
| 37 | + } |
| 38 | + |
| 39 | + fn serialize_commitment<Commitment: Serializable>(cm: &Commitment) -> Vec<u8> { |
| 40 | + cm.serialize() |
| 41 | + } |
| 42 | + |
| 43 | + // Repetitive. Same as in plonk/src/prover.rs |
| 44 | + fn deserialize_commitment<Commitment: Deserializable>( |
| 45 | + bytes: &[u8], |
| 46 | + offset: usize, |
| 47 | + ) -> Result<(usize, Commitment), DeserializationError> { |
| 48 | + let mut offset = offset; |
| 49 | + let element_size_bytes: [u8; size_of::<u32>()] = bytes |
| 50 | + .get(offset..offset + size_of::<u32>()) |
| 51 | + .ok_or(DeserializationError::InvalidAmountOfBytes)? |
| 52 | + .try_into() |
| 53 | + .map_err(|_| DeserializationError::InvalidAmountOfBytes)?; |
| 54 | + let element_size = u32::from_be_bytes(element_size_bytes) as usize; |
| 55 | + offset += size_of::<u32>(); |
| 56 | + let commitment = Commitment::deserialize( |
| 57 | + bytes |
| 58 | + .get(offset..offset + element_size) |
| 59 | + .ok_or(DeserializationError::InvalidAmountOfBytes)?, |
| 60 | + )?; |
| 61 | + offset += element_size; |
| 62 | + Ok((offset, commitment)) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +pub struct Prover; |
| 67 | +impl Prover { |
| 68 | + pub fn prove(w: &[FrElement], qap: &QuadraticArithmeticProgram, pk: &ProvingKey) -> Proof { |
| 69 | + let h_coefficients = qap |
| 70 | + .calculate_h_coefficients(w) |
| 71 | + .iter() |
| 72 | + .map(|elem| elem.representative()) |
| 73 | + .collect::<Vec<_>>(); |
| 74 | + |
| 75 | + let w = w |
| 76 | + .iter() |
| 77 | + .map(|elem| elem.representative()) |
| 78 | + .collect::<Vec<_>>(); |
| 79 | + |
| 80 | + // Sample randomness for hiding |
| 81 | + let r = sample_fr_elem(); |
| 82 | + let s = sample_fr_elem(); |
| 83 | + |
| 84 | + // [π_1]_1 |
| 85 | + let pi1 = msm(&w, &pk.l_tau_g1) |
| 86 | + .unwrap() |
| 87 | + .operate_with(&pk.alpha_g1) |
| 88 | + .operate_with(&pk.delta_g1.operate_with_self(r.representative())); |
| 89 | + |
| 90 | + // [π_2]_2 |
| 91 | + let pi2 = msm(&w, &pk.r_tau_g2) |
| 92 | + .unwrap() |
| 93 | + .operate_with(&pk.beta_g2) |
| 94 | + .operate_with(&pk.delta_g2.operate_with_self(s.representative())); |
| 95 | + |
| 96 | + // [ƍ^{-1} * t(τ)*h(τ)]_1 |
| 97 | + let t_tau_h_tau_assigned_g1 = msm( |
| 98 | + &h_coefficients, |
| 99 | + &pk.z_powers_of_tau_g1[..h_coefficients.len()], |
| 100 | + ) |
| 101 | + .unwrap(); |
| 102 | + |
| 103 | + // [ƍ^{-1} * (β*l(τ) + α*r(τ) + o(τ))]_1 |
| 104 | + let k_tau_assigned_prover_g1 = msm( |
| 105 | + &w[qap.num_of_public_inputs..], |
| 106 | + &pk.prover_k_tau_g1[..qap.num_of_private_inputs()], |
| 107 | + ) |
| 108 | + .unwrap(); |
| 109 | + |
| 110 | + // [π_2]_1 |
| 111 | + let pi2_g1 = msm(&w, &pk.r_tau_g1) |
| 112 | + .unwrap() |
| 113 | + .operate_with(&pk.beta_g1) |
| 114 | + .operate_with(&pk.delta_g1.operate_with_self(s.representative())); |
| 115 | + |
| 116 | + // [π_3]_1 |
| 117 | + let pi3 = k_tau_assigned_prover_g1 |
| 118 | + .operate_with(&t_tau_h_tau_assigned_g1) |
| 119 | + // s[π_1]_1 |
| 120 | + .operate_with(&pi1.operate_with_self(s.representative())) |
| 121 | + // r[π_2]_1 |
| 122 | + .operate_with(&pi2_g1.operate_with_self(r.representative())) |
| 123 | + // -rs[ƍ]_1 |
| 124 | + .operate_with(&pk.delta_g1.operate_with_self((-(&r * &s)).representative())); |
| 125 | + |
| 126 | + Proof { pi1, pi2, pi3 } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +#[cfg(test)] |
| 131 | +mod tests { |
| 132 | + use lambdaworks_math::elliptic_curve::traits::IsEllipticCurve; |
| 133 | + |
| 134 | + use super::*; |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn serde() { |
| 138 | + let proof = Proof { |
| 139 | + pi1: Curve::generator().operate_with_self(sample_fr_elem().representative()), |
| 140 | + pi2: TwistedCurve::generator().operate_with_self(sample_fr_elem().representative()), |
| 141 | + pi3: Curve::generator().operate_with_self(sample_fr_elem().representative()), |
| 142 | + }; |
| 143 | + let deserialized_proof = Proof::deserialize(&proof.serialize()).unwrap(); |
| 144 | + |
| 145 | + assert_eq!(proof.pi1, deserialized_proof.pi1); |
| 146 | + assert_eq!(proof.pi2, deserialized_proof.pi2); |
| 147 | + assert_eq!(proof.pi3, deserialized_proof.pi3); |
| 148 | + } |
| 149 | +} |
0 commit comments