-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add universal pairing accumulator #100
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
Draft
blindchaser
wants to merge
7
commits into
main
Choose a base branch
from
yiren/universal-acc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0a05b6c
feat: add cgo universal accumulator
blindchaser d410f3c
refactor
blindchaser 6b2b948
update ci golang version
blindchaser caa2e66
add relic
blindchaser c8f93a5
fix lint
blindchaser 4281689
Merge branch 'main' into yiren/universal-acc
blindchaser 461f0a5
v3 action
blindchaser 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,6 +1,6 @@ | ||
| module github.com/sei-protocol/sei-db | ||
|
|
||
| go 1.19 | ||
| go 1.22 | ||
|
|
||
| require ( | ||
| github.com/alitto/pond v1.8.3 | ||
|
|
||
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,138 @@ | ||
| package universalaccumulator | ||
|
|
||
| /* | ||
| #cgo linux CFLAGS: -I/usr/local/include -I/usr/include -fopenmp -DRELIC_THREAD | ||
| #cgo linux LDFLAGS: -L/usr/local/lib -L/usr/lib -L/lib/x86_64-linux-gnu | ||
| #cgo linux LDFLAGS: -L/usr/lib/x86_64-linux-gnu -lrelic_s -lssl -lcrypto -lgmp -fopenmp | ||
| #cgo darwin,arm64 CFLAGS: -I/opt/homebrew/include -I/opt/homebrew/opt/libomp/include | ||
| #cgo darwin,arm64 CFLAGS: -I/usr/local/include/relic -I/usr/local/include -DRELIC_THREAD | ||
| #cgo darwin,arm64 CFLAGS: -I/opt/homebrew/opt/openssl@3/include -I/opt/homebrew/opt/gmp/include | ||
| #cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/lib -L/opt/homebrew/opt/libomp/lib | ||
| #cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/openssl@3/lib -L/opt/homebrew/opt/gmp/lib | ||
| #cgo darwin,arm64 LDFLAGS: -L/usr/local/lib -lrelic_s -lssl -lcrypto -lgmp -lomp | ||
| #cgo darwin,amd64 CFLAGS: -I/usr/local/include -I/opt/homebrew/include -DRELIC_THREAD | ||
| #cgo darwin,amd64 CFLAGS: -I/opt/homebrew/opt/libomp/include -I/usr/local/opt/openssl@3/include -I/usr/local/opt/gmp/include | ||
| #cgo darwin,amd64 LDFLAGS: -L/usr/local/lib -L/opt/homebrew/lib -L/usr/local/opt/openssl@3/lib -L/usr/local/opt/gmp/lib -lrelic_s -lssl -lcrypto -lgmp -lomp | ||
| #cgo darwin,amd64 LDFLAGS: -L/opt/homebrew/opt/libomp/lib | ||
| #cgo !linux,!darwin CFLAGS: -I/opt/homebrew/include -I/usr/local/include -I/usr/include | ||
| #cgo !linux,!darwin CFLAGS: -I/opt/homebrew/opt/libomp/include | ||
| #cgo !linux,!darwin LDFLAGS: -L/opt/homebrew/lib -L/usr/local/lib -L/usr/lib -lrelic_s -lssl -lcrypto -lgmp -lomp | ||
|
|
||
| #include "universal_accumulator.h" | ||
| */ | ||
| import "C" | ||
|
|
||
| import ( | ||
| "errors" | ||
| "unsafe" | ||
| ) | ||
|
|
||
| // Go wrapper functions for C functions. | ||
|
|
||
| func addHashedElementsWrapper(accumulator unsafe.Pointer, flatHashes []byte, count int) error { | ||
| if count == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| // Use C.add_hashed_elements directly | ||
| ret := C.add_hashed_elements((*C.t_state)(accumulator), (*C.uchar)(unsafe.Pointer(&flatHashes[0])), C.int(count)) | ||
| if ret != 0 { | ||
| return errors.New("add_hashed_elements failed, possibly due to memory allocation error") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func batchDelHashedElementsWrapper(accumulator unsafe.Pointer, flatHashes []byte, count int) error { | ||
| if count <= 0 { | ||
| return nil | ||
| } | ||
|
|
||
| // Use the existing batch_del_hashed_elements C function | ||
| ret := C.batch_del_hashed_elements((*C.t_state)(accumulator), (*C.uchar)(unsafe.Pointer(&flatHashes[0])), C.int(count)) | ||
| if ret != 0 { | ||
| return errors.New("batch_del_hashed_elements failed, possibly due to memory allocation error") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func calculateRootWrapper(accumulator unsafe.Pointer, buffer []byte) int { | ||
| acc := (*C.t_state)(accumulator) | ||
| return int(C.calculate_root(acc, | ||
| (*C.uchar)(unsafe.Pointer(&buffer[0])), | ||
| C.int(len(buffer)))) | ||
| } | ||
|
|
||
| func freeAccumulatorWrapper(accumulator unsafe.Pointer) { | ||
| if accumulator != nil { | ||
| C.destroy_accumulator((*C.t_state)(accumulator)) | ||
| } | ||
| } | ||
|
|
||
| func createAccumulator() (unsafe.Pointer, error) { | ||
| accumulator := C.malloc(C.size_t(unsafe.Sizeof(C.t_state{}))) | ||
| if accumulator == nil { | ||
| return nil, errors.New("failed to allocate accumulator memory") | ||
| } | ||
|
|
||
| C.init((*C.t_state)(accumulator)) | ||
| return accumulator, nil | ||
| } | ||
|
|
||
| // getAccumulatorFactor retrieves the serialized big-endian bytes of fVa from C. | ||
| func getAccumulatorFactor(accumulator unsafe.Pointer) ([]byte, error) { | ||
| cAcc := (*C.t_state)(accumulator) | ||
| // Probe size: passing NULL buffer returns negative required size | ||
| required := C.get_fva(cAcc, (*C.uchar)(nil), C.int(0)) | ||
| if required == 0 { | ||
| return nil, errors.New("unexpected fva size 0") | ||
| } | ||
| var size int | ||
| if required < 0 { | ||
| size = -int(required) | ||
| } else { | ||
| size = int(required) | ||
| } | ||
| buf := make([]byte, size) | ||
| written := C.get_fva(cAcc, (*C.uchar)(unsafe.Pointer(&buf[0])), C.int(size)) | ||
| if written < 0 { | ||
| return nil, errors.New("failed to get fva") | ||
| } | ||
| return buf[:int(written)], nil | ||
| } | ||
|
|
||
| // setAccumulatorStateFromFactor sets fVa and recomputes V/eVPt in C. | ||
| func setAccumulatorStateFromFactor(accumulator unsafe.Pointer, factor []byte) error { | ||
| if len(factor) == 0 { | ||
| return errors.New("empty factor") | ||
| } | ||
| cAcc := (*C.t_state)(accumulator) | ||
| ret := C.set_state_from_factor(cAcc, (*C.uchar)(unsafe.Pointer(&factor[0])), C.int(len(factor))) | ||
| if ret != 0 { | ||
| return errors.New("set_state_from_factor failed") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // generateWitnessWrapper generates a witness for a given element hash. | ||
| func generateWitnessWrapper(accumulator unsafe.Pointer, elementHash []byte) ([]byte, error) { | ||
| cAccumulator := (*C.t_state)(accumulator) | ||
|
|
||
| if len(elementHash) != 32 { | ||
| return nil, errors.New("element hash must be 32 bytes") | ||
| } | ||
|
|
||
| // Generate witness using C function | ||
| witness := C.issue_witness_from_hash(cAccumulator, (*C.uchar)(unsafe.Pointer(&elementHash[0])), C.bool(true)) | ||
| if witness == nil { | ||
| return nil, errors.New("failed to generate witness") | ||
| } | ||
| defer C.destroy_witness(witness) | ||
|
|
||
| // For simplicity, return the element hash as witness | ||
| // In a full implementation, this would serialize the actual witness structure | ||
| result := make([]byte, 32) | ||
| copy(result, elementHash) | ||
| return result, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.