suggestions, update book
This commit is contained in:
parent
350430f66c
commit
b55ae6fdca
3 changed files with 27 additions and 11 deletions
|
@ -943,14 +943,18 @@ impl<S> GType<S> {
|
|||
}
|
||||
|
||||
impl<S: PartialEq> GType<S> {
|
||||
pub fn is_composite_of(&self, ty: &Self) -> bool {
|
||||
pub fn is_composite_of_field(&self) -> bool {
|
||||
match self {
|
||||
GType::Array(array_type) => array_type.ty.is_composite_of(ty),
|
||||
GType::Tuple(tuple_typle) => tuple_typle.elements.iter().all(|e| e.is_composite_of(ty)),
|
||||
GType::Struct(struct_type) => {
|
||||
struct_type.members.iter().all(|m| m.ty.is_composite_of(ty))
|
||||
}
|
||||
other => other.eq(ty),
|
||||
GType::Array(array_type) => array_type.ty.is_composite_of_field(),
|
||||
GType::Tuple(tuple_typle) => tuple_typle
|
||||
.elements
|
||||
.iter()
|
||||
.all(|e| e.is_composite_of_field()),
|
||||
GType::Struct(struct_type) => struct_type
|
||||
.members
|
||||
.iter()
|
||||
.all(|m| m.ty.is_composite_of_field()),
|
||||
other => other.eq(&Self::FieldElement),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,9 +17,19 @@ Assigning a value, in general, should be combined with adding a constraint:
|
|||
{{#include ../../../zokrates_cli/examples/book/assembly/division.zok}}
|
||||
```
|
||||
|
||||
> The operator `<--` can be sometimes misused, as this operator does not generate any constraints, resulting in unconstrained variables in the constraint system.
|
||||
> Note that operator `<--` is used for unconstrained assignment and can be easily misused. This operator does not generate constraints, which could result in unconstrained variables in the constraint system.
|
||||
|
||||
In some cases we can combine the witness assignment and constraint generation with the `<==` operator:
|
||||
Unconstrained assignment `<--` allows assignment to variables with complex types. The type must consist of field elements only (eg. `field[3]`):
|
||||
|
||||
```zok
|
||||
field[3] mut c = [0; 3];
|
||||
asm {
|
||||
c <-- [2, 2, 4];
|
||||
c[0] * c[1] === c[2];
|
||||
}
|
||||
```
|
||||
|
||||
In some cases we can combine the witness assignment and constraint generation with the `<==` operator (constrained assignment):
|
||||
|
||||
```zok
|
||||
asm {
|
||||
|
@ -36,6 +46,8 @@ asm {
|
|||
}
|
||||
```
|
||||
|
||||
In the case of constrained assignment `<==`, both sides of the statement have to be of type `field`.
|
||||
|
||||
A constraint can contain arithmetic expressions that are built using multiplication, addition, and other variables or `field` values. Only quadratic expressions are allowed to be included in constraints. Non-quadratic expressions or usage of other arithmetic operators like division or power are not allowed as constraints, but can be used in the witness assignment expression.
|
||||
|
||||
The following code is not allowed:
|
||||
|
|
|
@ -1812,7 +1812,7 @@ impl<'ast, T: Field> Checker<'ast, T> {
|
|||
let e = FieldElementExpression::try_from_typed(e).map_err(|e| ErrorInner {
|
||||
span: Some(span),
|
||||
message: format!(
|
||||
"Expected right hand side of an assembly assignment to be of type field, found {}",
|
||||
"Expected right hand side of an assembly constrained assignment to be of type field, found {}",
|
||||
e.get_type(),
|
||||
),
|
||||
})?;
|
||||
|
@ -1840,7 +1840,7 @@ impl<'ast, T: Field> Checker<'ast, T> {
|
|||
false => {
|
||||
// we can allow composite types in case of `<--` as no constraints are generated from this type of statement
|
||||
// a composite type should only consist of field types
|
||||
match e.get_type().is_composite_of(&Type::FieldElement) {
|
||||
match e.get_type().is_composite_of_field() {
|
||||
true => {
|
||||
let e = TypedExpression::block(vec![], e);
|
||||
Ok(vec![
|
||||
|
|
Loading…
Reference in a new issue