-
Notifications
You must be signed in to change notification settings - Fork 7
/
messages.rs
221 lines (180 loc) · 8.45 KB
/
messages.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use std::str::FromStr;
use crate::proofs::to_field_elements::ToFieldElements;
use crate::proofs::transaction::{checked_hash2, InnerCurve, PlonkVerificationKeyEvals};
use crate::proofs::witness::Witness;
use ark_ec::short_weierstrass_jacobian::GroupAffine;
use ark_ff::{BigInteger256, PrimeField};
use kimchi::verifier_index::VerifierIndex;
use mina_curves::{pasta::Fq, pasta::Pallas};
use mina_hasher::Fp;
use poly_commitment::PolyComm;
use crate::hash::hash_fields;
impl<'a> From<&'a VerifierIndex<Pallas>> for PlonkVerificationKeyEvals<Fp> {
fn from(verifier_index: &'a VerifierIndex<Pallas>) -> Self {
let to_curve = |v: &PolyComm<Pallas>| InnerCurve::of_affine(v.unshifted[0]);
Self {
sigma: verifier_index.sigma_comm.each_ref().map(to_curve),
coefficients: verifier_index.coefficients_comm.each_ref().map(to_curve),
generic: to_curve(&verifier_index.generic_comm),
psm: to_curve(&verifier_index.psm_comm),
complete_add: to_curve(&verifier_index.complete_add_comm),
mul: to_curve(&verifier_index.mul_comm),
emul: to_curve(&verifier_index.emul_comm),
endomul_scalar: to_curve(&verifier_index.endomul_scalar_comm),
}
}
}
/// Value of `Dummy.Ipa.Step.sg`
/// TODO: Compute it instead of hardcoded values
pub fn dummy_ipa_step_sg() -> (Fq, Fq) {
let fst = Fq::from_str(
"7157847628472818669877981787153253278122158060570991904823379281596325861730",
)
.unwrap();
let snd = Fq::from_str(
"9959746677904483136261451107528553963316638248277760417056251351537540061100",
)
.unwrap();
(fst, snd)
}
#[derive(Clone, Debug)]
pub struct MessagesForNextWrapProof {
pub challenge_polynomial_commitment: InnerCurve<Fq>,
pub old_bulletproof_challenges: Vec<[Fq; 15]>,
}
impl MessagesForNextWrapProof {
/// Implementation of `hash_messages_for_next_wrap_proof`
/// https://github.com/MinaProtocol/mina/blob/32a91613c388a71f875581ad72276e762242f802/src/lib/pickles/wrap_hack.ml#L50
pub fn hash(&self) -> [u64; 4] {
let fields: Vec<Fq> = self.to_fields();
let field: Fq = hash_fields(&fields);
let bigint: BigInteger256 = field.into_repr();
bigint.0
}
pub fn hash_checked(&self, w: &mut Witness<Fq>) -> [u64; 4] {
let fields: Vec<Fq> = self.to_fields();
let field: Fq = checked_hash2(&fields, w);
let bigint: BigInteger256 = field.into_repr();
bigint.0
}
// TODO: De-duplicate with above
pub fn hash_checked3(&self, w: &mut Witness<Fq>) -> [u64; 4] {
let fields: Vec<Fq> = self.to_fields();
let field: Fq = crate::proofs::transaction::checked_hash3(&fields, w);
let bigint: BigInteger256 = field.into_repr();
bigint.0
}
/// Implementation of `to_field_elements`
/// https://github.com/MinaProtocol/mina/blob/32a91613c388a71f875581ad72276e762242f802/src/lib/pickles/composition_types/composition_types.ml#L356
fn to_fields(&self) -> Vec<Fq> {
const NFIELDS: usize = 32;
let mut fields = Vec::with_capacity(NFIELDS);
let padding = 2usize
.checked_sub(self.old_bulletproof_challenges.len())
.expect("old_bulletproof_challenges must be of length <= 2");
for _ in 0..padding {
fields.extend_from_slice(&Self::dummy_padding());
}
for challenges in &self.old_bulletproof_challenges {
fields.extend_from_slice(challenges);
}
let GroupAffine { x, y, .. } = self.challenge_polynomial_commitment.to_affine();
fields.extend([x, y]);
assert_eq!(fields.len(), NFIELDS);
fields
}
/// Value of `Dummy.Ipa.Wrap.challenges_computed` here:
/// https://github.com/MinaProtocol/mina/blob/32a91613c388a71f875581ad72276e762242f802/src/lib/pickles/wrap_hack.ml#L37
///
/// Those are constants but they are computed once at runtime in Mina.
/// TODO: Compute them instead of hardcoded values
pub fn dummy_padding() -> [Fq; 15] {
let f = |s| Fq::from_str(s).unwrap();
[
f("7048930911355605315581096707847688535149125545610393399193999502037687877674"),
f("5945064094191074331354717685811267396540107129706976521474145740173204364019"),
f("20315491820009986698838977727629973056499886675589920515484193128018854963801"),
f("375929229548289966749422550601268097380795636681684498450629863247980915833"),
f("19682218496321100578766622300447982536359891434050417209656101638029891689955"),
f("516598185966802396400068849903674663130928531697254466925429658676832606723"),
f("23729760760563685146228624125180554011222918208600079938584869191222807389336"),
f("11155777282048225577422475738306432747575091690354122761439079853293714987855"),
f("24977767586983413450834833875715786066408803952857478894197349635213480783870"),
f("2813347787496113574506936084777563965225649411532015639663405402448028142689"),
f("22626141769059119580550800305467929090916842064220293932303261732461616709448"),
f("18748107085456859495495117012311103043200881556220793307463332157672741458218"),
f("22196219950929618042921320796106738233125483954115679355597636800196070731081"),
f("13054421325261400802177761929986025883530654947859503505174678618288142017333"),
f("4799483385651443229337780097631636300491234601736019220096005875687579936102"),
]
}
}
#[derive(Clone, Debug)]
pub struct MessagesForNextStepProof<'a, AppState: ToFieldElements<Fp>> {
pub app_state: &'a AppState,
pub dlog_plonk_index: &'a PlonkVerificationKeyEvals<Fp>,
pub challenge_polynomial_commitments: Vec<InnerCurve<Fp>>,
pub old_bulletproof_challenges: Vec<[Fp; 16]>,
}
impl<AppState> MessagesForNextStepProof<'_, AppState>
where
AppState: ToFieldElements<Fp>,
{
/// Implementation of `hash_messages_for_next_step_proof`
/// https://github.com/MinaProtocol/mina/blob/32a91613c388a71f875581ad72276e762242f802/src/lib/pickles/common.ml#L33
pub fn hash(&self) -> [u64; 4] {
let fields: Vec<Fp> = self.to_fields();
let field: Fp = hash_fields(&fields);
let bigint: BigInteger256 = field.into_repr();
bigint.0
}
/// Implementation of `to_field_elements`
/// https://github.com/MinaProtocol/mina/blob/32a91613c388a71f875581ad72276e762242f802/src/lib/pickles/composition_types/composition_types.ml#L493
fn to_fields(&self) -> Vec<Fp> {
const NFIELDS: usize = 93; // TODO: This is bigger with transactions
let mut fields = Vec::with_capacity(NFIELDS);
// Self::dlog_plonk_index
// Refactor with `src/account/account.rs`, this is the same code
{
let PlonkVerificationKeyEvals {
sigma,
coefficients,
generic,
psm,
complete_add,
mul,
emul,
endomul_scalar,
} = &self.dlog_plonk_index;
for GroupAffine { x, y, .. } in sigma.iter().map(InnerCurve::to_affine) {
fields.extend([x, y]);
}
for GroupAffine { x, y, .. } in coefficients.iter().map(InnerCurve::to_affine) {
fields.extend([x, y]);
}
let GroupAffine { x, y, .. } = generic.to_affine();
fields.extend([x, y]);
let GroupAffine { x, y, .. } = psm.to_affine();
fields.extend([x, y]);
let GroupAffine { x, y, .. } = complete_add.to_affine();
fields.extend([x, y]);
let GroupAffine { x, y, .. } = mul.to_affine();
fields.extend([x, y]);
let GroupAffine { x, y, .. } = emul.to_affine();
fields.extend([x, y]);
let GroupAffine { x, y, .. } = endomul_scalar.to_affine();
fields.extend([x, y]);
}
self.app_state.to_field_elements(&mut fields);
// Self::challenge_polynomial_commitments and Self::old_bulletproof_challenges
let commitments = &self.challenge_polynomial_commitments;
let old_challenges = &self.old_bulletproof_challenges;
for (commitments, old) in commitments.iter().zip(old_challenges) {
let GroupAffine { x, y, .. } = commitments.to_affine();
fields.extend([x, y]);
fields.extend_from_slice(old);
}
// assert!(fields.len() >= NFIELDS);
fields
}
}