Merge pull request #819 from Zokrates/update-rotr
Update bit rotation logic in sha256 and blake2s, add bit rotation tests
This commit is contained in:
commit
aa8e603b11
10 changed files with 266 additions and 94 deletions
46
zokrates_core_test/tests/tests/left_rotation.json
Normal file
46
zokrates_core_test/tests/tests/left_rotation.json
Normal file
|
@ -0,0 +1,46 @@
|
|||
{
|
||||
"entry_point": "./tests/tests/left_rotation.zok",
|
||||
"max_constraint_count": 34,
|
||||
"tests": [
|
||||
{
|
||||
"input": {
|
||||
"values": ["0"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["0"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"values": ["1"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["4"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"values": ["42"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["168"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"values": ["2147483658"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["42"]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
5
zokrates_core_test/tests/tests/left_rotation.zok
Normal file
5
zokrates_core_test/tests/tests/left_rotation.zok
Normal file
|
@ -0,0 +1,5 @@
|
|||
def rotl32<N>(u32 x) -> u32:
|
||||
return ((x << N) | (x >> (32 - N)))
|
||||
|
||||
def main(u32 i) -> u32:
|
||||
return rotl32::<2>(i)
|
46
zokrates_core_test/tests/tests/left_rotation_bits.json
Normal file
46
zokrates_core_test/tests/tests/left_rotation_bits.json
Normal file
|
@ -0,0 +1,46 @@
|
|||
{
|
||||
"entry_point": "./tests/tests/left_rotation_bits.zok",
|
||||
"max_constraint_count": 34,
|
||||
"tests": [
|
||||
{
|
||||
"input": {
|
||||
"values": ["0"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["0"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"values": ["1"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["4"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"values": ["42"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["168"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"values": ["2147483658"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["42"]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
9
zokrates_core_test/tests/tests/left_rotation_bits.zok
Normal file
9
zokrates_core_test/tests/tests/left_rotation_bits.zok
Normal file
|
@ -0,0 +1,9 @@
|
|||
import "EMBED/u32_to_bits" as to_bits
|
||||
import "EMBED/u32_from_bits" as from_bits
|
||||
|
||||
def rotl32<N>(u32 e) -> u32:
|
||||
bool[32] b = to_bits(e)
|
||||
return from_bits([...b[N..], ...b[..N]])
|
||||
|
||||
def main(u32 i) -> u32:
|
||||
return rotl32::<2>(i)
|
46
zokrates_core_test/tests/tests/right_rotation.json
Normal file
46
zokrates_core_test/tests/tests/right_rotation.json
Normal file
|
@ -0,0 +1,46 @@
|
|||
{
|
||||
"entry_point": "./tests/tests/right_rotation.zok",
|
||||
"max_constraint_count": 34,
|
||||
"tests": [
|
||||
{
|
||||
"input": {
|
||||
"values": ["0"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["0"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"values": ["1"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["1073741824"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"values": ["42"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["2147483658"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"values": ["2147483658"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["2684354562"]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
5
zokrates_core_test/tests/tests/right_rotation.zok
Normal file
5
zokrates_core_test/tests/tests/right_rotation.zok
Normal file
|
@ -0,0 +1,5 @@
|
|||
def rotr32<N>(u32 x) -> u32:
|
||||
return (x >> N) | (x << (32 - N))
|
||||
|
||||
def main(u32 i) -> u32:
|
||||
return rotr32::<2>(i)
|
46
zokrates_core_test/tests/tests/right_rotation_bits.json
Normal file
46
zokrates_core_test/tests/tests/right_rotation_bits.json
Normal file
|
@ -0,0 +1,46 @@
|
|||
{
|
||||
"entry_point": "./tests/tests/right_rotation_bits.zok",
|
||||
"max_constraint_count": 34,
|
||||
"tests": [
|
||||
{
|
||||
"input": {
|
||||
"values": ["0"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["0"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"values": ["1"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["1073741824"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"values": ["42"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["2147483658"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"input": {
|
||||
"values": ["2147483658"]
|
||||
},
|
||||
"output": {
|
||||
"Ok": {
|
||||
"values": ["2684354562"]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
9
zokrates_core_test/tests/tests/right_rotation_bits.zok
Normal file
9
zokrates_core_test/tests/tests/right_rotation_bits.zok
Normal file
|
@ -0,0 +1,9 @@
|
|||
import "EMBED/u32_to_bits" as to_bits
|
||||
import "EMBED/u32_from_bits" as from_bits
|
||||
|
||||
def rotr32<N>(u32 e) -> u32:
|
||||
bool[32] b = to_bits(e)
|
||||
return from_bits([...b[32-N..], ...b[..32-N]])
|
||||
|
||||
def main(u32 i) -> u32:
|
||||
return rotr32::<2>(i)
|
|
@ -3,9 +3,8 @@
|
|||
import "EMBED/u32_to_bits" as to_bits
|
||||
import "EMBED/u32_from_bits" as from_bits
|
||||
|
||||
def right_rotate<N>(u32 e) -> u32:
|
||||
bool[32] b = to_bits(e)
|
||||
return from_bits([...b[32 - N..], ...b[..32 - N]])
|
||||
def rotr32<N>(u32 x) -> u32:
|
||||
return (x >> N) | (x << (32 - N))
|
||||
|
||||
def blake2s_iv() -> (u32[8]):
|
||||
return [
|
||||
|
@ -29,13 +28,13 @@ def blake2s_sigma() -> (u32[10][16]):
|
|||
|
||||
def mixing_g(u32[16] v, u32 a, u32 b, u32 c, u32 d, u32 x, u32 y) -> (u32[16]):
|
||||
v[a] = (v[a] + v[b] + x)
|
||||
v[d] = right_rotate::<16>(v[d] ^ v[a])
|
||||
v[d] = rotr32::<16>(v[d] ^ v[a])
|
||||
v[c] = (v[c] + v[d])
|
||||
v[b] = right_rotate::<12>(v[b] ^ v[c])
|
||||
v[b] = rotr32::<12>(v[b] ^ v[c])
|
||||
v[a] = (v[a] + v[b] + y)
|
||||
v[d] = right_rotate::<8>(v[d] ^ v[a])
|
||||
v[d] = rotr32::<8>(v[d] ^ v[a])
|
||||
v[c] = (v[c] + v[d])
|
||||
v[b] = right_rotate::<7>(v[b] ^ v[c])
|
||||
v[b] = rotr32::<7>(v[b] ^ v[c])
|
||||
return v
|
||||
|
||||
def blake2s_compression(u32[8] h, u32[16] m, u32[2] t, bool last) -> (u32[8]):
|
||||
|
|
|
@ -1,50 +1,11 @@
|
|||
import "EMBED/u32_to_bits" as to_bits
|
||||
import "EMBED/u32_from_bits" as from_bits
|
||||
import "./IVconstants.zok"
|
||||
|
||||
def right_rotate_2(u32 e) -> u32:
|
||||
bool[32] b = to_bits(e)
|
||||
return from_bits([...b[30..], ...b[..30]])
|
||||
|
||||
def right_rotate_6(u32 e) -> u32:
|
||||
bool[32] b = to_bits(e)
|
||||
return from_bits([...b[26..], ...b[..26]])
|
||||
|
||||
def right_rotate_7(u32 e) -> u32:
|
||||
bool[32] b = to_bits(e)
|
||||
return from_bits([...b[25..], ...b[..25]])
|
||||
|
||||
def right_rotate_11(u32 e) -> u32:
|
||||
bool[32] b = to_bits(e)
|
||||
return from_bits([...b[21..], ...b[..21]])
|
||||
|
||||
def right_rotate_13(u32 e) -> u32:
|
||||
bool[32] b = to_bits(e)
|
||||
return from_bits([...b[19..], ...b[..19]])
|
||||
|
||||
def right_rotate_17(u32 e) -> u32:
|
||||
bool[32] b = to_bits(e)
|
||||
return from_bits([...b[15..], ...b[..15]])
|
||||
|
||||
def right_rotate_18(u32 e) -> u32:
|
||||
bool[32] b = to_bits(e)
|
||||
return from_bits([...b[14..], ...b[..14]])
|
||||
|
||||
def right_rotate_19(u32 e) -> u32:
|
||||
bool[32] b = to_bits(e)
|
||||
return from_bits([...b[13..], ...b[..13]])
|
||||
|
||||
def right_rotate_22(u32 e) -> u32:
|
||||
bool[32] b = to_bits(e)
|
||||
return from_bits([...b[10..], ...b[..10]])
|
||||
|
||||
def right_rotate_25(u32 e) -> u32:
|
||||
bool[32] b = to_bits(e)
|
||||
return from_bits([...b[7..], ...b[..7]])
|
||||
def rotr32<N>(u32 x) -> u32:
|
||||
return (x >> N) | (x << (32 - N))
|
||||
|
||||
def extend(u32[64] w, u32 i) -> u32:
|
||||
u32 s0 = right_rotate_7(w[i-15]) ^ right_rotate_18(w[i-15]) ^ (w[i-15] >> 3)
|
||||
u32 s1 = right_rotate_17(w[i-2]) ^ right_rotate_19(w[i-2]) ^ (w[i-2] >> 10)
|
||||
u32 s0 = rotr32::<7>(w[i-15]) ^ rotr32::<18>(w[i-15]) ^ (w[i-15] >> 3)
|
||||
u32 s1 = rotr32::<17>(w[i-2]) ^ rotr32::<19>(w[i-2]) ^ (w[i-2] >> 10)
|
||||
return w[i-16] + s0 + w[i-7] + s1
|
||||
|
||||
def temp1(u32 e, u32 f, u32 g, u32 h, u32 k, u32 w) -> u32:
|
||||
|
@ -52,7 +13,7 @@ def temp1(u32 e, u32 f, u32 g, u32 h, u32 k, u32 w) -> u32:
|
|||
u32 ch = (e & f) ^ ((!e) & g)
|
||||
|
||||
// S1 := (e rightrotate 6) xor (e rightrotate 11) xor (e rightrotate 25)
|
||||
u32 S1 = right_rotate_6(e) ^ right_rotate_11(e) ^ right_rotate_25(e)
|
||||
u32 S1 = rotr32::<6>(e) ^ rotr32::<11>(e) ^ rotr32::<25>(e)
|
||||
|
||||
// temp1 := h + S1 + ch + k + w
|
||||
return h + S1 + ch + k + w
|
||||
|
@ -62,7 +23,7 @@ def temp2(u32 a, u32 b, u32 c) -> u32:
|
|||
u32 maj = (a & b) ^ (a & c) ^ (b & c)
|
||||
|
||||
// S0 := (a rightrotate 2) xor (a rightrotate 13) xor (a rightrotate 22)
|
||||
u32 S0 = right_rotate_2(a) ^ right_rotate_13(a) ^ right_rotate_22(a)
|
||||
u32 S0 = rotr32::<2>(a) ^ rotr32::<13>(a) ^ rotr32::<22>(a)
|
||||
|
||||
// temp2 := S0 + maj
|
||||
return S0 + maj
|
||||
|
|
Loading…
Reference in a new issue