Longfellow ZK 0290cb32
Loading...
Searching...
No Matches
crc64.h
1// Copyright 2025 Google LLC.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef PRIVACY_PROOFS_ZK_LIB_UTIL_CRC64_H_
16#define PRIVACY_PROOFS_ZK_LIB_UTIL_CRC64_H_
17
18/*
19This package defines 3 basic methods for computing a simple 64-bit CRC.
20It is used for checksum and comparison of datastructures that are internal
21to this library.
22*/
23
24#include <stdlib.h>
25
26#include <cstdint>
27
28namespace proofs {
29namespace crc64 {
30static inline uint64_t shlu64(uint64_t x, size_t n) {
31 return (n >= 64) ? 0u : (x << n);
32}
33static inline uint64_t shru64(uint64_t x, size_t n) {
34 return (n >= 64) ? 0u : (x >> n);
35}
36static inline uint64_t update(uint64_t crc, uint64_t u, size_t n = 64) {
37 crc ^= u;
38 uint64_t l = shlu64(crc, 127u - n) ^ shlu64(crc, 125u - n) ^
39 shlu64(crc, 124u - n) ^ shlu64(crc, 64u - n);
40 return shru64(crc, n) ^ l ^ (l >> 1) ^ (l >> 3) ^ (l >> 4);
41}
42} // namespace crc64
43} // namespace proofs
44
45#endif // PRIVACY_PROOFS_ZK_LIB_UTIL_CRC64_H_