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
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.

// 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.

mod assert;
pub use assert::*;

mod async_;
pub use async_::*;

mod call;
pub use call::*;

mod cast;
pub use cast::*;

mod commit;
pub use commit::*;

mod hash;
pub use hash::*;

mod is;
pub use is::*;

mod literals;
pub use literals::*;

mod macros;

mod sign_verify;
pub use sign_verify::*;

use crate::Opcode;
use console::network::prelude::*;

#[allow(unused)]
use console::account::Signature;

pub trait Operation<N: Network, Value: Parser + ToBits, ValueType: Parser, const NUM_OPERANDS: usize> {
    /// The opcode of the operation.
    const OPCODE: Opcode;

    /// Returns the result of evaluating the operation on the given inputs.
    fn evaluate(inputs: &[Value; NUM_OPERANDS]) -> Result<Value>;

    /// Returns the result of executing the operation on the given circuit inputs.
    fn execute<A: circuit::Aleo<Network = N>>(
        inputs: &[circuit::Literal<A>; NUM_OPERANDS],
    ) -> Result<circuit::Literal<A>>;

    /// Returns the output type from the given input types.
    fn output_type(inputs: &[ValueType; NUM_OPERANDS]) -> Result<ValueType>;
}

/// Compute the absolute value of `first`, checking for overflow/underflow, and storing the outcome in `destination`.
pub type Abs<N> = UnaryLiteral<N, AbsOperation<N>>;

crate::operation!(
    pub struct AbsOperation<console::prelude::AbsChecked, circuit::traits::AbsChecked, abs_checked, "abs"> {
        I8 => I8 ("ensure overflows halt"),
        I16 => I16 ("ensure overflows halt"),
        I32 => I32 ("ensure overflows halt"),
        I64 => I64 ("ensure overflows halt"),
        I128 => I128 ("ensure overflows halt"),
    }
);

/// Compute the absolute value of `first`, wrapping around at the boundary of the type, and storing the outcome in `destination`.
pub type AbsWrapped<N> = UnaryLiteral<N, AbsWrappedOperation<N>>;

crate::operation!(
    pub struct AbsWrappedOperation<console::prelude::AbsWrapped, circuit::traits::AbsWrapped, abs_wrapped, "abs.w"> {
        I8 => I8,
        I16 => I16,
        I32 => I32,
        I64 => I64,
        I128 => I128,
    }
);

/// Adds `first` with `second`, storing the outcome in `destination`.
pub type Add<N> = BinaryLiteral<N, AddOperation<N>>;

crate::operation!(
    pub struct AddOperation<core::ops::Add, core::ops::Add, add, "add"> {
        (Field, Field) => Field,
        (Group, Group) => Group,
        (I8, I8) => I8 ("ensure overflows halt"),
        (I16, I16) => I16 ("ensure overflows halt"),
        (I32, I32) => I32 ("ensure overflows halt"),
        (I64, I64) => I64 ("ensure overflows halt"),
        (I128, I128) => I128 ("ensure overflows halt"),
        (U8, U8) => U8 ("ensure overflows halt"),
        (U16, U16) => U16 ("ensure overflows halt"),
        (U32, U32) => U32 ("ensure overflows halt"),
        (U64, U64) => U64 ("ensure overflows halt"),
        (U128, U128) => U128 ("ensure overflows halt"),
        (Scalar, Scalar) => Scalar,
    }
);

/// Adds `first` with `second`, wrapping around at the boundary of the type, and storing the outcome in `destination`.
pub type AddWrapped<N> = BinaryLiteral<N, AddWrappedOperation<N>>;

crate::operation!(
    pub struct AddWrappedOperation<console::prelude::AddWrapped, circuit::traits::AddWrapped, add_wrapped, "add.w"> {
        (I8, I8) => I8,
        (I16, I16) => I16,
        (I32, I32) => I32,
        (I64, I64) => I64,
        (I128, I128) => I128,
        (U8, U8) => U8,
        (U16, U16) => U16,
        (U32, U32) => U32,
        (U64, U64) => U64,
        (U128, U128) => U128,
    }
);

/// Performs a bitwise `and` on `first` and `second`, storing the outcome in `destination`.
pub type And<N> = BinaryLiteral<N, AndOperation<N>>;

