midnight-circuits 7.0.0

Circuit and gadget implementations for Midnight zero-knowledge proofs
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
// This file is part of MIDNIGHT-ZK.
// Copyright (C) 2025 Midnight Foundation
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Elliptic curve (in twisted Edwards form) operations over foreign fields.
//! This module supports curves of the form `a*x^2 + y^2 = 1 + d*x^2*y^2`, where
//! `a` is square and `d` is non-square.
//!
//! We require that the emulated elliptic curve do not have low-order points.
//! In particular, the curve (or the relevant subgroup) must have a large prime
//! order.

use std::{
    cell::RefCell,
    collections::HashMap,
    fmt::Debug,
    hash::{Hash, Hasher},
    rc::Rc,
};

use ff::{Field, PrimeField};
use group::Group;
use midnight_curves::{
    curve25519::{Curve25519, Curve25519Subgroup},
    ff_ext::Legendre,
};
#[cfg(any(test, feature = "testing"))]
use midnight_proofs::plonk::Instance;
use midnight_proofs::{
    circuit::{Chip, Layouter, Value},
    plonk::{Advice, Column, ConstraintSystem, Error, Fixed, Selector},
};
#[cfg(any(test, feature = "testing"))]
use {
    crate::testing_utils::{FromScratch, Sampleable},
    rand::RngCore,
};

use super::common::{
    add_1bit_scalar_bases, configure_multi_select_lookup, fill_dynamic_lookup_row, msm_preprocess,
};
use crate::{
    ecc::{
        curves::{CircuitCurve, EdwardsCurve},
        foreign::gates::edwards::addition::{self, AdditionConfig},
    },
    field::{
        foreign::{
            field_chip::{FieldChip, FieldChipConfig},
            params::FieldEmulationParams,
        },
        AssignedNative,
    },
    instructions::{
        ArithInstructions, AssertionInstructions, AssignmentInstructions, ControlFlowInstructions,
        DecompositionInstructions, EccInstructions, EqualityInstructions, NativeInstructions,
        PublicInputInstructions, ScalarFieldInstructions, ZeroInstructions,
    },
    types::{AssignedBit, AssignedByte, AssignedField, InnerConstants, InnerValue, Instantiable},
    CircuitField,
};

/// Number of columns required by the custom gates of this chip.
pub fn nb_foreign_edwards_chip_columns<F, C, B>() -> usize
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
{
    // Here we only account for the columns that this chip requires for its own
    // custom gates.
    // The outer `+ 1` corresponds to the advice column for the index of
    // `multi_select`.
    B::NB_LIMBS as usize + std::cmp::max(B::NB_LIMBS as usize, 1 + B::moduli().len()) + 1
}

/// Foreign Edwards ECC configuration.
#[derive(Clone, Debug)]
pub struct ForeignEdwardsEccConfig<C>
where
    C: EdwardsCurve,
{
    base_field_config: FieldChipConfig,
    addition_config: AdditionConfig<C>,
    // Dynamic lookup columns for windowed MSM table selection.
    q_multi_select: Selector,
    idx_col_multi_select: Column<Advice>,
    tag_col_multi_select: Column<Fixed>,
}

/// Cache of assigned constant points to their known group element values.
type ConstantPointCache<F, C, B> = Rc<
    RefCell<HashMap<AssignedForeignEdwardsPoint<F, C, B>, <C as CircuitCurve>::CryptographicGroup>>,
>;

/// ECC chip to perform foreign Edwards EC operations.
#[derive(Clone, Debug)]
pub struct ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F>,
{
    config: ForeignEdwardsEccConfig<C>,
    native_gadget: N,
    base_field_chip: FieldChip<F, C::Base, B, N>,
    scalar_field_chip: S,
    /// Per-chip tag counter for dynamic lookup tables (tag 0 is reserved).
    tag_cnt: Rc<RefCell<u64>>,
    /// Cache mapping assigned constant points to their known values.
    constant_cache: ConstantPointCache<F, C, B>,
}

impl<F, C, B, S, N> ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    C::Base: Legendre,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F>,
{
    /// Configures the foreign Edwards ECC chip.
    pub fn configure(
        meta: &mut ConstraintSystem<F>,
        base_field_config: &FieldChipConfig,
        advice_columns: &[Column<Advice>],
        fixed_columns: &[Column<Fixed>],
        nb_parallel_range_checks: usize,
        max_bit_len: u32,
    ) -> ForeignEdwardsEccConfig<C> {
        assert!(C::A.legendre() == 1);
        assert!(C::D.legendre() == -1);

        let addition_config = AdditionConfig::<C>::configure::<F, B>(
            meta,
            base_field_config,
            fixed_columns[0],
            nb_parallel_range_checks,
            max_bit_len,
        );

        let (q_multi_select, idx_col_multi_select, tag_col_multi_select) =
            configure_multi_select_lookup(meta, advice_columns, base_field_config);

        ForeignEdwardsEccConfig {
            base_field_config: base_field_config.clone(),
            addition_config,
            q_multi_select,
            idx_col_multi_select,
            tag_col_multi_select,
        }
    }
}

impl<F, C, B, S, N> ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F>,
{
    /// Creates new foreign Edwards ECC chip from its building blocks.
    pub fn new(
        config: &ForeignEdwardsEccConfig<C>,
        native_gadget: &N,
        scalar_field_chip: &S,
    ) -> Self {
        let base_field_chip = FieldChip::new(&config.base_field_config, native_gadget);
        Self {
            config: config.clone(),
            native_gadget: native_gadget.clone(),
            base_field_chip,
            scalar_field_chip: scalar_field_chip.clone(),
            tag_cnt: Rc::new(RefCell::new(1)),
            constant_cache: Rc::new(RefCell::new(HashMap::new())),
        }
    }

    /// The emulated base field chip of this foreign Edwards ECC chip.
    pub fn base_field_chip(&self) -> &FieldChip<F, C::Base, B, N> {
        &self.base_field_chip
    }

    /// Returns the constant value of `point` if it was created via
    /// `assign_fixed`, or `None` otherwise.
    pub fn as_known_constant(
        &self,
        point: &AssignedForeignEdwardsPoint<F, C, B>,
    ) -> Option<C::CryptographicGroup> {
        self.constant_cache.borrow().get(point).copied()
    }

    /// A chip with instructions for the scalar field of this ECC chip.
    pub fn scalar_field_chip(&self) -> &S {
        &self.scalar_field_chip
    }
}

