proofs/tensor_indexed_certificate.bend

stelliferous@0.0.3.0 · 108 lines · 0 definitions

1import Base
2import stelliferous@0.0.3.0/tensor_indexed.bend as Indexed
3import stelliferous@0.0.3.0/traversal_array.bend as Traversal
4import stelliferous@0.0.3.0/traversal_loop.bend as Loop
5import stelliferous@0.0.3.0/proofs/storage_tree_model.bend as Tree
6import stelliferous@0.0.3.0/proofs/storage_certificate.bend as Certificate
7import stelliferous@0.0.3.0/proofs/storage_witness.bend as Witness
8import stelliferous@0.0.3.0/proofs/traversal_witness.bend as TraversalWitness
9import stelliferous@0.0.3.0/proofs/traversal_certificate.bend as TraversalCertificate
10
11def sample(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~operation: U32 -> Element -> Element,
12  -input: Array<Element>,+context: Context,+position: U32,-result: Array<Element> & Element,
13  evidence: TraversalWitness.Sample<Array<Element>,Element,input,result>) ->
14  TraversalWitness.Sample<Array<Element>,Element,input,Indexed.value(~Element,~Context,~index,~operation,context,position,result)>:
15  match evidence:
16    case TraversalWitness.Sample{value,equation}:
17      %Equal.sym(Array<Element> & Element,result,(input,value),equation) :
18        TraversalWitness.Sample<Array<Element>,Element,input,Indexed.value(~Element,~Context,~index,~operation,context,position,_)>
19      TraversalWitness.Sample{operation(index(context,position),value),{==}}
20
21def read(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~operation: U32 -> Element -> Element,
22  -input: Array<Element>,context: Context,position: U32,evidence: Witness.ArrayWitness<Element,input>) ->
23  TraversalWitness.Sample<Array<Element>,Element,input,Indexed.read(~Element,~Context,~index,~operation,input,context,position)>:
24  +position = position
25  sample(~Element,~Context,~index,~operation,input,context,position,Array.get(Element,input,position),
26    TraversalWitness.array_sample(~Element,input,Array.get(Element,input,position),Witness.observed(Element,input,position,evidence)))
27
28def preserves(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~operation: U32 -> Element -> Element,
29  +depth: Nat,call: Indexed.Call<Context>,-input: Array<Element>,-output: Array<Element>,input_certificate: Certificate.Certificate<Element,input>,
30  equation: {output == Tree.pack(Element,Certificate.normalized(Element,depth,Tree.reflect(Element,output))) : Array<Element>}) ->
31  TraversalWitness.ResultEvidence<Array<Element>,Element,input,depth,Indexed.run(~Element,~Context,~index,~operation,call,input,output)>:
32  Indexed.Call{count,context} = call
33  TraversalCertificate.completed(~Array<Element>,~Element,~Context,~TraversalWitness.array_evidence(~Element),
34    ~Traversal.run(~Array<Element>,~Element,~Context,~Indexed.read(~Element,~Context,~index,~operation),~Indexed.destination(~Context)),
35    ~TraversalWitness.run(~Array<Element>,~Element,~Context,~TraversalWitness.array_evidence(~Element),~Indexed.read(~Element,~Context,~index,~operation),
36      ~Indexed.destination(~Context),~read(~Element,~Context,~index,~operation)),
37    count,0,context,input,output,Certificate.witness(Element,input,input_certificate),depth,equation)
38
39def next_block(~Element: Data,~Context: Data,+depth: Nat,block: U32,size: U32,context: Context,-input: Array<Element>,-state: Traversal.State<&1,&1,Array<Element>,Array<Element>,Indexed.Block>,
40  evidence: TraversalWitness.StateEvidence<Array<Element>,Element,Indexed.Block,input,depth,state>) ->
41  TraversalWitness.StateEvidence<Array<Element>,Element,Indexed.Blocks<Context>,input,depth,Indexed.next_block(~Element,~Context,block,size,context,state)>:
42  match evidence:
43    case TraversalWitness.StateEvidence{output,position,inner,equation,output_evidence}:
44      %Equal.sym(Traversal.State<&1,&1,Array<Element>,Array<Element>,Indexed.Block>,state,Traversal.State{input,output,position,inner},equation) :
45        TraversalWitness.StateEvidence<Array<Element>,Element,Indexed.Blocks<Context>,input,depth,Indexed.next_block(~Element,~Context,block,size,context,_)>
46      TraversalWitness.StateEvidence{output,position,Indexed.Blocks{(block + 1 : U32),size,context},{==},output_evidence}
47
48def block_step(~Element: Data,~Context: Data,~base: Context -> U32 -> U32,~operation: U32 -> Element -> Element,+depth: Nat,-input: Array<Element>,-state: Traversal.State<&1,&1,Array<Element>,Array<Element>,Indexed.Blocks<Context>>,
49  +input_evidence: Witness.ArrayWitness<Element,input>,evidence: TraversalWitness.StateEvidence<Array<Element>,Element,Indexed.Blocks<Context>,input,depth,state>) ->
50  TraversalWitness.StateEvidence<Array<Element>,Element,Indexed.Blocks<Context>,input,depth,Indexed.block_step(~Element,~Context,~base,~operation,state)>:
51  match evidence:
52    case TraversalWitness.StateEvidence{output,+position,Indexed.Blocks{+block,+size,+context},equation,output_evidence}:
53      %Equal.sym(Traversal.State<&1,&1,Array<Element>,Array<Element>,Indexed.Blocks<Context>>,state,Traversal.State{input,output,position,Indexed.Blocks{block,size,context}},equation) :
54        TraversalWitness.StateEvidence<Array<Element>,Element,Indexed.Blocks<Context>,input,depth,Indexed.block_step(~Element,~Context,~base,~operation,_)>
55      next_block(~Element,~Context,depth,block,size,context,input,
56        Traversal.run(~Array<Element>,~Element,~Indexed.Block,~Indexed.read(~Element,~Indexed.Block,~Indexed.block_index,~operation),~Indexed.destination(~Indexed.Block),U32.to_nat(size),
57          Traversal.State{input,output,position,Indexed.Block{base(context,block),position}}),
58        TraversalWitness.run(~Array<Element>,~Element,~Indexed.Block,~TraversalWitness.array_evidence(~Element),~Indexed.read(~Element,~Indexed.Block,~Indexed.block_index,~operation),
59          ~Indexed.destination(~Indexed.Block),~read(~Element,~Indexed.Block,~Indexed.block_index,~operation),depth,U32.to_nat(size),input,
60          Traversal.State{input,output,position,Indexed.Block{base(context,block),position}},input_evidence,
61          TraversalWitness.StateEvidence{output,position,Indexed.Block{base(context,block),position},{==},output_evidence}))
62
63def block_loop(~Element: Data,~Context: Data,~base: Context -> U32 -> U32,~operation: U32 -> Element -> Element,+depth: Nat,count: Nat,-input: Array<Element>,-state: Traversal.State<&1,&1,Array<Element>,Array<Element>,Indexed.Blocks<Context>>,
64  +input_evidence: Witness.ArrayWitness<Element,input>,evidence: TraversalWitness.StateEvidence<Array<Element>,Element,Indexed.Blocks<Context>,input,depth,state>) ->
65  TraversalWitness.StateEvidence<Array<Element>,Element,Indexed.Blocks<Context>,input,depth,
66    Loop.run(~Traversal.State<&1,&1,Array<Element>,Array<Element>,Indexed.Blocks<Context>>,~Indexed.block_step(~Element,~Context,~base,~operation),count,state)>:
67  match count:
68    case 0n: evidence
69    case 1n+rest:
70      block_loop(~Element,~Context,~base,~operation,depth,rest,input,Indexed.block_step(~Element,~Context,~base,~operation,state),input_evidence,
71        block_step(~Element,~Context,~base,~operation,depth,input,state,input_evidence,evidence))
72
73type Tiling<-Context: Data> is Data:
74  Tiling{total: U32,context: Context}
75
76def tiled_operation(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~base: Context -> U32 -> U32,~blocks: Context -> U32,~size: Context -> U32,~operation: U32 -> Element -> Element,
77  tiling: Tiling<Context>,input: Array<Element>,output: Array<Element>) -> Array<Element> & Array<Element>:
78  Tiling{total,context} = tiling
79  Indexed.run_tiled(~Element,~Context,~index,~base,~blocks,~size,~operation,total,context,input,output)
80
81def whole_preserves(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~base: Context -> U32 -> U32,~operation: U32 -> Element -> Element,
82  +depth: Nat,whole: Bool,blocks: U32,size: U32,total: U32,context: Context,-input: Array<Element>,-output: Array<Element>,input_certificate: Certificate.Certificate<Element,input>,
83  equation: {output == Tree.pack(Element,Certificate.normalized(Element,depth,Tree.reflect(Element,output))) : Array<Element>}) ->
84  TraversalWitness.ResultEvidence<Array<Element>,Element,input,depth,Indexed.run_whole(~Element,~Context,~index,~base,~operation,whole,blocks,size,total,context,input,output)>:
85  match whole:
86    case True{}:
87      TraversalCertificate.completed(~Array<Element>,~Element,~Indexed.Blocks<Context>,~TraversalWitness.array_evidence(~Element),
88        ~Loop.run(~Traversal.State<&1,&1,Array<Element>,Array<Element>,Indexed.Blocks<Context>>,~Indexed.block_step(~Element,~Context,~base,~operation)),~block_loop(~Element,~Context,~base,~operation),
89        U32.to_nat(blocks),0,Indexed.Blocks{0,size,context},input,output,Certificate.witness(Element,input,input_certificate),depth,equation)
90    case False{}:
91      preserves(~Element,~Context,~index,~operation,depth,Indexed.Call{U32.to_nat(total),context},input,output,input_certificate,equation)
92
93def tiled_preserves(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~base: Context -> U32 -> U32,~blocks: Context -> U32,~size: Context -> U32,~operation: U32 -> Element -> Element,
94  +depth: Nat,tiling: Tiling<Context>,-input: Array<Element>,-output: Array<Element>,input_certificate: Certificate.Certificate<Element,input>,
95  equation: {output == Tree.pack(Element,Certificate.normalized(Element,depth,Tree.reflect(Element,output))) : Array<Element>}) ->
96  TraversalWitness.ResultEvidence<Array<Element>,Element,input,depth,tiled_operation(~Element,~Context,~index,~base,~blocks,~size,~operation,tiling,input,output)>:
97  Tiling{+total,+context} = tiling
98  whole_preserves(~Element,~Context,~index,~base,~operation,depth,U32.is_le(total,Indexed.block_limit()) && Nat.is_eq(Nat.mul(U32.to_nat(blocks(context)),U32.to_nat(size(context))),U32.to_nat(total)),
99    blocks(context),size(context),total,context,input,output,input_certificate,equation)
100
101def tiled_certificates(~Element: Data,~Context: Data,~index: Context -> U32 -> U32,~base: Context -> U32 -> U32,~blocks: Context -> U32,~size: Context -> U32,~operation: U32 -> Element -> Element,
102  +total: U32,+context: Context,-input: Array<Element>,-output: Array<Element>,input_certificate: Certificate.Certificate<Element,input>,
103  output_certificate: Certificate.Certificate<Element,output>) ->
104  TraversalCertificate.Certificates<Array<Element>,Element,Certificate.family(~Element),Indexed.run_tiled(~Element,~Context,~index,~base,~blocks,~size,~operation,total,context,input,output)>:
105  Certificate.Certificate{+depth,+equation} = input_certificate
106  TraversalCertificate.operation(~Array<Element>,~Element,~Tiling<Context>,~Certificate.family(~Element),
107    ~tiled_operation(~Element,~Context,~index,~base,~blocks,~size,~operation),~tiled_preserves(~Element,~Context,~index,~base,~blocks,~size,~operation),
108    Tiling{total,context},input,output,Certificate.Certificate{depth,equation},Certificate.Certificate{depth,equation},Certificate.Certificate{depth,equation},output_certificate)