crate::operation!(
    pub struct AndOperation<core::ops::BitAnd, core::ops::BitAnd, bitand, "and"> {
        (Boolean, Boolean) => Boolean,
        (I8, I8) => I8,
        (I16, I16) => I16,
        (I32, I32) => I32,
        (I64, I64) => I64,
        (I128, I128) => I128,
        (U8, U8) => U8,
        (U16, U16) => U16,
        (U32, U32) => U32,
        (U64, U64) => U64,
        (U128, U128) => U128,
    }
);

/// Divides `first` by `second`, storing the outcome in `destination`.
pub type Div<N> = BinaryLiteral<N, DivOperation<N>>;

crate::operation!(
    pub struct DivOperation<core::ops::Div, core::ops::Div, div, "div"> {
        (Field, Field) => Field ("ensure divide by zero halts"),
        (I8, I8) => I8 ("ensure overflows halt", "ensure divide by zero halts"),
        (I16, I16) => I16 ("ensure overflows halt", "ensure divide by zero halts"),
        (I32, I32) => I32 ("ensure overflows halt", "ensure divide by zero halts"),
        (I64, I64) => I64 ("ensure overflows halt", "ensure divide by zero halts"),
        (I128, I128) => I128 ("ensure overflows halt", "ensure divide by zero halts"),
        (U8, U8) => U8 ("ensure divide by zero halts"),
        (U16, U16) => U16 ("ensure divide by zero halts"),
        (U32, U32) => U32 ("ensure divide by zero halts"),
        (U64, U64) => U64 ("ensure divide by zero halts"),
        (U128, U128) => U128 ("ensure divide by zero halts"),
        // (Scalar, Scalar) => Scalar,
    }
);

/// Divides `first` by `second`, wrapping around at the boundary of the type, storing the outcome in `destination`.
pub type DivWrapped<N> = BinaryLiteral<N, DivWrappedOperation<N>>;

crate::operation!(
    pub struct DivWrappedOperation<console::prelude::DivWrapped, circuit::traits::DivWrapped, div_wrapped, "div.w"> {
        (I8, I8) => I8 ("ensure divide by zero halts"),
        (I16, I16) => I16 ("ensure divide by zero halts"),
        (I32, I32) => I32 ("ensure divide by zero halts"),
        (I64, I64) => I64 ("ensure divide by zero halts"),
        (I128, I128) => I128 ("ensure divide by zero halts"),
        (U8, U8) => U8 ("ensure divide by zero halts"),
        (U16, U16) => U16 ("ensure divide by zero halts"),
        (U32, U32) => U32 ("ensure divide by zero halts"),
        (U64, U64) => U64 ("ensure divide by zero halts"),
        (U128, U128) => U128 ("ensure divide by zero halts"),
    }
);

/// Doubles `first`, storing the outcome in `destination`.
pub type Double<N> = UnaryLiteral<N, DoubleOperation<N>>;

crate::operation!(
    pub struct DoubleOperation<console::prelude::Double, circuit::traits::Double, double, "double"> {
        Field => Field,
        Group => Group,
    }
);

/// Computes whether `first` is greater than `second` as a boolean, storing the outcome in `destination`.
pub type GreaterThan<N> = BinaryLiteral<N, GreaterThanOperation<N>>;

crate::operation!(
    pub struct GreaterThanOperation<console::prelude::Compare, circuit::traits::Compare, is_greater_than, "gt"> {
        // (Address, Address) => Boolean,
        (Field, Field) => Boolean,
        (I8, I8) => Boolean,
        (I16, I16) => Boolean,
        (I32, I32) => Boolean,
        (I64, I64) => Boolean,
        (I128, I128) => Boolean,
        (U8, U8) => Boolean,
        (U16, U16) => Boolean,
        (U32, U32) => Boolean,
        (U64, U64) => Boolean,
        (U128, U128) => Boolean,
        (Scalar, Scalar) => Boolean,
    }
);

/// Computes whether `first` is greater than or equal to `second` as a boolean, storing the outcome in `destination`.
pub type GreaterThanOrEqual<N> = BinaryLiteral<N, GreaterThanOrEqualOperation<N>>;

