Skip to content

Commit

Permalink
chore: invert rem
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Aug 10, 2022
1 parent c2c6326 commit 99173d5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 53 deletions.
93 changes: 47 additions & 46 deletions src/webauthn.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ namespace Webauthn:
_verify_auth_flags(authenticator_data)

# We're doing using the sphinx cairo sha256 implementation until the cario hints support more efficient sha256
let (client_data_hash: felt*) = sha256(client_data_json, client_data_json_len * 4 - client_data_json_rem)
let (client_data_hash: felt*) = sha256(client_data_json, client_data_json_len * 4 - (4 - client_data_json_rem))
# let (local sha256_ptr_start : felt*) = alloc()
# let sha256_ptr = sha256_ptr_start
# let (client_data_hash: felt*) = sha256{sha256_ptr=sha256_ptr}(client_data_json, client_data_json_len * 4 - client_data_json_rem)
# let (client_data_hash: felt*) = sha256{sha256_ptr=sha256_ptr}(client_data_json, client_data_json_len * 4 - (4 - client_data_json_rem))
# finalize_sha256(sha256_ptr, sha256_ptr)

let (msg_data_ptr) = alloc()
Expand All @@ -71,10 +71,10 @@ namespace Webauthn:
_concat_msg_data{msg_data_ptr=msg_data_ptr}(authenticator_data_len, authenticator_data_rem, authenticator_data, client_data_hash)

# We're doing using the sphinx cairo sha256 implementation until the cario hints support more efficient sha256
let (msg_hash: felt*) = sha256(msg_data_start_ptr, authenticator_data_len * 4 - authenticator_data_rem + 32)
let (msg_hash: felt*) = sha256(msg_data_start_ptr, authenticator_data_len * 4 - (4 - authenticator_data_rem) + 32)
# let (local sha256_ptr_start : felt*) = alloc()
# let sha256_ptr = sha256_ptr_start
# let (msg_hash: felt*) = sha256{sha256_ptr=sha256_ptr}(msg_data_start_ptr, authenticator_data_len * 4 - authenticator_data_rem + 32)
# let (msg_hash: felt*) = sha256{sha256_ptr=sha256_ptr}(msg_data_start_ptr, authenticator_data_len * 4 (4 - authenticator_data_rem) + 32)
# finalize_sha256(sha256_ptr, sha256_ptr)

# Construct 86bit hash limbs
Expand Down Expand Up @@ -117,7 +117,7 @@ namespace Webauthn:
end

if challenge_len == 1 and callenge_rem == 1:
let (p, _) = unsigned_div_rem(shifted, 2 ** 8)
let (p, _) = unsigned_div_rem(shifted, 2 ** 24)
let c1 = challenge[0]
assert challenge[0] = p
return ()
Expand All @@ -131,7 +131,7 @@ namespace Webauthn:
end

if challenge_len == 1 and callenge_rem == 3:
let (p, _) = unsigned_div_rem(shifted, 2 ** 24)
let (p, _) = unsigned_div_rem(shifted, 2 ** 8)
let c1 = challenge[0]
assert challenge[0] = p
return ()
Expand Down Expand Up @@ -167,9 +167,9 @@ namespace Webauthn:
challenge_offset_rem: felt,
) -> (shifted: felt):
if challenge_offset_rem == 1:
let (_, r) = unsigned_div_rem(client_data_json[challenge_offset_len], 2 ** 8)
let (p, _) = unsigned_div_rem(client_data_json[challenge_offset_len], 2 ** 8)
let shifted = r * 2 ** 24 + p
let (_, r) = unsigned_div_rem(client_data_json[challenge_offset_len], 2 ** 24)
let (p, _) = unsigned_div_rem(client_data_json[challenge_offset_len], 2 ** 24)
let shifted = r * 2 ** 8 + p
return (shifted)
end

Expand All @@ -181,9 +181,9 @@ namespace Webauthn:
end

if challenge_offset_rem == 3:
let (_, r) = unsigned_div_rem(client_data_json[challenge_offset_len], 2 ** 24)
let (p, _) = unsigned_div_rem(client_data_json[challenge_offset_len], 2 ** 24)
let shifted = r * 2 ** 8 + p
let (_, r) = unsigned_div_rem(client_data_json[challenge_offset_len], 2 ** 8)
let (p, _) = unsigned_div_rem(client_data_json[challenge_offset_len], 2 ** 8)
let shifted = r * 2 ** 24 + p
return (shifted)
end

