BN254 mini-lib (using constantine under the hood)

This commit is contained in:
Balazs Komuves 2025-03-15 13:53:11 +01:00
parent 51ada29712
commit 28b368d92a
No known key found for this signature in database
GPG Key ID: F63B7AEF18435562
2 changed files with 80 additions and 1 deletions

View File

@ -1,6 +1,8 @@
version = "0.0.1"
author = "Balazs Komuves"
description = "Witness generation for circom circuits"
license = "MIT"
license = "MIT OR Apache-2.0"
bin = @["main"]
requires "constantine >= 0.2.0"

View File

@ -0,0 +1,77 @@
import pkg/constantine/math/arithmetic
import pkg/constantine/math/io/io_bigints
import pkg/constantine/math/io/io_fields
import pkg/constantine/named/properties_fields
#-------------------------------------------------------------------------------
type
B* = BigInt[254]
F* = Fr[BN254Snarks]
const zeroF* : F = fromHex( Fr[BN254Snarks], "0x00" )
const oneF* : F = fromHex( Fr[BN254Snarks], "0x01" )
func isZeroF* (x: F ) : bool = bool(isZero(x))
func isEqualF* (x, y: F ) : bool = bool(x == y)
func `===`* (x, y: F ) : bool = isEqualF(x,y)
const fieldMask* : B = fromHex( BigInt[254] , "0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", bigEndian )
const fieldPrime* : B = fromHex( BigInt[254] , "0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001", bigEndian )
const halfPrimPlus1* : B = fromHex( BigInt[254] , "0x183227397098d014dc2822db40c0ac2e9419f4243cdcb848a1f0fac9f8000001", bigEndian )
#-------------------------------------------------------------------------------
func intToF*(a: int): F =
var y : F
y.fromInt(a)
return y
func int64ToF*(a: int64): F =
var y : F
y.fromInt(a)
return y
func boolToF*(b: bool): F =
return (if b: oneF else: zeroF)
func fToBool*(x: F): bool =
return not (isZeroF x)
func bigToF*(big: B): F =
var x : F
x.fromBig( big )
return x
func fToBig*(x: F): B =
return x.toBig()
proc decimalToB*(s: string): B =
var y: B
let ok = y.fromDecimal(s)
return y
proc decimalToF*(s: string): F =
return bigToF(decimalToB(s))
# var y: F
# let ok = y.fromDecimal(s) # wtf nim
# return y
func fToDecimal*(x: F): string =
return toDecimal(x)
#-------------------------------------------------------------------------------
func negF* (y: F ): F = ( var z : F = zeroF ; z -= y ; return z )
func invF* (y: F ): F = ( var z : F = y ; inv(z) ; return z )
func `+`*[n](x, y: BigInt[n] ): BigInt[n] = ( var z : BigInt[n] = x ; z += y ; return z )
func `-`*[n](x, y: BigInt[n] ): BigInt[n] = ( var z : BigInt[n] = 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 `*`*(x, y: F ): F = ( var z : F = x ; z *= y ; return z )
func `/`*(x, y: F ): F = ( var z : F = x ; z *= invF(y) ; return z )
#-------------------------------------------------------------------------------