Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing CryptoFacade methods #874

Merged
merged 2 commits into from
Apr 21, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
}
}
13 changes: 13 additions & 0 deletions interpreter/src/main/scala/sigmastate/crypto/CryptoFacade.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion interpreter/src/main/scala/sigmastate/crypto/Platform.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions interpreter/src/main/scala/sigmastate/crypto/package.scala
Original file line number Diff line number Diff line change
@@ -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
}