Expand All @@ -209,24 +209,24 @@ namespace Webauthn:
let authenticator_data = authenticator_data + authenticator_data_len - 1
let msg_data_ptr = msg_data_ptr + authenticator_data_len - 1

let (p0, r0) = unsigned_div_rem(client_data_hash[0], 2 ** 24)
let (p1, r1) = unsigned_div_rem(client_data_hash[1], 2 ** 24)
let (p2, r2) = unsigned_div_rem(client_data_hash[2], 2 ** 24)
let (p3, r3) = unsigned_div_rem(client_data_hash[3], 2 ** 24)
let (p4, r4) = unsigned_div_rem(client_data_hash[4], 2 ** 24)
let (p5, r5) = unsigned_div_rem(client_data_hash[5], 2 ** 24)
let (p6, r6) = unsigned_div_rem(client_data_hash[6], 2 ** 24)
let (p7, r7) = unsigned_div_rem(client_data_hash[7], 2 ** 24)
let (p0, r0) = unsigned_div_rem(client_data_hash[0], 2 ** 8)
let (p1, r1) = unsigned_div_rem(client_data_hash[1], 2 ** 8)
let (p2, r2) = unsigned_div_rem(client_data_hash[2], 2 ** 8)
let (p3, r3) = unsigned_div_rem(client_data_hash[3], 2 ** 8)
let (p4, r4) = unsigned_div_rem(client_data_hash[4], 2 ** 8)
let (p5, r5) = unsigned_div_rem(client_data_hash[5], 2 ** 8)
let (p6, r6) = unsigned_div_rem(client_data_hash[6], 2 ** 8)
let (p7, r7) = unsigned_div_rem(client_data_hash[7], 2 ** 8)

assert msg_data_ptr[0] = authenticator_data[0] * 2 ** 8 + p0
assert msg_data_ptr[1] = r0 * 2 ** 8 + p1
assert msg_data_ptr[2] = r1 * 2 ** 8 + p2
assert msg_data_ptr[3] = r2 * 2 ** 8 + p3
assert msg_data_ptr[4] = r3 * 2 ** 8 + p4
assert msg_data_ptr[5] = r4 * 2 ** 8 + p5
assert msg_data_ptr[6] = r5 * 2 ** 8 + p6
assert msg_data_ptr[7] = r6 * 2 ** 8 + p7
assert msg_data_ptr[8] = r7 * 2 ** 8
assert msg_data_ptr[0] = authenticator_data[0] * 2 ** 24 + p0
assert msg_data_ptr[1] = r0 * 2 ** 24 + p1
assert msg_data_ptr[2] = r1 * 2 ** 24 + p2
assert msg_data_ptr[3] = r2 * 2 ** 24 + p3
assert msg_data_ptr[4] = r3 * 2 ** 24 + p4
assert msg_data_ptr[5] = r4 * 2 ** 24 + p5
assert msg_data_ptr[6] = r5 * 2 ** 24 + p6
assert msg_data_ptr[7] = r6 * 2 ** 24 + p7
assert msg_data_ptr[8] = r7 * 2 ** 24

return()
end
Expand Down Expand Up @@ -263,27 +263,28 @@ namespace Webauthn:
let authenticator_data = authenticator_data + authenticator_data_len - 1
let msg_data_ptr = msg_data_ptr + authenticator_data_len - 1

let (p0, r0) = unsigned_div_rem(client_data_hash[0], 2 ** 8)
let (p1, r1) = unsigned_div_rem(client_data_hash[1], 2 ** 8)
let (p2, r2) = unsigned_div_rem(client_data_hash[2], 2 ** 8)
let (p3, r3) = unsigned_div_rem(client_data_hash[3], 2 ** 8)
let (p4, r4) = unsigned_div_rem(client_data_hash[4], 2 ** 8)
let (p5, r5) = unsigned_div_rem(client_data_hash[5], 2 ** 8)
let (p6, r6) = unsigned_div_rem(client_data_hash[6], 2 ** 8)
let (p7, r7) = unsigned_div_rem(client_data_hash[7], 2 ** 8)
let (p0, r0) = unsigned_div_rem(client_data_hash[0], 2 ** 24)
let (p1, r1) = unsigned_div_rem(client_data_hash[1], 2 ** 24)
let (p2, r2) = unsigned_div_rem(client_data_hash[2], 2 ** 24)
let (p3, r3) = unsigned_div_rem(client_data_hash[3], 2 ** 24)
let (p4, r4) = unsigned_div_rem(client_data_hash[4], 2 ** 24)
let (p5, r5) = unsigned_div_rem(client_data_hash[5], 2 ** 24)
let (p6, r6) = unsigned_div_rem(client_data_hash[6], 2 ** 24)
let (p7, r7) = unsigned_div_rem(client_data_hash[7], 2 ** 24)

