-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] pre-compute a lookup table for bn256::scalarfield (#46)
* [feat] use pre-computed table for bn256::scalarfield * [chore] cargo fmt * [feat] turn off bn256-table by default --------- Co-authored-by: Han <[email protected]>
- Loading branch information
1 parent
cf57ee9
commit e97adcb
Showing
10 changed files
with
141 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,7 @@ | |
Cargo.lock | ||
**/*.rs.bk | ||
.vscode | ||
**/*.html | ||
**/*.html | ||
|
||
# script generated source code | ||
src/bn256/fr/table.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# This file generates the montogomary form integers for x in [0, 2^16) \intersect | ||
# BN::ScalarField | ||
|
||
verbose = False | ||
|
||
modulus = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001 | ||
R = 2**256 % modulus | ||
table_size = 1<<16 | ||
|
||
# @input: field element a | ||
# @output: 4 u64 a0, a1, a2, a3 s.t. | ||
# a = a3 * 2^192 + a2 * 2^128 + a1 * 2^64 + a0 | ||
def decompose_field_element(a): | ||
a0 = a % 2**64 | ||
a = a // 2**64 | ||
a1 = a % 2**64 | ||
a = a // 2**64 | ||
a2 = a % 2**64 | ||
a = a // 2**64 | ||
a3 = a | ||
return [a0, a1, a2, a3] | ||
|
||
|
||
# @input: field element a | ||
# @output: a rust format string that encodes | ||
# 4 u64 a0, a1, a2, a3 s.t. | ||
# a = a3 * 2^192 + a2 * 2^128 + a1 * 2^64 + a0 | ||
def format_field_element(a): | ||
[a0, a1, a2, a3] = decompose_field_element(a); | ||
return "Fr([" + hex(a0) + "," + hex(a1) + "," + hex(a2) + "," + hex(a3) + "]),\n" | ||
|
||
|
||
f = open("src/bn256/fr/table.rs", "w") | ||
f.write("//! auto generated file from scripts/bn256.sage, do not modify\n") | ||
f.write("//! see src/bn256/fr.rs for more details\n") | ||
f.write("use super::Fr;\n") | ||
f.write("pub const FR_TABLE: &[Fr] = &[\n") | ||
|
||
for i in range(table_size): | ||
a = (i * R) % modulus | ||
if verbose: | ||
print (i, a, format_field_element(a)) | ||
f.write(format_field_element(a)) | ||
|
||
f.write("\n];") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use crate::bn256::{Fr, FR_TABLE}; | ||
|
||
#[test] | ||
fn test_table() { | ||
for (i, e) in FR_TABLE.iter().enumerate() { | ||
assert_eq!(Fr::from(i as u64), *e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters