|
| 1 | +use ark_serialize::CanonicalSerialize; |
| 2 | +use banderwagon::Element; |
| 3 | + |
| 4 | +use crate::{ipa::slow_vartime_multiscalar_mul, lagrange_basis::LagrangeBasis}; |
| 5 | + |
| 6 | +#[derive(Debug, Clone)] |
| 7 | +pub struct CRS { |
| 8 | + pub n: usize, |
| 9 | + pub G: Vec<Element>, |
| 10 | + pub Q: Element, |
| 11 | +} |
| 12 | + |
| 13 | +impl CRS { |
| 14 | + pub fn new(n: usize, seed: &'static [u8]) -> CRS { |
| 15 | + // TODO generate the Q value from the seed also |
| 16 | + // TODO: this will also make assert_dedup work as expected |
| 17 | + // TODO: since we should take in `Q` too |
| 18 | + let G: Vec<_> = generate_random_elements(n, seed).into_iter().collect(); |
| 19 | + let Q = Element::prime_subgroup_generator(); |
| 20 | + |
| 21 | + CRS::assert_dedup(&G); |
| 22 | + |
| 23 | + CRS { n, G, Q } |
| 24 | + } |
| 25 | + // Asserts that not of the points generated are the same |
| 26 | + fn assert_dedup(points: &[Element]) { |
| 27 | + use std::collections::HashMap; |
| 28 | + let mut map = HashMap::new(); |
| 29 | + for point in points { |
| 30 | + assert!( |
| 31 | + map.insert(point.to_bytes(), ()).is_none(), |
| 32 | + "crs has duplicated points" |
| 33 | + ) |
| 34 | + } |
| 35 | + } |
| 36 | + pub fn commit_lagrange_poly(&self, polynomial: &LagrangeBasis) -> Element { |
| 37 | + slow_vartime_multiscalar_mul(polynomial.values().iter(), self.G.iter()) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl std::ops::Index<usize> for CRS { |
| 42 | + type Output = Element; |
| 43 | + |
| 44 | + fn index(&self, index: usize) -> &Self::Output { |
| 45 | + &self.G[index] |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +fn generate_random_elements(num_required_points: usize, seed: &'static [u8]) -> Vec<Element> { |
| 50 | + use ark_ec::group::Group; |
| 51 | + use ark_ff::PrimeField; |
| 52 | + use bandersnatch::Fq; |
| 53 | + use sha2::{Digest, Sha256}; |
| 54 | + |
| 55 | + let choose_largest = false; |
| 56 | + |
| 57 | + (0u64..) |
| 58 | + .into_iter() |
| 59 | + // Hash the seed + i to get a possible x value |
| 60 | + .map(|i| { |
| 61 | + let mut hasher = Sha256::new(); |
| 62 | + hasher.update(seed); |
| 63 | + hasher.update(&i.to_be_bytes()); |
| 64 | + let bytes: Vec<u8> = hasher.finalize().to_vec(); |
| 65 | + bytes |
| 66 | + }) |
| 67 | + // The Element::from_bytes method does not reduce the bytes, it expects the |
| 68 | + // input to be in a canonical format, so we must do the reduction ourselves |
| 69 | + .map(|hash_bytes| Fq::from_be_bytes_mod_order(&hash_bytes)) |
| 70 | + .map(|x_coord| { |
| 71 | + let mut bytes = [0u8; 32]; |
| 72 | + x_coord.serialize(&mut bytes[..]).unwrap(); |
| 73 | + // TODO: this reverse is hacky, and its because there is no way to specify the endianness in arkworks |
| 74 | + // TODO So we reverse it here, to be interopable with the banderwagon specs which needs big endian bytes |
| 75 | + bytes.reverse(); |
| 76 | + bytes |
| 77 | + }) |
| 78 | + // Deserialise the x-cordinate to get a valid banderwagon element |
| 79 | + .map(|bytes| Element::from_bytes(&bytes)) |
| 80 | + .filter_map(|point| point) |
| 81 | + .take(num_required_points) |
| 82 | + .collect() |
| 83 | +} |
| 84 | + |
| 85 | +#[test] |
| 86 | +fn crs_consistency() { |
| 87 | + // TODO: update hackmd as we are now using banderwagon and the point finding strategy |
| 88 | + // TODO is a bit different |
| 89 | + // See: https://hackmd.io/1RcGSMQgT4uREaq1CCx_cg#Methodology |
| 90 | + use ark_serialize::CanonicalSerialize; |
| 91 | + use bandersnatch::Fq; |
| 92 | + use sha2::{Digest, Sha256}; |
| 93 | + |
| 94 | + let points = generate_random_elements(256, b"eth_verkle_oct_2021"); |
| 95 | + |
| 96 | + let mut bytes = [0u8; 32]; |
| 97 | + points[0].serialize(&mut bytes[..]).unwrap(); |
| 98 | + assert_eq!( |
| 99 | + hex::encode(&bytes), |
| 100 | + "01587ad1336675eb912550ec2a28eb8923b824b490dd2ba82e48f14590a298a0", |
| 101 | + "the first point is incorrect" |
| 102 | + ); |
| 103 | + let mut bytes = [0u8; 32]; |
| 104 | + points[255].serialize(&mut bytes[..]).unwrap(); |
| 105 | + assert_eq!( |
| 106 | + hex::encode(&bytes), |
| 107 | + "3de2be346b539395b0c0de56a5ccca54a317f1b5c80107b0802af9a62276a4d8", |
| 108 | + "the 256th (last) point is incorrect" |
| 109 | + ); |
| 110 | + |
| 111 | + let mut hasher = Sha256::new(); |
| 112 | + for point in &points { |
| 113 | + let mut bytes = [0u8; 32]; |
| 114 | + point.serialize(&mut bytes[..]).unwrap(); |
| 115 | + hasher.update(&bytes); |
| 116 | + } |
| 117 | + let bytes = hasher.finalize().to_vec(); |
| 118 | + assert_eq!( |
| 119 | + hex::encode(&bytes), |
| 120 | + "1fcaea10bf24f750200e06fa473c76ff0468007291fa548e2d99f09ba9256fdb", |
| 121 | + "unexpected point encountered" |
| 122 | + ); |
| 123 | +} |
0 commit comments