Longfellow ZK 0290cb32

Loading...
Searching...
No Matches
fp_p256.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_ALGEBRA_FP_P256_H_
16#define PRIVACY_PROOFS_ZK_LIB_ALGEBRA_FP_P256_H_
17
18#include <array>
19#include <cstdint>
20
21#include "algebra/fp_generic.h"
22#include "algebra/nat.h"
23#include "algebra/sysdep.h"
24
25 namespace proofs {
26// Optimized implementation of
27// Fp(115792089210356248762697446949407573530086143415290314195533631308867097853951)
28
29/*
30 This struct contains an optimized reduction step for the chosen field.
31*/
32 struct Fp256Reduce {
33 // Harcoded base_64 modulus.
34 static const constexpr std::array<uint64_t, 4> kModulus = {
35 0xFFFFFFFFFFFFFFFFu,
36 0xFFFFFFFFu,
37 0,
38 0xFFFFFFFF00000001u,
39 };
40
41 static inline void reduction_step(uint64_t a[], uint64_t mprime,
42 const Nat<4>& m) {
43 uint64_t r = a[0];
44 uint64_t l[5] = {r, 0, 0, r << 32, r >> 32};
45 negaccum(6, a, 5, l);
46 uint64_t h[4] = {r << 32, r >> 32, r, r};
47 accum(5, a + 1, 4, h);
48 }
49
50 static inline void reduction_step(uint32_t a[], uint32_t mprime,
51 const Nat<4>& m) {
52 uint32_t r = a[0];
53 uint32_t l[8] = {r, 0, 0, 0, 0, 0, 0, r};
54 negaccum(10, a, 8, l);
55 uint32_t h[6] = {r, 0, 0, r, 0, r};
56 accum(7, a + 3, 6, h);
57 }
58};
59
60 template <bool optimized_mul = false>
61 using Fp256 = FpGeneric<4, optimized_mul, Fp256Reduce>;
62} // namespace proofs
63
64#endif // PRIVACY_PROOFS_ZK_LIB_ALGEBRA_FP_P256_H_
proofs::FpGeneric
Definition fp_generic.h:36
proofs::Nat
Definition nat.h:60
proofs::Fp256Reduce
Definition fp_p256.h:32