openzeppelin_crypto/pedersen/
mod.rs1pub mod instance;
7pub mod params;
8
9use crate::{
10 curve::{AffineRepr, CurveConfig},
11 field::prime::PrimeField,
12 pedersen::params::PedersenParams,
13};
14
15#[derive(Clone, Debug)]
17pub struct Pedersen<F: PedersenParams<P>, P: CurveConfig>
18where
19 <P as CurveConfig>::BaseField: PrimeField,
20 F::AffineRepr: AffineRepr<
21 Config = P,
22 BaseField = P::BaseField,
23 ScalarField = P::ScalarField,
24 >,
25{
26 params: core::marker::PhantomData<F>,
27 curve: core::marker::PhantomData<P>,
28}
29
30impl<F: PedersenParams<P>, P: CurveConfig> Default for Pedersen<F, P>
31where
32 <P as CurveConfig>::BaseField: PrimeField,
33 F::AffineRepr: AffineRepr<
34 Config = P,
35 BaseField = P::BaseField,
36 ScalarField = P::ScalarField,
37 >,
38{
39 fn default() -> Self {
40 Self::new()
41 }
42}
43
44impl<F: PedersenParams<P>, P: CurveConfig> Pedersen<F, P>
45where
46 <P as CurveConfig>::BaseField: PrimeField,
47 F::AffineRepr: AffineRepr<
48 Config = P,
49 BaseField = P::BaseField,
50 ScalarField = P::ScalarField,
51 >,
52{
53 #[must_use]
54 #[inline]
55 pub fn new() -> Self {
57 Self {
58 params: core::marker::PhantomData,
59 curve: core::marker::PhantomData,
60 }
61 }
62
63 fn process_single_element(
64 element: P::BaseField,
65 p1: F::AffineRepr,
66 p2: F::AffineRepr,
67 ) -> <F::AffineRepr as AffineRepr>::Group {
68 let element = element.into_bigint();
69
70 let high_nibble = element >> F::LOW_PART_BITS;
71 let low_part = element & F::LOW_PART_MASK;
72
73 p1.mul_bigint(low_part) + p2.mul_bigint(high_nibble)
74 }
75
76 #[must_use]
95 pub fn hash<T>(&self, x: T, y: T) -> Option<P::BaseField>
96 where
97 T: Into<P::BaseField>,
98 {
99 let hash: <F::AffineRepr as AffineRepr>::Group = F::P_0.into_group()
100 + Self::process_single_element(x.into(), F::P_1, F::P_2)
101 + Self::process_single_element(y.into(), F::P_3, F::P_4);
102
103 let hash: F::AffineRepr = hash.into();
104 hash.x()
105 }
106}