-
Notifications
You must be signed in to change notification settings - Fork 10
/
module.rs
146 lines (124 loc) · 3.79 KB
/
module.rs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use core::marker::PhantomData;
use ark_ec::pairing::Pairing;
use ark_ec::pairing::PairingOutput;
use ark_ec::VariableBaseMSM;
use ark_ff::AdditiveGroup;
use ark_ff::Field;
use ark_std::borrow::Borrow;
use ark_std::vec::Vec;
pub trait BilinearModule: Send + Sync {
type Lhs: AdditiveGroup<Scalar = Self::ScalarField>;
type Rhs: AdditiveGroup<Scalar = Self::ScalarField>;
type Target: AdditiveGroup<Scalar = Self::ScalarField>;
type ScalarField: Field;
fn p(a: impl Borrow<Self::Lhs>, b: impl Borrow<Self::Rhs>) -> Self::Target;
fn ip<I, J>(f: I, g: J) -> Self::Target
where
I: Iterator,
J: Iterator,
I::Item: Borrow<Self::Lhs>,
J::Item: Borrow<Self::Rhs>,
{
f.zip(g).map(|(x, y)| Self::p(x.borrow(), y.borrow())).sum()
}
}
pub(crate) struct GtModule<P: Pairing> {
_pairing: PhantomData<P>,
}
pub(crate) struct G1Module<P: Pairing> {
_pairing: PhantomData<P>,
}
pub(crate) struct G2Module<P: Pairing> {
_pairing: PhantomData<P>,
}
pub(crate) struct FModule<P: Pairing> {
_pairing: PhantomData<P>,
}
pub(crate) struct PModule<P: Pairing> {
_pairing: PhantomData<P>,
}
impl<P: Pairing> BilinearModule for GtModule<P> {
type Lhs = PairingOutput<P>;
type Rhs = P::ScalarField;
type Target = PairingOutput<P>;
type ScalarField = P::ScalarField;
fn p(a: impl Borrow<Self::Lhs>, b: impl Borrow<Self::Rhs>) -> Self::Target {
*a.borrow() * b.borrow()
}
}
impl<P: Pairing> BilinearModule for PModule<P> {
type Lhs = P::G1;
type Rhs = P::G2;
type Target = PairingOutput<P>;
type ScalarField = P::ScalarField;
fn p(a: impl Borrow<Self::Lhs>, b: impl Borrow<Self::Rhs>) -> Self::Target {
P::pairing(a.borrow(), b.borrow())
}
fn ip<I, J>(f: I, g: J) -> Self::Target
where
I: Iterator,
J: Iterator,
I::Item: Borrow<Self::Lhs>,
J::Item: Borrow<Self::Rhs>,
{
P::multi_pairing(f.map(|x| *x.borrow()), g.map(|x| *x.borrow()))
}
}
impl<P: Pairing> BilinearModule for G1Module<P> {
type Lhs = P::G1;
type Rhs = P::ScalarField;
type Target = P::G1;
type ScalarField = P::ScalarField;
fn p(a: impl Borrow<Self::Lhs>, b: impl Borrow<Self::Rhs>) -> Self::Target {
*a.borrow() * b.borrow()
}
fn ip<I, J>(f: I, g: J) -> Self::Target
where
I: Iterator,
J: Iterator,
I::Item: Borrow<Self::Lhs>,
J::Item: Borrow<Self::Rhs>,
{
let scalars = g.map(|x| *x.borrow()).collect::<Vec<_>>();
let bases = f.map(|x| (*x.borrow()).into()).collect::<Vec<_>>();
P::G1::msm_unchecked(&bases, &scalars)
}
}
impl<P: Pairing> BilinearModule for G2Module<P> {
type Lhs = P::ScalarField;
type Rhs = P::G2;
type Target = P::G2;
type ScalarField = P::ScalarField;
fn p(a: impl Borrow<Self::Lhs>, b: impl Borrow<Self::Rhs>) -> Self::Target {
*b.borrow() * a.borrow()
}
fn ip<I, J>(f: I, g: J) -> Self::Target
where
I: Iterator,
J: Iterator,
I::Item: Borrow<Self::Lhs>,
J::Item: Borrow<Self::Rhs>,
{
let scalars = f.map(|x| *x.borrow()).collect::<Vec<_>>();
let bases = g.map(|x| (*x.borrow()).into()).collect::<Vec<_>>();
P::G2::msm_unchecked(&bases, &scalars)
}
}
impl<P: Pairing> BilinearModule for FModule<P> {
type Lhs = P::ScalarField;
type Rhs = P::ScalarField;
type Target = P::ScalarField;
type ScalarField = P::ScalarField;
fn p(a: impl Borrow<Self::Lhs>, b: impl Borrow<Self::Rhs>) -> Self::Target {
*b.borrow() * a.borrow()
}
fn ip<I, J>(f: I, g: J) -> Self::Target
where
I: Iterator,
J: Iterator,
I::Item: Borrow<Self::Lhs>,
J::Item: Borrow<Self::Rhs>,
{
crate::misc::ip_unsafe(f, g)
}
}