Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:Zokrates/ZoKrates into rc/0.7.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Schaeff committed Aug 17, 2021
2 parents 9f469e5 + a31f1cd commit b69cfef
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions zokrates_core/src/static_analysis/uint_optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::embed::FlatEmbed;
use crate::zir::folder::*;
use crate::zir::*;
use std::collections::HashMap;
use std::ops::{BitAnd, Shl, Shr};
use zokrates_field::Field;

#[derive(Default)]
Expand Down Expand Up @@ -366,35 +367,25 @@ impl<'ast, T: Field> Folder<'ast, T> for UintOptimizer<'ast, T> {
// reduce both terms
let e = self.fold_uint_expression(e);

let e_max: u128 = e
.metadata
.clone()
.unwrap()
.max
.to_dec_string()
.parse()
.unwrap();
let e_max: num_bigint::BigUint = e.metadata.as_ref().unwrap().max.to_biguint();
let max = e_max
.shl(by as usize)
.bitand(&(2_u128.pow(range as u32) - 1).into());

let max = T::from((e_max << by) & (2_u128.pow(range as u32) - 1));
let max = T::try_from(max).unwrap();

UExpression::left_shift(force_reduce(e), by).with_max(max)
}
RightShift(box e, by) => {
// reduce both terms
let e = self.fold_uint_expression(e);

let e_max: u128 = e
.metadata
.clone()
.unwrap()
.max
.to_dec_string()
.parse()
.unwrap();
let e_max: num_bigint::BigUint = e.metadata.as_ref().unwrap().max.to_biguint();
let max = e_max
.bitand(&(2_u128.pow(range as u32) - 1).into())
.shr(by as usize);

let max = (e_max & (2_u128.pow(range as u32) - 1)) >> by;

let max = T::from(max);
let max = T::try_from(max).unwrap();

UExpression::right_shift(force_reduce(e), by).with_max(max)
}
Expand Down Expand Up @@ -727,7 +718,7 @@ mod tests {

#[test]
fn right_shift() {
fn right_shift_test(e_max: u128, by: u32, output_max: u32) {
fn right_shift_test<U: Into<Bn128Field>>(e_max: U, by: u32, output_max: u32) {
let left = e_with_max(e_max);

let right = by;
Expand All @@ -745,12 +736,12 @@ mod tests {

right_shift_test(0xff_u128, 2, 0xff >> 2);
right_shift_test(2, 2, 2 >> 2);
right_shift_test(0xffffffffffff_u128, 2, 0xffffffff >> 2);
right_shift_test(Bn128Field::max_unique_value(), 2, 0xffffffff >> 2);
}

#[test]
fn left_shift() {
fn left_shift_test(e_max: u128, by: u32, output_max: u32) {
fn left_shift_test<U: Into<Bn128Field>>(e_max: U, by: u32, output_max: u32) {
let left = e_with_max(e_max);

let right = by;
Expand All @@ -768,7 +759,7 @@ mod tests {

left_shift_test(0xff_u128, 2, 0xff << 2);
left_shift_test(2, 2, 2 << 2);
left_shift_test(0xffffffffffff_u128, 2, 0xffffffff << 2);
left_shift_test(Bn128Field::max_unique_value(), 2, 0xffffffff << 2);
}

#[test]
Expand Down

0 comments on commit b69cfef

Please sign in to comment.