add field type refactor code

This commit is contained in:
collin 2021-05-18 18:35:54 -07:00
parent abeb796cbb
commit 2b1b2839b0
5 changed files with 294 additions and 75 deletions

View File

@ -24,8 +24,8 @@ use crate::{
relational::*,
resolve_core_circuit,
value::{Address, ConstrainedValue, Integer},
FieldType,
GroupType,
OldFieldType,
};
use leo_asg::{expression::*, ConstValue, Expression, Node, Span};
@ -43,7 +43,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
Ok(match value {
ConstValue::Address(value) => ConstrainedValue::Address(Address::constant(value.to_string(), span)?),
ConstValue::Boolean(value) => ConstrainedValue::Boolean(Boolean::Constant(*value)),
ConstValue::Field(value) => ConstrainedValue::Field(FieldType::constant(value.to_string(), span)?),
ConstValue::Field(value) => ConstrainedValue::Field(OldFieldType::constant(value.to_string(), span)?),
ConstValue::Group(value) => ConstrainedValue::Group(G::constant(value, span)?),
ConstValue::Int(value) => ConstrainedValue::Integer(Integer::new(value)),
ConstValue::Tuple(values) => ConstrainedValue::Tuple(

View File

@ -26,9 +26,9 @@ use crate::{
group::input::group_from_input,
ConstrainedValue,
},
FieldType,
GroupType,
Integer,
OldFieldType,
};
use leo_asg::{ConstInt, Type};
use leo_ast::{InputValue, Span};
@ -80,7 +80,9 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
match (type_, input) {
(Type::Address, InputValue::Address(addr)) => Ok(ConstrainedValue::Address(Address::constant(addr, span)?)),
(Type::Boolean, InputValue::Boolean(value)) => Ok(ConstrainedValue::Boolean(Boolean::constant(value))),
(Type::Field, InputValue::Field(value)) => Ok(ConstrainedValue::Field(FieldType::constant(value, span)?)),
(Type::Field, InputValue::Field(value)) => {
Ok(ConstrainedValue::Field(OldFieldType::constant(value, span)?))
}
(Type::Group, InputValue::Group(value)) => Ok(ConstrainedValue::Group(G::constant(&value.into(), span)?)),
(Type::Integer(integer_type), InputValue::Integer(_, value)) => Ok(ConstrainedValue::Integer(
Integer::new(&ConstInt::parse(integer_type, &value, span)?),

View File

@ -37,19 +37,228 @@ use snarkvm_gadgets::{
};
use snarkvm_r1cs::{ConstraintSystem, SynthesisError};
use snarkvm_gadgets::utilities::eq::NEqGadget;
use std::{borrow::Borrow, cmp::Ordering};
#[derive(Clone, Debug)]
pub enum FieldType<F: PrimeField> {
pub struct FieldType<F: PrimeField>(FpGadget<F>);
impl<F: PrimeField> FieldType<F> {
/// Returns the value of the field.
pub fn get_value(&self) -> Option<F> {
self.0.get_value()
}
/// Returns a new `FieldType` from the given `String` or returns a `FieldError`.
pub fn constant<CS: ConstraintSystem<F>>(_cs: CS, string: String, span: &Span) -> Result<Self, FieldError> {
let number_info = number_string_typing(&string);
let value = match number_info {
(number, neg) if neg => {
-F::from_str(&number).map_err(|_| FieldError::invalid_field(string.clone(), span))?
}
(number, _) => F::from_str(&number).map_err(|_| FieldError::invalid_field(string.clone(), span))?,
};
let value = FpGadget::alloc_constant(_cs, || Ok(value)).map_err(|_| FieldError::invalid_field(string, span))?;
Ok(FieldType(value))
}
/// Returns a new `FieldType` by calling the `FpGadget` `negate` function.
pub fn negate<CS: ConstraintSystem<F>>(&self, cs: CS, span: &Span) -> Result<Self, FieldError> {
let result = self.0.negate(cs).map_err(|e| FieldError::negate_operation(e, span))?;
Ok(FieldType(result))
}
/// Returns a new `FieldType` by calling the `FpGadget` `add` function.
pub fn add<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self, span: &Span) -> Result<Self, FieldError> {
let value = self
.0
.add(cs, &other.0)
.map_err(|e| FieldError::binary_operation("+".to_string(), e, span))?;
Ok(FieldType(value))
}
/// Returns a new `FieldType` by calling the `FpGadget` `sub` function.
pub fn sub<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self, span: &Span) -> Result<Self, FieldError> {
let value = self
.0
.sub(cs, &other.0)
.map_err(|e| FieldError::binary_operation("-".to_string(), e, span))?;
Ok(FieldType(value))
}
/// Returns a new `FieldType` by calling the `FpGadget` `mul` function.
pub fn mul<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self, span: &Span) -> Result<Self, FieldError> {
let value = self
.0
.mul(cs, &other.0)
.map_err(|e| FieldError::binary_operation("*".to_string(), e, span))?;
Ok(FieldType(value))
}
/// Returns a new `FieldType` by calling the `FpGadget` `inverse` function.
pub fn inverse<CS: ConstraintSystem<F>>(&self, cs: CS, span: &Span) -> Result<Self, FieldError> {
let value = self
.0
.inverse(cs)
.map_err(|e| FieldError::binary_operation("inv".to_string(), e, span))?;
Ok(FieldType(value))
}
/// Returns a new `FieldType` by calling the `FpGadget` `div` function.
pub fn div<CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self, span: &Span) -> Result<Self, FieldError> {
let inverse = other.inverse(cs.ns(|| "division inverse"), span)?;
self.mul(cs, &inverse, span)
}
pub fn alloc_helper<Fn: FnOnce() -> Result<T, SynthesisError>, T: Borrow<String>>(
value_gen: Fn,
) -> Result<F, SynthesisError> {
let field_string = match value_gen() {
Ok(value) => {
let string_value = value.borrow().clone();
Ok(string_value)
}
_ => Err(SynthesisError::AssignmentMissing),
}?;
F::from_str(&field_string).map_err(|_| SynthesisError::AssignmentMissing)
}
}
impl<F: PrimeField> AllocGadget<String, F> for FieldType<F> {
fn alloc<Fn: FnOnce() -> Result<T, SynthesisError>, T: Borrow<String>, CS: ConstraintSystem<F>>(
cs: CS,
value_gen: Fn,
) -> Result<Self, SynthesisError> {
let value = FpGadget::alloc(cs, || Self::alloc_helper(value_gen))?;
Ok(FieldType(value))
}
fn alloc_input<Fn: FnOnce() -> Result<T, SynthesisError>, T: Borrow<String>, CS: ConstraintSystem<F>>(
cs: CS,
value_gen: Fn,
) -> Result<Self, SynthesisError> {
let value = FpGadget::alloc_input(cs, || Self::alloc_helper(value_gen))?;
Ok(FieldType(value))
}
}
impl<F: PrimeField> PartialEq for FieldType<F> {
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}
impl<F: PrimeField> Eq for FieldType<F> {}
impl<F: PrimeField> PartialOrd for FieldType<F> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
let self_value = self.get_value();
let other_value = other.get_value();
Option::from(self_value.cmp(&other_value))
}
}
impl<F: PrimeField> EvaluateEqGadget<F> for FieldType<F> {
fn evaluate_equal<CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
// self.0.is_eq(cs, &other.0)
unimplemented!();
}
}
impl<F: PrimeField> EqGadget<F> for FieldType<F> {}
impl<F: PrimeField> ConditionalEqGadget<F> for FieldType<F> {
fn conditional_enforce_equal<CS: ConstraintSystem<F>>(
&self,
cs: CS,
other: &Self,
condition: &Boolean,
) -> Result<(), SynthesisError> {
self.0.conditional_enforce_equal(cs, &other.0, condition)
}
fn cost() -> usize {
2 * <FpGadget<F> as ConditionalEqGadget<F>>::cost()
}
}
impl<F: PrimeField> NEqGadget<F> for FieldType<F> {
fn enforce_not_equal<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<(), SynthesisError> {
self.0.enforce_not_equal(cs, &other.0)
}
fn cost() -> usize {
<FpGadget<F> as NEqGadget<F>>::cost()
}
}
impl<F: PrimeField> CondSelectGadget<F> for FieldType<F> {
fn conditionally_select<CS: ConstraintSystem<F>>(
cs: CS,
cond: &Boolean,
first: &Self,
second: &Self,
) -> Result<Self, SynthesisError> {
let value = FpGadget::conditionally_select(cs, cond, &first.0, &second.0)?;
Ok(FieldType(value))
}
fn cost() -> usize {
2 * <FpGadget<F> as CondSelectGadget<F>>::cost()
}
}
impl<F: PrimeField> ToBitsBEGadget<F> for FieldType<F> {
fn to_bits_be<CS: ConstraintSystem<F>>(&self, cs: CS) -> Result<Vec<Boolean>, SynthesisError> {
self.0.to_bits_be(cs)
}
fn to_bits_be_strict<CS: ConstraintSystem<F>>(&self, cs: CS) -> Result<Vec<Boolean>, SynthesisError> {
self.0.to_bits_be(cs)
}
}
impl<F: PrimeField> ToBytesGadget<F> for FieldType<F> {
fn to_bytes<CS: ConstraintSystem<F>>(&self, cs: CS) -> Result<Vec<UInt8>, SynthesisError> {
self.0.to_bytes(cs)
}
fn to_bytes_strict<CS: ConstraintSystem<F>>(&self, cs: CS) -> Result<Vec<UInt8>, SynthesisError> {
self.0.to_bytes_strict(cs)
}
}
impl<F: PrimeField> std::fmt::Display for FieldType<F> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self.get_value().ok_or(std::fmt::Error))
}
}
#[derive(Clone, Debug)]
pub enum OldFieldType<F: PrimeField> {
Constant(F),
Allocated(FpGadget<F>),
}
impl<F: PrimeField> FieldType<F> {
impl<F: PrimeField> OldFieldType<F> {
pub fn get_value(&self) -> Option<F> {
match self {
FieldType::Constant(field) => Some(*field),
FieldType::Allocated(gadget) => gadget.get_value(),
OldFieldType::Constant(field) => Some(*field),
OldFieldType::Allocated(gadget) => gadget.get_value(),
}
}
@ -61,113 +270,119 @@ impl<F: PrimeField> FieldType<F> {
(number, _) => F::from_str(&number).map_err(|_| FieldError::invalid_field(string, span))?,
};
Ok(FieldType::Constant(value))
Ok(OldFieldType::Constant(value))
}
pub fn negate<CS: ConstraintSystem<F>>(&self, cs: CS, span: &Span) -> Result<Self, FieldError> {
match self {
FieldType::Constant(field) => Ok(FieldType::Constant(field.neg())),
FieldType::Allocated(field) => {
OldFieldType::Constant(field) => Ok(OldFieldType::Constant(field.neg())),
OldFieldType::Allocated(field) => {
let result = field.negate(cs).map_err(|e| FieldError::negate_operation(e, span))?;
Ok(FieldType::Allocated(result))
Ok(OldFieldType::Allocated(result))
}
}
}
pub fn add<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self, span: &Span) -> Result<Self, FieldError> {
match (self, other) {
(FieldType::Constant(self_value), FieldType::Constant(other_value)) => {
Ok(FieldType::Constant(self_value.add(other_value)))
(OldFieldType::Constant(self_value), OldFieldType::Constant(other_value)) => {
Ok(OldFieldType::Constant(self_value.add(other_value)))
}
(FieldType::Allocated(self_value), FieldType::Allocated(other_value)) => {
(OldFieldType::Allocated(self_value), OldFieldType::Allocated(other_value)) => {
let result = self_value
.add(cs, other_value)
.map_err(|e| FieldError::binary_operation("+".to_string(), e, span))?;
Ok(FieldType::Allocated(result))
Ok(OldFieldType::Allocated(result))
}
(FieldType::Constant(constant_value), FieldType::Allocated(allocated_value))
| (FieldType::Allocated(allocated_value), FieldType::Constant(constant_value)) => Ok(FieldType::Allocated(
allocated_value
.add_constant(cs, constant_value)
.map_err(|e| FieldError::binary_operation("+".to_string(), e, span))?,
)),
(OldFieldType::Constant(constant_value), OldFieldType::Allocated(allocated_value))
| (OldFieldType::Allocated(allocated_value), OldFieldType::Constant(constant_value)) => {
Ok(OldFieldType::Allocated(
allocated_value
.add_constant(cs, constant_value)
.map_err(|e| FieldError::binary_operation("+".to_string(), e, span))?,
))
}
}
}
pub fn sub<CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self, span: &Span) -> Result<Self, FieldError> {
match (self, other) {
(FieldType::Constant(self_value), FieldType::Constant(other_value)) => {
Ok(FieldType::Constant(self_value.sub(other_value)))
(OldFieldType::Constant(self_value), OldFieldType::Constant(other_value)) => {
Ok(OldFieldType::Constant(self_value.sub(other_value)))
}
(FieldType::Allocated(self_value), FieldType::Allocated(other_value)) => {
(OldFieldType::Allocated(self_value), OldFieldType::Allocated(other_value)) => {
let result = self_value
.sub(cs, other_value)
.map_err(|e| FieldError::binary_operation("-".to_string(), e, span))?;
Ok(FieldType::Allocated(result))
Ok(OldFieldType::Allocated(result))
}
(FieldType::Constant(constant_value), FieldType::Allocated(allocated_value)) => {
(OldFieldType::Constant(constant_value), OldFieldType::Allocated(allocated_value)) => {
let result = allocated_value
.sub_constant(cs.ns(|| "field_sub_constant"), constant_value)
.map_err(|e| FieldError::binary_operation("-".to_string(), e, span))?
.negate(cs.ns(|| "negate"))
.map_err(|e| FieldError::binary_operation("-".to_string(), e, span))?;
Ok(FieldType::Allocated(result))
Ok(OldFieldType::Allocated(result))
}
(FieldType::Allocated(allocated_value), FieldType::Constant(constant_value)) => Ok(FieldType::Allocated(
allocated_value
.sub_constant(cs, constant_value)
.map_err(|e| FieldError::binary_operation("-".to_string(), e, span))?,
)),
(OldFieldType::Allocated(allocated_value), OldFieldType::Constant(constant_value)) => {
Ok(OldFieldType::Allocated(
allocated_value
.sub_constant(cs, constant_value)
.map_err(|e| FieldError::binary_operation("-".to_string(), e, span))?,
))
}
}
}
pub fn mul<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self, span: &Span) -> Result<Self, FieldError> {
match (self, other) {
(FieldType::Constant(self_value), FieldType::Constant(other_value)) => {
Ok(FieldType::Constant(self_value.mul(other_value)))
(OldFieldType::Constant(self_value), OldFieldType::Constant(other_value)) => {
Ok(OldFieldType::Constant(self_value.mul(other_value)))
}
(FieldType::Allocated(self_value), FieldType::Allocated(other_value)) => {
(OldFieldType::Allocated(self_value), OldFieldType::Allocated(other_value)) => {
let result = self_value
.mul(cs, other_value)
.map_err(|e| FieldError::binary_operation("*".to_string(), e, span))?;
Ok(FieldType::Allocated(result))
Ok(OldFieldType::Allocated(result))
}
(FieldType::Constant(constant_value), FieldType::Allocated(allocated_value))
| (FieldType::Allocated(allocated_value), FieldType::Constant(constant_value)) => Ok(FieldType::Allocated(
allocated_value
.mul_by_constant(cs, constant_value)
.map_err(|e| FieldError::binary_operation("*".to_string(), e, span))?,
)),
(OldFieldType::Constant(constant_value), OldFieldType::Allocated(allocated_value))
| (OldFieldType::Allocated(allocated_value), OldFieldType::Constant(constant_value)) => {
Ok(OldFieldType::Allocated(
allocated_value
.mul_by_constant(cs, constant_value)
.map_err(|e| FieldError::binary_operation("*".to_string(), e, span))?,
))
}
}
}
pub fn div<CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self, span: &Span) -> Result<Self, FieldError> {
let inverse = match other {
FieldType::Constant(constant) => {
OldFieldType::Constant(constant) => {
let constant_inverse = constant
.inverse()
.ok_or_else(|| FieldError::no_inverse(constant.to_string(), span))?;
FieldType::Constant(constant_inverse)
OldFieldType::Constant(constant_inverse)
}
FieldType::Allocated(allocated) => {
OldFieldType::Allocated(allocated) => {
let allocated_inverse = allocated
.inverse(&mut cs)
.map_err(|e| FieldError::binary_operation("+".to_string(), e, span))?;
FieldType::Allocated(allocated_inverse)
OldFieldType::Allocated(allocated_inverse)
}
};
@ -190,22 +405,24 @@ impl<F: PrimeField> FieldType<F> {
pub fn allocated<CS: ConstraintSystem<F>>(&self, mut cs: CS) -> Result<FpGadget<F>, SynthesisError> {
match self {
FieldType::Constant(constant) => FpGadget::alloc(&mut cs.ns(|| format!("{:?}", constant)), || Ok(constant)),
FieldType::Allocated(allocated) => FpGadget::alloc(&mut cs.ns(|| format!("{:?}", allocated)), || {
OldFieldType::Constant(constant) => {
FpGadget::alloc(&mut cs.ns(|| format!("{:?}", constant)), || Ok(constant))
}
OldFieldType::Allocated(allocated) => FpGadget::alloc(&mut cs.ns(|| format!("{:?}", allocated)), || {
allocated.get_value().ok_or(SynthesisError::AssignmentMissing)
}),
}
}
}
impl<F: PrimeField> AllocGadget<String, F> for FieldType<F> {
impl<F: PrimeField> AllocGadget<String, F> for OldFieldType<F> {
fn alloc<Fn: FnOnce() -> Result<T, SynthesisError>, T: Borrow<String>, CS: ConstraintSystem<F>>(
cs: CS,
value_gen: Fn,
) -> Result<Self, SynthesisError> {
let value = FpGadget::alloc(cs, || Self::alloc_helper(value_gen))?;
Ok(FieldType::Allocated(value))
Ok(OldFieldType::Allocated(value))
}
fn alloc_input<Fn: FnOnce() -> Result<T, SynthesisError>, T: Borrow<String>, CS: ConstraintSystem<F>>(
@ -214,11 +431,11 @@ impl<F: PrimeField> AllocGadget<String, F> for FieldType<F> {
) -> Result<Self, SynthesisError> {
let value = FpGadget::alloc_input(cs, || Self::alloc_helper(value_gen))?;
Ok(FieldType::Allocated(value))
Ok(OldFieldType::Allocated(value))
}
}
impl<F: PrimeField> PartialEq for FieldType<F> {
impl<F: PrimeField> PartialEq for OldFieldType<F> {
fn eq(&self, other: &Self) -> bool {
let self_value = self.get_value();
let other_value = other.get_value();
@ -227,9 +444,9 @@ impl<F: PrimeField> PartialEq for FieldType<F> {
}
}
impl<F: PrimeField> Eq for FieldType<F> {}
impl<F: PrimeField> Eq for OldFieldType<F> {}
impl<F: PrimeField> PartialOrd for FieldType<F> {
impl<F: PrimeField> PartialOrd for OldFieldType<F> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
let self_value = self.get_value();
let other_value = other.get_value();
@ -238,10 +455,10 @@ impl<F: PrimeField> PartialOrd for FieldType<F> {
}
}
impl<F: PrimeField> EvaluateEqGadget<F> for FieldType<F> {
impl<F: PrimeField> EvaluateEqGadget<F> for OldFieldType<F> {
fn evaluate_equal<CS: ConstraintSystem<F>>(&self, mut _cs: CS, other: &Self) -> Result<Boolean, SynthesisError> {
match (self, other) {
(FieldType::Constant(first), FieldType::Constant(second)) => Ok(Boolean::constant(first.eq(second))),
(OldFieldType::Constant(first), OldFieldType::Constant(second)) => Ok(Boolean::constant(first.eq(second))),
_ => unimplemented!("field equality not implemented yet"), // (FieldType::Allocated(first), FieldType::Allocated(second)) => first.is_eq(cs, second),
// (FieldType::Constant(constant_value), FieldType::Allocated(allocated_value))
// | (FieldType::Allocated(allocated_value), FieldType::Constant(constant_value)) => {
@ -253,9 +470,9 @@ impl<F: PrimeField> EvaluateEqGadget<F> for FieldType<F> {
}
}
impl<F: PrimeField> EqGadget<F> for FieldType<F> {}
impl<F: PrimeField> EqGadget<F> for OldFieldType<F> {}
impl<F: PrimeField> ConditionalEqGadget<F> for FieldType<F> {
impl<F: PrimeField> ConditionalEqGadget<F> for OldFieldType<F> {
fn conditional_enforce_equal<CS: ConstraintSystem<F>>(
&self,
mut cs: CS,
@ -264,19 +481,19 @@ impl<F: PrimeField> ConditionalEqGadget<F> for FieldType<F> {
) -> Result<(), SynthesisError> {
match (self, other) {
// c - c
(FieldType::Constant(self_value), FieldType::Constant(other_value)) => {
(OldFieldType::Constant(self_value), OldFieldType::Constant(other_value)) => {
if self_value == other_value {
return Ok(());
}
Err(SynthesisError::AssignmentMissing)
}
// a - a
(FieldType::Allocated(self_value), FieldType::Allocated(other_value)) => {
(OldFieldType::Allocated(self_value), OldFieldType::Allocated(other_value)) => {
self_value.conditional_enforce_equal(cs, other_value, condition)
}
// c - a = a - c
(FieldType::Constant(constant_value), FieldType::Allocated(allocated_value))
| (FieldType::Allocated(allocated_value), FieldType::Constant(constant_value)) => {
(OldFieldType::Constant(constant_value), OldFieldType::Allocated(allocated_value))
| (OldFieldType::Allocated(allocated_value), OldFieldType::Constant(constant_value)) => {
let constant_gadget = FpGadget::from(AllocatedFp::from(&mut cs, constant_value));
constant_gadget.conditional_enforce_equal(cs, allocated_value, condition)
@ -289,7 +506,7 @@ impl<F: PrimeField> ConditionalEqGadget<F> for FieldType<F> {
}
}
impl<F: PrimeField> CondSelectGadget<F> for FieldType<F> {
impl<F: PrimeField> CondSelectGadget<F> for OldFieldType<F> {
fn conditionally_select<CS: ConstraintSystem<F>>(
mut cs: CS,
cond: &Boolean,
@ -303,7 +520,7 @@ impl<F: PrimeField> CondSelectGadget<F> for FieldType<F> {
let second_gadget = second.allocated(&mut cs)?;
let result = FpGadget::conditionally_select(cs, cond, &first_gadget, &second_gadget)?;
Ok(FieldType::Allocated(result))
Ok(OldFieldType::Allocated(result))
}
}
@ -312,7 +529,7 @@ impl<F: PrimeField> CondSelectGadget<F> for FieldType<F> {
}
}
impl<F: PrimeField> ToBitsBEGadget<F> for FieldType<F> {
impl<F: PrimeField> ToBitsBEGadget<F> for OldFieldType<F> {
fn to_bits_be<CS: ConstraintSystem<F>>(&self, mut cs: CS) -> Result<Vec<Boolean>, SynthesisError> {
let self_gadget = self.allocated(&mut cs)?;
self_gadget.to_bits_be(cs)
@ -324,7 +541,7 @@ impl<F: PrimeField> ToBitsBEGadget<F> for FieldType<F> {
}
}
impl<F: PrimeField> ToBytesGadget<F> for FieldType<F> {
impl<F: PrimeField> ToBytesGadget<F> for OldFieldType<F> {
fn to_bytes<CS: ConstraintSystem<F>>(&self, mut cs: CS) -> Result<Vec<UInt8>, SynthesisError> {
let self_gadget = self.allocated(&mut cs)?;
self_gadget.to_bytes(cs)
@ -336,7 +553,7 @@ impl<F: PrimeField> ToBytesGadget<F> for FieldType<F> {
}
}
impl<F: PrimeField> std::fmt::Display for FieldType<F> {
impl<F: PrimeField> std::fmt::Display for OldFieldType<F> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self.get_value().ok_or(std::fmt::Error))
}

View File

@ -16,7 +16,7 @@
//! Methods to enforce constraints on input field values in a compiled Leo program.
use crate::{errors::FieldError, number_string_typing, value::ConstrainedValue, FieldType, GroupType};
use crate::{errors::FieldError, number_string_typing, value::ConstrainedValue, GroupType, OldFieldType};
use leo_ast::{InputValue, Span};
use snarkvm_fields::PrimeField;
@ -28,19 +28,19 @@ pub(crate) fn allocate_field<F: PrimeField, CS: ConstraintSystem<F>>(
name: &str,
option: Option<String>,
span: &Span,
) -> Result<FieldType<F>, FieldError> {
) -> Result<OldFieldType<F>, FieldError> {
match option {
Some(string) => {
let number_info = number_string_typing(&string);
match number_info {
(number, neg) if neg => FieldType::alloc(
(number, neg) if neg => OldFieldType::alloc(
cs.ns(|| format!("`{}: field` {}:{}", name, span.line_start, span.col_start)),
|| Some(number).ok_or(SynthesisError::AssignmentMissing),
)
.map(|value| value.negate(cs, span))
.map_err(|_| FieldError::missing_field(format!("{}: field", name), span))?,
(number, _) => FieldType::alloc(
(number, _) => OldFieldType::alloc(
cs.ns(|| format!("`{}: field` {}:{}", name, span.line_start, span.col_start)),
|| Some(number).ok_or(SynthesisError::AssignmentMissing),
)

View File

@ -16,7 +16,7 @@
//! The in memory stored value for a defined name in a compiled Leo program.
use crate::{errors::ValueError, Address, FieldType, GroupType, Integer};
use crate::{errors::ValueError, Address, GroupType, Integer, OldFieldType};
use leo_asg::{Circuit, Identifier, Span, Type};
use snarkvm_fields::PrimeField;
@ -32,7 +32,7 @@ pub enum ConstrainedValue<'a, F: PrimeField, G: GroupType<F>> {
// Data types
Address(Address),
Boolean(Boolean),
Field(FieldType<F>),
Field(OldFieldType<F>),
Group(G),
Integer(Integer),
@ -189,7 +189,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> CondSelectGadget<F> for ConstrainedValu
ConstrainedValue::Boolean(Boolean::conditionally_select(cs, cond, bool_1, bool_2)?)
}
(ConstrainedValue::Field(field_1), ConstrainedValue::Field(field_2)) => {
ConstrainedValue::Field(FieldType::conditionally_select(cs, cond, field_1, field_2)?)
ConstrainedValue::Field(OldFieldType::conditionally_select(cs, cond, field_1, field_2)?)
}
(ConstrainedValue::Group(group_1), ConstrainedValue::Group(group_2)) => {
ConstrainedValue::Group(G::conditionally_select(cs, cond, group_1, group_2)?)