crate::operation!(
    pub struct GreaterThanOrEqualOperation<console::prelude::Compare, circuit::traits::Compare, is_greater_than_or_equal, "gte"> {
        // (Address, Address) => Boolean,
        (Field, Field) => Boolean,
        (I8, I8) => Boolean,
        (I16, I16) => Boolean,
        (I32, I32) => Boolean,
        (I64, I64) => Boolean,
        (I128, I128) => Boolean,
        (U8, U8) => Boolean,
        (U16, U16) => Boolean,
        (U32, U32) => Boolean,
        (U64, U64) => Boolean,
        (U128, U128) => Boolean,
        (Scalar, Scalar) => Boolean,
    }
);

/// Computes the multiplicative inverse of `first`, storing the outcome in `destination`.
pub type Inv<N> = UnaryLiteral<N, InvOperation<N>>;

crate::operation!(
    pub struct InvOperation<console::prelude::Inverse, circuit::traits::Inverse, inverse?, "inv"> {
        Field => Field ("ensure inverse of zero halts"),
    }
);

/// Computes whether `first` is less than `second` as a boolean, storing the outcome in `destination`.
pub type LessThan<N> = BinaryLiteral<N, LessThanOperation<N>>;

crate::operation!(
    pub struct LessThanOperation<console::prelude::Compare, circuit::traits::Compare, is_less_than, "lt"> {
        // (Address, Address) => Boolean,
        (Field, Field) => Boolean,
        (I8, I8) => Boolean,
        (I16, I16) => Boolean,
        (I32, I32) => Boolean,
        (I64, I64) => Boolean,
        (I128, I128) => Boolean,
        (U8, U8) => Boolean,
        (U16, U16) => Boolean,
        (U32, U32) => Boolean,
        (U64, U64) => Boolean,
        (U128, U128) => Boolean,
        (Scalar, Scalar) => Boolean,
    }
);

/// Computes whether `first` is less than or equal to `second` as a boolean, storing the outcome in `destination`.
pub type LessThanOrEqual<N> = BinaryLiteral<N, LessThanOrEqualOperation<N>>;

crate::operation!(
    pub struct LessThanOrEqualOperation<console::prelude::Compare, circuit::traits::Compare, is_less_than_or_equal, "lte"> {
        // (Address, Address) => Boolean,
        (Field, Field) => Boolean,
        (I8, I8) => Boolean,
        (I16, I16) => Boolean,
        (I32, I32) => Boolean,
        (I64, I64) => Boolean,
        (I128, I128) => Boolean,
        (U8, U8) => Boolean,
        (U16, U16) => Boolean,
        (U32, U32) => Boolean,
        (U64, U64) => Boolean,
        (U128, U128) => Boolean,
        (Scalar, Scalar) => Boolean,
    }
);

/// Computes the result of `first` mod `second`, storing the outcome in the destination.
pub type Modulo<N> = BinaryLiteral<N, ModuloOperation<N>>;

crate::operation!(
    pub struct ModuloOperation<console::prelude::Modulo, circuit::traits::Modulo, modulo, "mod"> {
        (U8, U8) => U8("ensure divide by zero halts"),
        (U16, U16) => U16("ensure divide by zero halts"),
        (U32, U32) => U32("ensure divide by zero halts"),
        (U64, U64) => U64("ensure divide by zero halts"),
        (U128, U128) => U128("ensure divide by zero halts"),
    }
);

/// Multiplies `first` and `second`, storing the outcome in `destination`.
pub type Mul<N> = BinaryLiteral<N, MulOperation<N>>;

crate::operation!(
    pub struct MulOperation<core::ops::Mul, core::ops::Mul, mul, "mul"> {
        (Field, Field) => Field,
        (Group, Scalar) => Group,
        (Scalar, Group) => Group,
        (I8, I8) => I8 ("ensure overflows halt"),
        (I16, I16) => I16 ("ensure overflows halt"),
        (I32, I32) => I32 ("ensure overflows halt"),
        (I64, I64) => I64 ("ensure overflows halt"),
        (I128, I128) => I128 ("ensure overflows halt"),
        (U8, U8) => U8 ("ensure overflows halt"),
        (U16, U16) => U16 ("ensure overflows halt"),
        (U32, U32) => U32 ("ensure overflows halt"),
        (U64, U64) => U64 ("ensure overflows halt"),
        (U128, U128) => U128 ("ensure overflows halt"),
        // (Scalar, Scalar) => Scalar,
    }
);

