forked from privacy-scaling-explorations/greco
-
Notifications
You must be signed in to change notification settings - Fork 1
(S)ponge (A)PI for (F)ield (E)lements Implementation #9
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
cf37fe4
WIP1
ozgurarmanc 926a0c2
implementation with ENUM for future optimization
ozgurarmanc 7678222
tag input
ozgurarmanc 1f537c6
deleted tag calculation and corrected some parts
ozgurarmanc 4726ada
domain seperator calculation
ozgurarmanc 527d5d9
added test and some algorithm fixes
ozgurarmanc 0e531bd
small fix
ozgurarmanc d2b6394
added safe to pk_encryption circuit
ozgurarmanc 68b7683
rs-script io calculation
ozgurarmanc db4dca8
rm constants
ozgurarmanc 9fc1392
minor fix
ozgurarmanc 8467eb0
resolve conversations
ozgurarmanc 5380803
remove zeroes.json
ozgurarmanc 9bf8fbe
fix
ozgurarmanc b101dd8
carry out to sponge
ozgurarmanc cff8837
multi gamma
ozgurarmanc a90f5ef
small fix
ozgurarmanc c599185
final fix
ozgurarmanc 3a3cc50
Merge branch 'noir' into safe-api
ozgurarmanc 83bdf5a
Update lib.nr
ozgurarmanc 8538913
Update main.rs
ozgurarmanc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod polynomial; | ||
pub mod pk_encryption; | ||
pub mod safe; |
This file contains hidden or 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 hidden or 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,81 @@ | ||
use super::pk_enc_constants_1024_2x52_2048::TAG; | ||
use poseidon::poseidon2_permutation; | ||
|
||
global RATE: u32 = 3; | ||
global CAPACITY: u32 = 1; | ||
global WIDTH: u32 = 4; | ||
|
||
pub struct SafeSponge<let L: u32, let S: u32> { | ||
state: [Field; WIDTH], | ||
out: Vec<Field>, | ||
absorb_pos: u32, | ||
squeeze_pos: u32, | ||
io_pattern: [u32; L], | ||
io_count: u32, | ||
} | ||
|
||
impl<let L: u32, let S: u32> SafeSponge<L, S> { | ||
pub fn start(pattern: [u32; L]) -> SafeSponge<L, S> { | ||
let mut sponge = SafeSponge::<L, S> { | ||
state: [0; WIDTH], | ||
out: Vec::new(), | ||
absorb_pos: 0, | ||
squeeze_pos: 0, | ||
io_pattern: pattern, | ||
io_count: 0, | ||
}; | ||
sponge.state[0] = TAG; | ||
sponge | ||
} | ||
|
||
pub fn absorb(mut self, input: [Field; S]) -> SafeSponge<L, S> { | ||
assert(self.io_pattern[self.io_count] as u32 == S); | ||
|
||
for i in 0..self.io_pattern[self.io_count] { | ||
if self.absorb_pos == RATE { | ||
self.state = poseidon2_permutation(self.state, self.state.len()); | ||
self.absorb_pos = 0; | ||
} | ||
let pos = self.absorb_pos + CAPACITY; | ||
self.state[pos] = self.state[pos] + input[i]; | ||
self.absorb_pos += 1; | ||
} | ||
self.io_count += 1; | ||
self.squeeze_pos = RATE; | ||
self | ||
} | ||
|
||
pub fn squeeze(mut self) -> Vec<Field> { | ||
for _ in 0..self.io_pattern[self.io_count] { | ||
if self.squeeze_pos == RATE { | ||
self.state = poseidon2_permutation(self.state, self.state.len()); | ||
self.squeeze_pos = 0; | ||
self.absorb_pos = 0; | ||
} | ||
self.out.push(self.state[self.squeeze_pos + CAPACITY]); | ||
self.squeeze_pos += 1; | ||
} | ||
self.io_count += 1; | ||
self.out | ||
} | ||
|
||
pub fn finish(mut self) { | ||
// Clear the state | ||
self.state = [0; WIDTH]; | ||
self.out = Vec::new(); | ||
self.io_count = 0; | ||
self.io_pattern = [0; L]; | ||
self.squeeze_pos = 0; | ||
self.absorb_pos = 0; | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_safe() { | ||
let pattern = [5, 4]; | ||
let mut safe = SafeSponge::start(pattern); | ||
safe = safe.absorb([1, 2, 3, 4, 5]); | ||
let res = safe.squeeze(); | ||
safe.finish(); | ||
assert_eq(res.get(0), 0x0ef061c5b88d5d81e9c7c306701e01e03c5040a3d3c4b051ada3f150b44dc595); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden
9E7A
or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.