1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2#![doc = include_str!("../README.md")]
3#![deny(missing_docs)]
4#![cfg_attr(not(feature = "std"), no_std)]
5
6use std_shims::{sync::LazyLock, vec::Vec};
7
8use sha3::{Digest, Keccak256};
9
10use curve25519_dalek::{constants::ED25519_BASEPOINT_POINT, edwards::EdwardsPoint};
11
12use monero_io::{write_varint, CompressedPoint};
13
14mod hash_to_point;
15pub use hash_to_point::biased_hash_to_point;
16
17#[cfg(test)]
18mod tests;
19
20fn keccak256(data: &[u8]) -> [u8; 32] {
21 Keccak256::digest(data).into()
22}
23
24#[allow(non_snake_case)]
29pub static H: LazyLock<EdwardsPoint> = LazyLock::new(|| {
30 CompressedPoint::from(keccak256(&ED25519_BASEPOINT_POINT.compress().to_bytes()))
31 .decompress()
32 .expect("known on-curve point wasn't on-curve")
33 .mul_by_cofactor()
34});
35
36static H_POW_2_CELL: LazyLock<[EdwardsPoint; 64]> = LazyLock::new(|| {
37 let mut res = [*H; 64];
38 for i in 1 .. 64 {
39 res[i] = res[i - 1] + res[i - 1];
40 }
41 res
42});
43#[allow(non_snake_case)]
47pub fn H_pow_2() -> &'static [EdwardsPoint; 64] {
48 &H_POW_2_CELL
49}
50
51pub const MAX_BULLETPROOF_COMMITMENTS: usize = 16;
53pub const COMMITMENT_BITS: usize = 64;
55
56#[allow(non_snake_case)]
58pub struct Generators {
59 pub G: Vec<EdwardsPoint>,
61 pub H: Vec<EdwardsPoint>,
63}
64
65pub fn bulletproofs_generators(dst: &'static [u8]) -> Generators {
70 const MAX_MN: usize = MAX_BULLETPROOF_COMMITMENTS * COMMITMENT_BITS;
72
73 let mut preimage = H.compress().to_bytes().to_vec();
74 preimage.extend(dst);
75
76 let mut res = Generators { G: Vec::with_capacity(MAX_MN), H: Vec::with_capacity(MAX_MN) };
77 for i in 0 .. MAX_MN {
78 let i = 2 * i;
80
81 let mut even = preimage.clone();
82 write_varint(&i, &mut even).expect("write failed but <Vec as io::Write> doesn't fail");
83 res.H.push(biased_hash_to_point(keccak256(&even)));
84
85 let mut odd = preimage.clone();
86 write_varint(&(i + 1), &mut odd).expect("write failed but <Vec as io::Write> doesn't fail");
87 res.G.push(biased_hash_to_point(keccak256(&odd)));
88 }
89 res
90}