impl<F, B, S, N> ForeignEdwardsEccChip<F, Curve25519, B, S, N>
where
    F: CircuitField,
    B: FieldEmulationParams<F, <Curve25519 as CircuitCurve>::Base>,
    S: ScalarFieldInstructions<F>,
    S::Scalar: InnerValue<Element = <Curve25519 as CircuitCurve>::ScalarField>,
    N: NativeInstructions<F>,
{
    /// In-circuit compression of a given subgroup point into canonical
    /// little-endian bytes.
    ///
    /// Let p = 2^255 -19 be the base field modulus.
    ///
    /// A curve point (x,y), with coordinates in the range 0 <= x,y < p, is
    /// encoded as follows. First, encode the y-coordinate as a little-endian
    /// array of 32 bytes. The most significant bit of the final byte (i.e., the
    /// most significant byte) is always zero. To form the encoding of the
    /// point, copy the least significant bit of the x-coordinate to the
    /// most significant bit of the final byte of the y-coordinate.
    ///
    /// # Returns
    /// An array [AssignedByte<F>; 32] constrained to represent a canonical
    /// encoding.
    pub fn to_canonical_compressed_bytes(
        &self,
        layouter: &mut impl Layouter<F>,
        point: &AssignedForeignEdwardsPoint<F, Curve25519, B>,
    ) -> Result<[AssignedByte<F>; 32], Error> {
        // Decomposition into (LE) bytes enforces canonicity.
        let mut y_bytes = self.base_field_chip().assigned_to_le_bytes(
            layouter,
            &self.y_coordinate(point),
            None,
        )?;

        let x_bits = self.base_field_chip().assigned_to_le_bits(
            layouter,
            &self.x_coordinate(point),
            Some(255),
            true,
        )?;

        // Encode the sign bit of x (= x mod 2, i.e., the least significant bit of x)
        // into the most significant byte of y: MSB = MSB of y + LSBit of x * 128.
        //
        // (This is safe: y <= p - 1 = 2^255 - 19 - 1, which means MSB of y <= 127;
        // hence, adding 128 causes _no_ overflow.)
        let last_byte: AssignedNative<F> = self.native_gadget.linear_combination(
            layouter,
            &[
                (F::ONE, y_bytes[y_bytes.len() - 1].clone().into()),
                (F::from(128), x_bits[0].clone().into()),
            ],
            F::ZERO,
        )?;

        let last = y_bytes.len() - 1;
        y_bytes[last] = self.native_gadget.convert_unsafe(layouter, &last_byte)?;

        Ok(y_bytes.try_into().expect("exactly 32 bytes"))
    }

    /// In-circuit decompression of little-endian canonical compressed bytes.
    ///
    /// Decoding a point, given as an array of 32 bytes, works as follows: The
    /// caller of this function provides the claimed decoded point as a
    /// witness. The function loads this point into the circuit,
    /// calls [Self::to_canonical_compressed_bytes] and checks if the
    /// resulting byte encoding matches the provided byte encoding.
    ///
    /// # Returns
    /// An [AssignedForeignEdwardsPoint] constrained to lie in the subgroup.
    ///
    /// # Unsatisfiable Circuit
    /// If the given array of [AssignedByte] is a non-canonical encoding of the
    /// point provided by [Value<Curve25519Subgroup>].
    pub fn from_canonical_compressed_bytes(
        &self,
        layouter: &mut impl Layouter<F>,
        compressed_bytes: &[AssignedByte<F>; 32],
        value: Value<Curve25519Subgroup>,
    ) -> Result<AssignedForeignEdwardsPoint<F, Curve25519, B>, Error> {
        let point = self.assign(layouter, value)?;
        let canonical_bytes = self.to_canonical_compressed_bytes(layouter, &point)?;
        compressed_bytes.iter().zip(canonical_bytes.iter()).try_for_each(
            |(com_byte, can_byte)| self.native_gadget.assert_equal(layouter, com_byte, can_byte),
        )?;

        Ok(point)
    }
}

/// Type for foreign Edwards EC points.
#[derive(Clone, Debug)]
#[must_use]
pub struct AssignedForeignEdwardsPoint<F, C, B>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
{
    point: Value<C::CryptographicGroup>,
    x: AssignedField<F, C::Base, B>,
    y: AssignedField<F, C::Base, B>,
}

impl<F, C, B> PartialEq for AssignedForeignEdwardsPoint<F, C, B>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
{
    fn eq(&self, other: &Self) -> bool {
        self.x == other.x && self.y == other.y
    }
}

impl<F, C, B> Eq for AssignedForeignEdwardsPoint<F, C, B>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
{
}

impl<F, C, B> Hash for AssignedForeignEdwardsPoint<F, C, B>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.x.hash(state);
        self.y.hash(state);
    }
}

impl<F, C, B> Instantiable<F> for AssignedForeignEdwardsPoint<F, C, B>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
{
    fn as_public_input(p: &C::CryptographicGroup) -> Vec<F> {
        let (x, y) = (*p).into().coordinates().expect("Edwards coordinates cannot fail");
        [
            AssignedField::<F, C::Base, B>::as_public_input(&x).as_slice(),
            AssignedField::<F, C::Base, B>::as_public_input(&y).as_slice(),
        ]
        .concat()
    }
}

impl<F, C, B> InnerValue for AssignedForeignEdwardsPoint<F, C, B>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
{
    type Element = C::CryptographicGroup;

    fn value(&self) -> Value<Self::Element> {
        self.point
    }
}

impl<F, C, B> InnerConstants for AssignedForeignEdwardsPoint<F, C, B>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
{
    fn inner_zero() -> C::CryptographicGroup {
        C::CryptographicGroup::identity()
    }

    fn inner_one() -> Self::Element {
        C::CryptographicGroup::generator()
    }
}

#[cfg(any(test, feature = "testing"))]
impl<F, C, B> Sampleable for AssignedForeignEdwardsPoint<F, C, B>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
{
    fn sample_inner(rng: impl RngCore) -> C::CryptographicGroup {
        C::CryptographicGroup::random(rng)
    }
}

impl<F, C, B, S, N> Chip<F> for ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F>,
{
    type Config = ForeignEdwardsEccConfig<C>;
    type Loaded = ();
    fn config(&self) -> &Self::Config {
        &self.config
    }
    fn loaded(&self) -> &Self::Loaded {
        &()
    }
}

impl<F, C, B, S, N> ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F>,
{
    /// Converts a subgroup point to [AssignedForeignEdwardsPoint].
    /// The point is _not_ asserted (with constraints) to be on the curve.
    fn assign_point_unchecked(
        &self,
        layouter: &mut impl Layouter<F>,
        value: Value<C::CryptographicGroup>,
    ) -> Result<AssignedForeignEdwardsPoint<F, C, B>, Error> {
        let (val_x, val_y) = value
            .map(|v| v.into().coordinates().expect("Edwards coordinates cannot fail"))
            .unzip();
        let x = self.base_field_chip().assign(layouter, val_x)?;
        let y = self.base_field_chip().assign(layouter, val_y)?;
        let p = AssignedForeignEdwardsPoint::<F, C, B> { point: value, x, y };

        Ok(p)
    }

    /// Asserts the curve equation `a*x^2 + y^2 = 1 + d*x^2*y^2` of an emulated
    /// twisted Edwards curve, given the x and y coordinates in form of
    /// [AssignedField].
    fn assert_on_curve(
        &self,
        layouter: &mut impl Layouter<F>,
        x: &AssignedField<F, C::Base, B>,
        y: &AssignedField<F, C::Base, B>,
    ) -> Result<(), Error> {
        let base_chip = self.base_field_chip();

        // Compute x^2, y^2 and a*x^2 + y^2 - 1 - d*x^2*y^2 in-circuit
        let x_sq = base_chip.mul(layouter, x, x, None)?;
        let y_sq = base_chip.mul(layouter, y, y, None)?;
        let d_xy_sq = base_chip.mul(layouter, &x_sq, &y_sq, Some(C::D))?;
        let lhs = base_chip.linear_combination(
            layouter,
            &[(C::A, x_sq), (C::Base::ONE, y_sq)],
            -C::Base::ONE,
        )?;

        // Assert a*x^2 + y^2 - 1 = d*x^2*y^2
        base_chip.assert_equal(layouter, &lhs, &d_xy_sq)
    }

