1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
pub mod error;
pub mod mlsag;
pub mod ringct;

// re-export deps used in our public API
pub use bls_bulletproofs::{self, blstrs, group, rand};
#[cfg(feature = "serde")]
pub use serde;

use bls_bulletproofs::{
    blstrs::{G1Projective, Scalar},
    group::{ff::Field, Group},
    rand::RngCore,
    PedersenGens,
};

pub use error::Error;
pub use mlsag::{DecoyInput, MlsagMaterial, MlsagSignature, TrueInput};
pub use ringct::{Output, RingCtMaterial};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

pub type Result<T> = std::result::Result<T, Error>;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy)]
pub struct RevealedCommitment {
    pub value: u64,
    pub blinding: Scalar,
}

impl RevealedCommitment {
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut v: Vec<u8> = Default::default();
        v.extend(&self.value.to_le_bytes());
        v.extend(&self.blinding.to_bytes_le());
        v
    }

    /// Construct a revealed commitment from a value, generating a blinding randomly
    pub fn from_value(value: u64, mut rng: impl RngCore) -> Self {
        Self {
            value,
            blinding: Scalar::random(&mut rng),
        }
    }

    pub fn commit(&self, pc_gens: &PedersenGens) -> G1Projective {
        pc_gens.commit(Scalar::from(self.value), self.blinding)
    }

    pub fn value(&self) -> u64 {
        self.value
    }

    pub fn blinding(&self) -> Scalar {
        self.blinding
    }
}

/// Hashes a point to another point on the G1 curve
pub fn hash_to_curve(p: G1Projective) -> G1Projective {
    const DOMAIN: &[u8; 25] = b"blst-ringct-hash-to-curve";
    G1Projective::hash_to_curve(&p.to_compressed(), DOMAIN, &[])
}

pub fn public_key<S: Into<Scalar>>(secret_key: S) -> G1Projective {
    G1Projective::generator() * secret_key.into()
}

/// returns KeyImage for the given public/secret key pair
/// A key image is defined to be I = x * Hp(P)
pub fn key_image<S: Into<Scalar>>(secret_key: S) -> G1Projective {
    let sk = secret_key.into();
    hash_to_curve(public_key(sk)) * sk
}

#[cfg(test)]
mod tests {

    #[test]
    fn it_works() {
        let result = 2 + 2;
        assert_eq!(result, 4);
    }
}