assert msg_data_ptr[0] = authenticator_data[0] * 2 ** 24 + p0
assert msg_data_ptr[1] = r0 * 2 ** 24 + p1
assert msg_data_ptr[2] = r1 * 2 ** 24 + p2
assert msg_data_ptr[3] = r2 * 2 ** 24 + p3
assert msg_data_ptr[4] = r3 * 2 ** 24 + p4
assert msg_data_ptr[5] = r4 * 2 ** 24 + p5
assert msg_data_ptr[6] = r5 * 2 ** 24 + p6
assert msg_data_ptr[7] = r6 * 2 ** 24 + p7
assert msg_data_ptr[8] = r7 * 2 ** 24
assert msg_data_ptr[0] = authenticator_data[0] * 2 ** 8 + p0
assert msg_data_ptr[1] = r0 * 2 ** 8 + p1
assert msg_data_ptr[2] = r1 * 2 ** 8 + p2
assert msg_data_ptr[3] = r2 * 2 ** 8 + p3
assert msg_data_ptr[4] = r3 * 2 ** 8 + p4
assert msg_data_ptr[5] = r4 * 2 ** 8 + p5
assert msg_data_ptr[6] = r5 * 2 ** 8 + p6
assert msg_data_ptr[7] = r6 * 2 ** 8 + p7
assert msg_data_ptr[8] = r7 * 2 ** 8

return()
end

return ()
end
end
6 changes: 3 additions & 3 deletions tests/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def split(G):
print("y", y0, y1, y2)
print("r", r0, r1, r2)
print("s", s0, s1, s2)
print("callenge_rem", 4 - (len(item["challenge"]) % 4))
print("callenge_rem", (len(item["challenge"]) % 4))
print("challenge_parts_len", len(challenge_parts))
print("challenge_parts", challenge_parts)
print("challenge_offset_len", challenge_offset_bytes // 4)
Expand All @@ -100,8 +100,8 @@ def split(G):
print("client_dat_json", client_data_bytes)
print("client_data_json_parts", client_data_json)
print("client_data_json_len", len(client_data_json))
print("client_data_json_rem", 4 - client_data_rem)
print("client_data_json_rem", client_data_rem)
print("authenticator_data_parts", authenticator_data_parts)
print("authenticator_data_len", len(authenticator_data_parts))
print("authenticator_data_rem", 4 - authenticator_data_rem)
print("authenticator_data_rem", authenticator_data_rem)
print("\n\n")
8 changes: 4 additions & 4 deletions tests/test_webauthn.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func test_verify_1{syscall_ptr : felt*, range_check_ptr, bitwise_ptr: BitwiseBui
assert origin[12] = 103

let client_data_json_len = 47
let client_data_json_rem = 3
let client_data_json_rem = 1
let (client_data_json) = alloc()
assert client_data_json[0] = 2065855609
assert client_data_json[1] = 1885676090
Expand Down Expand Up @@ -112,7 +112,7 @@ func test_verify_1{syscall_ptr : felt*, range_check_ptr, bitwise_ptr: BitwiseBui
assert client_data_json[46] = 2097152000

let authenticator_data_len = 10
let authenticator_data_rem = 3
let authenticator_data_rem = 1
let (authenticator_data) = alloc()
assert authenticator_data[0] = 547978947
assert authenticator_data[1] = 4176460842
Expand Down Expand Up @@ -189,7 +189,7 @@ func test_verify_2{syscall_ptr : felt*, range_check_ptr, bitwise_ptr: BitwiseBui
assert origin[12] = 103

let client_data_json_len = 57
let client_data_json_rem = 3
let client_data_json_rem = 1
let (client_data_json) = alloc()
assert client_data_json[0] = 2065855609
assert client_data_json[1] = 1885676090
Expand Down Expand Up @@ -251,7 +251,7 @@ func test_verify_2{syscall_ptr : felt*, range_check_ptr, bitwise_ptr: BitwiseBui


let authenticator_data_len = 10
let authenticator_data_rem = 3
let authenticator_data_rem = 1
let (authenticator_data) = alloc()
assert authenticator_data[0] = 547978947
assert authenticator_data[1] = 4176460842
Expand Down

0 comments on commit 99173d5

Please sign in to comment.