Skip to content

Commit 1ccc2cd

Browse files
authored
Migrate the math structs to be using stwo (#12)
* migrate the math structs to stwo * clippy, ibutterfly now takes m31 as itwid
1 parent 65a6e68 commit 1ccc2cd

22 files changed

+86
-422
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ sha2 = "0.10.8"
1212
rand = "0.8.5"
1313
rand_chacha = "0.3.1"
1414
stwo-prover = { git = "https://github.com/starkware-libs/stwo", branch = "dev", commit = "2c8b6e5" }
15+
num-traits = "0.2.0"
1516

1617
[profile.dev]
1718
opt-level = 3

src/channel/bitcoin_script.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ mod test {
6060
use crate::channel::{Channel, ChannelGadget};
6161
use crate::channel_commit::Commitment;
6262
use crate::channel_extract::ExtractorGadget;
63-
use crate::math::{CM31, M31, QM31};
6463
use crate::treepp::*;
6564
use bitcoin_script::script;
6665
use rand::{Rng, RngCore, SeedableRng};
6766
use rand_chacha::ChaCha20Rng;
6867
use rust_bitcoin_m31::qm31_equalverify;
68+
use stwo_prover::core::fields::cm31::CM31;
69+
use stwo_prover::core::fields::m31::M31;
70+
use stwo_prover::core::fields::qm31::QM31;
6971

7072
#[test]
7173
fn test_absorb_commitment() {

src/channel/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::channel_commit::Commitment;
22
use crate::channel_extract::{Extraction5M31, ExtractionQM31, Extractor};
3-
use crate::math::QM31;
43
use crate::utils::trim_m31;
54
use sha2::{Digest, Sha256};
5+
use stwo_prover::core::fields::qm31::QM31;
66

77
mod bitcoin_script;
88
pub use bitcoin_script::*;

src/channel_commit/bitcoin_script.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ impl CommitmentGadget {
3939
#[cfg(test)]
4040
mod test {
4141
use crate::channel_commit::{Commitment, CommitmentGadget};
42-
use crate::math::{CM31, M31, QM31};
4342
use crate::treepp::*;
4443
use bitcoin_script::script;
4544
use rand::{RngCore, SeedableRng};
4645
use rand_chacha::ChaCha20Rng;
46+
use stwo_prover::core::fields::cm31::CM31;
47+
use stwo_prover::core::fields::m31::M31;
48+
use stwo_prover::core::fields::qm31::QM31;
4749

4850
#[test]
4951
fn test_commit_m31() {

src/channel_commit/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use crate::utils::num_to_bytes;
22
use sha2::{Digest, Sha256};
3+
use stwo_prover::core::fields::cm31::CM31;
4+
use stwo_prover::core::fields::m31::M31;
5+
use stwo_prover::core::fields::qm31::QM31;
36

47
mod bitcoin_script;
5-
use crate::math::{CM31, M31, QM31};
68
pub use bitcoin_script::*;
79

810
/// A commitment, which is a 32-byte SHA256 hash

src/channel_extract/bitcoin_script.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,12 @@ impl ExtractorGadget {
199199
#[cfg(test)]
200200
mod test {
201201
use crate::channel_extract::{Extractor, ExtractorGadget};
202-
use crate::math::{Field, M31};
203202
use crate::treepp::*;
204203
use bitcoin_script::script;
204+
use num_traits::Zero;
205205
use rand::{Rng, SeedableRng};
206206
use rand_chacha::ChaCha20Rng;
207+
use stwo_prover::core::fields::m31::M31;
207208

208209
#[test]
209210
fn test_unpack_negative_zero() {
@@ -226,8 +227,6 @@ mod test {
226227
OP_EQUAL
227228
};
228229
let exec_result = execute_script(script);
229-
println!("{:8}", exec_result.final_stack);
230-
println!("{:?}", exec_result.error);
231230
assert!(exec_result.success);
232231
}
233232

src/channel_extract/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use crate::treepp::pushable::{Builder, Pushable};
22
use bitcoin::script::PushBytesBuf;
33
use core::ops::Neg;
4+
use stwo_prover::core::fields::cm31::CM31;
5+
use stwo_prover::core::fields::m31::M31;
6+
use stwo_prover::core::fields::qm31::QM31;
47

58
mod bitcoin_script;
6-
use crate::math::{CM31, M31, QM31};
79
pub use bitcoin_script::*;
810

911
/// Basic hint structure for extracting a single qm31 element.

src/circle/bitcoin_script.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::circle::CirclePoint;
21
use crate::treepp::*;
32
use rust_bitcoin_m31::{m31_add, m31_mul, m31_neg, m31_sub};
3+
use stwo_prover::core::circle::CirclePoint;
4+
use stwo_prover::core::fields::m31::M31;
45

56
/// Gadget for points on the circle curve over the m31 field.
67
/// This is not the secure field.
@@ -16,7 +17,7 @@ impl CirclePointGadget {
1617
}
1718

1819
/// Push a constant point.
19-
pub fn push(point: &CirclePoint) -> Script {
20+
pub fn push(point: &CirclePoint<M31>) -> Script {
2021
script! {
2122
{ point.x.0 }
2223
{ point.y.0 }
@@ -93,13 +94,13 @@ impl CirclePointGadget {
9394

9495
#[cfg(test)]
9596
mod test {
96-
use crate::circle::CirclePoint;
9797
use crate::circle::CirclePointGadget;
98-
use crate::math::M31;
9998
use crate::treepp::*;
10099
use rand_chacha::rand_core::{RngCore, SeedableRng};
101100
use rand_chacha::ChaCha20Rng;
102101
use std::ops::{Add, Sub};
102+
use stwo_prover::core::circle::CirclePoint;
103+
use stwo_prover::core::fields::m31::M31;
103104

104105
#[test]
105106
fn test_double() {

src/circle/mod.rs

-95
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,2 @@
11
mod bitcoin_script;
22
pub use bitcoin_script::*;
3-
4-
use std::ops::{Add, Neg, Sub};
5-
6-
use crate::math::M31;
7-
8-
/// A point on the complex circle. Treated as an additive group.
9-
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
10-
pub struct CirclePoint {
11-
/// x coordinate.
12-
pub x: M31,
13-
/// y coordinate.
14-
pub y: M31,
15-
}
16-
17-
impl CirclePoint {
18-
/// Push the zero point.
19-
pub fn zero() -> Self {
20-
Self {
21-
x: 1.into(),
22-
y: 0.into(),
23-
}
24-
}
25-
26-
/// Double a point.
27-
pub fn double(&self) -> Self {
28-
*self + *self
29-
}
30-
31-
/// Multiply a point with a scalar.
32-
pub fn mul(&self, mut scalar: u128) -> CirclePoint {
33-
let mut res = Self::zero();
34-
let mut cur = *self;
35-
while scalar > 0 {
36-
if scalar & 1 == 1 {
37-
res = res + cur;
38-
}
39-
cur = cur.double();
40-
scalar >>= 1;
41-
}
42-
res
43-
}
44-
45-
/// Double a point repeatedly for n times.
46-
pub fn repeated_double(&self, n: usize) -> Self {
47-
let mut res = *self;
48-
for _ in 0..n {
49-
res = res.double();
50-
}
51-
res
52-
}
53-
54-
/// Negate a point.
55-
pub fn conjugate(&self) -> CirclePoint {
56-
Self {
57-
x: self.x,
58-
y: -self.y,
59-
}
60-
}
61-
62-
/// Compute a subgroup generator for points on the circle curve over the m31 field.
63-
pub fn subgroup_gen(logn: usize) -> Self {
64-
M31_CIRCLE_GEN.repeated_double(M31_CIRCLE_LOG_ORDER - logn)
65-
}
66-
}
67-
impl Add for CirclePoint {
68-
type Output = Self;
69-
70-
fn add(self, rhs: Self) -> Self::Output {
71-
let x = self.x * rhs.x - self.y * rhs.y;
72-
let y = self.x * rhs.y + self.y * rhs.x;
73-
Self { x, y }
74-
}
75-
}
76-
impl Neg for CirclePoint {
77-
type Output = Self;
78-
79-
fn neg(self) -> Self::Output {
80-
self.conjugate()
81-
}
82-
}
83-
impl Sub for CirclePoint {
84-
type Output = Self;
85-
86-
fn sub(self, rhs: Self) -> Self::Output {
87-
self + (-rhs)
88-
}
89-
}
90-
91-
/// The group order of the points on the circle curve over the m31 field.
92-
pub const M31_CIRCLE_LOG_ORDER: usize = 31;
93-
/// A generator of the circle curve over the m31 field.
94-
pub const M31_CIRCLE_GEN: CirclePoint = CirclePoint {
95-
x: M31(2),
96-
y: M31(1268011823),
97-
};

src/circle_secure/bitcoin_script.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use crate::treepp::*;
2+
use num_traits::One;
23
use rust_bitcoin_m31::{
34
m31_add_n31, m31_sub, push_m31_one, push_n31_one, push_qm31_one, qm31_double, qm31_dup,
45
qm31_equalverify, qm31_from_bottom, qm31_mul, qm31_neg, qm31_roll, qm31_rot, qm31_square,
56
qm31_sub, qm31_swap,
67
};
78
use std::ops::{Add, Mul, Neg};
9+
use stwo_prover::core::fields::qm31::QM31;
10+
use stwo_prover::core::fields::{Field, FieldExpOps};
811

9-
use crate::{
10-
channel::ChannelGadget,
11-
math::{Field, QM31},
12-
};
12+
use crate::channel::ChannelGadget;
1313

1414
/// Gadget for points on the circle curve in the qm31 field.
1515
pub struct CirclePointSecureGadget;
@@ -104,18 +104,20 @@ impl CirclePointSecureGadget {
104104

105105
#[cfg(test)]
106106
mod test {
107+
use num_traits::One;
107108
use std::ops::{Add, Mul, Neg};
108109

109110
use crate::treepp::*;
110111
use rand::{Rng, RngCore, SeedableRng};
111112
use rand_chacha::ChaCha20Rng;
112113
use rust_bitcoin_m31::qm31_equalverify;
114+
use stwo_prover::core::fields::m31::M31;
115+
use stwo_prover::core::fields::qm31::QM31;
116+
use stwo_prover::core::fields::{Field, FieldExpOps};
113117

114118
use crate::{
115-
channel::Channel,
116-
channel_extract::ExtractorGadget,
119+
channel::Channel, channel_extract::ExtractorGadget,
117120
circle_secure::bitcoin_script::CirclePointSecureGadget,
118-
math::{Field, M31, QM31},
119121
};
120122

121123
#[test]

src/constraints/bitcoin_script.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::treepp::*;
22
use rust_bitcoin_m31::{qm31_add, qm31_mul, qm31_swap};
33
use std::ops::{Add, Mul, Neg};
4-
5-
use crate::math::QM31;
4+
use stwo_prover::core::fields::qm31::QM31;
65

76
/// Gadget for constraints over the circle curve
87
pub struct ConstraintsGadget;
@@ -48,8 +47,8 @@ mod test {
4847
use rand::{RngCore, SeedableRng};
4948
use rand_chacha::ChaCha20Rng;
5049
use rust_bitcoin_m31::qm31_equalverify;
51-
52-
use crate::math::{M31, QM31};
50+
use stwo_prover::core::fields::m31::M31;
51+
use stwo_prover::core::fields::qm31::QM31;
5352

5453
#[test]
5554
fn test_pair_vanishing() {

src/fri/bitcoin_script.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -227,19 +227,21 @@ impl FRIGadget {
227227
#[cfg(test)]
228228
mod test {
229229
use crate::channel::Channel;
230-
use crate::circle::CirclePoint;
231230
use crate::fri;
232231
use crate::fri::{FRIGadget, N_QUERIES};
233-
use crate::math::Field;
234232
use crate::treepp::*;
235233
use crate::twiddle_merkle_tree::{TwiddleMerkleTree, TWIDDLE_MERKLE_TREE_ROOT_18};
236234
use crate::utils::permute_eval;
237235
use bitcoin::hashes::Hash;
238236
use bitcoin::{TapLeafHash, Transaction};
239237
use bitcoin_scriptexec::{Exec, ExecCtx, Experimental, Options, TxTemplate};
238+
use num_traits::One;
240239
use rand::{Rng, SeedableRng};
241240
use rand_chacha::ChaCha20Rng;
242241
use rust_bitcoin_m31::qm31_equalverify;
242+
use stwo_prover::core::circle::CirclePointIndex;
243+
use stwo_prover::core::fields::m31::M31;
244+
use stwo_prover::core::fields::FieldExpOps;
243245

244246
#[test]
245247
fn test_fiat_shamir() {
@@ -255,15 +257,15 @@ mod test {
255257
let logn = 19;
256258

257259
let proof = {
258-
let p = CirclePoint::subgroup_gen(logn + 1);
260+
let p = CirclePointIndex::subgroup_gen(logn as u32 + 1).to_point();
259261

260262
let mut prng = ChaCha20Rng::seed_from_u64(0);
261263

262264
let mut channel_init_state = [0u8; 32];
263265
channel_init_state.iter_mut().for_each(|v| *v = prng.gen());
264266

265267
let evaluation = (0..(1 << logn))
266-
.map(|i| (p.mul(i * 2 + 1).x.square().square() + 1.into()).into())
268+
.map(|i| (p.mul(i * 2 + 1).x.square().square() + M31::one()).into())
267269
.collect();
268270
let evaluation = permute_eval(evaluation);
269271

@@ -325,10 +327,10 @@ mod test {
325327
channel_init_state.iter_mut().for_each(|v| *v = prng.gen());
326328

327329
let proof = {
328-
let p = CirclePoint::subgroup_gen(logn + 1);
330+
let p = CirclePointIndex::subgroup_gen(logn as u32 + 1).to_point();
329331

330332
let evaluation = (0..(1 << logn))
331-
.map(|i| (p.mul(i * 2 + 1).x.square().square() + 1.into()).into())
333+
.map(|i| (p.mul(i * 2 + 1).x.square().square() + M31::one()).into())
332334
.collect();
333335
let evaluation = permute_eval(evaluation);
334336

@@ -388,10 +390,10 @@ mod test {
388390
channel_init_state.iter_mut().for_each(|v| *v = prng.gen());
389391

390392
let proof = {
391-
let p = CirclePoint::subgroup_gen(logn + 1);
393+
let p = CirclePointIndex::subgroup_gen(logn as u32 + 1).to_point();
392394

393395
let evaluation = (0..(1 << logn))
394-
.map(|i| (p.mul(i * 2 + 1).x.square().square() + 1.into()).into())
396+
.map(|i| (p.mul(i * 2 + 1).x.square().square() + M31::one()).into())
395397
.collect();
396398
let evaluation = permute_eval(evaluation);
397399

@@ -448,10 +450,10 @@ mod test {
448450
channel_init_state.iter_mut().for_each(|v| *v = prng.gen());
449451

450452
let proof = {
451-
let p = CirclePoint::subgroup_gen(logn + 1);
453+
let p = CirclePointIndex::subgroup_gen(logn as u32 + 1).to_point();
452454

453455
let evaluation = (0..(1 << logn))
454-
.map(|i| (p.mul(i * 2 + 1).x.square().square() + 1.into()).into())
456+
.map(|i| (p.mul(i * 2 + 1).x.square().square() + M31::one()).into())
455457
.collect();
456458
let evaluation = permute_eval(evaluation);
457459

@@ -527,15 +529,15 @@ mod test {
527529
let logn = 19;
528530

529531
let proof = {
530-
let p = CirclePoint::subgroup_gen(logn + 1);
532+
let p = CirclePointIndex::subgroup_gen(logn as u32 + 1).to_point();
531533

532534
let mut prng = ChaCha20Rng::seed_from_u64(0);
533535

534536
let mut channel_init_state = [0u8; 32];
535537
channel_init_state.iter_mut().for_each(|v| *v = prng.gen());
536538

537539
let evaluation = (0..(1 << logn))
538-
.map(|i| (p.mul(i * 2 + 1).x.square().square() + 1.into()).into())
540+
.map(|i| (p.mul(i * 2 + 1).x.square().square() + M31::one()).into())
539541
.collect();
540542
let evaluation = permute_eval(evaluation);
541543

0 commit comments

Comments
 (0)