|
| 1 | +use super::FE; |
| 2 | +use crate::{fri::fri_commitment::FriCommitmentVec, PrimeField}; |
| 3 | +pub use lambdaworks_crypto::fiat_shamir::transcript::Transcript; |
| 4 | +use lambdaworks_crypto::merkle_tree::DefaultHasher; |
| 5 | + |
| 6 | +use lambdaworks_crypto::merkle_tree::proof::Proof; |
| 7 | + |
| 8 | +#[derive(Debug, Clone)] |
| 9 | +pub struct FriDecommitment { |
| 10 | + pub layer_merkle_paths: Vec<( |
| 11 | + Proof<PrimeField, DefaultHasher>, |
| 12 | + Proof<PrimeField, DefaultHasher>, |
| 13 | + )>, |
| 14 | + pub last_layer_evaluation: FE, |
| 15 | +} |
| 16 | + |
| 17 | +// verifier chooses a randomness and get the index where |
| 18 | +// they want to evaluate the poly |
| 19 | +// TODO: encapsulate the return type of this function in a struct. |
| 20 | +// This returns a list of authentication paths for evaluations on points and their symmetric counterparts. |
| 21 | +pub fn fri_decommit_layers( |
| 22 | + commit: &FriCommitmentVec<FE>, |
| 23 | + index_to_verify: usize, |
| 24 | +) -> FriDecommitment { |
| 25 | + let mut index = index_to_verify; |
| 26 | + |
| 27 | + let mut layer_merkle_paths = vec![]; |
| 28 | + |
| 29 | + // with every element of the commit, we look for that one in |
| 30 | + // the merkle tree and get the corresponding element |
| 31 | + for commit_i in commit { |
| 32 | + let length_i = commit_i.domain.len(); |
| 33 | + index %= length_i; |
| 34 | + let evaluation_i = commit_i.evaluation[index].clone(); |
| 35 | + let auth_path = commit_i.merkle_tree.get_proof(&evaluation_i).unwrap(); |
| 36 | + |
| 37 | + // symmetrical element |
| 38 | + let index_sym = (index + length_i / 2) % length_i; |
| 39 | + let evaluation_i_sym = commit_i.evaluation[index_sym].clone(); |
| 40 | + let auth_path_sym = commit_i.merkle_tree.get_proof(&evaluation_i_sym).unwrap(); |
| 41 | + |
| 42 | + layer_merkle_paths.push((auth_path, auth_path_sym)); |
| 43 | + } |
| 44 | + |
| 45 | + // send the last element of the polynomial |
| 46 | + let last = commit.last().unwrap(); |
| 47 | + let last_evaluation = last.poly.coefficients[0].clone(); |
| 48 | + |
| 49 | + FriDecommitment { |
| 50 | + layer_merkle_paths, |
| 51 | + last_layer_evaluation: last_evaluation, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Integration test: |
| 56 | +// * get an arbitrary polynomial |
| 57 | +// * have a domain containing roots of the unity (# is power of two) |
| 58 | +// p = 65_537 |
| 59 | +// * apply FRI commitment |
| 60 | +// * apply FRI decommitment |
| 61 | +// assert: |
| 62 | +// * evaluations of the polynomials coincide with calculations from the decommitment |
| 63 | +// * show a fail example: with a monomial |
| 64 | + |
| 65 | +#[cfg(test)] |
| 66 | +mod tests { |
| 67 | + use crate::fri::U64PrimeField; |
| 68 | + use lambdaworks_math::field::element::FieldElement; |
| 69 | + use std::collections::HashSet; |
| 70 | + const PRIME_GENERATOR: (u64, u64) = (0xFFFF_FFFF_0000_0001_u64, 2717_u64); |
| 71 | + pub type F = U64PrimeField<{ PRIME_GENERATOR.0 }>; |
| 72 | + pub type FeGoldilocks = FieldElement<F>; |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn test() { |
| 76 | + let subgroup_size = 1024_u64; |
| 77 | + let generator_field = FeGoldilocks::new(PRIME_GENERATOR.1); |
| 78 | + let exp = (PRIME_GENERATOR.0 - 1) / subgroup_size; |
| 79 | + let generator_of_subgroup = generator_field.pow(exp); |
| 80 | + let mut numbers = HashSet::new(); |
| 81 | + |
| 82 | + let mut i = 0; |
| 83 | + for exp in 0..1024_u64 { |
| 84 | + i += 1; |
| 85 | + let ret = generator_of_subgroup.pow(exp); |
| 86 | + numbers.insert(*ret.value()); |
| 87 | + println!("{ret:?}"); |
| 88 | + } |
| 89 | + |
| 90 | + let count = numbers.len(); |
| 91 | + println!("count: {count}"); |
| 92 | + println!("iter: {i}"); |
| 93 | + } |
| 94 | +} |
0 commit comments