From e991c9284f4144ae50b29d12f9b88b5a69304fe2 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 17 Apr 2023 21:52:15 +0200 Subject: [PATCH 1/2] missing-methods: add CryptoContext.curve, CryptoFacade.encodePoint, and getCurve --- .../scala/sigmastate/crypto/CryptoContext.scala | 3 +++ .../sigmastate/crypto/CryptoContextJvm.scala | 13 ++++++++----- .../scala/sigmastate/crypto/CryptoFacade.scala | 13 +++++++++++++ .../main/scala/sigmastate/crypto/Platform.scala | 16 +++++++++++++++- .../main/scala/sigmastate/crypto/package.scala | 4 ++++ 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/interpreter/src/main/scala/sigmastate/crypto/CryptoContext.scala b/interpreter/src/main/scala/sigmastate/crypto/CryptoContext.scala index b5aceec849..21b5282cdd 100644 --- a/interpreter/src/main/scala/sigmastate/crypto/CryptoContext.scala +++ b/interpreter/src/main/scala/sigmastate/crypto/CryptoContext.scala @@ -4,6 +4,9 @@ import java.math.BigInteger /** A context for cryptographic operations. */ abstract class CryptoContext { + /** The underlying elliptic curve. */ + def curve: Curve + /** The characteristics of the underlying finite field. */ def fieldCharacteristic: BigInteger diff --git a/interpreter/src/main/scala/sigmastate/crypto/CryptoContextJvm.scala b/interpreter/src/main/scala/sigmastate/crypto/CryptoContextJvm.scala index 6a3420242f..7ca4306490 100644 --- a/interpreter/src/main/scala/sigmastate/crypto/CryptoContextJvm.scala +++ b/interpreter/src/main/scala/sigmastate/crypto/CryptoContextJvm.scala @@ -6,9 +6,12 @@ import java.math.BigInteger /** JVM implementation of context for cryptographic operations using Bouncycastle. */ class CryptoContextJvm(x9params: X9ECParameters) extends CryptoContext { - private lazy val curve = x9params.getCurve + private lazy val _curve = x9params.getCurve - override def fieldCharacteristic: BigInteger = curve.getField.getCharacteristic + /** The underlying elliptic curve. */ + override def curve: Curve = Platform.Curve(_curve) + + override def fieldCharacteristic: BigInteger = _curve.getField.getCharacteristic override def order: BigInteger = x9params.getN @@ -17,14 +20,14 @@ class CryptoContextJvm(x9params: X9ECParameters) extends CryptoContext { } override def validatePoint(x: BigInteger, y: BigInteger): Ecp = { - Platform.Ecp(curve.validatePoint(x, y)) + Platform.Ecp(_curve.validatePoint(x, y)) } override def infinity(): Ecp = { - Platform.Ecp(curve.getInfinity) + Platform.Ecp(_curve.getInfinity) } override def decodePoint(encoded: Array[Byte]): Ecp = { - Platform.Ecp(curve.decodePoint(encoded)) + Platform.Ecp(_curve.decodePoint(encoded)) } } diff --git a/interpreter/src/main/scala/sigmastate/crypto/CryptoFacade.scala b/interpreter/src/main/scala/sigmastate/crypto/CryptoFacade.scala index 54ec07940c..87576f256d 100644 --- a/interpreter/src/main/scala/sigmastate/crypto/CryptoFacade.scala +++ b/interpreter/src/main/scala/sigmastate/crypto/CryptoFacade.scala @@ -48,6 +48,19 @@ object CryptoFacade { /** Returns byte representation of the given field element. */ def encodeFieldElem(p: ECFieldElem): Array[Byte] = Platform.encodeFieldElem(p) + /** Returns byte representation of the given point. + * + * @param p point to encode + * @param compressed if true, generates a compressed point encoding + */ + def encodePoint(p: Ecp, compressed: Boolean): Array[Byte] = Platform.encodePoint(p, compressed) + + /** Returns a [[Curve]] instance describing the elliptic curve of the point p + * + * @param p the elliptic curve point + */ + def getCurve(p: Ecp): Curve = Platform.getCurve(p) + /** Returns the x-coordinate. * * Caution: depending on the curve's coordinate system, this may not be the same value as in an diff --git a/interpreter/src/main/scala/sigmastate/crypto/Platform.scala b/interpreter/src/main/scala/sigmastate/crypto/Platform.scala index 5cb4a9f345..db15ae729a 100644 --- a/interpreter/src/main/scala/sigmastate/crypto/Platform.scala +++ b/interpreter/src/main/scala/sigmastate/crypto/Platform.scala @@ -1,12 +1,17 @@ package sigmastate.crypto import org.bouncycastle.crypto.ec.CustomNamedCurves -import org.bouncycastle.math.ec.{ECFieldElement, ECPoint} +import org.bouncycastle.math.ec.{ECPoint, ECFieldElement, ECCurve} import java.math.BigInteger /** JVM specific implementation of crypto methods*/ object Platform { + /** Returns a [[Curve]] instance describing the elliptic curve of the point p + * @param p the elliptic curve point + */ + def getCurve(p: Ecp): Curve = Curve(p.value.getCurve) + /** Returns the x-coordinate. * * Caution: depending on the curve's coordinate system, this may not be the same value as in an @@ -46,6 +51,12 @@ object Platform { /** Returns byte representation of the given field element. */ def encodeFieldElem(p: ECFieldElem): Array[Byte] = p.value.getEncoded + /** Returns byte representation of the given point. + * @param p point to encode + * @param compressed if true, generates a compressed point encoding + */ + def encodePoint(p: Ecp, compressed: Boolean): Array[Byte] = p.value.getEncoded(compressed) + /** Returns the value of bit 0 in BigInteger representation of this point. */ def signOf(p: ECFieldElem): Boolean = p.value.testBitZero() @@ -94,6 +105,9 @@ object Platform { /** Negate a point. */ def negatePoint(p: Ecp): Ecp = Ecp(p.value.negate()) + /** Wrapper for curve type. */ + case class Curve(private[crypto] val value: ECCurve) + /** Wrapper for point type. */ case class Ecp(private[crypto] val value: ECPoint) diff --git a/interpreter/src/main/scala/sigmastate/crypto/package.scala b/interpreter/src/main/scala/sigmastate/crypto/package.scala index 24d797be6f..2d6f1e8dbb 100644 --- a/interpreter/src/main/scala/sigmastate/crypto/package.scala +++ b/interpreter/src/main/scala/sigmastate/crypto/package.scala @@ -1,8 +1,12 @@ package sigmastate package object crypto { + /** Instance of Elliptic Curve. */ + type Curve = Platform.Curve + /** Instance of Elliptic Curve point. */ type Ecp = Platform.Ecp + /** Instance of Elliptic Curve field element. */ type ECFieldElem = Platform.ECFieldElem } From 52219026afc7cb07248195dd9559a9bd023851a9 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Thu, 20 Apr 2023 18:13:59 +0200 Subject: [PATCH 2/2] missing-methods: polishing ScalaDocs --- .../src/main/scala/sigmastate/crypto/CryptoContext.scala | 4 ++-- .../main/scala/sigmastate/crypto/CryptoContextJvm.scala | 1 - .../src/main/scala/sigmastate/crypto/CryptoFacade.scala | 4 ++-- .../src/main/scala/sigmastate/crypto/Platform.scala | 8 +++++--- .../src/main/scala/sigmastate/crypto/package.scala | 2 +- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/interpreter/src/main/scala/sigmastate/crypto/CryptoContext.scala b/interpreter/src/main/scala/sigmastate/crypto/CryptoContext.scala index 21b5282cdd..c0190ba093 100644 --- a/interpreter/src/main/scala/sigmastate/crypto/CryptoContext.scala +++ b/interpreter/src/main/scala/sigmastate/crypto/CryptoContext.scala @@ -2,9 +2,9 @@ package sigmastate.crypto import java.math.BigInteger -/** A context for cryptographic operations. */ +/** A context for cryptographic operations over elliptic curve group. */ abstract class CryptoContext { - /** The underlying elliptic curve. */ + /** The underlying elliptic curve descriptor. */ def curve: Curve /** The characteristics of the underlying finite field. */ diff --git a/interpreter/src/main/scala/sigmastate/crypto/CryptoContextJvm.scala b/interpreter/src/main/scala/sigmastate/crypto/CryptoContextJvm.scala index 7ca4306490..7b789f1db6 100644 --- a/interpreter/src/main/scala/sigmastate/crypto/CryptoContextJvm.scala +++ b/interpreter/src/main/scala/sigmastate/crypto/CryptoContextJvm.scala @@ -8,7 +8,6 @@ import java.math.BigInteger class CryptoContextJvm(x9params: X9ECParameters) extends CryptoContext { private lazy val _curve = x9params.getCurve - /** The underlying elliptic curve. */ override def curve: Curve = Platform.Curve(_curve) override def fieldCharacteristic: BigInteger = _curve.getField.getCharacteristic diff --git a/interpreter/src/main/scala/sigmastate/crypto/CryptoFacade.scala b/interpreter/src/main/scala/sigmastate/crypto/CryptoFacade.scala index 87576f256d..3a53bdfa29 100644 --- a/interpreter/src/main/scala/sigmastate/crypto/CryptoFacade.scala +++ b/interpreter/src/main/scala/sigmastate/crypto/CryptoFacade.scala @@ -48,14 +48,14 @@ object CryptoFacade { /** Returns byte representation of the given field element. */ def encodeFieldElem(p: ECFieldElem): Array[Byte] = Platform.encodeFieldElem(p) - /** Returns byte representation of the given point. + /** Byte representation of the given point. * * @param p point to encode * @param compressed if true, generates a compressed point encoding */ def encodePoint(p: Ecp, compressed: Boolean): Array[Byte] = Platform.encodePoint(p, compressed) - /** Returns a [[Curve]] instance describing the elliptic curve of the point p + /** A [[Curve]] instance describing the elliptic curve of the point p * * @param p the elliptic curve point */ diff --git a/interpreter/src/main/scala/sigmastate/crypto/Platform.scala b/interpreter/src/main/scala/sigmastate/crypto/Platform.scala index db15ae729a..55bc179e54 100644 --- a/interpreter/src/main/scala/sigmastate/crypto/Platform.scala +++ b/interpreter/src/main/scala/sigmastate/crypto/Platform.scala @@ -7,7 +7,7 @@ import java.math.BigInteger /** JVM specific implementation of crypto methods*/ object Platform { - /** Returns a [[Curve]] instance describing the elliptic curve of the point p + /** Description of elliptic curve of point `p` which belongs to the curve. * @param p the elliptic curve point */ def getCurve(p: Ecp): Curve = Curve(p.value.getCurve) @@ -51,7 +51,7 @@ object Platform { /** Returns byte representation of the given field element. */ def encodeFieldElem(p: ECFieldElem): Array[Byte] = p.value.getEncoded - /** Returns byte representation of the given point. + /** Byte representation of the given point. * @param p point to encode * @param compressed if true, generates a compressed point encoding */ @@ -105,7 +105,9 @@ object Platform { /** Negate a point. */ def negatePoint(p: Ecp): Ecp = Ecp(p.value.negate()) - /** Wrapper for curve type. */ + /** Wrapper for curve descriptor. Serves as the concrete implementation of the + * [[sigmastate.crypto.Curve]] type in JVM. + */ case class Curve(private[crypto] val value: ECCurve) /** Wrapper for point type. */ diff --git a/interpreter/src/main/scala/sigmastate/crypto/package.scala b/interpreter/src/main/scala/sigmastate/crypto/package.scala index 2d6f1e8dbb..a0feb011f9 100644 --- a/interpreter/src/main/scala/sigmastate/crypto/package.scala +++ b/interpreter/src/main/scala/sigmastate/crypto/package.scala @@ -1,7 +1,7 @@ package sigmastate package object crypto { - /** Instance of Elliptic Curve. */ + /** Instance of Elliptic Curve descriptor. */ type Curve = Platform.Curve /** Instance of Elliptic Curve point. */