Skip to content

Commit

Permalink
Add mimc (#106)
Browse files Browse the repository at this point in the history
Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
Co-authored-by: garyschulte <garyschulte@gmail.com>
  • Loading branch information
matkt and garyschulte authored Mar 31, 2023
1 parent 29021e8 commit 4ee5044
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 5 deletions.
54 changes: 52 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ jobs:
run: |
# secp256k1 dependencies
sudo apt-get update
sudo apt upgrade -y
sudo apt-get install -y autoconf build-essential libtool automake patchelf
sudo apt autoremove -y
# golang dependencies
wget https://go.dev/dl/go1.20.2.linux-amd64.tar.gz
echo "4eaea32f59cde4dc635fbc42161031d13e1c780b87097f4b4234cfce671f1768 go1.20.2.linux-amd64.tar.gz" | sha256sum -c || exit 1
tar -xzf go1.20.2.linux-amd64.tar.gz
export GOROOT=$(pwd)/go
export PATH=$GOROOT/bin:$PATH
# rust dependencies
export CARGO_HOME="$HOME/.cargo"
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.62.1
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.68.2
- name: Checkout Repo
uses: actions/checkout@v3
with:
Expand Down Expand Up @@ -54,6 +62,10 @@ jobs:
with:
name: ipa-multipoint native build artifacts
path: ipa-multipoint/build/
- uses: actions/upload-artifact@v3.1.0
with:
name: mimc native build artifacts
path: mimc/build/
native-build-linux-arm64:
runs-on: [self-hosted, Linux, ARM64]
env:
Expand Down Expand Up @@ -94,6 +106,10 @@ jobs:
with:
name: ipa-multipoint native build artifacts
path: ipa-multipoint/build/
- uses: actions/upload-artifact@v3.1.0
with:
name: mimc native build artifacts
path: mimc/build/
native-build-macos:
runs-on: macos-11
env:
Expand All @@ -103,9 +119,12 @@ jobs:
run: |
# secp256k1 dependencies
brew install autoconf automake libtool
# gnark dependencies
brew install go@1.20 || true
export PATH=/usr/local/Cellar/go/1.20.2/bin:$PATH
# rust dependencies
export CARGO_HOME="$HOME/.cargo"
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.62.1
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.68.2
# install both x86 and arm64 toolchains
export PATH="$HOME/.cargo/bin:$PATH"
rustup target add x86_64-apple-darwin
Expand Down Expand Up @@ -144,11 +163,29 @@ jobs:
with:
name: ipa-multipoint native build artifacts
path: ipa-multipoint/build/
- uses: actions/upload-artifact@v3.1.0
with:
name: mimc native build artifacts
path: mimc/build/
native-build-m1:
runs-on: [self-hosted, macOS, ARM64, MacStadium]
env:
SKIP_GRADLE: true
steps:
- name: Prepare
run: |
# secp256k1, gnark dependencies
brew install autoconf automake libtool
# gnark dependencies
brew install go@1.20 || true
export PATH=/usr/local/Cellar/go/1.20.2/bin:$PATH
# rust dependencies
export CARGO_HOME="$HOME/.cargo"
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.68.2
# install both x86 and arm64 toolchains
export PATH="$HOME/.cargo/bin:$PATH"
rustup target add x86_64-apple-darwin
rustup target add aarch64-apple-darwin
- name: Checkout Repo
uses: actions/checkout@v3
with:
Expand Down Expand Up @@ -183,6 +220,10 @@ jobs:
with:
name: ipa-multipoint native build artifacts
path: ipa-multipoint/build/
- uses: actions/upload-artifact@v3.1.0
with:
name: mimc native build artifacts
path: mimc/build/
final-assembly:
runs-on: ubuntu-20.04
needs:
Expand Down Expand Up @@ -228,6 +269,11 @@ jobs:
with:
name: ipa-multipoint native build artifacts
path: ipa-multipoint/build/
- name: Download mimc
uses: actions/download-artifact@v3
with:
name: mimc native build artifacts
path: mimc/build/
- name: Set up Java
uses: actions/setup-java@v2
with:
Expand Down Expand Up @@ -266,6 +312,10 @@ jobs:
with:
name: jars
path: ipa-multipoint/build/libs
- uses: actions/upload-artifact@v3.1.0
with:
name: jars
path: mimc/build/libs
- name: gradle publish
uses: gradle/gradle-build-action@v2
if: contains('refs/heads/release-', github.ref) || github.ref == 'refs/heads/main'
Expand Down
31 changes: 30 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ if [[ "$OSTYPE" == "linux-gnu" ]]; then
fi

if [[ "$OSTYPE" == "darwin"* ]]; then
export CFLAGS="-arch x86_64 -arch arm64"
CORE_COUNT=$(sysctl -n hw.ncpu)
if [[ "`machine`" == "arm"* ]]; then
arch_name="aarch64"
export CFLAGS="-arch arm64"
else
arch_name="x86-64"
export CFLAGS="-arch x86_64"
fi
OSARCH="darwin-$arch_name"
fi
Expand Down Expand Up @@ -311,13 +312,41 @@ EOF

}

