|
| 1 | +use super::sequence_builder::BigintMul256; |
1 | 2 | use super::{INPUT_LIMBS, OUTPUT_LIMBS}; |
2 | 3 | use jolt_inlines_sdk::host::Xlen; |
3 | 4 | use jolt_inlines_sdk::spec::{InlineMemoryLayout, InlineSpec, InlineTestHarness, INLINE}; |
4 | 5 |
|
5 | | -pub fn bigint_mul(lhs: [u64; INPUT_LIMBS], rhs: [u64; INPUT_LIMBS]) -> [u64; OUTPUT_LIMBS] { |
6 | | - let mut result = [0u64; OUTPUT_LIMBS]; |
| 6 | +impl InlineSpec for BigintMul256 { |
| 7 | + type Input = ([u64; INPUT_LIMBS], [u64; INPUT_LIMBS]); |
| 8 | + type Output = [u64; OUTPUT_LIMBS]; |
| 9 | + |
| 10 | + fn reference(input: &Self::Input) -> Self::Output { |
| 11 | + let (lhs, rhs) = input; |
| 12 | + let mut result = [0u64; OUTPUT_LIMBS]; |
7 | 13 |
|
8 | | - for (i, &lhs_limb) in lhs.iter().enumerate() { |
9 | | - for (j, &rhs_limb) in rhs.iter().enumerate() { |
10 | | - let product = (lhs_limb as u128) * (rhs_limb as u128); |
11 | | - let low = product as u64; |
12 | | - let high = (product >> 64) as u64; |
| 14 | + for (i, &lhs_limb) in lhs.iter().enumerate() { |
| 15 | + for (j, &rhs_limb) in rhs.iter().enumerate() { |
| 16 | + let product = (lhs_limb as u128) * (rhs_limb as u128); |
| 17 | + let low = product as u64; |
| 18 | + let high = (product >> 64) as u64; |
13 | 19 |
|
14 | | - let result_position = i + j; |
| 20 | + let result_position = i + j; |
15 | 21 |
|
16 | | - let (sum, carry1) = result[result_position].overflowing_add(low); |
17 | | - result[result_position] = sum; |
| 22 | + let (sum, carry1) = result[result_position].overflowing_add(low); |
| 23 | + result[result_position] = sum; |
18 | 24 |
|
19 | | - let mut carry = carry1 as u64; |
20 | | - if high != 0 || carry != 0 { |
21 | | - let (sum_with_hi, carry_hi) = result[result_position + 1].overflowing_add(high); |
22 | | - let (sum_with_carry, carry_carry) = sum_with_hi.overflowing_add(carry); |
23 | | - result[result_position + 1] = sum_with_carry; |
24 | | - carry = (carry_hi as u64) + (carry_carry as u64); |
| 25 | + let mut carry = carry1 as u64; |
| 26 | + if high != 0 || carry != 0 { |
| 27 | + let (sum_with_hi, carry_hi) = result[result_position + 1].overflowing_add(high); |
| 28 | + let (sum_with_carry, carry_carry) = sum_with_hi.overflowing_add(carry); |
| 29 | + result[result_position + 1] = sum_with_carry; |
| 30 | + carry = (carry_hi as u64) + (carry_carry as u64); |
25 | 31 |
|
26 | | - let mut carry_position = result_position + 2; |
27 | | - while carry != 0 && carry_position < OUTPUT_LIMBS { |
28 | | - let (sum, c) = result[carry_position].overflowing_add(carry); |
29 | | - result[carry_position] = sum; |
30 | | - carry = c as u64; |
31 | | - carry_position += 1; |
| 32 | + let mut carry_position = result_position + 2; |
| 33 | + while carry != 0 && carry_position < OUTPUT_LIMBS { |
| 34 | + let (sum, c) = result[carry_position].overflowing_add(carry); |
| 35 | + result[carry_position] = sum; |
| 36 | + carry = c as u64; |
| 37 | + carry_position += 1; |
| 38 | + } |
32 | 39 | } |
33 | 40 | } |
34 | 41 | } |
35 | | - } |
36 | | - result |
37 | | -} |
38 | | - |
39 | | -pub struct BigintMul256Spec; |
40 | | - |
41 | | -impl InlineSpec for BigintMul256Spec { |
42 | | - type Input = ([u64; INPUT_LIMBS], [u64; INPUT_LIMBS]); |
43 | | - type Output = [u64; OUTPUT_LIMBS]; |
44 | | - |
45 | | - fn reference(input: &Self::Input) -> Self::Output { |
46 | | - bigint_mul(input.0, input.1) |
| 42 | + result |
47 | 43 | } |
48 | 44 |
|
49 | 45 | fn create_harness() -> InlineTestHarness { |
|
0 commit comments