Skip to content

Commit

Permalink
renaming some things
Browse files Browse the repository at this point in the history
  • Loading branch information
logicalmechanism committed Jun 11, 2024
1 parent d497412 commit 6f164e5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 42 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions lib/seedelf/elgamal.ak
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use aiken/builtin
use aiken/hash
use seedelf/bls12_381 as bls
use seedelf/types/zk.{Register}
use seedelf/bls
use seedelf/types/register.{Register}

pub type CypherText {
// encrypted point
Expand Down
30 changes: 19 additions & 11 deletions lib/seedelf/signing.ak
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
use aiken/builtin
use aiken/bytearray
use aiken/hash
use seedelf/bls12_381 as bls
use seedelf/types/zk.{Register, ZK}
use seedelf/bls
use seedelf/types/register.{Register}

/// The zero knowledge elements required for the proof.
pub type FiatShamirRedeemer {
// this is z = r + c*x
z: ByteArray,
// this is the g^r compressed G1Element
g_r: ByteArray,
}

/// A variation of a Fiat-Shamir signature scheme. Sign some message m using
/// a secret key but without revealing the value in the process. This uses
Expand All @@ -14,7 +22,7 @@ use seedelf/types/zk.{Register, ZK}
/// ```aiken
/// signing.verify(msg, datum, redeemer)
/// ```
pub fn verify(m: ByteArray, datum: Register, redeemer: ZK) -> Bool {
pub fn verify(m: ByteArray, datum: Register, redeemer: FiatShamirRedeemer) -> Bool {
//
// hash the message and produce the challenge value e
let h: ByteArray = hash.sha3_256(m)
Expand Down Expand Up @@ -71,8 +79,8 @@ test good_verify() {
// calculate z
let z: Int = r + x * e
// build zk data
let redeemer: ZK =
ZK { z: builtin.integer_to_bytearray(True, 0, z), g_r: grb }
let redeemer: FiatShamirRedeemer =
FiatShamirRedeemer { z: builtin.integer_to_bytearray(True, 0, z), g_r: grb }
verify(m, datum, redeemer)
}

Expand All @@ -89,8 +97,8 @@ test rerandomized_good_verify() {
// The re-randomizer number
let d: Int =
53767766789778895376051712452456827203944826866229066056242444673395870447090
// rerandomizing the a0 register
let datum: Register = zk.rerandomizing(a0, d)
// rerandomize the a0 register
let datum: Register = register.rerandomize(a0, d)
// this message to sign
let m: ByteArray = #"acab"
// a random number
Expand All @@ -107,8 +115,8 @@ test rerandomized_good_verify() {
// calculate z
let z: Int = r + x * e
// build zk data
let redeemer: ZK =
ZK { z: builtin.integer_to_bytearray(True, 0, z), g_r: grb }
let redeemer: FiatShamirRedeemer =
FiatShamirRedeemer { z: builtin.integer_to_bytearray(True, 0, z), g_r: grb }
verify(m, datum, redeemer)
}

Expand Down Expand Up @@ -138,7 +146,7 @@ test bad_verify() fail {
// calculate z
let z: Int = r + x * e
// build zk data
let redeemer: ZK =
ZK { z: builtin.integer_to_bytearray(True, 0, z), g_r: grb }
let redeemer: FiatShamirRedeemer =
FiatShamirRedeemer { z: builtin.integer_to_bytearray(True, 0, z), g_r: grb }
verify(#"beef", datum, redeemer)
}
41 changes: 25 additions & 16 deletions lib/seedelf/spending.ak
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@
use aiken/builtin
use aiken/bytearray
use aiken/hash
use seedelf/bls12_381 as bls
use seedelf/types/zk.{Register, ZK}
use seedelf/bls
use seedelf/types/register.{Register}

/// The zero knowledge elements required for the proof. The c value will be
/// computed using the Fiat-Shamir heuristic.
pub type SchnorrRedeemer {
// this is z = r + c*x
z: ByteArray,
// this is the g^r compressed G1Element
g_r: ByteArray,
}

/// A bytearray of a value for the challenge c. This process should act like a
/// random oracle providing a large challenge value for the user. The inputs
Expand Down Expand Up @@ -58,7 +67,7 @@ test real_fiat_shamir_transform2() {
/// ```aiken
/// spending.d_log(datum, redeemer)
/// ```
pub fn d_log(datum: Register, redeemer: ZK) -> Bool {
pub fn d_log(datum: Register, redeemer: SchnorrRedeemer) -> Bool {
//
// get the z integer from the redeemer
let z: Int = builtin.bytearray_to_integer(True, redeemer.z)
Expand Down Expand Up @@ -117,15 +126,15 @@ test d_log_256_bit_secret() {
// the z value
let z: Int = r + c * x
// build zk data
let zk: ZK =
ZK {
let zk: SchnorrRedeemer =
SchnorrRedeemer {
z: builtin.integer_to_bytearray(True, 0, z),
g_r: g_r |> builtin.bls12_381_g1_compress,
}
d_log(a0, zk)
}

test can_unlock_after_rerandomizing() {
test can_unlock_after_rerandomize() {
// some secret x
let x: Int =
86478456268645743314319142250097583782656584143510069178425111699273215020899
Expand All @@ -138,8 +147,8 @@ test can_unlock_after_rerandomizing() {
// The re-randomizer number
let d: Int =
53767766789778895376051712452456827203944826866229066056242444673395870447090
// rerandomizing the a0 register
let a1: Register = zk.rerandomizing(a0, d)
// rerandomize the a0 register
let a1: Register = register.rerandomize(a0, d)
// a random number
let r: Int =
44421586105950619360037151099874190412588687312032470042646096642156420779682
Expand All @@ -158,8 +167,8 @@ test can_unlock_after_rerandomizing() {
// the z value
let z: Int = r + c * x
// build zk data
let zk: ZK =
ZK {
let zk: SchnorrRedeemer =
SchnorrRedeemer {
z: builtin.integer_to_bytearray(True, 0, z),
g_r: g_r |> builtin.bls12_381_g1_compress,
}
Expand Down Expand Up @@ -192,8 +201,8 @@ test cant_spend_good_verify_msg() fail {
// calculate z
let z: Int = r + x * e
// build zk data
let redeemer: ZK =
ZK { z: builtin.integer_to_bytearray(True, 0, z), g_r: grb }
let redeemer: SchnorrRedeemer =
SchnorrRedeemer { z: builtin.integer_to_bytearray(True, 0, z), g_r: grb }
d_log(datum, redeemer)
}

Expand All @@ -211,8 +220,8 @@ test many_unlocks() {
// The re-randomizer number
let d: Int =
53767766789778895376051712452456827203944826866229066056242444673395870447090
// rerandomizing the a0 register
let a1: Register = zk.rerandomizing(a0, d)
// rerandomize the a0 register
let a1: Register = register.rerandomize(a0, d)
// a random number
let r: Int =
44421586105950619360037151099874190412588687312032470042646096642156420779682
Expand All @@ -231,8 +240,8 @@ test many_unlocks() {
// the z value
let z: Int = r + c * x
// build zk data
let zk: ZK =
ZK {
let zk: SchnorrRedeemer =
SchnorrRedeemer {
z: builtin.integer_to_bytearray(True, 0, z),
g_r: g_r |> builtin.bls12_381_g1_compress,
}
Expand Down
15 changes: 5 additions & 10 deletions lib/seedelf/types/zk.ak → lib/seedelf/types/register.ak
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@ pub type Register {
beta: ByteArray,
}

/// The zero knowledge elements required for the proof. The c value will be
/// computed using the Fiat-Shamir heuristic.
pub type ZK {
// this is z = r + c*x
z: ByteArray,
// this is the g^r compressed G1Element
g_r: ByteArray,
}

/// This simulates re-randomizing a register into a new one. It is used for
/// testing purposes only. This function will be used in the off-chain as it
/// is the method for creating a new private address for some user.
pub fn rerandomizing(datum: Register, rng: Int) -> Register {
///
/// ```aiken
/// register.rerandomize(datum, scaler)
/// ```
pub fn rerandomize(datum: Register, rng: Int) -> Register {
// get the (g, u) element in their uncompressed form
let g: G1Element = builtin.bls12_381_g1_uncompress(datum.alpha)
let u: G1Element = builtin.bls12_381_g1_uncompress(datum.beta)
Expand Down
10 changes: 7 additions & 3 deletions validators/wallet.ak
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
////

use aiken/transaction.{ScriptContext, Spend}
use seedelf/spending
use seedelf/types/zk.{Register, ZK}
use seedelf/spending.{SchnorrRedeemer}
use seedelf/types/register.{Register}

validator(
// this allows many wallet contracts to exist
_random_string: ByteArray,
) {
fn params(datum: Register, redeemer: ZK, context: ScriptContext) -> Bool {
fn params(
datum: Register,
redeemer: SchnorrRedeemer,
context: ScriptContext,
) -> Bool {
expect Spend(_) = context.purpose
spending.d_log(datum, redeemer)?
}
Expand Down

0 comments on commit 6f164e5

Please sign in to comment.