/// Multiplies `first` and `second`, wrapping around at the boundary of the type, storing the outcome in `destination`.
pub type MulWrapped<N> = BinaryLiteral<N, MulWrappedOperation<N>>;

crate::operation!(
    pub struct MulWrappedOperation<console::prelude::MulWrapped, circuit::traits::MulWrapped, mul_wrapped, "mul.w"> {
        (I8, I8) => I8,
        (I16, I16) => I16,
        (I32, I32) => I32,
        (I64, I64) => I64,
        (I128, I128) => I128,
        (U8, U8) => U8,
        (U16, U16) => U16,
        (U32, U32) => U32,
        (U64, U64) => U64,
        (U128, U128) => U128,
    }
);

/// Returns `false` if `first` and `second` are `true`, storing the outcome in `destination`.
pub type Nand<N> = BinaryLiteral<N, NandOperation<N>>;

crate::operation!(
    pub struct NandOperation<console::prelude::Nand, circuit::traits::Nand, nand, "nand"> {
        (Boolean, Boolean) => Boolean,
    }
);

/// Negates `first`, storing the outcome in `destination`.
pub type Neg<N> = UnaryLiteral<N, NegOperation<N>>;

crate::operation!(
    pub struct NegOperation<core::ops::Neg, core::ops::Neg, neg, "neg"> {
        Field => Field,
        Group => Group,
        I8 => I8 ("ensure overflows halt"),
        I16 => I16 ("ensure overflows halt"),
        I32 => I32 ("ensure overflows halt"),
        I64 => I64 ("ensure overflows halt"),
        I128 => I128 ("ensure overflows halt"),
    }
);

/// Returns `true` if neither `first` nor `second` is `true`, storing the outcome in `destination`.
pub type Nor<N> = BinaryLiteral<N, NorOperation<N>>;

crate::operation!(
    pub struct NorOperation<console::prelude::Nor, circuit::traits::Nor, nor, "nor"> {
        (Boolean, Boolean) => Boolean,
    }
);

/// Flips each bit in the representation of `first`, storing the outcome in `destination`.
pub type Not<N> = UnaryLiteral<N, NotOperation<N>>;

crate::operation!(
    pub struct NotOperation<core::ops::Not, core::ops::Not, not, "not"> {
        Boolean => Boolean,
        I8 => I8,
        I16 => I16,
        I32 => I32,
        I64 => I64,
        I128 => I128,
        U8 => U8,
        U16 => U16,
        U32 => U32,
        U64 => U64,
        U128 => U128,
    }
);

/// Performs a bitwise `or` on `first` and `second`, storing the outcome in `destination`.
pub type Or<N> = BinaryLiteral<N, OrOperation<N>>;

crate::operation!(
    pub struct OrOperation<core::ops::BitOr, core::ops::BitOr, bitor, "or"> {
        (Boolean, Boolean) => Boolean,
        (I8, I8) => I8,
        (I16, I16) => I16,
        (I32, I32) => I32,
        (I64, I64) => I64,
        (I128, I128) => I128,
        (U8, U8) => U8,
        (U16, U16) => U16,
        (U32, U32) => U32,
        (U64, U64) => U64,
        (U128, U128) => U128,
    }
);

/// Raises `first` to the power of `second`, storing the outcome in `destination`.
pub type Pow<N> = BinaryLiteral<N, PowOperation<N>>;

