22////
33
44use aiken/ crypto.{VerificationKeyHash }
5+ use aiken/ crypto/ bitwise.{State }
56use aiken/ crypto/ bls12_381/ g1
67use aiken/ crypto/ bls12_381/ scalar.{Scalar }
78use aiken/ primitive/ bytearray
@@ -61,7 +62,7 @@ test real_fiat_shamir_transform() {
6162}
6263
6364/// Internal Use Only
64- fn randomize (datum: Register , s: Scalar ) -> Register {
65+ fn randomize (datum: Register , s: State < Scalar > ) -> Register {
6566 // decompress the generator and public value
6667 let g: G1Element = g1.decompress (datum.generator)
6768 let u: G1Element = g1.decompress (datum.public_value)
@@ -106,13 +107,13 @@ pub fn verify(
106107 // = g^r * (g^x)^c
107108 // = g^r * u^c
108109 //
109- expect Some (z): Option < Scalar > = scalar.from_bytearray_big_endian (z_b)
110+ let z: State < Scalar > = scalar.from_bytes (z_b)
110111 let g_z: G1Element = generator |> g1.decompress |> g1.scale (z)
111112 //
112113 // use the fiat-shamir heuristic to calculate the challenge then convert it to an scalar
113- expect Some (c): Option < Scalar > =
114+ let c: State < Scalar > =
114115 fiat_shamir_heuristic (generator, g_r_b, public_value, bound)
115- |> scalar.from_bytearray_big_endian ()
116+ |> scalar.from_bytes
116117 //
117118 // the u^c computation: u^c = (g^x)^c = g^(x * c)
118119 let u_c: G1Element = public_value |> g1.decompress |> g1.scale (c)
@@ -124,8 +125,8 @@ pub fn verify(
124125
125126test valid_schnorr_proof () {
126127 // some secret x
127- expect Some (x): Option < Scalar > =
128- scalar.new (
128+ let x: State < Scalar > =
129+ scalar.from_int (
129130 42435875175126190479447740508185965837690552500527637822603658699938581184513 ,
130131 )
131132 // the datum register using the g1 generator and the public value for x
@@ -135,8 +136,8 @@ test valid_schnorr_proof() {
135136 public_value: g1.generator |> g1.scale (x) |> g1.compress,
136137 }
137138 // a random number
138- expect Some (r): Option < Scalar > =
139- scalar.new (
139+ let r: State < Scalar > =
140+ scalar.from_int (
140141 32435875175126190479447740508185965837690552500527637822603658699938581184513 ,
141142 )
142143 // the bound, something unique from the tx
@@ -148,10 +149,10 @@ test valid_schnorr_proof() {
148149 // the challenge number using a fiat shamir transform
149150 let c_b: ByteArray =
150151 fiat_shamir_heuristic (datum.generator, g_r_b, datum.public_value, bound)
151- expect Some (c): Option < Scalar > = scalar.from_bytearray_big_endian (c_b)
152+ let c: State < Scalar > = scalar.from_bytes (c_b)
152153 // the z value
153- let z: Scalar = scalar.mul (c, x) |> scalar.add (r)
154- let z_b: ByteArray = scalar.to_bytearray_big_endian (z, 0 )
154+ let z: State < Scalar > = scalar.mul (c, x) |> scalar.add (r)
155+ let z_b: ByteArray = scalar.to_bytes (z )
155156 // trace datum.generator
156157 // trace datum.public_value
157158 // trace z_b
@@ -162,8 +163,8 @@ test valid_schnorr_proof() {
162163
163164test randomized_valid_schnorr_proof () {
164165 // some secret x
165- expect Some (x): Option < Scalar > =
166- scalar.new (
166+ let x: State < Scalar > =
167+ scalar.from_int (
167168 12435875175126190479447740508185965837690552500527637822603658699938581184513 ,
168169 )
169170 // the datum register using the g1 generator and the public value for x
@@ -173,13 +174,13 @@ test randomized_valid_schnorr_proof() {
173174 public_value: g1.generator |> g1.scale (x) |> g1.compress,
174175 }
175176 // a random number
176- expect Some (r): Option < Scalar > =
177- scalar.new (
177+ let r: State < Scalar > =
178+ scalar.from_int (
178179 32435875175126190479447740508185965837690552500527637822603658699938581184513 ,
179180 )
180181 // another random number
181- expect Some (d): Option < Scalar > =
182- scalar.new (
182+ let d: State < Scalar > =
183+ scalar.from_int (
183184 12435875175126190479447740508185965837690552500527637822603658699938581184513 ,
184185 )
185186 // rerandomize the a0 register
@@ -198,10 +199,10 @@ test randomized_valid_schnorr_proof() {
198199 datum_rng.public_value,
199200 bound,
200201 )
201- expect Some (c): Option < Scalar > = scalar.from_bytearray_big_endian (c_b)
202+ let c: State < Scalar > = scalar.from_bytes (c_b)
202203 // the z value
203- let z: Scalar = scalar.mul (c, x) |> scalar.add (r)
204- let z_b: ByteArray = scalar.to_bytearray_big_endian (z, 0 )
204+ let z: State < Scalar > = scalar.mul (c, x) |> scalar.add (r)
205+ let z_b: ByteArray = scalar.to_bytes (z )
205206 // trace datum_rng.generator
206207 // trace datum_rng.public_value
207208 // trace z_b
@@ -212,8 +213,8 @@ test randomized_valid_schnorr_proof() {
212213
213214test invalid_schnorr_proof () fail {
214215 // some secret x
215- expect Some (x): Option < Scalar > =
216- scalar.new (
216+ let x: State < Scalar > =
217+ scalar.from_int (
217218 42435875175126190479447740508185965837690552500527637822603658699938581184513 ,
218219 )
219220 // the datum register using the g1 generator and the public value for x
@@ -223,8 +224,8 @@ test invalid_schnorr_proof() fail {
223224 public_value: g1.generator |> g1.scale (x) |> g1.compress,
224225 }
225226 // a random number
226- expect Some (r): Option < Scalar > =
227- scalar.new (
227+ let r: State < Scalar > =
228+ scalar.from_int (
228229 32435875175126190479447740508185965837690552500527637822603658699938581184513 ,
229230 )
230231 // the bound, something unique from the tx
@@ -236,25 +237,25 @@ test invalid_schnorr_proof() fail {
236237 // the challenge number using a fiat shamir transform
237238 let c_b: ByteArray =
238239 fiat_shamir_heuristic (datum.generator, g_r_b, datum.public_value, bound)
239- expect Some (c): Option < Scalar > = scalar.from_bytearray_big_endian (c_b)
240+ let c: State < Scalar > = scalar.from_bytes (c_b)
240241 // the bad z value, it assumes the secret is the challenge
241- let z: Scalar = scalar.mul (c, c) |> scalar.add (r)
242- let z_b: ByteArray = scalar.to_bytearray_big_endian (z, 0 )
242+ let z: State < Scalar > = scalar.mul (c, c) |> scalar.add (r)
243+ let z_b: ByteArray = scalar.to_bytes (z )
243244 verify (datum.generator, datum.public_value, z_b, g_r_b, bound)
244245}
245246
246247// shows how it is constant time
247248test simple_schnorr_proof () {
248249 // some secret x
249- expect Some (x): Option < Scalar > = scalar.new (44203 )
250+ let x: State < Scalar > = scalar.from_int (44203 )
250251 // the datum register using the g1 generator and the public value for x
251252 let datum: Register =
252253 Register {
253254 generator: g1.generator |> g1.compress,
254255 public_value: g1.generator |> g1.scale (x) |> g1.compress,
255256 }
256257 // a random number
257- expect Some (r): Option < Scalar > = scalar.new (1337 )
258+ let r: State < Scalar > = scalar.from_int (1337 )
258259 // the bound, something unique from the tx
259260 let bound: ByteArray = #"acab"
260261 // calculate the g^r term
@@ -264,16 +265,16 @@ test simple_schnorr_proof() {
264265 // the challenge number using a fiat shamir transform
265266 let c_b: ByteArray =
266267 fiat_shamir_heuristic (datum.generator, g_r_b, datum.public_value, bound)
267- expect Some (c): Option < Scalar > = scalar.from_bytearray_big_endian (c_b)
268+ let c: State < Scalar > = scalar.from_bytes (c_b)
268269 // the z value
269- let z: Scalar = scalar.mul (c, x) |> scalar.add (r)
270- let z_b: ByteArray = scalar.to_bytearray_big_endian (z, 0 )
270+ let z: State < Scalar > = scalar.mul (c, x) |> scalar.add (r)
271+ let z_b: ByteArray = scalar.to_bytes (z )
271272 verify (datum.generator, datum.public_value, z_b, g_r_b, bound)
272273}
273274
274275test simple_regsiter () {
275276 // some secret x
276- expect Some (x): Option < Scalar > = scalar.new (18446744073709551606 )
277+ let x: State < Scalar > = scalar.from_int (18446744073709551606 )
277278 // the datum register using the g1 generator and the public value for x
278279 let datum: Register =
279280 Register {
0 commit comments