circuit_verifying_key.rs
1 // Copyright (c) 2025-2026 ACDC Network 2 // This file is part of the alphavm library. 3 // 4 // Alpha Chain | Delta Chain Protocol 5 // International Monetary Graphite. 6 // 7 // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com). 8 // They built world-class ZK infrastructure. We installed the EASY button. 9 // Their cryptography: elegant. Our modifications: bureaucracy-compatible. 10 // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours. 11 // 12 // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0 13 // All modifications and new work: CC0 1.0 Universal Public Domain Dedication. 14 // No rights reserved. No permission required. No warranty. No refunds. 15 // 16 // https://creativecommons.org/publicdomain/zero/1.0/ 17 // SPDX-License-Identifier: CC0-1.0 18 19 use crate::{polycommit::sonic_pc, snark::varuna::ahp::indexer::*}; 20 use alphavm_curves::PairingEngine; 21 use alphavm_utilities::{into_io_error, serialize::*, FromBytes, FromBytesDeserializer, ToBytes, ToBytesSerializer}; 22 23 use anyhow::Result; 24 use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; 25 use std::{ 26 cmp::Ordering, 27 fmt, 28 io::{self, Read, Write}, 29 str::FromStr, 30 string::String, 31 }; 32 33 /// Verification key for a specific index (i.e., R1CS matrices). 34 #[derive(Debug, Clone, PartialEq, Eq, CanonicalSerialize, CanonicalDeserialize)] 35 pub struct CircuitVerifyingKey<E: PairingEngine> { 36 /// Stores information about the size of the circuit, as well as its defined 37 /// field. 38 pub circuit_info: CircuitInfo, 39 /// Commitments to the indexed polynomials. 40 pub circuit_commitments: Vec<sonic_pc::Commitment<E>>, 41 pub id: CircuitId, 42 } 43 44 impl<E: PairingEngine> FromBytes for CircuitVerifyingKey<E> { 45 fn read_le<R: Read>(r: R) -> io::Result<Self> { 46 Self::deserialize_compressed(r) 47 .map_err(|err| into_io_error(anyhow::Error::from(err).context("could not deserialize CircuitVerifyingKey"))) 48 } 49 50 fn read_le_unchecked<R: Read>(r: R) -> io::Result<Self> { 51 Self::deserialize_compressed_unchecked(r) 52 .map_err(|err| into_io_error(anyhow::Error::from(err).context("could not deserialize CircuitVerifyingKey"))) 53 } 54 } 55 56 impl<E: PairingEngine> ToBytes for CircuitVerifyingKey<E> { 57 fn write_le<W: Write>(&self, w: W) -> io::Result<()> { 58 self.serialize_compressed(w) 59 .map_err(|err| into_io_error(anyhow::Error::from(err).context("could not serialize CircuitVerifyingKey"))) 60 } 61 } 62 63 impl<E: PairingEngine> CircuitVerifyingKey<E> { 64 /// Iterate over the commitments to indexed polynomials in `self`. 65 pub fn iter(&self) -> impl Iterator<Item = &sonic_pc::Commitment<E>> { 66 self.circuit_commitments.iter() 67 } 68 } 69 70 impl<E: PairingEngine> FromStr for CircuitVerifyingKey<E> { 71 type Err = anyhow::Error; 72 73 #[inline] 74 fn from_str(vk_hex: &str) -> Result<Self, Self::Err> { 75 Self::from_bytes_le(&hex::decode(vk_hex)?) 76 } 77 } 78 79 impl<E: PairingEngine> fmt::Display for CircuitVerifyingKey<E> { 80 #[inline] 81 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 82 let vk_hex = hex::encode(self.to_bytes_le().expect("Failed to convert verifying key to bytes")); 83 write!(f, "{vk_hex}") 84 } 85 } 86 87 impl<E: PairingEngine> Serialize for CircuitVerifyingKey<E> { 88 #[inline] 89 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { 90 match serializer.is_human_readable() { 91 true => serializer.collect_str(self), 92 false => ToBytesSerializer::serialize_with_size_encoding(self, serializer), 93 } 94 } 95 } 96 97 impl<'de, E: PairingEngine> Deserialize<'de> for CircuitVerifyingKey<E> { 98 #[inline] 99 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { 100 match deserializer.is_human_readable() { 101 true => { 102 let s: String = Deserialize::deserialize(deserializer)?; 103 FromStr::from_str(&s).map_err(de::Error::custom) 104 } 105 false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "verifying key"), 106 } 107 } 108 } 109 110 impl<E: PairingEngine> Ord for CircuitVerifyingKey<E> { 111 fn cmp(&self, other: &Self) -> Ordering { 112 self.id.cmp(&other.id) 113 } 114 } 115 116 impl<E: PairingEngine> PartialOrd for CircuitVerifyingKey<E> { 117 fn partial_cmp(&self, other: &Self) -> Option<Ordering> { 118 Some(self.cmp(other)) 119 } 120 }