-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathwriter.rs
889 lines (759 loc) · 31.8 KB
/
writer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
use std::fmt::{self, Display, Formatter};
use ark_ff::{One, Zero};
use kimchi::circuits::wires::Wire;
use num_bigint::BigUint;
use serde::{Deserialize, Serialize};
use crate::{
backends::{kimchi::VestaField, Backend},
circuit_writer::{CircuitWriter, DebugInfo, FnEnv, VarInfo},
constants::Span,
constraints::{boolean, field},
error::{Error, ErrorKind, Result},
imports::FnKind,
parser::{
types::{ForLoopArgument, FunctionDef, Stmt, StmtKind, TyKind},
Expr, ExprKind, Op2,
},
syntax::is_type,
type_checker::FullyQualified,
var::{ConstOrCell, Value, Var, VarOrRef},
};
//
// Data structures
//
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum GateKind {
Zero,
DoubleGeneric,
Poseidon,
}
impl From<GateKind> for kimchi::circuits::gate::GateType {
fn from(gate_kind: GateKind) -> Self {
use kimchi::circuits::gate::GateType::*;
match gate_kind {
GateKind::Zero => Zero,
GateKind::DoubleGeneric => Generic,
GateKind::Poseidon => Poseidon,
}
}
}
// TODO: this could also contain the span that defined the gate!
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Gate {
/// Type of gate
pub typ: GateKind,
/// Coefficients
#[serde(skip)]
pub coeffs: Vec<VestaField>,
}
impl Gate {
pub fn to_kimchi_gate(&self, row: usize) -> kimchi::circuits::gate::CircuitGate<VestaField> {
kimchi::circuits::gate::CircuitGate {
typ: self.typ.into(),
wires: Wire::for_row(row),
coeffs: self.coeffs.clone(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Cell {
pub row: usize,
pub col: usize,
}
impl Display for Cell {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "({},{})", self.row, self.col)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Wiring {
/// Not yet wired (just indicates the position of the cell itself)
NotWired(AnnotatedCell),
/// The wiring (associated to different spans)
Wired(Vec<AnnotatedCell>),
}
#[derive(Debug, Clone, Serialize, Deserialize, Eq)]
pub struct AnnotatedCell {
pub(crate) cell: Cell,
pub(crate) debug: DebugInfo,
}
impl PartialEq for AnnotatedCell {
fn eq(&self, other: &Self) -> bool {
self.cell == other.cell
}
}
impl PartialOrd for AnnotatedCell {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.cell.partial_cmp(&other.cell)
}
}
impl Ord for AnnotatedCell {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.cell.cmp(&other.cell)
}
}
//
// Circuit Writer (also used by witness generation)
//
impl<B: Backend> CircuitWriter<B> {
fn compile_stmt(
&mut self,
fn_env: &mut FnEnv<B::Field, B::Var>,
stmt: &Stmt,
) -> Result<Option<VarOrRef<B>>> {
match &stmt.kind {
StmtKind::Assign { mutable, lhs, rhs } => {
// compute the rhs
let rhs_var = self
.compute_expr(fn_env, rhs)?
.ok_or_else(|| self.error(ErrorKind::CannotComputeExpression, stmt.span))?;
// obtain the actual values
let rhs_var = rhs_var.value(self, fn_env);
let typ = self.expr_type(rhs).cloned();
let var_info = VarInfo::new(rhs_var, *mutable, typ);
// store the new variable
// TODO: do we really need to store that in the scope? That's not an actual var in the scope that's an internal var...
self.add_local_var(fn_env, lhs.value.clone(), var_info)?;
}
StmtKind::ForLoop {
var,
argument,
body,
} => {
match argument {
ForLoopArgument::Range(range) => {
// compute the start and end of the range
let start_bg: BigUint = self
.compute_expr(fn_env, &range.start)?
.ok_or_else(|| {
self.error(ErrorKind::CannotComputeExpression, range.start.span)
})?
.constant()
.expect("expected constant")
.into();
let start: u32 = start_bg.try_into().map_err(|_| {
self.error(ErrorKind::InvalidRangeSize, range.start.span)
})?;
let end_bg: BigUint = self
.compute_expr(fn_env, &range.end)?
.ok_or_else(|| {
self.error(ErrorKind::CannotComputeExpression, range.end.span)
})?
.constant()
.expect("expected constant")
.into();
let end: u32 = end_bg
.try_into()
.map_err(|_| self.error(ErrorKind::InvalidRangeSize, range.end.span))?;
// compute for the for loop block
for ii in start..end {
fn_env.nest();
let cst_var = Var::new_constant(ii.into(), var.span);
let var_info = VarInfo::new(
cst_var,
false,
Some(TyKind::Field { constant: true }),
);
self.add_local_var(fn_env, var.value.clone(), var_info)?;
self.compile_block(fn_env, body)?;
fn_env.pop();
}
}
ForLoopArgument::Iterator(iterator) => {
let iterator_var = self
.compute_expr(fn_env, iterator)?
.expect("array access on non-array");
let array_typ = self
.expr_type(iterator)
.cloned()
.expect("cannot find type of array");
let (elem_type, array_len) = match array_typ {
TyKind::Array(ty, array_len) => (ty, array_len),
_ => Err(Error::new(
"compile-stmt",
ErrorKind::UnexpectedError("expected array"),
stmt.span,
))?,
};
// compute the size of each element in the array
let len = self.size_of(&elem_type);
for idx in 0..array_len {
// compute the real index
let idx = idx as usize;
let start = idx * len;
fn_env.nest();
// add the variable to the inner enviroment corresponding
// to iterator[idx]
let indexed_var = iterator_var.narrow(start, len).value(self, fn_env);
let var_info =
VarInfo::new(indexed_var.clone(), false, Some(*elem_type.clone()));
self.add_local_var(fn_env, var.value.clone(), var_info)?;
self.compile_block(fn_env, body)?;
fn_env.pop();
}
}
}
}
StmtKind::Expr(expr) => {
// compute the expression
let var = self.compute_expr(fn_env, expr)?;
// make sure it does not return any value.
assert!(var.is_none());
}
StmtKind::Return(expr) => {
let var = self
.compute_expr(fn_env, expr)?
.ok_or_else(|| self.error(ErrorKind::CannotComputeExpression, stmt.span))?;
// we already checked in type checking that this is not an early return
return Ok(Some(var));
}
StmtKind::Comment(_) => (),
}
Ok(None)
}
/// might return something?
fn compile_block(
&mut self,
fn_env: &mut FnEnv<B::Field, B::Var>,
stmts: &[Stmt],
) -> Result<Option<Var<B::Field, B::Var>>> {
fn_env.nest();
for stmt in stmts {
let res = self.compile_stmt(fn_env, stmt)?;
if let Some(var) = res {
// a block doesn't return a pointer, only values
let var = var.value(self, fn_env);
// we already checked for early returns in type checking
return Ok(Some(var));
}
}
fn_env.pop();
Ok(None)
}
fn compile_native_function_call(
&mut self,
function: &FunctionDef,
args: Vec<VarInfo<B::Field, B::Var>>,
) -> Result<Option<Var<B::Field, B::Var>>> {
assert!(!function.is_main());
// create new fn_env
let fn_env = &mut FnEnv::new();
// set arguments
assert_eq!(function.sig.arguments.len(), args.len());
for (name, var_info) in function.sig.arguments.iter().zip(args) {
self.add_local_var(fn_env, name.name.value.clone(), var_info)?;
}
// compile it and potentially return a return value
self.compile_block(fn_env, &function.body)
}
pub(crate) fn constrain_inputs_to_main(
&mut self,
input: &[ConstOrCell<B::Field, B::Var>],
input_typ: &TyKind,
span: Span,
) -> Result<()> {
match input_typ {
TyKind::Field { constant: false } => (),
TyKind::Bool => {
assert_eq!(input.len(), 1);
boolean::check(self, &input[0], span);
}
TyKind::Array(tykind, _) => {
let el_size = self.size_of(tykind);
for el in input.chunks(el_size) {
self.constrain_inputs_to_main(el, tykind, span)?;
}
}
TyKind::Custom {
module,
name: struct_name,
} => {
let qualified = FullyQualified::new(module, &struct_name);
let struct_info = self
.struct_info(&qualified)
.ok_or(self.error(ErrorKind::UnexpectedError("struct not found"), span))?
.clone();
let mut offset = 0;
for (_field_name, field_typ) in &struct_info.fields {
let len = self.size_of(field_typ);
let range = offset..(offset + len);
self.constrain_inputs_to_main(&input[range], field_typ, span)?;
offset += len;
}
}
TyKind::Field { constant: true } => unreachable!(),
TyKind::GenericSizedArray(_, _) => {
unreachable!("generic array should have been resolved")
}
};
Ok(())
}
/// Compile a function. Used to compile `main()` only for now
pub(crate) fn compile_main_function(
&mut self,
fn_env: &mut FnEnv<B::Field, B::Var>,
function: &FunctionDef,
) -> Result<Option<Vec<B::Var>>> {
assert!(function.is_main());
// compile the block
let returned = self.compile_block(fn_env, &function.body)?;
// we're expecting something returned?
match (function.sig.return_type.as_ref(), returned) {
(None, None) => Ok(None),
(Some(expected), None) => Err(self.error(ErrorKind::MissingReturn, expected.span)),
(None, Some(returned)) => Err(self.error(ErrorKind::UnexpectedReturn, returned.span)),
(Some(_expected), Some(returned)) => {
// make sure there are no constants in the returned value
let mut returned_cells = vec![];
for r in &returned.cvars {
match r {
ConstOrCell::Cell(c) => returned_cells.push(c.clone()),
ConstOrCell::Const(_) => {
Err(self.error(ErrorKind::ConstantInOutput, returned.span))?
}
}
}
self.public_output
.as_ref()
.expect("bug in the compiler: missing public output");
Ok(Some(returned_cells))
}
}
}
fn compute_expr(
&mut self,
fn_env: &mut FnEnv<B::Field, B::Var>,
expr: &Expr,
) -> Result<Option<VarOrRef<B>>> {
match &expr.kind {
// `module::fn_name(args)`
ExprKind::FnCall {
module,
fn_name,
args,
..
} => {
// sanity check
if fn_name.value == "main" {
Err(self.error(ErrorKind::RecursiveMain, expr.span))?
}
// retrieve the function in the env
let qualified = FullyQualified::new(module, &fn_name.value);
let fn_info = self
.fn_info(&qualified)
.ok_or_else(|| {
self.error(
ErrorKind::UndefinedFunction(fn_name.value.clone()),
fn_name.span,
)
})?
.clone();
// compute the arguments
// module::fn_name(args)
// ^^^^
let mut vars = Vec::with_capacity(args.len());
for arg in args {
// get the variable behind the expression
let var = self
.compute_expr(fn_env, arg)?
.ok_or_else(|| self.error(ErrorKind::CannotComputeExpression, arg.span))?;
// we pass variables by values always
let var = var.value(self, fn_env);
let typ = self.expr_type(arg).cloned();
let mutable = false; // TODO: mut keyword in arguments?
let var_info = VarInfo::new(var, mutable, typ);
vars.push(var_info);
}
match &fn_info.kind {
// assert() <-- for example
FnKind::BuiltIn(sig, handle) => {
let res = handle(self, &sig.generics, &vars, expr.span);
res.map(|r| r.map(VarOrRef::Var))
}
// fn_name(args)
// ^^^^^^^
FnKind::Native(func) => {
// module::fn_name(args)
// ^^^^^^
if func.is_hint {
self.ir_writer
.compile_hint_function_call(func, vars)
.map(|r| {
let cvars: Vec<_> = r
.into_iter()
.map(|r| {
ConstOrCell::Cell(
self.backend.new_internal_var(r, expr.span),
)
})
.collect();
if cvars.is_empty() {
return None;
}
Some(VarOrRef::Var(Var::new(cvars, expr.span)))
})
} else {
self.compile_native_function_call(func, vars)
.map(|r| r.map(VarOrRef::Var))
}
}
}
}
ExprKind::FieldAccess { lhs, rhs } => {
// get var behind lhs
let lhs_var = self
.compute_expr(fn_env, lhs)?
.ok_or_else(|| self.error(ErrorKind::CannotComputeExpression, lhs.span))?;
// get struct info behind lhs
let lhs_struct = self
.expr_type(lhs)
.ok_or_else(|| self.error(ErrorKind::CannotComputeExpression, lhs.span))?;
let (module, self_struct) = match lhs_struct {
TyKind::Custom { module, name } => (module, name),
_ => Err(Error::new(
"compute-expr",
ErrorKind::UnexpectedError(
"could not figure out struct implementing that method call",
),
lhs.span,
))?,
};
let qualified = FullyQualified::new(module, self_struct);
let struct_info = self
.struct_info(&qualified)
.expect("struct info not found for custom struct");
// find range of field
let mut start = 0;
let mut len = 0;
for (field, field_typ) in &struct_info.fields {
if field == &rhs.value {
len = self.size_of(field_typ);
break;
}
start += self.size_of(field_typ);
}
// narrow the variable to the given range
let var = lhs_var.narrow(start, len);
Ok(Some(var))
}
// `Thing.method(args)` or `thing.method(args)`
ExprKind::MethodCall {
lhs,
method_name,
args,
} => {
// figure out the name of the custom struct
let lhs_typ = self.expr_type(lhs).expect("method call on what?").clone();
let (module, struct_name) = match &lhs_typ {
TyKind::Custom { module, name } => (module, name),
_ => {
return Err(self.error(
ErrorKind::UnexpectedError("method call only work on custom types"),
lhs.span,
))
}
};
// get var of `self`
// (might be `None` if it's a static method call)
let self_var = self.compute_expr(fn_env, lhs)?;
// find method info
let qualified = FullyQualified::new(module, struct_name);
let struct_info = self
.struct_info(&qualified)
.ok_or(self.error(
ErrorKind::UnexpectedError("struct not found"),
method_name.span,
))?
.clone();
let func = struct_info
.methods
.get(&method_name.value)
.expect("could not find method");
// if method has a `self` argument, manually add it to the list of argument
let mut vars = vec![];
if let Some(first_arg) = func.sig.arguments.first() {
if first_arg.name.value == "self" {
let self_var = self_var.ok_or_else(|| {
self.error(ErrorKind::NotAStaticMethod, method_name.span)
})?;
// TODO: for now we pass `self` by value as well
let mutable = false;
let self_var = self_var.value(self, fn_env);
let self_var_info = VarInfo::new(self_var, mutable, Some(lhs_typ.clone()));
vars.insert(0, self_var_info);
}
} else {
assert!(self_var.is_none());
}
// compute the arguments
for arg in args {
let var = self
.compute_expr(fn_env, arg)?
.ok_or_else(|| self.error(ErrorKind::CannotComputeExpression, arg.span))?;
// TODO: for now we pass `self` by value as well
let mutable = false;
let var = var.value(self, fn_env);
let typ = self.expr_type(arg).cloned();
let var_info = VarInfo::new(var, mutable, typ);
vars.push(var_info);
}
// execute method
self.compile_native_function_call(func, vars)
.map(|r| r.map(VarOrRef::Var))
}
ExprKind::IfElse { cond, then_, else_ } => {
let cond = self
.compute_expr(fn_env, cond)?
.unwrap()
.value(self, fn_env);
let then_ = self
.compute_expr(fn_env, then_)?
.unwrap()
.value(self, fn_env);
let else_ = self
.compute_expr(fn_env, else_)?
.unwrap()
.value(self, fn_env);
let res = field::if_else(self, &cond, &then_, &else_, expr.span);
Ok(Some(VarOrRef::Var(res)))
}
ExprKind::Assignment { lhs, rhs } => {
// figure out the local var of lhs
let lhs = self.compute_expr(fn_env, lhs)?.unwrap();
// figure out the var of what's on the right
let rhs = self.compute_expr(fn_env, rhs)?.unwrap();
let rhs_var = match rhs {
VarOrRef::Var(var) => var,
VarOrRef::Ref {
var_name,
start,
len,
} => {
let var_info = self.get_local_var(fn_env, &var_name);
let cvars = var_info.var.range(start, len).to_vec();
Var::new(cvars, var_info.var.span)
}
};
// replace the left with the right
match lhs {
VarOrRef::Var(_) => Err(Error::new(
"compute-expr",
ErrorKind::UnexpectedError("can't reassign this non-mutable variable"),
expr.span,
))?,
VarOrRef::Ref {
var_name,
start,
len,
} => {
fn_env.reassign_var_range(&var_name, rhs_var, start, len);
}
}
Ok(None)
}
ExprKind::BinaryOp { op, lhs, rhs, .. } => {
let lhs = self.compute_expr(fn_env, lhs)?.unwrap();
let rhs = self.compute_expr(fn_env, rhs)?.unwrap();
let lhs = lhs.value(self, fn_env);
let rhs = rhs.value(self, fn_env);
let res = match op {
Op2::Addition => field::add(self, &lhs[0], &rhs[0], expr.span),
Op2::Subtraction => field::sub(self, &lhs[0], &rhs[0], expr.span),
Op2::Multiplication => field::mul(self, &lhs[0], &rhs[0], expr.span),
Op2::Equality => field::equal(self, &lhs, &rhs, expr.span),
Op2::Inequality => field::not_equal(self, &lhs, &rhs, expr.span),
Op2::BoolAnd => boolean::and(self, &lhs[0], &rhs[0], expr.span),
Op2::BoolOr => boolean::or(self, &lhs[0], &rhs[0], expr.span),
Op2::Division => todo!(),
};
Ok(Some(VarOrRef::Var(res)))
}
ExprKind::Negated(b) => {
let var = self.compute_expr(fn_env, b)?.unwrap();
let var = var.value(self, fn_env);
todo!()
}
ExprKind::Not(b) => {
let var = self.compute_expr(fn_env, b)?.unwrap();
let var = var.value(self, fn_env);
let res = boolean::not(self, &var[0], expr.span.merge_with(b.span));
Ok(Some(VarOrRef::Var(res)))
}
ExprKind::BigUInt(b) => {
let ff = B::Field::try_from(b.to_owned()).map_err(|_| {
self.error(ErrorKind::CannotConvertToField(b.to_string()), expr.span)
})?;
let res = VarOrRef::Var(Var::new_constant(ff, expr.span));
Ok(Some(res))
}
ExprKind::Bool(b) => {
let value = if *b {
B::Field::one()
} else {
B::Field::zero()
};
let res = VarOrRef::Var(Var::new_constant(value, expr.span));
Ok(Some(res))
}
ExprKind::Variable { module, name } => {
// if it's a type we return nothing
// (most likely what follows is a static method call)
if is_type(&name.value) {
return Ok(None);
}
// search for constants first
let qualified = FullyQualified::new(module, &name.value);
let var_info = if let Some(cst_info) = self.const_info(&qualified) {
let var = Var::new_constant_typ(cst_info, name.span);
VarInfo::new(var, false, Some(cst_info.typ.kind.clone()))
} else {
// if no constant found, look in the function's scope
// remember: we can do this because the type checker already checked that we didn't shadow constant vars
self.get_local_var(fn_env, &name.value)
};
let res = VarOrRef::from_var_info(name.value.clone(), var_info);
Ok(Some(res))
}
ExprKind::ArrayAccess { array, idx } => {
// retrieve var of array
let var = self
.compute_expr(fn_env, array)?
.expect("array access on non-array");
// compute the index
let idx_var = self
.compute_expr(fn_env, idx)?
.ok_or_else(|| self.error(ErrorKind::CannotComputeExpression, expr.span))?;
let idx = idx_var
.constant()
.ok_or_else(|| self.error(ErrorKind::ExpectedConstant, expr.span))?;
let idx: BigUint = idx.into();
let idx: usize = idx.try_into().unwrap();
// retrieve the type of the elements in the array
let array_typ = self.expr_type(array).expect("cannot find type of array");
let elem_type = match array_typ {
TyKind::Array(ty, array_len) => {
if idx >= (*array_len as usize) {
return Err(self.error(
ErrorKind::ArrayIndexOutOfBounds(idx, *array_len as usize - 1),
expr.span,
));
}
ty
}
_ => Err(Error::new(
"compute-expr",
ErrorKind::UnexpectedError("expected array"),
expr.span,
))?,
};
// compute the size of each element in the array
let len = self.size_of(elem_type);
// compute the real index
let start = idx * len;
// out-of-bound checks
if start >= var.len() || start + len > var.len() {
return Err(self.error(
ErrorKind::ArrayIndexOutOfBounds(start, var.len()),
expr.span,
));
}
// index into the var
let var = var.narrow(start, len);
//
Ok(Some(var))
}
ExprKind::ArrayDeclaration(items) => {
let mut cvars = vec![];
for item in items {
let var = self.compute_expr(fn_env, item)?.unwrap();
let to_extend = var.value(self, fn_env).cvars.clone();
cvars.extend(to_extend);
}
let var = VarOrRef::Var(Var::new(cvars, expr.span));
Ok(Some(var))
}
ExprKind::CustomTypeDeclaration { custom: _, fields } => {
// create the struct by just concatenating all of its cvars
let mut cvars = vec![];
for (_field, rhs) in fields {
let var = self.compute_expr(fn_env, rhs)?.unwrap();
let to_extend = var.value(self, fn_env).cvars.clone();
cvars.extend(to_extend);
}
let var = VarOrRef::Var(Var::new(cvars, expr.span));
//
Ok(Some(var))
}
ExprKind::RepeatedArrayInit { item, size } => {
let size = self
.compute_expr(fn_env, size)?
.ok_or_else(|| self.error(ErrorKind::CannotComputeExpression, expr.span))?;
let size = size
.constant()
.ok_or_else(|| self.error(ErrorKind::ExpectedConstant, expr.span))?;
let size: BigUint = size.into();
let size: usize = size.try_into().unwrap();
let mut cvars = vec![];
for _ in 0..size {
let var = self.compute_expr(fn_env, item)?.unwrap();
let to_extend = var.value(self, fn_env).cvars.clone();
cvars.extend(to_extend);
}
let var = VarOrRef::Var(Var::new(cvars, expr.span));
Ok(Some(var))
}
}
}
pub fn add_public_inputs(
&mut self,
name: String,
num: usize,
span: Span,
) -> Var<B::Field, B::Var> {
let mut cvars = Vec::with_capacity(num);
for idx in 0..num {
let cvar = self
.backend
.add_public_input(Value::External(name.clone(), idx), span);
cvars.push(ConstOrCell::Cell(cvar));
}
Var::new(cvars, span)
}
pub fn add_public_outputs(&mut self, num: usize, span: Span) {
assert!(self.public_output.is_none());
let mut cvars = Vec::with_capacity(num);
for _ in 0..num {
let cvar = self
.backend
.add_public_output(Value::PublicOutput(None), span);
cvars.push(ConstOrCell::Cell(cvar));
}
// store it
let res = Var::new(cvars, span);
self.public_output = Some(res);
}
pub fn add_private_inputs(
&mut self,
name: String,
num: usize,
span: Span,
) -> Var<B::Field, B::Var> {
let mut cvars = Vec::with_capacity(num);
for idx in 0..num {
// create the var
let cvar = self
.backend
.add_private_input(Value::External(name.clone(), idx), span);
cvars.push(ConstOrCell::Cell(cvar));
}
Var::new(cvars, span)
}
}
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
pub(crate) struct PendingGate {
pub label: &'static str,
#[serde(skip)]
pub coeffs: Vec<VestaField>,
pub vars: Vec<Option<crate::backends::kimchi::KimchiCellVar>>,
pub span: Span,
}