Debian Codegraph @

ff/ff.go

raw symbols SCIP
1 package ff
2
3 import (
4 "math/big"
5 )
6
7 var (
8 // p as a *big.Int.
9 pBI, _ = new(big.Int).SetString("28948022309329048855892746252183396360603931420023084536990047309120118726721", 10)
10 // p as an Element.
11 p = Element{1126179130581057, 9223372036854775808, 33558592, 4611686018427387904}
12 )
13
14 // Element represents a single element a ∈ 𝔽ₚ, where
15 //
16 // p = 28948022309329048855892746252183396360603931420023084536990047309120118726721.
17 //
18 // Elements are represnted by an unsigned 256 bit integer.
19 // This is represented by an array of four 64 bit unsigned integers.
20 // Specifically,
21 //
22 // element = [4]uint64{a,b,c,d} = (a + b*2^64 + c*2^128 + d*2^192)
23 //
24 // A representation is "normalized" if the it represents an integer in the interval [0,p).
25 type Element [4]uint64
26
27 // ElementToBigInt converts an element to a *big.Int.
28 func ElementToBigInt(a Element) (z *big.Int) {
29 return uint64ArrayToBigInt(a[:])
30 }
31
32 // BigIntToElement converts a big.Int to an element.
33 func BigIntToElement(z *big.Int) (a Element) {
34 arr := bigIntToUint64Array(z)
35 for len(arr) < 4 {
36 arr = append(arr, 0)
37 }
38 return Element{arr[0], arr[1], arr[2], arr[3]}
39 }
40
41 // Transform a *big.Int to []uint64 in little-endian.
42 func bigIntToUint64Array(z *big.Int) (arr []uint64) {
43 z = new(big.Int).Set(z) // Use a copy to avoid overwriting anything.
44 low64 := new(big.Int).SetUint64(0xffffffffffffffff)
45 for z.BitLen() > 0 {
46 arr = append(arr, new(big.Int).And(z, low64).Uint64())
47 z.Rsh(z, 64)
48 }
49 return
50 }
51
52 func uint64ArrayToBigInt(arr []uint64) (z *big.Int) {
53 if len(arr) == 0 {
54 return new(big.Int)
55 }
56 z = new(big.Int).SetUint64(arr[len(arr)-1])
57 for i := len(arr) - 2; i >= 0; i-- {
58 z.Add(z.Lsh(z, 64), new(big.Int).SetUint64(arr[i]))
59 }
60 return z
61 }