mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-05-28 04:39:55 +00:00
rename
This commit is contained in:
parent
db31b9f662
commit
6164796898
@ -49,8 +49,8 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
|||||||
|
|
||||||
pub fn pad_biguints(
|
pub fn pad_biguints(
|
||||||
&mut self,
|
&mut self,
|
||||||
a: BigUintTarget,
|
a: &BigUintTarget,
|
||||||
b: BigUintTarget,
|
b: &BigUintTarget,
|
||||||
) -> (BigUintTarget, BigUintTarget) {
|
) -> (BigUintTarget, BigUintTarget) {
|
||||||
if a.num_limbs() > b.num_limbs() {
|
if a.num_limbs() > b.num_limbs() {
|
||||||
let mut padded_b = b.clone();
|
let mut padded_b = b.clone();
|
||||||
@ -58,7 +58,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
|||||||
padded_b.limbs.push(self.zero_u32());
|
padded_b.limbs.push(self.zero_u32());
|
||||||
}
|
}
|
||||||
|
|
||||||
(a, padded_b)
|
(a.clone(), padded_b)
|
||||||
} else {
|
} else {
|
||||||
let mut padded_a = a.clone();
|
let mut padded_a = a.clone();
|
||||||
let to_extend = b.num_limbs() - a.num_limbs();
|
let to_extend = b.num_limbs() - a.num_limbs();
|
||||||
@ -66,15 +66,15 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
|||||||
padded_a.limbs.push(self.zero_u32());
|
padded_a.limbs.push(self.zero_u32());
|
||||||
}
|
}
|
||||||
|
|
||||||
(padded_a, b)
|
(padded_a, b.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cmp_biguint(&mut self, a: &BigUintTarget, b: &BigUintTarget) -> BoolTarget {
|
pub fn cmp_biguint(&mut self, a: &BigUintTarget, b: &BigUintTarget) -> BoolTarget {
|
||||||
let (padded_a, padded_b) = self.pad_biguints(a.clone(), b.clone());
|
let (a, b) = self.pad_biguints(a, b);
|
||||||
|
|
||||||
let a_vec = padded_a.limbs.iter().map(|&x| x.0).collect();
|
let a_vec = a.limbs.iter().map(|&x| x.0).collect();
|
||||||
let b_vec = padded_b.limbs.iter().map(|&x| x.0).collect();
|
let b_vec = b.limbs.iter().map(|&x| x.0).collect();
|
||||||
|
|
||||||
self.list_le(a_vec, b_vec, 32)
|
self.list_le(a_vec, b_vec, 32)
|
||||||
}
|
}
|
||||||
@ -115,7 +115,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
|||||||
// Subtract two `BigUintTarget`s. We assume that the first is larger than the second.
|
// Subtract two `BigUintTarget`s. We assume that the first is larger than the second.
|
||||||
pub fn sub_biguint(&mut self, a: &BigUintTarget, b: &BigUintTarget) -> BigUintTarget {
|
pub fn sub_biguint(&mut self, a: &BigUintTarget, b: &BigUintTarget) -> BigUintTarget {
|
||||||
let num_limbs = a.limbs.len();
|
let num_limbs = a.limbs.len();
|
||||||
debug_assert!(b.limbs.len() == num_limbs);
|
let (a, b) = self.pad_biguints(a, b);
|
||||||
|
|
||||||
let mut result_limbs = vec![];
|
let mut result_limbs = vec![];
|
||||||
|
|
||||||
|
|||||||
@ -13,24 +13,24 @@ pub struct ForeignFieldTarget<FF: Field> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||||
pub fn biguint_to_ff<FF: Field>(&mut self, x: &BigUintTarget) -> ForeignFieldTarget<FF> {
|
pub fn biguint_to_nonnative<FF: Field>(&mut self, x: &BigUintTarget) -> ForeignFieldTarget<FF> {
|
||||||
ForeignFieldTarget {
|
ForeignFieldTarget {
|
||||||
value: x.clone(),
|
value: x.clone(),
|
||||||
_phantom: PhantomData,
|
_phantom: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ff_to_biguint<FF: Field>(&mut self, x: &ForeignFieldTarget<FF>) -> BigUintTarget {
|
pub fn nonnative_to_biguint<FF: Field>(&mut self, x: &ForeignFieldTarget<FF>) -> BigUintTarget {
|
||||||
x.value.clone()
|
x.value.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn constant_ff<FF: Field>(&mut self, x: FF) -> ForeignFieldTarget<FF> {
|
pub fn constant_nonnative<FF: Field>(&mut self, x: FF) -> ForeignFieldTarget<FF> {
|
||||||
let x_biguint = self.constant_biguint(&x.to_biguint());
|
let x_biguint = self.constant_biguint(&x.to_biguint());
|
||||||
self.biguint_to_ff(&x_biguint)
|
self.biguint_to_nonnative(&x_biguint)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assert that two ForeignFieldTarget's, both assumed to be in reduced form, are equal.
|
// Assert that two ForeignFieldTarget's, both assumed to be in reduced form, are equal.
|
||||||
pub fn connect_ff_reduced<FF: Field>(
|
pub fn connect_nonnative<FF: Field>(
|
||||||
&mut self,
|
&mut self,
|
||||||
lhs: &ForeignFieldTarget<FF>,
|
lhs: &ForeignFieldTarget<FF>,
|
||||||
rhs: &ForeignFieldTarget<FF>,
|
rhs: &ForeignFieldTarget<FF>,
|
||||||
@ -44,8 +44,8 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
|||||||
a: &ForeignFieldTarget<FF>,
|
a: &ForeignFieldTarget<FF>,
|
||||||
b: &ForeignFieldTarget<FF>,
|
b: &ForeignFieldTarget<FF>,
|
||||||
) -> ForeignFieldTarget<FF> {
|
) -> ForeignFieldTarget<FF> {
|
||||||
let a_biguint = self.ff_to_biguint(a);
|
let a_biguint = self.nonnative_to_biguint(a);
|
||||||
let b_biguint = self.ff_to_biguint(b);
|
let b_biguint = self.nonnative_to_biguint(b);
|
||||||
let result = self.add_biguint(&a_biguint, &b_biguint);
|
let result = self.add_biguint(&a_biguint, &b_biguint);
|
||||||
|
|
||||||
self.reduce(&result)
|
self.reduce(&result)
|
||||||
@ -72,8 +72,8 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
|||||||
a: &ForeignFieldTarget<FF>,
|
a: &ForeignFieldTarget<FF>,
|
||||||
b: &ForeignFieldTarget<FF>,
|
b: &ForeignFieldTarget<FF>,
|
||||||
) -> ForeignFieldTarget<FF> {
|
) -> ForeignFieldTarget<FF> {
|
||||||
let a_biguint = self.ff_to_biguint(a);
|
let a_biguint = self.nonnative_to_biguint(a);
|
||||||
let b_biguint = self.ff_to_biguint(b);
|
let b_biguint = self.nonnative_to_biguint(b);
|
||||||
let result = self.mul_biguint(&a_biguint, &b_biguint);
|
let result = self.mul_biguint(&a_biguint, &b_biguint);
|
||||||
|
|
||||||
self.reduce(&result)
|
self.reduce(&result)
|
||||||
@ -85,7 +85,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
|||||||
) -> ForeignFieldTarget<FF> {
|
) -> ForeignFieldTarget<FF> {
|
||||||
let neg_one = FF::order() - BigUint::one();
|
let neg_one = FF::order() - BigUint::one();
|
||||||
let neg_one_target = self.constant_biguint(&neg_one);
|
let neg_one_target = self.constant_biguint(&neg_one);
|
||||||
let neg_one_ff = self.biguint_to_ff(&neg_one_target);
|
let neg_one_ff = self.biguint_to_nonnative(&neg_one_target);
|
||||||
|
|
||||||
self.mul_nonnative(&neg_one_ff, x)
|
self.mul_nonnative(&neg_one_ff, x)
|
||||||
}
|
}
|
||||||
@ -102,8 +102,8 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_ff<FF: Field>(&mut self, x: &ForeignFieldTarget<FF>) -> ForeignFieldTarget<FF> {
|
fn reduce_nonnative<FF: Field>(&mut self, x: &ForeignFieldTarget<FF>) -> ForeignFieldTarget<FF> {
|
||||||
let x_biguint = self.ff_to_biguint(x);
|
let x_biguint = self.nonnative_to_biguint(x);
|
||||||
self.reduce(&x_biguint)
|
self.reduce(&x_biguint)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -132,12 +132,12 @@ mod tests {
|
|||||||
let pw = PartialWitness::new();
|
let pw = PartialWitness::new();
|
||||||
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
||||||
|
|
||||||
let x = builder.constant_ff(x_ff);
|
let x = builder.constant_nonnative(x_ff);
|
||||||
let y = builder.constant_ff(y_ff);
|
let y = builder.constant_nonnative(y_ff);
|
||||||
let sum = builder.add_nonnative(&x, &y);
|
let sum = builder.add_nonnative(&x, &y);
|
||||||
|
|
||||||
let sum_expected = builder.constant_ff(sum_ff);
|
let sum_expected = builder.constant_nonnative(sum_ff);
|
||||||
builder.connect_ff_reduced(&sum, &sum_expected);
|
builder.connect_nonnative(&sum, &sum_expected);
|
||||||
|
|
||||||
let data = builder.build();
|
let data = builder.build();
|
||||||
let proof = data.prove(pw).unwrap();
|
let proof = data.prove(pw).unwrap();
|
||||||
@ -159,12 +159,12 @@ mod tests {
|
|||||||
let pw = PartialWitness::new();
|
let pw = PartialWitness::new();
|
||||||
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
||||||
|
|
||||||
let x = builder.constant_ff(x_ff);
|
let x = builder.constant_nonnative(x_ff);
|
||||||
let y = builder.constant_ff(y_ff);
|
let y = builder.constant_nonnative(y_ff);
|
||||||
let diff = builder.sub_nonnative(&x, &y);
|
let diff = builder.sub_nonnative(&x, &y);
|
||||||
|
|
||||||
let diff_expected = builder.constant_ff(diff_ff);
|
let diff_expected = builder.constant_nonnative(diff_ff);
|
||||||
builder.connect_ff_reduced(&diff, &diff_expected);
|
builder.connect_nonnative(&diff, &diff_expected);
|
||||||
|
|
||||||
let data = builder.build();
|
let data = builder.build();
|
||||||
let proof = data.prove(pw).unwrap();
|
let proof = data.prove(pw).unwrap();
|
||||||
@ -183,12 +183,12 @@ mod tests {
|
|||||||
let pw = PartialWitness::new();
|
let pw = PartialWitness::new();
|
||||||
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
||||||
|
|
||||||
let x = builder.constant_ff(x_ff);
|
let x = builder.constant_nonnative(x_ff);
|
||||||
let y = builder.constant_ff(y_ff);
|
let y = builder.constant_nonnative(y_ff);
|
||||||
let product = builder.mul_nonnative(&x, &y);
|
let product = builder.mul_nonnative(&x, &y);
|
||||||
|
|
||||||
let product_expected = builder.constant_ff(product_ff);
|
let product_expected = builder.constant_nonnative(product_ff);
|
||||||
builder.connect_ff_reduced(&product, &product_expected);
|
builder.connect_nonnative(&product, &product_expected);
|
||||||
|
|
||||||
let data = builder.build();
|
let data = builder.build();
|
||||||
let proof = data.prove(pw).unwrap();
|
let proof = data.prove(pw).unwrap();
|
||||||
@ -206,11 +206,11 @@ mod tests {
|
|||||||
let pw = PartialWitness::new();
|
let pw = PartialWitness::new();
|
||||||
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
let mut builder = CircuitBuilder::<F, 4>::new(config);
|
||||||
|
|
||||||
let x = builder.constant_ff(x_ff);
|
let x = builder.constant_nonnative(x_ff);
|
||||||
let neg_x = builder.neg_nonnative(&x);
|
let neg_x = builder.neg_nonnative(&x);
|
||||||
|
|
||||||
let neg_x_expected = builder.constant_ff(neg_x_ff);
|
let neg_x_expected = builder.constant_nonnative(neg_x_ff);
|
||||||
builder.connect_ff_reduced(&neg_x, &neg_x_expected);
|
builder.connect_nonnative(&neg_x, &neg_x_expected);
|
||||||
|
|
||||||
let data = builder.build();
|
let data = builder.build();
|
||||||
let proof = data.prove(pw).unwrap();
|
let proof = data.prove(pw).unwrap();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user