-
Notifications
You must be signed in to change notification settings - Fork 8
/
transaction.rs
4903 lines (4221 loc) · 164 KB
/
transaction.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::{collections::HashMap, rc::Rc, str::FromStr, sync::Arc};
use ark_ec::{short_weierstrass_jacobian::GroupProjective, AffineCurve, ProjectiveCurve};
use ark_ff::{BigInteger256, Field, PrimeField};
use kimchi::{
circuits::{gate::CircuitGate, wires::COLUMNS},
proof::RecursionChallenge,
prover_index::ProverIndex,
};
use mina_curves::pasta::Fq;
use mina_hasher::Fp;
use mina_p2p_messages::v2::{
self, ConsensusGlobalSlotStableV1, ConsensusProofOfStakeDataConsensusStateValueStableV2,
ConsensusProofOfStakeDataEpochDataNextValueVersionedValueStableV1,
ConsensusProofOfStakeDataEpochDataStakingValueVersionedValueStableV1, CurrencyAmountStableV1,
MinaBaseEpochLedgerValueStableV1, MinaBaseFeeExcessStableV1,
MinaBaseProtocolConstantsCheckedValueStableV1, MinaNumbersGlobalSlotSinceGenesisMStableV1,
MinaNumbersGlobalSlotSinceHardForkMStableV1, MinaStateBlockchainStateValueStableV2,
MinaStateBlockchainStateValueStableV2LedgerProofStatement,
MinaStateBlockchainStateValueStableV2LedgerProofStatementSource,
MinaStateBlockchainStateValueStableV2SignedAmount, MinaStateProtocolStateBodyValueStableV2,
MinaTransactionLogicZkappCommandLogicLocalStateValueStableV1, SgnStableV1, SignedAmount,
TokenFeeExcess, UnsignedExtendedUInt32StableV1,
UnsignedExtendedUInt64Int64ForVersionTagsStableV1,
};
use mina_poseidon::constants::PlonkSpongeConstantsKimchi;
use mina_signer::{CompressedPubKey, PubKey};
use crate::{
decompress_pk, gen_keypair,
proofs::{
constants::{StepTransactionProof, WrapTransactionProof},
unfinalized::AllEvals,
util::sha256_sum,
wrap::{self, WrapParams},
},
scan_state::{
currency::{self, Sgn},
fee_excess::FeeExcess,
pending_coinbase,
scan_state::transaction_snark::{Registers, SokDigest, SokMessage, Statement},
transaction_logic::{local_state::LocalState, transaction_union_payload},
},
verifier::get_srs,
Account, MyCow, ReceiptChainHash, SpongeParamsForField, TimingAsRecord, TokenId, TokenSymbol,
};
use super::step::{InductiveRule, OptFlag, StepProof};
use super::{
constants::ProofConstants,
field::GroupAffine,
public_input::messages::{dummy_ipa_step_sg, MessagesForNextWrapProof},
to_field_elements::{ToFieldElements, ToFieldElementsDebug},
unfinalized::Unfinalized,
witness::Witness,
wrap::WrapProof,
};
use super::{
field::{field, Boolean, CircuitVar, FieldWitness, ToBoolean},
step,
};
pub trait Check<F: FieldWitness> {
fn check(&self, w: &mut Witness<F>);
}
struct FieldBitsIterator {
index: usize,
bigint: BigInteger256,
}
impl Iterator for FieldBitsIterator {
type Item = bool;
fn next(&mut self) -> Option<Self::Item> {
let index = self.index;
self.index += 1;
let limb_index = index / 64;
let bit_index = index % 64;
let limb = self.bigint.0.get(limb_index)?;
Some(limb & (1 << bit_index) != 0)
}
}
fn bigint_to_bits<const NBITS: usize>(bigint: BigInteger256) -> [bool; NBITS] {
let mut bits = FieldBitsIterator { index: 0, bigint }.take(NBITS);
std::array::from_fn(|_| bits.next().unwrap())
}
pub fn field_to_bits<F, const NBITS: usize>(field: F) -> [bool; NBITS]
where
F: Field + Into<BigInteger256>,
{
let bigint: BigInteger256 = field.into();
bigint_to_bits(bigint)
}
/// Difference with `bigint_to_bits`: the number of bits isn't a constant
fn bigint_to_bits2(bigint: BigInteger256, nbits: usize) -> Box<[bool]> {
FieldBitsIterator { index: 0, bigint }.take(nbits).collect()
}
/// Difference with `field_to_bits`: the number of bits isn't a constant
pub fn field_to_bits2<F>(field: F, nbits: usize) -> Box<[bool]>
where
F: Field + Into<BigInteger256>,
{
let bigint: BigInteger256 = field.into();
bigint_to_bits2(bigint, nbits)
}
fn bits_msb<F, const NBITS: usize>(field: F) -> [bool; NBITS]
where
F: Field + Into<BigInteger256>,
{
let mut bits = field_to_bits::<F, NBITS>(field);
bits.reverse();
bits
}
pub fn endos<F>() -> (F, F::Scalar)
where
F: FieldWitness,
{
use poly_commitment::srs::endos;
// Let's keep them in cache since they're used everywhere
cache!((F, F::Scalar), endos::<GroupAffine<F>>())
}
pub fn make_group<F>(x: F, y: F) -> GroupAffine<F>
where
F: FieldWitness,
{
GroupAffine::<F>::new(x, y, false)
}
pub mod scalar_challenge {
use super::*;
// TODO: `scalar` might be a `F::Scalar` here
// https://github.com/MinaProtocol/mina/blob/357144819e7ce5f61109d23d33da627be28024c7/src/lib/pickles/scalar_challenge.ml#L12
pub fn to_field_checked_prime<F, const NBITS: usize>(scalar: F, w: &mut Witness<F>) -> (F, F, F)
where
F: FieldWitness,
{
let zero = F::zero();
let one = F::one();
let neg_one = one.neg();
let a_array = [zero, zero, neg_one, one];
let a_func = |n: u64| a_array[n as usize];
let b_array = [neg_one, one, zero, zero];
let b_func = |n: u64| b_array[n as usize];
let bits_msb: [bool; NBITS] = bits_msb::<_, NBITS>(scalar);
let nybbles_per_row = 8;
let bits_per_row = 2 * nybbles_per_row;
assert_eq!((NBITS % bits_per_row), 0);
let rows = NBITS / bits_per_row;
// TODO: Use arrays when const feature allows it
// https://github.com/rust-lang/rust/issues/76560
let nybbles_by_row: Vec<Vec<u64>> = (0..rows)
.map(|i| {
(0..nybbles_per_row)
.map(|j| {
let bit = (bits_per_row * i) + (2 * j);
let b0 = bits_msb[bit + 1] as u64;
let b1 = bits_msb[bit] as u64;
b0 + (2 * b1)
})
.collect()
})
.collect();
let two: F = 2u64.into();
let mut a = two;
let mut b = two;
let mut n = F::zero();
for nybbles_by_row in nybbles_by_row.iter().take(rows) {
let n0 = n;
let a0 = a;
let b0 = b;
let xs: Vec<F> = (0..nybbles_per_row)
.map(|j| w.exists(F::from(nybbles_by_row[j])))
.collect();
let n8: F = w.exists(xs.iter().fold(n0, |accum, x| accum.double().double() + x));
let a8: F = w.exists(
nybbles_by_row
.iter()
.fold(a0, |accum, x| accum.double() + a_func(*x)),
);
let b8: F = w.exists(
nybbles_by_row
.iter()
.fold(b0, |accum, x| accum.double() + b_func(*x)),
);
n = n8;
a = a8;
b = b8;
}
(a, b, n)
}
// TODO: `scalar` might be a `F::Scalar` here
pub fn to_field_checked<F, const NBITS: usize>(scalar: F, endo: F, w: &mut Witness<F>) -> F
where
F: FieldWitness,
{
let (a, b, _n) = to_field_checked_prime::<F, NBITS>(scalar, w);
(a * endo) + b
}
// TODO: Use `F::Scalar` instead of `F2`
pub fn endo<F, F2, const NBITS: usize>(
t: GroupAffine<F>,
scalar: F2,
w: &mut Witness<F>,
) -> GroupAffine<F>
where
F: FieldWitness,
F2: FieldWitness,
{
endo_cvar::<F, F2, NBITS>(CircuitVar::Var(t), scalar, w)
}
// TODO: Remove
pub fn endo_cvar<F, F2, const NBITS: usize>(
t: CircuitVar<GroupAffine<F>>,
scalar: F2,
w: &mut Witness<F>,
) -> GroupAffine<F>
where
F: FieldWitness,
F2: FieldWitness,
{
let bits: [bool; NBITS] = bits_msb::<F2, NBITS>(scalar);
let bits_per_row = 4;
let rows = NBITS / bits_per_row;
let GroupAffine::<F> { x: xt, y: yt, .. } = *t.value();
let (endo_base, _) = endos::<F>();
let mut acc = {
// The `exists` call is made by the `seal` call in OCaml
// Note: it's actually `Cvar.scale`
let tmp = match t {
CircuitVar::Var(_) => w.exists(xt * endo_base),
CircuitVar::Constant(_) => xt * endo_base,
};
let p = w.add_fast(*t.value(), make_group::<F>(tmp, yt));
w.add_fast(p, p)
};
let mut n_acc = F::zero();
for i in 0..rows {
let n_acc_prev = n_acc;
let b1 = w.exists(F::from(bits[i * bits_per_row]));
let b2 = w.exists(F::from(bits[(i * bits_per_row) + 1]));
let b3 = w.exists(F::from(bits[(i * bits_per_row) + 2]));
let b4 = w.exists(F::from(bits[(i * bits_per_row) + 3]));
let GroupAffine::<F> { x: xp, y: yp, .. } = acc;
let xq1 = w.exists((F::one() + ((endo_base - F::one()) * b1)) * xt);
let yq1 = w.exists((b2.double() - F::one()) * yt);
let s1 = w.exists((yq1 - yp) / (xq1 - xp));
let s1_squared = w.exists(s1.square());
let s2 = w.exists((yp.double() / (xp.double() + xq1 - s1_squared)) - s1);
let xr = w.exists(xq1 + s2.square() - s1_squared);
let yr = w.exists(((xp - xr) * s2) - yp);
let xq2 = w.exists((F::one() + ((endo_base - F::one()) * b3)) * xt);
let yq2 = w.exists((b4.double() - F::one()) * yt);
let s3 = w.exists((yq2 - yr) / (xq2 - xr));
let s3_squared = w.exists(s3.square());
let s4 = w.exists((yr.double() / (xr.double() + xq2 - s3_squared)) - s3);
let xs = w.exists(xq2 + s4.square() - s3_squared);
let ys = w.exists(((xr - xs) * s4) - yr);
acc = make_group::<F>(xs, ys);
n_acc =
w.exists((((n_acc_prev.double() + b1).double() + b2).double() + b3).double() + b4);
}
acc
}
// TODO: Use `F::Scalar` for `chal`
pub fn endo_inv<F, F2, const NBITS: usize>(
t: GroupAffine<F>,
chal: F2,
w: &mut Witness<F>,
) -> GroupAffine<F>
where
F: FieldWitness,
F2: FieldWitness,
{
use crate::proofs::public_input::scalar_challenge::ScalarChallenge;
use ark_ff::One;
let (_, e) = endos::<F>();
let res = w.exists({
let chal = ScalarChallenge::from(chal).to_field(&e);
InnerCurve::<F>::of_affine(t).scale(<F::Scalar>::one() / chal)
});
let _ = endo::<F, F2, NBITS>(res.to_affine(), chal, w);
res.to_affine()
}
}
pub fn add_fast<F>(
p1: GroupAffine<F>,
p2: GroupAffine<F>,
check_finite: Option<bool>,
w: &mut Witness<F>,
) -> GroupAffine<F>
where
F: FieldWitness,
{
let GroupAffine::<F> { x: x1, y: y1, .. } = p1;
let GroupAffine::<F> { x: x2, y: y2, .. } = p2;
let check_finite = check_finite.unwrap_or(true);
let bool_to_field = |b: bool| if b { F::one() } else { F::zero() };
let same_x_bool = x1 == x2;
let _same_x = w.exists(bool_to_field(same_x_bool));
let _inf = if check_finite {
F::zero()
} else {
w.exists(bool_to_field(same_x_bool && y1 != y2))
};
let _inf_z = w.exists({
if y1 == y2 {
F::zero()
} else if same_x_bool {
(y2 - y1).inverse().unwrap()
} else {
F::zero()
}
});
let _x21_inv = w.exists({
if same_x_bool {
F::zero()
} else {
(x2 - x1).inverse().unwrap()
}
});
let s = w.exists({
if same_x_bool {
let x1_squared = x1.square();
(x1_squared + x1_squared + x1_squared) / (y1 + y1)
} else {
(y2 - y1) / (x2 - x1)
}
});
let x3 = w.exists(s.square() - (x1 + x2));
let y3 = w.exists(s * (x1 - x3) - y1);
make_group::<F>(x3, y3)
}
fn fold_map<T, Acc, U>(
iter: impl Iterator<Item = T>,
init: Acc,
mut fun: impl FnMut(Acc, T) -> (Acc, U),
) -> (Acc, Vec<U>) {
let mut acc = Some(init);
let result = iter
.map(|x| {
let (new_acc, y) = fun(acc.take().unwrap(), x);
acc = Some(new_acc);
y
})
.collect::<Vec<_>>();
(acc.unwrap(), result)
}
pub mod plonk_curve_ops {
use crate::proofs::public_input::plonk_checks::ShiftingValue;
use super::*;
const BITS_PER_CHUNK: usize = 5;
// TODO: `scalar` is a `F::Scalar` here
pub fn scale_fast<F, F2, const NBITS: usize>(
base: GroupAffine<F>,
shifted_value: F2::Shifting,
w: &mut Witness<F>,
) -> GroupAffine<F>
where
F: FieldWitness,
F2: FieldWitness,
{
let (r, _bits) = scale_fast_unpack::<F, F2, NBITS>(base, shifted_value, w);
r
}
// TODO: `scalar` is a `F::Scalar` here
// https://github.com/openmina/mina/blob/8f83199a92faa8ff592b7ae5ad5b3236160e8c20/src/lib/pickles/plonk_curve_ops.ml#L140
pub fn scale_fast_unpack<F, F2, const NBITS: usize>(
base: GroupAffine<F>,
shifted: F2::Shifting,
w: &mut Witness<F>,
) -> (GroupAffine<F>, [bool; NBITS])
where
F: FieldWitness,
F2: FieldWitness,
{
let scalar = shifted.shifted_raw();
let GroupAffine::<F> {
x: x_base,
y: y_base,
..
} = base;
let chunks: usize = NBITS / BITS_PER_CHUNK;
assert_eq!(NBITS % BITS_PER_CHUNK, 0);
let bits_msb: [bool; NBITS] = w.exists(bits_msb::<F2, NBITS>(scalar));
let mut acc = w.add_fast(base, base);
let mut n_acc = F::zero();
for chunk in 0..chunks {
let bs: [bool; BITS_PER_CHUNK] =
std::array::from_fn(|i| bits_msb[(chunk * BITS_PER_CHUNK) + i]);
let n_acc_prev = n_acc;
n_acc = w.exists(
bs.iter()
.fold(n_acc_prev, |acc, b| acc.double() + F::from(*b)),
);
let (_, v) = fold_map(bs.iter(), acc, |acc, b| {
let GroupAffine::<F> {
x: x_acc, y: y_acc, ..
} = acc;
let b: F = F::from(*b);
let s1: F =
w.exists((y_acc - (y_base * (b.double() - F::one()))) / (x_acc - x_base));
let s1_squared = w.exists(s1.square());
let s2 = w.exists((y_acc.double() / (x_acc.double() + x_base - s1_squared)) - s1);
let x_res = w.exists(x_base + s2.square() - s1_squared);
let y_res = w.exists(((x_acc - x_res) * s2) - y_acc);
let acc = make_group(x_res, y_res);
(acc, (acc, s1))
});
let (mut accs, _slopes): (Vec<_>, Vec<_>) = v.into_iter().unzip();
accs.insert(0, acc);
acc = accs.last().cloned().unwrap();
}
let bits_lsb = {
let mut bits_msb = bits_msb.clone();
bits_msb.reverse();
bits_msb
};
(acc, bits_lsb)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PlonkVerificationKeyEvals<F: FieldWitness> {
pub sigma: [InnerCurve<F>; 7],
pub coefficients: [InnerCurve<F>; 15],
pub generic: InnerCurve<F>,
pub psm: InnerCurve<F>,
pub complete_add: InnerCurve<F>,
pub mul: InnerCurve<F>,
pub emul: InnerCurve<F>,
pub endomul_scalar: InnerCurve<F>,
}
// Here cvars are not used correctly, but it's just temporary
#[derive(Clone, Debug)]
pub struct CircuitPlonkVerificationKeyEvals<F: FieldWitness> {
pub sigma: [CircuitVar<GroupAffine<F>>; 7],
pub coefficients: [CircuitVar<GroupAffine<F>>; 15],
pub generic: CircuitVar<GroupAffine<F>>,
pub psm: CircuitVar<GroupAffine<F>>,
pub complete_add: CircuitVar<GroupAffine<F>>,
pub mul: CircuitVar<GroupAffine<F>>,
pub emul: CircuitVar<GroupAffine<F>>,
pub endomul_scalar: CircuitVar<GroupAffine<F>>,
}
impl CircuitPlonkVerificationKeyEvals<Fp> {
pub fn to_non_cvar(&self) -> PlonkVerificationKeyEvals<Fp> {
let Self {
sigma,
coefficients,
generic,
psm,
complete_add,
mul,
emul,
endomul_scalar,
} = self;
let c = |c: &CircuitVar<GroupAffine<Fp>>| InnerCurve::<Fp>::of_affine(*c.value());
PlonkVerificationKeyEvals::<Fp> {
sigma: sigma.each_ref().map(c),
coefficients: coefficients.each_ref().map(c),
generic: c(generic),
psm: c(psm),
complete_add: c(complete_add),
mul: c(mul),
emul: c(emul),
endomul_scalar: c(endomul_scalar),
}
}
}
impl PlonkVerificationKeyEvals<Fp> {
pub fn to_cvar(
&self,
cvar: impl Fn(GroupAffine<Fp>) -> CircuitVar<GroupAffine<Fp>>,
) -> CircuitPlonkVerificationKeyEvals<Fp> {
let Self {
sigma,
coefficients,
generic,
psm,
complete_add,
mul,
emul,
endomul_scalar,
} = self;
let cvar = |c: &InnerCurve<Fp>| cvar(c.to_affine());
CircuitPlonkVerificationKeyEvals::<Fp> {
sigma: sigma.each_ref().map(cvar),
coefficients: coefficients.each_ref().map(cvar),
generic: cvar(&generic),
psm: cvar(&psm),
complete_add: cvar(&complete_add),
mul: cvar(&mul),
emul: cvar(&emul),
endomul_scalar: cvar(&endomul_scalar),
}
}
/// For debugging
fn to_string(&self) -> String {
let Self {
sigma,
coefficients,
generic,
psm,
complete_add,
mul,
emul,
endomul_scalar,
} = self;
let mut string = String::with_capacity(1_000);
use crate::util::FpExt;
let mut inner_to_s = |c: &InnerCurve<Fp>| {
let GroupAffine::<Fp> { x, y, .. } = c.to_affine();
string.push_str(&format!("{}\n", x.to_decimal()));
string.push_str(&format!("{}\n", y.to_decimal()));
};
sigma.iter().for_each(|c| inner_to_s(c));
coefficients.iter().for_each(|c| inner_to_s(c));
inner_to_s(generic);
inner_to_s(psm);
inner_to_s(complete_add);
inner_to_s(mul);
inner_to_s(emul);
inner_to_s(endomul_scalar);
string.trim().to_string()
}
/// For debugging
fn from_string(s: &str) -> Self {
let mut s = s.lines();
let mut to_inner = || {
let a = s.next().unwrap();
let b = s.next().unwrap();
let a = Fp::from_str(a).unwrap();
let b = Fp::from_str(b).unwrap();
InnerCurve::<Fp>::of_affine(make_group(a, b))
};
Self {
sigma: std::array::from_fn(|_| to_inner()),
coefficients: std::array::from_fn(|_| to_inner()),
generic: to_inner(),
psm: to_inner(),
complete_add: to_inner(),
mul: to_inner(),
emul: to_inner(),
endomul_scalar: to_inner(),
}
}
pub fn rand() -> Self {
Self {
sigma: [
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
],
coefficients: [
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
InnerCurve::rand(),
],
generic: InnerCurve::rand(),
psm: InnerCurve::rand(),
complete_add: InnerCurve::rand(),
mul: InnerCurve::rand(),
emul: InnerCurve::rand(),
endomul_scalar: InnerCurve::rand(),
}
}
}
impl crate::ToInputs for PlonkVerificationKeyEvals<Fp> {
fn to_inputs(&self, inputs: &mut crate::Inputs) {
let Self {
sigma,
coefficients,
generic,
psm,
complete_add,
mul,
emul,
endomul_scalar,
} = self;
let mut to_input = |v: &InnerCurve<Fp>| {
let GroupAffine::<Fp> { x, y, .. } = v.to_affine();
inputs.append(&x);
inputs.append(&y);
};
sigma.iter().for_each(|c| to_input(c));
coefficients.iter().for_each(|c| to_input(c));
to_input(generic);
to_input(psm);
to_input(complete_add);
to_input(mul);
to_input(emul);
to_input(endomul_scalar);
}
}
// Implementation for references
impl<F: FieldWitness, T: Check<F>> Check<F> for &T {
fn check(&self, w: &mut Witness<F>) {
(*self).check(w)
}
}
impl<F: FieldWitness, T: Check<F> + Clone> Check<F> for std::borrow::Cow<'_, T> {
fn check(&self, w: &mut Witness<F>) {
let this: &T = self.as_ref();
this.check(w)
}
}
impl<F: FieldWitness> Check<F> for PlonkVerificationKeyEvals<F> {
fn check(&self, w: &mut Witness<F>) {
let Self {
sigma,
coefficients,
generic,
psm,
complete_add,
mul,
emul,
endomul_scalar,
} = self;
sigma.iter().for_each(|s| s.check(w));
coefficients.iter().for_each(|c| c.check(w));
generic.check(w);
psm.check(w);
complete_add.check(w);
mul.check(w);
emul.check(w);
endomul_scalar.check(w);
}
}
impl<F: FieldWitness> Check<F> for SgnStableV1 {
fn check(&self, _w: &mut Witness<F>) {
// Does not modify the witness
}
}
impl<F: FieldWitness> Check<F> for bool {
fn check(&self, _w: &mut Witness<F>) {
// Does not modify the witness
}
}
impl<F: FieldWitness> Check<F> for mina_signer::Signature {
fn check(&self, _w: &mut Witness<F>) {
// Does not modify the witness
}
}
impl<F: FieldWitness, T: Check<F>> Check<F> for MyCow<'_, T> {
fn check(&self, w: &mut Witness<F>) {
let this: &T = &*self;
this.check(w);
}
}
impl<F: FieldWitness> Check<F> for Fp {
fn check(&self, _w: &mut Witness<F>) {
// Does not modify the witness
}
}
impl<F: FieldWitness> Check<F> for Fq {
fn check(&self, _w: &mut Witness<F>) {
// Does not modify the witness
}
}
impl<F: FieldWitness, const N: usize> Check<F> for crate::address::raw::Address<N> {
fn check(&self, _w: &mut Witness<F>) {
// Does not modify the witness
}
}
impl<F: FieldWitness, T: Check<F>, const N: usize> Check<F> for [T; N] {
fn check(&self, w: &mut Witness<F>) {
self.iter().for_each(|v| v.check(w));
}
}
impl<F: FieldWitness> Check<F> for CurrencyAmountStableV1 {
fn check(&self, w: &mut Witness<F>) {
const NBITS: usize = u64::BITS as usize;
let amount: u64 = self.as_u64();
assert_eq!(NBITS, std::mem::size_of_val(&amount) * 8);
let amount: F = amount.into();
scalar_challenge::to_field_checked_prime::<F, NBITS>(amount, w);
}
}
impl<F: FieldWitness> Check<F> for SignedAmount {
fn check(&self, w: &mut Witness<F>) {
let Self { magnitude, sgn } = self;
magnitude.check(w);
sgn.check(w);
}
}
impl<F: FieldWitness, T: currency::Magnitude + Check<F>> Check<F> for currency::Signed<T> {
fn check(&self, w: &mut Witness<F>) {
let Self { magnitude, sgn } = self;
magnitude.check(w);
sgn.check(w);
}
}
impl<F: FieldWitness> Check<F> for MinaStateBlockchainStateValueStableV2SignedAmount {
fn check(&self, w: &mut Witness<F>) {
let Self { magnitude, sgn } = self;
magnitude.check(w);
sgn.check(w);
}
}
impl<F: FieldWitness> Check<F> for UnsignedExtendedUInt32StableV1 {
fn check(&self, w: &mut Witness<F>) {
let number: u32 = self.as_u32();
number.check(w);
}
}
impl<F: FieldWitness> Check<F> for u32 {
fn check(&self, w: &mut Witness<F>) {
const NBITS: usize = u32::BITS as usize;
let number: u32 = *self;
assert_eq!(NBITS, std::mem::size_of_val(&number) * 8);
let number: F = number.into();
scalar_challenge::to_field_checked_prime::<F, NBITS>(number, w);
}
}
impl<F: FieldWitness> Check<F> for MinaStateBlockchainStateValueStableV2LedgerProofStatementSource {
fn check(&self, w: &mut Witness<F>) {
let Self {
first_pass_ledger: _,
second_pass_ledger: _,
pending_coinbase_stack: _,
local_state:
MinaTransactionLogicZkappCommandLogicLocalStateValueStableV1 {
stack_frame: _,
call_stack: _,
transaction_commitment: _,
full_transaction_commitment: _,
excess,
supply_increase,
ledger: _,
success,
account_update_index,
failure_status_tbl: _,
will_succeed,
},
} = self;
excess.check(w);
supply_increase.check(w);
success.check(w);
account_update_index.check(w);
will_succeed.check(w);
}
}
impl<F: FieldWitness> Check<F> for Registers {
fn check(&self, w: &mut Witness<F>) {
let Self {
first_pass_ledger: _,
second_pass_ledger: _,
pending_coinbase_stack: _,
local_state:
LocalState {
stack_frame: _,
call_stack: _,
transaction_commitment: _,
full_transaction_commitment: _,
excess,
supply_increase,
ledger: _,
success,
account_update_index,
failure_status_tbl: _,
will_succeed,
},
} = self;
excess.check(w);
supply_increase.check(w);
success.check(w);
account_update_index.check(w);
will_succeed.check(w);
}
}
impl<F: FieldWitness> Check<F> for MinaStateBlockchainStateValueStableV2LedgerProofStatement {
fn check(&self, w: &mut Witness<F>) {
let Self {
source,
target,
connecting_ledger_left: _,
connecting_ledger_right: _,
supply_increase,
fee_excess,
sok_digest: _,
} = self;
source.check(w);
target.check(w);
supply_increase.check(w);
fee_excess.check(w);
}
}
impl<F: FieldWitness, T> Check<F> for Statement<T> {
fn check(&self, w: &mut Witness<F>) {
let Self {
source,
target,
connecting_ledger_left: _,
connecting_ledger_right: _,
supply_increase,
fee_excess,
sok_digest: _,
} = self;
source.check(w);
target.check(w);
supply_increase.check(w);
fee_excess.check(w);
}
}
impl<F: FieldWitness> Check<F> for MinaBaseFeeExcessStableV1 {
fn check(&self, w: &mut Witness<F>) {
let Self(
TokenFeeExcess {
token: _fee_token_l,
amount: fee_excess_l,
},
TokenFeeExcess {
token: _fee_token_r,
amount: fee_excess_r,
},
) = self;
fee_excess_l.check(w);
fee_excess_r.check(w);
}
}
impl<F: FieldWitness> Check<F> for FeeExcess {
fn check(&self, w: &mut Witness<F>) {
let Self {
fee_token_l: _,
fee_excess_l,
fee_token_r: _,
fee_excess_r,
} = self;
fee_excess_l.check(w);
fee_excess_r.check(w);
}
}
impl<F: FieldWitness> Check<F> for UnsignedExtendedUInt64Int64ForVersionTagsStableV1 {
fn check(&self, w: &mut Witness<F>) {
const NBITS: usize = u64::BITS as usize;
let number: u64 = self.as_u64();
assert_eq!(NBITS, std::mem::size_of_val(&number) * 8);
let number: F = number.into();
scalar_challenge::to_field_checked_prime::<F, NBITS>(number, w);
}
}
impl<F: FieldWitness> Check<F> for MinaNumbersGlobalSlotSinceGenesisMStableV1 {
fn check(&self, w: &mut Witness<F>) {
let Self::SinceGenesis(global_slot) = self;
global_slot.check(w);
}
}
impl<F: FieldWitness> Check<F> for MinaNumbersGlobalSlotSinceHardForkMStableV1 {
fn check(&self, w: &mut Witness<F>) {
let Self::SinceHardFork(global_slot) = self;
global_slot.check(w);
}
}
impl<F: FieldWitness> Check<F>
for ConsensusProofOfStakeDataEpochDataStakingValueVersionedValueStableV1
{