mirror of
https://github.com/logos-storage/nim-groth16.git
synced 2026-05-18 16:49:30 +00:00
138 lines
4.3 KiB
Nim
138 lines
4.3 KiB
Nim
|
|
{.used.}
|
|
|
|
import std/unittest
|
|
import std/options
|
|
import std/strutils
|
|
|
|
import constantine/math/io/io_fields
|
|
import constantine/named/properties_fields
|
|
|
|
import groth16/bn128/fields
|
|
import groth16/bn128/curves
|
|
import groth16/bn128/rnd
|
|
|
|
import groth16/bn128/debug
|
|
|
|
#-------------------------------------------------------------------------------
|
|
# compression test cases generated by arkworks
|
|
|
|
# last bit is NOT SET
|
|
const testA_compressed = "fbc1b30a5acf74d512a3db0ab849bf095c106e5f52cf289c05b58dbb949b2824a156d453ec510b7faad49260faa23781e0962dbb5bbd419753896d42672da717"
|
|
const testA_g2_x_real = fromHex(Fp[BN254_Snarks], "0x24289b94bb8db5059c28cf525f6e105c09bf49b80adba312d574cf5a0ab3c1fb")
|
|
const testA_g2_x_imag = fromHex(Fp[BN254_Snarks], "0x17a72d67426d89539741bd5bbb2d96e08137a2fa6092d4aa7f0b51ec53d456a1")
|
|
const testA_g2_y_real = fromHex(Fp[BN254_Snarks], "0x1c596fe08af669b99b08a7198a94b8abc59e711a5ba88f84b3670aa3da0775f1")
|
|
const testA_g2_y_imag = fromHex(Fp[BN254_Snarks], "0x1166e754640ae7db87c1ad56886af9270bed8afd813922628fdd700e36048f09")
|
|
|
|
# last bit is SET
|
|
const testB_compressed = "979a0fece9f1d92ac5889660f19370145ede8269fbd483714ec0517f76f3c51ced51ff0e98cfb98d94dbade55df493cd57f6af07c60b5e58ce8de13ceac68b9d"
|
|
const testB_g2_x_real = fromHex(Fp[BN254_Snarks], "0x1cc5f3767f51c04e7183d4fb6982de5e147093f1609688c52ad9f1e9ec0f9a97")
|
|
const testB_g2_x_imag = fromHex(Fp[BN254_Snarks], "0x1d8bc6ea3ce18dce585e0bc607aff657cd93f45de5addb948db9cf980eff51ed")
|
|
const testB_g2_y_real = fromHex(Fp[BN254_Snarks], "0x0d034c3de83b9cb8fb066a360afe5391c7e170efc6ebe6d4b93f252126ac204d")
|
|
const testB_g2_y_imag = fromHex(Fp[BN254_Snarks], "0x2a7090cf51be2141e049d0176e744fa0420099090b636984bfaa1142456f4b3a")
|
|
|
|
func hexStringToByteSeq(hex: string): seq[byte] =
|
|
let s = parseHexStr(hex)
|
|
result = newSeq[byte](s.len)
|
|
for i, ch in s:
|
|
result[i] = byte(ch)
|
|
|
|
func hexStringToComprG2(hex: string): ComprG2 =
|
|
let bseq = hexStringToByteSeq(hex)
|
|
var arr: array[64,byte]
|
|
for i, b in bseq:
|
|
arr[i] = b
|
|
return ComprG2(arr)
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
func mbEqualsG1(mb: Option[G1] , refVal: G1): bool =
|
|
var ok: bool = false
|
|
if isSome(mb):
|
|
let re : G1 = mb.unsafeGet()
|
|
ok = (refVal === re)
|
|
return ok
|
|
|
|
func mbEqualsG2(mb: Option[G2] , refVal: G2): bool =
|
|
var ok: bool = false
|
|
if isSome(mb):
|
|
let re : G2 = mb.unsafeGet()
|
|
ok = (refVal === re)
|
|
return ok
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
suite "point compression":
|
|
|
|
test "unit test for G2 point compression, test case `A` (flag is not set)":
|
|
|
|
let x = mkFp2( testA_g2_x_real , testA_g2_x_imag )
|
|
let y = mkFp2( testA_g2_y_real , testA_g2_y_imag )
|
|
let pt = mkG2( x , y )
|
|
let c = hexStringToComprG2(testA_compressed)
|
|
|
|
let ok1 = (compressG2(pt) == c)
|
|
let ok2 = mbEqualsG2( uncompressG2(c) , pt )
|
|
|
|
check (ok1 and ok2)
|
|
|
|
#---------------------------
|
|
|
|
test "unit test for G2 point compression, test case `B` (flag is set)":
|
|
|
|
let x = mkFp2( testB_g2_x_real , testB_g2_x_imag )
|
|
let y = mkFp2( testB_g2_y_real , testB_g2_y_imag )
|
|
let pt = mkG2( x , y )
|
|
let c = hexStringToComprG2(testB_compressed)
|
|
|
|
let ok1 = (compressG2(pt) == c)
|
|
let ok2 = mbEqualsG2( uncompressG2(c) , pt )
|
|
|
|
check (ok1 and ok2)
|
|
|
|
#---------------------------
|
|
|
|
test "test G1 point compression and decompression for 500 random points":
|
|
|
|
let N = 500
|
|
var ok = true
|
|
var cnt = 0
|
|
|
|
for i in 1..N:
|
|
let pt : G1 = randG1()
|
|
let c : ComprG1 = compressG1(pt)
|
|
let mb : Option[G1] = uncompressG1(c)
|
|
|
|
var this_ok = mbEqualsG1(mb, pt)
|
|
|
|
ok = ok and this_ok
|
|
if this_ok:
|
|
cnt += 1
|
|
|
|
echo "out of " & $N & " random tests, " & $cnt & " passed"
|
|
check ok
|
|
|
|
#---------------------------
|
|
|
|
test "test G2 point compression and decompression for 500 random points":
|
|
|
|
let N = 500
|
|
var ok = true
|
|
var cnt = 0
|
|
|
|
for i in 1..N:
|
|
let pt : G2 = randG2()
|
|
let c : ComprG2 = compressG2(pt)
|
|
let mb : Option[G2] = uncompressG2(c)
|
|
|
|
var this_ok = mbEqualsG2(mb, pt)
|
|
|
|
ok = ok and this_ok
|
|
if this_ok:
|
|
cnt += 1
|
|
|
|
echo "out of " & $N & " random tests, " & $cnt & " passed"
|
|
check ok
|
|
|
|
#-------------------------------------------------------------------------------
|