    /// Adds an assigned point `p` to a constant point `q_val`. Cheaper than
    /// general `add` because the constant coordinates turn emulated `mul`
    /// calls into `mul_by_constant` calls.
    fn add_constant(
        &self,
        layouter: &mut impl Layouter<F>,
        p: &AssignedForeignEdwardsPoint<F, C, B>,
        q_val: C::CryptographicGroup,
    ) -> Result<AssignedForeignEdwardsPoint<F, C, B>, Error> {
        // If both operands are known constants, compute off-circuit.
        if let Some(pv) = self.as_known_constant(p) {
            return self.assign_fixed(layouter, pv + q_val);
        }

        let (qx, qy) = q_val.into().coordinates().expect("Edwards coordinates cannot fail");

        let base_chip = self.base_field_chip();

        let r_value = p.value().map(|pv| pv + q_val);
        let r = self.assign_point_unchecked(layouter, r_value)?;

        let px_qx = base_chip.mul_by_constant(layouter, &p.x, qx)?;
        let py_qy = base_chip.mul_by_constant(layouter, &p.y, qy)?;
        let px_qy = base_chip.mul_by_constant(layouter, &p.x, qy)?;
        let py_qx = base_chip.mul_by_constant(layouter, &p.y, qx)?;
        let neg_a_px_qx = base_chip.mul_by_constant(layouter, &px_qx, -C::A)?;
        let d_px_py_qx_qy = base_chip.mul(layouter, &px_qx, &py_qy, Some(C::D))?;

        // Rx * (1 + d * Px * Py * Qx * Qy) = (Px * Qy + Py * Qx)
        addition::assert_addition_coordinate(
            layouter,
            &r.x,
            &px_qy,
            &py_qx,
            &d_px_py_qx_qy,
            false,
            base_chip,
            &self.config.addition_config,
        )?;

        // Ry * (1 - d * Px * Py * Qx * Qy) = (Py * Qy - a * Px * Qx)
        addition::assert_addition_coordinate(
            layouter,
            &r.y,
            &py_qy,
            &neg_a_px_qx,
            &d_px_py_qx_qy,
            true,
            base_chip,
            &self.config.addition_config,
        )?;

        Ok(AssignedForeignEdwardsPoint {
            point: r_value,
            x: r.x,
            y: r.y,
        })
    }
}

impl<F, C, B, S, N> AssignmentInstructions<F, AssignedForeignEdwardsPoint<F, C, B>>
    for ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F>,
{
    fn assign(
        &self,
        layouter: &mut impl Layouter<F>,
        p_value: Value<C::CryptographicGroup>,
    ) -> Result<AssignedForeignEdwardsPoint<F, C, B>, Error> {
        // Let h be the cofactor of the subgroup.
        //
        // Instead of witnessing P, we witness an h-root Q, and return h * Q.
        // This guarantess that the returned point is in the desired subgroup.
        let cofactor = C::ScalarField::from_u128(C::COFACTOR);
        let q =
            self.assign_point_unchecked(layouter, p_value.map(|p| p * cofactor.invert().unwrap()))?;

        self.assert_on_curve(layouter, &q.x, &q.y)?;
        self.mul_by_constant(layouter, cofactor, &q)
    }

    fn assign_fixed(
        &self,
        layouter: &mut impl Layouter<F>,
        constant: C::CryptographicGroup,
    ) -> Result<AssignedForeignEdwardsPoint<F, C, B>, Error> {
        let (x, y) = constant.into().coordinates().expect("Edwards coordinates cannot fail");
        let x = self.base_field_chip().assign_fixed(layouter, x)?;
        let y = self.base_field_chip().assign_fixed(layouter, y)?;

        let p = AssignedForeignEdwardsPoint::<F, C, B> {
            point: Value::known(constant),
            x,
            y,
        };
        self.constant_cache.borrow_mut().insert(p.clone(), constant);
        Ok(p)
    }
}

impl<F, C, B, S, N> PublicInputInstructions<F, AssignedForeignEdwardsPoint<F, C, B>>
    for ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F> + PublicInputInstructions<F, AssignedBit<F>>,
{
    fn as_public_input(
        &self,
        layouter: &mut impl Layouter<F>,
        p: &AssignedForeignEdwardsPoint<F, C, B>,
    ) -> Result<Vec<AssignedNative<F>>, Error> {
        Ok([
            self.base_field_chip.as_public_input(layouter, &p.x)?.as_slice(),
            self.base_field_chip.as_public_input(layouter, &p.y)?.as_slice(),
        ]
        .concat())
    }

    fn constrain_as_public_input(
        &self,
        layouter: &mut impl Layouter<F>,
        assigned: &AssignedForeignEdwardsPoint<F, C, B>,
    ) -> Result<(), Error> {
        self.as_public_input(layouter, assigned)?
            .iter()
            .try_for_each(|c| self.native_gadget.constrain_as_public_input(layouter, c))
    }

    fn assign_as_public_input(
        &self,
        layouter: &mut impl Layouter<F>,
        value: Value<C::CryptographicGroup>,
    ) -> Result<AssignedForeignEdwardsPoint<F, C, B>, Error> {
        let point = self.assign(layouter, value)?;
        self.constrain_as_public_input(layouter, &point)?;
        Ok(point)
    }
}

/// Inherit assignment instructions for [AssignedField], from the
/// `scalar_field_chip`.
impl<F, C, B, S, SP, N> AssignmentInstructions<F, AssignedField<F, C::ScalarField, SP>>
    for ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F, Scalar = AssignedField<F, C::ScalarField, SP>>,
    SP: FieldEmulationParams<F, C::ScalarField>,
    N: NativeInstructions<F>,
{
    fn assign(
        &self,
        layouter: &mut impl Layouter<F>,
        value: Value<<S::Scalar as InnerValue>::Element>,
    ) -> Result<S::Scalar, Error> {
        self.scalar_field_chip().assign(layouter, value)
    }

    fn assign_fixed(
        &self,
        layouter: &mut impl Layouter<F>,
        constant: <S::Scalar as InnerValue>::Element,
    ) -> Result<S::Scalar, Error> {
        self.scalar_field_chip().assign_fixed(layouter, constant)
    }
}

