mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-21 18:57:47 +03:00
impl i8 i16 i32 i64 i128 for leo-inputs
This commit is contained in:
parent
6f36dc6cd6
commit
d30d72d76e
@ -120,12 +120,15 @@ type_integer = {
|
||||
| type_i64
|
||||
| type_i128
|
||||
}
|
||||
|
||||
// Declared in types/integer.rs
|
||||
type_u8 = { "u8" }
|
||||
type_u16 = { "u16" }
|
||||
type_u32 = { "u32" }
|
||||
type_u64 = { "u64" }
|
||||
type_u128 = { "u128" }
|
||||
|
||||
// Declared in types/integer.rs
|
||||
type_i8 = { "i8" }
|
||||
type_i16 = { "i16" }
|
||||
type_i32 = { "i32" }
|
||||
|
@ -19,6 +19,7 @@ pub enum IntegerType {
|
||||
}
|
||||
|
||||
// Unsigned
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::type_u8))]
|
||||
pub struct U8Type {}
|
||||
@ -39,7 +40,7 @@ pub struct U64Type {}
|
||||
#[pest_ast(rule(Rule::type_u128))]
|
||||
pub struct U128Type {}
|
||||
|
||||
// Singed
|
||||
// Signed
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq)]
|
||||
#[pest_ast(rule(Rule::type_i8))]
|
||||
|
@ -130,14 +130,17 @@ impl Integer {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_value(&self) -> Option<u128> {
|
||||
pub fn get_value(&self) -> Option<String> {
|
||||
let integer = self;
|
||||
match_integer!(integer => integer.get_value())
|
||||
}
|
||||
|
||||
pub fn to_usize(&self, span: Span) -> Result<usize, IntegerError> {
|
||||
let value = self.get_value().ok_or(IntegerError::invalid_index(span))?;
|
||||
Ok(value as usize)
|
||||
let value = self.get_value().ok_or(IntegerError::invalid_index(span.clone()))?;
|
||||
let value_usize = value
|
||||
.parse::<usize>()
|
||||
.map_err(|_| IntegerError::invalid_integer(value, span))?;
|
||||
Ok(value_usize)
|
||||
}
|
||||
|
||||
pub fn to_bits_le(&self) -> Vec<Boolean> {
|
||||
@ -165,14 +168,20 @@ impl Integer {
|
||||
cs: &mut CS,
|
||||
integer_type: IntegerType,
|
||||
name: String,
|
||||
option: Option<u128>,
|
||||
option: Option<String>,
|
||||
span: Span,
|
||||
) -> Result<Self, IntegerError> {
|
||||
Ok(match integer_type {
|
||||
IntegerType::U8 => {
|
||||
let u8_name = format!("{}: u8", name);
|
||||
let u8_name_unique = format!("`{}` {}:{}", u8_name, span.line, span.start);
|
||||
let u8_option = option.map(|integer| integer as u8);
|
||||
|
||||
let u8_option = option.map(|s| {
|
||||
s.parse::<u8>()
|
||||
.map_err(|_| IntegerError::invalid_integer(s, span.clone()))
|
||||
.unwrap()
|
||||
});
|
||||
|
||||
let u8_result = UInt8::alloc(cs.ns(|| u8_name_unique), || {
|
||||
u8_option.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
@ -183,7 +192,11 @@ impl Integer {
|
||||
IntegerType::U16 => {
|
||||
let u16_name = format!("{}: u16", name);
|
||||
let u16_name_unique = format!("`{}` {}:{}", u16_name, span.line, span.start);
|
||||
let u16_option = option.map(|integer| integer as u16);
|
||||
let u16_option = option.map(|s| {
|
||||
s.parse::<u16>()
|
||||
.map_err(|_| IntegerError::invalid_integer(s, span.clone()))
|
||||
.unwrap()
|
||||
});
|
||||
let u16_result = UInt16::alloc(cs.ns(|| u16_name_unique), || {
|
||||
u16_option.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
@ -194,7 +207,11 @@ impl Integer {
|
||||
IntegerType::U32 => {
|
||||
let u32_name = format!("{}: u32", name);
|
||||
let u32_name_unique = format!("`{}` {}:{}", u32_name, span.line, span.start);
|
||||
let u32_option = option.map(|integer| integer as u32);
|
||||
let u32_option = option.map(|s| {
|
||||
s.parse::<u32>()
|
||||
.map_err(|_| IntegerError::invalid_integer(s, span.clone()))
|
||||
.unwrap()
|
||||
});
|
||||
let u32_result = UInt32::alloc(cs.ns(|| u32_name_unique), || {
|
||||
u32_option.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
@ -205,7 +222,11 @@ impl Integer {
|
||||
IntegerType::U64 => {
|
||||
let u64_name = format!("{}: u64", name);
|
||||
let u64_name_unique = format!("`{}` {}:{}", u64_name, span.line, span.start);
|
||||
let u64_option = option.map(|integer| integer as u64);
|
||||
let u64_option = option.map(|s| {
|
||||
s.parse::<u64>()
|
||||
.map_err(|_| IntegerError::invalid_integer(s, span.clone()))
|
||||
.unwrap()
|
||||
});
|
||||
let u64_result = UInt64::alloc(cs.ns(|| u64_name_unique), || {
|
||||
u64_option.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
@ -216,7 +237,11 @@ impl Integer {
|
||||
IntegerType::U128 => {
|
||||
let u128_name = format!("{}: u128", name);
|
||||
let u128_name_unique = format!("`{}` {}:{}", u128_name, span.line, span.start);
|
||||
let u128_option = option.map(|integer| integer as u128);
|
||||
let u128_option = option.map(|s| {
|
||||
s.parse::<u128>()
|
||||
.map_err(|_| IntegerError::invalid_integer(s, span.clone()))
|
||||
.unwrap()
|
||||
});
|
||||
let u128_result = UInt128::alloc(cs.ns(|| u128_name_unique), || {
|
||||
u128_option.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
@ -228,7 +253,11 @@ impl Integer {
|
||||
IntegerType::I8 => {
|
||||
let i8_name = format!("{}: i8", name);
|
||||
let i8_name_unique = format!("`{}` {}:{}", i8_name, span.line, span.start);
|
||||
let i8_option = option.map(|integer| integer as i8);
|
||||
let i8_option = option.map(|s| {
|
||||
s.parse::<i8>()
|
||||
.map_err(|_| IntegerError::invalid_integer(s, span.clone()))
|
||||
.unwrap()
|
||||
});
|
||||
let i8_result = Int8::alloc(cs.ns(|| i8_name_unique), || {
|
||||
i8_option.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
@ -239,7 +268,11 @@ impl Integer {
|
||||
IntegerType::I16 => {
|
||||
let i16_name = format!("{}: i16", name);
|
||||
let i16_name_unique = format!("`{}` {}:{}", i16_name, span.line, span.start);
|
||||
let i16_option = option.map(|integer| integer as i16);
|
||||
let i16_option = option.map(|s| {
|
||||
s.parse::<i16>()
|
||||
.map_err(|_| IntegerError::invalid_integer(s, span.clone()))
|
||||
.unwrap()
|
||||
});
|
||||
let i16_result = Int16::alloc(cs.ns(|| i16_name_unique), || {
|
||||
i16_option.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
@ -250,7 +283,11 @@ impl Integer {
|
||||
IntegerType::I32 => {
|
||||
let i32_name = format!("{}: i32", name);
|
||||
let i32_name_unique = format!("`{}` {}:{}", i32_name, span.line, span.start);
|
||||
let i32_option = option.map(|integer| integer as i32);
|
||||
let i32_option = option.map(|s| {
|
||||
s.parse::<i32>()
|
||||
.map_err(|_| IntegerError::invalid_integer(s, span.clone()))
|
||||
.unwrap()
|
||||
});
|
||||
let i32_result = Int32::alloc(cs.ns(|| i32_name_unique), || {
|
||||
i32_option.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
@ -261,7 +298,11 @@ impl Integer {
|
||||
IntegerType::I64 => {
|
||||
let i64_name = format!("{}: i64", name);
|
||||
let i64_name_unique = format!("`{}` {}:{}", i64_name, span.line, span.start);
|
||||
let i64_option = option.map(|integer| integer as i64);
|
||||
let i64_option = option.map(|s| {
|
||||
s.parse::<i64>()
|
||||
.map_err(|_| IntegerError::invalid_integer(s, span.clone()))
|
||||
.unwrap()
|
||||
});
|
||||
let i64_result = Int64::alloc(cs.ns(|| i64_name_unique), || {
|
||||
i64_option.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
@ -272,7 +313,11 @@ impl Integer {
|
||||
IntegerType::I128 => {
|
||||
let i128_name = format!("{}: i128", name);
|
||||
let i128_name_unique = format!("`{}` {}:{}", i128_name, span.line, span.start);
|
||||
let i128_option = option.map(|integer| integer as i128);
|
||||
let i128_option = option.map(|s| {
|
||||
s.parse::<i128>()
|
||||
.map_err(|_| IntegerError::invalid_integer(s, span.clone()))
|
||||
.unwrap()
|
||||
});
|
||||
let i128_result = Int128::alloc(cs.ns(|| i128_name_unique), || {
|
||||
i128_option.ok_or(SynthesisError::AssignmentMissing)
|
||||
})
|
||||
|
@ -7,7 +7,7 @@ use snarkos_models::gadgets::utilities::{
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub trait IntegerTrait: Sized + Clone + Debug + Add {
|
||||
fn get_value(&self) -> Option<u128>;
|
||||
fn get_value(&self) -> Option<String>;
|
||||
|
||||
fn get_bits(&self) -> Vec<Boolean>;
|
||||
}
|
||||
@ -15,8 +15,8 @@ pub trait IntegerTrait: Sized + Clone + Debug + Add {
|
||||
macro_rules! integer_trait_impl {
|
||||
($($gadget: ident)*) => ($(
|
||||
impl IntegerTrait for $gadget {
|
||||
fn get_value(&self) -> Option<u128> {
|
||||
self.value.map(|num| num as u128)
|
||||
fn get_value(&self) -> Option<String> {
|
||||
self.value.map(|num| num.to_string())
|
||||
}
|
||||
|
||||
fn get_bits(&self) -> Vec<Boolean> {
|
||||
|
@ -191,7 +191,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
|
||||
ConstrainedValue::Integer(integer) => {
|
||||
let integer_type = integer.get_type();
|
||||
let option = integer.get_value();
|
||||
let name = option.map(|n| n.to_string()).unwrap_or(format!("[allocated]"));
|
||||
let name = option.clone().unwrap_or(format!("[allocated]"));
|
||||
|
||||
*integer = Integer::allocate_type(&mut cs, integer_type, name, option, span)?;
|
||||
}
|
||||
|
@ -2,7 +2,22 @@
|
||||
|
||||
// Declared in common/identifier.rs
|
||||
identifier = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* }
|
||||
protected_name = { "let" | "for"| "if" | "else" | "as" | "return" }
|
||||
protected_name = {
|
||||
"address"
|
||||
| "as"
|
||||
| "const"
|
||||
| "else"
|
||||
| "false"
|
||||
| "function"
|
||||
| "for"
|
||||
| "if"
|
||||
| "in"
|
||||
| "let"
|
||||
| "mut"
|
||||
| "return"
|
||||
| "static"
|
||||
| "true"
|
||||
}
|
||||
|
||||
// Declared in common/line_end.rs
|
||||
LINE_END = { ";" ~ NEWLINE* }
|
||||
@ -20,13 +35,27 @@ type_integer = {
|
||||
| type_u32
|
||||
| type_u64
|
||||
| type_u128
|
||||
| type_i8
|
||||
| type_i16
|
||||
| type_i32
|
||||
| type_i64
|
||||
| type_i128
|
||||
}
|
||||
|
||||
// Declared in types/integer_type.rs
|
||||
type_u8 = { "u8" }
|
||||
type_u16 = { "u16" }
|
||||
type_u32 = { "u32" }
|
||||
type_u64 = { "u64" }
|
||||
type_u128 = { "u128" }
|
||||
|
||||
// Declared in types/integer_type.rs
|
||||
type_i8 = { "i8" }
|
||||
type_i16 = { "i16" }
|
||||
type_i32 = { "i32" }
|
||||
type_i64 = { "i64" }
|
||||
type_i128 = { "i128" }
|
||||
|
||||
// Declared in types/field_type.rs
|
||||
type_field = { "field" }
|
||||
|
||||
|
@ -10,8 +10,16 @@ pub enum IntegerType {
|
||||
U32Type(U32Type),
|
||||
U64Type(U64Type),
|
||||
U128Type(U128Type),
|
||||
|
||||
I8Type(I8Type),
|
||||
I16Type(I16Type),
|
||||
I32Type(I32Type),
|
||||
I64Type(I64Type),
|
||||
I128Type(I128Type),
|
||||
}
|
||||
|
||||
// Unsigned
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
|
||||
#[pest_ast(rule(Rule::type_u8))]
|
||||
pub struct U8Type {}
|
||||
@ -32,6 +40,28 @@ pub struct U64Type {}
|
||||
#[pest_ast(rule(Rule::type_u128))]
|
||||
pub struct U128Type {}
|
||||
|
||||
// Signed
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
|
||||
#[pest_ast(rule(Rule::type_i8))]
|
||||
pub struct I8Type {}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
|
||||
#[pest_ast(rule(Rule::type_i16))]
|
||||
pub struct I16Type {}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
|
||||
#[pest_ast(rule(Rule::type_i32))]
|
||||
pub struct I32Type {}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
|
||||
#[pest_ast(rule(Rule::type_i64))]
|
||||
pub struct I64Type {}
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
|
||||
#[pest_ast(rule(Rule::type_i128))]
|
||||
pub struct I128Type {}
|
||||
|
||||
impl std::fmt::Display for IntegerType {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
@ -40,6 +70,12 @@ impl std::fmt::Display for IntegerType {
|
||||
IntegerType::U32Type(_) => write!(f, "u32"),
|
||||
IntegerType::U64Type(_) => write!(f, "u64"),
|
||||
IntegerType::U128Type(_) => write!(f, "u128"),
|
||||
|
||||
IntegerType::I8Type(_) => write!(f, "i8"),
|
||||
IntegerType::I16Type(_) => write!(f, "i16"),
|
||||
IntegerType::I32Type(_) => write!(f, "i32"),
|
||||
IntegerType::I64Type(_) => write!(f, "i64"),
|
||||
IntegerType::I128Type(_) => write!(f, "i128"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ pub enum InputValue {
|
||||
Boolean(bool),
|
||||
Field(String),
|
||||
Group(String),
|
||||
Integer(IntegerType, u128),
|
||||
Integer(IntegerType, String),
|
||||
Array(Vec<InputValue>),
|
||||
}
|
||||
|
||||
@ -29,8 +29,7 @@ impl InputValue {
|
||||
}
|
||||
|
||||
fn from_number(integer_type: IntegerType, number: NumberValue) -> Result<Self, InputParserError> {
|
||||
let integer = number.value.parse::<u128>()?;
|
||||
Ok(InputValue::Integer(integer_type, integer))
|
||||
Ok(InputValue::Integer(integer_type, number.value))
|
||||
}
|
||||
|
||||
fn from_group(group: GroupValue) -> Self {
|
||||
|
Loading…
Reference in New Issue
Block a user