monero_bulletproofs/plus/
mod.rs

1#![allow(non_snake_case)]
2
3use std_shims::sync::LazyLock;
4
5use curve25519_dalek::{constants::ED25519_BASEPOINT_POINT, scalar::Scalar, edwards::EdwardsPoint};
6
7use monero_generators::{H, Generators};
8
9pub(crate) use crate::{scalar_vector::ScalarVector, point_vector::PointVector};
10
11pub(crate) mod transcript;
12pub(crate) mod weighted_inner_product;
13pub(crate) use weighted_inner_product::*;
14pub(crate) mod aggregate_range_proof;
15pub(crate) use aggregate_range_proof::*;
16
17pub(crate) fn padded_pow_of_2(i: usize) -> usize {
18  let mut next_pow_of_2 = 1;
19  while next_pow_of_2 < i {
20    next_pow_of_2 <<= 1;
21  }
22  next_pow_of_2
23}
24
25#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
26pub(crate) enum GeneratorsList {
27  GBold,
28  HBold,
29}
30
31#[derive(Clone, Debug)]
32pub(crate) struct BpPlusGenerators {
33  g_bold: &'static [EdwardsPoint],
34  h_bold: &'static [EdwardsPoint],
35}
36
37include!(concat!(env!("OUT_DIR"), "/generators_plus.rs"));
38
39impl BpPlusGenerators {
40  #[allow(clippy::new_without_default)]
41  pub(crate) fn new() -> Self {
42    let gens = &GENERATORS;
43    BpPlusGenerators { g_bold: &gens.G, h_bold: &gens.H }
44  }
45
46  pub(crate) fn len(&self) -> usize {
47    self.g_bold.len()
48  }
49
50  pub(crate) fn g() -> EdwardsPoint {
51    *H
52  }
53
54  pub(crate) fn h() -> EdwardsPoint {
55    ED25519_BASEPOINT_POINT
56  }
57
58  pub(crate) fn generator(&self, list: GeneratorsList, i: usize) -> EdwardsPoint {
59    match list {
60      GeneratorsList::GBold => self.g_bold[i],
61      GeneratorsList::HBold => self.h_bold[i],
62    }
63  }
64
65  pub(crate) fn reduce(&self, generators: usize) -> Self {
66    // Round to the nearest power of 2
67    let generators = padded_pow_of_2(generators);
68    assert!(generators <= self.g_bold.len());
69
70    BpPlusGenerators { g_bold: &self.g_bold[.. generators], h_bold: &self.h_bold[.. generators] }
71  }
72}
73
74// Returns the little-endian decomposition.
75fn u64_decompose(value: u64) -> ScalarVector {
76  let mut bits = ScalarVector::new(64);
77  for bit in 0 .. 64 {
78    bits[bit] = Scalar::from((value >> bit) & 1);
79  }
80  bits
81}