impl<F, C, B, S, N> AssertionInstructions<F, AssignedForeignEdwardsPoint<F, C, B>>
    for ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F>,
{
    fn assert_equal(
        &self,
        layouter: &mut impl Layouter<F>,
        p: &AssignedForeignEdwardsPoint<F, C, B>,
        q: &AssignedForeignEdwardsPoint<F, C, B>,
    ) -> Result<(), Error> {
        self.base_field_chip().assert_equal(layouter, &p.x, &q.x)?;
        self.base_field_chip().assert_equal(layouter, &p.y, &q.y)
    }

    fn assert_not_equal(
        &self,
        layouter: &mut impl Layouter<F>,
        p: &AssignedForeignEdwardsPoint<F, C, B>,
        q: &AssignedForeignEdwardsPoint<F, C, B>,
    ) -> Result<(), Error> {
        let p_eq_q = self.is_equal(layouter, p, q)?;
        self.native_gadget.assert_equal_to_fixed(layouter, &p_eq_q, false)
    }

    fn assert_equal_to_fixed(
        &self,
        layouter: &mut impl Layouter<F>,
        p: &AssignedForeignEdwardsPoint<F, C, B>,
        constant: C::CryptographicGroup,
    ) -> Result<(), Error> {
        let coordinates = constant.into().coordinates().expect("Edwards coordinates cannot fail");
        self.base_field_chip().assert_equal_to_fixed(layouter, &p.x, coordinates.0)?;
        self.base_field_chip().assert_equal_to_fixed(layouter, &p.y, coordinates.1)
    }

    fn assert_not_equal_to_fixed(
        &self,
        layouter: &mut impl Layouter<F>,
        p: &AssignedForeignEdwardsPoint<F, C, B>,
        constant: C::CryptographicGroup,
    ) -> Result<(), Error> {
        let p_eq_constant = self.is_equal_to_fixed(layouter, p, constant)?;
        self.native_gadget.assert_equal_to_fixed(layouter, &p_eq_constant, false)
    }
}

impl<F, C, B, S, N> EqualityInstructions<F, AssignedForeignEdwardsPoint<F, C, B>>
    for ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F>,
{
    fn is_equal(
        &self,
        layouter: &mut impl Layouter<F>,
        p: &AssignedForeignEdwardsPoint<F, C, B>,
        q: &AssignedForeignEdwardsPoint<F, C, B>,
    ) -> Result<AssignedBit<F>, Error> {
        let eq_x = self.base_field_chip().is_equal(layouter, &p.x, &q.x)?;
        let eq_y = self.base_field_chip().is_equal(layouter, &p.y, &q.y)?;
        self.native_gadget.and(layouter, &[eq_x, eq_y])
    }

    fn is_equal_to_fixed(
        &self,
        layouter: &mut impl Layouter<F>,
        p: &AssignedForeignEdwardsPoint<F, C, B>,
        constant: <AssignedForeignEdwardsPoint<F, C, B> as InnerValue>::Element,
    ) -> Result<AssignedBit<F>, Error> {
        let coordinates = constant.into().coordinates().expect("Edwards coordinates cannot fail");
        let eq_x = self.base_field_chip().is_equal_to_fixed(layouter, &p.x, coordinates.0)?;
        let eq_y = self.base_field_chip().is_equal_to_fixed(layouter, &p.y, coordinates.1)?;
        self.native_gadget.and(layouter, &[eq_x, eq_y])
    }

    fn is_not_equal(
        &self,
        layouter: &mut impl Layouter<F>,
        x: &AssignedForeignEdwardsPoint<F, C, B>,
        y: &AssignedForeignEdwardsPoint<F, C, B>,
    ) -> Result<AssignedBit<F>, Error> {
        let b = self.is_equal(layouter, x, y)?;
        self.native_gadget.not(layouter, &b)
    }

    fn is_not_equal_to_fixed(
        &self,
        layouter: &mut impl Layouter<F>,
        x: &AssignedForeignEdwardsPoint<F, C, B>,
        constant: <AssignedForeignEdwardsPoint<F, C, B> as InnerValue>::Element,
    ) -> Result<AssignedBit<F>, Error> {
        let b = self.is_equal_to_fixed(layouter, x, constant)?;
        self.native_gadget.not(layouter, &b)
    }
}

impl<F, C, B, S, N> ZeroInstructions<F, AssignedForeignEdwardsPoint<F, C, B>>
    for ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F>,
{
    fn is_zero(
        &self,
        layouter: &mut impl Layouter<F>,
        p: &AssignedForeignEdwardsPoint<F, C, B>,
    ) -> Result<AssignedBit<F>, Error> {
        self.is_equal_to_fixed(layouter, p, C::CryptographicGroup::identity())
    }
}

impl<F, C, B, S, N> ControlFlowInstructions<F, AssignedForeignEdwardsPoint<F, C, B>>
    for ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F>,
{
    /// Returns `p` if `cond = 1` and `q` otherwise. In essence, this enforces
    /// `cond * p + (1 - cond) * q = 0` over the emulated twisted Edwards curve.
    fn select(
        &self,
        layouter: &mut impl Layouter<F>,
        cond: &AssignedBit<F>,
        p: &AssignedForeignEdwardsPoint<F, C, B>,
        q: &AssignedForeignEdwardsPoint<F, C, B>,
    ) -> Result<AssignedForeignEdwardsPoint<F, C, B>, Error> {
        let point = p.point.zip(q.point).zip(cond.value()).map(|((p, q), b)| if b { p } else { q });
        let x = self.base_field_chip().select(layouter, cond, &p.x, &q.x)?;
        let y = self.base_field_chip().select(layouter, cond, &p.y, &q.y)?;
        Ok(AssignedForeignEdwardsPoint::<F, C, B> { point, x, y })
    }
}

impl<F, C, B, S, N> EccInstructions<F, C> for ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F>,
{
    type Point = AssignedForeignEdwardsPoint<F, C, B>;
    type Coordinate = AssignedField<F, C::Base, B>;
    type Scalar = S::Scalar;

    fn add(
        &self,
        layouter: &mut impl Layouter<F>,
        p: &Self::Point,
        q: &Self::Point,
    ) -> Result<Self::Point, Error> {
        if p == q {
            return self.double(layouter, p);
        }

        // If one operand is constant, use the cheaper `add_constant` path.
        if let Some(qv) = self.as_known_constant(q) {
            return self.add_constant(layouter, p, qv);
        }
        if let Some(pv) = self.as_known_constant(p) {
            return self.add_constant(layouter, q, pv);
        }

        // Complete addition law on twisted Edwards curve:
        // (see https://eprint.iacr.org/2008/013.pdf)
        //
        // P + Q = R
        // <=>
        // (Px, Py) + (Qx, Qy) = (Rx, Ry)
        // <=>
        // Rx = (Px * Qy +     Py * Qx) / (1 + d * Px * Py * Qx * Qy)
        // Ry = (Py * Qy - a * Px * Qx) / (1 - d * Px * Py * Qx * Qy)
        // <=> (denominators are non-zero)
        // Rx * (1 + d * Px * Py * Qx * Qy) = (Px * Qy +     Py * Qx)
        // Ry * (1 - d * Px * Py * Qx * Qy) = (Py * Qy - a * Px * Qx)

        let base_chip = self.base_field_chip();

        let r_value = p.value().zip(q.value()).map(|(p, q)| p + q);
        let r = self.assign_point_unchecked(layouter, r_value)?;

        let px_qx = base_chip.mul(layouter, &p.x, &q.x, None)?;
        let py_qy = base_chip.mul(layouter, &p.y, &q.y, None)?;
        let px_qy = base_chip.mul(layouter, &p.x, &q.y, None)?;
        let py_qx = base_chip.mul(layouter, &p.y, &q.x, None)?;
        let neg_a_px_qx = base_chip.mul_by_constant(layouter, &px_qx, -C::A)?;
        let d_px_py_qx_qy = base_chip.mul(layouter, &px_qx, &py_qy, Some(C::D))?;

        // Constraint for Rx coordinate
        // Rx * (1 + d * Px * Py * Qx * Qy) = (Px * Qy + Py * Qx)
        addition::assert_addition_coordinate(
            layouter,
            &r.x,
            &px_qy,
            &py_qx,
            &d_px_py_qx_qy,
            false,
            base_chip,
            &self.config.addition_config,
        )?;

        // Constraint for Ry coordinate
        // Ry * (1 - d * Px * Py * Qx * Qy) = (Py * Qy - a * Px * Qx)
        addition::assert_addition_coordinate(
            layouter,
            &r.y,
            &py_qy,
            &neg_a_px_qx,
            &d_px_py_qx_qy,
            true,
            base_chip,
            &self.config.addition_config,
        )?;

        Ok(AssignedForeignEdwardsPoint {
            point: r_value,
            x: r.x,
            y: r.y,
        })
    }