crate::operation!(
    pub struct PowOperation<console::prelude::Pow, circuit::traits::Pow, pow, "pow"> {
        (Field, Field) => Field,
        (I8, U8) => I8 ("ensure exponentiation overflows halt"),
        (I8, U16) => I8 ("ensure exponentiation overflows halt"),
        (I8, U32) => I8 ("ensure exponentiation overflows halt"),
        (I16, U8) => I16 ("ensure exponentiation overflows halt"),
        (I16, U16) => I16 ("ensure exponentiation overflows halt"),
        (I16, U32) => I16 ("ensure exponentiation overflows halt"),
        (I32, U8) => I32 ("ensure exponentiation overflows halt"),
        (I32, U16) => I32 ("ensure exponentiation overflows halt"),
        (I32, U32) => I32 ("ensure exponentiation overflows halt"),
        (I64, U8) => I64 ("ensure exponentiation overflows halt"),
        (I64, U16) => I64 ("ensure exponentiation overflows halt"),
        (I64, U32) => I64 ("ensure exponentiation overflows halt"),
        (I128, U8) => I128 ("ensure exponentiation overflows halt"),
        (I128, U16) => I128 ("ensure exponentiation overflows halt"),
        (I128, U32) => I128 ("ensure exponentiation overflows halt"),
        (U8, U8) => U8 ("ensure exponentiation overflows halt"),
        (U8, U16) => U8 ("ensure exponentiation overflows halt"),
        (U8, U32) => U8 ("ensure exponentiation overflows halt"),
        (U16, U8) => U16 ("ensure exponentiation overflows halt"),
        (U16, U16) => U16 ("ensure exponentiation overflows halt"),
        (U16, U32) => U16 ("ensure exponentiation overflows halt"),
        (U32, U8) => U32 ("ensure exponentiation overflows halt"),
        (U32, U16) => U32 ("ensure exponentiation overflows halt"),
        (U32, U32) => U32 ("ensure exponentiation overflows halt"),
        (U64, U8) => U64 ("ensure exponentiation overflows halt"),
        (U64, U16) => U64 ("ensure exponentiation overflows halt"),
        (U64, U32) => U64 ("ensure exponentiation overflows halt"),
        (U128, U8) => U128 ("ensure exponentiation overflows halt"),
        (U128, U16) => U128 ("ensure exponentiation overflows halt"),
        (U128, U32) => U128 ("ensure exponentiation overflows halt"),
    }
);

/// Raises `first` to the power of `second`, wrapping around at the boundary of the type, storing the outcome in `destination`.
pub type PowWrapped<N> = BinaryLiteral<N, PowWrappedOperation<N>>;

crate::operation!(
    pub struct PowWrappedOperation<console::prelude::PowWrapped, circuit::traits::PowWrapped, pow_wrapped, "pow.w"> {
        (I8, U8) => I8,
        (I8, U16) => I8,
        (I8, U32) => I8,
        (I16, U8) => I16,
        (I16, U16) => I16,
        (I16, U32) => I16,
        (I32, U8) => I32,
        (I32, U16) => I32,
        (I32, U32) => I32,
        (I64, U8) => I64,
        (I64, U16) => I64,
        (I64, U32) => I64,
        (I128, U8) => I128,
        (I128, U16) => I128,
        (I128, U32) => I128,
        (U8, U8) => U8,
        (U8, U16) => U8,
        (U8, U32) => U8,
        (U16, U8) => U16,
        (U16, U16) => U16,
        (U16, U32) => U16,
        (U32, U8) => U32,
        (U32, U16) => U32,
        (U32, U32) => U32,
        (U64, U8) => U64,
        (U64, U16) => U64,
        (U64, U32) => U64,
        (U128, U8) => U128,
        (U128, U16) => U128,
        (U128, U32) => U128,
    }
);

/// Divides `first` by `second`, storing the remainder in `destination`.
pub type Rem<N> = BinaryLiteral<N, RemOperation<N>>;

crate::operation!(
    pub struct RemOperation<core::ops::Rem, core::ops::Rem, rem, "rem"> {
        (I8, I8) => I8 ("ensure overflows halt", "ensure divide by zero halts"),
        (I16, I16) => I16 ("ensure overflows halt", "ensure divide by zero halts"),
        (I32, I32) => I32 ("ensure overflows halt", "ensure divide by zero halts"),
        (I64, I64) => I64 ("ensure overflows halt", "ensure divide by zero halts"),
        (I128, I128) => I128 ("ensure overflows halt", "ensure divide by zero halts"),
        (U8, U8) => U8 ("ensure divide by zero halts"),
        (U16, U16) => U16 ("ensure divide by zero halts"),
        (U32, U32) => U32 ("ensure divide by zero halts"),
        (U64, U64) => U64 ("ensure divide by zero halts"),
        (U128, U128) => U128 ("ensure divide by zero halts"),
    }
);

/// Divides `first` by `second`, wrapping around at the boundary of the type, storing the remainder in `destination`.
pub type RemWrapped<N> = BinaryLiteral<N, RemWrappedOperation<N>>;

