|
@ -14,7 +14,7 @@ use core::borrow::Borrow; |
|
|
#[derive(Clone, Debug)]
|
|
|
#[derive(Clone, Debug)]
|
|
|
pub struct UInt64 {
|
|
|
pub struct UInt64 {
|
|
|
// Least significant bit_gadget first
|
|
|
// Least significant bit_gadget first
|
|
|
bits: Vec<Boolean>,
|
|
|
|
|
|
|
|
|
bits: Vec<Boolean>,
|
|
|
value: Option<u64>,
|
|
|
value: Option<u64>,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
@ -56,7 +56,7 @@ impl UInt64 { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
v
|
|
|
v
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
None => vec![None; 64],
|
|
|
None => vec![None; 64],
|
|
|
};
|
|
|
};
|
|
|
|
|
|
|
|
@ -95,19 +95,19 @@ impl UInt64 { |
|
|
if b {
|
|
|
if b {
|
|
|
value.as_mut().map(|v| *v |= 1);
|
|
|
value.as_mut().map(|v| *v |= 1);
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
&Boolean::Is(ref b) => match b.get_value() {
|
|
|
&Boolean::Is(ref b) => match b.get_value() {
|
|
|
Some(true) => {
|
|
|
Some(true) => {
|
|
|
value.as_mut().map(|v| *v |= 1);
|
|
|
value.as_mut().map(|v| *v |= 1);
|
|
|
},
|
|
|
|
|
|
Some(false) => {},
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
Some(false) => {}
|
|
|
None => value = None,
|
|
|
None => value = None,
|
|
|
},
|
|
|
},
|
|
|
&Boolean::Not(ref b) => match b.get_value() {
|
|
|
&Boolean::Not(ref b) => match b.get_value() {
|
|
|
Some(false) => {
|
|
|
Some(false) => {
|
|
|
value.as_mut().map(|v| *v |= 1);
|
|
|
value.as_mut().map(|v| *v |= 1);
|
|
|
},
|
|
|
|
|
|
Some(true) => {},
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
Some(true) => {}
|
|
|
None => value = None,
|
|
|
None => value = None,
|
|
|
},
|
|
|
},
|
|
|
}
|
|
|
}
|
|
@ -129,7 +129,7 @@ impl UInt64 { |
|
|
.collect();
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
UInt64 {
|
|
|
UInt64 {
|
|
|
bits: new_bits,
|
|
|
|
|
|
|
|
|
bits: new_bits,
|
|
|
value: self.value.map(|v| v.rotate_right(by as u32)),
|
|
|
value: self.value.map(|v| v.rotate_right(by as u32)),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@ -194,12 +194,12 @@ impl UInt64 { |
|
|
match op.value {
|
|
|
match op.value {
|
|
|
Some(val) => {
|
|
|
Some(val) => {
|
|
|
result_value.as_mut().map(|v| *v += u128::from(val));
|
|
|
result_value.as_mut().map(|v| *v += u128::from(val));
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
None => {
|
|
|
None => {
|
|
|
// If any of our operands have unknown value, we won't
|
|
|
// If any of our operands have unknown value, we won't
|
|
|
// know the value of the result
|
|
|
// know the value of the result
|
|
|
result_value = None;
|
|
|
result_value = None;
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
// Iterate over each bit_gadget of the operand and add the operand to
|
|
|
// Iterate over each bit_gadget of the operand and add the operand to
|
|
@ -212,18 +212,18 @@ impl UInt64 { |
|
|
|
|
|
|
|
|
// Add coeff * bit_gadget
|
|
|
// Add coeff * bit_gadget
|
|
|
lc += (coeff, bit.get_variable());
|
|
|
lc += (coeff, bit.get_variable());
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
Boolean::Not(ref bit) => {
|
|
|
Boolean::Not(ref bit) => {
|
|
|
all_constants = false;
|
|
|
all_constants = false;
|
|
|
|
|
|
|
|
|
// Add coeff * (1 - bit_gadget) = coeff * ONE - coeff * bit_gadget
|
|
|
// Add coeff * (1 - bit_gadget) = coeff * ONE - coeff * bit_gadget
|
|
|
lc = lc + (coeff, CS::one()) - (coeff, bit.get_variable());
|
|
|
lc = lc + (coeff, CS::one()) - (coeff, bit.get_variable());
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
Boolean::Constant(bit) => {
|
|
|
Boolean::Constant(bit) => {
|
|
|
if bit {
|
|
|
if bit {
|
|
|
lc += (coeff, CS::one());
|
|
|
lc += (coeff, CS::one());
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
coeff.double_in_place();
|
|
|
coeff.double_in_place();
|
|
@ -270,7 +270,7 @@ impl UInt64 { |
|
|
result_bits.truncate(64);
|
|
|
result_bits.truncate(64);
|
|
|
|
|
|
|
|
|
Ok(UInt64 {
|
|
|
Ok(UInt64 {
|
|
|
bits: result_bits,
|
|
|
|
|
|
|
|
|
bits: result_bits,
|
|
|
value: modular_value,
|
|
|
value: modular_value,
|
|
|
})
|
|
|
})
|
|
|
}
|
|
|
}
|
|
@ -330,7 +330,7 @@ impl ToBytesGadget for UInt64 { |
|
|
let mut bytes = Vec::new();
|
|
|
let mut bytes = Vec::new();
|
|
|
for (i, chunk8) in self.to_bits_le().chunks(8).enumerate() {
|
|
|
for (i, chunk8) in self.to_bits_le().chunks(8).enumerate() {
|
|
|
let byte = UInt8 {
|
|
|
let byte = UInt8 {
|
|
|
bits: chunk8.to_vec(),
|
|
|
|
|
|
|
|
|
bits: chunk8.to_vec(),
|
|
|
value: value_chunks[i],
|
|
|
value: value_chunks[i],
|
|
|
};
|
|
|
};
|
|
|
bytes.push(byte);
|
|
|
bytes.push(byte);
|
|
@ -397,7 +397,7 @@ mod test { |
|
|
match bit_gadget {
|
|
|
match bit_gadget {
|
|
|
&Boolean::Constant(bit_gadget) => {
|
|
|
&Boolean::Constant(bit_gadget) => {
|
|
|
assert!(bit_gadget == ((b.value.unwrap() >> i) & 1 == 1));
|
|
|
assert!(bit_gadget == ((b.value.unwrap() >> i) & 1 == 1));
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
_ => unreachable!(),
|
|
|
_ => unreachable!(),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@ -406,8 +406,8 @@ mod test { |
|
|
|
|
|
|
|
|
for x in v.iter().zip(expected_to_be_same.iter()) {
|
|
|
for x in v.iter().zip(expected_to_be_same.iter()) {
|
|
|
match x {
|
|
|
match x {
|
|
|
(&Boolean::Constant(true), &Boolean::Constant(true)) => {},
|
|
|
|
|
|
(&Boolean::Constant(false), &Boolean::Constant(false)) => {},
|
|
|
|
|
|
|
|
|
(&Boolean::Constant(true), &Boolean::Constant(true)) => {}
|
|
|
|
|
|
(&Boolean::Constant(false), &Boolean::Constant(false)) => {}
|
|
|
_ => unreachable!(),
|
|
|
_ => unreachable!(),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@ -442,13 +442,13 @@ mod test { |
|
|
match b {
|
|
|
match b {
|
|
|
&Boolean::Is(ref b) => {
|
|
|
&Boolean::Is(ref b) => {
|
|
|
assert!(b.get_value().unwrap() == (expected & 1 == 1));
|
|
|
assert!(b.get_value().unwrap() == (expected & 1 == 1));
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
&Boolean::Not(ref b) => {
|
|
|
&Boolean::Not(ref b) => {
|
|
|
assert!(!b.get_value().unwrap() == (expected & 1 == 1));
|
|
|
assert!(!b.get_value().unwrap() == (expected & 1 == 1));
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
&Boolean::Constant(b) => {
|
|
|
&Boolean::Constant(b) => {
|
|
|
assert!(b == (expected & 1 == 1));
|
|
|
assert!(b == (expected & 1 == 1));
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
expected >>= 1;
|
|
|
expected >>= 1;
|
|
@ -483,7 +483,7 @@ mod test { |
|
|
&Boolean::Not(_) => panic!(),
|
|
|
&Boolean::Not(_) => panic!(),
|
|
|
&Boolean::Constant(b) => {
|
|
|
&Boolean::Constant(b) => {
|
|
|
assert!(b == (expected & 1 == 1));
|
|
|
assert!(b == (expected & 1 == 1));
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
expected >>= 1;
|
|
|
expected >>= 1;
|
|
@ -521,10 +521,10 @@ mod test { |
|
|
match b {
|
|
|
match b {
|
|
|
&Boolean::Is(ref b) => {
|
|
|
&Boolean::Is(ref b) => {
|
|
|
assert!(b.get_value().unwrap() == (expected & 1 == 1));
|
|
|
assert!(b.get_value().unwrap() == (expected & 1 == 1));
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
&Boolean::Not(ref b) => {
|
|
|
&Boolean::Not(ref b) => {
|
|
|
assert!(!b.get_value().unwrap() == (expected & 1 == 1));
|
|
|
assert!(!b.get_value().unwrap() == (expected & 1 == 1));
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
&Boolean::Constant(_) => unreachable!(),
|
|
|
&Boolean::Constant(_) => unreachable!(),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
@ -560,7 +560,7 @@ mod test { |
|
|
match b {
|
|
|
match b {
|
|
|
&Boolean::Constant(b) => {
|
|
|
&Boolean::Constant(b) => {
|
|
|
assert_eq!(b, tmp & 1 == 1);
|
|
|
assert_eq!(b, tmp & 1 == 1);
|
|
|
},
|
|
|
|
|
|
|
|
|
}
|
|
|
_ => unreachable!(),
|
|
|
_ => unreachable!(),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|