Skip to content

Commit 23849da

Browse files
authored
Merge pull request #12 from yugocabrio/clean1216
feat: add proof size consts and drop unused loader
2 parents 1bbba84 + f8c6101 commit 23849da

3 files changed

Lines changed: 19 additions & 43 deletions

File tree

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ pub mod transcript;
1414
pub mod types;
1515
pub mod utils;
1616
pub mod verifier;
17-
pub use utils::load_proof_and_public_inputs;
17+
pub const PROOF_FIELDS: usize = 456;
18+
pub const PROOF_BYTES: usize = PROOF_FIELDS * 32;
19+
1820
pub use verifier::UltraHonkVerifier;

src/utils.rs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -347,41 +347,3 @@ pub fn load_vk_from_json(json_data: &str) -> VerificationKey {
347347
lagrange_last,
348348
}
349349
}
350-
351-
/// Load a VerificationKey from a `vk` binary emitted by `bb write_vk --output_format bytes_and_fields`.
352-
/// This parser assumes the `vk` file contains a flat sequence of BN254 G1 affine elements (x||y),
353-
/// each coordinate encoded as 32-byte big-endian field element. If the length does not match an
354-
/// integral number of points, or on-curve checks fail, this function returns None.
355-
356-
/// Load a proof and public inputs from a byte array.
357-
pub fn load_proof_and_public_inputs(bytes: &[u8]) -> (Vec<Fr>, Vec<u8>) {
358-
// First 4 bytes = total number of field elements (big-endian)
359-
let total_fields = u32::from_be_bytes(bytes[0..4].try_into().unwrap()) as usize;
360-
361-
// Proof is always 440 field elements
362-
const PROOF_NUM_FIELDS: usize = 440;
363-
assert!(
364-
total_fields >= PROOF_NUM_FIELDS,
365-
"total_fields < proof field count"
366-
);
367-
let num_inputs = total_fields - PROOF_NUM_FIELDS;
368-
369-
// Next num_inputs × 32 bytes = public inputs
370-
let mut public_inputs = Vec::with_capacity(num_inputs);
371-
let mut cursor = 4; // start right after the 4-byte header
372-
for _ in 0..num_inputs {
373-
let mut bytes32 = [0u8; 32];
374-
bytes32.copy_from_slice(&bytes[cursor..cursor + 32]);
375-
public_inputs.push(bytes_to_fr(&bytes32));
376-
cursor += 32;
377-
}
378-
379-
// Remaining bytes = proof (must be 440 × 32 bytes)
380-
let proof_bytes = bytes[cursor..].to_vec();
381-
assert!(
382-
proof_bytes.len() == PROOF_NUM_FIELDS * 32,
383-
"invalid proof length"
384-
);
385-
386-
(public_inputs, proof_bytes)
387-
}

src/verifier.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,22 @@ impl UltraHonkVerifier {
6161
// 1) parse proof
6262
let proof = load_proof(proof_bytes);
6363

64-
// 2) sanity on public inputs
65-
// If VK metadata is missing/zero, fall back to actual provided public inputs.
66-
// Newer bb versions may omit header fields in vk_fields.json.
67-
// We avoid failing hard here and instead trust caller-provided inputs.
64+
// 2) sanity on public inputs (length and VK metadata if present)
65+
if public_inputs_bytes.iter().any(|pi| pi.len() != 32) {
66+
return Err(VerifyError::InvalidInput(
67+
"public inputs must be 32 bytes each".into(),
68+
));
69+
}
70+
if self.vk.public_inputs_size != 0 {
71+
let expected = self.vk.public_inputs_size.saturating_sub(16);
72+
let provided = public_inputs_bytes.len() as u64;
73+
if expected != provided {
74+
return Err(VerifyError::InvalidInput(format!(
75+
"public inputs count mismatch (vk: {}, provided: {})",
76+
expected, provided
77+
)));
78+
}
79+
}
6880

6981
// 3) Fiat–Shamir transcript
7082
// In bb v0.87.0, publicInputsSize includes pairing point object (16 elements)

0 commit comments

Comments
 (0)