Longfellow ZK 0290cb32

Loading...
Searching...
No Matches
fp_p128.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_P128_H_
16#define PRIVACY_PROOFS_ZK_LIB_ALGEBRA_FP_P128_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 Fp(2^128 - 2^108 + 1). We call this
27// prime P128 because of lack of imagination, but unlike P256,
28// this is not a NIST standard name. The field contains
29// roots of unity of order 2^108.
30
31// Root of unity from pari-gp:
32// ? p=2^128-2^108+1
33// %1 = 340282042402384805036647824275747635201
34// ? g=ffgen(x+Mod(1,p))
35// %2 = 340282042402384805036647824275747635200
36// ? w=sqrtn(g,2^107)
37// %3 = 17166008163159356379329005055841088858
38//
39// ? w=Mod(17166008163159356379329005055841088858, p)
40// %4 = Mod(17166008163159356379329005055841088858,
41// 340282042402384805036647824275747635201)
42// ? w^(2^107)
43// %5 = Mod(340282042402384805036647824275747635200,
44// 340282042402384805036647824275747635201)
45// ? w^(2^108)
46// %6 = Mod(1, 340282042402384805036647824275747635201)
47//
48// Root of unity of order 32:
49// ? w32=w^(2^(108-32))
50// %15 = Mod(164956748514267535023998284330560247862,
51// 340282042402384805036647824275747635201)
52// ? w32^(2^31)
53// %16 = Mod(340282042402384805036647824275747635200,
54// 340282042402384805036647824275747635201)
55// ? w32^(2^32)
56// %17 = Mod(1, 340282042402384805036647824275747635201)
57
58/*
59 This struct contains an optimized reduction step for the chosen field.
60*/
61 struct Fp128Reduce {
62 // Harcoded base_64 modulus.
63 static const constexpr std::array<uint64_t, 2> kModulus = {
64 0x0000000000000001u,
65 0xFFFFF00000000000u,
66 };
67
68 static inline void reduction_step(uint64_t a[], uint64_t mprime,
69 const Nat<2>& m) {
70 uint64_t r = -a[0];
71 uint64_t sub[2] = {r << 44, r >> 20};
72 uint64_t add[3] = {r, 0, r};
73 accum(4, a, 3, add);
74 negaccum(3, a + 1, 2, sub);
75 }
76
77 static inline void reduction_step(uint32_t a[], uint32_t mprime,
78 const Nat<2>& m) {
79 uint32_t r = -a[0];
80 uint32_t sub[2] = {r << 12, r >> 20};
81 uint32_t add[5] = {r, 0, 0, 0, r};
82 accum(6, a, 5, add);
83 negaccum(3, a + 3, 2, sub);
84 }
85};
86
87 template <bool optimized_mul = false>
88 using Fp128 = FpGeneric<2, optimized_mul, Fp128Reduce>;
89} // namespace proofs
90
91#endif // PRIVACY_PROOFS_ZK_LIB_ALGEBRA_FP_P128_H_
proofs::FpGeneric
Definition fp_generic.h:36
proofs::Nat
Definition nat.h:60
proofs::Fp128Reduce
Definition fp_p128.h:61