mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-05 15:23:06 +00:00
Merge pull request #89 from mir-protocol/insert_gadget_fix
Insertion gadget fix
This commit is contained in:
commit
7a5b04ad49
@ -37,8 +37,7 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
self.sub(one, not_equal)
|
||||
}
|
||||
|
||||
/// Inserts a `Target` in a vector at a non-deterministic index. This is done by rotating to the
|
||||
/// left, inserting at 0 and then rotating to the right.
|
||||
/// Inserts a `Target` in a vector at a non-deterministic index.
|
||||
/// Note: `index` is not range-checked.
|
||||
pub fn insert(
|
||||
&mut self,
|
||||
@ -49,9 +48,8 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
let mut already_inserted = self.zero();
|
||||
let mut new_list = Vec::new();
|
||||
|
||||
for i in 0..v.len() {
|
||||
let one = self.one();
|
||||
|
||||
let one = self.one();
|
||||
for i in 0..=v.len() {
|
||||
let cur_index = self.constant(F::from_canonical_usize(i));
|
||||
let insert_here = self.is_equal(cur_index, index);
|
||||
|
||||
@ -63,11 +61,14 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||
already_inserted = self.add(already_inserted, insert_here);
|
||||
|
||||
let not_already_inserted = self.sub(one, already_inserted);
|
||||
new_item = self.scalar_mul_add_extension(not_already_inserted, v[i], new_item);
|
||||
if i < v.len() {
|
||||
new_item = self.scalar_mul_add_extension(not_already_inserted, v[i], new_item);
|
||||
}
|
||||
|
||||
new_list.push(new_item);
|
||||
}
|
||||
|
||||
|
||||
new_list
|
||||
}
|
||||
}
|
||||
@ -106,6 +107,8 @@ mod tests {
|
||||
let inserted = real_insert(i, elem, &v);
|
||||
let purported_inserted = builder.insert(it, elem, v.clone());
|
||||
|
||||
assert_eq!(inserted.len(), purported_inserted.len());
|
||||
|
||||
for (x, y) in inserted.into_iter().zip(purported_inserted) {
|
||||
builder.route_extension(x, y);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user