72 lines
1.9 KiB
Nim
Raw Normal View History

2023-10-24 13:54:39 +02:00
import
2023-11-07 11:58:15 +01:00
constantine/math/arithmetic,
2023-10-24 13:54:39 +02:00
constantine/math/io/io_fields,
2023-10-26 14:39:27 +02:00
constantine/math/io/io_bigints,
2024-08-08 14:45:13 +02:00
constantine/named/algebras
#-------------------------------------------------------------------------------
type B* = BigInt[254]
2024-08-08 14:45:13 +02:00
type F* = Fr[BN254_Snarks]
type S* = (F,F,F)
#-------------------------------------------------------------------------------
func getZero*() : F =
var z : F
setZero(z)
return z
2023-11-07 14:21:37 +01:00
#-------------------------------------------------------------------------------
const zero* : F = getZero()
const one* : F = fromHex(F,"0x01") # note: `fromUint()` does not work at compile time
2026-01-21 14:36:11 +01:00
const two* : F = fromHex(F,"0x02")
2023-11-07 14:21:37 +01:00
const twoToThe64* : F = fromHex(F,"0x10000000000000000")
2023-11-07 14:21:37 +01:00
#-------------------------------------------------------------------------------
2023-10-26 14:39:27 +02:00
func hexToF*(s : string, endian: static Endianness = bigEndian) : F =
let bigint = B.fromHex(s, endian)
return F.fromBig(bigint)
2023-10-26 14:39:27 +02:00
func arrayFromHex*[N](
inp: array[N, string],
endian: static Endianness = bigEndian) : array[N, F] =
var tmp : array[N, F]
for i in low(inp)..high(inp):
2023-10-26 14:39:27 +02:00
tmp[i] = hexToF(inp[i], endian)
return tmp
2026-01-21 14:36:11 +01:00
#-------------------------------------------------------------------------------
func `+`*(x, y: F): F = ( var z: F = x ; z += y ; return z )
func `-`*(x, y: F): F = ( var z: F = x ; z -= y ; return z )
func `*`*(x, y: F): F = ( var z: F = x ; z *= y ; return z )
func `==`*(a, b: F): bool =
bool(arithmetic.`==`(a, b))
2026-01-21 14:36:11 +01:00
func sqr*(x : F): F =
var y = x
y.square()
return y
#-------------------------------------------------------------------------------
func fastPow*(base: F, expo: B): F =
var s : F = base
var a : F = one
var e : B = expo
for i in 0..<254:
if bool(isOdd(e)):
a *= s
s.square()
e.div2()
return a
#-------------------------------------------------------------------------------