Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v7
- name: Restore caches
uses: coursier/cache-action@v8
uses: coursier/cache-action@v9
- name: Setup Scala
uses: coursier/setup-action@v3
with:
jvm: 21
apps: sbt
- name: Run tests
run: sbt +compile +test scalastyle
# sbt 2 wants one argument with `;` between commands, not a bare list. benchmark gets its own
# tasks because the root can't aggregate it (see build.sbt).
run: >
sbt "+compile; +test; +benchmark/test; +benchmark/Jmh/compile;
scalafmtCheckAll; benchmark/scalafmtCheckAll; scalafmtSbtCheck;
scalafixAll --check; benchmark/scalafixAll --check"
60 changes: 60 additions & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Replaces the code-smell checks scalastyle used to enforce. Formatting, line length and whitespace are
// scalafmt's (.scalafmt.conf). These have no scalafix equivalent and are no longer checked: naming,
// method and file length, parameter count, cyclomatic complexity, equals/hashCode pairing, covariant
// equals, boolean simplification, and declaring a clone() method.
rules = [
DisableSyntax
LeakingImplicitClassVal
NoAutoTupling
NoValInForComprehension
ProcedureSyntax
RedundantSyntax
OrganizeImports
]

DisableSyntax {
noNulls = true
noReturns = true
noFinalize = true
noTabs = true

regex = [
{
id = "noPrintln"
pattern = "(?<!\\.)\\bprintln\\b"
message = "Don't use println; return a value or use a logger."
}
{
id = "noTripleQuestion"
pattern = "\\?\\?\\?"
message = "??? left in code; finish the implementation."
}
{
id = "uppercaseL"
pattern = "\\b\\d+l\\b"
message = "Use uppercase L for Long literals (e.g. 100L not 100l)."
}
{
id = "todoComment"
pattern = "(?i)\\b(TODO|FIXME)\\b"
message = "Resolve TODO/FIXME before merging."
}
{
id = "noSunImport"
pattern = "import\\s+sun\\."
message = "Don't import from sun.*"
}
{
id = "noAwtImport"
pattern = "import\\s+java\\.awt\\."
message = "Don't import from java.awt.*"
}
]
}

OrganizeImports {
removeUnused = true
groupedImports = Merge
expandRelative = true
targetDialect = Scala2
}
60 changes: 60 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
version = "3.11.5"
runner.dialect = scala213
style = default
docstrings.style = Asterisk
docstrings.wrap = no
maxColumn = 160
indent.defnSite = 2
includeCurlyBraceInSelectChains = false
assumeStandardLibraryStripMargin = true

# sbt 2 compiles build definitions with Scala 3.
fileOverride {
"glob:**.sbt" {
runner.dialect = scala3
}
"glob:**/project/*.scala" {
runner.dialect = scala3
}
}

align.tokens."+" = [
"<-", #Try to align simple for blocks for readability
"//", #If you have many `//` on the end of lines next to each other, try and align them for readability
{code = "%", owner = "Term.ApplyInfix"}, #SBT specific stuff. Format the dependencies in a way that makes them more readable.
{code = "%%", owner = "Term.ApplyInfix"},
{code = "%%%", owner = "Term.ApplyInfix"}
]

rewrite.rules = [
AvoidInfix
RedundantBraces
RedundantParens
AsciiSortImports
PreferCurlyFors
]

