Debian Codegraph @

src/Util/Strings/ParseArithmeticToTaps.v

raw
1 From Coq Require Import Ascii String List.
2 From Coq Require Import QArith_base.
3 From Coq Require Import ZArith.
4 Require Import Crypto.Util.Option.
5 Require Import Crypto.Util.OptionList.
6 Require Import Crypto.Util.Strings.ParseArithmetic.
7 Require Import Crypto.Util.Notations.
8 Import ListNotations.
9 Local Open Scope option_scope.
10 Local Open Scope list_scope.
11 Local Open Scope char_scope.
12 Local Open Scope string_scope.
13 Local Open Scope Z_scope.
14
15 Local Open Scope parse_scope.
16
17 (** From the Python:
18 <<
19 # given a string representing one term or "tap" in a prime, returns a pair of
20 # integers representing the weight and coefficient of that tap
21 # "2 ^ y" -> [1, y]
22 # "x * 2 ^ y" -> [x, y]
23 # "x * y" -> [x*y,0]
24 # "x" -> [x,0]
25 def parse_term(t) :
26 if "*" not in t and "^" not in t:
27 return [int(t),0]
28
29 if "*" in t:
30 if len(t.split("*")) > 2: # this occurs when e.g. [w - x * y] has been turned into [w + -1 * x * y]
31 a1,a2,b = t.split("*")
32 a = int(a1) * int(a2)
33 else:
34 a,b = t.split("*")
35 if "^" not in b:
36 return [int(a) * int(b),0]
37 else:
38 a,b = (1,t)
39
40 b,e = b.split("^")
41 if int(b) != 2:
42 raise NonBase2Exception("Could not parse term, power with base other than 2: %s" %t)
43 return [int(a),int(e)]
44 >> *)
45
46 (** given an expression representing one term or "tap" in a prime,
47 returns a pair of integers representing the coefficient and weight
48 of that tap:
49 * "2 ^ y" -> Some (2^y, 1)
50 * "x * 2 ^ y" -> Some (2^y, x)
51 * "x * y" -> Some (1,x*y)
52 * "x" -> Some (1,x)
53 *)
54 Fixpoint parse_term_of_Qexpr (v : Qexpr) : option (Z * Z)
55 := match v with
56 | Qvar x => match x with end
57 | Qv x => q <- Q_to_Z_strict x; Some (1, q)
58 | Qeopp a => v <- parse_term_of_Qexpr a; Some (fst v, -snd v)
59 | Qeadd a b => None
60 | Qesub a b => None
61 | Qemul a b => b <- parse_term_of_Qexpr b;
62 a <- eval_Qexpr_strict a;
63 a <- Q_to_Z_strict a;
64 Some (fst b, a * snd b)
65 | Qediv a b => None
66 | Qepow b e => v <- eval_Qexpr_strict v; v <- Q_to_Z_strict v; Some (v, 1)
67 end%option.
68
69 Fixpoint Qexpr_to_add_list (v : Qexpr) : list Qexpr
70 := match v with
71 | Qeadd a b => Qexpr_to_add_list a ++ Qexpr_to_add_list b
72 | Qesub a b => Qexpr_to_add_list a ++ [Qeopp b]
73 | v => [v]
74 end.
75
76 (** Given a Qexpr which is a sequence of additions and subtractions,
77 we return the value of the first component, and a list of the taps
78 for the negation of the other components *)
79 Definition parse_prime_and_taps_of_Qexpr (v : Qexpr) : option (Z * list (Z * Z))
80 := match Qexpr_to_add_list v with
81 | nil => None
82 | cons p taps
83 => taps <- Option.List.lift (List.map (fun v => parse_term_of_Qexpr (Qeopp v)) taps);
84 p <- eval_Qexpr_strict p;
85 p <- Q_to_Z_strict p;
86 Some (p, taps)
87 end.
88
89 Definition parseZ_arith_to_taps (s : string) : option (Z * list (Z * Z))
90 := v <- parseQexpr_arith s; parse_prime_and_taps_of_Qexpr v.
91
92 Local Coercion QArith_base.inject_Z : Z >-> Q.
93 Local Example parse_v25519 : parse_prime_and_taps_of_Qexpr (2^255 - 19) = Some (2^255, [(1,19)]) := eq_refl.
94 Local Example parse_25519 : parseZ_arith_to_taps "2^255 - 19" = Some (2^255, [(1,19)]) := eq_refl.
95 Local Example parse_p521 : parseZ_arith_to_taps "2^521 - 1" = Some (2^521, [(1,1)]) := eq_refl.
96 Local Example parse_p448 : parseZ_arith_to_taps "2^448 - 2^224 - 1" = Some (2^448, [(2^224,1); (1,1)]) := eq_refl.
97 Local Example parse_p256 : parseZ_arith_to_taps "2^256 - 2^224 + 2^192 + 2^96 - 1" = Some (2^256, [(2^224,1); (2^192,-1); (2^96,-1); (1,1)]) := eq_refl.
98 Local Example parse_p434 : parseZ_arith_to_taps "2^216 * 3^137 - 1" = Some (2^216 * 3^137, [(1,1)]) := eq_refl.