-
Notifications
You must be signed in to change notification settings - Fork 150
/
composer.rs
1206 lines (989 loc) · 38.1 KB
/
composer.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.
//! PLONK turbo composer definitions
use alloc::vec::Vec;
use core::{cmp, ops};
use hashbrown::HashMap;
use dusk_bls12_381::BlsScalar;
use dusk_jubjub::{JubJubAffine, JubJubExtended, JubJubScalar};
use crate::bit_iterator::BitIterator8;
use crate::error::Error;
use crate::runtime::{Runtime, RuntimeEvent};
mod circuit;
mod compress;
mod constraint_system;
mod gate;
pub(crate) mod permutation;
pub use circuit::Circuit;
pub use constraint_system::{Constraint, Witness, WitnessPoint};
pub use gate::Gate;
pub(crate) use constraint_system::{Selector, WireData, WiredWitness};
pub(crate) use permutation::Permutation;
/// Construct and prove circuits
#[derive(Debug, Clone)]
pub struct Composer {
/// Constraint system gates
pub(crate) constraints: Vec<Gate>,
/// Sparse representation of the public inputs
pub(crate) public_inputs: HashMap<usize, BlsScalar>,
/// Witness values
pub(crate) witnesses: Vec<BlsScalar>,
/// Permutation argument.
pub(crate) perm: Permutation,
/// PLONK runtime controller
pub(crate) runtime: Runtime,
}
impl ops::Index<Witness> for Composer {
type Output = BlsScalar;
fn index(&self, w: Witness) -> &Self::Output {
&self.witnesses[w.index()]
}
}
// pub trait Composer: Sized + Index<Witness, Output = BlsScalar> {
/// Circuit builder tool
impl Composer {
/// Zero representation inside the constraint system.
///
/// A turbo composer expects the first witness to be always present and to
/// be zero.
pub const ZERO: Witness = Witness::ZERO;
/// `One` representation inside the constraint system.
///
/// A turbo composer expects the 2nd witness to be always present and to
/// be one.
pub const ONE: Witness = Witness::ONE;
/// Identity point representation inside the constraint system
pub const IDENTITY: WitnessPoint = WitnessPoint::new(Self::ZERO, Self::ONE);
/// Constraints count
pub fn constraints(&self) -> usize {
self.constraints.len()
}
/// Create a [`Composer`] instance from a compressed circuit
pub(crate) fn from_bytes(compressed: &[u8]) -> Result<Self, Error> {
compress::CompressedCircuit::from_bytes(compressed)
}
/// Allocate a witness value into the composer and return its index.
fn append_witness_internal(&mut self, witness: BlsScalar) -> Witness {
let n = self.witnesses.len();
// Get a new Witness from the permutation
self.perm.new_witness();
// Bind the allocated witness
self.witnesses.push(witness);
Witness::new(n)
}
/// Append a new width-4 gate/constraint.
fn append_custom_gate_internal(&mut self, constraint: Constraint) {
let n = self.constraints.len();
let a = constraint.witness(WiredWitness::A);
let b = constraint.witness(WiredWitness::B);
let c = constraint.witness(WiredWitness::C);
let d = constraint.witness(WiredWitness::D);
let q_m = *constraint.coeff(Selector::Multiplication);
let q_l = *constraint.coeff(Selector::Left);
let q_r = *constraint.coeff(Selector::Right);
let q_o = *constraint.coeff(Selector::Output);
let q_f = *constraint.coeff(Selector::Fourth);
let q_c = *constraint.coeff(Selector::Constant);
let q_arith = *constraint.coeff(Selector::Arithmetic);
let q_range = *constraint.coeff(Selector::Range);
let q_logic = *constraint.coeff(Selector::Logic);
let q_fixed_group_add = *constraint.coeff(Selector::GroupAddFixedBase);
let q_variable_group_add =
*constraint.coeff(Selector::GroupAddVariableBase);
let gate = Gate {
q_m,
q_l,
q_r,
q_o,
q_f,
q_c,
q_arith,
q_range,
q_logic,
q_fixed_group_add,
q_variable_group_add,
a,
b,
c,
d,
};
self.constraints.push(gate);
if constraint.has_public_input() {
let pi = *constraint.coeff(Selector::PublicInput);
self.public_inputs.insert(n, pi);
}
self.perm.add_witnesses_to_map(a, b, c, d, n);
}
/// PLONK runtime controller
pub(crate) fn runtime(&mut self) -> &mut Runtime {
&mut self.runtime
}
/// Initialize the constraint system with the constants for 0 and 1 and
/// append two dummy gates
pub fn initialized() -> Self {
let mut slf = Self::uninitialized();
let zero = slf.append_witness(0);
let one = slf.append_witness(1);
slf.assert_equal_constant(zero, 0, None);
slf.assert_equal_constant(one, 1, None);
slf.append_dummy_gates();
slf
}
/// Create an empty constraint system.
///
/// This shouldn't be used directly; instead, use [`Self::initialized`]
pub(crate) fn uninitialized() -> Self {
Self {
constraints: Vec::new(),
public_inputs: HashMap::new(),
witnesses: Vec::new(),
perm: Permutation::new(),
runtime: Runtime::new(),
}
}
/// Adds blinding factors to the witness polynomials with two dummy
/// arithmetic constraints
fn append_dummy_gates(&mut self) {
let six = self.append_witness(BlsScalar::from(6));
let one = self.append_witness(BlsScalar::from(1));
let seven = self.append_witness(BlsScalar::from(7));
let min_twenty = self.append_witness(-BlsScalar::from(20));
// Add a dummy constraint so that we do not have zero polynomials
let constraint = Constraint::new()
.mult(1)
.left(2)
.right(3)
.fourth(1)
.constant(4)
.output(4)
.a(six)
.b(seven)
.d(one)
.c(min_twenty);
self.append_gate(constraint);
// Add another dummy constraint so that we do not get the identity
// permutation
let constraint = Constraint::new()
.mult(1)
.left(1)
.right(1)
.constant(127)
.output(1)
.a(min_twenty)
.b(six)
.c(seven);
self.append_gate(constraint);
}
/// Allocate a witness value into the composer and return its index.
pub fn append_witness<W: Into<BlsScalar>>(
&mut self,
witness: W,
) -> Witness {
let witness = witness.into();
let witness = self.append_witness_internal(witness);
let v = self[witness];
self.runtime()
.event(RuntimeEvent::WitnessAppended { w: witness, v });
witness
}
/// Append a new width-4 gate/constraint.
pub fn append_custom_gate(&mut self, constraint: Constraint) {
self.runtime()
.event(RuntimeEvent::ConstraintAppended { c: constraint });
self.append_custom_gate_internal(constraint)
}
/// Performs a logical AND or XOR op between the inputs provided for
/// `num_bits = BIT_PAIRS * 2` bits (counting from the least significant).
///
/// Each logic gate adds `BIT_PAIRS + 1` gates to the circuit to
/// perform the whole operation.
///
/// ## Constraint
/// - is_component_xor = 1 -> Performs XOR between the first `num_bits` for
/// `a` and `b`.
/// - is_component_xor = 0 -> Performs AND between the first `num_bits` for
/// `a` and `b`.
pub fn append_logic_component<const BIT_PAIRS: usize>(
&mut self,
a: Witness,
b: Witness,
is_component_xor: bool,
) -> Witness {
// the bits are iterated as chunks of two; hence, we require an even
// number
let num_bits = cmp::min(BIT_PAIRS * 2, 256);
let num_quads = num_bits >> 1;
let bls_four = BlsScalar::from(4u64);
let mut left_acc = BlsScalar::zero();
let mut right_acc = BlsScalar::zero();
let mut out_acc = BlsScalar::zero();
// skip bits outside of argument `num_bits`
let a_bit_iter = BitIterator8::new(self[a].to_bytes());
let a_bits: Vec<_> = a_bit_iter.skip(256 - num_bits).collect();
let b_bit_iter = BitIterator8::new(self[b].to_bytes());
let b_bits: Vec<_> = b_bit_iter.skip(256 - num_bits).collect();
//
// * +-----+-----+-----+-----+
// * | A | B | C | D |
// * +-----+-----+-----+-----+
// * | 0 | 0 | w1 | 0 |
// * | a1 | b1 | w2 | d1 |
// * | a2 | b2 | w3 | d2 |
// * | : | : | : | : |
// * | an | bn | 0 | dn |
// * +-----+-----+-----+-----+
// `an`, `bn` and `dn` are accumulators: `an [& OR ^] bd = dn`
//
// each step will shift last computation two bits to the left and add
// current quad.
//
// `wn` product accumulators will safeguard the quotient polynomial.
let mut constraint = if is_component_xor {
Constraint::logic_xor(&Constraint::new())
} else {
Constraint::logic(&Constraint::new())
};
for i in 0..num_quads {
// commit every accumulator
let idx = i * 2;
let l = (a_bits[idx] as u8) << 1;
let r = a_bits[idx + 1] as u8;
let left_quad = l + r;
let left_quad_bls = BlsScalar::from(left_quad as u64);
let l = (b_bits[idx] as u8) << 1;
let r = b_bits[idx + 1] as u8;
let right_quad = l + r;
let right_quad_bls = BlsScalar::from(right_quad as u64);
let out_quad_bls = if is_component_xor {
left_quad ^ right_quad
} else {
left_quad & right_quad
} as u64;
let out_quad_bls = BlsScalar::from(out_quad_bls);
// `w` argument to safeguard the quotient polynomial
let prod_quad_bls = (left_quad * right_quad) as u64;
let prod_quad_bls = BlsScalar::from(prod_quad_bls);
// Now that we've computed this round results, we need to apply the
// logic transition constraint that will check that
// a_{i+1} - (a_i << 2) < 4
// b_{i+1} - (b_i << 2) < 4
// d_{i+1} - (d_i << 2) < 4 with d_i = a_i [& OR ^] b_i
// Note that multiplying by four is the equivalent of shifting the
// bits two positions to the left.
left_acc = left_acc * bls_four + left_quad_bls;
right_acc = right_acc * bls_four + right_quad_bls;
out_acc = out_acc * bls_four + out_quad_bls;
let wit_a = self.append_witness(left_acc);
let wit_b = self.append_witness(right_acc);
let wit_c = self.append_witness(prod_quad_bls);
let wit_d = self.append_witness(out_acc);
constraint = constraint.c(wit_c);
self.append_custom_gate(constraint);
constraint = constraint.a(wit_a).b(wit_b).d(wit_d);
}
// pad last output with `0`
// | an | bn | 0 | dn |
let a = constraint.witness(WiredWitness::A);
let b = constraint.witness(WiredWitness::B);
let d = constraint.witness(WiredWitness::D);
let constraint = Constraint::new().a(a).b(b).d(d);
self.append_custom_gate(constraint);
d
}
/// Evaluate `jubjub · Generator` as a [`WitnessPoint`]
///
/// `generator` will be appended to the circuit description as constant
///
/// Will error with a `JubJubScalarMalformed` error if `jubjub` doesn't fit
/// `Fr`
pub fn component_mul_generator<P: Into<JubJubExtended>>(
&mut self,
jubjub: Witness,
generator: P,
) -> Result<WitnessPoint, Error> {
let generator = generator.into();
// the number of bits is truncated to the maximum possible. however, we
// could slice off 3 bits from the top of wnaf since Fr price is
// 252 bits. Alternatively, we could move to base4 and halve the
// number of gates considering that the product of wnaf adjacent
// entries is zero.
let bits: usize = 256;
// compute 2^iG
let mut wnaf_point_multiples: Vec<_> = {
let mut multiples = vec![JubJubExtended::default(); bits];
multiples[0] = generator;
for i in 1..bits {
multiples[i] = multiples[i - 1].double();
}
dusk_jubjub::batch_normalize(&mut multiples).collect()
};
wnaf_point_multiples.reverse();
// we should error instead of producing invalid proofs - otherwise this
// can easily become an attack vector to either shutdown prover
// services or create malicious statements
let scalar: JubJubScalar =
match JubJubScalar::from_bytes(&self[jubjub].to_bytes()).into() {
Some(s) => s,
None => return Err(Error::JubJubScalarMalformed),
};
let width = 2;
let wnaf_entries = scalar.compute_windowed_naf(width);
// this will pass as long as `compute_windowed_naf` returns an array
// with 256 elements
debug_assert_eq!(
wnaf_entries.len(),
bits,
"the wnaf_entries array is expected to be 256 elements long"
);
// initialize the accumulators
let mut scalar_acc = vec![BlsScalar::zero()];
let mut point_acc = vec![JubJubAffine::identity()];
// auxillary point to help with checks on the backend
let two = BlsScalar::from(2u64);
let xy_alphas: Vec<_> = wnaf_entries
.iter()
.rev()
.enumerate()
.map(|(i, entry)| {
let (scalar_to_add, point_to_add) = match entry {
0 => (BlsScalar::zero(), JubJubAffine::identity()),
-1 => (BlsScalar::one().neg(), -wnaf_point_multiples[i]),
1 => (BlsScalar::one(), wnaf_point_multiples[i]),
_ => return Err(Error::UnsupportedWNAF2k),
};
let prev_accumulator = two * scalar_acc[i];
let scalar = prev_accumulator + scalar_to_add;
scalar_acc.push(scalar);
let a = JubJubExtended::from(point_acc[i]);
let b = JubJubExtended::from(point_to_add);
let point = a + b;
point_acc.push(point.into());
let x_alpha = point_to_add.get_u();
let y_alpha = point_to_add.get_v();
Ok(x_alpha * y_alpha)
})
.collect::<Result<_, Error>>()?;
for i in 0..bits {
let acc_x = self.append_witness(point_acc[i].get_u());
let acc_y = self.append_witness(point_acc[i].get_v());
let accumulated_bit = self.append_witness(scalar_acc[i]);
// the point accumulator must start from identity and its scalar
// from zero
if i == 0 {
self.assert_equal_constant(acc_x, BlsScalar::zero(), None);
self.assert_equal_constant(acc_y, BlsScalar::one(), None);
self.assert_equal_constant(
accumulated_bit,
BlsScalar::zero(),
None,
);
}
let x_beta = wnaf_point_multiples[i].get_u();
let y_beta = wnaf_point_multiples[i].get_v();
let xy_alpha = self.append_witness(xy_alphas[i]);
let xy_beta = x_beta * y_beta;
let wnaf_round = constraint_system::ecc::WnafRound {
acc_x,
acc_y,
accumulated_bit,
xy_alpha,
x_beta,
y_beta,
xy_beta,
};
let constraint =
Constraint::group_add_fixed_base(&Constraint::new())
.left(wnaf_round.x_beta)
.right(wnaf_round.y_beta)
.constant(wnaf_round.xy_beta)
.a(wnaf_round.acc_x)
.b(wnaf_round.acc_y)
.c(wnaf_round.xy_alpha)
.d(wnaf_round.accumulated_bit);
self.append_custom_gate(constraint)
}
// last gate isn't activated for ecc
let acc_x = self.append_witness(point_acc[bits].get_u());
let acc_y = self.append_witness(point_acc[bits].get_v());
// FIXME this implementation presents a plethora of vulnerabilities and
// requires reworking
//
// we are accepting any scalar argument and trusting it to be the
// expected input. it happens to be correct in this
// implementation, but can be exploited by malicious provers who
// might just input anything here
let last_accumulated_bit = self.append_witness(scalar_acc[bits]);
// FIXME the gate isn't checking anything. maybe remove?
let constraint =
Constraint::new().a(acc_x).b(acc_y).d(last_accumulated_bit);
self.append_gate(constraint);
// constrain the last element in the accumulator to be equal to the
// input jubjub scalar
self.assert_equal(last_accumulated_bit, jubjub);
Ok(WitnessPoint::new(acc_x, acc_y))
}
/// Append a new width-4 gate/constraint.
///
/// The constraint added will enforce the following:
/// `q_M · a · b + q_L · a + q_R · b + q_O · o + q_F · d + q_C + PI = 0`.
pub fn append_gate(&mut self, constraint: Constraint) {
let constraint = Constraint::arithmetic(&constraint);
self.append_custom_gate(constraint)
}
/// Evaluate the polynomial and append an output that satisfies the equation
///
/// Return `None` if the output selector is zero
pub fn append_evaluated_output(
&mut self,
s: Constraint,
) -> Option<Witness> {
let a = s.witness(WiredWitness::A);
let b = s.witness(WiredWitness::B);
let d = s.witness(WiredWitness::D);
let a = self[a];
let b = self[b];
let d = self[d];
let qm = s.coeff(Selector::Multiplication);
let ql = s.coeff(Selector::Left);
let qr = s.coeff(Selector::Right);
let qf = s.coeff(Selector::Fourth);
let qc = s.coeff(Selector::Constant);
let pi = s.coeff(Selector::PublicInput);
let x = qm * a * b + ql * a + qr * b + qf * d + qc + pi;
let y = s.coeff(Selector::Output);
// Invert is an expensive operation; in most cases, `q_O` is going to be
// either 1 or -1, so we can optimize these
#[allow(dead_code)]
let c = {
const ONE: BlsScalar = BlsScalar::one();
const MINUS_ONE: BlsScalar = BlsScalar([
0xfffffffd00000003,
0xfb38ec08fffb13fc,
0x99ad88181ce5880f,
0x5bc8f5f97cd877d8,
]);
// Can't use a match pattern here since `BlsScalar` doesn't derive
// `PartialEq`
if y == &ONE {
Some(-x)
} else if y == &MINUS_ONE {
Some(x)
} else {
y.invert().map(|y| x * (-y))
}
};
c.map(|c| self.append_witness(c))
}
/// Constrain a scalar into the circuit description and return an allocated
/// [`Witness`] with its value
pub fn append_constant<C: Into<BlsScalar>>(
&mut self,
constant: C,
) -> Witness {
let constant = constant.into();
let witness = self.append_witness(constant);
self.assert_equal_constant(witness, constant, None);
witness
}
/// Appends a point in affine form as [`WitnessPoint`]
pub fn append_point<P: Into<JubJubAffine>>(
&mut self,
affine: P,
) -> WitnessPoint {
let affine = affine.into();
let x = self.append_witness(affine.get_u());
let y = self.append_witness(affine.get_v());
WitnessPoint::new(x, y)
}
/// Constrain a point into the circuit description and return an allocated
/// [`WitnessPoint`] with its coordinates
pub fn append_constant_point<P: Into<JubJubAffine>>(
&mut self,
affine: P,
) -> WitnessPoint {
let affine = affine.into();
let x = self.append_constant(affine.get_u());
let y = self.append_constant(affine.get_v());
WitnessPoint::new(x, y)
}
/// Appends a point in affine form as [`WitnessPoint`]
///
/// Creates two public inputs as `(x, y)`
pub fn append_public_point<P: Into<JubJubAffine>>(
&mut self,
affine: P,
) -> WitnessPoint {
let affine = affine.into();
let point = self.append_point(affine);
self.assert_equal_constant(
*point.x(),
BlsScalar::zero(),
Some(affine.get_u()),
);
self.assert_equal_constant(
*point.y(),
BlsScalar::zero(),
Some(affine.get_v()),
);
point
}
/// Allocate a witness value into the composer and return its index.
///
/// Create a public input with the scalar
pub fn append_public<P: Into<BlsScalar>>(&mut self, public: P) -> Witness {
let public = public.into();
let witness = self.append_witness(public);
let constraint = Constraint::new()
.left(-BlsScalar::one())
.a(witness)
.public(public);
self.append_gate(constraint);
witness
}
/// Asserts `a == b` by appending a gate
pub fn assert_equal(&mut self, a: Witness, b: Witness) {
let constraint =
Constraint::new().left(1).right(-BlsScalar::one()).a(a).b(b);
self.append_gate(constraint);
}
/// Adds a logical AND gate that performs the bitwise AND between two values
/// specified first `num_bits = BIT_PAIRS * 2` bits returning a [`Witness`]
/// holding the result.
pub fn append_logic_and<const BIT_PAIRS: usize>(
&mut self,
a: Witness,
b: Witness,
) -> Witness {
self.append_logic_component::<BIT_PAIRS>(a, b, false)
}
/// Adds a logical XOR gate that performs the XOR between two values for the
/// specified first `num_bits = BIT_PAIRS * 2` bits returning a [`Witness`]
/// holding the result.
pub fn append_logic_xor<const BIT_PAIRS: usize>(
&mut self,
a: Witness,
b: Witness,
) -> Witness {
self.append_logic_component::<BIT_PAIRS>(a, b, true)
}
/// Constrain `a` to be equal to `constant + pi`.
///
/// `constant` will be defined as part of the public circuit description.
pub fn assert_equal_constant<C: Into<BlsScalar>>(
&mut self,
a: Witness,
constant: C,
public: Option<BlsScalar>,
) {
let constant = constant.into();
let constraint = Constraint::new()
.left(-BlsScalar::one())
.a(a)
.constant(constant);
let constraint =
public.map(|p| constraint.public(p)).unwrap_or(constraint);
self.append_gate(constraint);
}
/// Asserts that the coordinates of the two points `a` and `b` are the same
/// by appending two gates
pub fn assert_equal_point(&mut self, a: WitnessPoint, b: WitnessPoint) {
self.assert_equal(*a.x(), *b.x());
self.assert_equal(*a.y(), *b.y());
}
/// Asserts `point == public`.
///
/// Will add `public` affine coordinates `(x,y)` as public inputs
pub fn assert_equal_public_point<P: Into<JubJubAffine>>(
&mut self,
point: WitnessPoint,
public: P,
) {
let public = public.into();
self.assert_equal_constant(
*point.x(),
BlsScalar::zero(),
Some(public.get_u()),
);
self.assert_equal_constant(
*point.y(),
BlsScalar::zero(),
Some(public.get_v()),
);
}
/// Adds two curve points by consuming 2 gates.
pub fn component_add_point(
&mut self,
a: WitnessPoint,
b: WitnessPoint,
) -> WitnessPoint {
// In order to verify that two points were correctly added
// without going over a degree 4 polynomial, we will need
// x_1, y_1, x_2, y_2
// x_3, y_3, x_1 * y_2
let x_1 = *a.x();
let y_1 = *a.y();
let x_2 = *b.x();
let y_2 = *b.y();
let p1 = JubJubAffine::from_raw_unchecked(self[x_1], self[y_1]);
let p2 = JubJubAffine::from_raw_unchecked(self[x_2], self[y_2]);
let point: JubJubAffine = (JubJubExtended::from(p1) + p2).into();
let x_3 = point.get_u();
let y_3 = point.get_v();
let x1_y2 = self[x_1] * self[y_2];
let x_1_y_2 = self.append_witness(x1_y2);
let x_3 = self.append_witness(x_3);
let y_3 = self.append_witness(y_3);
// Add the rest of the prepared points into the composer
let constraint = Constraint::new().a(x_1).b(y_1).c(x_2).d(y_2);
let constraint = Constraint::group_add_variable_base(&constraint);
self.append_custom_gate(constraint);
let constraint = Constraint::new().a(x_3).b(y_3).d(x_1_y_2);
self.append_custom_gate(constraint);
WitnessPoint::new(x_3, y_3)
}
/// Adds a boolean constraint (also known as binary constraint) where the
/// gate eq. will enforce that the [`Witness`] received is either `0` or `1`
/// by adding a constraint in the circuit.
///
/// Note that using this constraint with whatever [`Witness`] that
/// is not representing a value equalling 0 or 1, will always force the
/// equation to fail.
pub fn component_boolean(&mut self, a: Witness) {
let zero = Self::ZERO;
let constraint = Constraint::new()
.mult(1)
.output(-BlsScalar::one())
.a(a)
.b(a)
.c(a)
.d(zero);
self.append_gate(constraint);
}
/// Decomposes `scalar` into an array truncated to `N` bits (max 256) in
/// little endian.
/// The `scalar` for 4, for example, would be deconstructed into the array
/// `[0, 0, 1]` for `N = 3` and `[0, 0, 1, 0, 0]` for `N = 5`.
///
/// Asserts the reconstruction of the bits to be equal to `scalar`. So with
/// the above example, the deconstruction of 4 for `N < 3` would result in
/// an unsatisfied circuit.
///
/// Consumes `2 · N + 1` gates
pub fn component_decomposition<const N: usize>(
&mut self,
scalar: Witness,
) -> [Witness; N] {
// Static assertion
assert!(0 < N && N <= 256);
let mut decomposition = [Self::ZERO; N];
let acc = Self::ZERO;
let acc = self[scalar]
.to_bits()
.iter()
.enumerate()
.zip(decomposition.iter_mut())
.fold(acc, |acc, ((i, bit), w_bit)| {
*w_bit = self.append_witness(BlsScalar::from(*bit as u64));
self.component_boolean(*w_bit);
let constraint = Constraint::new()
.left(BlsScalar::pow_of_2(i as u64))
.right(1)
.a(*w_bit)
.b(acc);
self.gate_add(constraint)
});
self.assert_equal(acc, scalar);
decomposition
}
/// Conditionally selects identity as [`WitnessPoint`] based on an input
/// bit.
///
/// bit == 1 => a,
/// bit == 0 => identity,
///
/// `bit` is expected to be constrained by
/// [`Composer::component_boolean`]
pub fn component_select_identity(
&mut self,
bit: Witness,
a: WitnessPoint,
) -> WitnessPoint {
let x = self.component_select_zero(bit, *a.x());
let y = self.component_select_one(bit, *a.y());
WitnessPoint::new(x, y)
}
/// Evaluate `jubjub · point` as a [`WitnessPoint`]
pub fn component_mul_point(
&mut self,
jubjub: Witness,
point: WitnessPoint,
) -> WitnessPoint {
// Turn scalar into bits
let scalar_bits = self.component_decomposition::<252>(jubjub);
let mut result = Self::IDENTITY;
for bit in scalar_bits.iter().rev() {
result = self.component_add_point(result, result);
let point_to_add = self.component_select_identity(*bit, point);
result = self.component_add_point(result, point_to_add);
}
result
}
/// Conditionally selects a [`Witness`] based on an input bit.
///
/// bit == 1 => a,
/// bit == 0 => b,
///
/// `bit` is expected to be constrained by
/// [`Composer::component_boolean`]
pub fn component_select(
&mut self,
bit: Witness,
a: Witness,
b: Witness,
) -> Witness {
// bit * a
let constraint = Constraint::new().mult(1).a(bit).b(a);
let bit_times_a = self.gate_mul(constraint);
// 1 - bit
let constraint =
Constraint::new().left(-BlsScalar::one()).constant(1).a(bit);
let one_min_bit = self.gate_add(constraint);
// (1 - bit) * b
let constraint = Constraint::new().mult(1).a(one_min_bit).b(b);
let one_min_bit_b = self.gate_mul(constraint);
// [ (1 - bit) * b ] + [ bit * a ]
let constraint = Constraint::new()
.left(1)
.right(1)
.a(one_min_bit_b)
.b(bit_times_a);
self.gate_add(constraint)
}
/// Conditionally selects a [`Witness`] based on an input bit.
///
/// bit == 1 => value,
/// bit == 0 => 1,
///
/// `bit` is expected to be constrained by
/// [`Composer::component_boolean`]
pub fn component_select_one(
&mut self,
bit: Witness,
value: Witness,
) -> Witness {
let b = self[bit];
let v = self[value];
let f_x = BlsScalar::one() - b + (b * v);
let f_x = self.append_witness(f_x);
let constraint = Constraint::new()
.mult(1)
.left(-BlsScalar::one())
.output(-BlsScalar::one())
.constant(1)
.a(bit)
.b(value)
.c(f_x);
self.append_gate(constraint);
f_x
}
/// Conditionally selects a [`WitnessPoint`] based on an input bit.
///
/// bit == 1 => a,
/// bit == 0 => b,
///
/// `bit` is expected to be constrained by
/// [`Composer::component_boolean`]
pub fn component_select_point(
&mut self,
bit: Witness,
a: WitnessPoint,
b: WitnessPoint,
) -> WitnessPoint {
let x = self.component_select(bit, *a.x(), *b.x());
let y = self.component_select(bit, *a.y(), *b.y());
WitnessPoint::new(x, y)
}
/// Conditionally selects a [`Witness`] based on an input bit.
///
/// bit == 1 => value,
/// bit == 0 => 0,
///
/// `bit` is expected to be constrained by
/// [`Composer::component_boolean`]
pub fn component_select_zero(
&mut self,
bit: Witness,
value: Witness,
) -> Witness {
let constraint = Constraint::new().mult(1).a(bit).b(value);
self.gate_mul(constraint)
}