1
0
Fork 0
mirror of synced 2025-09-23 12:18:44 +00:00

update docs, implement agreed change ie only print compact for in zir

This commit is contained in:
schaeff 2019-01-30 18:33:55 +01:00
parent 0001ea6670
commit c55fdf98b8
3 changed files with 33 additions and 11 deletions

View file

@ -0,0 +1,17 @@
# ZIR
ZIR is the intermediate representation ZoKrates uses to represent programs. It is close to R1CS but still encapsulates witness generation.
**Note that ZIR is still in development and can change without notice.**
When generating R1CS constraints, very large numbers are often used, which can make reading ZIR hard for humans.
To mitigate this, ZIR uses an abuse of language (or an isomorphism) when displaying field elements: they are shown as members of the interval `[-(p-1)/2, (p-1)/2]`.
Therefore, instead of writing p - 1 as:
```
21888242871839275222246405745257275088548364400416034343698204186575808495616
```
... in ZIR, we simply write:
```
-1
```

View file

@ -57,7 +57,7 @@ impl<T: Field> fmt::Display for LinComb<T> {
"{}",
self.0
.iter()
.map(|(k, v)| format!("{} * {}", v, k))
.map(|(k, v)| format!("{} * {}", v.to_compact_dec_string(), k))
.collect::<Vec<_>>()
.join(" + ")
)

View file

@ -71,6 +71,9 @@ pub trait Field:
fn get_required_bits() -> usize;
/// Tries to parse a string into this representation
fn try_from_str<'a>(s: &'a str) -> Result<Self, ()>;
/// Returns a decimal string representing a the member of the equivalence class of this `Field` in Z/pZ
/// which lies in [-(p-1)/2, (p-1)/2]
fn to_compact_dec_string(&self) -> String;
}
#[derive(PartialEq, PartialOrd, Clone, Eq, Ord, Hash, Serialize, Deserialize)]
@ -129,6 +132,17 @@ impl Field for FieldPrime {
value: &x - x.div_floor(&*P) * &*P,
})
}
fn to_compact_dec_string(&self) -> String {
// values up to (p-1)/2 included are represented as positive, values between (p+1)/2 and p-1 as represented as negative by subtracting p
if self.value <= FieldPrime::max_value().value / 2 {
format!("{}", self.value.to_str_radix(10))
} else {
format!(
"({})",
(&self.value - (FieldPrime::max_value().value + BigInt::one())).to_str_radix(10)
)
}
}
}
impl Default for FieldPrime {
@ -141,16 +155,7 @@ impl Default for FieldPrime {
impl Display for FieldPrime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// values up to (p-1)/2 included are represented as positive, values between (p+1)/2 and p-1 as represented as negative by subtracting p
if self.value <= FieldPrime::max_value().value / 2 {
write!(f, "{}", self.value.to_str_radix(10))
} else {
write!(
f,
"({})",
(&self.value - (FieldPrime::max_value().value + BigInt::one())).to_str_radix(10)
)
}
write!(f, "{}", self.value.to_str_radix(10))
}
}