crate::operation!(
    pub struct RemWrappedOperation<console::prelude::RemWrapped, circuit::traits::RemWrapped, rem_wrapped, "rem.w"> {
        (I8, I8) => I8 ("ensure divide by zero halts"),
        (I16, I16) => I16 ("ensure divide by zero halts"),
        (I32, I32) => I32 ("ensure divide by zero halts"),
        (I64, I64) => I64 ("ensure divide by zero halts"),
        (I128, I128) => I128 ("ensure divide by zero halts"),
        (U8, U8) => U8 ("ensure divide by zero halts"),
        (U16, U16) => U16 ("ensure divide by zero halts"),
        (U32, U32) => U32 ("ensure divide by zero halts"),
        (U64, U64) => U64 ("ensure divide by zero halts"),
        (U128, U128) => U128 ("ensure divide by zero halts"),
    }
);

/// Shifts `first` left by `second` bits, storing the outcome in `destination`.
pub type Shl<N> = BinaryLiteral<N, ShlOperation<N>>;

crate::operation!(
    pub struct ShlOperation<console::prelude::ShlChecked, circuit::traits::ShlChecked, shl_checked, "shl"> {
        (I8, U8) => I8 ("ensure shifting past boundary halts"),
        (I8, U16) => I8 ("ensure shifting past boundary halts"),
        (I8, U32) => I8 ("ensure shifting past boundary halts"),
        (I16, U8) => I16 ("ensure shifting past boundary halts"),
        (I16, U16) => I16 ("ensure shifting past boundary halts"),
        (I16, U32) => I16 ("ensure shifting past boundary halts"),
        (I32, U8) => I32 ("ensure shifting past boundary halts"),
        (I32, U16) => I32 ("ensure shifting past boundary halts"),
        (I32, U32) => I32 ("ensure shifting past boundary halts"),
        (I64, U8) => I64 ("ensure shifting past boundary halts"),
        (I64, U16) => I64 ("ensure shifting past boundary halts"),
        (I64, U32) => I64 ("ensure shifting past boundary halts"),
        (I128, U8) => I128 ("ensure shifting past boundary halts"),
        (I128, U16) => I128 ("ensure shifting past boundary halts"),
        (I128, U32) => I128 ("ensure shifting past boundary halts"),
        (U8, U8) => U8 ("ensure shifting past boundary halts"),
        (U8, U16) => U8 ("ensure shifting past boundary halts"),
        (U8, U32) => U8 ("ensure shifting past boundary halts"),
        (U16, U8) => U16 ("ensure shifting past boundary halts"),
        (U16, U16) => U16 ("ensure shifting past boundary halts"),
        (U16, U32) => U16 ("ensure shifting past boundary halts"),
        (U32, U8) => U32 ("ensure shifting past boundary halts"),
        (U32, U16) => U32 ("ensure shifting past boundary halts"),
        (U32, U32) => U32 ("ensure shifting past boundary halts"),
        (U64, U8) => U64 ("ensure shifting past boundary halts"),
        (U64, U16) => U64 ("ensure shifting past boundary halts"),
        (U64, U32) => U64 ("ensure shifting past boundary halts"),
        (U128, U8) => U128 ("ensure shifting past boundary halts"),
        (U128, U16) => U128 ("ensure shifting past boundary halts"),
        (U128, U32) => U128 ("ensure shifting past boundary halts"),
    }
);

/// Shifts `first` left by `second` bits, continuing past the boundary of the type, storing the outcome in `destination`.
pub type ShlWrapped<N> = BinaryLiteral<N, ShlWrappedOperation<N>>;

crate::operation!(
    pub struct ShlWrappedOperation<console::prelude::ShlWrapped, circuit::traits::ShlWrapped, shl_wrapped, "shl.w"> {
        (I8, U8) => I8,
        (I8, U16) => I8,
        (I8, U32) => I8,
        (I16, U8) => I16,
        (I16, U16) => I16,
        (I16, U32) => I16,
        (I32, U8) => I32,
        (I32, U16) => I32,
        (I32, U32) => I32,
        (I64, U8) => I64,
        (I64, U16) => I64,
        (I64, U32) => I64,
        (I128, U8) => I128,
        (I128, U16) => I128,
        (I128, U32) => I128,
        (U8, U8) => U8,
        (U8, U16) => U8,
        (U8, U32) => U8,
        (U16, U8) => U16,
        (U16, U16) => U16,
        (U16, U32) => U16,
        (U32, U8) => U32,
        (U32, U16) => U32,
        (U32, U32) => U32,
        (U64, U8) => U64,
        (U64, U16) => U64,
        (U64, U32) => U64,
        (U128, U8) => U128,
        (U128, U16) => U128,
        (U128, U32) => U128,
    }
);

