Rational numbers library – Frac.aes #712
@ -8,6 +8,15 @@ executors:
|
||||
working_directory: ~/aesophia
|
||||
|
||||
jobs:
|
||||
verify_rebar_lock:
|
||||
executor: aebuilder
|
||||
steps:
|
||||
- checkout
|
||||
- run:
|
||||
name: Ensure lock file is up-to-date
|
||||
command: |
|
||||
./rebar3 upgrade
|
||||
git diff --quiet -- rebar.lock || (echo "rebar.lock is not up-to-date" && exit 1)
|
||||
build:
|
||||
executor: aebuilder
|
||||
steps:
|
||||
@ -35,3 +44,10 @@ jobs:
|
||||
- _build/default/rebar3_20.3.8_plt
|
||||
- store_artifacts:
|
||||
path: _build/test/logs
|
||||
|
||||
workflows:
|
||||
version: 2
|
||||
build_test:
|
||||
jobs:
|
||||
- build
|
||||
- verify_rebar_lock
|
||||
|
68
priv/stdlib/BLS12_381.aes
Normal file
68
priv/stdlib/BLS12_381.aes
Normal file
@ -0,0 +1,68 @@
|
||||
namespace BLS12_381 =
|
||||
type fr = MCL_BLS12_381.fr
|
||||
type fp = MCL_BLS12_381.fp
|
||||
record fp2 = { x1 : fp, x2 : fp }
|
||||
record g1 = { x : fp, y : fp, z : fp }
|
||||
record g2 = { x : fp2, y : fp2, z : fp2 }
|
||||
record gt = { x1 : fp, x2 : fp, x3 : fp, x4 : fp, x5 : fp, x6 : fp,
|
||||
x7 : fp, x8 : fp, x9 : fp, x10 : fp, x11 : fp, x12 : fp }
|
||||
|
||||
function pairing_check(xs : list(g1), ys : list(g2)) =
|
||||
switch((xs, ys))
|
||||
([], []) => true
|
||||
(x :: xs, y :: ys) => pairing_check_(pairing(x, y), xs, ys)
|
||||
|
||||
function pairing_check_(acc : gt, xs : list(g1), ys : list(g2)) =
|
||||
switch((xs, ys))
|
||||
([], []) => gt_is_one(acc)
|
||||
(x :: xs, y :: ys) =>
|
||||
pairing_check_(gt_mul(acc, pairing(x, y)), xs, ys)
|
||||
|
||||
function int_to_fr(x : int) = MCL_BLS12_381.int_to_fr(x)
|
||||
function int_to_fp(x : int) = MCL_BLS12_381.int_to_fp(x)
|
||||
function fr_to_int(x : fr) = MCL_BLS12_381.fr_to_int(x)
|
||||
function fp_to_int(x : fp) = MCL_BLS12_381.fp_to_int(x)
|
||||
|
||||
function mk_g1(x : int, y : int, z : int) : g1 =
|
||||
{ x = int_to_fp(x), y = int_to_fp(y), z = int_to_fp(z) }
|
||||
|
||||
function mk_g2(x1 : int, x2 : int, y1 : int, y2 : int, z1 : int, z2 : int) : g2 =
|
||||
{ x = {x1 = int_to_fp(x1), x2 = int_to_fp(x2)},
|
||||
y = {x1 = int_to_fp(y1), x2 = int_to_fp(y2)},
|
||||
z = {x1 = int_to_fp(z1), x2 = int_to_fp(z2)} }
|
||||
|
||||
function pack_g1(t) = switch(t)
|
||||
(x, y, z) => {x = x, y = y, z = z} : g1
|
||||
function pack_g2(t) = switch(t)
|
||||
((x1, x2), (y1, y2), (z1, z2)) =>
|
||||
{x = {x1 = x1, x2 = x2}, y = {x1 = y1, x2 = y2}, z = {x1 = z1, x2 = z2}} : g2
|
||||
function pack_gt(t) = switch(t)
|
||||
(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12) =>
|
||||
{x1 = x1, x2 = x2, x3 = x3, x4 = x4, x5 = x5, x6 = x6,
|
||||
x7 = x7, x8 = x8, x9 = x9, x10 = x10, x11 = x11, x12 = x12} : gt
|
||||
|
||||
function g1_neg(p : g1) = pack_g1(MCL_BLS12_381.g1_neg((p.x, p.y, p.z)))
|
||||
function g1_norm(p : g1) = pack_g1(MCL_BLS12_381.g1_norm((p.x, p.y, p.z)))
|
||||
function g1_valid(p : g1) = MCL_BLS12_381.g1_valid((p.x, p.y, p.z))
|
||||
function g1_is_zero(p : g1) = MCL_BLS12_381.g1_is_zero((p.x, p.y, p.z))
|
||||
function g1_add(p : g1, q : g1) = pack_g1(MCL_BLS12_381.g1_add((p.x, p.y, p.z), (q.x, q.y, q.z)))
|
||||
function g1_mul(k : fr, p : g1) = pack_g1(MCL_BLS12_381.g1_mul(k, (p.x, p.y, p.z)))
|
||||
|
||||
function g2_neg(p : g2) = pack_g2(MCL_BLS12_381.g2_neg(((p.x.x1, p.x.x2), (p.y.x1, p.y.x2), (p.z.x1, p.z.x2))))
|
||||
function g2_norm(p : g2) = pack_g2(MCL_BLS12_381.g2_norm(((p.x.x1, p.x.x2), (p.y.x1, p.y.x2), (p.z.x1, p.z.x2))))
|
||||
function g2_valid(p : g2) = MCL_BLS12_381.g2_valid(((p.x.x1, p.x.x2), (p.y.x1, p.y.x2), (p.z.x1, p.z.x2)))
|
||||
function g2_is_zero(p : g2) = MCL_BLS12_381.g2_is_zero(((p.x.x1, p.x.x2), (p.y.x1, p.y.x2), (p.z.x1, p.z.x2)))
|
||||
function g2_add(p : g2, q : g2) = pack_g2(MCL_BLS12_381.g2_add(((p.x.x1, p.x.x2), (p.y.x1, p.y.x2), (p.z.x1, p.z.x2)),
|
||||
((q.x.x1, q.x.x2), (q.y.x1, q.y.x2), (q.z.x1, q.z.x2))))
|
||||
function g2_mul(k : fr, p : g2) = pack_g2(MCL_BLS12_381.g2_mul(k, ((p.x.x1, p.x.x2), (p.y.x1, p.y.x2), (p.z.x1, p.z.x2))))
|
||||
|
||||
function gt_inv(p : gt) = pack_gt(MCL_BLS12_381.gt_inv((p.x1, p.x2, p.x3, p.x4, p.x5, p.x6, p.x7, p.x8, p.x9, p.x10, p.x11, p.x12)))
|
||||
function gt_add(p : gt, q : gt) = pack_gt(MCL_BLS12_381.gt_add((p.x1, p.x2, p.x3, p.x4, p.x5, p.x6, p.x7, p.x8, p.x9, p.x10, p.x11, p.x12),
|
||||
(q.x1, q.x2, q.x3, q.x4, q.x5, q.x6, q.x7, q.x8, q.x9, q.x10, q.x11, q.x12)))
|
||||
function gt_mul(p : gt, q : gt) = pack_gt(MCL_BLS12_381.gt_mul((p.x1, p.x2, p.x3, p.x4, p.x5, p.x6, p.x7, p.x8, p.x9, p.x10, p.x11, p.x12),
|
||||
(q.x1, q.x2, q.x3, q.x4, q.x5, q.x6, q.x7, q.x8, q.x9, q.x10, q.x11, q.x12)))
|
||||
function gt_pow(p : gt, k : fr) = pack_gt(MCL_BLS12_381.gt_pow((p.x1, p.x2, p.x3, p.x4, p.x5, p.x6, p.x7, p.x8, p.x9, p.x10, p.x11, p.x12), k))
|
||||
function gt_is_one(p : gt) = MCL_BLS12_381.gt_is_one((p.x1, p.x2, p.x3, p.x4, p.x5, p.x6, p.x7, p.x8, p.x9, p.x10, p.x11, p.x12))
|
||||
function pairing(p : g1, q : g2) = pack_gt(MCL_BLS12_381.pairing((p.x, p.y, p.z), ((q.x.x1, q.x.x2), (q.y.x1, q.y.x2), (q.z.x1, q.z.x2))))
|
||||
function miller_loop(p : g1, q : g2) = pack_gt(MCL_BLS12_381.miller_loop((p.x, p.y, p.z), ((q.x.x1, q.x.x2), (q.y.x1, q.y.x2), (q.z.x1, q.z.x2))))
|
||||
function final_exp(p : gt) = pack_gt(MCL_BLS12_381.final_exp((p.x1, p.x2, p.x3, p.x4, p.x5, p.x6, p.x7, p.x8, p.x9, p.x10, p.x11, p.x12)))
|
171
priv/stdlib/Frac.aes
Normal file
171
priv/stdlib/Frac.aes
Normal file
@ -0,0 +1,171 @@
|
||||
namespace Frac =
|
||||
|
||||
private function gcd(a : int, b : int) =
|
||||
if (b == 0) a else gcd(b, a mod b)
|
||||
|
||||
private function abs_int(a : int) = if (a < 0) -a else a
|
||||
|
||||
datatype frac = Pos(int, int) | Zero | Neg(int, int)
|
||||
|
||||
// Checks if internal representation is correct. Numerator and denominator must be positive.
|
||||
function is_sane(f : frac) : bool = switch(f)
|
||||
Pos(n, d) => n > 0 && d > 0
|
||||
Zero => true
|
||||
Neg(n, d) => n > 0 && d > 0
|
||||
|
||||
function num(f : frac) : int = switch(f)
|
||||
Pos(n, _) => n
|
||||
Neg(n, _) => -n
|
||||
Zero => 0
|
||||
|
||||
function den(f : frac) : int = switch(f)
|
||||
Pos(_, d) => d
|
||||
Neg(_, d) => d
|
||||
Zero => 1
|
||||
|
||||
function to_pair(f : frac) : int * int = switch(f)
|
||||
Pos(n, d) => (n, d)
|
||||
Neg(n, d) => (-n, d)
|
||||
Zero => (0, 1)
|
||||
|
||||
function sign(f : frac) : int = switch(f)
|
||||
Pos(_, _) => 1
|
||||
Neg(_, _) => -1
|
||||
Zero => 0
|
||||
|
||||
function to_str(f : frac) : string = switch(f)
|
||||
Pos(n, d) => String.concat(Int.to_str(n), if (d == 1) "" else String.concat("/", Int.to_str(d)))
|
||||
Neg(n, d) => String.concat("-", to_str(Pos(n, d)))
|
||||
Zero => "0"
|
||||
|
||||
// Reduce fraction to normal form
|
||||
function simplify(f : frac) : frac =
|
||||
switch(f)
|
||||
Neg(n, d) =>
|
||||
let cd = gcd(n, d)
|
||||
Neg(n / cd, d / cd)
|
||||
Zero => Zero
|
||||
Pos(n, d) =>
|
||||
let cd = gcd(n, d)
|
||||
Pos(n / cd, d / cd)
|
||||
|
||||
function make_frac(n : int, d : int) : frac =
|
||||
if (d == 0) abort("Division by zero")
|
||||
elif (n == 0) Zero
|
||||
elif ((n < 0) == (d < 0)) simplify(Pos(abs_int(n), abs_int(d)))
|
||||
else simplify(Neg(abs_int(n), abs_int(d)))
|
||||
|
||||
function eq(a : frac, b : frac) : bool =
|
||||
let na = num(a)
|
||||
let nb = num(b)
|
||||
let da = den(a)
|
||||
let db = den(b)
|
||||
(na == nb && da == db) || na * db == nb * da // they are more likely to be normalized
|
||||
|
||||
function neq(a : frac, b : frac) : bool =
|
||||
let na = num(a)
|
||||
let nb = num(b)
|
||||
let da = den(a)
|
||||
let db = den(b)
|
||||
(na != nb || da != db) && na * db != nb * da
|
||||
|
||||
function geq(a : frac, b : frac) : bool = num(a) * den(b) >= num(b) * den(a)
|
||||
|
||||
function leq(a : frac, b : frac) : bool = num(a) * den(b) =< num(b) * den(a)
|
||||
|
||||
function gt(a : frac, b : frac) : bool = num(a) * den(b) > num(b) * den(a)
|
||||
|
||||
function lt(a : frac, b : frac) : bool = num(a) * den(b) < num(b) * den(a)
|
||||
|
||||
function min(a : frac, b : frac) : frac = if (leq(a, b)) a else b
|
||||
|
||||
function max(a : frac, b : frac) : frac = if (geq(a, b)) a else b
|
||||
|
||||
function abs(f : frac) : frac = switch(f)
|
||||
Pos(n, d) => Pos(n, d)
|
||||
Zero => Zero
|
||||
Neg(n, d) => Pos(n, d)
|
||||
|
||||
function from_int(n : int) : frac =
|
||||
if (n > 0) Pos(n, 1)
|
||||
elif (n < 0) Neg(-n, 1)
|
||||
else Zero
|
||||
|
||||
function floor(f : frac) : int = switch(f)
|
||||
Pos(n, d) => n / d
|
||||
Zero => 0
|
||||
Neg(n, d) => -(n + d - 1) / d
|
||||
|
||||
function ceil(f : frac) : int = switch(f)
|
||||
Pos(n, d) => (n + d - 1) / d
|
||||
Zero => 0
|
||||
Neg(n, d) => -n / d
|
||||
|
||||
function round_to_zero(f : frac) : int = switch(f)
|
||||
Pos(n, d) => n / d
|
||||
Zero => 0
|
||||
Neg(n, d) => -n / d
|
||||
|
||||
function round_from_zero(f : frac) : int = switch(f)
|
||||
Pos(n, d) => (n + d - 1) / d
|
||||
Zero => 0
|
||||
Neg(n, d) => -(n + d - 1) / d
|
||||
|
||||
// Round towards nearest integer. If two integers are in the same distance, choose the even one.
|
||||
function round(f : frac) : int =
|
||||
let fl = floor(f)
|
||||
let cl = ceil(f)
|
||||
let dif_fl = abs(sub(f, from_int(fl)))
|
||||
let dif_cl = abs(sub(f, from_int(cl)))
|
||||
if (gt(dif_fl, dif_cl)) cl
|
||||
elif (gt(dif_cl, dif_fl)) fl
|
||||
elif (fl mod 2 == 0) fl
|
||||
else cl
|
||||
|
||||
function add(a : frac, b : frac) : frac =
|
||||
let na = num(a)
|
||||
let nb = num(b)
|
||||
let da = den(a)
|
||||
let db = den(b)
|
||||
if (da == db) make_frac(na + nb, da)
|
||||
else make_frac(na * db + nb * da, da * db)
|
||||
|
||||
function neg(a : frac) : frac = switch(a)
|
||||
Neg(n, d) => Pos(n, d)
|
||||
Zero => Zero
|
||||
Pos(n, d) => Neg(n, d)
|
||||
|
||||
function sub(a : frac, b : frac) : frac = add(a, neg(b))
|
||||
|
||||
function inv(a : frac) : frac = switch(a)
|
||||
Neg(n, d) => Neg(d, n)
|
||||
Zero => abort("Inversion of zero")
|
||||
Pos(n, d) => Pos(d, n)
|
||||
|
||||
function mul(a : frac, b : frac) : frac = make_frac(num(a) * num(b), den(a) * den(b))
|
||||
|
||||
function div(a : frac, b : frac) : frac = mul(a, inv(b))
|
||||
|
||||
function int_exp(b : frac, e : int) : frac =
|
||||
if (sign(b) == 0 && e == 0) abort("Zero to the zero exponentation")
|
||||
elif (e < 0) inv(int_exp_(b, -e))
|
||||
else int_exp_(b, e)
|
||||
private function int_exp_(b : frac, e : int) =
|
||||
if (e == 0) from_int(1)
|
||||
elif (e == 1) b
|
||||
else
|
||||
let half = int_exp_(b, e / 2)
|
||||
if (e mod 2 == 1) mul(mul(half, half), b)
|
||||
else mul(half, half)
|
||||
|
||||
// Reduces the fraction's in-memory size by dividing its components by two until the
|
||||
// the error is bigger than `loss` value
|
||||
function optimize(f : frac, loss : frac) : frac =
|
||||
require(geq(loss, Zero), "negative loss optimize")
|
||||
let s = sign(f)
|
||||
mul(from_int(s), run_optimize(abs(f), loss))
|
||||
private function run_optimize(f : frac, loss : frac) : frac =
|
||||
let t = make_frac((num(f) + 1) / 2, (den(f) + 1)/2)
|
||||
if(gt(abs(sub(t, f)), loss)) f
|
||||
elif (eq(t, f)) f
|
||||
else run_optimize(t, loss)
|
@ -19,18 +19,28 @@ namespace List =
|
||||
[x] => Some(x)
|
||||
_::t => last(t)
|
||||
|
||||
function drop_last(l : list('a)) : option(list('a)) = switch(l)
|
||||
[] => None
|
||||
_ => Some(drop_last_unsafe(l))
|
||||
|
||||
function drop_last_unsafe(l : list('a)) : list('a) = switch(l)
|
||||
[_] => []
|
||||
h::t => h::drop_last_unsafe(t)
|
||||
[] => abort("drop_last_unsafe: list empty")
|
||||
|
||||
function find(p : 'a => bool, l : list('a)) : option('a) = switch(l)
|
||||
[] => None
|
||||
h::t => if(p(h)) Some(h) else find(p, t)
|
||||
|
||||
function find_indices(p : 'a => bool, l : list('a)) : list(int) = find_indices_(p, l, 0, [])
|
||||
function find_indices(p : 'a => bool, l : list('a)) : list(int) = find_indices_(p, l, 0)
|
||||
private function find_indices_( p : 'a => bool
|
||||
, l : list('a)
|
||||
, n : int
|
||||
, acc : list(int)
|
||||
) : list(int) = switch(l)
|
||||
[] => reverse(acc)
|
||||
h::t => find_indices_(p, t, n+1, if(p(h)) n::acc else acc)
|
||||
[] => []
|
||||
h::t =>
|
||||
let rest = find_indices_(p, t, n+1)
|
||||
if(p(h)) n::rest else rest
|
||||
|
||||
function nth(n : int, l : list('a)) : option('a) =
|
||||
switch(l)
|
||||
@ -52,39 +62,40 @@ namespace List =
|
||||
|
||||
function from_to(a : int, b : int) : list(int) = [a..b]
|
||||
|
||||
function from_to_step(a : int, b : int, s : int) : list(int) = from_to_step_(a, b, s, [])
|
||||
private function from_to_step_(a, b, s, acc) =
|
||||
if (a > b) reverse(acc) else from_to_step_(a + s, b, s, a :: acc)
|
||||
function from_to_step(a : int, b : int, s : int) : list(int) =
|
||||
from_to_step_(a, b - (b-a) mod s, s, [])
|
||||
private function from_to_step_(a : int, b : int, s : int, acc : list(int)) : list(int) =
|
||||
if(b < a) acc
|
||||
else from_to_step_(a, b - s, s, b::acc)
|
||||
|
||||
|
||||
|
||||
/* Unsafe. Replaces `n`th element of `l` with `e`. Crashes on over/underflow */
|
||||
function replace_at(n : int, e : 'a, l : list('a)) : list('a) =
|
||||
if(n<0) abort("insert_at underflow") else replace_at_(n, e, l, [])
|
||||
private function replace_at_(n : int, e : 'a, l : list('a), acc : list('a)) : list('a) =
|
||||
if(n<0) abort("insert_at underflow") else replace_at_(n, e, l)
|
||||
private function replace_at_(n : int, e : 'a, l : list('a)) : list('a) =
|
||||
switch(l)
|
||||
[] => abort("replace_at overflow")
|
||||
h::t => if (n == 0) reverse(e::acc) ++ t
|
||||
else replace_at_(n-1, e, t, h::acc)
|
||||
h::t => if (n == 0) e::t
|
||||
else h::replace_at_(n-1, e, t)
|
||||
|
||||
/* Unsafe. Adds `e` to `l` to be its `n`th element. Crashes on over/underflow */
|
||||
function insert_at(n : int, e : 'a, l : list('a)) : list('a) =
|
||||
if(n<0) abort("insert_at underflow") else insert_at_(n, e, l, [])
|
||||
private function insert_at_(n : int, e : 'a, l : list('a), acc : list('a)) : list('a) =
|
||||
if (n == 0) reverse(e::acc) ++ l
|
||||
if(n<0) abort("insert_at underflow") else insert_at_(n, e, l)
|
||||
private function insert_at_(n : int, e : 'a, l : list('a)) : list('a) =
|
||||
if (n == 0) e::l
|
||||
else switch(l)
|
||||
[] => abort("insert_at overflow")
|
||||
h::t => insert_at_(n-1, e, t, h::acc)
|
||||
h::t => h::insert_at_(n-1, e, t)
|
||||
|
||||
function insert_by(cmp : (('a, 'a) => bool), x : 'a, l : list('a)) : list('a) =
|
||||
insert_by_(cmp, x, l, [])
|
||||
private function insert_by_(cmp : (('a, 'a) => bool), x : 'a, l : list('a), acc : list('a)) : list('a) =
|
||||
switch(l)
|
||||
[] => reverse(x::acc)
|
||||
[] => [x]
|
||||
h::t =>
|
||||
if(cmp(x, h)) // x < h
|
||||
reverse(acc) ++ (x::l)
|
||||
x::l
|
||||
else
|
||||
insert_by_(cmp, x, t, h::acc)
|
||||
h::insert_by(cmp, x, t)
|
||||
|
||||
|
||||
function foldr(cons : ('a, 'b) => 'b, nil : 'b, l : list('a)) : 'b = switch(l)
|
||||
@ -102,43 +113,46 @@ namespace List =
|
||||
f(e)
|
||||
foreach(l', f)
|
||||
|
||||
function reverse(l : list('a)) : list('a) = foldl((lst, el) => el :: lst, [], l)
|
||||
function reverse(l : list('a)) : list('a) = reverse_(l, [])
|
||||
private function reverse_(l : list('a), acc : list('a)) : list('a) = switch(l)
|
||||
[] => acc
|
||||
h::t => reverse_(t, h::acc)
|
||||
|
||||
function map(f : 'a => 'b, l : list('a)) : list('b) = map_(f, l, [])
|
||||
private function map_(f : 'a => 'b, l : list('a), acc : list('b)) : list('b) = switch(l)
|
||||
[] => reverse(acc)
|
||||
h::t => map_(f, t, f(h)::acc)
|
||||
function map(f : 'a => 'b, l : list('a)) : list('b) = switch(l)
|
||||
[] => []
|
||||
h::t => f(h)::map(f, t)
|
||||
|
||||
function flat_map(f : 'a => list('b), l : list('a)) : list('b) =
|
||||
ListInternal.flat_map(f, l)
|
||||
|
||||
function filter(p : 'a => bool, l : list('a)) : list('a) = filter_(p, l, [])
|
||||
private function filter_(p : 'a => bool, l : list('a), acc : list('a)) : list('a) = switch(l)
|
||||
[] => reverse(acc)
|
||||
h::t => filter_(p, t, if(p(h)) h::acc else acc)
|
||||
function filter(p : 'a => bool, l : list('a)) : list('a) = switch(l)
|
||||
[] => []
|
||||
h::t =>
|
||||
let rest = filter(p, t)
|
||||
if(p(h)) h::rest else rest
|
||||
|
||||
/* Take `n` first elements */
|
||||
/* Take up to `n` first elements */
|
||||
function take(n : int, l : list('a)) : list('a) =
|
||||
if(n < 0) abort("Take negative number of elements") else take_(n, l, [])
|
||||
private function take_(n : int, l : list('a), acc : list('a)) : list('a) =
|
||||
if(n == 0) reverse(acc)
|
||||
else switch(l)
|
||||
[] => reverse(acc)
|
||||
h::t => take_(n-1, t, h::acc)
|
||||
|
||||
/* Drop `n` first elements */
|
||||
function drop(n : int, l : list('a)) : list('a) =
|
||||
if(n < 0) abort("Drop negative number of elements")
|
||||
elif (n == 0) l
|
||||
if(n < 0) abort("Take negative number of elements") else take_(n, l)
|
||||
private function take_(n : int, l : list('a)) : list('a) =
|
||||
if(n == 0) []
|
||||
else switch(l)
|
||||
[] => []
|
||||
h::t => drop(n-1, t)
|
||||
h::t => h::take_(n-1, t)
|
||||
|
||||
/* Drop up to `n` first elements */
|
||||
function drop(n : int, l : list('a)) : list('a) =
|
||||
if(n < 0) abort("Drop negative number of elements") else drop_(n, l)
|
||||
private function drop_(n : int, l : list('a)) : list('a) =
|
||||
if (n == 0) l
|
||||
else switch(l)
|
||||
[] => []
|
||||
h::t => drop_(n-1, t)
|
||||
|
||||
/* Get the longest prefix of a list in which every element matches predicate `p` */
|
||||
function take_while(p : 'a => bool, l : list('a)) : list('a) = take_while_(p, l, [])
|
||||
private function take_while_(p : 'a => bool, l : list('a), acc : list('a)) : list('a) = switch(l)
|
||||
[] => reverse(acc)
|
||||
h::t => if(p(h)) take_while_(p, t, h::acc) else reverse(acc)
|
||||
function take_while(p : 'a => bool, l : list('a)) : list('a) = switch(l)
|
||||
[] => []
|
||||
h::t => if(p(h)) h::take_while(p, t) else []
|
||||
|
||||
/* Drop elements from `l` until `p` holds */
|
||||
function drop_while(p : 'a => bool, l : list('a)) : list('a) = switch(l)
|
||||
@ -146,17 +160,15 @@ namespace List =
|
||||
h::t => if(p(h)) drop_while(p, t) else l
|
||||
|
||||
/* Splits list into two lists of elements that respectively match and don't match predicate `p` */
|
||||
function partition(p : 'a => bool, l : list('a)) : (list('a) * list('a)) = partition_(p, l, [], [])
|
||||
private function partition_( p : 'a => bool
|
||||
, l : list('a)
|
||||
, acc_t : list('a)
|
||||
, acc_f : list('a)
|
||||
) : (list('a) * list('a)) = switch(l)
|
||||
[] => (reverse(acc_t), reverse(acc_f))
|
||||
h::t => if(p(h)) partition_(p, t, h::acc_t, acc_f) else partition_(p, t, acc_t, h::acc_f)
|
||||
function partition(p : 'a => bool, l : list('a)) : (list('a) * list('a)) = switch(l)
|
||||
[] => ([], [])
|
||||
h::t =>
|
||||
let (l, r) = partition(p, t)
|
||||
if(p(h)) (h::l, r) else (l, h::r)
|
||||
|
||||
|
||||
function flatten(ll : list(list('a))) : list('a) = foldr((l1, l2) => l1 ++ l2, [], ll)
|
||||
function flatten(l : list(list('a))) : list('a) = switch(l)
|
||||
[] => []
|
||||
h::t => h ++ flatten(t)
|
||||
|
||||
function all(p : 'a => bool, l : list('a)) : bool = switch(l)
|
||||
[] => true
|
||||
@ -172,25 +184,21 @@ namespace List =
|
||||
|
||||
|
||||
/* Zips two list by applying bimapping function on respective elements. Drops longer tail. */
|
||||
function zip_with(f : ('a, 'b) => 'c, l1 : list('a), l2 : list('b)) : list('c) = zip_with_(f, l1, l2, [])
|
||||
private function zip_with_( f : ('a, 'b) => 'c
|
||||
private function zip_with( f : ('a, 'b) => 'c
|
||||
, l1 : list('a)
|
||||
, l2 : list('b)
|
||||
, acc : list('c)
|
||||
) : list('c) = switch ((l1, l2))
|
||||
(h1::t1, h2::t2) => zip_with_(f, t1, t2, f(h1, h2)::acc)
|
||||
_ => reverse(acc)
|
||||
(h1::t1, h2::t2) => f(h1, h2)::zip_with(f, t1, t2)
|
||||
_ => []
|
||||
|
||||
/* Zips two lists into list of pairs. Drops longer tail. */
|
||||
function zip(l1 : list('a), l2 : list('b)) : list('a * 'b) = zip_with((a, b) => (a, b), l1, l2)
|
||||
|
||||
function unzip(l : list('a * 'b)) : list('a) * list('b) = unzip_(l, [], [])
|
||||
private function unzip_( l : list('a * 'b)
|
||||
, acc_l : list('a)
|
||||
, acc_r : list('b)
|
||||
) : (list('a) * list('b)) = switch(l)
|
||||
[] => (reverse(acc_l), reverse(acc_r))
|
||||
(left, right)::t => unzip_(t, left::acc_l, right::acc_r)
|
||||
function unzip(l : list('a * 'b)) : (list('a) * list('b)) = switch(l)
|
||||
[] => ([], [])
|
||||
(h1, h2)::t =>
|
||||
let (t1, t2) = unzip(t)
|
||||
(h1::t1, h2::t2)
|
||||
|
||||
|
||||
// TODO: Improve?
|
||||
@ -200,15 +208,14 @@ namespace List =
|
||||
(lesser, bigger) => sort(lesser_cmp, lesser) ++ h::sort(lesser_cmp, bigger)
|
||||
|
||||
|
||||
function intersperse(delim : 'a, l : list('a)) : list('a) = intersperse_(delim, l, [])
|
||||
private function intersperse_(delim : 'a, l : list('a), acc : list('a)) : list('a) = switch(l)
|
||||
[] => reverse(acc)
|
||||
[e] => reverse(e::acc)
|
||||
h::t => intersperse_(delim, t, delim::h::acc)
|
||||
function intersperse(delim : 'a, l : list('a)) : list('a) = switch(l)
|
||||
[] => []
|
||||
[e] => [e]
|
||||
h::t => h::delim::intersperse(delim, t)
|
||||
|
||||
|
||||
function enumerate(l : list('a)) : list(int * 'a) = enumerate_(l, 0, [])
|
||||
private function enumerate_(l : list('a), n : int, acc : list(int * 'a)) : list(int * 'a) = switch(l)
|
||||
[] => reverse(acc)
|
||||
h::t => enumerate_(t, n + 1, (n, h)::acc)
|
||||
function enumerate(l : list('a)) : list(int * 'a) = enumerate_(l, 0)
|
||||
private function enumerate_(l : list('a), n : int) : list(int * 'a) = switch(l)
|
||||
[] => []
|
||||
h::t => (n, h)::enumerate_(t, n + 1)
|
||||
|
||||
|
@ -53,17 +53,17 @@ namespace Option =
|
||||
None => []
|
||||
Some(x) => [x]
|
||||
|
||||
function filter_options(l : list(option('a))) : list('a) = filter_options_(l, [])
|
||||
private function filter_options_(l : list (option('a)), acc : list('a)) : list('a) = switch(l)
|
||||
[] => List.reverse(acc)
|
||||
None::t => filter_options_(t, acc)
|
||||
Some(x)::t => filter_options_(t, x::acc)
|
||||
function filter_options(l : list(option('a))) : list('a) = switch(l)
|
||||
[] => []
|
||||
None::t => filter_options(t)
|
||||
Some(x)::t => x::filter_options(t)
|
||||
|
||||
function seq_options(l : list (option('a))) : option (list('a)) = seq_options_(l, [])
|
||||
private function seq_options_(l : list (option('a)), acc : list('a)) : option(list('a)) = switch(l)
|
||||
[] => Some(List.reverse(acc))
|
||||
None::t => None
|
||||
Some(x)::t => seq_options_(t, x::acc)
|
||||
function seq_options(l : list (option('a))) : option (list('a)) = switch(l)
|
||||
[] => Some([])
|
||||
None::_ => None
|
||||
Some(x)::t => switch(seq_options(t))
|
||||
None => None
|
||||
Some(st) => Some(x::st)
|
||||
|
||||
|
||||
function choose(o1 : option('a), o2 : option('a)) : option('a) =
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
{erl_opts, [debug_info]}.
|
||||
|
||||
{deps, [ {aebytecode, {git, "https://github.com/aeternity/aebytecode.git", {ref,"4f4d6d3"}}}
|
||||
{deps, [ {aebytecode, {git, "https://github.com/aeternity/aebytecode.git", {ref,"8a9c9de"}}}
|
||||
, {getopt, "1.0.1"}
|
||||
, {eblake2, "1.0.0"}
|
||||
, {jsx, {git, "https://github.com/talentdeficit/jsx.git",
|
||||
|
@ -1,7 +1,7 @@
|
||||
{"1.1.0",
|
||||
[{<<"aebytecode">>,
|
||||
{git,"https://github.com/aeternity/aebytecode.git",
|
||||
{ref,"4f4d6d30cd2c46b3830454d650a424d513f69134"}},
|
||||
{ref,"8a9c9dec956b1d2322b38490b2759e53a91affab"}},
|
||||
0},
|
||||
{<<"aeserialization">>,
|
||||
{git,"https://github.com/aeternity/aeserialization.git",
|
||||
|
@ -389,6 +389,15 @@ global_env() ->
|
||||
Signature = {named_arg_t, Ann, SignId, SignId, {typed, Ann, SignDef, SignId}},
|
||||
SignFun = fun(Ts, T) -> {type_sig, [stateful|Ann], none, [Signature], Ts, T} end,
|
||||
TTL = {qid, Ann, ["Chain", "ttl"]},
|
||||
Pointee = {qid, Ann, ["AENS", "pointee"]},
|
||||
AENSName = {qid, Ann, ["AENS", "name"]},
|
||||
Fr = {qid, Ann, ["MCL_BLS12_381", "fr"]},
|
||||
Fp = {qid, Ann, ["MCL_BLS12_381", "fp"]},
|
||||
Fp2 = {tuple_t, Ann, [Fp, Fp]},
|
||||
G1 = {tuple_t, Ann, [Fp, Fp, Fp]},
|
||||
G2 = {tuple_t, Ann, [Fp2, Fp2, Fp2]},
|
||||
GT = {tuple_t, Ann, lists:duplicate(12, Fp)},
|
||||
|
||||
Fee = Int,
|
||||
[A, Q, R, K, V] = lists:map(TVar, ["a", "q", "r", "k", "v"]),
|
||||
|
||||
@ -447,6 +456,7 @@ global_env() ->
|
||||
OracleScope = #scope
|
||||
{ funs = MkDefs(
|
||||
[{"register", SignFun([Address, Fee, TTL], Oracle(Q, R))},
|
||||
{"expiry", Fun([Oracle(Q, R)], Fee)},
|
||||
{"query_fee", Fun([Oracle(Q, R)], Fee)},
|
||||
{"query", StateFun([Oracle(Q, R), Q, Fee, TTL, TTL], Query(Q, R))},
|
||||
{"get_question", Fun([Oracle(Q, R), Query(Q, R)], Q)},
|
||||
@ -462,7 +472,18 @@ global_env() ->
|
||||
{"preclaim", SignFun([Address, Hash], Unit)},
|
||||
{"claim", SignFun([Address, String, Int, Int], Unit)},
|
||||
{"transfer", SignFun([Address, Address, String], Unit)},
|
||||
{"revoke", SignFun([Address, String], Unit)}]) },
|
||||
{"revoke", SignFun([Address, String], Unit)},
|
||||
{"update", SignFun([Address, String, Option(TTL), Option(Int), Option(Map(String, Pointee))], Unit)},
|
||||
{"lookup", Fun([String], option_t(Ann, AENSName))},
|
||||
%% AENS pointee constructors
|
||||
{"AccountPt", Fun1(Address, Pointee)},
|
||||
{"OraclePt", Fun1(Address, Pointee)},
|
||||
{"ContractPt", Fun1(Address, Pointee)},
|
||||
{"ChannelPt", Fun1(Address, Pointee)},
|
||||
%% Name object constructor
|
||||
{"Name", Fun([Address, TTL, Map(String, Pointee)], AENSName)}
|
||||
])
|
||||
, types = MkDefs([{"pointee", 0}, {"name", 0}]) },
|
||||
|
||||
MapScope = #scope
|
||||
{ funs = MkDefs(
|
||||
@ -485,6 +506,40 @@ global_env() ->
|
||||
{"sha256", Fun1(A, Hash)},
|
||||
{"blake2b", Fun1(A, Hash)}]) },
|
||||
|
||||
%% Fancy BLS12-381 crypto operations
|
||||
MCL_BLS12_381_Scope = #scope
|
||||
{ funs = MkDefs(
|
||||
[{"g1_neg", Fun1(G1, G1)},
|
||||
{"g1_norm", Fun1(G1, G1)},
|
||||
{"g1_valid", Fun1(G1, Bool)},
|
||||
{"g1_is_zero", Fun1(G1, Bool)},
|
||||
{"g1_add", Fun ([G1, G1], G1)},
|
||||
{"g1_mul", Fun ([Fr, G1], G1)},
|
||||
|
||||
{"g2_neg", Fun1(G2, G2)},
|
||||
{"g2_norm", Fun1(G2, G2)},
|
||||
{"g2_valid", Fun1(G2, Bool)},
|
||||
{"g2_is_zero", Fun1(G2, Bool)},
|
||||
{"g2_add", Fun ([G2, G2], G2)},
|
||||
{"g2_mul", Fun ([Fr, G2], G2)},
|
||||
|
||||
{"gt_inv", Fun1(GT, GT)},
|
||||
{"gt_add", Fun ([GT, GT], GT)},
|
||||
{"gt_mul", Fun ([GT, GT], GT)},
|
||||
{"gt_pow", Fun ([GT, Fr], GT)},
|
||||
{"gt_is_one", Fun1(GT, Bool)},
|
||||
{"pairing", Fun ([G1, G2], GT)},
|
||||
{"miller_loop", Fun ([G1, G2], GT)},
|
||||
{"final_exp", Fun1(GT, GT)},
|
||||
|
||||
{"int_to_fr", Fun1(Int, Fr)},
|
||||
{"int_to_fp", Fun1(Int, Fp)},
|
||||
{"fr_to_int", Fun1(Fr, Int)},
|
||||
{"fp_to_int", Fun1(Fp, Int)}
|
||||
]),
|
||||
types = MkDefs(
|
||||
[{"fr", 0}, {"fp", 0}]) },
|
||||
|
||||
%% Authentication
|
||||
AuthScope = #scope
|
||||
{ funs = MkDefs(
|
||||
@ -539,12 +594,15 @@ global_env() ->
|
||||
, ["Map"] => MapScope
|
||||
, ["Auth"] => AuthScope
|
||||
, ["Crypto"] => CryptoScope
|
||||
, ["MCL_BLS12_381"] => MCL_BLS12_381_Scope
|
||||
, ["String"] => StringScope
|
||||
, ["Bits"] => BitsScope
|
||||
, ["Bytes"] => BytesScope
|
||||
, ["Int"] => IntScope
|
||||
, ["Address"] => AddressScope
|
||||
} }.
|
||||
}
|
||||
}.
|
||||
|
||||
|
||||
option_t(As, T) -> {app_t, As, {id, As, "option"}, [T]}.
|
||||
map_t(As, K, V) -> {app_t, As, {id, As, "map"}, [K, V]}.
|
||||
|
@ -36,7 +36,14 @@
|
||||
bits_intersection | bits_union | bits_difference |
|
||||
contract_to_address | address_to_contract | crypto_verify_sig | crypto_verify_sig_secp256k1 |
|
||||
crypto_sha3 | crypto_sha256 | crypto_blake2b |
|
||||
crypto_ecverify_secp256k1 | crypto_ecrecover_secp256k1.
|
||||
crypto_ecverify_secp256k1 | crypto_ecrecover_secp256k1 |
|
||||
mcl_bls12_381_g1_neg | mcl_bls12_381_g1_norm | mcl_bls12_381_g1_valid |
|
||||
mcl_bls12_381_g1_is_zero | mcl_bls12_381_g1_add | mcl_bls12_381_g1_mul |
|
||||
mcl_bls12_381_g2_neg | mcl_bls12_381_g2_norm | mcl_bls12_381_g2_valid |
|
||||
mcl_bls12_381_g2_is_zero | mcl_bls12_381_g2_add | mcl_bls12_381_g2_mul |
|
||||
mcl_bls12_381_gt_inv | mcl_bls12_381_gt_add | mcl_bls12_381_gt_mul | mcl_bls12_381_gt_pow |
|
||||
mcl_bls12_381_gt_is_one | mcl_bls12_381_pairing | mcl_bls12_381_miller_loop | mcl_bls12_381_final_exp |
|
||||
mcl_bls12_381_int_to_fr | mcl_bls12_381_int_to_fp | mcl_bls12_381_fr_to_int | mcl_bls12_381_fp_to_int.
|
||||
|
||||
-type flit() :: {int, integer()}
|
||||
| {string, binary()}
|
||||
@ -177,7 +184,12 @@ init_env(Options) ->
|
||||
con_env => #{["None"] => #con_tag{ tag = 0, arities = [0, 1] },
|
||||
["Some"] => #con_tag{ tag = 1, arities = [0, 1] },
|
||||
["RelativeTTL"] => #con_tag{ tag = 0, arities = [1, 1] },
|
||||
["FixedTTL"] => #con_tag{ tag = 1, arities = [1, 1] }
|
||||
["FixedTTL"] => #con_tag{ tag = 1, arities = [1, 1] },
|
||||
["AENS", "AccountPt"] => #con_tag{ tag = 0, arities = [1, 1, 1, 1] },
|
||||
["AENS", "OraclePt"] => #con_tag{ tag = 1, arities = [1, 1, 1, 1] },
|
||||
["AENS", "ContractPt"] => #con_tag{ tag = 2, arities = [1, 1, 1, 1] },
|
||||
["AENS", "ChannelPt"] => #con_tag{ tag = 3, arities = [1, 1, 1, 1] },
|
||||
["AENS", "Name"] => #con_tag{ tag = 0, arities = [3] }
|
||||
},
|
||||
options => Options,
|
||||
functions => #{} }.
|
||||
@ -194,16 +206,21 @@ builtins() ->
|
||||
{["Contract"], [{"address", none}, {"balance", none}, {"creator", none}]},
|
||||
{["Call"], [{"origin", none}, {"caller", none}, {"value", none}, {"gas_price", none},
|
||||
{"gas_left", 0}]},
|
||||
{["Oracle"], [{"register", 4}, {"query_fee", 1}, {"query", 5}, {"get_question", 2},
|
||||
{["Oracle"], [{"register", 4}, {"expiry", 1}, {"query_fee", 1}, {"query", 5}, {"get_question", 2},
|
||||
{"respond", 4}, {"extend", 3}, {"get_answer", 2},
|
||||
{"check", 1}, {"check_query", 2}]},
|
||||
{["AENS"], [{"resolve", 2}, {"preclaim", 3}, {"claim", 5}, {"transfer", 4},
|
||||
{"revoke", 3}]},
|
||||
{"revoke", 3}, {"update", 6}, {"lookup", 1}]},
|
||||
{["Map"], [{"from_list", 1}, {"to_list", 1}, {"lookup", 2},
|
||||
{"lookup_default", 3}, {"delete", 2}, {"member", 2}, {"size", 1}]},
|
||||
{["Crypto"], [{"verify_sig", 3}, {"verify_sig_secp256k1", 3},
|
||||
{"ecverify_secp256k1", 3}, {"ecrecover_secp256k1", 2},
|
||||
{"sha3", 1}, {"sha256", 1}, {"blake2b", 1}]},
|
||||
{["MCL_BLS12_381"], [{"g1_neg", 1}, {"g1_norm", 1}, {"g1_valid", 1}, {"g1_is_zero", 1}, {"g1_add", 2}, {"g1_mul", 2},
|
||||
{"g2_neg", 1}, {"g2_norm", 1}, {"g2_valid", 1}, {"g2_is_zero", 1}, {"g2_add", 2}, {"g2_mul", 2},
|
||||
{"gt_inv", 1}, {"gt_add", 2}, {"gt_mul", 2}, {"gt_pow", 2}, {"gt_is_one", 1},
|
||||
{"pairing", 2}, {"miller_loop", 2}, {"final_exp", 1},
|
||||
{"int_to_fr", 1}, {"int_to_fp", 1}, {"fr_to_int", 1}, {"fp_to_int", 1}]},
|
||||
{["Auth"], [{"tx_hash", none}]},
|
||||
{["String"], [{"length", 1}, {"concat", 2}, {"sha3", 1}, {"sha256", 1}, {"blake2b", 1}]},
|
||||
{["Bits"], [{"set", 2}, {"clear", 2}, {"test", 2}, {"sum", 1}, {"intersection", 2},
|
||||
@ -233,11 +250,15 @@ init_type_env() ->
|
||||
["hash"] => ?type(hash),
|
||||
["signature"] => ?type(signature),
|
||||
["oracle"] => ?type(Q, R, {oracle, Q, R}),
|
||||
["oracle_query"] => ?type(_, _, oracle_query),
|
||||
["oracle_query"] => ?type(_, _, oracle_query), %% TODO: not in Fate
|
||||
["list"] => ?type(T, {list, T}),
|
||||
["map"] => ?type(K, V, {map, K, V}),
|
||||
["option"] => ?type(T, {variant, [[], [T]]}),
|
||||
["Chain", "ttl"] => ?type({variant, [[integer], [integer]]})
|
||||
["Chain", "ttl"] => ?type({variant, [[integer], [integer]]}),
|
||||
["AENS", "pointee"] => ?type({variant, [[address], [address], [address], [address]]}),
|
||||
["AENS", "name"] => ?type({variant, [[address, {variant, [[integer], [integer]]}, {map, string, {variant, [[address], [address], [address], [address]]}}]]}),
|
||||
["MCL_BLS12_381", "fr"] => ?type({bytes, 32}),
|
||||
["MCL_BLS12_381", "fp"] => ?type({bytes, 48})
|
||||
}.
|
||||
|
||||
is_no_code(Env) ->
|
||||
@ -991,7 +1012,14 @@ op_builtins() ->
|
||||
bits_difference, int_to_str, address_to_str, crypto_verify_sig,
|
||||
address_to_contract,
|
||||
crypto_verify_sig_secp256k1, crypto_sha3, crypto_sha256, crypto_blake2b,
|
||||
crypto_ecverify_secp256k1, crypto_ecrecover_secp256k1
|
||||
crypto_ecverify_secp256k1, crypto_ecrecover_secp256k1,
|
||||
mcl_bls12_381_g1_neg, mcl_bls12_381_g1_norm, mcl_bls12_381_g1_valid,
|
||||
mcl_bls12_381_g1_is_zero, mcl_bls12_381_g1_add, mcl_bls12_381_g1_mul,
|
||||
mcl_bls12_381_g2_neg, mcl_bls12_381_g2_norm, mcl_bls12_381_g2_valid,
|
||||
mcl_bls12_381_g2_is_zero, mcl_bls12_381_g2_add, mcl_bls12_381_g2_mul,
|
||||
mcl_bls12_381_gt_inv, mcl_bls12_381_gt_add, mcl_bls12_381_gt_mul, mcl_bls12_381_gt_pow,
|
||||
mcl_bls12_381_gt_is_one, mcl_bls12_381_pairing, mcl_bls12_381_miller_loop, mcl_bls12_381_final_exp,
|
||||
mcl_bls12_381_int_to_fr, mcl_bls12_381_int_to_fp, mcl_bls12_381_fr_to_int, mcl_bls12_381_fp_to_int
|
||||
].
|
||||
|
||||
set_state({reg, R}, Val) ->
|
||||
|
@ -469,6 +469,7 @@ is_builtin_fun({qid, _, ["AENS", "preclaim"]}, _Icode) ->
|
||||
is_builtin_fun({qid, _, ["AENS", "claim"]}, _Icode) -> true;
|
||||
is_builtin_fun({qid, _, ["AENS", "transfer"]}, _Icode) -> true;
|
||||
is_builtin_fun({qid, _, ["AENS", "revoke"]}, _Icode) -> true;
|
||||
is_builtin_fun({qid, _, ["AENS", "update"]}, _Icode) -> true;
|
||||
is_builtin_fun({qid, _, ["Map", "lookup"]}, _Icode) -> true;
|
||||
is_builtin_fun({qid, _, ["Map", "lookup_default"]}, _Icode) -> true;
|
||||
is_builtin_fun({qid, _, ["Map", "member"]}, _Icode) -> true;
|
||||
@ -624,6 +625,12 @@ builtin_code(_, {qid, _, ["AENS", "revoke"]}, Args, _, _, Icode) ->
|
||||
[ast_body(Addr, Icode), ast_body(Name, Icode), ast_body(Sign, Icode)],
|
||||
[word, word, sign_t()], {tuple, []});
|
||||
|
||||
builtin_code(_, {qid, _, ["AENS", "update"]}, Args, _, _, Icode) ->
|
||||
{Sign, [Addr, Name, TTL, ClientTTL, Pointers]} = get_signature_arg(Args),
|
||||
prim_call(?PRIM_CALL_AENS_UPDATE, #integer{value = 0},
|
||||
[ast_body(Addr, Icode), ast_body(Name, Icode), ast_body(TTL, Icode), ast_body(ClientTTL, Icode), ast_body(Pointers, Icode), ast_body(Sign, Icode)],
|
||||
[word, string, word, word, word, sign_t()], {tuple, []});
|
||||
|
||||
%% -- Maps
|
||||
%% -- lookup functions
|
||||
builtin_code(_, {qid, _, ["Map", "lookup"]}, [Key, Map], _, _, Icode) ->
|
||||
@ -932,6 +939,9 @@ ast_typerep1({variant_t, Cons}, Icode) ->
|
||||
ttl_t(Icode) ->
|
||||
ast_typerep({qid, [], ["Chain", "ttl"]}, Icode).
|
||||
|
||||
%% pointee_t(Icode) ->
|
||||
%% ast_typerep({qid, [], ["AENS", "pointee"]}, Icode).
|
||||
|
||||
sign_t() -> bytes_t(64).
|
||||
bytes_t(Len) when Len =< 32 -> word;
|
||||
bytes_t(Len) -> {tuple, lists:duplicate((31 + Len) div 32, word)}.
|
||||
|
@ -498,6 +498,8 @@ builtin_to_scode(_Env, call_gas_left, []) ->
|
||||
[aeb_fate_ops:gas(?a)];
|
||||
builtin_to_scode(Env, oracle_register, [_Sign,_Account,_QFee,_TTL,_QType,_RType] = Args) ->
|
||||
call_to_scode(Env, aeb_fate_ops:oracle_register(?a, ?a, ?a, ?a, ?a, ?a, ?a), Args);
|
||||
builtin_to_scode(Env, oracle_expiry, [_Oracle] = Args) ->
|
||||
call_to_scode(Env, aeb_fate_ops:oracle_expiry(?a, ?a), Args);
|
||||
builtin_to_scode(Env, oracle_query_fee, [_Oracle] = Args) ->
|
||||
call_to_scode(Env, aeb_fate_ops:oracle_query_fee(?a, ?a), Args);
|
||||
builtin_to_scode(Env, oracle_query, [_Oracle, _Question, _QFee, _QTTL, _RTTL, _QType, _RType] = Args) ->
|
||||
@ -536,6 +538,11 @@ builtin_to_scode(Env, aens_transfer, [_Sign, _From, _To, _Name] = Args) ->
|
||||
builtin_to_scode(Env, aens_revoke, [_Sign, _Account, _Name] = Args) ->
|
||||
call_to_scode(Env, [aeb_fate_ops:aens_revoke(?a, ?a, ?a),
|
||||
tuple(0)], Args);
|
||||
builtin_to_scode(Env, aens_update, [_Sign, _Account, _NameString, _TTL, _ClientTTL, _Pointers] = Args) ->
|
||||
call_to_scode(Env, [aeb_fate_ops:aens_update(?a, ?a, ?a, ?a, ?a, ?a),
|
||||
tuple(0)], Args);
|
||||
builtin_to_scode(Env, aens_lookup, [_Name] = Args) ->
|
||||
call_to_scode(Env, aeb_fate_ops:aens_lookup(?a, ?a), Args);
|
||||
builtin_to_scode(_Env, auth_tx_hash, []) ->
|
||||
[aeb_fate_ops:auth_tx_hash(?a)].
|
||||
|
||||
@ -586,7 +593,31 @@ op_to_scode(crypto_sha256) -> aeb_fate_ops:sha256(?a, ?a);
|
||||
op_to_scode(crypto_blake2b) -> aeb_fate_ops:blake2b(?a, ?a);
|
||||
op_to_scode(string_sha3) -> aeb_fate_ops:sha3(?a, ?a);
|
||||
op_to_scode(string_sha256) -> aeb_fate_ops:sha256(?a, ?a);
|
||||
op_to_scode(string_blake2b) -> aeb_fate_ops:blake2b(?a, ?a).
|
||||
op_to_scode(string_blake2b) -> aeb_fate_ops:blake2b(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_g1_neg) -> aeb_fate_ops:bls12_381_g1_neg(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_g1_norm) -> aeb_fate_ops:bls12_381_g1_norm(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_g1_valid) -> aeb_fate_ops:bls12_381_g1_valid(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_g1_is_zero) -> aeb_fate_ops:bls12_381_g1_is_zero(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_g1_add) -> aeb_fate_ops:bls12_381_g1_add(?a, ?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_g1_mul) -> aeb_fate_ops:bls12_381_g1_mul(?a, ?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_g2_neg) -> aeb_fate_ops:bls12_381_g2_neg(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_g2_norm) -> aeb_fate_ops:bls12_381_g2_norm(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_g2_valid) -> aeb_fate_ops:bls12_381_g2_valid(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_g2_is_zero) -> aeb_fate_ops:bls12_381_g2_is_zero(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_g2_add) -> aeb_fate_ops:bls12_381_g2_add(?a, ?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_g2_mul) -> aeb_fate_ops:bls12_381_g2_mul(?a, ?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_gt_inv) -> aeb_fate_ops:bls12_381_gt_inv(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_gt_add) -> aeb_fate_ops:bls12_381_gt_add(?a, ?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_gt_mul) -> aeb_fate_ops:bls12_381_gt_mul(?a, ?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_gt_pow) -> aeb_fate_ops:bls12_381_gt_pow(?a, ?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_gt_is_one) -> aeb_fate_ops:bls12_381_gt_is_one(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_pairing) -> aeb_fate_ops:bls12_381_pairing(?a, ?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_miller_loop) -> aeb_fate_ops:bls12_381_miller_loop(?a, ?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_final_exp) -> aeb_fate_ops:bls12_381_final_exp(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_int_to_fr) -> aeb_fate_ops:bls12_381_int_to_fr(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_int_to_fp) -> aeb_fate_ops:bls12_381_int_to_fp(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_fr_to_int) -> aeb_fate_ops:bls12_381_fr_to_int(?a, ?a);
|
||||
op_to_scode(mcl_bls12_381_fp_to_int) -> aeb_fate_ops:bls12_381_fp_to_int(?a, ?a).
|
||||
|
||||
%% PUSH and STORE ?a are the same, so we use STORE to make optimizations
|
||||
%% easier, and specialize to PUSH (which is cheaper) at the end.
|
||||
@ -853,12 +884,38 @@ attributes(I) ->
|
||||
{'ORACLE_GET_ANSWER', A, B, C, D, E} -> Pure(A, [B, C, D, E]);
|
||||
{'ORACLE_GET_QUESTION', A, B, C, D, E}-> Pure(A, [B, C, D, E]);
|
||||
{'ORACLE_QUERY_FEE', A, B} -> Pure(A, [B]);
|
||||
{'AENS_RESOLVE', A, B, C, D} -> Pure(A, [B, C, D]);
|
||||
{'ORACLE_EXPIRY', A, B} -> Impure(A, [B]);
|
||||
{'AENS_RESOLVE', A, B, C, D} -> Impure(A, [B, C, D]);
|
||||
{'AENS_PRECLAIM', A, B, C} -> Impure(none, [A, B, C]);
|
||||
{'AENS_CLAIM', A, B, C, D, E} -> Impure(none, [A, B, C, D, E]);
|
||||
'AENS_UPDATE' -> Impure(none, []);%% TODO
|
||||
{'AENS_UPDATE', A, B, C, D, E, F} -> Impure(none, [A, B, C, D, E, F]);
|
||||
{'AENS_TRANSFER', A, B, C, D} -> Impure(none, [A, B, C, D]);
|
||||
{'AENS_REVOKE', A, B, C} -> Impure(none, [A, B, C]);
|
||||
{'AENS_LOOKUP', A, B} -> Impure(A, [B]);
|
||||
{'BLS12_381_G1_NEG', A, B} -> Pure(A, [B]);
|
||||
{'BLS12_381_G1_NORM', A, B} -> Pure(A, [B]);
|
||||
{'BLS12_381_G1_VALID', A, B} -> Pure(A, [B]);
|
||||
{'BLS12_381_G1_IS_ZERO', A, B} -> Pure(A, [B]);
|
||||
{'BLS12_381_G1_ADD', A, B, C} -> Pure(A, [B, C]);
|
||||
{'BLS12_381_G1_MUL', A, B, C} -> Pure(A, [B, C]);
|
||||
{'BLS12_381_G2_NEG', A, B} -> Pure(A, [B]);
|
||||
{'BLS12_381_G2_NORM', A, B} -> Pure(A, [B]);
|
||||
{'BLS12_381_G2_VALID', A, B} -> Pure(A, [B]);
|
||||
{'BLS12_381_G2_IS_ZERO', A, B} -> Pure(A, [B]);
|
||||
{'BLS12_381_G2_ADD', A, B, C} -> Pure(A, [B, C]);
|
||||
{'BLS12_381_G2_MUL', A, B, C} -> Pure(A, [B, C]);
|
||||
{'BLS12_381_GT_INV', A, B} -> Pure(A, [B]);
|
||||
{'BLS12_381_GT_ADD', A, B, C} -> Pure(A, [B, C]);
|
||||
{'BLS12_381_GT_MUL', A, B, C} -> Pure(A, [B, C]);
|
||||
{'BLS12_381_GT_POW', A, B, C} -> Pure(A, [B, C]);
|
||||
{'BLS12_381_GT_IS_ONE', A, B} -> Pure(A, [B]);
|
||||
{'BLS12_381_PAIRING', A, B, C} -> Pure(A, [B, C]);
|
||||
{'BLS12_381_MILLER_LOOP', A, B, C} -> Pure(A, [B, C]);
|
||||
{'BLS12_381_FINAL_EXP', A, B} -> Pure(A, [B]);
|
||||
{'BLS12_381_INT_TO_FR', A, B} -> Pure(A, [B]);
|
||||
{'BLS12_381_INT_TO_FP', A, B} -> Pure(A, [B]);
|
||||
{'BLS12_381_FR_TO_INT', A, B} -> Pure(A, [B]);
|
||||
{'BLS12_381_FP_TO_INT', A, B} -> Pure(A, [B]);
|
||||
{'ABORT', A} -> Impure(pc, A);
|
||||
{'EXIT', A} -> Impure(pc, A);
|
||||
'NOP' -> Pure(none, [])
|
||||
|
@ -88,13 +88,18 @@ builtin_types() ->
|
||||
, "option" => fun([A]) -> {variant, [[], [A]]} end
|
||||
, "map" => fun([K, V]) -> map_typerep(K, V) end
|
||||
, ["Chain", "ttl"] => fun([]) -> {variant, [[word], [word]]} end
|
||||
, ["AENS", "pointee"] => fun([]) -> {variant, [[word], [word], [word]]} end
|
||||
}.
|
||||
|
||||
builtin_constructors() ->
|
||||
#{ ["RelativeTTL"] => 0
|
||||
, ["FixedTTL"] => 1
|
||||
, ["None"] => 0
|
||||
, ["Some"] => 1 }.
|
||||
, ["Some"] => 1
|
||||
, ["AccountPointee"] => 0
|
||||
, ["OraclePointee"] => 1
|
||||
, ["ContractPointee"] => 2
|
||||
}.
|
||||
|
||||
map_typerep(K, V) ->
|
||||
{map, K, V}.
|
||||
@ -146,4 +151,3 @@ get_constructor_tag(Name, #{constructors := Constructors}) ->
|
||||
undefined -> error({undefined_constructor, Name});
|
||||
Tag -> Tag
|
||||
end.
|
||||
|
||||
|
@ -154,6 +154,7 @@ compilable_contracts() ->
|
||||
"bytes_to_x",
|
||||
"bytes_concat",
|
||||
"aens",
|
||||
"aens_update",
|
||||
"tuple_match",
|
||||
"cyclic_include",
|
||||
"stdlib_include",
|
||||
@ -163,13 +164,14 @@ compilable_contracts() ->
|
||||
"payable",
|
||||
"unapplied_builtins",
|
||||
"underscore_number_literals",
|
||||
"pairing_crypto",
|
||||
"qualified_constructor",
|
||||
"let_patterns",
|
||||
"lhs_matching"
|
||||
].
|
||||
|
||||
not_yet_compilable(fate) -> [];
|
||||
not_yet_compilable(aevm) -> [].
|
||||
not_yet_compilable(aevm) -> ["pairing_crypto", "aens_update"].
|
||||
|
||||
%% Contracts that should produce type errors
|
||||
|
||||
|
@ -33,7 +33,22 @@ contract AENSTest =
|
||||
sign : signature) : unit =
|
||||
AENS.claim(addr, name, salt, name_fee, signature = sign)
|
||||
|
||||
// TODO: update() -- how to handle pointers?
|
||||
|
||||
stateful entrypoint update(owner : address,
|
||||
name : string,
|
||||
ttl : option(Chain.ttl),
|
||||
client_ttl : option(int),
|
||||
pointers : option(map(string, AENS.pointee))) : unit =
|
||||
AENS.update(owner, name, ttl, client_ttl, pointers)
|
||||
|
||||
stateful entrypoint signedUpdate(owner : address,
|
||||
name : string,
|
||||
ttl : option(Chain.ttl),
|
||||
client_ttl : option(int),
|
||||
pointers : option(map(string, AENS.pointee)),
|
||||
sign : signature) : unit =
|
||||
AENS.update(owner, name, ttl, client_ttl, pointers, signature = sign)
|
||||
|
||||
|
||||
stateful entrypoint transfer(owner : address,
|
||||
new_owner : address,
|
||||
|
17
test/contracts/aens_update.aes
Normal file
17
test/contracts/aens_update.aes
Normal file
@ -0,0 +1,17 @@
|
||||
contract AENSUpdate =
|
||||
stateful entrypoint update_name(owner : address, name : string) =
|
||||
let p1 : AENS.pointee = AENS.AccountPt(Call.caller)
|
||||
let p2 : AENS.pointee = AENS.OraclePt(Call.caller)
|
||||
let p3 : AENS.pointee = AENS.ContractPt(Call.caller)
|
||||
let p4 : AENS.pointee = AENS.ChannelPt(Call.caller)
|
||||
AENS.update(owner, name, None, None,
|
||||
Some({ ["account_pubkey"] = p1, ["oracle_pubkey"] = p2,
|
||||
["contract_pubkey"] = p3, ["misc"] = p4 }))
|
||||
|
||||
entrypoint get_ttl(name : string) =
|
||||
switch(AENS.lookup(name))
|
||||
Some(AENS.Name(_, FixedTTL(ttl), _)) => ttl
|
||||
|
||||
entrypoint expiry(o : oracle(int, int)) : int =
|
||||
Oracle.expiry(o)
|
||||
|
@ -25,6 +25,9 @@ contract ChannelOnChainContractOracle =
|
||||
Some(_value) =>
|
||||
"bet_already_taken"
|
||||
|
||||
public function expiry() =
|
||||
Oracle.expiry(state.oracle)
|
||||
|
||||
public function query_fee() =
|
||||
Oracle.query_fee(state.oracle)
|
||||
|
||||
|
30
test/contracts/pairing_crypto.aes
Normal file
30
test/contracts/pairing_crypto.aes
Normal file
@ -0,0 +1,30 @@
|
||||
include "BLS12_381.aes"
|
||||
|
||||
contract GrothVerify =
|
||||
type fr = BLS12_381.fr
|
||||
type g1 = BLS12_381.g1
|
||||
type g2 = BLS12_381.g2
|
||||
|
||||
record proof = { a : g1, b : g2, c : g1 }
|
||||
record verify_key = { a : g1, b : g2, c : g2, d : g2, ic : list(g1) }
|
||||
|
||||
record state = { vk : verify_key }
|
||||
|
||||
entrypoint init(vk0 : verify_key) = {vk = vk0}
|
||||
|
||||
entrypoint verify_proof(p : proof, input : list(fr)) =
|
||||
let vk = state.vk
|
||||
let vk_x = calc_vk_x(vk.ic, input)
|
||||
|
||||
BLS12_381.pairing_check([BLS12_381.g1_neg(p.a), vk.a, vk_x, p.c],
|
||||
[p.b, vk.b, vk.c, vk.d])
|
||||
|
||||
function calc_vk_x(ics : list(g1), xs : list(fr)) =
|
||||
switch(ics)
|
||||
(ic :: ics) => calc_vk_x_(ic, ics, xs)
|
||||
|
||||
function calc_vk_x_(vk_x : g1, ics : list(g1), xs : list(fr)) =
|
||||
switch((ics, xs))
|
||||
([], []) => vk_x
|
||||
(ic :: ics, x :: xs) => calc_vk_x_(BLS12_381.g1_add(vk_x, BLS12_381.g1_mul(x, ic)), ics, xs)
|
||||
|
@ -21,6 +21,7 @@ contract UnappliedBuiltins =
|
||||
function b_abort() = abort
|
||||
function b_require() = require
|
||||
function oracle_query_fee() = Oracle.query_fee
|
||||
function oracle_expiry() = Oracle.expiry
|
||||
stateful function oracle_query() = Oracle.query : (o, _, _, _, _) => _
|
||||
function oracle_get_question() = Oracle.get_question : (o, _) => _
|
||||
function oracle_get_answer() = Oracle.get_answer : (o, _) => _
|
||||
|
Loading…
x
Reference in New Issue
Block a user