-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: correct batch inversion function #117
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -237,21 +237,20 @@ pub(crate) unconstrained fn __batch_invert<let N: u32, let MOD_BITS: u32, let M: | |
) -> [[Field; N]; M] { | ||
// TODO: ugly! Will fail if input slice is empty | ||
let mut accumulator: [Field; N] = __one::<N>(); | ||
let mut result: [[Field; N]; M] = [[0; N]; M]; | ||
let mut temporaries: [[Field; N]; N] = std::mem::zeroed(); | ||
for i in 0..N { | ||
for i in 0..M { | ||
temporaries[i] = accumulator; | ||
if (!__is_zero(x[i])) { | ||
accumulator = __mul::<_, MOD_BITS>(params, accumulator, x[i]); | ||
} | ||
} | ||
|
||
let mut result: [[Field; N]; M] = [[0; N]; M]; | ||
accumulator = __invmod::<_, MOD_BITS>(params, accumulator); | ||
let mut T0: [Field; N] = [0; N]; | ||
for i in 0..N { | ||
let idx = N - 1 - i; | ||
for i in 0..M { | ||
let idx = M - 1 - i; | ||
if (!__is_zero(x[idx])) { | ||
T0 = __mul::<_, MOD_BITS>(params, accumulator, temporaries[idx]); | ||
let T0 = __mul::<_, MOD_BITS>(params, accumulator, temporaries[idx]); | ||
accumulator = __mul::<_, MOD_BITS>(params, accumulator, x[idx]); | ||
result[idx] = T0; | ||
} | ||
|
@@ -265,26 +264,28 @@ pub(crate) unconstrained fn __batch_invert_slice<let N: u32, let MOD_BITS: u32>( | |
) -> [[Field; N]] { | ||
// TODO: ugly! Will fail if input slice is empty | ||
let mut accumulator: [Field; N] = __one::<N>(); | ||
let mut result: [[Field; N]] = [[0; N]]; | ||
let mut temporaries: [[Field; N]; N] = std::mem::zeroed(); | ||
for i in 0..N { | ||
temporaries[i] = accumulator; | ||
let mut temporaries: [[Field; N]] = &[]; | ||
for i in 0..x.len() { | ||
temporaries = temporaries.push_back(accumulator); | ||
if (!__is_zero(x[i])) { | ||
accumulator = __mul::<_, MOD_BITS>(params, accumulator, x[i]); | ||
} | ||
result = result.push_back([0; N]); | ||
} | ||
|
||
let mut result: [[Field; N]] = []; | ||
accumulator = __invmod::<_, MOD_BITS>(params, accumulator); | ||
let mut T0: [Field; N] = [0; N]; | ||
for i in 0..x.len() { | ||
let idx = x.len() - 1 - i; | ||
if (__is_zero(x[idx]) == false) { | ||
T0 = __mul::<_, MOD_BITS>(params, accumulator, temporaries[idx]); | ||
if (!__is_zero(x[idx])) { | ||
let T0 = __mul::<_, MOD_BITS>(params, accumulator, temporaries[idx]); | ||
accumulator = __mul::<_, MOD_BITS>(params, accumulator, x[idx]); | ||
result[idx] = T0; | ||
} | ||
assert(__mul::<_, MOD_BITS>(params, T0, x[idx]) == __one::<N>()); | ||
result = result.push_front(T0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The order was reversed, so we should push things to the front instead of the back There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed it in a later commit |
||
} else { | ||
result = result.push_front([0; N]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}; | ||
} | ||
|
||
result | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added an assertion for easier debugging later if something goes wrong, it wasn't showing the call stack properly.