    fn double(
        &self,
        layouter: &mut impl Layouter<F>,
        p: &AssignedForeignEdwardsPoint<F, C, B>,
    ) -> Result<AssignedForeignEdwardsPoint<F, C, B>, Error> {
        if let Some(pv) = self.as_known_constant(p) {
            return self.assign_fixed(layouter, pv + pv);
        }

        // Complete doubling on twisted Edwards curve.
        // (see https://eprint.iacr.org/2008/013.pdf)
        //
        // P + P = R
        // <=>
        // (Px, Py) + (Px, Py) = (Rx, Ry)
        // <=>
        // Rx = (Px * Py +     Py * Px) / (1 + d * Px * Py * Px * Py)
        // Ry = (Py * Py - a * Px * Px) / (1 - d * Px * Py * Px * Py)
        // <=> (denominators are non-zero)
        // Rx * (1 + d * Px^2 * Py^2) = 2 * Px * Py
        // Ry * (1 - d * Px^2 * Py^2) = Py^2 - a * Px^2
        //
        // Since P is on the curve: a * Px^2 + Py^2 = 1 + d * Px^2 * Py^2,
        // we substitute w = a * Px^2 + Py^2 - 1 for d * Px^2 * Py^2,
        // saving one emulated multiplication.

        let base_chip = self.base_field_chip();

        let r_value = p.value().map(|p| p + p);
        let r = self.assign_point_unchecked(layouter, r_value)?;

        let px_sq = base_chip.mul(layouter, &p.x, &p.x, None)?;
        let py_sq = base_chip.mul(layouter, &p.y, &p.y, None)?;
        let px_py = base_chip.mul(layouter, &p.x, &p.y, None)?;

        let neg_a_px_sq = base_chip.mul_by_constant(layouter, &px_sq, -C::A)?;

        // w = d * Px^2 * Py^2 = a * Px^2 + Py^2 - 1  (on-curve relation)
        let w = base_chip.linear_combination(
            layouter,
            &[(C::A, px_sq), (C::Base::ONE, py_sq.clone())],
            -C::Base::ONE,
        )?;

        // Rx * (1 + w) = 2 * Px * Py
        addition::assert_addition_coordinate(
            layouter,
            &r.x,
            &px_py,
            &px_py,
            &w,
            false,
            base_chip,
            &self.config.addition_config,
        )?;

        // Ry * (1 - w) = Py^2 - a * Px^2
        addition::assert_addition_coordinate(
            layouter,
            &r.y,
            &py_sq,
            &neg_a_px_sq,
            &w,
            true,
            base_chip,
            &self.config.addition_config,
        )?;

        Ok(AssignedForeignEdwardsPoint {
            point: r_value,
            x: r.x,
            y: r.y,
        })
    }

    fn negate(
        &self,
        layouter: &mut impl Layouter<F>,
        p: &Self::Point,
    ) -> Result<Self::Point, Error> {
        if let Some(pv) = self.as_known_constant(p) {
            return self.assign_fixed(layouter, -pv);
        }

        // The negation of `P = (x, y)` on a twisted Edwards curve is `-P = (-x, y)`
        let neg_x = self.base_field_chip().neg(layouter, &p.x)?;
        let neg_x = self.base_field_chip().normalize(layouter, &neg_x)?;
        Ok(AssignedForeignEdwardsPoint::<F, C, B> {
            point: -p.point,
            x: neg_x,
            y: p.y.clone(),
        })
    }

    fn msm(
        &self,
        layouter: &mut impl Layouter<F>,
        scalars: &[Self::Scalar],
        bases: &[Self::Point],
    ) -> Result<Self::Point, Error> {
        let scalars = scalars
            .iter()
            .map(|s| (s.clone(), C::ScalarField::NUM_BITS as usize))
            .collect::<Vec<_>>();
        self.msm_by_bounded_scalars(layouter, &scalars, bases)
    }

    fn msm_by_bounded_scalars(
        &self,
        layouter: &mut impl Layouter<F>,
        scalars: &[(S::Scalar, usize)],
        bases: &[AssignedForeignEdwardsPoint<F, C, B>],
    ) -> Result<AssignedForeignEdwardsPoint<F, C, B>, Error> {
        if scalars.len() != bases.len() {
            panic!("Number of scalars and points should be the same.")
        }
        let scalar_chip = self.scalar_field_chip();

        let (scalars, bases, bases_with_1bit_scalar) =
            msm_preprocess(self, scalar_chip, layouter, scalars, bases)?;

        // Decompose scalars to bits with tight bound, then chunk into windows.
        const WS: usize = 4;
        let scalar_windows: Vec<Vec<AssignedNative<F>>> = scalars
            .iter()
            .map(|(s, num_bits)| {
                let bits = scalar_chip.assigned_to_le_bits(layouter, s, Some(*num_bits), true)?;
                bits.chunks(WS)
                    .map(|chunk| self.native_gadget.assigned_from_le_bits(layouter, chunk))
                    .collect::<Result<Vec<_>, _>>()
            })
            .collect::<Result<Vec<_>, _>>()?;

        let res = self.windowed_msm::<WS>(layouter, &scalar_windows, &bases)?;

        // Add 1-bit scalar bases.
        add_1bit_scalar_bases(layouter, self, scalar_chip, &bases_with_1bit_scalar, res)
    }

    fn mul_by_constant(
        &self,
        layouter: &mut impl Layouter<F>,
        scalar: C::ScalarField,
        base: &AssignedForeignEdwardsPoint<F, C, B>,
    ) -> Result<AssignedForeignEdwardsPoint<F, C, B>, Error> {
        if scalar == C::ScalarField::ZERO {
            return self.assign_fixed(layouter, C::CryptographicGroup::identity());
        } else if scalar == C::ScalarField::ONE {
            return Ok(base.clone());
        }

        // If the base is a known constant, compute off-circuit.
        if let Some(base_val) = self.as_known_constant(base) {
            return self.assign_fixed(layouter, base_val * scalar);
        }

        let scalar_bits = scalar.to_bits_le(None);
        let mut p = base.clone();
        let mut res = None;

        // Simple double-and-add
        for (i, b) in scalar_bits.iter().enumerate() {
            if *b {
                res = match res {
                    None => Some(p.clone()),
                    Some(acc) => Some(self.add(layouter, &acc, &p)?),
                }
            }
            // The doubling in the last iteration is not needed
            if i + 1 < scalar_bits.len() {
                p = self.double(layouter, &p)?;
            }
        }

        Ok(res.unwrap_or(self.assign_fixed(layouter, C::CryptographicGroup::identity())?))
    }

