multiScalar_multiplication Pippeneger算法

本文探讨了椭圆曲线密码学中批量标量乘法的不同算法优化,包括Bos-Coster、Straus及Pippenger算法,并通过实验对比了不同参数下算法的性能表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 引言

multiScalar_multiplication定义如下:
V = a 1 P 1 + a 2 P 2 + . . . + a n P n , 其 中 a i 为 s c a l a r 值 , P i 为 e l l i p t i c − c u r v e   p o i n t V=a_1P_1+a_2P_2+...+a_nP_n,其中a_i为scalar值,P_i为elliptic-curve\ point V=a1P1+a2P2+...+anPnaiscalarPiellipticcurve point

在论文《Faster batch forgery identification》第四节中,分别提及了三种算法:

  • Bos-Coster算法:对multiScalar_multiplication速度的提升有限。
    在这里插入图片描述
  • Straus算法:选择 w = 5 w=5 w=5
    在这里插入图片描述
  • Pippenger算法:可分别选择 w = 6 w=6 w=6 w = 7 w=7 w=7 w = 8 w=8 w=8
    在这里插入图片描述

2. curve25519-dalek中算法选择边界

curve25519-dalek的边界为:

  • <190: straus
  • =190 && <500: w=6 Pippeneger算法

  • =500 && <800: w=7 Pippeneger算法

  • =800: w=8 Pippeneger算法

all w=8:
     Running target/release/deps/dalek_benchmarks-53fcb1faec6cb376
Variable-time variable-base multiscalar multiplication/189
                        time:   [4.4437 ms 4.4703 ms 4.5316 ms]
                        change: [-1.7336% +23.795% +57.793%] (p = 0.11 > 0.05)
                        No change in performance detected.
Found 3 outliers among 15 measurements (20.00%)
  3 (20.00%) high severe
Variable-time variable-base multiscalar multiplication/190
                        time:   [4.4633 ms 4.4925 ms 4.5632 ms]
                        change: [+2.0396% +27.568% +58.014%] (p = 0.05 < 0.05)
                        Performance has regressed.
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/499
                        time:   [7.2052 ms 7.2595 ms 7.3616 ms]
                        change: [-14.260% +8.9149% +34.793%] (p = 0.53 > 0.05)
                        No change in performance detected.
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/500
                        time:   [7.2194 ms 7.2761 ms 7.3911 ms]
                        change: [-12.083% +9.2730% +36.544%] (p = 0.48 > 0.05)
                        No change in performance detected.
Found 3 outliers among 15 measurements (20.00%)
  1 (6.67%) high mild
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/799
                        time:   [9.8895 ms 9.9910 ms 10.142 ms]
                        change: [-13.810% +4.7550% +23.994%] (p = 0.70 > 0.05)
                        No change in performance detected.
Found 3 outliers among 15 measurements (20.00%)
  3 (20.00%) high severe
Variable-time variable-base multiscalar multiplication/800
                        time:   [9.9245 ms 9.9687 ms 10.077 ms]
                        change: [-15.381% +3.3416% +29.183%] (p = 0.78 > 0.05)
                        No change in performance detected.
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe



all w=7:
Running target/release/deps/dalek_benchmarks-53fcb1faec6cb376
Variable-time variable-base multiscalar multiplication/189
                        time:   [3.5630 ms 3.5914 ms 3.6504 ms]
                        change: [-11.315% +12.310% +41.991%] (p = 0.39 > 0.05)
                        No change in performance detected.
Found 3 outliers among 15 measurements (20.00%)
  3 (20.00%) high severe
Variable-time variable-base multiscalar multiplication/190
                        time:   [3.5012 ms 3.5234 ms 3.5752 ms]
                        change: [-10.315% +10.701% +39.224%] (p = 0.41 > 0.05)
                        No change in performance detected.
Found 3 outliers among 15 measurements (20.00%)
  3 (20.00%) high severe