build_mimc() {
cat <<EOF
############################
####### build mimc #######
############################
EOF

cd "$SCRIPTDIR/mimc/mimc-jni"

# delete old build dir, if exists
rm -rf "$SCRIPTDIR/mimc/build" || true
mkdir -p "$SCRIPTDIR/mimc/build/lib"

if [[ "$OSTYPE" == "msys" ]]; then
LIBRARY_EXTENSION=dll
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
LIBRARY_EXTENSION=so
elif [[ "$OSTYPE" == "darwin"* ]]; then
LIBRARY_EXTENSION=dylib
fi

go build -buildmode=c-shared -o libmimc_jni.$LIBRARY_EXTENSION mimc-jni.go

mkdir -p "$SCRIPTDIR/mimc/build/${OSARCH}/lib"
cp libmimc_jni.* "$SCRIPTDIR/mimc/build/${OSARCH}/lib"
}

build_blake2bf
build_secp256k1
build_altbn128
build_arithmetic
build_bls12_381
build_ipa_multipoint
build_secp256r1
build_mimc


build_jars
Expand Down
132 changes: 132 additions & 0 deletions mimc/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright Besu Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

plugins {
id 'java-library'
id 'maven-publish'
id 'com.jfrog.artifactory' version '4.20.0'
}

dependencies {
implementation 'net.java.dev.jna:jna:5.12.1'
testImplementation 'net.java.dev.jna:jna:5.12.1'
implementation 'org.apache.tuweni:tuweni-bytes:2.2.0'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.assertj:assertj-core:3.22.0'
testImplementation 'org.mockito:mockito-core:4.4.0'
}

task macArmLibCopy(type: Copy) {
from 'build/darwin-aarch64/lib/libmimc_jni.dylib'
into 'build/resources/main/darwin-aarch64'
}
processResources.dependsOn macArmLibCopy

task macLibCopy(type: Copy) {
from 'build/darwin-x86-64/lib/libmimc_jni.dylib'
into 'build/resources/main/darwin-x86-64'
}
processResources.dependsOn macLibCopy

task linuxLibCopy(type: Copy) {
from 'build/linux-gnu-x86_64/lib/libmimc_jni.so'
into 'build/resources/main/linux-x86-64'
}
processResources.dependsOn linuxLibCopy

task linuxArm64LibCopy(type: Copy) {
from 'build/linux-gnu-aarch64/lib/libmimc_jni.so'
into 'build/resources/main/linux-aarch64'
}
processResources.dependsOn linuxArm64LibCopy

jar {
archiveBaseName = 'besu-native-mimc'
includeEmptyDirs = false
manifest {
attributes(
'Specification-Title': archiveBaseName,
'Specification-Version': project.version,
'Implementation-Title': archiveBaseName,
'Implementation-Version': project.version,
'Automatic-Module-Name': 'org.hyperledger.besu.native.mimc'
)
}
}

task sourcesJar(type: Jar, dependsOn: classes) {
archiveBaseName = 'besu-native-mimc'
classifier = 'sources'
from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
archiveBaseName = 'besu-native-mimc'
classifier = 'javadoc'
from javadoc.destinationDir
}


publishing {
publications {
mavenJava(MavenPublication) {
groupId "org.hyperledger.besu"
artifactId "mimc"
version "${project.version}"

from components.java
artifact sourcesJar
artifact javadocJar

pom {
name = "Besu Native - ${project.name}"
description = 'Mimc'
url = 'http://github.com/hyperledger/besu-native'
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
scm {
connection = 'scm:git:git://github.com/hyperledger/besu-native.git'
developerConnection = 'scm:git:ssh://github.com/hyperledger/besu-native.git'
url = 'https://github.com/hyperledger/besu-native'
}
}
}
}
}

def artifactoryUser = project.hasProperty('artifactoryUser') ? project.property('artifactoryUser') : System.getenv('ARTIFACTORY_USER')
def artifactoryKey = project.hasProperty('artifactoryApiKey') ? project.property('artifactoryApiKey') : System.getenv('ARTIFACTORY_KEY')
def artifactoryRepo = System.getenv('ARTIFACTORY_REPO') ?: 'besu-maven'
def artifactoryOrg = System.getenv('ARTIFACTORY_ORG') ?: 'hyperledger'

artifactory {
contextUrl = "https://hyperledger.jfrog.io/${artifactoryOrg}"
publish {
repository {
repoKey = artifactoryRepo
username = artifactoryUser
password = artifactoryKey
}
defaults {
publications('mavenJava')
publishArtifacts = true
publishPom = true
}
}
}
10 changes: 10 additions & 0 deletions mimc/mimc-jni/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/hyperledger/besu-native

go 1.18

require github.com/consensys/gnark-crypto v0.9.1

require (
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/sys v0.2.0 // indirect
)
6 changes: 6 additions & 0 deletions mimc/mimc-jni/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/consensys/gnark-crypto v0.9.1 h1:mru55qKdWl3E035hAoh1jj9d7hVnYY5pfb6tmovSmII=
github.com/consensys/gnark-crypto v0.9.1/go.mod h1:a2DQL4+5ywF6safEeZFEPGRiiGbjzGFRUN2sg06VuU4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
27 changes: 27 additions & 0 deletions mimc/mimc-jni/mimc-jni.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main



import "C"
import "unsafe"
import (
"github.com/consensys/gnark-crypto/ecc/bn254/fr/mimc"
)

func MiMCHash(b []byte) []byte {
hasher := mimc.NewMiMC()
hasher.Write(b)
return hasher.Sum(nil)
}


//export compute
func compute(input *C.char, inputLength C.int, output *C.char) C.int {
inputSlice := C.GoBytes(unsafe.Pointer(input), inputLength)
outputSlice := (*[32]byte)(unsafe.Pointer(output))[:]
hash := MiMCHash(inputSlice)
copy(outputSlice, hash)
return C.int(len(hash))
}

func main() {}
Loading

0 comments on commit 4ee5044

Please sign in to comment.