    fn point_from_coordinates(
        &self,
        layouter: &mut impl Layouter<F>,
        x: &AssignedField<F, C::Base, B>,
        y: &AssignedField<F, C::Base, B>,
    ) -> Result<Self::Point, Error> {
        let p_value = x.value().zip(y.value()).map_with_result(|(x, y)| {
            C::from_xy(x, y)
                .map(|p| p.into_subgroup())
                .ok_or(Error::Synthesis("invalid coordinates".into()))
        })?;

        let p = self.assign(layouter, p_value)?;

        self.base_field_chip.assert_equal(layouter, x, &self.x_coordinate(&p))?;
        self.base_field_chip.assert_equal(layouter, y, &self.y_coordinate(&p))?;

        Ok(p)
    }

    fn x_coordinate(&self, point: &Self::Point) -> Self::Coordinate {
        point.x.clone()
    }

    fn y_coordinate(&self, point: &Self::Point) -> Self::Coordinate {
        point.y.clone()
    }

    fn base_field(&self) -> &impl DecompositionInstructions<F, Self::Coordinate> {
        self.base_field_chip()
    }

    fn assign_without_subgroup_check(
        &self,
        layouter: &mut impl Layouter<F>,
        value: Value<C::CryptographicGroup>,
    ) -> Result<Self::Point, Error> {
        let p = self.assign_point_unchecked(layouter, value)?;
        self.assert_on_curve(layouter, &p.x, &p.y)?;
        Ok(p)
    }
}

/// Precomputed table of points for windowed MSM.
#[derive(Clone, Debug)]
struct PrecomputedTable<F, C, B, const WS: usize>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
{
    /// Table of precomputed points, where `table[i] = i * base`.
    table: Vec<AssignedForeignEdwardsPoint<F, C, B>>,
}

impl<F, C, B, S, N> ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F>,
{
    /// Builds table `[0*base, 1*base, ..., (2^WS-1)*base]`.
    /// Uses doubling for even indices (`table[2k] = double(table[k])`) and
    /// addition for odd indices (`table[2k+1] = add(table[2k], base)`),
    /// which is cheaper than a linear chain of additions.
    fn precompute<const WS: usize>(
        &self,
        layouter: &mut impl Layouter<F>,
        base: &AssignedForeignEdwardsPoint<F, C, B>,
    ) -> Result<PrecomputedTable<F, C, B, WS>, Error> {
        let identity = self.assign_fixed(layouter, C::CryptographicGroup::identity())?;
        let mut table = vec![identity, base.clone()];
        for i in 2..1 << WS {
            let entry = if i % 2 == 0 {
                self.double(layouter, &table[i / 2])?
            } else {
                self.add(layouter, &table[i - 1], base)?
            };
            table.push(entry);
        }
        Ok(PrecomputedTable { table })
    }

    /// Delegates to [`fill_dynamic_lookup_row`] with this chip's columns.
    #[allow(clippy::type_complexity)]
    fn fill_dynamic_lookup_row(
        &self,
        layouter: &mut impl Layouter<F>,
        point: &AssignedForeignEdwardsPoint<F, C, B>,
        index: &AssignedNative<F>,
        table_tag: F,
        enable_lookup: bool,
    ) -> Result<(Vec<AssignedNative<F>>, Vec<AssignedNative<F>>), Error> {
        fill_dynamic_lookup_row(
            layouter,
            &point.x.limb_values(),
            &point.y.limb_values(),
            index,
            &self.config.base_field_config.x_cols,
            &self.config.base_field_config.z_cols, // z_cols used for y (y_cols == x_cols)
            self.config.idx_col_multi_select,
            self.config.tag_col_multi_select,
            self.config.q_multi_select,
            table_tag,
            enable_lookup,
        )
    }

    /// Loads a precomputed point table into the dynamic lookup.  Entry `i` is
    /// paired with index `i` and the given `table_tag`.
    fn load_multi_select_table(
        &self,
        layouter: &mut impl Layouter<F>,
        point_table: &[AssignedForeignEdwardsPoint<F, C, B>],
        table_tag: F,
    ) -> Result<(), Error> {
        for (i, point) in point_table.iter().enumerate() {
            let index = self.native_gadget.assign_fixed(layouter, F::from(i as u64))?;
            self.fill_dynamic_lookup_row(layouter, point, &index, table_tag, false)?;
        }
        Ok(())
    }

    /// Returns `point_table[selector]` using the dynamic lookup.
    ///
    /// The table must have been loaded via [`Self::load_multi_select_table`]
    /// with the same `table_tag`.
    fn multi_select(
        &self,
        layouter: &mut impl Layouter<F>,
        selector: &AssignedNative<F>,
        point_table: &[AssignedForeignEdwardsPoint<F, C, B>],
        table_tag: F,
    ) -> Result<AssignedForeignEdwardsPoint<F, C, B>, Error> {
        let mut selector_idx = 0usize;
        selector.value().map(|v| {
            let digits = v.to_biguint().to_u32_digits();
            let digit = if digits.is_empty() { 0 } else { digits[0] };
            debug_assert!(digits.len() <= 1);
            debug_assert!((digit as usize) < point_table.len());
            selector_idx = digit as usize;
        });

        let selected = point_table[selector_idx].clone();

        let (xs, ys) =
            self.fill_dynamic_lookup_row(layouter, &selected, selector, table_tag, true)?;
        let x = AssignedField::<F, C::Base, B>::from_limbs_unsafe(xs);
        let y = AssignedField::<F, C::Base, B>::from_limbs_unsafe(ys);

        Ok(AssignedForeignEdwardsPoint::<F, C, B> {
            point: selected.point,
            x,
            y,
        })
    }

    /// Windowed interleaved MSM over pre-chunked scalars. Each scalar is a
    /// sequence of WS-bit window values (native field elements). Shares the
    /// doubling chain across all bases. Horner evaluation. Scalars may have
    /// different numbers of windows; short scalars are skipped in high windows.
    /// Point selection uses a dynamic-lookup table.
    fn windowed_msm<const WS: usize>(
        &self,
        layouter: &mut impl Layouter<F>,
        scalars: &[Vec<AssignedNative<F>>],
        bases: &[AssignedForeignEdwardsPoint<F, C, B>],
    ) -> Result<AssignedForeignEdwardsPoint<F, C, B>, Error> {
        assert_eq!(scalars.len(), bases.len());

        if bases.is_empty() {
            return self.assign_fixed(layouter, C::CryptographicGroup::identity());
        }

        // Number of windows per scalar.
        let num_windows: Vec<usize> = scalars.iter().map(|s| s.len()).collect();
        let max_num_windows = *num_windows.iter().max().unwrap();

        // Precompute tables for each base and load them into the dynamic lookup.
        let tag_cnt = *self.tag_cnt.borrow();
        self.tag_cnt.replace(tag_cnt + bases.len() as u64);
        debug_assert!(F::NUM_BITS > 64);

        let mut tables = vec![];
        for (i, base) in bases.iter().enumerate() {
            let table = self.precompute::<WS>(layouter, base)?;
            self.load_multi_select_table(layouter, &table.table, F::from(tag_cnt + i as u64))?;
            tables.push(table);
        }

        let mut res = self.assign_fixed(layouter, C::CryptographicGroup::identity())?;
        for w in (0..max_num_windows).rev() {
            // Skip doubling in the most-significant window.
            if w < max_num_windows - 1 {
                for _ in 0..WS {
                    res = self.double(layouter, &res)?;
                }
            }
            for (i, (windows, nw)) in scalars.iter().zip(&num_windows).enumerate() {
                // Skip scalars that have no windows in this position.
                if w >= *nw {
                    continue;
                }
                let addend = self.multi_select(
                    layouter,
                    &windows[w],
                    &tables[i].table,
                    F::from(tag_cnt + i as u64),
                )?;
                res = self.add(layouter, &res, &addend)?;
            }
        }

        Ok(res)
    }
}

