add lookup 1bit, 2bit and 3bitSigned
This commit is contained in:
parent
f9a24bd502
commit
9074dc3d7c
9 changed files with 217 additions and 0 deletions
13
zokrates_stdlib/stdlib/utils/multiplexer/lookup1bit.code
Normal file
13
zokrates_stdlib/stdlib/utils/multiplexer/lookup1bit.code
Normal file
|
@ -0,0 +1,13 @@
|
|||
// /**
|
||||
// * One-bit window lookup table using one constraint
|
||||
// */
|
||||
def main(field selector, field[2] target) -> (field):
|
||||
|
||||
// field out = if selector == 0 then target[0] else target[1] fi
|
||||
// 6 constraints
|
||||
//TODO: should be equivalent to the following implementation
|
||||
|
||||
field out = target[0] + selector*target[1] - selector*target[0]
|
||||
// 3 constraints
|
||||
|
||||
return out
|
10
zokrates_stdlib/stdlib/utils/multiplexer/lookup2bit.code
Normal file
10
zokrates_stdlib/stdlib/utils/multiplexer/lookup2bit.code
Normal file
|
@ -0,0 +1,10 @@
|
|||
// /**
|
||||
// * Two-bit window lookup table using one constraint
|
||||
// * Maps the bits `b` to a list of constant `c`
|
||||
// */
|
||||
def main(field[2] b, field[4] c) -> (field):
|
||||
|
||||
field alpha = c[1] - c[0] + (b[1] * (c[3] - c[2] - c[1] + c[0]))
|
||||
field out = alpha*b[0] + c[0] - (b[1] * (0 - c[2] + c[0]))
|
||||
|
||||
return out
|
|
@ -0,0 +1,11 @@
|
|||
import "./lookup2bit.code" as lookup
|
||||
// /**
|
||||
// * Three-bit window lookup (2bits + signature bit) in 2bit table
|
||||
// * using two constraints. Maps the bits `b` to a list of constants `c`
|
||||
// */
|
||||
def main(field[3] b, field[4] c) -> (field):
|
||||
|
||||
field alpha = lookup([b[0], b[1]], c)
|
||||
field out = alpha - 2*b[2]*alpha
|
||||
|
||||
return out
|
|
@ -0,0 +1,24 @@
|
|||
import "utils/multiplexer/lookup1bit.code" as lookup
|
||||
|
||||
def left() -> (field):
|
||||
field sel = 0 //left
|
||||
field[2] t = [0, 1]
|
||||
|
||||
0 == lookup(sel, t)
|
||||
|
||||
return 1
|
||||
|
||||
def right() -> (field):
|
||||
field sel = 1 //right
|
||||
field[2] t = [0, 1]
|
||||
|
||||
1 == lookup(sel, t)
|
||||
|
||||
return 1
|
||||
|
||||
def main() -> (field):
|
||||
|
||||
1 == left()
|
||||
1 == right()
|
||||
|
||||
return 1
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"entry_point": "./tests/bench/utils/multiplexer/lookup1bit.code",
|
||||
"tests": [
|
||||
{
|
||||
"input": {
|
||||
"values": []
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["1"]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
import "utils/multiplexer/lookup2bit.code" as lookup
|
||||
|
||||
def first() -> (field):
|
||||
field[2] sel = [0,0]
|
||||
field[4] t = [0, 1, 2 , 3]
|
||||
|
||||
0 == lookup(sel, t)
|
||||
|
||||
return 1
|
||||
|
||||
def second() -> (field):
|
||||
field[2] sel = [1,0]
|
||||
field[4] t = [0, 1, 2 , 3]
|
||||
|
||||
1 == lookup(sel, t)
|
||||
|
||||
return 1
|
||||
|
||||
def thrid() -> (field):
|
||||
field[2] sel = [0,1]
|
||||
field[4] t = [0, 1, 2 , 3]
|
||||
|
||||
2 == lookup(sel, t)
|
||||
|
||||
return 1
|
||||
|
||||
def fourth() -> (field):
|
||||
field[2] sel = [1,1]
|
||||
field[4] t = [0, 1, 2 , 3]
|
||||
|
||||
3 == lookup(sel, t)
|
||||
|
||||
return 1
|
||||
|
||||
def main() -> (field):
|
||||
|
||||
1 == first()
|
||||
1 == second()
|
||||
1 == thrid()
|
||||
1 == fourth()
|
||||
|
||||
return 1
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"entry_point": "./tests/bench/utils/multiplexer/lookup2bit.code",
|
||||
"tests": [
|
||||
{
|
||||
"input": {
|
||||
"values": []
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["1"]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
import "utils/multiplexer/lookup3bitSigned.code" as lookup
|
||||
|
||||
def first() -> (field):
|
||||
field[3] sel = [0,0,0]
|
||||
field[4] t = [0, 1, 2 , 3]
|
||||
|
||||
0 == lookup(sel, t)
|
||||
|
||||
return 1
|
||||
|
||||
def second() -> (field):
|
||||
field[3] sel = [1,0,0]
|
||||
field[4] t = [0, 1, 2 , 3]
|
||||
|
||||
1 == lookup(sel, t)
|
||||
|
||||
return 1
|
||||
|
||||
def secondNeg() -> (field):
|
||||
field[3] sel = [1,0,1]
|
||||
field[4] t = [0, 1, 2 , 3]
|
||||
|
||||
field out = 0 - 1
|
||||
out == lookup(sel, t)
|
||||
|
||||
return 1
|
||||
|
||||
def thrid() -> (field):
|
||||
field[3] sel = [0,1,0]
|
||||
field[4] t = [0, 1, 2 , 3]
|
||||
|
||||
2 == lookup(sel, t)
|
||||
|
||||
return 1
|
||||
|
||||
def thridNeg() -> (field):
|
||||
field[3] sel = [0,1,1]
|
||||
field[4] t = [0, 1, 2 , 3]
|
||||
|
||||
field out = 0 - 2
|
||||
out == lookup(sel, t)
|
||||
|
||||
return 1
|
||||
|
||||
def fourth() -> (field):
|
||||
field[3] sel = [1,1,0]
|
||||
field[4] t = [0, 1, 2 , 3]
|
||||
|
||||
3 == lookup(sel, t)
|
||||
|
||||
return 1
|
||||
|
||||
def fourthNeg() -> (field):
|
||||
field[3] sel = [1,1,1]
|
||||
field[4] t = [0, 1, 2 , 3]
|
||||
|
||||
field out = 0 - 3
|
||||
out == lookup(sel, t)
|
||||
|
||||
return 1
|
||||
|
||||
def main() -> (field):
|
||||
|
||||
1 == first()
|
||||
1 == second()
|
||||
1 == secondNeg()
|
||||
1 == thrid()
|
||||
1 == thridNeg()
|
||||
1 == fourth()
|
||||
1 == fourthNeg()
|
||||
|
||||
return 1
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"entry_point": "./tests/bench/utils/multiplexer/lookup3bitSigned.code",
|
||||
"tests": [
|
||||
{
|
||||
"input": {
|
||||
"values": []
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["1"]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in a new issue