-
Notifications
You must be signed in to change notification settings - Fork 189
/
lib.rs
513 lines (460 loc) · 18.6 KB
/
lib.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
#![forbid(unsafe_code)]
#![warn(unused_crate_dependencies, unused_extern_crates)]
#![warn(unreachable_pub)]
#![warn(clippy::semicolon_if_nothing_returned)]
use acvm::{
acir::{
circuit::ErrorSelector,
native_types::{Witness, WitnessMap},
},
AcirField, FieldElement,
};
use errors::AbiError;
use input_parser::InputValue;
use iter_extended::{try_btree_map, try_vecmap, vecmap};
use noirc_printable_type::{
decode_value as printable_type_decode_value, PrintableType, PrintableValue,
PrintableValueDisplay,
};
use serde::{Deserialize, Serialize};
use std::borrow::Borrow;
use std::{collections::BTreeMap, str};
// This is the ABI used to bridge the different TOML formats for the initial
// witness, the partial witness generator and the interpreter.
//
// This ABI has nothing to do with ACVM or ACIR. Although they implicitly have a relationship
#[cfg(test)]
mod arbitrary;
pub mod errors;
pub mod input_parser;
mod serialization;
/// A map from the fields in an TOML/JSON file which correspond to some ABI to their values
pub type InputMap = BTreeMap<String, InputValue>;
pub const MAIN_RETURN_NAME: &str = "return";
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
/// Types that are allowed in the (main function in binary)
///
/// we use this separation so that we can have types like Strings
/// without needing to introduce this in the Noir types
///
/// NOTE: If Strings are introduced as a native type, the translation will
/// be straightforward. Whether exotic types like String will be natively supported
/// depends on the types of programs that users want to do. I don't envision string manipulation
/// in programs, however it is possible to support, with many complications like encoding character set
/// support.
pub enum AbiType {
Field,
Array {
length: u32,
#[serde(rename = "type")]
typ: Box<AbiType>,
},
Integer {
sign: Sign,
width: u32,
},
Boolean,
Struct {
path: String,
#[serde(
serialize_with = "serialization::serialize_struct_fields",
deserialize_with = "serialization::deserialize_struct_fields"
)]
fields: Vec<(String, AbiType)>,
},
Tuple {
fields: Vec<AbiType>,
},
String {
length: u32,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
#[serde(rename_all = "lowercase")]
/// Represents whether the parameter is public or known only to the prover.
pub enum AbiVisibility {
Public,
// Constants are not allowed in the ABI for main at the moment.
// Constant,
Private,
DataBus,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
#[serde(rename_all = "lowercase")]
pub enum Sign {
Unsigned,
Signed,
}
impl AbiType {
/// Returns the number of field elements required to represent the type once encoded.
pub fn field_count(&self) -> u32 {
match self {
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean => 1,
AbiType::Array { length, typ } => typ.field_count() * *length,
AbiType::Struct { fields, .. } => {
fields.iter().fold(0, |acc, (_, field_type)| acc + field_type.field_count())
}
AbiType::Tuple { fields } => {
fields.iter().fold(0, |acc, field_typ| acc + field_typ.field_count())
}
AbiType::String { length } => *length,
}
}
}
impl From<&AbiType> for PrintableType {
fn from(value: &AbiType) -> Self {
match value {
AbiType::Field => PrintableType::Field,
AbiType::String { length } => PrintableType::String { length: *length },
AbiType::Tuple { fields } => {
let fields = fields.iter().map(|field| field.into()).collect();
PrintableType::Tuple { types: fields }
}
AbiType::Array { length, typ } => {
let borrowed: &AbiType = typ.borrow();
PrintableType::Array { length: *length, typ: Box::new(borrowed.into()) }
}
AbiType::Boolean => PrintableType::Boolean,
AbiType::Struct { path, fields } => {
let fields =
fields.iter().map(|(name, field)| (name.clone(), field.into())).collect();
PrintableType::Struct {
name: path.split("::").last().unwrap_or_default().to_string(),
fields,
}
}
AbiType::Integer { sign: Sign::Unsigned, width } => {
PrintableType::UnsignedInteger { width: *width }
}
AbiType::Integer { sign: Sign::Signed, width } => {
PrintableType::SignedInteger { width: *width }
}
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
/// An argument or return value of the circuit's `main` function.
pub struct AbiParameter {
pub name: String,
#[serde(rename = "type")]
#[cfg_attr(test, proptest(strategy = "arbitrary::arb_abi_type()"))]
pub typ: AbiType,
pub visibility: AbiVisibility,
}
impl AbiParameter {
pub fn is_public(&self) -> bool {
self.visibility == AbiVisibility::Public
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
pub struct AbiReturnType {
#[cfg_attr(test, proptest(strategy = "arbitrary::arb_abi_type()"))]
pub abi_type: AbiType,
pub visibility: AbiVisibility,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
pub struct Abi {
/// An ordered list of the arguments to the program's `main` function, specifying their types and visibility.
pub parameters: Vec<AbiParameter>,
pub return_type: Option<AbiReturnType>,
#[cfg_attr(test, proptest(strategy = "proptest::prelude::Just(BTreeMap::from([]))"))]
pub error_types: BTreeMap<ErrorSelector, AbiErrorType>,
}
impl Abi {
pub fn parameter_names(&self) -> Vec<&String> {
self.parameters.iter().map(|x| &x.name).collect()
}
pub fn num_parameters(&self) -> usize {
self.parameters.len()
}
/// Returns the number of field elements required to represent the ABI's input once encoded.
pub fn field_count(&self) -> u32 {
self.parameters.iter().map(|param| param.typ.field_count()).sum()
}
/// Returns whether any values are needed to be made public for verification.
pub fn has_public_inputs(&self) -> bool {
let has_public_args = self.parameters.iter().any(|param| param.is_public());
let has_public_return = self
.return_type
.as_ref()
.map_or(false, |typ| matches!(typ.visibility, AbiVisibility::Public));
has_public_args || has_public_return
}
/// Returns `true` if the ABI contains no parameters or return value.
pub fn is_empty(&self) -> bool {
self.return_type.is_none() && self.parameters.is_empty()
}
pub fn to_btree_map(&self) -> BTreeMap<String, AbiType> {
let mut map = BTreeMap::new();
for param in self.parameters.iter() {
map.insert(param.name.clone(), param.typ.clone());
}
map
}
/// Encode a set of inputs as described in the ABI into a `WitnessMap`.
pub fn encode(
&self,
input_map: &InputMap,
return_value: Option<InputValue>,
) -> Result<WitnessMap<FieldElement>, AbiError> {
// Check that no extra witness values have been provided.
let param_names = self.parameter_names();
if param_names.len() < input_map.len() {
let unexpected_params: Vec<String> =
input_map.keys().filter(|param| !param_names.contains(param)).cloned().collect();
return Err(AbiError::UnexpectedParams(unexpected_params));
}
// First encode each input separately, performing any input validation.
let mut encoded_inputs: Vec<Vec<FieldElement>> = self
.parameters
.iter()
.map(|param| {
let value = input_map
.get(¶m.name)
.ok_or_else(|| AbiError::MissingParam(param.name.clone()))?
.clone();
value.find_type_mismatch(¶m.typ, param.name.clone())?;
Self::encode_value(value, ¶m.typ)
})
.collect::<Result<_, _>>()?;
// When encoding public inputs to be passed to the verifier, the user can must provide a return value
// to be inserted into the witness map. This is not needed when generating a witness when proving the circuit.
match (&self.return_type, return_value) {
(Some(AbiReturnType { abi_type: return_type, .. }), Some(return_value)) => {
if !return_value.matches_abi(return_type) {
return Err(AbiError::ReturnTypeMismatch {
return_type: return_type.clone(),
value: return_value,
});
}
let encoded_return_fields = Self::encode_value(return_value, return_type)?;
encoded_inputs.push(encoded_return_fields);
}
(None, Some(return_value)) => {
return Err(AbiError::UnexpectedReturnValue(return_value))
}
// We allow not passing a return value despite the circuit defining one
// in order to generate the initial partial witness.
(_, None) => {}
}
// Write input field elements into witness map.
let witness_map: BTreeMap<Witness, FieldElement> = encoded_inputs
.into_iter()
.flatten()
.enumerate()
.map(|(index, field_element)| (Witness(index as u32), field_element))
.collect::<BTreeMap<Witness, FieldElement>>();
Ok(witness_map.into())
}
fn encode_value(value: InputValue, abi_type: &AbiType) -> Result<Vec<FieldElement>, AbiError> {
let mut encoded_value = Vec::new();
match (value, abi_type) {
(InputValue::Field(elem), _) => encoded_value.push(elem),
(InputValue::Vec(vec_elements), AbiType::Array { typ, .. }) => {
for elem in vec_elements {
encoded_value.extend(Self::encode_value(elem, typ)?);
}
}
(InputValue::String(string), _) => {
let str_as_fields =
string.bytes().map(|byte| FieldElement::from_be_bytes_reduce(&[byte]));
encoded_value.extend(str_as_fields);
}
(InputValue::Struct(object), AbiType::Struct { fields, .. }) => {
for (field, typ) in fields {
encoded_value.extend(Self::encode_value(object[field].clone(), typ)?);
}
}
(InputValue::Vec(vec_elements), AbiType::Tuple { fields }) => {
for (value, typ) in vec_elements.into_iter().zip(fields) {
encoded_value.extend(Self::encode_value(value, typ)?);
}
}
_ => unreachable!("value should have already been checked to match abi type"),
}
Ok(encoded_value)
}
/// Decode a `WitnessMap` into the types specified in the ABI.
pub fn decode(
&self,
witness_map: &WitnessMap<FieldElement>,
) -> Result<(InputMap, Option<InputValue>), AbiError> {
let mut pointer: u32 = 0;
let public_inputs_map =
try_btree_map(self.parameters.clone(), |AbiParameter { name, typ, .. }| {
let num_fields = typ.field_count();
let param_witness_values = try_vecmap(0..num_fields, |index| {
let witness_index = Witness(pointer + index);
witness_map
.get(&witness_index)
.ok_or_else(|| AbiError::MissingParamWitnessValue {
name: name.clone(),
witness_index,
})
.copied()
})?;
pointer += num_fields;
decode_value(&mut param_witness_values.into_iter(), &typ)
.map(|input_value| (name.clone(), input_value))
})?;
// We also attempt to decode the circuit's return value from `witness_map`.
let return_value = if let Some(return_type) = &self.return_type {
if let Ok(return_witness_values) =
try_vecmap(0..return_type.abi_type.field_count(), |index| {
let witness_index = Witness(pointer + index);
witness_map
.get(&witness_index)
.ok_or_else(|| AbiError::MissingParamWitnessValue {
name: MAIN_RETURN_NAME.to_string(),
witness_index,
})
.copied()
})
{
Some(decode_value(&mut return_witness_values.into_iter(), &return_type.abi_type)?)
} else {
// Unlike for the circuit inputs, we tolerate not being able to find the witness values for the return value.
// This is because the user may be decoding a partial witness map for which is hasn't been calculated yet.
// If a return value is expected, this should be checked for by the user.
None
}
} else {
None
};
Ok((public_inputs_map, return_value))
}
}
pub fn decode_value(
field_iterator: &mut impl Iterator<Item = FieldElement>,
value_type: &AbiType,
) -> Result<InputValue, AbiError> {
// This function assumes that `field_iterator` contains enough `FieldElement`s in order to decode a `value_type`
// `Abi.decode` enforces that the encoded inputs matches the expected length defined by the ABI so this is safe.
let value = match value_type {
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean => {
let field_element = field_iterator.next().unwrap();
InputValue::Field(field_element)
}
AbiType::Array { length, typ } => {
let length = *length as usize;
let mut array_elements = Vec::with_capacity(length);
for _ in 0..length {
array_elements.push(decode_value(field_iterator, typ)?);
}
InputValue::Vec(array_elements)
}
AbiType::String { length } => {
let field_elements: Vec<FieldElement> = field_iterator.take(*length as usize).collect();
InputValue::String(decode_string_value(&field_elements))
}
AbiType::Struct { fields, .. } => {
let mut struct_map = BTreeMap::new();
for (field_key, param_type) in fields {
let field_value = decode_value(field_iterator, param_type)?;
struct_map.insert(field_key.to_owned(), field_value);
}
InputValue::Struct(struct_map)
}
AbiType::Tuple { fields } => {
let mut tuple_elements = Vec::with_capacity(fields.len());
for field_typ in fields {
tuple_elements.push(decode_value(field_iterator, field_typ)?);
}
InputValue::Vec(tuple_elements)
}
};
Ok(value)
}
fn decode_string_value(field_elements: &[FieldElement]) -> String {
let string_as_slice = vecmap(field_elements, |e| {
let mut field_as_bytes = e.to_be_bytes();
let char_byte = field_as_bytes.pop().unwrap(); // A character in a string is represented by a u8, thus we just want the last byte of the element
assert!(field_as_bytes.into_iter().all(|b| b == 0)); // Assert that the rest of the field element's bytes are empty
char_byte
});
let final_string = str::from_utf8(&string_as_slice).unwrap();
final_string.to_owned()
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum AbiValue {
Field {
value: FieldElement,
},
Integer {
sign: bool,
value: String,
},
Boolean {
value: bool,
},
String {
value: String,
},
Array {
value: Vec<AbiValue>,
},
Struct {
#[serde(
serialize_with = "serialization::serialize_struct_field_values",
deserialize_with = "serialization::deserialize_struct_field_values"
)]
fields: Vec<(String, AbiValue)>,
},
Tuple {
fields: Vec<AbiValue>,
},
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "error_kind", rename_all = "lowercase")]
pub enum AbiErrorType {
FmtString { length: u32, item_types: Vec<AbiType> },
Custom(AbiType),
}
pub fn display_abi_error<F: AcirField>(
fields: &[F],
error_type: AbiErrorType,
) -> PrintableValueDisplay<F> {
match error_type {
AbiErrorType::FmtString { length, item_types } => {
let mut fields_iter = fields.iter().copied();
let PrintableValue::String(string) =
printable_type_decode_value(&mut fields_iter, &PrintableType::String { length })
else {
unreachable!("Got non-string from string decoding");
};
let _length_of_items = fields_iter.next();
let items = item_types.into_iter().map(|abi_type| {
let printable_typ = (&abi_type).into();
let decoded = printable_type_decode_value(&mut fields_iter, &printable_typ);
(decoded, printable_typ)
});
PrintableValueDisplay::FmtString(string, items.collect())
}
AbiErrorType::Custom(abi_typ) => {
let printable_type = (&abi_typ).into();
let decoded = printable_type_decode_value(&mut fields.iter().copied(), &printable_type);
PrintableValueDisplay::Plain(decoded, printable_type)
}
}
}
#[cfg(test)]
mod test {
use proptest::prelude::*;
use crate::arbitrary::arb_abi_and_input_map;
proptest! {
#[test]
fn encoding_and_decoding_returns_original_witness_map((abi, input_map) in arb_abi_and_input_map()) {
let witness_map = abi.encode(&input_map, None).unwrap();
let (decoded_inputs, return_value) = abi.decode(&witness_map).unwrap();
prop_assert_eq!(decoded_inputs, input_map);
prop_assert_eq!(return_value, None);
}
}
}