/// Shifts `first` right by `second` bits, storing the outcome in `destination`.
pub type Shr<N> = BinaryLiteral<N, ShrOperation<N>>;

crate::operation!(
    pub struct ShrOperation<console::prelude::ShrChecked, circuit::traits::ShrChecked, shr_checked, "shr"> {
        (I8, U8) => I8 ("ensure shifting past boundary halts"),
        (I8, U16) => I8 ("ensure shifting past boundary halts"),
        (I8, U32) => I8 ("ensure shifting past boundary halts"),
        (I16, U8) => I16 ("ensure shifting past boundary halts"),
        (I16, U16) => I16 ("ensure shifting past boundary halts"),
        (I16, U32) => I16 ("ensure shifting past boundary halts"),
        (I32, U8) => I32 ("ensure shifting past boundary halts"),
        (I32, U16) => I32 ("ensure shifting past boundary halts"),
        (I32, U32) => I32 ("ensure shifting past boundary halts"),
        (I64, U8) => I64 ("ensure shifting past boundary halts"),
        (I64, U16) => I64 ("ensure shifting past boundary halts"),
        (I64, U32) => I64 ("ensure shifting past boundary halts"),
        (I128, U8) => I128 ("ensure shifting past boundary halts"),
        (I128, U16) => I128 ("ensure shifting past boundary halts"),
        (I128, U32) => I128 ("ensure shifting past boundary halts"),
        (U8, U8) => U8 ("ensure shifting past boundary halts"),
        (U8, U16) => U8 ("ensure shifting past boundary halts"),
        (U8, U32) => U8 ("ensure shifting past boundary halts"),
        (U16, U8) => U16 ("ensure shifting past boundary halts"),
        (U16, U16) => U16 ("ensure shifting past boundary halts"),
        (U16, U32) => U16 ("ensure shifting past boundary halts"),
        (U32, U8) => U32 ("ensure shifting past boundary halts"),
        (U32, U16) => U32 ("ensure shifting past boundary halts"),
        (U32, U32) => U32 ("ensure shifting past boundary halts"),
        (U64, U8) => U64 ("ensure shifting past boundary halts"),
        (U64, U16) => U64 ("ensure shifting past boundary halts"),
        (U64, U32) => U64 ("ensure shifting past boundary halts"),
        (U128, U8) => U128 ("ensure shifting past boundary halts"),
        (U128, U16) => U128 ("ensure shifting past boundary halts"),
        (U128, U32) => U128 ("ensure shifting past boundary halts"),
    }
);

/// Shifts `first` right by `second` bits, continuing past the boundary of the type, storing the outcome in `destination`.
pub type ShrWrapped<N> = BinaryLiteral<N, ShrWrappedOperation<N>>;

crate::operation!(
    pub struct ShrWrappedOperation<console::prelude::ShrWrapped, circuit::traits::ShrWrapped, shr_wrapped, "shr.w"> {
        (I8, U8) => I8,
        (I8, U16) => I8,
        (I8, U32) => I8,
        (I16, U8) => I16,
        (I16, U16) => I16,
        (I16, U32) => I16,
        (I32, U8) => I32,
        (I32, U16) => I32,
        (I32, U32) => I32,
        (I64, U8) => I64,
        (I64, U16) => I64,
        (I64, U32) => I64,
        (I128, U8) => I128,
        (I128, U16) => I128,
        (I128, U32) => I128,
        (U8, U8) => U8,
        (U8, U16) => U8,
        (U8, U32) => U8,
        (U16, U8) => U16,
        (U16, U16) => U16,
        (U16, U32) => U16,
        (U32, U8) => U32,
        (U32, U16) => U32,
        (U32, U32) => U32,
        (U64, U8) => U64,
        (U64, U16) => U64,
        (U64, U32) => U64,
        (U128, U8) => U128,
        (U128, U16) => U128,
        (U128, U32) => U128,
    }
);