#[derive(Clone, Debug)]
#[cfg(any(test, feature = "testing"))]
/// Configuration used to implement `FromScratch` for the
/// `ForeignEdwardsEccChip` chip. This should only be used for testing.
pub struct ForeignEdwardsEccTestConfig<F, C, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    S: ScalarFieldInstructions<F> + FromScratch<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F> + FromScratch<F>,
{
    native_gadget_config: <N as FromScratch<F>>::Config,
    scalar_field_config: <S as FromScratch<F>>::Config,
    ff_ecc_config: ForeignEdwardsEccConfig<C>,
}

#[cfg(any(test, feature = "testing"))]
impl<F, C, B, S, N> FromScratch<F> for ForeignEdwardsEccChip<F, C, B, S, N>
where
    F: CircuitField,
    C: EdwardsCurve,
    C::Base: Legendre,
    B: FieldEmulationParams<F, C::Base>,
    S: ScalarFieldInstructions<F> + FromScratch<F>,
    S::Scalar: InnerValue<Element = C::ScalarField>,
    N: NativeInstructions<F> + FromScratch<F>,
{
    type Config = ForeignEdwardsEccTestConfig<F, C, S, N>;

    fn new_from_scratch(config: &Self::Config) -> Self {
        let native_gadget = <N as FromScratch<F>>::new_from_scratch(&config.native_gadget_config);
        let scalar_field_chip =
            <S as FromScratch<F>>::new_from_scratch(&config.scalar_field_config);
        ForeignEdwardsEccChip::new(&config.ff_ecc_config, &native_gadget, &scalar_field_chip)
    }

    fn load_from_scratch(&self, layouter: &mut impl Layouter<F>) -> Result<(), Error> {
        self.native_gadget.load_from_scratch(layouter)?;
        self.scalar_field_chip.load_from_scratch(layouter)
    }

    fn configure_from_scratch(
        meta: &mut ConstraintSystem<F>,
        advice_columns: &mut Vec<Column<Advice>>,
        fixed_columns: &mut Vec<Column<Fixed>>,
        instance_columns: &[Column<Instance>; 2],
    ) -> ForeignEdwardsEccTestConfig<F, C, S, N> {
        use crate::field::foreign::nb_field_chip_columns;

        let native_gadget_config = <N as FromScratch<F>>::configure_from_scratch(
            meta,
            advice_columns,
            fixed_columns,
            instance_columns,
        );
        let scalar_field_config = <S as FromScratch<F>>::configure_from_scratch(
            meta,
            advice_columns,
            fixed_columns,
            instance_columns,
        );
        let nb_advice_cols = nb_field_chip_columns::<F, C::Base, B>();
        while advice_columns.len() < nb_advice_cols {
            advice_columns.push(meta.advice_column());
        }
        let nb_parallel_range_checks = 4;
        let max_bit_len = 8;
        let base_field_config = FieldChip::<F, C::Base, B, N>::configure(
            meta,
            &advice_columns[..nb_advice_cols],
            nb_parallel_range_checks,
            max_bit_len,
        );
        let ff_ecc_config = ForeignEdwardsEccChip::<F, C, B, S, N>::configure(
            meta,
            &base_field_config,
            advice_columns,
            fixed_columns,
            nb_parallel_range_checks,
            max_bit_len,
        );
        ForeignEdwardsEccTestConfig {
            native_gadget_config,
            scalar_field_config,
            ff_ecc_config,
        }
    }
}

#[cfg(test)]
mod tests {
    use ff::Field;
    use group::{Group, GroupEncoding};
    use midnight_curves::{curve25519::Curve25519, BlsScalar};
    use midnight_proofs::{circuit::SimpleFloorPlanner, dev::MockProver, plonk::Circuit};
    use rand::SeedableRng;
    use rand_chacha::ChaCha8Rng;

    use super::*;
    use crate::{
        ecc::curves::CircuitCurve,
        field::{
            decomposition::chip::P2RDecompositionChip, foreign::params::MultiEmulationParams,
            NativeChip, NativeGadget,
        },
        instructions::{assertions, control_flow, ecc, equality, public_input, zero},
    };

    type F = BlsScalar;
    type Native<F> = NativeGadget<F, P2RDecompositionChip<F>, NativeChip<F>>;
    type EmulatedField<F, C> = FieldChip<F, <C as Group>::Scalar, MultiEmulationParams, Native<F>>;

    macro_rules! test_generic {
        ($mod:ident, $op:ident, $native:ty, $curve:ty, $scalar_field:ty,
    $name:expr) => {
            $mod::tests::$op::<
                $native,
                AssignedForeignEdwardsPoint<$native, $curve, MultiEmulationParams>,
                ForeignEdwardsEccChip<
                    $native,
                    $curve,
                    MultiEmulationParams,
                    $scalar_field,
                    Native<$native>,
                >,
            >($name);
        };
    }

    macro_rules! test {
        ($mod:ident, $op:ident) => {
            #[test]
            fn $op() {
                test_generic!($mod, $op, F, Curve25519, EmulatedField<F, Curve25519>, "emulated_curve25519");
            }
        };
    }

    test!(assertions, test_assertions);

    test!(public_input, test_public_inputs);

    test!(equality, test_is_equal);

    test!(zero, test_zero_assertions);
    test!(zero, test_is_zero);

    test!(control_flow, test_select);
    test!(control_flow, test_cond_assert_equal);
    test!(control_flow, test_cond_swap);

    macro_rules! ecc_test {
        ($op:ident, $native:ty, $curve:ty, $scalar_field:ty, $name:expr) => {
            ecc::tests::$op::<
                $native,
                $curve,
                ForeignEdwardsEccChip<
                    $native,
                    $curve,
                    MultiEmulationParams,
                    $scalar_field,
                    Native<$native>,
                >,
            >($name);
        };
    }

    macro_rules! ecc_tests {
        ($op:ident) => {
            #[test]
            fn $op() {
                ecc_test!($op, BlsScalar, Curve25519, EmulatedField<BlsScalar, Curve25519>, "emulated_curve25519");
            }
        };
    }

    ecc_tests!(test_assign);
    ecc_tests!(test_assign_without_subgroup_check);
    ecc_tests!(test_add);
    ecc_tests!(test_double);
    ecc_tests!(test_negate);
    ecc_tests!(test_msm);
    ecc_tests!(test_msm_by_bounded_scalars);
    ecc_tests!(test_mul_by_constant);
    ecc_tests!(test_coordinates);