Variable-time variable-base multiscalar multiplication/499
                        time:   [6.6358 ms 6.6664 ms 6.7455 ms]
                        change: [-22.010% -2.0152% +25.105%] (p = 0.83 > 0.05)
                        No change in performance detected.
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/500
                        time:   [6.6411 ms 6.6746 ms 6.7609 ms]
                        change: [-22.621% -3.1515% +24.251%] (p = 0.80 > 0.05)
                        No change in performance detected.
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/799
                        time:   [9.6734 ms 9.7493 ms 9.8566 ms]
                        change: [-32.201% -15.383% +6.3693%] (p = 0.20 > 0.05)
                        No change in performance detected.
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/800
                        time:   [9.6891 ms 9.7310 ms 9.8314 ms]
                        change: [-21.197% -4.7756% +15.300%] (p = 0.66 > 0.05)
                        No change in performance detected.
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe





all w=6:
     Running target/release/deps/dalek_benchmarks-53fcb1faec6cb376
Variable-time variable-base multiscalar multiplication/189
                        time:   [3.1587 ms 3.1799 ms 3.2284 ms]
                        change: [-17.560% +4.6755% +32.986%] (p = 0.74 > 0.05)
                        No change in performance detected.
Found 3 outliers among 15 measurements (20.00%)
  3 (20.00%) high severe
Variable-time variable-base multiscalar multiplication/190
                        time:   [3.1706 ms 3.1918 ms 3.2404 ms]
                        change: [-21.148% +1.7730% +30.036%] (p = 0.90 > 0.05)
                        No change in performance detected.
Found 3 outliers among 15 measurements (20.00%)
  3 (20.00%) high severe
Variable-time variable-base multiscalar multiplication/499
                        time:   [6.8064 ms 6.8407 ms 6.9261 ms]
                        change: [-30.371% -12.039% +12.024%] (p = 0.32 > 0.05)
                        No change in performance detected.
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/500
                        time:   [6.8180 ms 6.8740 ms 6.9684 ms]
                        change: [-31.891% -13.085% +10.826%] (p = 0.29 > 0.05)
                        No change in performance detected.
Found 3 outliers among 15 measurements (20.00%)
  1 (6.67%) high mild
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/799
                        time:   [10.399 ms 10.546 ms 10.819 ms]
                        change: [-27.627% -9.1493% +13.573%] (p = 0.46 > 0.05)
                        No change in performance detected.
Found 3 outliers among 15 measurements (20.00%)
  1 (6.67%) high mild
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/800
                        time:   [10.281 ms 10.318 ms 10.392 ms]
                        change: [-39.340% -22.582% -2.0183%] (p = 0.06 > 0.05)
                        No change in performance detected.
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe



all straus:
    Running target/release/deps/dalek_benchmarks-53fcb1faec6cb376
Variable-time variable-base multiscalar multiplication/189
                        time:   [2.9865 ms 3.0344 ms 3.1013 ms]
                        change: [-18.787% +2.8603% +30.058%] (p = 0.84 > 0.05)
                        No change in performance detected.
Found 3 outliers among 15 measurements (20.00%)
  1 (6.67%) high mild
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/190
                        time:   [2.9745 ms 3.0011 ms 3.0599 ms]
                        change: [-22.820% -0.3394% +30.681%] (p = 0.98 > 0.05)
                        No change in performance detected.
Found 3 outliers among 15 measurements (20.00%)
  3 (20.00%) high severe
Variable-time variable-base multiscalar multiplication/499
                        time:   [7.7750 ms 7.8195 ms 7.9154 ms]
                        change: [-17.989% +2.7770% +32.322%] (p = 0.83 > 0.05)
                        No change in performance detected.
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/500
                        time:   [7.8235 ms 7.8651 ms 7.9713 ms]
                        change: [-7.2904% +16.179% +49.302%] (p = 0.26 > 0.05)
                        No change in performance detected.
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/799
                        time:   [12.478 ms 12.538 ms 12.667 ms]
                        change: [+4.1941% +28.354% +57.937%] (p = 0.02 < 0.05)
                        Performance has regressed.
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/800
                        time:   [12.667 ms 12.782 ms 12.937 ms]
                        change: [+4.4294% +34.305% +70.910%] (p = 0.03 < 0.05)
                        Performance has regressed.
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe





