Skip to content

Commit

Permalink
Add VecGroups.hasNativeLibrary.
Browse files Browse the repository at this point in the history
Test for hasNativeLibrary when running tests that need native.
  • Loading branch information
JohnLCaron committed Apr 9, 2024
1 parent 71015ca commit 2026695
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 103 deletions.
5 changes: 2 additions & 3 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ You should find that the library jar file is placed into:

## Building the Verificatum library (optional)

While the C libraries in VEC, VECJ and GMP are optional, they are needed for good performance.

### Installing or Building the GMP library

1. You can check to see if there is a pre-built gmp library available for your machine.
Expand All @@ -83,11 +85,8 @@ version is probably fine.

3. Install into one of the library paths, usually /usr/lib.


### Building the Verificatum Elliptic Curve library (VEC)

While the C libraries in VEC, VECJ and GMP are optional, they are needed for good performance.

```
cd devhome
git clone https://github.com/verificatum/verificatum-vec.git
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/cryptobiotic/eg/cli/RunShowSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ class RunShowSystem {
}

if (showSet.has("hasVEC")) {
if (testVecDirectExp())
if (testVecNative())
println("VECJ, VEC and GMP are installed")
else
println("VECJ, VEC and GMP are not installed")
}
}

fun testVecDirectExp(): Boolean {
fun testVecNative(): Boolean {
try {
val group = productionGroup("P-256") as EcGroupContext
if (!(group.vecGroup is VecGroupNative)) {
Expand Down
11 changes: 9 additions & 2 deletions src/main/kotlin/org/cryptobiotic/eg/core/ecgroup/VecGroups.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class VecGroups(

init {
// NIST

NAMED_PARAMS["prime192v3"] = VecGroups(
"fffffffffffffffffffffffffffffffeffffffffffffffff",
"fffffffffffffffffffffffffffffffefffffffffffffffc",
Expand Down Expand Up @@ -329,19 +328,27 @@ class VecGroups(
return res
}

var hasNativeLibrary = true

fun hasNativeLibrary(): Boolean {
getEcGroup("P-256", true)
return hasNativeLibrary
}

// BTW, setting LD_LIBRARY_PATH to /usr/local/lib doesnt work when debugging, add to /usr/lib directly.
// Ok when running from CLI, though.
fun getEcGroup(name: String, useNative: Boolean = true): VecGroup {
val params: VecGroups? = NAMED_PARAMS.get(name)
if (params == null) {
throw RuntimeException("Unknown named curve! ($name)")
} else {
return if (useNative) {
return if (hasNativeLibrary && useNative) {
val haveNative = try {
VEC.getCurve(name)
true
} catch (t: Throwable) {
logger.warn { "VECJ not installed, using non-native library" }
hasNativeLibrary = false
false
}
if (haveNative)
Expand Down
Binary file added src/test/data/workflow/allAvailableEc.zip
Binary file not shown.
1 change: 0 additions & 1 deletion src/test/kotlin/org/cryptobiotic/eg/core/SchnorrTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import io.kotest.property.Arb
import io.kotest.property.arbitrary.int
import io.kotest.property.checkAll
import org.cryptobiotic.eg.core.ecgroup.EcGroupContext
import org.cryptobiotic.eg.core.productionGroup

class SchnorrTest : WordSpec({
val intGroup = productionGroup()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.cryptobiotic.eg.core.ecgroup

import org.cryptobiotic.eg.core.ElementModP
import java.math.BigInteger
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -11,23 +10,25 @@ class TestAgainstNative {

@Test
fun testSqrt3() {
val group = EcGroupContext("P-256", false)
val vecGroup = group.vecGroup
val vecGroupN = EcGroupContext("P-256", true).vecGroup
assertTrue(vecGroupN is VecGroupNative)
if (VecGroups.hasNativeLibrary()) {
val group = EcGroupContext("P-256", false)
val vecGroup = group.vecGroup
val vecGroupN = EcGroupContext("P-256", true).vecGroup
assertTrue(vecGroupN is VecGroupNative)

// randomElementModP seems to always be the case when p = 3 mod 4
repeat(100) {
val elemP = group.randomElementModP(2).ec
val elemPy2 = vecGroupN.equationf(elemP.x)
// randomElementModP seems to always be the case when p = 3 mod 4
repeat(100) {
val elemP = group.randomElementModP(2).ec
val elemPy2 = vecGroupN.equationf(elemP.x)

val elemPy = vecGroup.sqrt(elemPy2)
assertEquals(elemP.y, elemPy)
val elemPy = vecGroup.sqrt(elemPy2)
assertEquals(elemP.y, elemPy)

val elemPyt = vecGroupN.sqrt(elemPy2)
assertEquals(elemP.y, elemPyt)
val elemPyt = vecGroupN.sqrt(elemPy2)
assertEquals(elemP.y, elemPyt)

assertEquals(elemPy, elemPyt)
assertEquals(elemPy, elemPyt)
}
}
}

Expand Down Expand Up @@ -55,16 +56,18 @@ class TestAgainstNative {

@Test
fun testProdPowers() {
val group = EcGroupContext("P-256", false)
val groupN = EcGroupContext("P-256", true)
assertTrue(groupN.vecGroup is VecGroupNative)
if (VecGroups.hasNativeLibrary()) {
val group = EcGroupContext("P-256", false)
val groupN = EcGroupContext("P-256", true)
assertTrue(groupN.vecGroup is VecGroupNative)

val n = 100
val bases = List(n) { group.randomElementModP() }
val nonces = List(n) { group.randomElementModQ() }
val prodpow: ElementModP = group.prodPowers(bases, nonces)
val prodpowN: ElementModP = groupN.prodPowers(bases, nonces)
assertEquals(prodpow, prodpowN)
val n = 100
val bases = List(n) { group.randomElementModP() }
val nonces = List(n) { group.randomElementModQ() }
val prodpow: ElementModP = group.prodPowers(bases, nonces)
val prodpowN: ElementModP = groupN.prodPowers(bases, nonces)
assertEquals(prodpow, prodpowN)
}
}

}
22 changes: 12 additions & 10 deletions src/test/kotlin/org/cryptobiotic/eg/core/ecgroup/TestElem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,21 @@ class TestElem {

@Test
fun testSqrt() {
val group = productionGroup("P-256") as EcGroupContext
val vecGroupN = group.vecGroup as VecGroupNative
if (VecGroups.hasNativeLibrary()) {
val group = productionGroup("P-256") as EcGroupContext
val vecGroupN = group.vecGroup as VecGroupNative

repeat (100) {
val elemP = group.randomElementModP(2).ec
val elemPx = elemP.x
val elemPy2 = vecGroupN.equationf(elemPx)
repeat(100) {
val elemP = group.randomElementModP(2).ec
val elemPx = elemP.x
val elemPy2 = vecGroupN.equationf(elemPx)

val elemPy = vecGroupN.sqrt(elemPy2)
assertEquals(elemP.y, elemPy)
val elemPy = vecGroupN.sqrt(elemPy2)
assertEquals(elemP.y, elemPy)

val elemPyt = vecGroupN.sqrt(elemPy2)
assertEquals(elemP.y, elemPyt)
val elemPyt = vecGroupN.sqrt(elemPy2)
assertEquals(elemP.y, elemPyt)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ class TestSerialize {
checkAll(
elementsModP(group),
) { p ->
assertTrue (p is EcElementModP)
assertTrue(p is EcElementModP)
val ec = (p as EcElementModP).ec
assertTrue (ec is VecElementPnative)
if (VecGroups.hasNativeLibrary()) {
assertTrue(ec is VecElementPnative)
}
}
}
}
Expand Down Expand Up @@ -56,15 +58,15 @@ class TestSerialize {
) { p ->
val ec = (p as EcElementModP).ec
val vecGroup = ec.pGroup
val convert2 : VecElementP? = vecGroup.elementFromByteArray2(ec.toByteArray2())
val convert2: VecElementP? = vecGroup.elementFromByteArray2(ec.toByteArray2())
assertNotNull(convert2)
assertEquals(ec, convert2)

val convert12 : VecElementP? = vecGroup.elementFromByteArray1from2(ec.toByteArray2())
val convert12: VecElementP? = vecGroup.elementFromByteArray1from2(ec.toByteArray2())
assertNotNull(convert12)
assertEquals(ec, convert12)

val convert1 : VecElementP? = vecGroup.elementFromByteArray1(ec.toByteArray1())
val convert1: VecElementP? = vecGroup.elementFromByteArray1(ec.toByteArray1())
assertNotNull(convert1)
assertEquals(ec, convert1)
}
Expand Down
5 changes: 2 additions & 3 deletions src/test/kotlin/org/cryptobiotic/eg/publish/TestZippedJson.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ import kotlin.test.assertNotNull

import org.cryptobiotic.eg.publish.json.*
import org.cryptobiotic.util.ErrorMessages
import org.cryptobiotic.util.Testing


// run verifier on zipped JSON record, only supported on JVM
// run verifier on zipped JSON record
@OptIn(ExperimentalSerializationApi::class)
class TestZippedJson {
val jsonReader = Json { explicitNulls = false; ignoreUnknownKeys = true; prettyPrint = true }

val inputDir = "src/test/data/workflow/allAvailableEc"
val zippedJson = "${Testing.testOut}/allAvailableJson.zip"
val zippedJson = "src/test/data/workflow/allAvailableEc.zip"
val fs: FileSystem
val fsp: FileSystemProvider

Expand Down
Loading

0 comments on commit 2026695

Please sign in to comment.