rewrite.avoidInfix.excludeFilters = [
until
to
by
eq
ne
"should.*"
"contain.*"
"must.*"
in
be
taggedAs
thrownBy
synchronized
have
when
size
theSameElementsAs
# Spire's field and group operations read as math, and the curve arithmetic below is unreadable in
# method-call form.
pow
mod
"\\*\\*"
]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![CI](https://github.com/IronCoreLabs/recrypt/actions/workflows/ci.yaml/badge.svg)](https://github.com/IronCoreLabs/recrypt/actions/workflows/ci.yaml)
[![codecov.io](https://codecov.io/github/IronCoreLabs/recrypt/coverage.svg?branch=main)](https://codecov.io/github/IronCoreLabs/recrypt?branch=main)
[![scaladoc](https://javadoc-badge.appspot.com/com.ironcorelabs/recrypt-core_2.12.svg?label=scaladoc)](https://javadoc-badge.appspot.com/com.ironcorelabs/recrypt-core_2.12)
[![scaladoc](https://javadoc.io/badge2/com.ironcorelabs/recrypt-core_2.13/scaladoc.svg)](https://javadoc.io/doc/com.ironcorelabs/recrypt-core_2.13)

This is a library that implements a set of cryptographic primitives that are needed for a _multi-hop proxy re-encryption_ scheme.

Expand Down
13 changes: 6 additions & 7 deletions benchmark/src/main/scala/ApiBenchmark.scala
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
package bench

import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
import cats.effect.unsafe.implicits.global

import com.ironcorelabs.recrypt
import org.openjdk.jmh.annotations._

import java.util.concurrent.TimeUnit

@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.SECONDS)
class ApiBenchmark extends BenchmarkHelper {
final val Ed25519Instance = recrypt.Ed25519Signing(
{ (privateRecryptKey, message) =>
val sig = Ed25519.sign(
//To be safe, just pad out the bytes we get from recrypt.
// To be safe, just pad out the bytes we get from recrypt.
Ed25519.PrivateKey.fromPaddedBytes(privateRecryptKey.bytes),
message
)
recrypt.Signature(sig.bytes)
},
{ (publicRecryptKey, message, signature) =>
(publicRecryptKey, message, signature) =>
Ed25519.verify(
//To be safe, just pad out the bytes we get from recrypt.
// To be safe, just pad out the bytes we get from recrypt.
Ed25519.PublicKey.fromPaddedBytes(publicRecryptKey.bytes),
message,
Ed25519.Signature.fromPaddedBytes(signature.bytes)
)
}
)
val api = new recrypt.Api(randomBytesIO, Ed25519Instance)

Expand Down
8 changes: 4 additions & 4 deletions benchmark/src/main/scala/BenchmarkeHelper.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package bench

import cats.effect.IO
import scodec.bits.ByteVector
import com.ironcorelabs.recrypt.internal.{ Fp, FP12Elem }
import cats.implicits._
import Fp.implicits._
import com.ironcorelabs.recrypt.internal.Fp.implicits._
import com.ironcorelabs.recrypt.internal.{FP12Elem, Fp}
import scodec.bits.ByteVector

trait BenchmarkHelper {
val randomBytesIO: IO[ByteVector] = {
//Start with a known value, we'll just add one to it each time
// Start with a known value, we'll just add one to it each time
var i = 100L
val hashFunc = java.security.MessageDigest.getInstance("SHA-256").digest(_: Array[Byte])
IO {
Expand Down
33 changes: 20 additions & 13 deletions benchmark/src/main/scala/Ed25519.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
*/
package bench

import org.bouncycastle.crypto.params.{ Ed25519PrivateKeyParameters, Ed25519PublicKeyParameters }
import org.bouncycastle.crypto.params.{Ed25519PrivateKeyParameters, Ed25519PublicKeyParameters}
import org.bouncycastle.crypto.signers.Ed25519Signer
import scodec.bits.ByteVector

/**
* Functions and types for working with the ed25519 signature.
*/
object Ed25519 { //scalastyle:ignore
object Ed25519 {

/**
* An Ed25519 Public Key, which always has the correct length, can be used in `verify`
*/
sealed abstract case class PublicKey(bytes: ByteVector) {
override def toString: String = s"Ed25519.PublicKey as Base64: '${bytes.toBase64}'"
}
final object PublicKey {
object PublicKey {
final val Length = 32
final val LengthLong = Length.toLong
private[Ed25519] def create(bytes: ByteVector): PublicKey = new PublicKey(bytes) {}
Expand All @@ -34,9 +35,11 @@ object Ed25519 { //scalastyle:ignore
* because of the interaction with recrypt and lack of sizing guarantees (a la refined) it's needed.
*/
def fromPaddedBytes(bytes: ByteVector): PublicKey =
create(bytes
.take(Length)
.padLeft(Length))
create(
bytes
.take(Length)
.padLeft(Length)
)
}

/**
Expand All @@ -51,18 +54,20 @@ object Ed25519 { //scalastyle:ignore
* `generateKeyPair` to obtain a well-formed key.
*/
sealed abstract case class PrivateKey(bytes: ByteVector) {
//A protection against logging Private keys.
override def toString: String = s"Ed25519.PrivateKey(<BYTES>)"
// A protection against logging Private keys.
override def toString: String = "Ed25519.PrivateKey(<BYTES>)"

/** The 32-byte Ed25519 seed (first half of the NaCl layout). */
def seed: ByteVector = bytes.take(PrivateKey.SeedLength.toLong)

/** The 32-byte public key embedded in this private key (second half of the NaCl layout). */
def embeddedPublicKey: ByteVector = bytes.drop(PrivateKey.SeedLength.toLong)
}
final object PrivateKey {
object PrivateKey {

/** Length of the Ed25519 seed in bytes. */
final val SeedLength = 32

/** Total length of a NaCl-style Ed25519 private key: seed (32) || public key (32). */
final val Length = SeedLength + PublicKey.Length
final val LengthLong = Length.toLong
Expand All @@ -86,7 +91,7 @@ object Ed25519 { //scalastyle:ignore
sealed abstract case class Signature(bytes: ByteVector) {
override def toString: String = s"Signature in Base64: '${bytes.toBase64}'"
}
final object Signature {
object Signature {
val Length = 64
val LengthLong = Length.toLong
private[Ed25519] def create(bytes: ByteVector): Signature = new Signature(bytes) {}
Expand All @@ -103,9 +108,11 @@ object Ed25519 { //scalastyle:ignore
* because of the interaction with recrypt and lack of sizing guarantees (a la refined) it's needed.
*/
def fromPaddedBytes(bytes: ByteVector): Signature =
create(bytes
.take(LengthLong)
.padLeft(LengthLong))
create(
bytes
.take(LengthLong)
.padLeft(LengthLong)
)
}

def verify(key: PublicKey, message: ByteVector, signature: Signature): Boolean = {
Expand Down
8 changes: 3 additions & 5 deletions benchmark/src/main/scala/FP2ElemBenchmark.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package bench

import java.util.concurrent.TimeUnit
import com.ironcorelabs.recrypt.internal.{FP2Elem, Fp}
import org.openjdk.jmh.annotations._

import com.ironcorelabs.recrypt.internal.Fp
import com.ironcorelabs.recrypt.internal.FP2Elem
import java.util.concurrent.TimeUnit

@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
Expand All @@ -21,9 +20,8 @@ class FP2ElemBenchmark extends BenchmarkHelper {
var elements: Vector[FP2Elem[Fp]] = Vector.empty

@Setup
def setup(): Unit = {
def setup(): Unit =
elements = Vector.fill(size)(point)
}

@Benchmark
def addingFP2 = elements.foldLeft(point)((acc, p) => acc + p)
Expand Down
15 changes: 7 additions & 8 deletions benchmark/src/main/scala/GenerateFp12Benchmark.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package bench

import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._

import com.ironcorelabs.recrypt.internal._
import cats.effect.unsafe.implicits.global
import cats.instances.list._
import cats.syntax.traverse._
import cats.effect.unsafe.implicits.global
import com.ironcorelabs.recrypt.internal._
import org.openjdk.jmh.annotations._

import java.util.concurrent.TimeUnit

@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.SECONDS)
Expand All @@ -19,13 +19,12 @@ class GenerateFp12ElemBenchmark extends BenchmarkHelper {

var fp12s: List[FP12Elem[Fp]] = List.empty
private[this] val fp12Elem = pairing.pair(Fp.curvePoints.generator, Fp.curvePoints.g1)
//Old, inferior way of doing fp12 generation. Here just for comparison with FinalExp.
// Old, inferior way of doing fp12 generation. Here just for comparison with FinalExp.
def generateFp12(fp: Fp): FP12Elem[Fp] = fp12Elem ^ fp

@Setup
def setup(): Unit = {
def setup(): Unit =
fp12s = List.fill(size)(randomFp12IO).sequence.unsafeRunSync()
}

@Benchmark
def generateUsingFinalExp() = fp12s.foreach(pairing.finalExponentiation(_))
Expand Down
13 changes: 6 additions & 7 deletions benchmark/src/main/scala/HomogeneousPointBenchmark.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package bench

import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
import cats.effect.unsafe.implicits.global

import com.ironcorelabs.recrypt.internal._
import point.HomogeneousPoint
import cats.implicits._
import com.ironcorelabs.recrypt.internal._
import com.ironcorelabs.recrypt.internal.point.HomogeneousPoint
import org.openjdk.jmh.annotations._

import java.util.concurrent.TimeUnit

@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
Expand All @@ -19,9 +19,8 @@ class HomogeneousPointBenchmark extends BenchmarkHelper {
var points: Vector[HomogeneousPoint[FP2Elem[Fp]]] = Vector.empty

@Setup
def setup(): Unit = {
def setup(): Unit =
points = Vector.fill(size)(randomFp.map(point.times)).sequence.unsafeRunSync()
}

@Benchmark
def add() = points.foldLeft(point)((acc, p) => acc.add(p))
Expand Down
Loading