diff --git a/interpreter/src/main/scala/sigmastate/crypto/CryptoContext.scala b/interpreter/src/main/scala/sigmastate/crypto/CryptoContext.scala index b5aceec849..c0190ba093 100644 --- a/interpreter/src/main/scala/sigmastate/crypto/CryptoContext.scala +++ b/interpreter/src/main/scala/sigmastate/crypto/CryptoContext.scala @@ -2,8 +2,11 @@ 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 descriptor. */ + 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..7b789f1db6 100644 --- a/interpreter/src/main/scala/sigmastate/crypto/CryptoContextJvm.scala +++ b/interpreter/src/main/scala/sigmastate/crypto/CryptoContextJvm.scala @@ -6,9 +6,11 @@ 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 + override def curve: Curve = Platform.Curve(_curve) + + override def fieldCharacteristic: BigInteger = _curve.getField.getCharacteristic override def order: BigInteger = x9params.getN @@ -17,14 +19,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..3a53bdfa29 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) + /** 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) + + /** 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..55bc179e54 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 { + /** 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) + /** 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 + /** 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,11 @@ object Platform { /** Negate a point. */ def negatePoint(p: Ecp): Ecp = Ecp(p.value.negate()) + /** 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. */ 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..a0feb011f9 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 descriptor. */ + type Curve = Platform.Curve + /** Instance of Elliptic Curve point. */ type Ecp = Platform.Ecp + /** Instance of Elliptic Curve field element. */ type ECFieldElem = Platform.ECFieldElem }