Arithmetic Queries and Complex Multiplication #
A simple example showing how to use FreeM.cost with variable/parametrized query costs.
ArithQuery α supports addition, subtraction, and multiplication, each with
independently parametrized costs. Complex number multiplication provides a toy example
where two algorithms (naive and Gauss's trick) trade multiplications for additions,
and the optimal choice depends on the cost ratio.
Arithmetic queries: addition, subtraction, and multiplication.
- add {α : Type} (a b : α) : ArithQuery α α
- sub {α : Type} (a b : α) : ArithQuery α α
- mul {α : Type} (a b : α) : ArithQuery α α
Instances For
Lift ArithQuery.add a b into a FreeM that returns the sum.
Equations
Instances For
Lift ArithQuery.sub a b into a FreeM that returns the difference.
Equations
Instances For
Lift ArithQuery.mul a b into a FreeM that returns the product.
Equations
Instances For
An honest oracle interprets arithmetic queries using the actual ring operations.
Equations
- (Cslib.Query.ArithQuery.add a b).honest = a + b
- (Cslib.Query.ArithQuery.sub a b).honest = a - b
- (Cslib.Query.ArithQuery.mul a b).honest = a * b
Instances For
Weighted cost model for arithmetic queries. Subtraction costs the same as addition (both are linear-time on bignums).
Equations
- Cslib.Query.ArithQuery.weight c_add c_mul (Cslib.Query.ArithQuery.add a b) = c_add
- Cslib.Query.ArithQuery.weight c_add c_mul (Cslib.Query.ArithQuery.sub a b) = c_add
- Cslib.Query.ArithQuery.weight c_add c_mul (Cslib.Query.ArithQuery.mul a b) = c_mul
Instances For
Naive complex multiplication: (a + bi)(c + di) = (ac - bd) + (ad + bc)i.
Uses 4 multiplications, 1 subtraction, 1 addition.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Gauss's trick for complex multiplication: computes (a+b)(c+d) to save one
multiplication, at the cost of extra additions and subtractions.
Uses 3 multiplications, 2 subtractions, 3 additions.
Equations
- One or more equations did not get rendered due to their size.