Generate Bulletproofs(+) generators at compile time

Creates a new monero-generators crate so the monero crate can run the 
code in question at build time.

Saves several seconds from running the tests.

Closes https://github.com/serai-dex/serai/issues/101.
This commit is contained in:
Luke Parker
2022-08-21 06:36:53 -04:00
parent 577fe99a08
commit 603a3f8c9f
19 changed files with 274 additions and 133 deletions

View File

@@ -13,10 +13,9 @@ use dalek_ff_group::{Scalar, EdwardsPoint};
use multiexp::multiexp as multiexp_const;
use crate::{
H as DALEK_H, Commitment, hash, hash_to_scalar as dalek_hash,
ringct::hash_to_point::raw_hash_to_point, serialize::write_varint,
};
pub(crate) use monero_generators::Generators;
use crate::{H as DALEK_H, Commitment, hash_to_scalar as dalek_hash};
pub(crate) use crate::ringct::bulletproofs::scalar_vector::*;
// Bring things into ff/group
@@ -33,29 +32,6 @@ pub(crate) fn hash_to_scalar(data: &[u8]) -> Scalar {
pub(crate) const MAX_M: usize = 16;
pub(crate) const LOG_N: usize = 6; // 2 << 6 == N
pub(crate) const N: usize = 64;
pub(crate) const MAX_MN: usize = MAX_M * N;
pub(crate) struct Generators {
pub(crate) G: Vec<EdwardsPoint>,
pub(crate) H: Vec<EdwardsPoint>,
}
pub(crate) fn generators_core(prefix: &'static [u8]) -> Generators {
let mut res = Generators { G: Vec::with_capacity(MAX_MN), H: Vec::with_capacity(MAX_MN) };
for i in 0 .. MAX_MN {
let i = 2 * i;
let mut even = (*H).compress().to_bytes().to_vec();
even.extend(prefix);
let mut odd = even.clone();
write_varint(&i.try_into().unwrap(), &mut even).unwrap();
write_varint(&(i + 1).try_into().unwrap(), &mut odd).unwrap();
res.H.push(EdwardsPoint(raw_hash_to_point(hash(&even))));
res.G.push(EdwardsPoint(raw_hash_to_point(hash(&odd))));
}
res
}
pub(crate) fn prove_multiexp(pairs: &[(Scalar, EdwardsPoint)]) -> EdwardsPoint {
multiexp_const(pairs) * *INV_EIGHT
@@ -153,12 +129,6 @@ lazy_static! {
pub(crate) static ref TWO_N: ScalarVector = ScalarVector::powers(Scalar::from(2u8), N);
}
pub(crate) fn init() {
let _ = &*INV_EIGHT;
let _ = &*H;
let _ = &*TWO_N;
}
pub(crate) fn challenge_products(w: &[Scalar], winv: &[Scalar]) -> Vec<Scalar> {
let mut products = vec![Scalar::zero(); 1 << w.len()];
products[0] = winv[0];