default:

     Running target/release/deps/dalek_benchmarks-53fcb1faec6cb376
Variable-time variable-base multiscalar multiplication/189
                        time:   [2.9304 ms 2.9669 ms 3.0377 ms]
                        change: [-78.836% -74.890% -70.059%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 3 outliers among 15 measurements (20.00%)
  1 (6.67%) high mild
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/190
                        time:   [3.1322 ms 3.1516 ms 3.1971 ms]
                        change: [-78.159% -74.341% -68.876%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 3 outliers among 15 measurements (20.00%)
  1 (6.67%) high mild
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/499
                        time:   [6.8036 ms 7.0906 ms 7.7292 ms]
                        change: [-74.384% -69.294% -62.834%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 3 outliers among 15 measurements (20.00%)
  2 (13.33%) high mild
  1 (6.67%) high severe
Variable-time variable-base multiscalar multiplication/500
                        time:   [6.6848 ms 6.7521 ms 6.8618 ms]
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe
Variable-time variable-base multiscalar multiplication/799
                        time:   [9.5685 ms 9.7077 ms 9.9853 ms]
Found 3 outliers among 15 measurements (20.00%)
  3 (20.00%) high severe
Variable-time variable-base multiscalar multiplication/800
                        time:   [9.7910 ms 9.8422 ms 9.9460 ms]
Found 2 outliers among 15 measurements (13.33%)
  2 (13.33%) high severe

#[cfg(feature = "alloc")]
impl VartimeMultiscalarMul for EdwardsPoint {
    type Point = EdwardsPoint;

    fn optional_multiscalar_mul<I, J>(scalars: I, points: J) -> Option<EdwardsPoint>
    where
        I: IntoIterator,
        I::Item: Borrow<Scalar>,
        J: IntoIterator<Item = Option<EdwardsPoint>>,
    {
        // Sanity-check lengths of input iterators
        let mut scalars = scalars.into_iter();
        let mut points = points.into_iter();

        // Lower and upper bounds on iterators
        let (s_lo, s_hi) = scalars.by_ref().size_hint();
        let (p_lo, p_hi) = points.by_ref().size_hint();

        // They should all be equal
        assert_eq!(s_lo, p_lo);
        assert_eq!(s_hi, Some(s_lo));
        assert_eq!(p_hi, Some(p_lo));

        // Now we know there's a single size.
        // Use this as the hint to decide which algorithm to use.
        let size = s_lo;

        if size < 190 {
            scalar_mul::straus::Straus::optional_multiscalar_mul(scalars, points)
        } else {
            scalar_mul::pippenger::Pippenger::optional_multiscalar_mul(scalars, points)
        }
    }
}

参考资料:
[1] 论文《Faster batch forgery identification》

<think>好的,用户想了解编程中与乘法相关的概念或实现。首先,我需要明确用户的需求,可能是指乘法在不同编程环境中的实现方式,或者算法层面的乘法原理,比如基本乘法、快速乘法(如Karatsuba算法)等。 首先,我应该从基础的乘法实现讲起,比如在编程中如何用循环和累加来实现整数乘法。这有助于理解乘法的本质,尤其对于初学者来说,是一个很好的起点。接下来,可以考虑递归方法,例如将乘法分解为更小的问题,这可能涉及到分治策略,比如Karatsuba算法,这种方法比传统乘法更高效,适合大数相乘。 然后,需要考虑编程语言中的特殊处理,比如Python支持大整数乘法,底层可能已经优化过,但用户可能想知道原理。此外,浮点数乘法的实现也是一个方面,IEEE 754标准中的处理方式,涉及符号位、指数位和尾数位的操作,这里可能需要简要说明。 用户可能还关心乘法在算法中的应用,比如矩阵乘法,这在机器学习和图形处理中非常重要。可以提及暴力法和Strassen算法,后者是更高效的矩阵乘法方法。 另外,乘法在硬件层面的实现,比如使用位运算和加法器的组合,这也是一个有趣的角度,但可能稍微底层,需要根据用户的背景决定是否深入。 需要确保回答结构化,分点阐述,每个部分给出示例代码,比如用Python展示基础乘法和Karatsuba算法。同时,注意使用LaTeX格式的数学公式,如$a \times b = \sum_{i=1}^{b} a$(当b为整数时),以及矩阵乘法的公式$$C_{ij} = \sum_{k=1}^{n} A_{ik} \times B_{kj}$$。 还要考虑生成相关问题,比如乘法的时间复杂度、处理大数乘法的技巧、浮点数乘法的误差等,这些都能帮助用户进一步学习。同时,引用站内资料,比如梯度下降中的参数更新涉及乘法,可以引用相关的引用[4]。 最后,检查是否符合系统级指令:行内公式用$...$,独立公式用$$...$$,语法正确,中文回答,生成相关问题,引用正确。确保代码示例正确,比如递归实现的Karatsuba算法可能需要处理奇偶数位数的情况,并正确分割数字。 总结回答结构:先介绍基本概念,分点讨论不同实现方法,提供代码示例,引用相关资料,最后生成相关问题。这样用户能逐步理解乘法在编程中的多种实现和应用。</think>### 乘法在编程中的概念与实现 #### 1. 基本乘法实现 在编程中,乘法的基础实现通常通过循环累加或位移运算完成。例如,整数乘法可通过累加实现: ```python def multiply(a, b): result = 0 for _ in range(abs(b)): result += abs(a) return result if (a > 0) == (b > 0) else -result ``` 该方法时间复杂度为$O(n)$($n$为$b$的绝对值)[^1]。 #### 2. 快速乘法算法(Karatsuba算法) 对于大整数乘法,Karatsuba算法通过分治策略将时间复杂度优化到$O(n^{\log_2 3}) \approx O(n^{1.585})$: $$(a \cdot 10^{n/2} + b)(c \cdot 10^{n/2} + d) = ac \cdot 10^n + (ad + bc) \cdot 10^{n/2} + bd$$ 其中中间项$(ad + bc)$可优化为$(a + b)(c + d) - ac - bd$以减少乘法次数[^4]。 ```python def karatsuba(x, y): if x < 10 or y < 10: return x * y n = max(len(str(x)), len(str(y))) m = n // 2 a, b = divmod(x, 10**m) c, d = divmod(y, 10**m) ac = karatsuba(a, c) bd = karatsuba(b, d) ad_plus_bc = karatsuba(a + b, c + d) - ac - bd return ac * 10**(2*m) + ad_plus_bc * 10**m + bd ``` #### 3. 浮点数乘法实现 根据IEEE 754标准,浮点数乘法需处理: - 符号位异或 - 指数位相加并补偿偏移量 - 尾数位相乘后规格化 例如:$(-1)^{s_1} \cdot 2^{e_1} \cdot m_1 \times (-1)^{s_2} \cdot 2^{e_2} \cdot m_2 = (-1)^{s_1 \oplus s_2} \cdot 2^{e_1+e_2-127} \cdot (m_1 \times m_2)$[^3] #### 4. 矩阵乘法 矩阵乘法$C = A \times B$的实现包含三重循环: $$C_{ij} = \sum_{k=1}^{n} A_{ik} \times B_{kj}$$ Strassen算法通过分治将复杂度从$O(n^3)$降低到$O(n^{2.807})$。 ```python def matrix_mult(A, B): return [[sum(a*b for a,b in zip(row, col)) for col in zip(*B)] for row in A] ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值