mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-11 05:45:30 +03:00
impl add, sub, mul, div
This commit is contained in:
parent
c4f233e142
commit
82eaba499c
@ -1 +1 @@
|
||||
return a + 1
|
||||
return a / 2
|
@ -25,16 +25,16 @@ fn bool_from_variable<F: Field, CS: ConstraintSystem<F>>(
|
||||
}
|
||||
|
||||
fn u32_from_variable<F: Field, CS: ConstraintSystem<F>>(cs: &mut CS, variable: Variable) -> UInt32 {
|
||||
// let argument = std::env::args()
|
||||
// .nth(1)
|
||||
// .unwrap_or("1".into())
|
||||
// .parse::<u32>()
|
||||
// .unwrap();
|
||||
//
|
||||
// println!(" argument passed to command line a = {:?}", argument);
|
||||
let argument = std::env::args()
|
||||
.nth(1)
|
||||
.unwrap_or("1".into())
|
||||
.parse::<u32>()
|
||||
.unwrap();
|
||||
|
||||
let a = 1;
|
||||
UInt32::alloc(cs.ns(|| variable.0), Some(a)).unwrap()
|
||||
println!(" argument passed to command line a = {:?}", argument);
|
||||
|
||||
// let a = 1;
|
||||
UInt32::alloc(cs.ns(|| variable.0), Some(argument)).unwrap()
|
||||
}
|
||||
|
||||
fn get_bool_value<F: Field, CS: ConstraintSystem<F>>(
|
||||
@ -138,7 +138,64 @@ fn enforce_add<F: Field, CS: ConstraintSystem<F>>(
|
||||
let left = get_u32_value(cs, left);
|
||||
let right = get_u32_value(cs, right);
|
||||
|
||||
// let _r = UInt32::addmany(cs.ns(|| "addition"), &[left, right]).unwrap();
|
||||
let r = left
|
||||
.add(
|
||||
cs.ns(|| format!("enforce {} + {}", left.value.unwrap(), right.value.unwrap())),
|
||||
&right,
|
||||
)
|
||||
.unwrap();
|
||||
println!("result {}", r.value.unwrap());
|
||||
}
|
||||
|
||||
fn enforce_sub<F: Field, CS: ConstraintSystem<F>>(
|
||||
cs: &mut CS,
|
||||
left: FieldExpression,
|
||||
right: FieldExpression,
|
||||
) {
|
||||
let left = get_u32_value(cs, left);
|
||||
let right = get_u32_value(cs, right);
|
||||
|
||||
let r = left
|
||||
.sub(
|
||||
cs.ns(|| format!("enforce {} - {}", left.value.unwrap(), right.value.unwrap())),
|
||||
&right,
|
||||
)
|
||||
.unwrap();
|
||||
println!("result {}", r.value.unwrap());
|
||||
}
|
||||
|
||||
fn enforce_mul<F: Field, CS: ConstraintSystem<F>>(
|
||||
cs: &mut CS,
|
||||
left: FieldExpression,
|
||||
right: FieldExpression,
|
||||
) {
|
||||
let left = get_u32_value(cs, left);
|
||||
let right = get_u32_value(cs, right);
|
||||
|
||||
let r = left
|
||||
.mul(
|
||||
cs.ns(|| format!("enforce {} * {}", left.value.unwrap(), right.value.unwrap())),
|
||||
&right,
|
||||
)
|
||||
.unwrap();
|
||||
println!("result {}", r.value.unwrap());
|
||||
}
|
||||
|
||||
fn enforce_div<F: Field, CS: ConstraintSystem<F>>(
|
||||
cs: &mut CS,
|
||||
left: FieldExpression,
|
||||
right: FieldExpression,
|
||||
) {
|
||||
let left = get_u32_value(cs, left);
|
||||
let right = get_u32_value(cs, right);
|
||||
|
||||
let r = left
|
||||
.div(
|
||||
cs.ns(|| format!("enforce {} / {}", left.value.unwrap(), right.value.unwrap())),
|
||||
&right,
|
||||
)
|
||||
.unwrap();
|
||||
println!("result {}", r.value.unwrap());
|
||||
}
|
||||
|
||||
fn enforce_field_expression<F: Field, CS: ConstraintSystem<F>>(
|
||||
@ -149,6 +206,15 @@ fn enforce_field_expression<F: Field, CS: ConstraintSystem<F>>(
|
||||
FieldExpression::Add(left, right) => {
|
||||
enforce_add(cs, *left, *right);
|
||||
}
|
||||
FieldExpression::Sub(left, right) => {
|
||||
enforce_sub(cs, *left, *right);
|
||||
}
|
||||
FieldExpression::Mul(left, right) => {
|
||||
enforce_mul(cs, *left, *right);
|
||||
}
|
||||
FieldExpression::Div(left, right) => {
|
||||
enforce_div(cs, *left, *right);
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
61
src/aleo_program/gadgets.rs
Normal file
61
src/aleo_program/gadgets.rs
Normal file
@ -0,0 +1,61 @@
|
||||
// use snarkos_errors::gadgets::SynthesisError;
|
||||
// use snarkos_models::{
|
||||
// curves::Field,
|
||||
// gadgets::{
|
||||
// r1cs::ConstraintSystem,
|
||||
// utilities::uint32::UInt32
|
||||
// }
|
||||
// };
|
||||
// use snarkos_models::gadgets::utilities::boolean::Boolean;
|
||||
//
|
||||
// impl UInt32 {
|
||||
// pub fn and<F: Field, CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Self, SynthesisError> {
|
||||
// let value= match (self.value, other.valoue) {
|
||||
// (Some(a), Some(b)) => Some(a & b),
|
||||
// _=> None,
|
||||
// };
|
||||
//
|
||||
// let bits = self
|
||||
// .bits
|
||||
// .iter()
|
||||
// .zip(other.bits.iter())
|
||||
// .enumerate()
|
||||
// .map(|(i, (a, b)) | Boolean::and(cs.ns(|| format!("and of bit gadget {}", i)), a, b))
|
||||
// .collect();
|
||||
//
|
||||
// Ok(UInt32 { bits, value })
|
||||
// }
|
||||
//
|
||||
// fn recursive_add<F: Field, CS: ConstraintSystem<F>>(mut cs: CS, a: &Self, b: &Self) -> Result<Self, SynthesisError> {
|
||||
// let uncommon_bits = a.xor(cs.ns(|| format!("{} ^ {}", a.value.unwrap(), b.value.unwrap())),&b)?;
|
||||
// let common_bits = a.and(cs.ns(|| format!("{} & {}", a.value.unwrap(), b.value.unwrap())), &b)?;
|
||||
//
|
||||
// if common_bits.value == 0 {
|
||||
// return Ok(uncommon_bits)
|
||||
// }
|
||||
// let shifted_common_bits = common_bits.rotr(common_bits.bits.len() - 1);
|
||||
// return Self::recursive_add(cs.ns(|| format!("recursive add {} + {}", uncommon_bits.value, shifted_common_bits.value)), &uncommon_bits, &shifted_common_bits)
|
||||
// }
|
||||
//
|
||||
// pub fn add<F: Field, CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Self, SynthesisError> {
|
||||
// let new_value = match (self.value, other.value) {
|
||||
// (Some(a), Some(b)) => Some(a + b),
|
||||
// _ => None,
|
||||
// };
|
||||
//
|
||||
// return Self::recursive_add(cs.ns( || format!("recursive add {} + {}", self.value, other.value)), &self, &other)
|
||||
//
|
||||
// // let bits = self
|
||||
// // .bits
|
||||
// // .iter()
|
||||
// // .zip(other.bits.iter())
|
||||
// // .enumerate()
|
||||
// // .map(|(i, (a, b))| Boo)
|
||||
// }
|
||||
//
|
||||
// pub fn sub<F: Field, CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Self, SynthesisError> {}
|
||||
//
|
||||
// pub fn mul<F: Field, CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Self, SynthesisError> {}
|
||||
//
|
||||
// pub fn div<F: Field, CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Self, SynthesisError> {}
|
||||
// }
|
@ -10,6 +10,9 @@ pub use self::types::*;
|
||||
pub mod constraints;
|
||||
pub use self::constraints::*;
|
||||
|
||||
pub mod gadgets;
|
||||
pub use self::gadgets::*;
|
||||
|
||||
pub mod types_display;
|
||||
pub use self::types_display::*;
|
||||
|
||||
|
58
src/main.rs
58
src/main.rs
@ -57,42 +57,42 @@ impl<F: Field> ConstraintSynthesizer<F> for Benchmark<F> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut setup = Duration::new(0, 0);
|
||||
let mut proving = Duration::new(0, 0);
|
||||
let mut verifying = Duration::new(0, 0);
|
||||
// let mut setup = Duration::new(0, 0);
|
||||
// let mut proving = Duration::new(0, 0);
|
||||
// let mut verifying = Duration::new(0, 0);
|
||||
|
||||
let rng = &mut thread_rng();
|
||||
|
||||
let start = Instant::now();
|
||||
// let start = Instant::now();
|
||||
|
||||
let params = {
|
||||
let _params = {
|
||||
let c = Benchmark::<Fr>::new();
|
||||
generate_random_parameters::<Bls12_377, _, _>(c, rng).unwrap()
|
||||
};
|
||||
|
||||
let prepared_verifying_key = prepare_verifying_key(¶ms.vk);
|
||||
|
||||
setup += start.elapsed();
|
||||
|
||||
let start = Instant::now();
|
||||
let proof = {
|
||||
let c = Benchmark::new();
|
||||
create_random_proof(c, ¶ms, rng).unwrap()
|
||||
};
|
||||
|
||||
proving += start.elapsed();
|
||||
|
||||
// let _inputs: Vec<_> = [1u32; 1].to_vec();
|
||||
|
||||
let start = Instant::now();
|
||||
|
||||
let _ = verify_proof(&prepared_verifying_key, &proof, &[]).unwrap();
|
||||
|
||||
verifying += start.elapsed();
|
||||
|
||||
println!("\n Setup time: {:?} seconds", setup.as_secs());
|
||||
println!(" Proving time: {:?} seconds", proving.as_secs());
|
||||
println!(" Verifying time: {:?} seconds", verifying.as_secs());
|
||||
//
|
||||
// let prepared_verifying_key = prepare_verifying_key(¶ms.vk);
|
||||
//
|
||||
// setup += start.elapsed();
|
||||
//
|
||||
// let start = Instant::now();
|
||||
// let proof = {
|
||||
// let c = Benchmark::new();
|
||||
// create_random_proof(c, ¶ms, rng).unwrap()
|
||||
// };
|
||||
//
|
||||
// proving += start.elapsed();
|
||||
//
|
||||
// // let _inputs: Vec<_> = [1u32; 1].to_vec();
|
||||
//
|
||||
// let start = Instant::now();
|
||||
//
|
||||
// let _ = verify_proof(&prepared_verifying_key, &proof, &[]).unwrap();
|
||||
//
|
||||
// verifying += start.elapsed();
|
||||
//
|
||||
// println!("\n Setup time: {:?} seconds", setup.as_secs());
|
||||
// println!(" Proving time: {:?} seconds", proving.as_secs());
|
||||
// println!(" Verifying time: {:?} seconds", verifying.as_secs());
|
||||
|
||||
// let mut cs = TestConstraintSystem::<Fr>::new();
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user