    #[test]
    fn test_assert_on_curve() {
        run_test_assert_on_curve::<Curve25519>();
    }

    /// Negative tests for `assert_on_curve`. Positive cases (identity,
    /// generator, random points) are covered by the generic `test_assign`.
    fn run_test_assert_on_curve<C>()
    where
        C: EdwardsCurve,
        C::Base: Legendre,
        MultiEmulationParams: FieldEmulationParams<BlsScalar, C::Base>
            + FieldEmulationParams<BlsScalar, C::ScalarField>,
    {
        fn assert_not_on_curve<C: EdwardsCurve>(x: C::Base, y: C::Base)
        where
            C::Base: Legendre,
            MultiEmulationParams: FieldEmulationParams<BlsScalar, C::Base>
                + FieldEmulationParams<BlsScalar, C::ScalarField>,
        {
            let circuit = OnCurveCheckCircuit::<C> { x, y };
            let prover = MockProver::run(&circuit, vec![vec![], vec![]])
                .expect("proof generation should not fail");
            assert!(prover.verify().is_err());
        }

        let mut rng = ChaCha8Rng::seed_from_u64(0x0);

        // Random point with y offset by 1
        let point = C::CryptographicGroup::random(&mut rng);
        let (x, y) = point.into().coordinates().expect("valid curve point");

        assert_not_on_curve::<C>(x, y + C::Base::ONE);
        assert_not_on_curve::<C>(C::Base::ONE, C::Base::ONE);
        assert_not_on_curve::<C>(C::Base::ZERO, C::Base::ZERO);
    }

    type EdwardsChip<C> = ForeignEdwardsEccChip<
        F,
        C,
        MultiEmulationParams,
        FieldChip<F, <C as CircuitCurve>::ScalarField, MultiEmulationParams, Native<F>>,
        Native<F>,
    >;

    /// Test circuit that calls `assert_on_curve` for arbitrary (x, y)
    /// coordinates (not necessarily representing a valid curve point).
    ///
    /// This circuit checks if `assert_on_curve` correctly verifies, or fails,
    /// on a selected set of inputs.
    #[derive(Clone, Debug)]
    struct OnCurveCheckCircuit<C: EdwardsCurve> {
        x: C::Base,
        y: C::Base,
    }

    impl<C> Circuit<F> for OnCurveCheckCircuit<C>
    where
        C: EdwardsCurve,
        C::Base: Legendre,
        MultiEmulationParams:
            FieldEmulationParams<F, C::Base> + FieldEmulationParams<F, C::ScalarField>,
    {
        type Config = <EdwardsChip<C> as FromScratch<F>>::Config;
        type FloorPlanner = SimpleFloorPlanner;
        type Params = ();

        fn without_witnesses(&self) -> Self {
            unreachable!()
        }

        fn configure(meta: &mut ConstraintSystem<F>) -> Self::Config {
            let committed = meta.instance_column();
            let instance = meta.instance_column();
            EdwardsChip::<C>::configure_from_scratch(
                meta,
                &mut vec![],
                &mut vec![],
                &[committed, instance],
            )
        }

        fn synthesize(
            &self,
            config: Self::Config,
            mut layouter: impl Layouter<F>,
        ) -> Result<(), Error> {
            let chip = EdwardsChip::<C>::new_from_scratch(&config);

            let x = chip.base_field_chip().assign(&mut layouter, Value::known(self.x))?;
            let y = chip.base_field_chip().assign(&mut layouter, Value::known(self.y))?;

            chip.assert_on_curve(&mut layouter, &x, &y)?;
            chip.load_from_scratch(&mut layouter)
        }
    }

    /// Test circuit that calls `from_canonical_compressed_bytes` on a
    /// given byte array together with the claimed subgroup point.
    ///
    /// The proof succeeds if and only if the byte array is the canonical
    /// encoding of the subgroup point.
    #[derive(Clone, Debug)]
    struct FromCompressedBytesCheckCircuit {
        point: Curve25519Subgroup,
        bytes: [u8; 32],
    }

    impl Circuit<F> for FromCompressedBytesCheckCircuit {
        type Config = <EdwardsChip<Curve25519> as FromScratch<F>>::Config;
        type FloorPlanner = SimpleFloorPlanner;
        type Params = ();

        fn without_witnesses(&self) -> Self {
            unreachable!()
        }

        fn configure(meta: &mut ConstraintSystem<F>) -> Self::Config {
            let committed = meta.instance_column();
            let instance = meta.instance_column();
            EdwardsChip::<Curve25519>::configure_from_scratch(
                meta,
                &mut vec![],
                &mut vec![],
                &[committed, instance],
            )
        }

        fn synthesize(
            &self,
            config: Self::Config,
            mut layouter: impl Layouter<F>,
        ) -> Result<(), Error> {
            let chip = EdwardsChip::<Curve25519>::new_from_scratch(&config);

            let byte_cells: [AssignedByte<F>; 32] = self
                .bytes
                .iter()
                .map(|b| chip.native_gadget.assign(&mut layouter, Value::known(*b)))
                .collect::<Result<Vec<_>, _>>()?
                .try_into()
                .expect("exactly 32 bytes");

            let _ = chip.from_canonical_compressed_bytes(
                &mut layouter,
                &byte_cells,
                Value::known(self.point),
            )?;

            chip.load_from_scratch(&mut layouter)
        }
    }

    fn run_test_compressed_bytes(point: Curve25519Subgroup, bytes: [u8; 32], should_accept: bool) {
        let circuit = FromCompressedBytesCheckCircuit { point, bytes };
        let prover = MockProver::run(&circuit, vec![vec![], vec![]])
            .expect("proof generation should not fail");
        assert_eq!(prover.verify().is_ok(), should_accept);
    }

    #[test]
    fn test_compressed_bytes() {
        // Canonical LE encoding of the identity with y = 1 and sign_x = 0.
        let mut canonical = [0; 32];
        canonical[0] = 1;
        run_test_compressed_bytes(Curve25519Subgroup::identity(), canonical, true);

        // Non-canonical LE encoding of the identity with y = 2^255 - 18 and sign_x = 0.
        let mut non_canonical = [0xff_u8; 32];
        non_canonical[0] = 0xee;
        non_canonical[31] = 0x7f;
        run_test_compressed_bytes(Curve25519Subgroup::identity(), non_canonical, false);

        // Non-canonical LE encoding of the identity with y = 1 and sign_x = 1.
        let mut non_canonical_with_sign = canonical;
        non_canonical_with_sign[31] = 0x80;
        run_test_compressed_bytes(
            Curve25519Subgroup::identity(),
            non_canonical_with_sign,
            false,
        );

        // Canonical LE encoding of the subgroup generator.
        let g = Curve25519Subgroup::generator();
        run_test_compressed_bytes(g, Curve25519::from(g).to_bytes(), true);

        // Canonical LE encoding of a random subgroup point.
        let mut rng = ChaCha8Rng::seed_from_u64(0x7374727564656C);
        let p = Curve25519Subgroup::random(&mut rng);
        run_test_compressed_bytes(p, Curve25519::from(p).to_bytes(), true);
    }
}