/// Squares `first`, storing the outcome in `destination`.
pub type Square<N> = UnaryLiteral<N, SquareOperation<N>>;

crate::operation!(
    pub struct SquareOperation<console::prelude::Square, circuit::traits::Square, square, "square"> {
        Field => Field,
    }
);

/// Computes the square root of `first`, storing the outcome in `destination`.
pub type SquareRoot<N> = UnaryLiteral<N, SquareRootOperation<N>>;

crate::operation!(
    pub struct SquareRootOperation<console::prelude::SquareRoot, circuit::traits::SquareRoot, square_root?, "sqrt"> {
        Field => Field ("ensure quadratic nonresidues halt"),
    }
);

/// Computes `first - second`, storing the outcome in `destination`.
pub type Sub<N> = BinaryLiteral<N, SubOperation<N>>;

crate::operation!(
    pub struct SubOperation<core::ops::Sub, core::ops::Sub, sub, "sub"> {
        (Field, Field) => Field,
        (Group, Group) => Group,
        (I8, I8) => I8 ("ensure overflows halt"),
        (I16, I16) => I16 ("ensure overflows halt"),
        (I32, I32) => I32 ("ensure overflows halt"),
        (I64, I64) => I64 ("ensure overflows halt"),
        (I128, I128) => I128 ("ensure overflows halt"),
        (U8, U8) => U8 ("ensure overflows halt"),
        (U16, U16) => U16 ("ensure overflows halt"),
        (U32, U32) => U32 ("ensure overflows halt"),
        (U64, U64) => U64 ("ensure overflows halt"),
        (U128, U128) => U128 ("ensure overflows halt"),
        // (Scalar, Scalar) => Scalar,
    }
);

/// Computes `first - second`, wrapping around at the boundary of the type, and storing the outcome in `destination`.
pub type SubWrapped<N> = BinaryLiteral<N, SubWrappedOperation<N>>;

crate::operation!(
    pub struct SubWrappedOperation<console::prelude::SubWrapped, circuit::traits::SubWrapped, sub_wrapped, "sub.w"> {
        (I8, I8) => I8,
        (I16, I16) => I16,
        (I32, I32) => I32,
        (I64, I64) => I64,
        (I128, I128) => I128,
        (U8, U8) => U8,
        (U16, U16) => U16,
        (U32, U32) => U32,
        (U64, U64) => U64,
        (U128, U128) => U128,
    }
);

/// Selects `first`, if `condition` is true, otherwise selects `second`, storing the result in `destination`.
pub type Ternary<N> = TernaryLiteral<N, TernaryOperation<N>>;

crate::operation!(
    pub struct TernaryOperation<console::prelude::Ternary, circuit::traits::Ternary, ternary, "ternary"> {
        (Boolean, Address, Address) => Address,
        (Boolean, Boolean, Boolean) => Boolean,
        (Boolean, Field, Field) => Field,
        (Boolean, Group, Group) => Group,
        (Boolean, I8, I8) => I8,
        (Boolean, I16, I16) => I16,
        (Boolean, I32, I32) => I32,
        (Boolean, I64, I64) => I64,
        (Boolean, I128, I128) => I128,
        (Boolean, U8, U8) => U8,
        (Boolean, U16, U16) => U16,
        (Boolean, U32, U32) => U32,
        (Boolean, U64, U64) => U64,
        (Boolean, U128, U128) => U128,
        (Boolean, Scalar, Scalar) => Scalar,
        (Boolean, Signature, Signature) => Signature,
        // (Boolean, StringType, StringType) => StringType,
    }
);

/// Performs a bitwise `xor` on `first` and `second`, storing the outcome in `destination`.
pub type Xor<N> = BinaryLiteral<N, XorOperation<N>>;

crate::operation!(
    pub struct XorOperation<core::ops::BitXor, core::ops::BitXor, bitxor, "xor"> {
        (Boolean, Boolean) => Boolean,
        (I8, I8) => I8,
        (I16, I16) => I16,
        (I32, I32) => I32,
        (I64, I64) => I64,
        (I128, I128) => I128,
        (U8, U8) => U8,
        (U16, U16) => U16,
        (U32, U32) => U32,
        (U64, U64) => U64,
        (U128, U128) => U128,
    }
);