Debian Codegraph @

src/Util/AdditionChainExponentiation.v

raw
1 From Coq Require Import FunInd.
2 From Coq Require Import List SetoidList. Import ListNotations.
3 From Coq Require Import BinNums BinNat.
4 Require Import Crypto.Util.ListUtil.
5 Require Import Crypto.Algebra.Monoid Crypto.Algebra.ScalarMult.
6 Require Import Crypto.Util.Option.
7 Require Import Crypto.Util.Tactics.BreakMatch.
8
9 Section AddChainExp.
10 (* TODO: rewrite this.
11 - use CPS and Loop
12 - use an inner loop for repeated squaring
13 - connect to something that abstracts over F.pow, Z.pow, N.pow NOT scalarmult
14 *)
15
16 Function fold_chain {T} (id:T) (op:T->T->T) (is:list (nat*nat)) (acc:list T) {struct is} : T :=
17 match is with
18 | nil =>
19 match acc with
20 | nil => id
21 | ret::_ => ret
22 end
23 | (i,j)::is' =>
24 let ijx := op (nth_default id acc i) (nth_default id acc j) in
25 fold_chain id op is' (ijx::acc)
26 end.
27
28 Example wikipedia_addition_chain : fold_chain 0 plus [
29 (0, 0); (* 2 = 1 + 1 *) (* the indices say how far back the chain to look *)
30 (0, 1); (* 3 = 2 + 1 *)
31 (0, 0); (* 6 = 3 + 3 *)
32 (0, 0); (* 12 = 6 + 6 *)
33 (0, 0); (* 24 = 12 + 12 *)
34 (0, 2); (* 30 = 24 + 6 *)
35 (0, 6)] (* 31 = 30 + 1 *)
36 [1] = 31. reflexivity. Qed.
37 End AddChainExp.